陈力
2023-05-29 2b270f01facd18f5d2c105e48279482d3ff4b9c8
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.lotaai.canguiayw.device;
 
import com.blankj.utilcode.util.LogUtils;
 
import org.greenrobot.eventbus.EventBus;
 
import java.util.ArrayList;
import java.util.List;
 
public class CanGuiCommand {
    private static CanGuiCommand canGuiCommand;
 
    private CanGuiCommand() {
    }
 
    public static CanGuiCommand getInstance() {
        if (canGuiCommand == null) {
            canGuiCommand = new CanGuiCommand();
        }
        return canGuiCommand;
    }
 
    //模块上电启动完成后,主动发出命令
    public byte[] initCommand() {
        byte[] command = new byte[16];
        command[0] = (byte) 0xAA;
        command[1] = (byte) 0x55;
        command[2] = (byte) 0xE0;
 
        command[3] = (byte) 0x00;
        command[4] = (byte) 0x00;
        command[5] = (byte) 0x00;
        command[6] = (byte) 0x00;
        command[7] = (byte) 0x00;
        command[8] = (byte) 0x00;
        command[9] = (byte) 0x00;
        command[10] = (byte) 0x00;
        command[11] = (byte) 0x00;
        command[12] = (byte) 0x00;
        command[13] = (byte) 0xE0;
        command[14] = (byte) 0xC3;
        command[15] = (byte) 0x3C;
        return command;
    }
 
    /**
     * 查询格子的状态
     *
     * @param guiNo       查询下位机号
     * @param netType     网络类型 0:4G   1:WiFi  * 2:4G  * 3:以太网
     * @param netStatus   0:正常 * 1:断网
     * @param openOrClose 0:不关机 * 1:关机
     * @param closeLong   关机持续时间 x:x分钟后重启 0:立即重启
     * @param check       通信检测 0:不检测  X:X分钟不通信复位
     * @param type        0:按轮询号 x:按格子查询
     * @param selectValue 从0开始(一次可查8个货道)默认:4
     * @return
     */
    public byte[] selectStatusCommand(int guiNo, int netType, int netStatus, int openOrClose, int closeLong, int check, int type, int selectValue) {
        byte[] command = new byte[16];
        command[0] = (byte) 0xAA;
        command[1] = (byte) 0x55;
        command[2] = (byte) 0xE0;
 
        command[3] = (byte) (guiNo & 0xFF);
        command[4] = (byte) (netType & 0xFF);
        command[5] = (byte) (netStatus & 0xFF);
        command[6] = (byte) (openOrClose & 0xFF);
        command[7] = (byte) (closeLong & 0xFF);
        command[8] = (byte) (check & 0xFF);
        command[9] = (byte) 0x00;
        command[10] = (byte) (type & 0xFF);
        command[11] = (byte) (selectValue & 0xFF);
        command[12] = (byte) 0x00;
 
        //command[13] = (byte) (command[2] & command[3] & command[4] & command[5] & command[6] & command[7] & command[8] & command[9] & command[10] & command[11] & command[12] & 0xFF);
        command[13] = matchCrc(command, 10, 2);
        command[14] = (byte) 0xC3;
        command[15] = (byte) 0x3C;
        return command;
    }
 
    /**
     * 控制单个货道
     *
     * @param guiNo         柜子号
     * @param gridNo        格子号1-104
     * @param openOrNoActon 0:无操作  1:打开
     * @param wendu         FF:不设置温度
     * @param xiaodu        0:无操作  1:停止消毒  2:打开消毒
     * @param dengGuang     0:无操作   1:关闭  2:打开
     * @param jiaRe         0:无操作  1:不加热 2:加热
     * @param guangDengji   0:无操作  FF:关 等级1-10
     * @return
     */
    public byte[] controlGridCommand(int guiNo, int gridNo, int openOrNoActon, int wendu, int xiaodu, int dengGuang, int jiaRe, int guangDengji) {
        byte[] command = new byte[16];
        command[0] = (byte) 0xAA;
        command[1] = (byte) 0x55;
 
        command[2] = (byte) 0xB1;//动作
 
        command[3] = (byte) (guiNo & 0xFF);
        command[4] = (byte) (gridNo & 0xFF);
        command[5] = (byte) 0x00;
        command[6] = (byte) (openOrNoActon & 0xFF);
        command[7] = (byte) (wendu & 0xFF);
        command[8] = (byte) (xiaodu & 0xFF);
        command[9] = (byte) (dengGuang & 0xFF);
        command[10] = (byte) (jiaRe & 0xFF);
        command[11] = (byte) (guangDengji & 0xFF);
        command[12] = (byte) 0x00;
 
//      command[13] = (byte) (command[2] & command[3] & command[4] & command[5] & command[6] & command[7] & command[8] & command[9] & command[10] & command[11] & command[12] & 0xFF);
        command[13] = matchCrc(command, 10, 2);
        command[14] = (byte) 0xC3;
        command[15] = (byte) 0x3C;
        return command;
    }
 
