package com.ssj.conch.util;

import com.ssj.framework.basic.utils.EncryptUtil;
import com.ssj.framework.core.security.exception.ConchException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;


/** 
 * @author  ZhangCaibao 
 * @version 2018年4月11日 下午3:07:08 
 * 类说明 
 */
public class ConchVerify {
	private static Logger logger = LoggerFactory.getLogger(ConchVerify.class);
	  // 小塾相机验签
    public static  void verify(String derver, String signStr, StringBuffer dataStr) throws ConchException{
    	logger.info("小塾相机验证，参数：设备号:{}, 加密信息：{}，组合信息：{}", derver, signStr, dataStr.toString());
        String sign = EncryptUtil.getMD5String(dataStr.toString());
        if (!signStr.equalsIgnoreCase(sign)) {
        	String message = String.format("derver加密验证失败 [%s] is invalid", derver);
            throw new ConchException(message);
        }
    }
    // 小塾相机时间验证
    public static void verifyTime(Long currentTime) throws ConchException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -2);
        Long startTime = calendar.getTimeInMillis();
        calendar.add(Calendar.MINUTE, 4);
        Long endTime = calendar.getTimeInMillis();
        //logger.info("时间验证，参数：上传时间:{}, 系统时间前：{}，系统时间后：{}", currentTime, startTime, endTime);
        if (startTime > currentTime || endTime < currentTime) {
            throw new ConchException("请求时间无效");
        }
    }
}
