package com.imdroid.secapi.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.imdroid.common.util.ByteUtil;
import com.imdroid.common.util.HexUtil;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * GNSS设备配置数据
 *
 * @author LiGang
 */
@Data
@TableName(value = "gnssdevices")
public class GnssDevice {
    public static final short TYPE_ROVER = 0;
    public static final short TYPE_REFERENCE_STATION = 1;

    public static final short OP_MODE_USE = 0;
    public static final short OP_MODE_CHECK = 1;
    public static final short OP_MODE_UNUSE = 2;
    public static final short OP_MODE_UPGRADING = 3;

    public static final short MODEL_G505 = 0; //F9P
    public static final short MODEL_G510 = 1; //博通

    public static final short LOGGING_MODE_SIMPLE = 0; // 精简模式（ 仅D3F0和D3F2 ）
    public static final short LOGGING_MODE_FULL = 1;   // 完整模式

    public static final int PARA_MASK_HAS_BATTERY = 0x01;
    public static final int PARA_MASK_VOLTAGE_FACTOR = 0x02;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private Integer tenantid;
    private Short opmode;
    private LocalDateTime createtime;
    private String createuser;
    private LocalDateTime updatetime;
    private String updateuser;
    private String deviceid;
    private String fwddeviceid; //推送的设备名
    private String name;
    private String parentid;
    private Integer devicetype;
    private String tenantname;
    private String project_id;
    private String project2_id;
    private Integer group_id = 1;   //组参数，缓存自动下发
    private Integer calc_group_id = 1;
    private String fwd_group_id;
    private String fwd_group_id2;
    private Boolean has_battery;
    private Byte voltage_factor; //电压测量分压比
    private Boolean syn;    //组参数是否同步
    private String pictures;
    private Double ipose; //初始位置
    private Double iposn; //初始位置
    private Double iposd; //初始位置

    private Double ecefx;
    private Double ecefy;
    private Double ecefz;

    private String sector;//桩号
    private String appver;
    private String imei;
    private Short model;

    // 日志记录控制
    private Short loggingmode;
    private String remark;
    private String iccid;

    // 参数改变
    private Integer change_flag = 0;

    private String getBatteryConfigCmd(){
        return "d3130006"+ HexUtil.Int2HexString(Integer.parseInt(deviceid))+
                "55"+(has_battery?"01":"00");
    }

    private String getVoltageFactorConfigCmd(){
        return "d3130006"+ HexUtil.Int2HexString(Integer.parseInt(deviceid))+
                "52"+HexUtil.Byte2HexString(voltage_factor);
    }
    public List<String> getConfigCmd(){
        List<String> cmdList = new ArrayList<>();
        if((change_flag&PARA_MASK_HAS_BATTERY) !=0 ){
            cmdList.add(getBatteryConfigCmd());
        }
        if((change_flag&PARA_MASK_VOLTAGE_FACTOR) !=0 ){
            cmdList.add(getVoltageFactorConfigCmd());
        }
        return cmdList;
    }

    public boolean clearChangeFlag(String ack){
        boolean result = false;
        if(change_flag!=0) {
            byte[] ackBytes = ByteUtil.hexStringTobyte(ack);
            if(ackBytes[ackBytes.length-1] == 1) {
                if (ackBytes[8] == 0x55) {
                    change_flag &= ~PARA_MASK_HAS_BATTERY;
                    result = true;
                } else if (ackBytes[8] == 0x52) {
                    change_flag &= ~PARA_MASK_VOLTAGE_FACTOR;
                    result = true;
                }
            }
        }
        return result;
    }

}
