package com.mvc.service.aiiden;

import com.mvc.forwarmana.ZHYServerThermalMonitor;
import com.mvc.model.TAireadModel;
import com.mvc.netty.SimpleResponse;
import com.mvc.service.tcpsocket.TcpClient;
import com.mvc.utils.StoreBlockingQueue;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SystemReviewPushThread implements Runnable {
    private static Logger log = LoggerFactory.getLogger(SystemReviewPushThread.class);

    @Resource
    private SimpleResponse simpleResponse;

    @Resource
    private TcpClient tcpClient;

    @Override
    public void run() {
        while (true) {
            try {
                TAireadModel aireadModel = StoreBlockingQueue.sysReviewQueue.take();
                if (aireadModel.getResultValue() == null) {
                    continue;
                }

                String content;
                //机房温度监测的
                if (ZHYServerThermalMonitor.TARGET_DEVICE_SNS.contains(aireadModel.getDeviceSn())) {
                    content = simpleResponse.generatefor_content_sls_temperature(aireadModel);
                } else {//常规推送的
                    content = simpleResponse.generatefor_content_sls(aireadModel);
                }

                if (StringUtils.isEmpty(content)) {
                    log.info("转发水利所:生成内容报文异常[ID]:" + aireadModel.getStationCode());
                    continue;
                }

                String msg = simpleResponse.generatefor_sls_send(aireadModel, content);
                if (StringUtils.isNotEmpty(msg)) {
                    tcpClient.SendMessageToSlsServer(msg, aireadModel);
                    log.info("系统向水利所补发数据[ID]:" + aireadModel.getStationCode());
                } else {
                    log.info("数据中有无效值,不推送[ID]:" + aireadModel.getStationCode());
                }

                Thread.sleep(10);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                e.printStackTrace();
            }
        }
    }
}
