xuxiaofei пре 3 година
родитељ
комит
153685cfa3

+ 6 - 0
zd-model/src/main/java/com/zd/model/constant/MqttConstants.java

@@ -94,4 +94,10 @@ public interface MqttConstants {
 
     //灭火设备异常通知
     String TOPIC_FIRE_DEVICE_WARN = "lab/fireDevice/Warn/";
+
+    //灭火设备是否在线
+    String TOPIC_FIRE_DEVICE_ONLINE = "lab/fireDevice/isOnline/";
+
+    //一键灭火是否正在执行
+    String TOPIC_FIRE_DEVICE_EXECUTING = "lab/fireDevice/executing/";
 }

+ 168 - 22
zd-modules/zd-algorithm/src/main/java/com/zd/alg/fire/controller/FireDeviceController.java

@@ -1,9 +1,10 @@
 package com.zd.alg.fire.controller;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.zd.alg.fire.service.IFireDeviceLogService;
 import com.zd.alg.fire.service.IFireDeviceService;
-import com.zd.alg.fire.utils.FireDeviceStatusTask;
+import com.zd.alg.mqtt.MqttSend;
 import com.zd.algorithm.api.fire.entity.HardwareFireDevice;
 import com.zd.algorithm.api.fire.entity.HardwareFireDeviceLog;
 import com.zd.algorithm.api.fire.entity.vo.HardwareFireDeviceVO;
@@ -16,6 +17,7 @@ import com.zd.common.core.web.controller.BaseController;
 import com.zd.laboratory.api.feign.RemoteLaboratoryService;
 import com.zd.model.constant.CacheConstants;
 import com.zd.model.constant.CacheDevice;
+import com.zd.model.constant.MqttConstants;
 import com.zd.model.domain.ResultData;
 import com.zd.model.domain.per.PerFun;
 import com.zd.model.domain.per.PerPrefix;
@@ -27,6 +29,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import org.springframework.web.bind.annotation.*;
 
