package org.ts.ddcs.DataTemplate;

import org.ts.ddcs.DataItemTemplate.DataItemTemplateImg;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import java.util.HashMap;


/**
 * created by swp on 2017/8/15
 * 2014 规约报文数据模板
 * 图片上报
 * func 0x36
 */
public class DataTemplateF36 extends DataTemplate {
    private byte[] serialNo;
    private byte[] sendPacketTime;
    private byte[] siteKind;
    private byte[] collectTime;

    @Override
    public byte[] SerialNo() {
        return this.serialNo;
    }

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

    @Override
    public byte[] SiteKind() {
        return this.siteKind;
    }

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

    @Override
    @SuppressWarnings("all")
    public void Stream() {
        try {
            if (dataLen > 0 && dataLen == dataArea.length) {
                int count = 0;
                byte[] packIndex = BytesHelp.subBytes(dataArea, count, 3);
                int indexValue = BytesHelp.bytes2Int(packIndex, 0, 3);
                int packpagSize = (indexValue >> 12) & 0x0FFF;
                int packpageCount = indexValue & 0x0FFF;
                count += 3;
                DataItemTemplateImg itemTemplateImg = new DataItemTemplateImg();
                itemTemplateImg.setPackageSize(packpagSize);
                itemTemplateImg.setPackageCount(packpageCount);

                if (packpageCount == 1) {
                    serialNo = BytesHelp.subBytes(dataArea, count, 2); //流水号
                    count += 2;
                    sendPacketTime = BytesHelp.subBytes(dataArea, count, 6); // 发报时间
                    count += 6;
                    byte[] addrFlag = BytesHelp.subBytes(dataArea, count, 2); //地址标识符
                    count += 2;
                    byte[] dataAreaRtuCode = BytesHelp.subBytes(dataArea, count, 5); //遥测站地址
                    count += 5;
                    siteKind = BytesHelp.subBytes(dataArea, count, 1);//遥测站分类码
                    count += 1;
                    byte[] collectTimeFlag = BytesHelp.subBytes(dataArea, count, 2); //采集时间
                    count += 2;
                    collectTime = BytesHelp.subBytes(dataArea, count, 5); //采集时间
                    count += 5;
                    if (dataArea.length - count > 0) {
                        count += 2;
                        if (dataArea.length - count > 0) {
                            byte[] imageData = BytesHelp.subBytes(dataArea, count, dataArea.length - count);
                            if (null != imageData && imageData.length > 0) {
                                itemTemplateImg.setBuff(imageData);
                            }
                        }
                    }
                } else if (packpagSize == packpageCount) {
                    if (dataArea.length - count > 0) {
                        byte[] imageData = BytesHelp.subBytes(dataArea, count, dataArea.length - count);
                        if (null != imageData && imageData.length > 0) {
                            itemTemplateImg.setBuff(imageData);
                        }
                    }
                } else {
                    if (dataArea.length - count > 0) {
                        byte[] imageData = BytesHelp.subBytes(dataArea, count, dataArea.length - count);
                        if (null != imageData && imageData.length > 0) {
                            itemTemplateImg.setBuff(imageData);
                        }
                    }
                }
                this.dataItem = new HashMap<>();
                this.dataItem.put("photo", itemTemplateImg);

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