Переглянути джерело

补充测试离线状态接口,补充主动上报离线对接预警

yf_zn 11 місяців тому
батько
коміт
f25d3611c2

+ 42 - 14
src/main/java/com/yunfeiyun/agmp/iots/device/controller/TestController.java

@@ -6,6 +6,7 @@ import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
 import com.yunfeiyun.agmp.common.utils.DateUtils;
 import com.yunfeiyun.agmp.common.utils.JSONUtils;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
 import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
 import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
 import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictConst;
@@ -22,6 +23,7 @@ import com.yunfeiyun.agmp.iots.device.serviceImp.YfQxzDeviceImpl;
 import com.yunfeiyun.agmp.iots.service.IIotDeviceService;
 import com.yunfeiyun.agmp.iots.service.IIotDevicelasteddataService;
 import com.yunfeiyun.agmp.iots.service.IIotYfScddataService;
+import com.yunfeiyun.agmp.iots.warn.service.WarnService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.mongodb.core.MongoTemplate;
@@ -44,7 +46,8 @@ import java.util.concurrent.CompletableFuture;
 @RequestMapping("/test")
 @Slf4j
 public class TestController {
-
+    @Autowired
+    private IIotDeviceService iIotDeviceService;
 
     @Resource(name = "threadPoolTaskExecutor")
     private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@@ -609,11 +612,11 @@ public class TestController {
         String devCode = req.getDevCode();
         JSONObject data = req.getData();
 
-        if(StringUtils.isEmpty(devCode)) {
+        if (StringUtils.isEmpty(devCode)) {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备编码为空");
         }
 
-        if(data == null) {
+        if (data == null) {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "数据为空");
         }
 
@@ -621,7 +624,7 @@ public class TestController {
         if (iotDeviceList == null || iotDeviceList.isEmpty()) {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备不存在");
         }
-        if(iotDeviceList.size() > 1) {
+        if (iotDeviceList.size() > 1) {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备不唯一");
         }
         IotDevice iotDevice = iotDeviceList.get(0);
@@ -629,11 +632,11 @@ public class TestController {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备不存在");
         }
         String devtypeBid = iotDevice.getDevtypeBid();
-        if(QxzTypeUtil.isYfCommQxz(devtypeBid)){
+        if (QxzTypeUtil.isYfCommQxz(devtypeBid)) {
             YfQxzDeviceImpl yfQxzDevice = (YfQxzDeviceImpl) iYfQxzDevice;
             String devUpdateddate = DateUtils.dateTimeNow();
             yfQxzDevice.processData(iotDevice, data, devUpdateddate);
-        }else{
+        } else {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备类型不支持");
         }
         return AjaxResult.success();
@@ -641,9 +644,9 @@ public class TestController {
 
     public Class getTableClass(String devtypeBid) {
         Class tableClass = null;
-        if(QxzTypeUtil.isXphYfQxSq(devtypeBid)){
+        if (QxzTypeUtil.isXphYfQxSq(devtypeBid)) {
             tableClass = IotXphYfqxzdata.class;
-        }else if(QxzTypeUtil.isYfQxSq(devtypeBid)){
+        } else if (QxzTypeUtil.isYfQxSq(devtypeBid)) {
             tableClass = IotYfqxzdata.class;
         }
 //        else if (devtypeBid.equals(IotDeviceDictConst.TYPE_ADZN_GSSQ)) {
@@ -655,6 +658,7 @@ public class TestController {
 
     /**
      * 设备上报数据
+     *
      * @param req
      * @return
      * @throws Exception
@@ -663,7 +667,7 @@ public class TestController {
     public AjaxResult getDevData(WarnTestReq req) throws Exception {
         String devCode = req.getDevCode();
 
-        if(StringUtils.isEmpty(devCode)) {
+        if (StringUtils.isEmpty(devCode)) {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备编码为空");
         }
 
@@ -671,16 +675,16 @@ public class TestController {
         if (iotDeviceList == null || iotDeviceList.isEmpty()) {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备不存在");
         }
-        if(iotDeviceList.size() > 1) {
+        if (iotDeviceList.size() > 1) {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备不唯一");
         }
         IotDevice iotDevice = iotDeviceList.get(0);
 
         String devtypeBid = iotDevice.getDevtypeBid();
         String devBid = iotDevice.getDevBid();
-        if(QxzTypeUtil.isYfCommQxz(devtypeBid)){
+        if (QxzTypeUtil.isYfCommQxz(devtypeBid)) {
             Class tableClass = getTableClass(devtypeBid);
-            if(tableClass == null){
+            if (tableClass == null) {
                 throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备类型不存在");
             }
 
@@ -688,7 +692,7 @@ public class TestController {
             String devldContent = iotDevicelasteddata.getDevldContent();
             List jsonList = JSONUtils.toList(devldContent);
             List<Map<String, String>> dataList = new ArrayList<>();
-            for(Object dataObj : jsonList){
+            for (Object dataObj : jsonList) {
                 JSONObject jsonData = JSONObject.from(dataObj);
                 Map<String, String> dataMap = new HashMap<>();
                 dataMap.put("eNum", jsonData.getString("eNum"));
@@ -700,8 +704,32 @@ public class TestController {
             JSONObject data = new JSONObject();
             data.put("data", dataList);
             return AjaxResult.success(data);
-        }else{
+        } else {
             throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备类型不支持");
         }
     }
+
+    @RequestMapping("/warn/cmd/offline")
+    public AjaxResult cmdOffline(WarnTestReq req) {
+        List<IotDevice> oldIotDevice = iIotDeviceService.selectIotDeviceByDevCode(req.getDevCode());
+        for (IotDevice iotDevice : oldIotDevice) {
+            IotDevice newIotDevice = new IotDevice();
+            newIotDevice.setDevBid(iotDevice.getDevBid());
+            newIotDevice.setDevOriginalStatus(iotDevice.getDevStatus());
+            newIotDevice.setDevStatus("0");
+            newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
+            newIotDevice.setTid(iotDevice.getTid());
+            newIotDevice.setDevtypeBid(iotDevice.getDevtypeBid());
+            newIotDevice.setDevCreateddate(iotDevice.getDevCreateddate());
+            newIotDevice.setDevUpdateddate(iotDevice.getDevUpdateddate());
+            newIotDevice.setDevCode(iotDevice.getDevCode());
+            iIotDeviceService.updateIotDevice(newIotDevice);
+            //发送离线预警
+            SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, JSONObject.from(iotDevice));
+        }
+
+        return AjaxResult.success();
+    }
+
+
 }

+ 6 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/BzyDeviceImpl.java

@@ -242,6 +242,12 @@ public class BzyDeviceImpl extends DeviceAbstractImpl implements IBzyDevice {
         newIotDevice.setDevStatus("0");
         newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
         iIotDeviceService.updateIotDevice(newIotDevice);
+        //这些参数预警需要用,携带过去
+        newIotDevice.setTid(oldIotDevice.getTid());
+        newIotDevice.setDevtypeBid(oldIotDevice.getDevtypeBid());
+        newIotDevice.setDevCreateddate(oldIotDevice.getDevCreateddate());
+        newIotDevice.setDevUpdateddate(oldIotDevice.getDevUpdateddate());
+        newIotDevice.setDevCode(oldIotDevice.getDevCode());
         //发送离线预警
         SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, ext);
     }

+ 7 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/CqCbdDeviceImpl.java

@@ -296,7 +296,14 @@ public class CqCbdDeviceImpl extends DeviceAbstractImpl implements ICbdDevice {
         newIotDevice.setDevBid(devId);
         newIotDevice.setDevStatus("0");
         newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
+        newIotDevice.setTid(iotDevice.getTid());
+        newIotDevice.setDevtypeBid(iotDevice.getDevtypeBid());
+
         iIotDeviceService.updateIotDevice(newIotDevice);
+        newIotDevice.setDevCreateddate(iotDevice.getDevCreateddate());
+        newIotDevice.setDevUpdateddate(iotDevice.getDevUpdateddate());
+        newIotDevice.setDevCode(iotDevice.getDevCode());
+
         //发送离线预警
         SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, ext);
     }

+ 6 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/IYfXctDeviceImpl.java

@@ -240,7 +240,13 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
         newIotDevice.setDevBid(iotDevice.getDevBid());
         newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.OFFLINE.getCode());
         newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
+        newIotDevice.setTid(iotDevice.getTid());
+        newIotDevice.setDevtypeBid(iotDevice.getDevtypeBid());
         iIotDeviceService.updateIotDevice(newIotDevice);
+        newIotDevice.setDevCreateddate(iotDevice.getDevCreateddate());
+        newIotDevice.setDevUpdateddate(iotDevice.getDevUpdateddate());
+        newIotDevice.setDevCode(iotDevice.getDevCode());
+
         //发送离线预警
         SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, ext);
     }

+ 6 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/XphYfQxzDeviceImpl.java

@@ -305,8 +305,14 @@ public class XphYfQxzDeviceImpl extends DeviceAbstractImpl implements IXphYfQxzD
         }else if("online".equalsIgnoreCase(cmd)){
             newIotDevice.setDevStatus("1");
         }
+        newIotDevice.setTid(iotDeviceOld.getTid());
         newIotDevice.setDevUpdateddate(devUpdateddate);
+        newIotDevice.setDevtypeBid(iotDeviceOld.getDevtypeBid());
         iIotDeviceService.updateIotDevice(newIotDevice);
+        newIotDevice.setDevCreateddate(iotDeviceOld.getDevCreateddate());
+        newIotDevice.setDevUpdateddate(iotDeviceOld.getDevUpdateddate());
+        newIotDevice.setDevCode(iotDeviceOld.getDevCode());
+
         //发送离线预警
         SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, dataJson);
 

+ 6 - 1
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/YfQxzDeviceImpl.java

@@ -449,7 +449,8 @@ public class YfQxzDeviceImpl extends DeviceAbstractImpl implements IYfQxzDevice
         // 设备不存在 就不再处理
         IotDevice newIotDevice = new IotDevice();
         newIotDevice.setDevBid(iotDeviceOld.getDevBid());
-
+        newIotDevice.setTid(iotDeviceOld.getTid());
+        newIotDevice.setDevtypeBid(iotDeviceOld.getDevtypeBid());
         if("offline".equalsIgnoreCase(cmd)){
             newIotDevice.setDevStatus("0");
             newIotDevice.setDevOfflinedate(devUpdateddate);
@@ -458,6 +459,10 @@ public class YfQxzDeviceImpl extends DeviceAbstractImpl implements IYfQxzDevice
             newIotDevice.setDevUpdateddate(devUpdateddate);
         }
         iIotDeviceService.updateIotDevice(newIotDevice);
+        newIotDevice.setDevCreateddate(iotDeviceOld.getDevCreateddate());
+        newIotDevice.setDevUpdateddate(iotDeviceOld.getDevUpdateddate());
+        newIotDevice.setDevCode(iotDeviceOld.getDevCode());
+
         //发送离线预警
         SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, ext);
         // 主动进行查询,以确保不是误判离线

+ 6 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/YfScdDeviceImpl.java

@@ -228,7 +228,13 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
         newIotDevice.setDevBid(oldIotDevice.getDevBid());
         newIotDevice.setDevStatus("0");
         newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
+        newIotDevice.setTid(oldIotDevice.getTid());
+        newIotDevice.setDevtypeBid(oldIotDevice.getDevtypeBid());
         iIotDeviceService.updateIotDevice(newIotDevice);
+        newIotDevice.setDevCreateddate(oldIotDevice.getDevCreateddate());
+        newIotDevice.setDevUpdateddate(oldIotDevice.getDevUpdateddate());
+        newIotDevice.setDevCode(oldIotDevice.getDevCode());
+
         //发送离线预警
         SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, dataJson);
 

+ 6 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/YfXycbIIIDeviceImpl.java

@@ -206,7 +206,13 @@ public class YfXycbIIIDeviceImpl extends DeviceAbstractImpl implements IYfXycbII
         newIotDevice.setDevBid(oldIotDevice.getDevBid());
         newIotDevice.setDevStatus("0");
         newIotDevice.setDevOfflinedate(devUpdateddate);
+        newIotDevice.setTid(oldIotDevice.getTid());
+        newIotDevice.setDevtypeBid(oldIotDevice.getDevtypeBid());
+
         iIotDeviceService.updateIotDevice(newIotDevice);
+        newIotDevice.setDevCreateddate(oldIotDevice.getDevCreateddate());
+        newIotDevice.setDevUpdateddate(oldIotDevice.getDevUpdateddate());
+        newIotDevice.setDevCode(oldIotDevice.getDevCode());
         //发送离线预警
         SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, dataJson);
         /**

+ 3 - 0
src/main/java/com/yunfeiyun/agmp/iots/task/IotStatusService.java

@@ -155,6 +155,9 @@ public class IotStatusService {
         iotDevice.setDevModifieddate(DateUtils.dateTimeNow());
         iotDevice.setDevtypeBid(iotDeviceStatusResVo.getDevtypeBid());
         iotDevice.setDevconnBid(iotDeviceStatusResVo.getDevconnBid());
+        iotDevice.setTid(iotDeviceStatusResVo.getTid());
+        iotDevice.setDevCreateddate(iotDeviceStatusResVo.getDevCreateddate());
+        iotDevice.setDevUpdateddate(iotDeviceStatusResVo.getDevUpdateddate());
         //编辑离线
         iIotDeviceService.updateIotDevice(iotDevice);
         //发送预警

+ 36 - 31
src/main/java/com/yunfeiyun/agmp/iots/warn/service/WarnService.java

@@ -92,32 +92,36 @@ public class WarnService {
             }
 
             CompletableFuture.runAsync(() -> {
-                // 获取该设备有哪些告警配置
-                IotWarnconfig warnConfigInfo = iotWarnBussinessService.selectIotWarnOfflineConfigInfo(iotDevice.getTid());
-
-                // 存在离线配置,且开启
-                if (warnConfigInfo != null && "0".equals(warnConfigInfo.getWcStatus())) {
-                    // 离线
-                    if (IotEnumOnlineStatus.OFFLINE.getStatus().equals(iotDevice.getDevStatus())) {
-                        WarnResult warnResult = new WarnResult();
-                        warnResult.setMessageId(warnResult.getUUId());
-                        warnResult.setDevId(iotDevice.getDevBid());
-                        warnResult.setTid(iotDevice.getTid());
-                        warnResult.setConfigId(warnConfigInfo.getWcBid());
-                        warnResult.setReportData(data.toJSONString());
-                        warnResult.setTargetReCount(warnConfigInfo.getWcRepeatnum());
-                        warnResult.setDevtypeBid(iotDevice.getDevtypeBid());
-                        warnResult.setConfig(warnConfigInfo);
-                        warnResult.setOffline(true);
-                        warnResult.setTriggered(true);
-                        warnResult.setMessage(WarnMessageBuilderUtil.buildWarningOfflineMessage(iotDevice.getDevtypeBid(), iotDevice.getDevCode(), iotDevice.getDevName(), iotDevice.getDevUpdateddate()));
-                        // 处理警告记录前的日志
-                        log.info("[设备告警] 正在为设备{}处理警告记录:{}", iotDevice.getDevBid(), warnResult);
-                        handleWarnRecord(warnResult);
+                try {
+                    // 获取该设备有哪些告警配置
+                    IotWarnconfig warnConfigInfo = iotWarnBussinessService.selectIotWarnOfflineConfigInfo(iotDevice.getTid());
+
+                    // 存在离线配置,且开启
+                    if (warnConfigInfo != null && "0".equals(warnConfigInfo.getWcStatus())) {
+                        // 离线
+                        if (IotEnumOnlineStatus.OFFLINE.getStatus().equals(iotDevice.getDevStatus())) {
+                            WarnResult warnResult = new WarnResult();
+                            warnResult.setMessageId(warnResult.getUUId());
+                            warnResult.setDevId(iotDevice.getDevBid());
+                            warnResult.setTid(iotDevice.getTid());
+                            warnResult.setConfigId(warnConfigInfo.getWcBid());
+                            warnResult.setReportData(data.toJSONString());
+                            warnResult.setTargetReCount(warnConfigInfo.getWcRepeatnum() == null ? 0 : warnConfigInfo.getWcRepeatnum());
+                            warnResult.setDevtypeBid(iotDevice.getDevtypeBid());
+                            warnResult.setConfig(warnConfigInfo);
+                            warnResult.setOffline(true);
+                            warnResult.setTriggered(true);
+                            warnResult.setMessage(WarnMessageBuilderUtil.buildWarningOfflineMessage(iotDevice.getDevtypeBid(), iotDevice.getDevCode(),  StringUtils.isEmpty(iotDevice.getDevUpdateddate())?iotDevice.getDevCreateddate():iotDevice.getDevUpdateddate()));
+                            // 处理警告记录前的日志
+                            log.info("[设备告警] 正在为设备{}处理警告记录:{}", iotDevice.getDevBid(), warnResult);
+                            handleWarnRecord(warnResult);
+                        }
+                    } else {
+                        // 日志记录没有离线告警配置或者已经关闭的情况
+                        log.error("[设备告警] devId:{} , tid{},没有离线告警配置或者已经关闭:{}", iotDevice.getDevBid(), iotDevice.getTid(), warnConfigInfo);
                     }
-                } else {
-                    // 日志记录没有离线告警配置或者已经关闭的情况
-                    log.error("[设备告警] tid{},没有离线告警配置或者已经关闭:{}", iotDevice.getDevBid(), warnConfigInfo);
+                } catch (Exception e) {
+                    log.error("{}", e);
                 }
 
             }, threadPoolTaskExecutor);
@@ -537,8 +541,9 @@ public class WarnService {
      * @param iotDeviceList
      */
     public void processWarningOfflineToOnlineDeviceData(List<IotDevice> iotDeviceList) {
-        try {
-            CompletableFuture.runAsync(() -> {
+
+        CompletableFuture.runAsync(() -> {
+            try {
                 for (IotDevice iotDevice : iotDeviceList) {
                     //离线转在线
                     if (IotDeviceStatusTypeEnum.OFFLINE.getCode().equals(iotDevice.getDevOriginalStatus())
@@ -548,11 +553,11 @@ public class WarnService {
                         autoDealWarnOfflineLog(iotDevice.getDevBid());
                     }
                 }
+            } catch (Exception e) {
+                log.error("[离线告警]自动处理离线告警消息:失败 {}", e);
+            }
+        }, threadPoolTaskExecutor);
 
-            }, threadPoolTaskExecutor);
-        } catch (Exception e) {
-            log.error("[离线告警]自动处理离线告警消息:失败 {}", e);
-        }
     }
 
     private void autoDealWarnOfflineLog(String devId) {

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iots/warn/util/WarnMessageBuilderUtil.java

@@ -39,7 +39,7 @@ public class WarnMessageBuilderUtil {
      * @param name
      * @return
      */
-    public static String buildWarningOfflineMessage(String devType, String devCode, String name, String lastUpdateTime) {
+    public static String buildWarningOfflineMessage(String devType, String devCode, String lastUpdateTime) {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         try {
             Date lastUpdate = sdf.parse(lastUpdateTime);

+ 1 - 1
src/main/resources/mapper/IotWarnBusinessMapper.xml

@@ -148,7 +148,7 @@
             <if test="wlDealresult != null">wlDealresult = #{wlDealresult},</if>
             <if test="wlDealtime != null">wlDealtime = #{wlDealtime},</if>
         </trim>
-        where devBid = #{devBid}
+        where devBid = #{devBid} and wlType='1' and status='0'
 
     </update>
 </mapper>