+import javax.annotation.Resource;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 
@@ -54,12 +57,42 @@ public class FireDeviceController extends BaseController {
     @Autowired
     private RemoteLaboratoryService remoteLaboratoryService;
 
+    /***
+     * 固定30s
+     */
+    @Value("${time.direTime}")
+    private Long direTime;
+
+    @Autowired
+    private MqttSend mqttSend;
+
+    /***
+     * 是否取消灭火
+     */
+    private String CANCEL_OUTFILE="cancel_outFile";
+
+    /**
+     * 手动灭火时间
+     */
+    private String  ARTIFICIAL_OUT_FILE="artificial_out_file";
+
+    /**
+     * 继电器等待时间
+     */
     @Value("${time.waitTime}")
     private Long waitTime;
 
+    /***
+     * 灭火主机掉线时间
+     */
     @Value("${time.onlineTime}")
-    private Long onlineTime;
+    public Long onlineTime;
 
+    /***
+     * 灭火倒计时时间
+     */
+    @Value("${time.countDown}")
+    public Long countDown;
 
     /**
      * 获取主机状态(首次加载)
@@ -81,7 +114,8 @@ public class FireDeviceController extends BaseController {
         boolean isexist = false;
         //是否灭火中
         boolean outfireing =false;
-
+        //时间差
+        long timeDifference=-1;
         if (StringUtils.isNull(subjectId)) {
             return ResultData.fail("实验室id参数为空");
         }
@@ -125,7 +159,12 @@ public class FireDeviceController extends BaseController {
                 outfireing = startSpray==1?true:false;
             }
 
-
+            //手动灭火和当前时差
+            Long outfireTime = redisService.getCacheObject(ARTIFICIAL_OUT_FILE+list.get(0).getDeviceCode());
+            if(StringUtils.isNotNull(outfireTime)){
+                timeDifference =    System.currentTimeMillis()-outfireTime;
+                timeDifference = timeDifference/1000;
+            }
             //查询该实验室是否存在火焰摄像头报警
             JSONObject fireJson = redisService.getCacheObject(CacheDevice.FIRE_CAMERA_KEY.getRedisKey()+subjectId);
             if (StringUtils.isNotNull(fireJson)) {
@@ -139,6 +178,8 @@ public class FireDeviceController extends BaseController {
         map.put("isexist", isexist);
         map.put("outfireing", outfireing);
         map.put("fireDevice", list.get(0));
+        map.put("newTime", System.currentTimeMillis());
+        map.put("timeDifference", timeDifference);
         return ResultData.success(map);
     }
 
@@ -228,39 +269,129 @@ public class FireDeviceController extends BaseController {
      * @return
      */
     @GetMapping(value = "/outfireapp")
-    public ResultData outFire(Long subjectId) throws InterruptedException {
+    public ResultData outFire(Long subjectId,String deviceCode) throws InterruptedException {
         logger.info("=========小程序端开始一键灭火=======");
-        Map<String, Object> map = outFireCom(subjectId);
-        if(StringUtils.isNotNull(map.get("check"))){
-            return ResultData.fail(map.get("check").toString());
+        Map<String, Object> map = new HashMap<>();
+        //设备是否在线
+        boolean online=false;
+        //获取灭火主机最后一次响应时间,判断响应是否超时
+        Long timeJson = redisService.getCacheObject(CacheConstants.FIRE_DEVICE_RESPOND_TIME+deviceCode);
+        if (StringUtils.isNotNull(timeJson)) {
+            Long timeDiff = (System.currentTimeMillis() - timeJson) / 1000;
+            //时间差 在设置时间内
+            if (timeDiff < onlineTime) {
+                online = true;
+            }
         }
+        //int waitTime = 5;//倒计时等待时间(秒)
+        map.put("online", online);
+        map.put("waitTime", countDown);
+        map.put("direTime", direTime);
+        new Thread(()->startTT(subjectId,deviceCode,countDown)).start();
         return ResultData.success(map);
     }
 
     /***
+     * 确定执行一键灭火
+     * 缓存10秒
+     * @return
+     */
+/*    @GetMapping(value = "/isConfirmlOutFile")
+    public ResultData isConfirmlOutFile(Long subjectId){
+        redisService.setCacheObject(CANCEL_OUTFILE+subjectId,"1",6L, TimeUnit.SECONDS);
+        return ResultData.success(1);
+    }*/
+
+    /***
+     * 取消执行一键灭火
+     * 缓存10秒
+     * @return
+     */
+    @GetMapping(value = "/isCancelOutFile")
+    public ResultData isCancelOutFile(Long subjectId){
+        redisService.setCacheObject(CANCEL_OUTFILE+subjectId,"0",countDown, TimeUnit.SECONDS);
+        return ResultData.success(1);
+    }
+
+    /***
      *发送灭火请求-PC
      * @param subjectId
      * @return
      * @throws InterruptedException
      */
     @GetMapping(value = "/outfirepc")
-    public ResultData outFirePc(Long subjectId) throws InterruptedException {
-        logger.info("=========PC端开始一键灭火=======");
-        //执行灭火指令
-        Map<String, Object> map = outFireCom(subjectId);
-        if(StringUtils.isNotNull(map.get("check"))){
-            return ResultData.fail(map.get("check").toString());
+    public ResultData outFirePc(Long subjectId,String deviceCode) throws InterruptedException {
+        Map<String, Object> map = new HashMap<>();
+        //设备是否在线
+        boolean online=false;
+        //获取灭火主机最后一次响应时间,判断响应是否超时
+        Long timeJson = redisService.getCacheObject(CacheConstants.FIRE_DEVICE_RESPOND_TIME+deviceCode);
+        if (StringUtils.isNotNull(timeJson)) {
+            Long timeDiff = (System.currentTimeMillis() - timeJson) / 1000;
+            //时间差 在设置时间内
+            if (timeDiff < onlineTime) {
+                online = true;
+            }
         }
+        //int waitTime = 5;//倒计时等待时间(秒)
+        map.put("online", online);
+        map.put("waitTime", countDown);
+        map.put("direTime", direTime);
+        new Thread(()->startTT(subjectId,deviceCode,countDown)).start();
         return ResultData.success(map);
     }
 
+    public void startTT(Long subjectId,String deviceCode,Long waitTime){
+        //休眠5秒等待前端响应是否取消操作结果
+        try {
+            Thread.sleep(waitTime*1000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        Map<String, Object> map = new HashMap<>();
+        //设备是否在线
+        boolean online=false;
+
+        //是否取消灭火
+        String isflg= redisService.getCacheObject(CANCEL_OUTFILE+subjectId);
+        if(StringUtils.isNotNull(isflg)){
+            map.put("isCancel",true);
+        }else{
+            map.put("isCancel",false);
+
+            //获取灭火主机最后一次响应时间,判断响应是否超时
+            Long timeJson = redisService.getCacheObject(CacheConstants.FIRE_DEVICE_RESPOND_TIME+deviceCode);
+            if (StringUtils.isNotNull(timeJson)) {
+                Long timeDiff = (System.currentTimeMillis() - timeJson) / 1000;
+                //时间差 在设置时间内
+                if (timeDiff < onlineTime) {
+                    online = true;
+                }
+            }
+            map.put("online", online);
+
+            logger.info("=========PC端开始一键灭火=======");
+            //执行灭火指令
+            try {
+                map = outFireCom(subjectId,map);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+
+
+        /*if(StringUtils.isNotNull(map.get("check"))){
+            return ResultData.fail(map.get("check").toString());
+        }*/
+
+    }
     /***
      *灭火指令发送方法
      * @param subjectId
      * @return
      */
-    public Map<String,Object> outFireCom(Long subjectId) throws InterruptedException {
-        Map<String, Object> map = new HashMap<>();
+    public Map<String,Object> outFireCom(Long subjectId,Map<String, Object> map) throws InterruptedException {
+        //Map<String, Object> map = new HashMap<>();
         map.put("check","");
         //查询实验室下的硬件
         RemoteLabHardware hd = new RemoteLabHardware();
@@ -279,6 +410,7 @@ public class FireDeviceController extends BaseController {
             return map;
         }
 
+
         //获取当前实验室下灭火设备状态
         HardwareFireDeviceVO fireDeviceVO = new HardwareFireDeviceVO();
         fireDeviceVO.setSubjectId(subjectId);
@@ -286,6 +418,9 @@ public class FireDeviceController extends BaseController {
         JSONObject object = redisService.getCacheObject(list.get(0).getDeviceCode());
         logger.info("灭火设备状态:" + object);
 
+        //手动灭火时间
+        redisService.setCacheObject(ARTIFICIAL_OUT_FILE+list.get(0).getDeviceCode(),System.currentTimeMillis(),Long.valueOf(list.get(0).getDeviceCountDown()+direTime),TimeUnit.SECONDS);
+
         if (StringUtils.isNotNull(object)) {
             //异常传感器数量
             Integer fireNumber = (Integer) object.get("fireNumber");
@@ -294,14 +429,14 @@ public class FireDeviceController extends BaseController {
                 //随机补充一路信号 触发灭火
                 List<RemoteLabHardware> li = new ArrayList<>();
                 li.add(hdlist.get(0));
-                map = sendOutFireMsg(li);
+                map = sendOutFireMsg(li,map,list.get(0).getDeviceCode());
             } else if (fireNumber == 0) {
                 logger.info("手动灭火,灭火设备2路信号正常,发送两路电信号,触发灭火!");
-                map = sendOutFireMsg(hdlist);
+                map = sendOutFireMsg(hdlist,map,list.get(0).getDeviceCode());
             }
         } else {
             logger.info("手动灭火,redis中没有获取到信息,但是仍然需要执行灭火流程,发送两路电信号,触发灭火");
-            map = sendOutFireMsg(hdlist);
+            map = sendOutFireMsg(hdlist,map,list.get(0).getDeviceCode());
         }
 
         //灭火日志添加
@@ -322,8 +457,8 @@ public class FireDeviceController extends BaseController {
      * @return
      * @throws InterruptedException
      */
-    private Map<String, Object> sendOutFireMsg(List<RemoteLabHardware> hdlist) throws InterruptedException {
-        Map<String, Object> map = new HashMap<>();
+    private Map<String, Object> sendOutFireMsg(List<RemoteLabHardware> hdlist,Map<String, Object> map,String deviceCode) throws InterruptedException {
+        //Map<String, Object> map = new HashMap<>();
         StringBuilder msg = new StringBuilder();
         //执行动作结果
         boolean flg = true;
@@ -345,6 +480,12 @@ public class FireDeviceController extends BaseController {
             Thread.sleep(waitTime);
         }
 
+        //通知前端一键灭火正在执行
+        Map<String,Object> mapstatus = new HashMap<>();
+        mapstatus.put("fireStatus",1);//1成功 0失败
+        mapstatus.put("direTime",direTime);
+        mqttSend.send(MqttConstants.TOPIC_FIRE_DEVICE_EXECUTING+deviceCode,JSON.toJSONString(mapstatus));
+
         //关闭继电器
         for (RemoteLabHardware hardware : hdlist) {
             Thread.sleep(waitTime);
@@ -353,7 +494,7 @@ public class FireDeviceController extends BaseController {
             logger.info("关闭继电器,执行结果:" + o);
         }
         map.put("msg", msg);
-        map.put("flg", flg);
+        map.put("flg", true);
         return map;
     }
 
@@ -446,4 +587,9 @@ public class FireDeviceController extends BaseController {
         return ResultData.success(fireDeviceLogService.insertFireAutomatic(code));
     }
 
+    public static void main(String[] args) {
+        Map<String,Object> map = new HashMap<>();
+        map.put("fireStatus",1);
+        System.out.println(JSON.toJSONString(map));
+    }
 }

+ 23 - 2
zd-modules/zd-algorithm/src/main/java/com/zd/alg/fire/utils/FireDeviceStatusTask.java

@@ -1,10 +1,12 @@
 package com.zd.alg.fire.utils;
 
 import com.alibaba.fastjson.JSON;
+import com.zd.alg.fire.controller.FireDeviceController;
 import com.zd.alg.fire.service.IFireDeviceService;
 import com.zd.alg.mqtt.MqttSend;
 import com.zd.algorithm.api.fire.entity.vo.HardwareFireDeviceVO;
 import com.zd.common.core.redis.RedisService;
+import com.zd.common.core.utils.StringUtils;
 import com.zd.model.constant.CacheConstants;
 import com.zd.model.constant.MqttConstants;
 import org.slf4j.Logger;
@@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -33,16 +36,19 @@ public class FireDeviceStatusTask {
     @Autowired
     private RedisService redisService;
 
+    @Autowired
+    private FireDeviceController controller;
 
     /**
      * 定时更新灭火装置状态
      *
      */
     @Scheduled(cron = "0/20 * * * * ?")
-    public void getFireDeviceStatus() {
+    public void getFireDeviceStatus() throws InterruptedException {
         //redis取值
         List<HardwareFireDeviceVO> list = JSON.parseArray(redisService.getCacheObject(CacheConstants.FIRE_DEVICE_LIST) + "", HardwareFireDeviceVO.class);
         if (list == null) {
+            logger.info("====灭火设备集合,redis未取到值,数据库查询======");
             list = hardwareFireDeviceService.selectHardwareFireDeviceList(new HardwareFireDeviceVO());
             //redis存值,时效1分钟
             redisService.setCacheObject(CacheConstants.FIRE_DEVICE_LIST, list, 1L, TimeUnit.MINUTES);
@@ -53,8 +59,23 @@ public class FireDeviceStatusTask {
                 //根据主机地址-位获取主机状态指令
                 String activeCode = FireLaborUtil.getFireActiveOrder(vo.getDeviceUrl());
                 //向mqtt发送状态信息
-                mqttSend.send(MqttConstants.TOPIC_FIRE_DEVICE + vo.getDeviceCode(), activeCode);
+                byte[] activeCodeByte=activeCode.getBytes(StandardCharsets.UTF_8);
+                mqttSend.send(MqttConstants.TOPIC_FIRE_DEVICE + vo.getDeviceCode(), activeCodeByte);
+                //兼容指令无反应问题
+                Thread.sleep(500);
+                mqttSend.send(MqttConstants.TOPIC_FIRE_DEVICE + vo.getDeviceCode(), activeCodeByte);
                 logger.info("集合数量:" + list.size() + ",mqtt消息推送, 灭火装置状态请求,实验室id:" + vo.getSubjectId() + ",设备编号:" + vo.getDeviceCode());
+
+
+                //获取灭火主机最后一次响应时间,判断响应是否超时
+                Long timeJson = redisService.getCacheObject(CacheConstants.FIRE_DEVICE_RESPOND_TIME+list.get(0).getDeviceCode());
+                if (StringUtils.isNull(timeJson)) {
+                    //离线
+                    mqttSend.send(MqttConstants.TOPIC_FIRE_DEVICE_ONLINE+list.get(0).getDeviceCode(), "0");
+                }else{
+                    //在线
+                    mqttSend.send(MqttConstants.TOPIC_FIRE_DEVICE_ONLINE+list.get(0).getDeviceCode(), "1");
+                }
             }
         } else {
             logger.info("====暂未查询到灭火主机信息====");

+ 5 - 0
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/mqtt/config/MqttConfig.java

@@ -270,6 +270,11 @@ public class MqttConfig {
                 }}else if(receivedTopic.startsWith(MqttConstants.TOPIC_FIRE_DEVICE_RECEIVE)){
                     logger.info("灭火消息消费,receivedTopic:" + receivedTopic + " messageStr:" + messageStr);
                     receivedTopic = receivedTopic.substring(receivedTopic.lastIndexOf("/") + 1, receivedTopic.length());
+
+                    //返回非指令时不向下执行
+                    if(receivedTopic.length()<30){
+                        return;
+                    }
                     //根据状态指令获取主机状态
                     JSONObject jsonObject = FireLaborUtil.getFireStatus(messageStr);
                     //redis存值 采集器编号:主机状态json

+ 3 - 2
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/utils/FireLaborUtil.java

@@ -118,7 +118,7 @@ public class FireLaborUtil {
         }
         List<String> mainCodeList=parseArrayList(mainCode);
         JSONObject jsonObject=new JSONObject();
-        if(mainCodeList.get(0).equals("05")){
+        if(mainCodeList.get(0).equals("05") || mainCodeList.get(0).equals("0C")){
             jsonObject.put("startSpray","1");
         }else{
             jsonObject.put("startSpray","0");
@@ -170,8 +170,9 @@ public class FireLaborUtil {
     public static void main(String[] args) {
         boolean fireStatus=false;
         String msg="";
+        String str = "02 03 48 00 00 00 00 02 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0A 00 00 00 40 3D ";
         System.out.println(getFireActiveOrder(2));
-        JSONObject jsonObject=getFireStatus("0");
+        JSONObject jsonObject=getFireStatus(str);
         System.out.println(jsonObject.toJSONString());
     }
 }