ソースを参照

Merge branch 'fu-normal-push' of uskycloud/usky-modules into master

gez 1 ヶ月 前
コミット
3eeff5d474

+ 16 - 20
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/DmpDeviceInfoController.java

@@ -1,28 +1,19 @@
 package com.usky.iot.controller.web;
 
-
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.backend.domain.*;
 import com.usky.common.core.bean.ApiResult;
 import com.usky.common.core.bean.CommonPage;
 import com.usky.common.core.utils.poi.ExcelUtil;
-import com.usky.iot.domain.BaseFacilityDevice;
 import com.usky.iot.domain.DmpDeviceInfo;
 import com.usky.iot.service.DmpDeviceInfoService;
-import com.usky.iot.service.vo.BaseFacilityDeviceVO;
-import com.usky.iot.service.vo.DmpDeviceInfoRequest;
-import com.usky.iot.service.vo.DmpTopicResponseVO;
+import com.usky.iot.service.vo.*;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
-import javax.validation.constraints.NotNull;
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -204,19 +195,24 @@ public class DmpDeviceInfoController {
 
     /**
      * 获取设备topic列表
+     * @param requestVO
+     * @return
+     */
+    @PostMapping("topic")
+    public ApiResult<CommonPage<DmpTopicResponseVO>> topic(@RequestBody DmpTopicRequestVO requestVO) {
+        return ApiResult.success(dmpDeviceInfoService.topic(requestVO));
+    }
+
+    /**
+     * 获取设备告警请求信息
+     * @param deviceId 设备id
      * @param productCode 产品编码
-     * @param deviceId 设备自身的唯一标识
-     * @param pageNum
-     * @param pageSize
      * @return
      */
-    @GetMapping("topic")
-    public ApiResult<CommonPage<DmpTopicResponseVO>> topic(@RequestParam(value = "productCode") String productCode,
-                                                     @RequestParam(value = "deviceId") String deviceId,
-                                                     @RequestParam(value = "deviceUuid") String deviceUuid,
-                                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
-                                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
-        return ApiResult.success(dmpDeviceInfoService.topic(productCode, deviceId, deviceUuid, pageNum, pageSize));
+    @GetMapping("alarmResponse")
+    public ApiResult<DmpHttpAlarmResponseVO> alarmResponse(@RequestParam(value = "deviceId") String deviceId,
+                                                           @RequestParam(value = "productCode") String productCode){
+        return ApiResult.success(dmpDeviceInfoService.alarmResponse(deviceId, productCode));
     }
 }
 

+ 12 - 4
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/DmpDeviceInfoService.java

@@ -4,9 +4,7 @@ import com.usky.backend.domain.*;
 import com.usky.common.core.bean.CommonPage;
 import com.usky.common.mybatis.core.CrudService;
 import com.usky.iot.domain.DmpDeviceInfo;
-import com.usky.iot.service.vo.BaseFacilityDeviceVO;
-import com.usky.iot.service.vo.DmpDeviceInfoRequest;
-import com.usky.iot.service.vo.DmpTopicResponseVO;
+import com.usky.iot.service.vo.*;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.multipart.MultipartFile;
@@ -83,5 +81,15 @@ public interface DmpDeviceInfoService extends CrudService<DmpDeviceInfo> {
      * @param: [pageNum, pageSize]
      * @return: java.util.List<com.usky.iot.service.vo.DmpTopicResponseVO>
      **/
-    CommonPage<DmpTopicResponseVO> topic(String productCode, String deviceId, String deviceUuid, Integer pageNum, Integer pageSize);
+    CommonPage<DmpTopicResponseVO> topic(DmpTopicRequestVO requestVO);
+
+    /**
+     * @description: 获取设备告警请求信息
+     * @author: fyc
+     * @email yuchuan.fu@chinausky.com
+     * @date: 2025/2/7 17:49
+     * @param: [deviceId, productCode]
+     * @return: com.usky.iot.service.vo.DmpHttpAlarmResponseVO
+     **/
+    DmpHttpAlarmResponseVO alarmResponse(String deviceId, String productCode);
 }

+ 150 - 16
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -7,6 +7,7 @@ import cn.afterturn.easypoi.excel.entity.ExportParams;
 import cn.afterturn.easypoi.excel.entity.ImportParams;
 import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -30,6 +31,7 @@ import com.usky.common.mybatis.core.AbstractCrudService;
 import com.usky.iot.domain.*;
 import com.usky.common.security.utils.SecurityUtils;
 import com.usky.iot.mapper.DmpDeviceInfoMapper;
+import com.usky.iot.mapper.DmpProductAttributeMapper;
 import com.usky.iot.service.BaseFacilityDeviceService;
 import com.usky.iot.service.DmpDeviceInfoService;
 import com.usky.iot.service.DmpDeviceStatusService;
@@ -81,6 +83,11 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
     @Autowired
     private RemoteTransferService remoteTransferService;
 
+    @Autowired
+    private DmpProductAttributeMapper dmpProductAttributeMapper;
+
+    private static final String ALARM_HTTP_URL = "/service-alarm/baseAlarm/alarmInfo";
+
     @Override
     public Map<String, Object> control(String productCode, String deviceUuid, String commandCode, String commandValue, String domain, Long userId, String userName) {
         Integer tenantId;
@@ -132,24 +139,50 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
     }
 
     @Override
-    public CommonPage<DmpTopicResponseVO> topic(String productCode, String deviceId, String deviceUuid, Integer pageNum, Integer pageSize) {
+    public CommonPage<DmpTopicResponseVO> topic(DmpTopicRequestVO requestVO) {
+
+        String productCode = requestVO.getProductCode();
+        String deviceId = requestVO.getDeviceId();
+        String deviceUuid = requestVO.getDeviceUuid();
+        String deviceName = requestVO.getDeviceName();
+        String simCode = requestVO.getSimCode();
+        String installAddress = requestVO.getInstallAddress();
+        Integer productId = requestVO.getProductId();
+        Integer pageNum = requestVO.getPageNum();
+        Integer pageSize = requestVO.getPageSize();
+
+        pageNum = (pageNum != null) ? pageNum : 1;
+        pageSize = (pageSize != null) ? pageSize : 10;
 
         List<DmpTopicResponseVO> responseVOList = new ArrayList<>();
-        DmpTopicResponseVO add = new DmpTopicResponseVO();
-        add.setTopic("/" + productCode + "/" + deviceId + "/" + TopicInfo.ADD_REMARKS.getLetter());
-        add.setPublisher(TopicInfo.GATEWAY_DEVICE.getValue());
-        add.setSubscriber(TopicInfo.IOT_PLATFORM.getValue());
-        add.setRemarks(TopicInfo.ADD_REMARKS.getValue());
-        responseVOList.add(add);
-
-        DmpTopicResponseVO info = new DmpTopicResponseVO();
-        info.setTopic("/" + productCode + "/" + deviceId + "/" + TopicInfo.INFO_REMARKS.getLetter());
-        info.setPublisher(TopicInfo.GATEWAY_DEVICE.getValue());
-        info.setSubscriber(TopicInfo.IOT_PLATFORM.getValue());
-        info.setRemarks(TopicInfo.INFO_REMARKS.getValue());
-        responseVOList.add(info);
-
-/*        DmpTopicResponseVO control = new DmpTopicResponseVO();
+        String topicType = requestVO.getTopicType();
+
+        if (topicType == null) {
+
+            if (StringUtils.isBlank(productCode)) {
+                throw new BusinessException("产品编码“productCode”不能为空!");
+            } else if (StringUtils.isBlank(deviceId)) {
+                throw new BusinessException("设备编号“deviceId”不能为空!");
+            }
+/*            else if (StringUtils.isBlank(deviceUuid)) {
+                throw new BusinessException("设备uuid“deviceUuid“不能为空!");
+            }*/
+
+/*            DmpTopicResponseVO add = new DmpTopicResponseVO();
+            add.setTopic("/" + productCode + "/" + deviceId + "/" + TopicInfo.ADD_REMARKS.getLetter());
+            add.setPublisher(TopicInfo.GATEWAY_DEVICE.getValue());
+            add.setSubscriber(TopicInfo.IOT_PLATFORM.getValue());
+            add.setRemarks(TopicInfo.ADD_REMARKS.getValue());
+            responseVOList.add(add);*/
+
+            DmpTopicResponseVO info = new DmpTopicResponseVO();
+            info.setTopic("/" + productCode + "/" + deviceId + "/" + TopicInfo.INFO_REMARKS.getLetter());
+            info.setPublisher(TopicInfo.GATEWAY_DEVICE.getValue());
+            info.setSubscriber(TopicInfo.IOT_PLATFORM.getValue());
+            info.setRemarks(TopicInfo.INFO_REMARKS.getValue());
+            responseVOList.add(info);
+
+            /*        DmpTopicResponseVO control = new DmpTopicResponseVO();
         control.setTopic("/" + deviceUuid + "/" + TopicInfo.CONTROL_REMARKS.getLetter());
         control.setSubscriber(TopicInfo.GATEWAY_DEVICE.getValue());
         control.setPublisher(TopicInfo.IOT_PLATFORM.getValue());
@@ -163,6 +196,76 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
         controlResponse.setRemarks(TopicInfo.CONTROL_RESPONSE_REMARKS.getValue());
         responseVOList.add(controlResponse);*/
 
+        }
+/*        else if ("add".equals(topicType)) {
+
+            if (StringUtils.isBlank(productCode)) {
+                throw new BusinessException("产品编码“productCode”不能为空!");
+            } else if (StringUtils.isBlank(deviceId)) {
+                throw new BusinessException("设备编号“deviceId”不能为空!");
+            } else if (StringUtils.isBlank(deviceName)) {
+                throw new BusinessException("设备名“deviceName“不能为空!");
+            }
+
+            DmpTopicResponseVO add = new DmpTopicResponseVO();
+            add.setTopic("/" + productCode + "/" + deviceId + "/" + TopicInfo.ADD_REMARKS.getLetter());
+            add.setPublisher(TopicInfo.GATEWAY_DEVICE.getValue());
+            add.setSubscriber(TopicInfo.IOT_PLATFORM.getValue());
+            add.setRemarks(TopicInfo.ADD_REMARKS.getValue());
+
+            // 组装topicPayload
+            JSONObject topicPayloadJson = new JSONObject();
+            topicPayloadJson.put("productCode", productCode);
+            topicPayloadJson.put("deviceId", deviceId);
+            topicPayloadJson.put("deviceName", deviceName);
+            if (!StringUtils.isBlank(simCode)) {
+                topicPayloadJson.put("simCode", simCode);
+            }
+            if (!StringUtils.isBlank(installAddress)) {
+                topicPayloadJson.put("installAddress", installAddress);
+            }
+            String topicPayload = topicPayloadJson.toString();
+            add.setTopicPayload(topicPayload);
+
+        }*/
+
+        else if ("info".equals(topicType)) {
+
+            if (StringUtils.isBlank(productCode)) {
+                throw new BusinessException("产品编码“productCode”不能为空!");
+            } else if (productId == null) {
+                throw new BusinessException("产品ID“productId“不能为空!");
+            } else if (StringUtils.isBlank(deviceId)) {
+                throw new BusinessException("设备编号“deviceId”不能为空!");
+            }
+
+            DmpTopicResponseVO info = new DmpTopicResponseVO();
+            info.setTopic("/" + productCode + "/" + deviceId + "/" + TopicInfo.INFO_REMARKS.getLetter());
+            info.setPublisher(TopicInfo.GATEWAY_DEVICE.getValue());
+            info.setSubscriber(TopicInfo.IOT_PLATFORM.getValue());
+            info.setRemarks(TopicInfo.INFO_REMARKS.getValue());
+
+            // 组装topicPayload
+            JSONObject infoPayloadJson = new JSONObject();
+            infoPayloadJson.put("productCode", productCode);
+            Long timestamp = System.currentTimeMillis();
+            infoPayloadJson.put("timestamp", timestamp);
+
+            JSONObject metricsJson = new JSONObject();
+            List<DmpProductAttribute> productAttributeList = getProductAttributeList(productId);
+            for (DmpProductAttribute productAttribute : productAttributeList) {
+                metricsJson.put(productAttribute.getAttributeCode(), productAttribute.getMaximum());
+            }
+            infoPayloadJson.put("metrics", metricsJson);
+
+            JSONObject tagsJson = new JSONObject();
+            tagsJson.put("device_id", deviceId);
+            infoPayloadJson.put("tags", tagsJson);
+            String infoPayload = infoPayloadJson.toString();
+            info.setTopicPayload(infoPayload);
+            responseVOList.add(info);
+        }
+
         int fromIndex = (pageNum - 1) * pageSize;
         int toIndex = Math.min(fromIndex + pageSize, responseVOList.size());
         List<DmpTopicResponseVO> paginatedList = responseVOList.subList(fromIndex, toIndex);
@@ -173,6 +276,37 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
         return ToCommonPage(page);
     }
 
+    private List<DmpProductAttribute> getProductAttributeList(Integer productId) {
+        LambdaQueryWrapper<DmpProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.select(DmpProductAttribute::getAttributeCode, DmpProductAttribute::getMaximum)
+                .eq(DmpProductAttribute::getProductId, productId);
+        return dmpProductAttributeMapper.selectList(queryWrapper);
+    }
+
+    @Override
+    public DmpHttpAlarmResponseVO alarmResponse(String deviceId, String productCode) {
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+        LocalDateTime now = LocalDateTime.now();
+        String alarmTime = now.format(formatter);
+
+        DmpHttpAlarmResponseVO responseVO = new DmpHttpAlarmResponseVO();
+        JSONObject alarmPayloadJson = new JSONObject();
+        alarmPayloadJson.put("productCode", productCode);
+        alarmPayloadJson.put("deviceId", deviceId);
+        alarmPayloadJson.put("alarmTime", alarmTime);
+        alarmPayloadJson.put("alarmType", "311");
+        alarmPayloadJson.put("alarmObject", "烟雾报警");
+        alarmPayloadJson.put("alarmData", "0");
+        alarmPayloadJson.put("alarmAttribute", "烟雾报警");
+        alarmPayloadJson.put("alarmContent", "烟感");
+        alarmPayloadJson.put("alarmGrade", 1);
+        alarmPayloadJson.put("alarmAddress", "上海市青浦区华徐公路628号C幢");
+        String alarmPayload = alarmPayloadJson.toString();
+        responseVO.setAlarmPayload(alarmPayload);
+        responseVO.setAlarmHttpUrl(ALARM_HTTP_URL);
+        return responseVO;
+    }
+
     @Override
     public void add(DmpDeviceInfo dmpDeviceInfo) {
         if (checkNameUnique(dmpDeviceInfo)) {

+ 23 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/vo/DmpHttpAlarmResponseVO.java

@@ -0,0 +1,23 @@
+package com.usky.iot.service.vo;
+
+import lombok.Data;
+
+/**
+ *
+ * @author fyc
+ * @email yuchuan.fu@chinausky.com
+ * @date 2025/2/10
+ */
+@Data
+public class DmpHttpAlarmResponseVO {
+
+    /**
+     * 报警事件数据上报http地址
+     **/
+    private String alarmHttpUrl;
+
+    /**
+     * 报警事件数据上报参数body
+     **/
+    private String alarmPayload;
+}

+ 79 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/vo/DmpTopicRequestVO.java

@@ -0,0 +1,79 @@
+package com.usky.iot.service.vo;
+
+import lombok.Data;
+
+/**
+ *
+ * @author fyc
+ * @email yuchuan.fu@chinausky.com
+ * @date 2025/2/10
+ */
+@Data
+public class DmpTopicRequestVO {
+
+    /**
+     * topic类型
+     **/
+    private String topicType;
+
+    /**
+     * 产品编码
+     **/
+    private String productCode;
+
+    /**
+     * 产品id
+     **/
+    private Integer productId;
+
+    /**
+     * 设备id
+     **/
+    private String deviceId;
+
+    /**
+     * 设备唯一标识
+     **/
+    private String deviceUuid;
+
+    /**
+     * 设备名称
+     **/
+    private String deviceName;
+
+    /**
+     * sim卡号
+     **/
+    private String simCode;
+
+    /**
+     * 安装位置
+     **/
+    private String installAddress;
+
+    /**
+     * 时间戳
+     **/
+    private Long timestamp;
+
+    /**
+     * 属性值集合
+     **/
+    private String metrics;
+
+    /**
+     * 标签值集合
+     **/
+    private String tags;
+
+    /**
+     * 页码
+     **/
+    private Integer pageNum;
+
+    /**
+     * 每页条数
+     **/
+    private Integer pageSize;
+
+}

+ 14 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/vo/DmpTopicResponseVO.java

@@ -31,5 +31,19 @@ public class DmpTopicResponseVO {
      **/
     private String remarks;
 
+    /**
+     * mqtt数据上报数据
+     **/
+    private String topicPayload;
+
+    /**
+     * http报警数据上报地址
+     **/
+    private String alarm;
+
+    /**
+     * http报警数据上报数据
+     **/
+    private String alarmPayload;
 
 }