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;
|
}
|
|
}
|