From 22e1c583386955482354cb5ee327739c65acb28c Mon Sep 17 00:00:00 2001 From: xins Date: Tue, 19 Sep 2023 11:31:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=A5=E4=BE=9D=E5=BE=AE=E6=9C=8D=E5=8A=A11.?= =?UTF-8?q?2.4=201=E3=80=81TDengine=E6=9C=8D=E5=8A=A1=E5=AE=8C=E5=96=84=20?= =?UTF-8?q?2=E3=80=81=E8=AE=BE=E5=A4=87=E7=AE=A1=E7=90=86=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=97=B6=E5=9C=A8tdengine=E5=88=9B=E5=BB=BA=E8=A1=A8?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8redis=E5=AD=98=E5=82=A8=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=90=8D=E5=92=8C=E5=AF=86=E7=A0=81=203=E3=80=81=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E5=B9=B3=E5=8F=B0=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/constant/HwDictConstants.java | 1 + .../core/constant/TdEngineConstants.java | 6 +- .../controller/HwDeviceController.java | 7 + .../HwMonitorPlatformController.java | 15 +- .../com/ruoyi/business/domain/HwDevice.java | 362 ++++++++++-------- .../ruoyi/business/mapper/HwDeviceMapper.java | 8 + .../business/service/IHwDeviceService.java | 9 + .../service/impl/HwDeviceServiceImpl.java | 221 ++++++++++- .../impl/HwMonitorPlatformServiceImpl.java | 4 +- .../mapper/business/HwSceneMapper.xml | 2 +- .../mapper/tdengine/TdEngineMapper.xml | 8 +- ruoyi-ui/src/views/business/device/index.vue | 66 ++-- 12 files changed, 483 insertions(+), 226 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/HwDictConstants.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/HwDictConstants.java index b14a75e..a4cd2a4 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/HwDictConstants.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/HwDictConstants.java @@ -54,5 +54,6 @@ public class HwDictConstants { public static final String DEVICE_TYPE_GATEWAY_SUB_EQUIPMENT = "2";//网关子设备 public static final String DEVICE_TYPE_DIRECT_CONNECT_DEVICE = "3";//直连设备 + public static final String REDIS_KEY_DEVICE_INFO = "hw_device_info";//保存设备用户名和密码等信息的redis的key } diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/TdEngineConstants.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/TdEngineConstants.java index 312d2b2..79db57d 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/TdEngineConstants.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/TdEngineConstants.java @@ -85,10 +85,14 @@ public class TdEngineConstants { * @date 2023-09-16 14:42 * @return String */ - public static String getSupertTableName(Long deviceModeId) { + public static String getSuperTableName(Long deviceModeId) { return DEFAULT_SUPER_TABLE_NAME_PREFIX + deviceModeId; } + public static String getTableName(Long deviceId) { + return DEFAULT_TABLE_NAME_PREFIX + deviceId; + } + /** * @param: deviceId * @return String diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwDeviceController.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwDeviceController.java index dc74dd0..7f70dca 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwDeviceController.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwDeviceController.java @@ -17,6 +17,8 @@ import com.ruoyi.common.core.constant.HwDictConstants; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.system.api.domain.SysDept; import com.ruoyi.system.api.domain.SysRole; +import com.ruoyi.system.api.domain.SysUser; +import com.ruoyi.system.api.model.LoginUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -104,6 +106,10 @@ public class HwDeviceController extends BaseController @PostMapping public AjaxResult add(@RequestBody HwDevice hwDevice) { + LoginUser loginUser = SecurityUtils.getLoginUser(); + SysUser user = loginUser.getSysUser(); + hwDevice.setTenantId(user.getTenantId()); + hwDevice.setCreateBy(user.getUserName()); return toAjax(hwDeviceService.insertHwDevice(hwDevice)); } @@ -115,6 +121,7 @@ public class HwDeviceController extends BaseController @PutMapping public AjaxResult edit(@RequestBody HwDevice hwDevice) { + hwDevice.setUpdateBy(SecurityUtils.getUsername()); return toAjax(hwDeviceService.updateHwDevice(hwDevice)); } diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java index 4481fa6..749d965 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java @@ -179,15 +179,14 @@ public class HwMonitorPlatformController extends BaseController { * @return list * @throws */ - @GetMapping("/treeList{sceneId}") - public TableDataInfo treeList(@PathVariable("sceneId") Long sceneId) { - HwMonitorUnit hwMonitorUnit = new HwMonitorUnit(); - hwMonitorUnit.setSceneId(sceneId); - List hwMonitorUnits = hwMonitorUnitService.selectHwMonitorUnitList(hwMonitorUnit); - List list = hwMonitorUnitService.selectTreeList(hwMonitorUnits); - return getDataTable(list); - + @GetMapping("/treeList/{sceneId}") + public AjaxResult monitorUnitTree(@PathVariable("sceneId") Long sceneId) + { + HwMonitorUnit queryMonitorUnit = new HwMonitorUnit(); + queryMonitorUnit.setSceneId(sceneId); + return success(hwMonitorUnitService.selectMonitorTreeList(queryMonitorUnit)); } + // /** // * // * 选择场景 diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/domain/HwDevice.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/domain/HwDevice.java index 3c2890d..a77b95c 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/domain/HwDevice.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/domain/HwDevice.java @@ -1,6 +1,7 @@ package com.ruoyi.business.domain; import java.util.Date; + import com.fasterxml.jackson.annotation.JsonFormat; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -9,327 +10,354 @@ import com.ruoyi.common.core.web.domain.BaseEntity; /** * 设备信息对象 hw_device - * + * * @author xins * @date 2023-09-13 */ -public class HwDevice extends BaseEntity -{ +public class HwDevice extends BaseEntity { private static final long serialVersionUID = 1L; - /** 设备ID */ + /** + * 设备ID + */ private Long deviceId; - /** 设备编号 */ + /** + * 设备编号 + */ @Excel(name = "设备编号") private String deviceCode; - /** 设备名称 */ + /** + * 设备名称 + */ @Excel(name = "设备名称") private String deviceName; - /** 所属场景,关联hw_scene表的scene_id字段 */ + /** + * 所属场景,关联hw_scene表的scene_id字段 + */ @Excel(name = "所属场景,关联hw_scene表的scene_id字段") private Long sceneId; - /** 所属监控单元,关联表hw_monitor_unit字段monitor_unit_id */ + /** + * 所属监控单元,关联表hw_monitor_unit字段monitor_unit_id + */ @Excel(name = "所属监控单元,关联表hw_monitor_unit字段monitor_unit_id") private Long monitorUnitId; - /** 设备类型(1:网关设备,2:网关子设备,3:直连设备) */ + /** + * 设备类型(1:网关设备,2:网关子设备,3:直连设备) + */ @Excel(name = "设备类型", readConverterExp = "1=:网关设备,2:网关子设备,3:直连设备") private String deviceType; - /** 联网方式(1:Wi-Fi,2、蜂窝(2G/3G/4G/5G),3、以太网,4、其他) */ + /** + * 联网方式(1:Wi-Fi,2、蜂窝(2G/3G/4G/5G),3、以太网,4、其他) + */ @Excel(name = "联网方式(1:Wi-Fi,2、蜂窝(2G/3G/4G/5G),3、以太网,4、其他)") private String networkingMode; - /** 接入协议(1、MQTT) */ + /** + * 接入协议(1、MQTT) + */ @Excel(name = "接入协议", readConverterExp = "1=、MQTT") private Long accessProtocol; - /** 数据格式(1、Json) */ + /** + * 数据格式(1、Json) + */ @Excel(name = "数据格式", readConverterExp = "1=、Json") private Long dataFormat; - /** 关联设备,hw_device表中国的device_id */ + /** + * 关联设备,hw_device表中国的device_id + */ @Excel(name = "关联设备,hw_device表中国的device_id") private Long releatedDeviceId; - /** 设备模型,关联表hw_device_mode的字段device_mode_id */ + /** + * 设备模型,关联表hw_device_mode的字段device_mode_id + */ @Excel(name = "设备模型,关联表hw_device_mode的字段device_mode_id") private Long deviceModeId; - /** 接入网关协议(1、Modbus) - OPC-UA,Modbus,北向接入(阿里云、小度接入),南向接入,视频类接入,通道类接入,自定义接入 */ + /** + * 接入网关协议(1、Modbus) + * OPC-UA,Modbus,北向接入(阿里云、小度接入),南向接入,视频类接入,通道类接入,自定义接入 + */ @Excel(name = "接入网关协议", readConverterExp = "1=、Modbus") private Long accessGwProtocol; - /** 激活状态(1、激活,0、未激活) */ + /** + * 激活状态(1、激活,0、未激活) + */ @Excel(name = "激活状态", readConverterExp = "1=、激活,0、未激活") private String activeStatus; - /** 设备状态(0、测试,1、发布,9、删除) */ + /** + * 设备状态(0、测试,1、发布,9、删除) + */ @Excel(name = "设备状态", readConverterExp = "0=、测试,1、发布,9、删除") private String deviceStatus; - /** 设备激活时间 */ + /** + * 设备激活时间 + */ @JsonFormat(pattern = "yyyy-MM-dd") @Excel(name = "设备激活时间", width = 30, dateFormat = "yyyy-MM-dd") private Date activeTime; - /** 设备图片地址(如果为空则可以使用设备模型图片) */ + /** + * 设备图片地址(如果为空则可以使用设备模型图片) + */ @Excel(name = "设备图片地址", readConverterExp = "如=果为空则可以使用设备模型图片") private String devicePic; - /** 网络地址 */ + /** + * 网络地址 + */ @Excel(name = "网络地址") private String ipAddress; - /** 预留字段,设备所在区域ID,关联表hw_area字段area_id) */ + /** + * 预留字段,设备所在区域ID,关联表hw_area字段area_id) + */ @Excel(name = "预留字段,设备所在区域ID,关联表hw_area字段area_id)") private Long areaId; - /** 预留字段,设备位置(手动定位(在地图上选择设备所在的固定位置),自动定位(设备自动定位位置,关联网关自动定位位置。目前仅G780V2(固件版本V2.2.0之后)、PLCNET510、G800 V2和ModbusRTU(云端轮询)定位型变量支持选择自动定位功能)) */ + /** + * 预留字段,设备位置(手动定位(在地图上选择设备所在的固定位置),自动定位(设备自动定位位置,关联网关自动定位位置。目前仅G780V2(固件版本V2.2.0之后)、PLCNET510、G800 V2和ModbusRTU(云端轮询)定位型变量支持选择自动定位功能)) + */ @Excel(name = "预留字段,设备位置", readConverterExp = "手=动定位(在地图上选择设备所在的固定位置") private String deviceLocation; - /** 当前固件版本(Linux系统) */ + /** + * 当前固件版本(Linux系统) + */ @Excel(name = "当前固件版本(Linux系统)") private String currentModuleVersion; - /** 当前单片机固件版本 */ + /** + * 当前单片机固件版本 + */ @Excel(name = "当前单片机固件版本") private String currentSinglechipVersion; - /** 预留字段 */ + /** + * 预留字段 + */ @Excel(name = "预留字段") private String deviceField; - /** 租户ID,关联hw_tenant的tenant_id */ + /** + * 租户ID,关联hw_tenant的tenant_id + */ @Excel(name = "租户ID,关联hw_tenant的tenant_id") private Long tenantId; - /** 在线状态(1、在线,0、离线) */ + /** + * 在线状态(1、在线,0、离线) + */ @Excel(name = "在线状态", readConverterExp = "1=、在线,0、离线") private String onlineStatus; private String monitorUnitName; - public void setDeviceId(Long deviceId) - { + private String sceneName; + private String deviceModeName; + private String tenantName; + + + public void setDeviceId(Long deviceId) { this.deviceId = deviceId; } - public Long getDeviceId() - { + public Long getDeviceId() { return deviceId; } - public void setDeviceCode(String deviceCode) - { + + public void setDeviceCode(String deviceCode) { this.deviceCode = deviceCode; } - public String getDeviceCode() - { + public String getDeviceCode() { return deviceCode; } - public void setDeviceName(String deviceName) - { + + public void setDeviceName(String deviceName) { this.deviceName = deviceName; } - public String getDeviceName() - { + public String getDeviceName() { return deviceName; } - public void setSceneId(Long sceneId) - { + + public void setSceneId(Long sceneId) { this.sceneId = sceneId; } - public Long getSceneId() - { + public Long getSceneId() { return sceneId; } - public void setMonitorUnitId(Long monitorUnitId) - { + + public void setMonitorUnitId(Long monitorUnitId) { this.monitorUnitId = monitorUnitId; } - public Long getMonitorUnitId() - { + public Long getMonitorUnitId() { return monitorUnitId; } - public void setDeviceType(String deviceType) - { + + public void setDeviceType(String deviceType) { this.deviceType = deviceType; } - public String getDeviceType() - { + public String getDeviceType() { return deviceType; } - public void setNetworkingMode(String networkingMode) - { + + public void setNetworkingMode(String networkingMode) { this.networkingMode = networkingMode; } - public String getNetworkingMode() - { + public String getNetworkingMode() { return networkingMode; } - public void setAccessProtocol(Long accessProtocol) - { + + public void setAccessProtocol(Long accessProtocol) { this.accessProtocol = accessProtocol; } - public Long getAccessProtocol() - { + public Long getAccessProtocol() { return accessProtocol; } - public void setDataFormat(Long dataFormat) - { + + public void setDataFormat(Long dataFormat) { this.dataFormat = dataFormat; } - public Long getDataFormat() - { + public Long getDataFormat() { return dataFormat; } - public void setReleatedDeviceId(Long releatedDeviceId) - { + + public void setReleatedDeviceId(Long releatedDeviceId) { this.releatedDeviceId = releatedDeviceId; } - public Long getReleatedDeviceId() - { + public Long getReleatedDeviceId() { return releatedDeviceId; } - public void setDeviceModeId(Long deviceModeId) - { + + public void setDeviceModeId(Long deviceModeId) { this.deviceModeId = deviceModeId; } - public Long getDeviceModeId() - { + public Long getDeviceModeId() { return deviceModeId; } - public void setAccessGwProtocol(Long accessGwProtocol) - { + + public void setAccessGwProtocol(Long accessGwProtocol) { this.accessGwProtocol = accessGwProtocol; } - public Long getAccessGwProtocol() - { + public Long getAccessGwProtocol() { return accessGwProtocol; } - public void setActiveStatus(String activeStatus) - { + + public void setActiveStatus(String activeStatus) { this.activeStatus = activeStatus; } - public String getActiveStatus() - { + public String getActiveStatus() { return activeStatus; } - public void setDeviceStatus(String deviceStatus) - { + + public void setDeviceStatus(String deviceStatus) { this.deviceStatus = deviceStatus; } - public String getDeviceStatus() - { + public String getDeviceStatus() { return deviceStatus; } - public void setActiveTime(Date activeTime) - { + + public void setActiveTime(Date activeTime) { this.activeTime = activeTime; } - public Date getActiveTime() - { + public Date getActiveTime() { return activeTime; } - public void setDevicePic(String devicePic) - { + + public void setDevicePic(String devicePic) { this.devicePic = devicePic; } - public String getDevicePic() - { + public String getDevicePic() { return devicePic; } - public void setIpAddress(String ipAddress) - { + + public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; } - public String getIpAddress() - { + public String getIpAddress() { return ipAddress; } - public void setAreaId(Long areaId) - { + + public void setAreaId(Long areaId) { this.areaId = areaId; } - public Long getAreaId() - { + public Long getAreaId() { return areaId; } - public void setDeviceLocation(String deviceLocation) - { + + public void setDeviceLocation(String deviceLocation) { this.deviceLocation = deviceLocation; } - public String getDeviceLocation() - { + public String getDeviceLocation() { return deviceLocation; } - public void setCurrentModuleVersion(String currentModuleVersion) - { + + public void setCurrentModuleVersion(String currentModuleVersion) { this.currentModuleVersion = currentModuleVersion; } - public String getCurrentModuleVersion() - { + public String getCurrentModuleVersion() { return currentModuleVersion; } - public void setCurrentSinglechipVersion(String currentSinglechipVersion) - { + + public void setCurrentSinglechipVersion(String currentSinglechipVersion) { this.currentSinglechipVersion = currentSinglechipVersion; } - public String getCurrentSinglechipVersion() - { + public String getCurrentSinglechipVersion() { return currentSinglechipVersion; } - public void setDeviceField(String deviceField) - { + + public void setDeviceField(String deviceField) { this.deviceField = deviceField; } - public String getDeviceField() - { + public String getDeviceField() { return deviceField; } - public void setTenantId(Long tenantId) - { + + public void setTenantId(Long tenantId) { this.tenantId = tenantId; } - public Long getTenantId() - { + public Long getTenantId() { return tenantId; } - public void setOnlineStatus(String onlineStatus) - { + + public void setOnlineStatus(String onlineStatus) { this.onlineStatus = onlineStatus; } - public String getOnlineStatus() - { + public String getOnlineStatus() { return onlineStatus; } @@ -341,38 +369,62 @@ public class HwDevice extends BaseEntity this.monitorUnitName = monitorUnitName; } + public String getSceneName() { + return sceneName; + } + + public void setSceneName(String sceneName) { + this.sceneName = sceneName; + } + + public String getDeviceModeName() { + return deviceModeName; + } + + public void setDeviceModeName(String deviceModeName) { + this.deviceModeName = deviceModeName; + } + + public String getTenantName() { + return tenantName; + } + + public void setTenantName(String tenantName) { + this.tenantName = tenantName; + } + @Override public String toString() { - return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) - .append("deviceId", getDeviceId()) - .append("deviceCode", getDeviceCode()) - .append("deviceName", getDeviceName()) - .append("sceneId", getSceneId()) - .append("monitorUnitId", getMonitorUnitId()) - .append("deviceType", getDeviceType()) - .append("networkingMode", getNetworkingMode()) - .append("accessProtocol", getAccessProtocol()) - .append("dataFormat", getDataFormat()) - .append("releatedDeviceId", getReleatedDeviceId()) - .append("deviceModeId", getDeviceModeId()) - .append("accessGwProtocol", getAccessGwProtocol()) - .append("activeStatus", getActiveStatus()) - .append("deviceStatus", getDeviceStatus()) - .append("activeTime", getActiveTime()) - .append("devicePic", getDevicePic()) - .append("ipAddress", getIpAddress()) - .append("areaId", getAreaId()) - .append("deviceLocation", getDeviceLocation()) - .append("currentModuleVersion", getCurrentModuleVersion()) - .append("currentSinglechipVersion", getCurrentSinglechipVersion()) - .append("remark", getRemark()) - .append("createBy", getCreateBy()) - .append("createTime", getCreateTime()) - .append("updateBy", getUpdateBy()) - .append("updateTime", getUpdateTime()) - .append("deviceField", getDeviceField()) - .append("tenantId", getTenantId()) - .append("onlineStatus", getOnlineStatus()) - .toString(); + return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) + .append("deviceId", getDeviceId()) + .append("deviceCode", getDeviceCode()) + .append("deviceName", getDeviceName()) + .append("sceneId", getSceneId()) + .append("monitorUnitId", getMonitorUnitId()) + .append("deviceType", getDeviceType()) + .append("networkingMode", getNetworkingMode()) + .append("accessProtocol", getAccessProtocol()) + .append("dataFormat", getDataFormat()) + .append("releatedDeviceId", getReleatedDeviceId()) + .append("deviceModeId", getDeviceModeId()) + .append("accessGwProtocol", getAccessGwProtocol()) + .append("activeStatus", getActiveStatus()) + .append("deviceStatus", getDeviceStatus()) + .append("activeTime", getActiveTime()) + .append("devicePic", getDevicePic()) + .append("ipAddress", getIpAddress()) + .append("areaId", getAreaId()) + .append("deviceLocation", getDeviceLocation()) + .append("currentModuleVersion", getCurrentModuleVersion()) + .append("currentSinglechipVersion", getCurrentSinglechipVersion()) + .append("remark", getRemark()) + .append("createBy", getCreateBy()) + .append("createTime", getCreateTime()) + .append("updateBy", getUpdateBy()) + .append("updateTime", getUpdateTime()) + .append("deviceField", getDeviceField()) + .append("tenantId", getTenantId()) + .append("onlineStatus", getOnlineStatus()) + .toString(); } } diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/mapper/HwDeviceMapper.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/mapper/HwDeviceMapper.java index e9dbf24..ff8246e 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/mapper/HwDeviceMapper.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/mapper/HwDeviceMapper.java @@ -80,4 +80,12 @@ public interface HwDeviceMapper public List selectLinkedDevices(Long deviceId); + + /** + * 查询设备信息列表,join监控单元、设备模型等表 + * + * @param hwDevice 设备信息 + * @return 设备信息集合 + */ + public List selectHwDeviceJoinList(HwDevice hwDevice); } diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/IHwDeviceService.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/IHwDeviceService.java index 4c1d0d8..517869f 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/IHwDeviceService.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/IHwDeviceService.java @@ -7,6 +7,7 @@ import com.ruoyi.business.domain.HwDevice; import com.ruoyi.business.domain.VO.DeviceModeVo; import com.ruoyi.business.domain.VO.HwDeviceVo; import com.ruoyi.business.domain.VO.HwMonitorUnitVo; +import com.ruoyi.common.datascope.annotation.DataScope; /** @@ -95,4 +96,12 @@ public interface IHwDeviceService */ public Map> getDevicesByMonitor(HwDevice queryHwDevice); + /** + * 查询设备信息列表,join监控单元、设备模型等表 + * + * @param hwDevice 设备信息 + * @return 设备信息 + */ + + public List selectHwDeviceJoinList(HwDevice hwDevice); } diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwDeviceServiceImpl.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwDeviceServiceImpl.java index bac0b7e..bbfbeef 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwDeviceServiceImpl.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwDeviceServiceImpl.java @@ -1,30 +1,43 @@ package com.ruoyi.business.service.impl; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; - +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.shaded.com.google.gson.JsonElement; +import com.ruoyi.business.domain.HwDevice; import com.ruoyi.business.domain.HwDeviceModeFunction; +import com.ruoyi.business.domain.HwScene; import com.ruoyi.business.domain.VO.DeviceModeVo; import com.ruoyi.business.domain.VO.HwDeviceVo; +import com.ruoyi.business.mapper.HwDeviceMapper; import com.ruoyi.business.mapper.HwDeviceModeFunctionMapper; +import com.ruoyi.business.mapper.HwSceneMapper; +import com.ruoyi.business.service.IHwDeviceService; +import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.HwDictConstants; import com.ruoyi.common.core.constant.TdEngineConstants; +import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.enums.DataTypeEnums; +import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.utils.DateUtils; import com.ruoyi.common.core.utils.NumberUtils; +import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.datascope.annotation.DataScope; import com.ruoyi.tdengine.api.RemoteTdEngineService; -import com.ruoyi.tdengine.api.domain.TdHistorySelectDto; +import com.ruoyi.tdengine.api.domain.AlterTagVo; +import com.ruoyi.tdengine.api.domain.TdField; import com.ruoyi.tdengine.api.domain.TdSelectDto; +import com.ruoyi.tdengine.api.domain.TdTableVo; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; -import com.ruoyi.business.mapper.HwDeviceMapper; -import com.ruoyi.business.domain.HwDevice; -import com.ruoyi.business.service.IHwDeviceService; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * 设备信息Service业务层处理 @@ -37,9 +50,13 @@ public class HwDeviceServiceImpl implements IHwDeviceService { @Autowired private HwDeviceMapper hwDeviceMapper; @Autowired + private HwSceneMapper hwSceneMapper; + @Autowired private HwDeviceModeFunctionMapper hwDevieModeFunctionMapper; @Autowired - private RemoteTdEngineService remoteTdEgineService; + private RemoteTdEngineService remoteTdEngineService; + @Autowired + private StringRedisTemplate redisTemplate; /** * 查询设备信息 @@ -64,6 +81,7 @@ public class HwDeviceServiceImpl implements IHwDeviceService { public HwDevice selectHwDeviceJoinByDeviceId(Long deviceId) { return hwDeviceMapper.selectHwDeviceJoinByDeviceId(deviceId); } + /** * 查询设备信息列表 * @@ -93,11 +111,116 @@ public class HwDeviceServiceImpl implements IHwDeviceService { * @param hwDevice 设备信息 * @return 结果 */ + @Transactional @Override public int insertHwDevice(HwDevice hwDevice) { hwDevice.setCreateTime(DateUtils.getNowDate()); + String deviceType = hwDevice.getDeviceType(); + int deviceId = hwDeviceMapper.insertHwDevice(hwDevice); + if (deviceType.equals(HwDictConstants.DEVICE_TYPE_GATEWAY_DEVICE)) { + this.updateMqttAuth(hwDevice); + } else if (deviceType.equals(HwDictConstants.DEVICE_TYPE_DIRECT_CONNECT_DEVICE)) { + this.updateMqttAuth(hwDevice); + this.createTdTable(hwDevice); + } else if (deviceType.equals(HwDictConstants.DEVICE_TYPE_GATEWAY_SUB_EQUIPMENT)) { + this.createTdTable(hwDevice); + } + return deviceId; + } + + /** + * @param: hwDevice + * @description 更新redis中的设备信息 + * @author xins + * @date 2023-09-19 10:06 + */ + private void updateMqttAuth(HwDevice hwDevice) { + try { + HwScene scene = hwSceneMapper.selectHwSceneBySceneId(hwDevice.getSceneId()); + String modeAccount = scene.getModeAccount(); + String modeKey = scene.getModeKey(); + if (StringUtils.isNotEmpty(modeAccount) && StringUtils.isNotEmpty(modeKey)) { + String deviceInfoStr = redisTemplate.opsForValue().get(HwDictConstants.REDIS_KEY_DEVICE_INFO); + JSONArray deviceInfoJsonArr = new JSONArray(); +// JSONObject deviceInfoJson; + if (StringUtils.isNotEmpty(deviceInfoStr)) { + deviceInfoJsonArr = JSON.parseArray(deviceInfoStr); + } + boolean redisUpdated = false;//是否更新已有的值 + for (Object deviceInfoObj : deviceInfoJsonArr) { + JSONObject deviceInfoJson = (JSONObject) deviceInfoObj; + if (deviceInfoJson.getString("deviceCode").equals(hwDevice.getDeviceCode())) { + deviceInfoJson.put("username", modeAccount); + deviceInfoJson.put("password", modeKey); + redisUpdated = true; + } + } - return hwDeviceMapper.insertHwDevice(hwDevice); + if (!redisUpdated) { + JSONObject deviceInfoJson = new JSONObject(); + deviceInfoJson.put("deviceCode", hwDevice.getDeviceCode()); + deviceInfoJson.put("username", modeAccount); + deviceInfoJson.put("password", modeKey); + deviceInfoJsonArr.add(deviceInfoJson); + } + + redisTemplate.opsForValue().set(HwDictConstants.REDIS_KEY_DEVICE_INFO, deviceInfoJsonArr.toJSONString()); + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + + /** + * @param: hwDevice + * @description 在tdengine创建table + * @author xins + * @date 2023-09-19 10:07 + */ + private void createTdTable(HwDevice hwDevice) { + TdTableVo tdTableVo = new TdTableVo(); + String databaseName = TdEngineConstants.getDatabaseName(1L); + String superTableName = TdEngineConstants.getSuperTableName(hwDevice.getDeviceModeId()); + String tableName = TdEngineConstants.getTableName(hwDevice.getDeviceId()); + + List tagsFields = new ArrayList<>(); + + TdField deviceIdTag = new TdField(); + deviceIdTag.setFieldName(TdEngineConstants.ST_TAG_DEVICEID); + deviceIdTag.setFieldValue(hwDevice.getDeviceId()); + tagsFields.add(deviceIdTag); + + TdField deviceCodeTag = new TdField(); + deviceCodeTag.setFieldName(TdEngineConstants.ST_TAG_DEVICECODE); + deviceCodeTag.setFieldValue(hwDevice.getDeviceCode()); + deviceCodeTag.setDataTypeCode(DataTypeEnums.NCHAR.getDataCode()); + tagsFields.add(deviceCodeTag); + + TdField deviceNameTag = new TdField(); + deviceNameTag.setFieldName(TdEngineConstants.ST_TAG_DEVICENAME); + deviceNameTag.setFieldValue(hwDevice.getDeviceName()); + deviceNameTag.setDataTypeCode(DataTypeEnums.NCHAR.getDataCode()); + tagsFields.add(deviceNameTag); + + TdField deviceModeIdTag = new TdField(); + deviceModeIdTag.setFieldName(TdEngineConstants.ST_TAG_DEVICEMODEID); + deviceModeIdTag.setFieldValue(hwDevice.getDeviceModeId()); + tagsFields.add(deviceModeIdTag); + + TdField monitorUnitIdTag = new TdField(); + monitorUnitIdTag.setFieldName(TdEngineConstants.ST_TAG_MONITORUNITID); + monitorUnitIdTag.setFieldValue(hwDevice.getMonitorUnitId()); + tagsFields.add(monitorUnitIdTag); + + tdTableVo.setDatabaseName(databaseName); + tdTableVo.setSuperTableName(superTableName); + tdTableVo.setTableName(tableName); + tdTableVo.setTagsFieldValues(tagsFields); + + R tdReturnMsg = this.remoteTdEngineService.createTable(tdTableVo); + if (tdReturnMsg.getCode() != Constants.SUCCESS) {//抛出异常,回滚事务 + throw new RuntimeException(tdReturnMsg.getMsg()); + } } /** @@ -147,10 +270,72 @@ public class HwDeviceServiceImpl implements IHwDeviceService { * @param hwDevice 设备信息 * @return 结果 */ + @Transactional @Override public int updateHwDevice(HwDevice hwDevice) { + HwDevice dbDevice = hwDeviceMapper.selectHwDeviceByDeviceId(hwDevice.getDeviceId()); + if (dbDevice.getDeviceStatus().equals(HwDictConstants.DEVICE_STATUS_PUBLISH)) { + throw new ServiceException("已发布状态不能修改"); + } hwDevice.setUpdateTime(DateUtils.getNowDate()); - return hwDeviceMapper.updateHwDevice(hwDevice); + int rows = hwDeviceMapper.updateHwDevice(hwDevice); + this.updateTdEngine(hwDevice, dbDevice); + return rows; + } + + /** + * @param: hwDevice 前端传的对象 + * @param: dbDevice 目前数据库的对象 + * @description 更新TdEngine数据库tag + * @author xins + * @date 2023-09-19 10:55 + */ + private void updateTdEngine(HwDevice hwDevice, HwDevice dbDevice) { + String deviceType = hwDevice.getDeviceType(); + String databaseName = TdEngineConstants.getDatabaseName(1L);//TODO + String tableName = TdEngineConstants.getTableName(hwDevice.getDeviceId()); + AlterTagVo alterTagVo = new AlterTagVo(); + alterTagVo.setDatabaseName(databaseName); + alterTagVo.setTableName(tableName); + R tdReturnMsg; + if (deviceType.equals(HwDictConstants.DEVICE_TYPE_DIRECT_CONNECT_DEVICE) + || deviceType.equals(HwDictConstants.DEVICE_TYPE_GATEWAY_SUB_EQUIPMENT)) { + if (!hwDevice.getDeviceCode().equals(dbDevice.getDeviceCode())) { + alterTagVo.setTagName(TdEngineConstants.ST_TAG_DEVICECODE); + alterTagVo.setTagValue("'"+hwDevice.getDeviceCode()+"'"); + + tdReturnMsg = this.remoteTdEngineService.alterTableTag(alterTagVo); + if (tdReturnMsg.getCode() != Constants.SUCCESS) {//抛出异常,回滚事务 + throw new RuntimeException(tdReturnMsg.getMsg()); + } + } + if (!hwDevice.getDeviceName().equals(dbDevice.getDeviceName())) { + alterTagVo.setTagName(TdEngineConstants.ST_TAG_DEVICENAME); + alterTagVo.setTagValue("'"+hwDevice.getDeviceName()+"'"); + tdReturnMsg = this.remoteTdEngineService.alterTableTag(alterTagVo); + if (tdReturnMsg.getCode() != Constants.SUCCESS) {//抛出异常,回滚事务 + throw new RuntimeException(tdReturnMsg.getMsg()); + } + } + + if (!hwDevice.getMonitorUnitId().equals(dbDevice.getMonitorUnitId())) { + alterTagVo.setTagName(TdEngineConstants.ST_TAG_MONITORUNITID); + alterTagVo.setTagValue(hwDevice.getMonitorUnitId()); + tdReturnMsg = this.remoteTdEngineService.alterTableTag(alterTagVo); + if (tdReturnMsg.getCode() != Constants.SUCCESS) {//抛出异常,回滚事务 + throw new RuntimeException(tdReturnMsg.getMsg()); + } + } + + if (!hwDevice.getDeviceModeId().equals(dbDevice.getDeviceModeId())) { + alterTagVo.setTagName(TdEngineConstants.ST_TAG_DEVICEMODEID); + alterTagVo.setTagValue(hwDevice.getDeviceModeId()); + tdReturnMsg = this.remoteTdEngineService.alterTableTag(alterTagVo); + if (tdReturnMsg.getCode() != Constants.SUCCESS) {//抛出异常,回滚事务 + throw new RuntimeException(tdReturnMsg.getMsg()); + } + } + } } /** @@ -177,14 +362,14 @@ public class HwDeviceServiceImpl implements IHwDeviceService { /** - * @return Map> + * @return Map> * @description 设备监测页面,根据监控单元获取设备监控信息 * @author xins * @date 2023-09-15 16:59 */ @Override - public Map> getDevicesByMonitor(HwDevice queryHwDevice) { - Map> devicesMap = new HashMap>(); + public Map> getDevicesByMonitor(HwDevice queryHwDevice) { + Map> devicesMap = new HashMap>(); List controlDeviceVos = new ArrayList(); List acquisitionDeviceVos = new ArrayList(); Long sceneId = queryHwDevice.getSceneId(); @@ -198,7 +383,7 @@ public class HwDeviceServiceImpl implements IHwDeviceService { TdSelectDto tdSelectDto = new TdSelectDto(); tdSelectDto.setDatabaseName(databaseName); tdSelectDto.setTableName(tableName); - List> deviceLatestDataMapList = (List>) this.remoteTdEgineService.getLatestData(tdSelectDto).getData(); + List> deviceLatestDataMapList = (List>) this.remoteTdEngineService.getLatestData(tdSelectDto).getData(); HwDeviceModeFunction queryDeviceModeFunction = new HwDeviceModeFunction(); queryDeviceModeFunction.setDeviceModeId(hwDevice.getDeviceModeId()); diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwMonitorPlatformServiceImpl.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwMonitorPlatformServiceImpl.java index 135aaff..6c92963 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwMonitorPlatformServiceImpl.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/service/impl/HwMonitorPlatformServiceImpl.java @@ -39,10 +39,10 @@ public class HwMonitorPlatformServiceImpl implements IHwMonitorPlatformService { public List> selectLatestDataByTags(DeviceLatestDataVo deviceLatestDataVo) { TdSuperTableSelectVo tdSuperTableSelectVo = new TdSuperTableSelectVo(); String databaseName = TdEngineConstants.getDatabaseName(deviceLatestDataVo.getSceneId()); - String superTableName = TdEngineConstants.getSupertTableName(deviceLatestDataVo.getDeviceModeId()); + String superTableName = TdEngineConstants.getSuperTableName(deviceLatestDataVo.getDeviceModeId()); tdSuperTableSelectVo.setDatabaseName(databaseName); tdSuperTableSelectVo.setSuperTableName(superTableName); - tdSuperTableSelectVo.setGroupByTagsName(TdEngineConstants.ST_TAG_DEVICEID); + tdSuperTableSelectVo.setGroupByTagsName(TdEngineConstants.ST_TAG_DEVICECODE);//todo,换成deviceid if (deviceLatestDataVo.getStartTime() != 0 || deviceLatestDataVo.getEndTime() != 0) { tdSuperTableSelectVo.setFirstFieldName(TdEngineConstants.DEFAULT_FIRST_FIELD_NAME); } diff --git a/ruoyi-modules/hw-business/src/main/resources/mapper/business/HwSceneMapper.xml b/ruoyi-modules/hw-business/src/main/resources/mapper/business/HwSceneMapper.xml index 9ce06f7..febd6cc 100644 --- a/ruoyi-modules/hw-business/src/main/resources/mapper/business/HwSceneMapper.xml +++ b/ruoyi-modules/hw-business/src/main/resources/mapper/business/HwSceneMapper.xml @@ -54,7 +54,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select scene_id, scene_name, tenant_id, scene_mode_id, scene_pic, default_flag, scene_status, auth_mode, mode_account, mode_key, mode_secret, preserve_time, test_preserve_time, remark, create_by, create_time, update_by, update_time, scene_environment, scene_field from hw_scene + select scene_id, scene_name, tenant_id, scene_mode_id, scene_pic, default_flag, scene_status, auth_mode, mode_account, mode_key, mode_secret, preserve_time, test_preserve_time, remark, create_by, create_time, update_by, update_time, scene_environment, scene_field from hw_scene hs