562935844@qq.com
2023-11-27 566ddb2ea03e2514de50f2ca861a2674f6e840ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.hdl.sdk.ttl.HDLAppliances.HDLDoorMachine.Parser;
 
 
import android.util.Log;
 
import com.hdl.sdk.ttl.Config.Configuration;
import com.hdl.sdk.ttl.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLDeviceManager;
import com.hdl.sdk.ttl.Utils.HDLUtlis.AesUtil;
import com.hdl.sdk.ttl.Utils.HDLUtlis.CommonHelper;
 
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * Created by panlili on 2023/8/22
 */
public class DoorMachineParser {
    //    附加数据:  回路号,备注,状态
    public static final int fail = 0;
 
    public static final int doorNum = 0;
 
    public static final int doorStatus = 1;//门锁状态
    public static final int doorStatusClose = 0;//关
    public static final int doorStatusOpen = 1;//开
    public static final int doorStatusNormallyOpen = 2;//常开
 
    public static final int normalOpenMode = 2;//常开模式
    public static final int normalOpenModeClose = 0;//关闭
    public static final int normalOpenModeOpen = 1;//打开
 
    public static final int doorVolume = 3;//音量
 
    public static final int doorElectricity = 4;//电池百分比
 
    public static byte[] getDoorMachineAddByte(AppliancesInfo appliancesInfo, byte[] secretBytes, String password, int doorStatus) {
        try {
            AppliancesInfo newInfo = null;
            byte[] doorMachineBytes = null;
 
            outter:
            for (int i = 0; i < HDLDeviceManager.devicesDataList.size(); i++) {
                if (appliancesInfo.getDeviceSubnetID() == HDLDeviceManager.devicesDataList.get(i).getSourceSubnetID()
                        && appliancesInfo.getDeviceDeviceID() == HDLDeviceManager.devicesDataList.get(i).getSourceDeviceID()) {
                    for (int j = 0; j < HDLDeviceManager.devicesDataList.get(i).getAppliancesInfoList().size(); j++) {
                        if (HDLDeviceManager.devicesDataList.get(i).getAppliancesInfoList().get(j).getBigType() == Configuration.DOOR_MACHINE_BIG_TYPE
                                && HDLDeviceManager.devicesDataList.get(i).getAppliancesInfoList().get(j).getDeviceType() == HDLApConfig.TYPE_DOOR_MACHINE
                                && appliancesInfo.getChannelNum() == HDLDeviceManager.devicesDataList.get(i).getAppliancesInfoList().get(j).getChannelNum()) {
                            newInfo = HDLDeviceManager.devicesDataList.get(i).getAppliancesInfoList().get(j);
                            if (newInfo.getArrCurState() == null) {
                                newInfo.setArrCurState(new byte[9]);
                            }
                            doorMachineBytes = newInfo.getArrCurState();
                            break outter;
                        }
 
                    }
                }
            }
 
            byte[] passwordBytes = new byte[6];
            byte[] dataBytes = new byte[32];
 
            if (doorMachineBytes != null && doorMachineBytes.length >= 9) {
                //开锁密码6byte
                if (password != null && password.length() == 6) {
                    for (int i = 0; i < password.length(); i++) {
                        String s = String.valueOf(password.charAt(i));
                        passwordBytes[i] = Byte.parseByte(s);
                    }
                }
                dataBytes = lockControlEncrypt(secretBytes, passwordBytes, doorStatus);
            } else {
 
            }
 
            return dataBytes;
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[]{fail};
        }
 
    }
 
    public static byte[] getDoorMachineSettingPwdByte(AppliancesInfo appliancesInfo, String tempPassword, String startTime, String endTime) {
        try {
            byte[] addBytes = new byte[15];
            addBytes[0] = (byte) appliancesInfo.getChannelNum();
 
            //开锁密码6byte
            if (tempPassword != null && tempPassword.length() == 6) {
                for (int i = 0; i < tempPassword.length(); i++) {
                    String s = String.valueOf(tempPassword.charAt(i));
                    addBytes[i] = Byte.parseByte(s);
                }
            }
 
            //开始时间戳4byte
            long st = Long.parseLong(startTime);
            addBytes[7] = (byte) (st / 255 / 255 / 255);
            addBytes[8] = (byte) (st / 255 / 255);
            addBytes[9] = (byte) (st / 255);
            addBytes[10] = (byte) (st % 255);
 
            //结束时间戳4byte
            long et = Long.parseLong(endTime);
            addBytes[11] = (byte) (et / 255 / 255 / 255);
            addBytes[12] = (byte) (et / 255 / 255);
            addBytes[13] = (byte) (et / 255);
            addBytes[14] = (byte) (et % 255);
 
            return addBytes;
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[]{fail};
        }
 
    }
 
    public static byte[] lockControlEncrypt(byte[] usefulBytes, byte[] passwordBytes, int doorStatus) {
        byte[] sendBytes = new byte[32];
        int loopID = usefulBytes[0] & 0xFF;
 
        byte[] randomBytes = Arrays.copyOfRange(usefulBytes, 1, 9);
 
        byte[] passwordArray = new byte[6];
        passwordArray[0] = Byte.parseByte("1");
        passwordArray[1] = Byte.parseByte("2");
        passwordArray[2] = Byte.parseByte("3");
        passwordArray[3] = Byte.parseByte("1");
        passwordArray[4] = Byte.parseByte("2");
        passwordArray[5] = Byte.parseByte("3");
 
        try {
            //备注20位
            byte[] deviceBytes = "HDL ON".getBytes("GB2312");
 
            /*if (loopID > 0) {
                loopID -= 1;
            }*/
            byte[] inpuptBytes = new byte[]{(byte) 0xE4, (byte) 0xF0, (byte) loopID, (byte) doorStatus};
            byte[] allAddBytes = new byte[4 + 6 + 2 + 20];
            System.arraycopy(inpuptBytes, 0, allAddBytes, 0, 4);
            System.arraycopy(passwordBytes, 0, allAddBytes, 4, 6);
            System.arraycopy(deviceBytes, 0, allAddBytes, 4 + 6 + 2, deviceBytes.length);
 
            sendBytes = CommonHelper.Encryption(randomBytes, allAddBytes);
            return sendBytes;
 
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return sendBytes;
    }
 
}