From eef1b021c016d10399885ace03faef40976e7a51 Mon Sep 17 00:00:00 2001 From: xins Date: Tue, 12 Sep 2023 16:43:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=A5=E4=BE=9D=E5=BE=AE=E6=9C=8D=E5=8A=A11.?= =?UTF-8?q?1.6=E7=89=88=E6=9C=AC=20=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=A4=84=E7=90=86=E8=AE=BE=E5=A4=87=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=B8=8A=E4=B8=8B=E7=BA=BF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mqtt/client/config/MqttConfiguration.java | 2 +- .../service/IDeviceStatusService.java | 2 +- .../service/impl/DeviceStatusServiceImpl.java | 54 +++++++++++++++++-- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/mqtt/client/config/MqttConfiguration.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/mqtt/client/config/MqttConfiguration.java index 7f9b9ce..9e9431a 100644 --- a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/mqtt/client/config/MqttConfiguration.java +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/mqtt/client/config/MqttConfiguration.java @@ -129,7 +129,7 @@ public class MqttConfiguration { if (topic.startsWith(dataTopicFilter.replace("#",""))) { dataProcessService.processBusinessData(payloadString, imagePath, imagePatterns); } else if (topic.equals(deviceStatusTopic)) { - deviceStatusService.handleDeviceStatus(); + deviceStatusService.handleDeviceStatus(payloadString,clientId); } } }); diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/IDeviceStatusService.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/IDeviceStatusService.java index 99f5a34..6d4bcd0 100644 --- a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/IDeviceStatusService.java +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/IDeviceStatusService.java @@ -9,5 +9,5 @@ package com.ruoyi.dataprocess.service; */ public interface IDeviceStatusService { - public void handleDeviceStatus(); + public void handleDeviceStatus(String payloadString,String clientId); } diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java index c90474f..bdf19dc 100644 --- a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java @@ -1,27 +1,73 @@ package com.ruoyi.dataprocess.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.ruoyi.common.core.constant.TdEngineConstants; +import com.ruoyi.dataprocess.domain.HwDevice; +import com.ruoyi.dataprocess.mapper.HwDeviceMapper; import com.ruoyi.dataprocess.service.IDeviceStatusService; import com.ruoyi.tdengine.api.RemoteTdEngineService; +import com.ruoyi.tdengine.api.domain.TdField; +import com.ruoyi.tdengine.api.domain.TdTableVo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; /** * @Description: 设备状态处理服务 * @ClassName: DeviceStatusServiceImpl - * @Author : xins - * @Date :2023-09-04 15:31 + * @Author : xins + * @Date :2023-09-04 15:31 * @Version :1.0 */ @Service public class DeviceStatusServiceImpl implements IDeviceStatusService { + @Autowired + private HwDeviceMapper hwDeviceMapper; + @Resource private RemoteTdEngineService remoteTdEngineService; @Override - public void handleDeviceStatus(){ + public void handleDeviceStatus(String payloadString, String clientId) { + System.out.println("ddd:" + payloadString); +// ddd:{"msg":"设备设备连接状态信息","deviceType":"edge","connectStatus":1, +// "statusTime":1694506127199,"deviceCode":"hw-data-process-1"} + JSONObject json = JSON.parseObject(payloadString); + String deviceCode = json.getString("deviceCode"); + if (clientId.equals(deviceCode)) { //校验是不是自己,如果是自己则不记录状态,返回即可。 + return; + } + HwDevice hwDevice = hwDeviceMapper.selectHwDeviceByDeviceCode(deviceCode); + if (hwDevice != null) { + Long deviceId = hwDevice.getDeviceId(); + TdTableVo tdTableVo = new TdTableVo(); + tdTableVo.setDatabaseName(TdEngineConstants.PLATFORM_DB_NAME); + tdTableVo.setTableName(TdEngineConstants.DEFAULT_DEVICE_STATUS_TABLE_NAME_PREFIX + deviceId); + + List schemaFields = new ArrayList<>(); + TdField onlineStatusField = new TdField(); + onlineStatusField.setFieldName("onlineStatus"); + onlineStatusField.setFieldValue(json.getInteger("connectStatus")); + schemaFields.add(onlineStatusField); + + TdField deviceTypeField = new TdField(); + deviceTypeField.setFieldName("deviceType"); + deviceTypeField.setFieldValue(json.getString("deviceType")); + schemaFields.add(deviceTypeField); + + TdField tsField = new TdField(); + tsField.setFieldName(TdEngineConstants.DEFAULT_FIRST_FIELD_NAME); + tsField.setFieldValue(json.getLong("statusTime")); + schemaFields.add(tsField); + + tdTableVo.setSchemaFields(schemaFields); - System.out.println("handleDeviceStatus"); + this.remoteTdEngineService.insertTable(tdTableVo); + } } }