package org.ts.ddcs.DataTemplate;

import org.ts.ddcs.DataItemTemplate.DataItemTemplateSzy2012.*;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@SuppressWarnings("all")
public class DataTemplateSzy2012C81 extends DataTemplateSzy2012 {

    /**
     * 采集时间
     */
    private byte[] collectTime;

    /**
     * 发报时间
     */
    private byte[] sendPacketTime;

    /**
     * 接收时间
     */
    private byte[] receiveTime;

    /*
     * GZ 功能码
     */

    /**
     * 水质参数
     */
    public final static int CONTROL_DIR0_FN_REPORT_WATERQUALITY = 10;


    /**
     * 流速参数
     */
    public final static int CONTROL_DIR0_FN_REPORT_FLOW = 4;


    public final static int CONTROL_DIR0_FN_WATER_HIGH = 2;

    public final static int CONTROL_DIR0_FN_FLOW_RATE = 3;


    @Override
    public byte[] CollectTime() {
        return this.collectTime;
    }

    @Override
    public byte[] SendPacketTime() {
        return this.sendPacketTime;
    }

    @Override
    public byte[] ReceiveTime() {
        return this.receiveTime;
    }

    @Override
    public void Stream() {
        try {
            if (dataLen > 0) {

                dataItem = new HashMap<>();
                int count = 0;
                byte[] warningKind = BytesHelp.subBytes(this.dataArea, 0, 2);
                byte tmpbyte = warningKind[0];
                warningKind[0] = warningKind[1];
                warningKind[1]=tmpbyte;
                count += 2;
                String binStr = BytesHelp.BytesToBinStr(warningKind);
                binStr = new StringBuilder(binStr).reverse().toString();
                int index = 0;
                byte[] waring = BytesHelp.subBytes(this.dataArea, this.dataArea.length - 9, 2);

                ItemTemplateSet template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("power");
                template.setTable("t_szy_warining_info");
                template.setDesc("工作交流电停电告警");
                DataItemTemplateC0_WarningInfo warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //工作交流电停电告警
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("bt");
                template.setTable("t_szy_warining_info");
                template.setDesc("蓄电池电压报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //水位超限报警
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);

                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("water");
                template.setTable("t_szy_warining_info");
                template.setDesc("水位超限报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //水位超限报警
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("flow");
                template.setTable("t_szy_warining_info");
                template.setDesc("流量超限报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //流量超限报警
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("water_qa");
                template.setTable("t_szy_warining_info");
                template.setDesc("水质超限报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //水质超限报警；
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("flow_device");
                template.setTable("t_szy_warining_info");
                template.setDesc("流量仪表故障报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //流量仪表故障报警
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("water_pump");
                template.setTable("t_szy_warining_info");
                template.setDesc("水泵开停状态");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //水泵开停状态
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("water_level");
                template.setTable("t_szy_warining_info");
                template.setDesc("水位仪表故障报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //水位仪表故障报警
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("water_p");
                template.setTable("t_szy_warining_info");
                template.setDesc("水压超限报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                //水压超限报警
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                //备用
                if (binStr.substring(index, index + 1).equals("1")) {

                }
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("ic");
                template.setTable("t_szy_warining_info");
                template.setDesc("终端IC卡功能报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("control");
                template.setTable("t_szy_warining_info");
                template.setDesc("定值控制报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    LogHelper.info("定值控制报警" + "报警");
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("status");
                template.setTable("t_szy_warining_info");
                template.setDesc("剩余水量的下限报警");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("door");
                template.setDesc("终端箱门状态报警");
                template.setTable("t_szy_warining_info");
                warningInfo = new DataItemTemplateC0_WarningInfo();
                warningInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    warningInfo.SetVarchar("报警");
                }else {
                    warningInfo.SetVarchar("正常");
                }
                dataItem.put(template.getColumn(), warningInfo);

                byte[] statusKind = BytesHelp.subBytes(this.dataArea, count, 2);
                tmpbyte = statusKind[0];
                statusKind[0] = statusKind[1];
                statusKind[1]=tmpbyte;

                count += 2;
                binStr = BytesHelp.BytesToBinStr(statusKind);
                binStr = new StringBuilder(binStr).reverse().toString();


                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("work_status");
                template.setTable("t_szy_status_info");
                template.setDesc("工作状态");
                index = 0;
                DataItemTemplateC0_RtuStatus statusInfo = new DataItemTemplateC0_RtuStatus();
                statusInfo.Init(template);
                byte workType = (byte) (statusKind[1] & 0x03);
                if (workType == 0x00) {
                    //终端在自报、遥测工作状态
                    statusInfo.SetVarchar("终端在自报、遥测工作状态");
                } else if (workType == 0x01) {
                    //终端在自报确认工作状态
                    statusInfo.SetVarchar("终端在自报确认工作状态");
                } else if (workType == 0x02) {
                    //终端在遥测工作状态
                    statusInfo.SetVarchar("终端在遥测工作状态");
                } else if (workType == 0x03) {
                    //终端在调试或维修状态
                    statusInfo.SetVarchar("终端在调试或维修状态");
                }
                dataItem.put(template.getColumn(), statusInfo);

                index += 2;

                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("ic_status");
                template.setTable("t_szy_status_info");
                template.setDesc("终端IC卡功能是否有效");
                statusInfo = new DataItemTemplateC0_RtuStatus();
                statusInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    statusInfo.SetVarchar("有效");
                } else {
                    statusInfo.SetVarchar("无效");
                }
                dataItem.put(template.getColumn(), statusInfo);

                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("value_status");
                template.setTable("t_szy_status_info");
                template.setDesc("定值控制是否投入");
                statusInfo = new DataItemTemplateC0_RtuStatus();
                statusInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    statusInfo.SetVarchar("投入");
                } else {
                    statusInfo.SetVarchar("退出");
                }
                dataItem.put(template.getColumn(), statusInfo);

                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("water_pump_status");
                template.setTable("t_szy_status_info");
                template.setDesc("水泵工作状态");
                statusInfo = new DataItemTemplateC0_RtuStatus();
                statusInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    statusInfo.SetVarchar("停止");
                } else {
                    statusInfo.SetVarchar("启动");
                }
                dataItem.put(template.getColumn(), statusInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("door_status");
                template.setTable("t_szy_status_info");
                template.setDesc("终端箱门状态报警");
                statusInfo = new DataItemTemplateC0_RtuStatus();
                statusInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    statusInfo.SetVarchar("关闭");
                } else {
                    statusInfo.SetVarchar("开启");
                }
                dataItem.put(template.getColumn(), statusInfo);
                index += 1;
                template = new ItemTemplateSet();
                template.setFeature("81");
                template.setColumn("power_status");
                template.setTable("t_szy_status_info");
                template.setDesc("电源工作状态");
                statusInfo = new DataItemTemplateC0_RtuStatus();
                statusInfo.Init(template);
                if (binStr.substring(index, index + 1).equals("1")) {
                    statusInfo.SetVarchar("蓄电池供电");
                } else {
                    statusInfo.SetVarchar("AC220V供电");
                }
                dataItem.put(template.getColumn(), statusInfo);

                /**观测时间*/
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String dt = timeFormat.format(new Date());
                this.sendPacketTime = BytesHelp.hexStr2Bytes(dt.substring(2));
                this.receiveTime = BytesHelp.hexStr2Bytes(dt.substring(2));
                byte[] tm = BytesHelp.subBytes(this.dataArea, this.dataArea.length - 5, 5);
                count += 5;
                byte tmp = tm[0];
                tm[0] = tm[4];
                tm[4] = tmp;
                tmp = tm[1];
                tm[1] = tm[3];
                tm[3] = tmp;
                this.collectTime = BytesHelp.arrayApend(BytesHelp.subBytes(BytesHelp.hexStr2Bytes(dt), 1, 1), tm);

                if (count != this.dataArea.length) {
                    LogHelper.info("******************* C0 DATA ERROR******************* ");
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
