package com.wdkl.ncs.s433.transbox.utils;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;

public class AlarmMessageUtil {

    public static String getHeartBeatMessage() {
        StringBuilder sb = new StringBuilder();
        sb.append("a55a01");
        JSONObject data = new JSONObject();
        data.put("devId", NetUtil.getInstance().getMacAddress());
        String dataJsonStr = data.toJSONString();
        //数据长度16进制字符串
        Log.i("数据长度", String.format("%04x",dataJsonStr.length()));
        sb.append(String.format("%04X",dataJsonStr.length()));
        String s = ByteUtil.str2HexStr(dataJsonStr);
        sb.append(s);
        byte[] bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
        int sum = 0;
        for (int i = 0; i < bytes.length; i++) {
            sum += bytes[i];
        }
        sb.append(Integer.toHexString(sum));
        return sb.toString();
    }

    public static String getAlarmMessage(String mac){
        StringBuilder sb = new StringBuilder();
        sb.append("a55a02");
        JSONObject data = new JSONObject();
        data.put("keycoDe", mac);
        String dataJsonStr = data.toJSONString();
        //数据长度16进制字符串
        Log.i("数据长度", String.format("%04x",dataJsonStr.length()));
        sb.append(String.format("%04X",dataJsonStr.length()));
        String s = ByteUtil.str2HexStr(dataJsonStr);
        sb.append(s);
        byte[] bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
        int sum = 0;
        for (int i = 0; i < bytes.length; i++) {
            sum += bytes[i];
        }
        sb.append(Integer.toHexString(sum));
        return sb.toString();
    }

}
