Browse Source

动环状态代码提交

jingyuanchao 1 year ago
parent
commit
c03ea7a61e

+ 3 - 2
project_data/sql/0.1.1/soc/soc.sql

@@ -786,7 +786,7 @@ DROP TABLE IF EXISTS `iot_device_info`;
 CREATE TABLE `iot_device_info`  (
                                     `id` bigint(0) NOT NULL,
                                     `device_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '设备类型',
-                                    `device_product` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '设备品牌',
+                                    `device_product` varchar(62) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '设备品牌',
                                     `device_model` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '设备型号',
                                     `device_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '设备编码',
                                     `device_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '设备名称',
@@ -828,10 +828,11 @@ DROP TABLE IF EXISTS `iot_device_status`;
 CREATE TABLE `iot_device_status` (
                                      `id` bigint NOT NULL,
                                      `device_id` bigint DEFAULT NULL COMMENT '设备id',
-                                     `device_product` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备品牌',
+                                     `device_product` varchar(62) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备品牌',
                                      `device_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备编码',
                                      `device_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备名称',
                                      `iot_token` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'iot服务唯一编码',
+                                     `unique_code` varchar(225) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备编码',
                                      `org_id` bigint DEFAULT NULL COMMENT '机构id',
                                      `org_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '机构名称',
                                      `org_path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '机构路径',

+ 18 - 20
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/domain/iot/IotDeviceStatus.java

@@ -1,5 +1,7 @@
 package com.xunmei.system.api.domain.iot;
 
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.xunmei.common.core.web.domain.BaseEntity;
 import io.swagger.annotations.ApiModel;
@@ -7,8 +9,6 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
 
 /**
  * 【请填写功能名称】对象 iot_device_status
@@ -24,53 +24,51 @@ import org.apache.commons.lang3.builder.ToStringStyle;
 public class IotDeviceStatus extends BaseEntity {
     private static final long serialVersionUID = 1L;
 
+    @TableId
     private Long id;
 
+    @TableField(value = "device_id")
     @ApiModelProperty(value = "设备id")
     private Long deviceId;
 
+    @TableField(value = "device_product")
     @ApiModelProperty(value = "设备品牌")
     private String deviceProduct;
 
+    @TableField(value = "device_code")
     @ApiModelProperty(value = "设备编码")
     private String deviceCode;
 
+    @TableField(value = "device_name")
     @ApiModelProperty(value = "设备名称")
     private String deviceName;
 
+    @TableField(value = "iot_token")
     @ApiModelProperty(value = "iot服务唯一编码")
     private String iotToken;
 
+    @TableField(value = "unique_code")
+    @ApiModelProperty(value = "设备唯一编码")
+    private String uniqueCode;
+
+    @TableField(value = "org_id")
     @ApiModelProperty(value = "机构id")
     private Long orgId;
 
+    @TableField(value = "org_name")
     @ApiModelProperty(value = "机构名称")
     private String orgName;
 
+    @TableField(value = "org_path")
     @ApiModelProperty(value = "机构路径")
     private String orgPath;
 
+    @TableField(value = "info")
     @ApiModelProperty(value = "设备状态数据")
     private String info;
 
 
-    @Override
-    public String toString() {
-        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
-                .append("id", getId())
-                .append("deviceId", getDeviceId())
-                .append("deviceProduct", getDeviceProduct())
-                .append("deviceCode", getDeviceCode())
-                .append("deviceName", getDeviceName())
-                .append("iotToken", getIotToken())
-                .append("orgId", getOrgId())
-                .append("orgName", getOrgName())
-                .append("orgPath", getOrgPath())
-                .append("info", getInfo())
-                .append("createTime", getCreateTime())
-                .append("updateTime", getUpdateTime())
-                .append("createBy", getCreateBy())
-                .append("updateBy", getUpdateBy())
-                .toString();
+    public void setUniqueCode() {
+        this.uniqueCode = this.iotToken + "_" + this.deviceProduct + "_" + this.deviceCode;
     }
 }

+ 34 - 7
soc-common/soc-common-core/src/main/java/com/xunmei/common/core/enums/iot/SensorType.java

@@ -1,5 +1,7 @@
 package com.xunmei.common.core.enums.iot;
 
+import cn.hutool.core.util.ObjectUtil;
+import io.netty.util.internal.StringUtil;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 
@@ -11,24 +13,40 @@ import java.util.Map;
 public enum SensorType {
 
 
-    WATER_INTRUSION(4184, "水浸"),
+    WATER_INTRUSION(4184, "水浸","FSU_Water"),
 
-    SMOKE_SENSOR(4182, "烟感"),
+    SMOKE_SENSOR(4182, "烟感","FSU_Smoke"),
 
-    HUMIDITY_TEMPERATURE_SENSOR(4183, "温湿度"),
+    HUMIDITY_TEMPERATURE_SENSOR(4183, "温湿度","FSU_TemperatureAndHumidity"),
 
-    GAS_SENSOR(41885, "燃气"),
+    GAS_SENSOR(41885, "燃气","FSU_Gas"),
 
-    DOOR_MAGNETIC_SENSOR(4188, "门磁"),
+    DOOR_MAGNETIC_SENSOR(4188, "门磁","FSU_DoorMagnetic"),
 
-    INFRARED_SENSOR(4181, "红外"),
+    INFRARED_SENSOR(4181, "红外","FSU_Infrared"),
 
-    ALARM_SENSOR(41881, "盗情");
+    ALARM_SENSOR(41881, "盗情", StringUtil.EMPTY_STRING),
+
+    SMART_METER(4160, "智能电表", "FSU_SmartMeter"),
+
+    AIR_CONDITIONER(415, "空调", "FSU_AirConditioner"),
+
+    UPS(408, "UPS", "FSU_Ups"),
+
+    DO_POWER_CONTROL(4186, "DO8小时控电", "FSU_DoPowerControl"),
+
+    //待确认
+    THREE_PHASE_AC_VOLTAGE(41881, "三相电电压传感器", "FSU_ThreePhaseACVoltage"),
+
+
+    ;
 
     private Integer code;
 
     private String desc;
 
+    private String productName;
+
 
     /**
      * 所有枚举
@@ -49,6 +67,15 @@ public enum SensorType {
         return e != null ? e.getDesc() : "";
     }
 
+    public static Integer getCodeByProduct(String productName) {
+        for (SensorType value : SensorType.values()) {
+            if (ObjectUtil.equal(value.getProductName(),productName)){
+                return value.getCode();
+            }
+        }
+        return null;
+    }
+
     /**
      * 根据code获取name
      */

+ 2 - 0
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/iot/service/IotDeviceStatusService.java

@@ -16,4 +16,6 @@ public interface IotDeviceStatusService extends IService<IotDeviceStatus> {
 
 
     WebsocketResult dealDeviceStatusChange(WebsocketExecuteReq executeReq);
+
+    IotDeviceStatus getByDeviceId(Long deviceId);
 }

+ 91 - 45
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/iot/service/impl/IotDeviceStatusServiceImpl.java

@@ -1,21 +1,23 @@
 package com.xunmei.mediator.iot.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.xunmei.common.core.utils.DateUtils;
-import com.xunmei.common.core.web.page.TableDataInfo;
+import com.xunmei.common.core.constant.Constants;
+import com.xunmei.common.core.enums.iot.SensorType;
 import com.xunmei.mediator.iot.mapper.IotDeviceStatusMapper;
 import com.xunmei.mediator.iot.service.IIotDeviceInfoService;
 import com.xunmei.mediator.iot.service.IotDeviceStatusService;
 import com.xunmei.mediator.websocket.dto.WebsocketExecuteReq;
 import com.xunmei.mediator.websocket.dto.WebsocketResult;
 import com.xunmei.mediator.websocket.enums.DeviceTypeEnum;
-import com.xunmei.mediator.websocket.enums.ProductEnums;
+import com.xunmei.system.api.domain.iot.IotDeviceInfo;
 import com.xunmei.system.api.domain.iot.IotDeviceStatus;
-import com.xunmei.system.api.enums.DeviceType;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -26,6 +28,7 @@ import org.springframework.stereotype.Service;
  * @author xunmei
  * @date 2024-07-18
  */
+@Slf4j
 @Service
 public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMapper, IotDeviceStatus> implements IotDeviceStatusService {
     @Autowired
@@ -33,46 +36,56 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
     @Autowired
     private IIotDeviceInfoService iotDeviceInfoService;
 
+
+    @Override
+    public IotDeviceStatus getByDeviceId(Long deviceId) {
+        LambdaQueryWrapper<IotDeviceStatus> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(IotDeviceStatus::getDeviceId, deviceId);
+        wrapper.last(Constants.LIMIT1);
+        return iotDeviceStatusMapper.selectOne(wrapper);
+    }
+
     /**
      * {
-     *   "topic": "/things/FSU_TemperatureAndHumidity/XXDevice/property/post",
-     *   "id":"xxx",  // Guid,消息IoT生成。
-     *   "timestamp": "2023-01-11T18:00:00 +08:00",
-     *   "headers":{
-     *     "topicType":"",// Topic类型,相对于topicCategory的细分
-     *     "topicCategory": "",// Topic分类
-     *     "productName": "FSU_TemperatureAndHumidity",
-     *     "deviceName": "XXDevice"
-     *   },
-     *   "payload":[
-     *     {
-     *       "propertyName": "temperature",// 应用方调用的属性名称
-     *       "args": 23.6,
-     *       "dataSpec": {
-     *         "defaultValue": 0,
-     *         "max": 5000,
-     *         "min": -300,
-     *         "step": 0.01,
-     *         "unit": "℃",
-     *         "unitName": "摄氏度",
-     *         "dataType": "Int"
-     *       }
-     *     },
-     *     {
-     *       "propertyName": "humidity",// 应用方调用的属性名称
-     *       "args": 23.6,
-     *       "dataSpec": {
-     *         "defaultValue": 0,
-     *         "max": 5000,
-     *         "min": -300,
-     *         "step": 0.01,
-     *         "unit": "%RH",
-     *         "unitName": "",
-     *         "dataType": "Int"
-     *       }
-     *     }
-     *   ]
+     * "topic": "/things/FSU_TemperatureAndHumidity/XXDevice/property/post",
+     * "id":"xxx",  // Guid,消息IoT生成。
+     * "timestamp": "2023-01-11T18:00:00 +08:00",
+     * "headers":{
+     * "topicType":"",// Topic类型,相对于topicCategory的细分
+     * "topicCategory": "",// Topic分类
+     * "productName": "FSU_TemperatureAndHumidity",
+     * "deviceName": "XXDevice"
+     * },
+     * "payload":[
+     * {
+     * "propertyName": "temperature",// 应用方调用的属性名称
+     * "args": 23.6,
+     * "dataSpec": {
+     * "defaultValue": 0,
+     * "max": 5000,
+     * "min": -300,
+     * "step": 0.01,
+     * "unit": "℃",
+     * "unitName": "摄氏度",
+     * "dataType": "Int"
      * }
+     * },
+     * {
+     * "propertyName": "humidity",// 应用方调用的属性名称
+     * "args": 23.6,
+     * "dataSpec": {
+     * "defaultValue": 0,
+     * "max": 5000,
+     * "min": -300,
+     * "step": 0.01,
+     * "unit": "%RH",
+     * "unitName": "",
+     * "dataType": "Int"
+     * }
+     * }
+     * ]
+     * }
+     *
      * @param req
      * @return
      */
@@ -84,8 +97,41 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
         final String productName = req.getProductName();
         final DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.valueOf(productName);
 
-        //iotDeviceInfoService.selectByTypeAndCode()
+        final IotDeviceInfo code = iotDeviceInfoService.selectByTypeAndCode(req.getToken(), deviceTypeEnum.getCode(), req.getProductName(), req.getDeviceName());
+        if (code == null) {
+            log.error("动环设备状态同步时,未能找到对应设备,token:{},productName:{},deviceNane:{}", req.getToken(), req.getProductName(), req.getDeviceName());
+            return new WebsocketResult();
+        }
+        final JSONArray array = dealStatusData(data);
+        IotDeviceStatus status = getByDeviceId(code.getId());
+        if (status == null) {
+            status = new IotDeviceStatus();
+            BeanUtils.copyProperties(code, status, "id");
+            status.setDeviceId(code.getId());
+            status.setId(IdWorker.getId());
+            status.setUniqueCode();
+            status.setInfo(JSON.toJSONString(array));
+            save(status);
+        }else {
+            status.setInfo(JSON.toJSONString(array));
+            updateById(status);
+        }
 
-        return null;
+        return new WebsocketResult();
+    }
+    private static JSONArray dealStatusData(JSONArray data) {
+        final JSONArray array = new JSONArray();
+        for (Object datum : data) {
+            JSONObject jsb = (JSONObject) datum;
+            JSONObject object = new JSONObject();
+            object.put("propertyName", jsb.get("propertyName"));
+            object.put("args", jsb.get("args"));
+            JSONObject dataSpec = JSON.parseObject(jsb.get("dataSpec").toString());
+            object.put("unit", dataSpec.get("unit"));
+            object.put("unitName", dataSpec.get("unitName"));
+            object.put("dataType", dataSpec.get("dataType"));
+            array.add(object);
+        }
+        return array;
     }
 }

+ 1 - 1
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/constant/WebSocketConstants.java

@@ -154,7 +154,7 @@ public interface WebSocketConstants {
 
     String STATUS_DESCRIPTION="statusDescription";
 
-    String DVS="DVS";
+    String DVS="Dvs";
 
     String ALARM_HOST="AlarmHost";
     String FSU_GATEWAY="FSU_Gateway";

+ 6 - 6
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/dto/WebsocketPayloadResolve.java

@@ -33,15 +33,15 @@ public class WebsocketPayloadResolve {
             JSONObject header = (JSONObject) map.get(WebSocketConstants.HEADER);
             String routingKey = ObjectUtil.isNotEmpty(event) ? event : service;
             JSONObject args = (JSONObject) (ObjectUtil.isNotEmpty(map.get(WebSocketConstants.DATA)) ? map.get(WebSocketConstants.DATA) : map.get(WebSocketConstants.ARGS));
-            resolve.setRoutingKey(routingKey);
-            resolve.setHeader(header);
-            resolve.setData(args);
+            this.routingKey=routingKey;
+            this.header=header;
+            this.data=args;
         }
         if (result.getPayload() instanceof JSONArray) {
             final JSONArray jsonArray = (JSONArray) result.getPayload();
-            resolve.setData(jsonArray);
-            resolve.setRoutingKey(StringUtil.EMPTY_STRING);
-            resolve.setHeader(result.getHeaders());
+            this.routingKey=StringUtil.EMPTY_STRING;
+            this.header=new JSONObject();
+            this.data=jsonArray;
         }
 
 

+ 1 - 1
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/enums/ProductEnums.java

@@ -14,7 +14,7 @@ public enum ProductEnums {
     DVS(new String[]{"VGSII_Hik","VGSII_DaHua"}),
     ALARM_HOST(new String[]{"InAnter_BM1600NTSmall", "Hik_DS19A", "HikModule", "FengYe_H402", "HengTong_CKWU01C", "MtaOCX", "CrossProcessDemo", "HoneywellOCX_IPM", "DaHuaAlarmHost", "BOSCH_CMS"}),
 
-    IOT_SERVER(new String[]{"IoTServer"}),
+    IOT_SERVER(new String[]{"IoTServer","IoTServerDevice"}),
     FSU_GATEWAY(new String[]{"FSU_Gateway","FSU_Smoke","FSU_Infrared","FSU_TemperatureAndHumidity","FSU_DoorMagnetic","FSU_Gas","FSU_RollingShutterDoor","FSU_Water","FSU_SmartMeter","FSU_AirConditioner","FSU_Ups","FSU_DoPowerControl","FSU_ThreePhaseACVoltage"}),
     ;
 

+ 1 - 1
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/service/RouterServiceHandler.java

@@ -40,7 +40,7 @@ public class RouterServiceHandler {
 
         return PRODUCT_HASH_MAP.get(enums)
                 .stream()
-                .filter(routerService -> routerService.routerKey().equals(routerKey))
+                .filter(routerService -> routerService.routerKey().contains(routerKey))
                 .findFirst()
                 .orElseThrow(() -> new RuntimeException("未找到对应的处理类"));
 

+ 9 - 2
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/service/impl/WebsocketServiceImpl.java

@@ -1,6 +1,7 @@
 package com.xunmei.mediator.websocket.service.impl;
 
 import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.xunmei.common.core.domain.iot.domain.IotAlarmDefenceArea;
@@ -48,6 +49,7 @@ import org.springframework.transaction.annotation.Transactional;
 import java.time.LocalDateTime;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 @Service
 public class WebsocketServiceImpl implements WebsocketService, RouterService {
@@ -676,6 +678,10 @@ public class WebsocketServiceImpl implements WebsocketService, RouterService {
         info.setOrgName(serverInfo.getOrgName());
         info.setOrgId(serverInfo.getOrgId());
         info.setOrgPath(serverInfo.getOrgPath());
+        info.setCreateTime(new Date());
+        info.setUpdateTime(new Date());
+        info.setCreateBy("system");
+        info.setUpdateBy("system");
         info.setId(id);
         return info;
     }
@@ -885,8 +891,9 @@ public class WebsocketServiceImpl implements WebsocketService, RouterService {
             if (WebSocketConstants.GET_DEVICE_BASE_INFOS.equals(req.getEvent())) {
                 JSONObject object = (JSONObject) req.getData();
                 if (object != null) {
-                    List<DeviceBaseInfo> deviceBaseInfos = (List<DeviceBaseInfo>) object.get("deviceBaseInfos");
-                    dealBaseDeviceInfo(deviceBaseInfos, req.getToken());
+                    List<JSONObject> deviceBaseInfos = (List<JSONObject>) object.get("deviceBaseInfos");
+                    final List<DeviceBaseInfo> list = deviceBaseInfos.stream().map(r -> JSON.toJavaObject(r, DeviceBaseInfo.class)).collect(Collectors.toList());
+                    dealBaseDeviceInfo(list, req.getToken());
                 }
             } else if (WebSocketConstants.GET_ALARM_HOST_DEVICE_INFOS.equals(req.getEvent())) {
                 dealAlarmHostBaseInfo((AlarmHostBaseInfo) req.getData(), req.getToken());