接收设备消息的异常处理

This commit is contained in:
kerwincui
2022-03-18 22:02:20 +08:00
parent 1bc1fbb354
commit 23edf7e794

View File

@@ -33,7 +33,9 @@ public class EmqxService {
@Autowired @Autowired
private IDeviceLogService deviceLogService; private IDeviceLogService deviceLogService;
/** 订阅的主题 */ /**
* 订阅的主题
*/
private static final String prefix = "/+/+/"; private static final String prefix = "/+/+/";
String sInfoTopic = prefix + "info/post"; String sInfoTopic = prefix + "info/post";
String sNtpTopic = prefix + "ntp/post"; String sNtpTopic = prefix + "ntp/post";
@@ -43,7 +45,9 @@ public class EmqxService {
String sShadowPropertyTopic = prefix + "property-offline/post"; String sShadowPropertyTopic = prefix + "property-offline/post";
String sShadowFunctionTopic = prefix + "function-offline/post"; String sShadowFunctionTopic = prefix + "function-offline/post";
/** 发布的主题 */ /**
* 发布的主题
*/
String pStatusTopic = "/status/post"; String pStatusTopic = "/status/post";
String pNtpTopic = "/ntp/get"; String pNtpTopic = "/ntp/get";
String pPropertyTopic = "/property/get"; String pPropertyTopic = "/property/get";
@@ -107,14 +111,19 @@ public class EmqxService {
* 上报设备信息 * 上报设备信息
*/ */
private void reportDevice(Long productId, String deviceNum, String message) { private void reportDevice(Long productId, String deviceNum, String message) {
try {
Device device = JSON.parseObject(message, Device.class); Device device = JSON.parseObject(message, Device.class);
device.setProductId(productId); device.setProductId(productId);
device.setSerialNumber(deviceNum); device.setSerialNumber(deviceNum);
deviceService.reportDevice(device); deviceService.reportDevice(device);
} catch (Exception e) {
logger.error("接收设备信息,解析数据时异常 message={}", e.getMessage());
}
} }
/** /**
* 上报属性 * 上报属性
*
* @param message * @param message
*/ */
private void reportProperty(Long productId, String deviceNum, String message, boolean isShadow) { private void reportProperty(Long productId, String deviceNum, String message, boolean isShadow) {
@@ -132,22 +141,29 @@ public class EmqxService {
/** /**
* 上报功能 * 上报功能
*
* @param message * @param message
*/ */
private void reportFunction(Long productId, String deviceNum, String message, boolean isShadow) { private void reportFunction(Long productId, String deviceNum, String message, boolean isShadow) {
try {
List<ThingsModelValueRemarkItem> thingsModelValueRemarkItems = JSON.parseArray(message, ThingsModelValueRemarkItem.class); List<ThingsModelValueRemarkItem> thingsModelValueRemarkItems = JSON.parseArray(message, ThingsModelValueRemarkItem.class);
ThingsModelValuesInput input = new ThingsModelValuesInput(); ThingsModelValuesInput input = new ThingsModelValuesInput();
input.setProductId(productId); input.setProductId(productId);
input.setDeviceNumber(deviceNum); input.setDeviceNumber(deviceNum);
input.setThingsModelValueRemarkItem(thingsModelValueRemarkItems); input.setThingsModelValueRemarkItem(thingsModelValueRemarkItems);
deviceService.reportDeviceThingsModelValue(input, 2, isShadow); deviceService.reportDeviceThingsModelValue(input, 2, isShadow);
} catch (Exception e) {
logger.error("接收功能,解析数据时异常 message={}", e.getMessage());
}
} }
/** /**
* 上报事件 * 上报事件
*
* @param message * @param message
*/ */
private void reportEvent(Long productId, String deviceNum, String message) { private void reportEvent(Long productId, String deviceNum, String message) {
try {
List<ThingsModelValueRemarkItem> thingsModelValueRemarkItems = JSON.parseArray(message, ThingsModelValueRemarkItem.class); List<ThingsModelValueRemarkItem> thingsModelValueRemarkItems = JSON.parseArray(message, ThingsModelValueRemarkItem.class);
Device device = deviceService.selectDeviceBySerialNumber(deviceNum); Device device = deviceService.selectDeviceBySerialNumber(deviceNum);
for (int i = 0; i < thingsModelValueRemarkItems.size(); i++) { for (int i = 0; i < thingsModelValueRemarkItems.size(); i++) {
@@ -163,8 +179,10 @@ public class EmqxService {
deviceLog.setIsMonitor(0); deviceLog.setIsMonitor(0);
deviceLogService.insertDeviceLog(deviceLog); deviceLogService.insertDeviceLog(deviceLog);
} }
} catch (Exception e) {
logger.error("接收事件,解析数据时异常 message={}", e.getMessage());
}
} }
/** /**
@@ -177,6 +195,7 @@ public class EmqxService {
/** /**
* 2.发布时钟同步信息 * 2.发布时钟同步信息
*
* @param message * @param message
*/ */
private void publishNtp(Long productId, String deviceNum, String message) { private void publishNtp(Long productId, String deviceNum, String message) {
@@ -210,6 +229,4 @@ public class EmqxService {
} }
} }