    /**
     * 控制全部货道
     *
     * @param guiNo         柜子号
     * @param openOrNoActon 0:无操作  1:打开
     * @param wendu         FF:不设置温度
     * @param xiaodu        0:无操作  1:停止消毒  2:打开消毒
     * @param dengGuang     0:无操作   1:关闭  2:打开
     * @param jiaRe         0:无操作  1:不加热 2:加热
     * @return
     */
    public byte[] controlAllGridCommand(int guiNo, int openOrNoActon, int wendu, int xiaodu, int dengGuang, int jiaRe) {
        byte[] command = new byte[16];
        command[0] = (byte) 0xAA;
        command[1] = (byte) 0x55;
 
        command[2] = (byte) 0xB2;//动作
 
        command[3] = (byte) (guiNo & 0xFF);
        command[4] = (byte) 0x00;
        command[5] = (byte) 0x00;
        command[6] = (byte) (openOrNoActon & 0xFF);
        command[7] = (byte) (wendu & 0xFF);
        command[8] = (byte) (xiaodu & 0xFF);
        command[9] = (byte) (dengGuang & 0xFF);
        command[10] = (byte) (jiaRe & 0xFF);
        command[11] = (byte) 0x00;
        command[12] = (byte) 0x00;
 
        //  command[13] = (byte) (command[3] & command[4] & command[5] & command[6] & command[7] & command[8] & command[9] & command[10] & command[11] & command[12] & 0xFF);
        command[13] = matchCrc(command, 10, 2);
        command[14] = (byte) 0xC3;
        command[15] = (byte) 0x3C;
 
        return command;
    }
 
    /**
     * 发送指令
     *
     * @param message
     */
    public void sendMessage(byte[] message) {
        DeviceMessage deviceMessage = new DeviceMessage();
        deviceMessage.setDeviceType(DeviceType.CANGUI.ordinal());
        deviceMessage.setMessageType(MessageType.SENDMESSAGE.ordinal());
        deviceMessage.setMessageByte(message);
        EventBus.getDefault().post(deviceMessage);
    }
 
    public byte matchCrc(byte[] readData, int packLength, int index) {
        int total = 0;
        int x = 0;
        // 计算校验和,这里帧头长度为12
        for (x = index; x < index + packLength && x < readData.length; x++) {
            total += readData[x];
            if (readData[x] < 0) {
                total += 256;
            }
        }
 
        if (total > 256) {
            total = total % 256;
        }
        return (byte) (total & 0xFF);
    }
 
    public List<CanguiStatus> analysisRecvByte(byte[] recv) {
        List<CanguiStatus> list = new ArrayList<>();
        if (recv.length > 0) {
            int guiHao = recv[3] & 0xff;
            int num = 9;
            if (guiHao == 0){
                num = 8;
            }
            for (int i = 1; i <= num; i++) {
                //开始解析格子的状态
                CanguiStatus status = new CanguiStatus();
                //柜号
                status.setGuiHao(guiHao);
                //格子号
                status.setGridNo(i);
                //温度
                int site = (i-1) * 2 + 5;//温度
                int wendu = recv[site] & 0xff;
                status.setWendu(wendu);
                site = site + 1;
                // Bit0:1锁已开0锁关闭,
                //Bit1:1灯光开0灯光灭,
                //Bit2:1消毒开0消毒关,
                //Bit3:1加热开0加热关, Bit4:1有货物0无货物
                int mark = recv[site] & 0x01;
                if (mark == 1) {
                    status.setDoorIsOpen(true);
                } else {
                    status.setDoorIsOpen(false);
                }
 
                mark = recv[site] & 0x02;
                if (mark == 2) {
                    status.setDengGuangIsOpen(true);
                } else {
                    status.setDengGuangIsOpen(false);
                }
 
                mark = recv[site] & 0x04;
                if (mark == 4) {
                    status.setXiaoDuIsOpen(true);
                } else {
                    status.setXiaoDuIsOpen(false);
                }
 
                mark = recv[site] & 0x08;
                if (mark == 8) {
                    status.setJiareIsOpen(true);
                } else {
                    status.setJiareIsOpen(false);
                }
                mark = recv[site] & 0x10;
                if (mark == 16) {
                    status.setGoodsIsIn(true);
                } else {
                    status.setGoodsIsIn(false);
                }
                list.add(status);
            }
        }
        return list;
    }
 
}