package org.ts.ddcs.DataItemTemplate;

import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;

/**
 * 0x45
 * 遥测站状态及报警信息
 */
@SuppressWarnings("all")
public class DataItemTemplateZT extends DataItemTemplate {


    @Override
    public byte[] Stream(byte[] data) {
        byte[] remain = data;
        try {
            if (this.macth) {
                return remain;
            }
            int count = 0;
            String flag = BytesHelp.byte2HexStr(remain, count, 1);
            count += 1;
            if (flag.equals(this.feture)) {
                byte[] len = BytesHelp.subBytes(remain, count, 1);
                count += 1;
                int dataTotalLen = len[0] >> 3;
                int decimalLen = len[0] & 7;
                byte[] buff = BytesHelp.subBytes(remain, count, dataTotalLen);
                count += dataTotalLen;
                String hexStr = BytesHelp.byte2HexStr(buff);
                String binStr = BytesHelp.hexString2binaryString(hexStr);
                int bininxdex = 31;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("交流电充电状态:停电");
//                } else {
//                    LogHelper.info("交流电充电状态 正常");
//                }
                this.datas.put("alternatingCurrentCharging", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("蓄电池电压状态 电压低");
//                } else {
//                    LogHelper.info("蓄电池电压状态 正常");
//                }
                this.datas.put("batteryVoltage", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("水位超限报警状态 报警");
//                } else {
//                    LogHelper.info("水位超限报警状态 正常");
//                }
                this.datas.put("waterLevelWarning", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("流量超限报警状态 报警");
//                } else {
//                    LogHelper.info("流量超限报警状态 正常");
//                }
                this.datas.put("flowWarning", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("水质超限报警状态 报警");
//                } else {
//                    LogHelper.info("水质超限报警状态 正常");
//                }
                this.datas.put("waterQaWarning", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("流量仪表状态 故障");
//                } else {
//                    LogHelper.info("流量仪表状态 正常");
//                }
                this.datas.put("waterFlowDeviceStatsu", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("水位仪表状态 故障");
//                } else {
//                    LogHelper.info("水位仪表状态 正常");
//                }
                this.datas.put("waterLevelDeviceStatus", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("终端箱门状态 关闭");
//                } else {
//                    LogHelper.info("终端箱门状态 开启");
//                }
                this.datas.put("waterDoorStatus", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("存储器状态 异常");
//                } else {
//                    LogHelper.info("存储器状态 正常");
//                }
                this.datas.put("memoryStatus", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("IC 卡功能 有效");
//                } else {
//                    LogHelper.info("IC 卡功能 关闭");
//                }
                this.datas.put("icCard", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("水泵工作状态 停机");
//                } else {
//                    LogHelper.info("水泵工作状态 工作");
//                }
                this.datas.put("waterPump", binStr.substring(bininxdex, bininxdex + 1));
                bininxdex -= 1;
//                if (binStr.substring(bininxdex, bininxdex + 1).equals("1")) {
//                    LogHelper.info("剩余水量报警 超限");
//                } else {
//                    LogHelper.info("剩余水量报警 未超限");
//                }
                this.datas.put("waterYield", binStr.substring(bininxdex, bininxdex + 1));
                remain = BytesHelp.subBytes(remain, count, remain.length - count);
                this.macth = true;
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return remain;
    }

    @Override
    public String toString() {
        String showInfo = "";
        if (this.datas.containsKey("alternatingCurrentCharging")) {
            String value = (String) this.datas.get("alternatingCurrentCharging");
            if (value.equals("1")) {
                showInfo += "交流电充电状态:停电; ";
            } else {
                showInfo += "交流电充电状态 正常; ";
            }
        }

        if (this.datas.containsKey("batteryVoltage")) {
            String value = (String) this.datas.get("batteryVoltage");
            if (value.equals("1")) {
                showInfo += "蓄电池电压状态 电压低; ";
            } else {
                showInfo += "蓄电池电压状态 正常; ";
            }
        }

        if (this.datas.containsKey("waterLevelWarning")) {
            String value = (String) this.datas.get("waterLevelWarning");
            if (value.equals("1")) {
                showInfo += "水位超限报警状态 报警; ";
            } else {
                showInfo += "水位超限报警状态 正常; ";
            }
        }

        if (this.datas.containsKey("flowWarning")) {
            String value = (String) this.datas.get("flowWarning");
            if (value.equals("1")) {
                showInfo += "流量超限报警状态 报警; ";
            } else {
                showInfo += "流量超限报警状态 正常; ";
            }
        }

        if (this.datas.containsKey("waterQaWarning")) {
            String value = (String) this.datas.get("waterQaWarning");
            if (value.equals("1")) {
                showInfo += "水质超限报警状态 报警; ";
            } else {
                showInfo += "水质超限报警状态 正常; ";
            }
        }


        if (this.datas.containsKey("waterFlowDeviceStatsu")) {
            String value = (String) this.datas.get("waterFlowDeviceStatsu");
            if (value.equals("1")) {
                showInfo += "流量仪表状态 故障; ";
            } else {
                showInfo += "流量仪表状态 正常; ";
            }
        }

        if (this.datas.containsKey("waterLevelDeviceStatus")) {
            String value = (String) this.datas.get("waterLevelDeviceStatus");
            if (value.equals("1")) {
                showInfo += "水位仪表状态 故障; ";
            } else {
                showInfo += "水位仪表状态 正常; ";
            }
        }

        if (this.datas.containsKey("waterDoorStatus")) {
            String value = (String) this.datas.get("waterDoorStatus");
            if (value.equals("1")) {
                showInfo += "终端箱门状态 关闭; ";
            } else {
                showInfo += "终端箱门状态 开启; ";
            }
        }

        if (this.datas.containsKey("memoryStatus")) {
            String value = (String) this.datas.get("memoryStatus");
            if (value.equals("1")) {
                showInfo += "存储器状态 异常; ";
            } else {
                showInfo += "存储器状态 正常; ";
            }
        }
        if (this.datas.containsKey("icCard")) {
            String value = (String) this.datas.get("icCard");
            if (value.equals("1")) {
                showInfo += "IC 卡功能 有效; ";
            } else {
                showInfo += "IC 卡功能 关闭; ";
            }
        }
        if (this.datas.containsKey("waterPump")) {
            String value = (String) this.datas.get("waterPump");
            if (value.equals("1")) {
                showInfo += "水泵工作状态 停机; ";
            } else {
                showInfo += "水泵工作状态 工作; ";
            }
        }
        if (this.datas.containsKey("waterYield")) {
            String value = (String) this.datas.get("waterYield");
            if (value.equals("1")) {
                showInfo += "剩余水量报警 超限; ";
            } else {
                showInfo += "剩余水量报警 未超限; ";
            }
        }

        return showInfo;
    }
}
