/**
 * 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.common.dict.BaseSettingFlag;
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.util.*;

/***
 * Date:2021/12/16
 * Title:水资源规约数据处理器模块
 * Description：水资源 查询基本配置信息 0x87
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSzyReadBaseSettingDatagramProcessor extends DataStreamProcessor {

    private DataStreamCache dataStreamCache;

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

    @Override
    public void process(DataStreamAdapter dataStreamAdapter) {
        this.dataStreamCache.setState(TestState.MATHCE);
        this.dataStreamCache.setMatchProcessor(this);
        String datagramCode = this.dataStreamCache.getDatagramCode();
        if (SzyDatagramTypeCodeEnum.DATAGRAM_CODE_87.getName().equals(datagramCode)) {
            byte[] dataArea = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_DATA_AREA.getName());
            int count = 0;
//                01  通讯方式
            count += 1;
//                00  是否招测
            count += 1;
//                08 发送起始时间
            count += 1;
//                18 定时发送次数
            count += 1;
//                05 发送时间间隔
            count += 1;
//                02  //发送服务器个数
            count += 1;
//                434D4E4554002020202020202020202020202020 20字节  APN接入点
            count += 20;
//                7573657200202020202020202020202020202020 20字节  APN用户名
            count += 20;
//                70617373776F7264002020202020202020202020 20字节 APN密码
            count += 20;
//                3131312E3230342E3232382E323237   服务器1IP地址
            byte[] ip1buff = BytesHelp.subBytes(dataArea, count, 15);
            count += 15;
//                3131312E3230342E3232382E323237   服务器2IP地址
            byte[] ip2buff = BytesHelp.subBytes(dataArea, count, 15);
            count += 15;
//                3231392E3233322E3131372E330303
            byte[] ip3buff = BytesHelp.subBytes(dataArea, count, 15);
            count += 15;
//                3231392E3233322E3131372E303838 //服务器4IP地址
            byte[] ip4buff = BytesHelp.subBytes(dataArea, count, 15);
            count += 15;
//                1F43 //服务器1端口号
            byte[] port1buff = BytesHelp.subBytes(dataArea, count, 2);
            count += 2;
//                1F41  //服务器2端口号
            byte[] port2buff = BytesHelp.subBytes(dataArea, count, 2);
            count += 2;
//                1770 //服务器1端口号
            byte[] port3buff = BytesHelp.subBytes(dataArea, count, 2);
            count += 2;
//                046C  //服务器2端口号
            byte[] port4buff = BytesHelp.subBytes(dataArea, count, 2);
            count += 2;
            List<Map<String,Object>> dataList = new ArrayList<>();
            Map<String,Object> data = new HashMap<>();
            String ip1Str = new String(ip1buff);
            String ip2Str = new String(ip2buff);
            String ip3Str = new String(ip3buff);
            String ip4Str = new String(ip4buff);
            data.put("ip1", ip1Str);
            data.put("ip1port", String.format("%d", BytesHelp.bytes2Int(port1buff, 0, 2)));
            data.put("code", BaseSettingFlag.IP1.getId());
            dataList.add(data);
            data = new HashMap<>();
            data.put("ip2", ip2Str);
            data.put("ip2port", String.format("%d", BytesHelp.bytes2Int(port2buff, 0, 2)));
            data.put("code", BaseSettingFlag.IP2.getId());
            dataList.add(data);
            data = new HashMap<>();
            data.put("ip3", ip3Str);
            data.put("ip3port", String.format("%d", BytesHelp.bytes2Int(port3buff, 0, 2)));
            data.put("code", BaseSettingFlag.IP3.getId());
            dataList.add(data);
            data = new HashMap<>();
            data.put("ip4", ip4Str);
            data.put("ip4port", String.format("%d", BytesHelp.bytes2Int(port4buff, 0, 2)));
            data.put("code", BaseSettingFlag.IP4.getId());
            dataList.add(data);
//                3133373136313433333136   //服务器1中心卡号
            count += 11;
//                3133383130363239393538
            count += 11;
//                3135303131323035393639
            count += 11;
//                3137303530303639383633
            count += 11;
//                B8
//                16
            this.next(false);
        } else {
            this.next(true);
        }
    }

    @Override
    public void close(DataStreamAdapter dataStreamAdapter) {

    }

    @Override
    public void batchComplete(DataStreamAdapter dataStreamAdapter) {
        log.info("接收到水资源 87 查询基本配置信息");

    }

    @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;
    }
}
