package org.ts.ddcs.DataItemTemplate.jtDataItemTemplate;

import org.ts.ddcs.DataItemTemplate.DataItemTemplate;
import org.ts.ddcs.Tool.BytesHelp;

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


/**
 * AFN C0
 * 自报 报警状态
 *
 * @author dylan
 */
public class JtDataItemTemplateLocation extends DataItemTemplate {
    @Override
    public byte[] Stream(byte[] data) {
        byte[] remain = data;
        if (this.macth) {
            return remain;
        }
        try {
            datas = new HashMap<>(6);

            int count = 0;
            byte[] lat = BytesHelp.subBytes(remain, count, 4);
            double latValue = (double) BytesHelp.bytes2Int(lat, 0, 4) / (double) 1000000;
            count += 4;

            byte[] lng = BytesHelp.subBytes(remain, count, 4);
            double lngValue = (double) BytesHelp.bytes2Int(lng, 0, 4) / (double) 1000000;
            count += 4;


            datas.put("lat", new BigDecimal(latValue).toPlainString());
            datas.put("lng", new BigDecimal(lngValue).toPlainString());

            //高程
            byte[] highBuff = BytesHelp.subBytes(remain, count, 2);
            int highValue = BytesHelp.bytes2short(highBuff);
            datas.put("altitude", new Integer(highValue));
            count += 2;
            //速度
            byte[] speedBuff = BytesHelp.subBytes(remain, count, 2);
            int speedValue = BytesHelp.bytes2short(speedBuff);
            datas.put("speed", new Integer(speedValue));
            count += 2;
            //方向
            byte[] directionBuff = BytesHelp.subBytes(remain, count, 2);
            int directionValue = BytesHelp.bytes2short(directionBuff);
            datas.put("direction", new Integer(directionValue));
            count += 2;
            //时间
            byte[] timeBuff = BytesHelp.subBytes(remain, count, 6);
            Date dt = new Date();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String time = timeFormat.format(dt).substring(0, 2) + BytesHelp.byte2HexStr(timeBuff);
            datas.put("reportTime", timeFormat.parse(time));
        }catch (Exception e){

        }
        return new byte[0];
    }
}
