This commit is contained in:
kerwincui
2024-03-17 14:59:23 +08:00
parent 3d44f4674c
commit 5539c1b6af
999 changed files with 115642 additions and 10757 deletions

View File

@@ -0,0 +1,53 @@
package com.fastbee.common;
import com.fastbee.common.core.iot.response.DeCodeBo;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.gateway.CRC16Utils;
import io.netty.buffer.ByteBufUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;
/**
* 协议编解码
*
* @author gsb
* @date 2023/4/8 15:50
*/
@Component
@Slf4j
public class ProtocolDeCodeService {
public String protocolDeCode(DeCodeBo bo) {
if (null == bo) {
throw new ServiceException("输入内容为空");
}
String payload = bo.getPayload();
/*1-解析 2-读指令 3-写指令 4-CRC生成 5-CRC校验*/
switch (bo.getType()) {
case 1:
case 2:
case 3:
case 4:
byte[] crc16Byte = ByteBufUtil.decodeHexDump(payload);
String crc = CRC16Utils.getCRC(crc16Byte);
return payload + crc;
case 5:
byte[] crcByte = ByteBufUtil.decodeHexDump(payload);
byte[] checksCRC = {crcByte[crcByte.length -2],crcByte[crcByte.length-1]};
byte[] sourceCRC = ArrayUtils.subarray(crcByte, 0, crcByte.length - 2);
String crc1 = CRC16Utils.getCRC(sourceCRC);
String check = ByteBufUtil.hexDump(checksCRC);
if (!crc1.equalsIgnoreCase(check)){
return "原报文CRC:" + check +"校验失败,CRC值应为:" + crc1 +
"<br/>完整报文:" + ByteBufUtil.hexDump(sourceCRC) +crc1;
}else {
return "校验通过!";
}
}
return null;
}
}

View File

@@ -0,0 +1,70 @@
package com.fastbee.json;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fastbee.common.annotation.SysProtocol;
import com.fastbee.common.constant.FastBeeConstant;
import com.fastbee.common.core.mq.DeviceReport;
import com.fastbee.common.core.mq.message.DeviceData;
import com.fastbee.common.core.thingsModel.ThingsModelSimpleItem;
import com.fastbee.common.core.thingsModel.ThingsModelValuesInput;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.model.ThingsModels.ValueItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
/**
* @author gsb
* @date 2022/10/10 16:55
*/
@Slf4j
@Component
@SysProtocol(name = "JSONArray解析协议",protocolCode = FastBeeConstant.PROTOCOL.JsonArray,description = "系统内置JSONArray解析协议")
public class JsonProtocolService {
public DeviceReport decode(DeviceData deviceData, String clientId) {
try {
DeviceReport reportMessage = new DeviceReport();
// bytep[] 转String
String data = new String(deviceData.getData(),StandardCharsets.UTF_8);
List<ThingsModelSimpleItem> values = JSON.parseArray(data, ThingsModelSimpleItem.class);
//上报数据时间
for (ThingsModelSimpleItem value : values) {
value.setTs(DateUtils.getNowDate());
}
ThingsModelValuesInput valuesInput = new ThingsModelValuesInput();
valuesInput.setThingsModelValueRemarkItem(values);
reportMessage.setValuesInput(valuesInput);
reportMessage.setClientId(clientId);
reportMessage.setSerialNumber(clientId);
return reportMessage;
}catch (Exception e){
throw new ServiceException("数据解析异常"+e.getMessage());
}
}
public byte[] encode(DeviceData message, String clientId) {
try {
JSONObject body = (JSONObject) message.getDownMessage().getBody();
ValueItem valueItem = new ValueItem();
for (Map.Entry<String, Object> entry : body.entrySet()) {
valueItem.setId(entry.getKey());
valueItem.setValue(entry.getValue()+"");
valueItem.setRemark("");
}
String msg = "[" + JSONObject.toJSONString(valueItem) +"]";
return msg.getBytes(StandardCharsets.UTF_8);
}catch (Exception e){
log.error("=>指令编码异常,device={},data={}",message.getSerialNumber(),
message.getDownMessage().getBody());
return null;
}
}
}