package com.mvc.forwarmana;

import com.mvc.model.TAireadModel;
import com.mvc.utils.*;
import org.apache.commons.lang3.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 智慧院机房温度监控
 * 这是一个非常规业务类,需要将机房的两台带传感器设备数据转发至智慧院
 */
@Component
public class ZHYServerThermalMonitor {
    private Logger log = LoggerFactory.getLogger(ZHYServerThermalMonitor.class);

    @Resource
    private NotifyUtil notifyUtil;

    //需要监控的目标设备sn
    public final static List<String> TARGET_DEVICE_SNS = Arrays.asList("25019900001", "24090300054");

    //接收通知的邮箱地址
    private static final String NOTIFICATION_RECIPIENT_EMAIL_ADDRESSES = "870892757@qq.com;13810064304@163.com";//

    //短信套餐到期了,不能用
    //腾讯云短信模板id
    //private final static String SMS_TEMPLATEID = "2387847";
    //通知接收人手机号
    //public static final String[] NOTIFICATION_RECIPIENT_MOBILE_NUMBERS = new String[]{"15097911597"};

    public void handleData(TAireadModel aireadModel) {
        if (!TARGET_DEVICE_SNS.contains(aireadModel.getDeviceSn())) {
            return;
        }

        if (aireadModel.getTemperature() == null) {
            log.info("{} 温度数据为空", aireadModel.getDeviceSn());
            return;
        }
        //因为要改数据(两台无摄像无识别值,直接将温度给到识别值),所以要先拷贝一份
        TAireadModel copyModel = SerializationUtils.clone(aireadModel);

        copyModel.setRawValue(copyModel.getTemperature());
        copyModel.setIdentValue(copyModel.getTemperature());
        copyModel.setResultValue(copyModel.getTemperature());

        //1.先向智慧院转发
        try {
            StoreBlockingQueue.sysReviewQueue.put(copyModel);
        } catch (Exception e) {
            log.error("{} 智慧院温度监控加入转发队列异常", aireadModel.getDeviceSn());
            e.printStackTrace();
        }

        //低于30度不通知
        if (copyModel.getTemperature().compareTo(new BigDecimal(30)) < 0) {
            if (copyModel.getVoltage().compareTo(new BigDecimal(3.4)) < 0) {
                try {
                    //2.邮件通知
                    String subject = "智慧院机房温度监控设备电量低";
                    String content = String.format("当前电压:%s V,请及时更换!", copyModel.getVoltage());
                    notifyUtil.sendEMail("870892757@qq.com", subject, content);//只通知此地址
                } catch (Exception e) {
                    log.error("{} 智慧院温度邮件通知异常", aireadModel.getDeviceSn());
                    e.printStackTrace();
                }
            }
            return;
        }

        try {
            //2.邮件通知
            String subject = "智慧院机房温度监控报警";
            String content = String.format("当前温度温度:%s ℃,请及时处理!", copyModel.getTemperature());
            notifyUtil.sendEMail(NOTIFICATION_RECIPIENT_EMAIL_ADDRESSES, subject, content);
        } catch (Exception e) {
            log.error("{} 智慧院温度邮件通知异常", aireadModel.getDeviceSn());
            e.printStackTrace();
        }

        /*try {
            //2.短信通知(短信套餐过期了)
            String[] templateParamSet = new String[]{aireadModel.getTemperature().intValue() + ""};
            SmsUtil.sendSms(NOTIFICATION_RECIPIENT_MOBILE_NUMBERS, templateParamSet, SMS_TEMPLATEID, null);
        } catch (Exception e) {
            log.error("{} 智慧院温度短信通知异常", aireadModel.getDeviceSn());
            e.printStackTrace();
        }*/
    }
}
