package org.ts.ddcs.Worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.DataTemplate.TemplateLineNode;
import org.ts.ddcs.Entity.RtuStatusEntity;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.utils.GBConstants;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;


@SuppressWarnings("all")
public class RtuStatusInfoRelayProcess implements Callable<Integer> {
    private TemplateLineNode _templateLineNode;
    private Date _pickDt;
    private String _datagram;


    RtuStatusInfoRelayProcess(Date pickDt, TemplateLineNode templateLineNode, String datagram) {
        this._pickDt = pickDt;
        this._templateLineNode = templateLineNode;
        this._datagram = datagram;
    }

    @Override
    public Integer call() throws Exception {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            DataTemplateInterface dataTemplateInterface = _templateLineNode.getDataTemplate();
            RtuStatusEntity statusEntity = new RtuStatusEntity();
            String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
            statusEntity.setStcd(stcd);
            statusEntity.setPickTime(new Timestamp(_pickDt.getTime()));
            byte[] collectBuff = dataTemplateInterface.CollectTime();
            if (null != collectBuff && collectBuff.length > 0) {
                String str = "20" + BytesHelp.byte2HexStr(collectBuff) + "00";
                statusEntity.setCollectTime(new Timestamp(timeFormat.parse(str).getTime()));
            }
            byte[] sendBuff = dataTemplateInterface.SendPacketTime();
            if (null != sendBuff && sendBuff.length > 0) {
                String str = "20" + BytesHelp.byte2HexStr(sendBuff);
                statusEntity.setSendDatagramTime(new Timestamp(timeFormat.parse(str).getTime()));
            }
            String func = dataTemplateInterface.GetFeture();
            if (func.equals("F5")) {
                List<DataItemTemplateInterface> items = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
                if (null != items && items.size() > 0) {
                    for (DataItemTemplateInterface itemTemplateInterface : items) {
                        itemTemplateInterface.SetEntity(statusEntity);
                    }
                }
            } else {
                if (func.equals("30") || func.equals("32") || func.equals("33") || func.equals("34")) {
                    LinkedList<DataItemTemplateInterface> dataItem = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            if (item.GetFeture().equals("45")) {
                                Map<String, Object> datas = item.GetDatas();
                                if (datas.containsKey(GBConstants.RTU_STATUS_INFO_NAME_WSE)) {//水位计
                                    String waterSE = (String) datas.get(GBConstants.RTU_STATUS_INFO_NAME_WSE);
                                    statusEntity.setWaterSe(waterSE);
                                }
                                if (datas.containsKey(GBConstants.RTU_STATUS_INFO_NAME_ACC)) {//交流电
                                    String alternatingCurrentCharging = (String) datas.get(GBConstants.RTU_STATUS_INFO_NAME_ACC);
                                    statusEntity.setAcZt(alternatingCurrentCharging);
                                }
                                if (datas.containsKey(GBConstants.RTU_STATUS_INFO_NAME_BV)) {//电池电压
                                    String batteryVoltage = (String) datas.get(GBConstants.RTU_STATUS_INFO_NAME_BV);
                                    statusEntity.setBatteryVoltage(batteryVoltage);
                                }
                            } else if (item.GetFeture().equals("38")) {//电源电压
                                statusEntity.setVoltage(item.GetDecimal());
                            }
                        }
                    }
                }
            }

            String body = JSON.toJSONString(statusEntity);
            String checkStateQueue = DataProcessHolder.newInstance().dataProcess.queueConfig.getCheckStateQueue();
            DataProcessHolder.newInstance().dataProcess.kafkaTemplate.send(checkStateQueue,body);

        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
