/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2021/12/16
 */
package org.ts.ddcs.dataStream.processor.szy;


import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.enums.SzyElementCodeEnum;
import org.ts.ddcs.common.metadata.ElementDataMetadataEnum;
import org.ts.ddcs.common.metadata.JsonPackMetadataEnum;
import org.ts.ddcs.enums.SzyDatagramTypeCodeEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataStream.DataStreamCache;
import org.ts.ddcs.dataStream.DataStreamLineManager;
import org.ts.ddcs.dataStream.DataStreamProcessor;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/***
 * Date:2021/12/16
 * Title:水资源规约数据处理器模块
 * Description：水资源 查询遥测终端的数据自报种类及时间间隔 0x53
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSzyReadUpModelDatagramProcessor extends DataStreamProcessor {

    private DataStreamCache dataStreamCache;
    private Map<String, List<Map<String, Object>>> keyStore = new HashMap<>();

    @Override
    public void init(DataStreamLineManager context) {
        this.context = context;
        this.dataStreamCache = context.getCache();
    }

    @Override
    public void process(DataStreamAdapter dataStreamAdapter) {
        LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0);
        this.dataStreamCache.setState(TestState.MATHCE);
        this.dataStreamCache.setMatchProcessor(this);
        String datagramCode = this.dataStreamCache.getDatagramCode();
        if (SzyDatagramTypeCodeEnum.DATAGRAM_CODE_53.getName().equals(datagramCode)) {
            byte[] dataArea = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_DATA_AREA.getName());
            int count = 0;
            byte[] buff = BytesHelp.subBytes(dataArea, count, 2);
            byte tmpByte = buff[0];
            buff[0] = buff[1];
            buff[1] = tmpByte;
            String binStr = BytesHelp.BytesToBinStr(buff);
            binStr = new StringBuilder(binStr).reverse().toString();
            count += 2;
            List<Map<String, Object>> list = new ArrayList<>();
            for (int i = 0; i < 14; i++) {
                if (binStr.substring(i, i + 1).equals("1")) {
                    Map<String, Object> data = new HashMap<>();
                    data.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), SzyElementCodeEnum.CODE_COUNT_RAIN.getCode());
                    data.put(ElementDataMetadataEnum.ELEMENT_METADATA_NAME.getName(), "szy_intervalTime_setting");
                    data.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), String.format("%d", i));
                    if (0 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报雨量数据");
                    } else if (1 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报水位数据");
                    } else if (2 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报流量（水量）数据");
                    } else if (3 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报流速数据");
                    } else if (4 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报闸位数据");
                    } else if (5 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报功率数据");
                    } else if (6 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报气压数据");
                    } else if (7 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "出现上报风速（风向）数据");
                    } else if (8 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报水温数据");
                    } else if (9 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报水质数据");
                    } else if (10 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报土壤含水率数据");
                    } else if (11 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报蒸发量数据");
                    } else if (12 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报报警或状态数据");
                    } else if (13 == i) {
                        data.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), "主动上报水压数据");
                    }

                    data.put(ElementDataMetadataEnum.ELEMENT_METADATA_UNIT.getName(), "");

                    byte[] valueBuff = BytesHelp.subBytes(dataArea, count, 2);
                    tmpByte = valueBuff[0];
                    valueBuff[0] = valueBuff[1];
                    valueBuff[1] = tmpByte;
                    String value = BytesHelp.byte2HexStr(valueBuff);
                    data.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), Integer.valueOf(value).toString());

                    list.add(data);
                }
                count += 2;
            }
            this.keyStore.put(SzyElementCodeEnum.CODE_COUNT_RAIN.getCode(), list);
            //发报时间
            DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String st = now.format(timeFormat);
            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName(), st);
            //采集时间
            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), st);
            this.next(false);
        } else {
            this.next(true);
        }
    }

    @Override
    public void close(DataStreamAdapter dataStreamAdapter) {

    }

    @Override
    public void batchComplete(DataStreamAdapter dataStreamAdapter) {
        log.info("接收到水资源 53 查询遥测终端的数据自报种类及时间间隔");

    }

    @Override
    public List<Map<String, Object>> sink(DataStreamAdapter dataStreamAdapter) {
        List<Map<String, Object>> datas = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        data.put(JsonPackMetadataEnum.PACK_METADATA_DATAGRAM.getName(), BytesHelp.byte2HexStr(this.dataStreamCache.getDatagramBuff()));
        data.put(JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
        data.put(JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
        datas.add(data);
        return datas;
    }
}
