From 8ac4d9334df66aaf3f06c00d6587d683d033bfc0 Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期四, 07 十一月 2024 11:10:26 +0800 Subject: [PATCH] 优化设备信息获取主题处理逻辑 --- jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java | 94 ++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 92 insertions(+), 2 deletions(-) diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java index 55c571f..565cebd 100644 --- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java +++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java @@ -1,21 +1,32 @@ package org.jeecg.modules.dry.mqtt; import cn.hutool.core.thread.ThreadUtil; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.jeecg.common.config.TenantContext; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.constant.MqttConstant; import org.jeecg.common.util.DateUtils; import org.jeecg.common.util.RedisUtil; import org.jeecg.modules.base.service.BaseCommonService; import org.jeecg.modules.dry.api.EmqxApi; +import org.jeecg.modules.dry.entity.DryEquipment; +import org.jeecg.modules.dry.entity.DryShop; +import org.jeecg.modules.dry.service.IDryEquipmentService; +import org.jeecg.modules.dry.service.IDryRealTimeDataService; +import org.jeecg.modules.dry.service.IDryShopService; +import org.jeecg.modules.dry.vo.DryEquipmentVo; +import org.jeecg.modules.dry.vo.RealTimeDataVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; +@Slf4j @Component @Scope("prototype") public class MqttSampleCallback implements MqttCallback { @@ -29,6 +40,18 @@ private BaseCommonService baseCommonService; @Autowired private RedisUtil redisUtil; + + @Autowired + private IDryRealTimeDataService realTimeDataService; + + + @Autowired + private IDryEquipmentService equipmentService; + + @Autowired + private IDryShopService dryShopService; + + @Override @@ -84,7 +107,12 @@ // 鏅�氱敤鎴� case "user": System.err.println("user"); - parseUserCommand(topic, mqttMessage); + try { + parseUserCommand(topic, mqttMessage); + } catch (Exception e) { + e.printStackTrace(); + } + break; } @@ -111,7 +139,11 @@ if (messageJson.containsKey("timestamp")) { messageJson.put("timestamp", DateUtils.zone2Str(messageJson.get("timestamp").toString())); } - baseCommonService.addLog(message, CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_1); + // 瀹炴椂鏁版嵁涓婁紶澶绻佷笖鏁版嵁鍐呭瓒呰繃瀛楁澶у皬涓嶈褰曟棩蹇� + if (!topic.equals(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA)){ + baseCommonService.addLog(message, CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_1); + } + switch (topic) { // 鏌ヨ璁惧鍦ㄧ嚎 case MqttConstant.MOBILE_QUERY_EQU_STATU: @@ -149,6 +181,36 @@ }); break; + // 鎺ユ敹璁惧瀹炴椂鏁版嵁 + case MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA: + try { + RealTimeDataVo vo = JSON.parseObject(message, RealTimeDataVo.class); + realTimeDataService.realTimeDataHandle(vo); + } catch (Exception e) { + e.printStackTrace(); + } + + break; + case MqttConstant.TENANT_UP_PREFIX_EQU: + try { + DryEquipment equipment = (DryEquipment) messageJson.get("equipment"); + + DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(equipment.getTenantId() + "", equipment.getCode()); + + if (dryEquipment == null) { + equipmentService.save(equipment); + } + + + + // 鑾峰彇璁惧鎵�灞炶溅闂� + DryShop shop = (DryShop) messageJson.get("shop"); + shop.setTenantId(equipment.getTenantId()); + dryShopService.save(shop); + + } catch (Exception e) { + e.printStackTrace(); + } } } @@ -192,6 +254,34 @@ break; + case MqttConstant.SERVICE_REQ_PREFIX: + log.debug("鏀跺埌璁惧璇︾粏淇℃伅鏌ヨ璇锋眰"); + String tenantId = messageJson.getString("tenantId"); + if (tenantId!=null && tenantId.equals(TenantContext.getTenant())) { + // 鏍规嵁璁惧缂栫爜鏌ヨ璁惧淇℃伅 + String code = messageJson.getString("code"); + DryEquipment equipmentVo = equipmentService.selectByTenantIdEquipmentId(tenantId,code); + // 鏍规嵁璁惧杞﹂棿id鏌ヨ杞﹂棿淇℃伅 + DryShop shop = dryShopService.getById(equipmentVo.getShopId()); + + JSONObject res = new JSONObject(); + + res.put("tenant", TenantContext.getTenant()); + res.put("equipment", equipmentVo); + res.put("shop", shop); + try { + MqttMessage sendMessage = new MqttMessage(res.toJSONString().getBytes()); + sendMessage.setQos(0); + mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_EQU, sendMessage); + // baseCommonService.addLog(res.toString(), CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_2); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + break; + } } -- Gitblit v1.9.3