Browse Source

update device event logic

wuweihao 5 years ago
parent
commit
6e5a1f8db2

File diff suppressed because it is too large
+ 360 - 317
.idea/workspace.xml


+ 2 - 1
wsm-admin-dao/src/main/java/com/wsm/admin/dao/IDeviceEventDao.java

@@ -16,5 +16,6 @@ public interface IDeviceEventDao extends IBaseDao<DeviceEvent, Long> {
     @Query(value = "SELECT * from tb_device_event where device_id = ?1 and rec_status = 'A' order by update_time desc LIMIT 0,1", nativeQuery = true)
     DeviceEvent findByDeviceIdTop(Long id);
 
-
+    @Query(value = "SELECT * from tb_device_event where device_id = ?1 and rec_status = 'A' and status = ?2 ", nativeQuery = true)
+    DeviceEvent findByStatusAndDeviceId(Long deviceId, Integer status);
 }

+ 20 - 0
wsm-admin-dao/src/main/java/com/wsm/admin/model/DeviceEvent.java

@@ -25,12 +25,23 @@ public class DeviceEvent extends BaseModel implements Serializable {
      * 事件内容
      */
     private String content;
+
+    /**
+     * 事件异常状态: 0-正常  1-报警  2-低压  3-故障
+     *
+     */
+    @Column
+    private Integer status;
+
     /**
      * 处理状态:0-待解决, 1-已立案,2-出警中,3-已解决
      */
     @Column(length = 2)
     private Byte handleStatus;
 
+
+
+
     /**
      * 上报时间 作去重校验
      */
@@ -62,6 +73,15 @@ public class DeviceEvent extends BaseModel implements Serializable {
         this.handleStatus = handleStatus;
     }
 
+
+    public Integer getStatus() {
+        return status;
+    }
+
+    public void setStatus(Integer status) {
+        this.status = status;
+    }
+
     public Date getPostTime() {
         return postTime;
     }

+ 2 - 0
wsm-admin-service/src/main/java/com/wsm/admin/service/IDeviceEventService.java

@@ -17,4 +17,6 @@ public interface IDeviceEventService extends IBaseService<DeviceEvent, Long> {
      * @return
      */
     DeviceEvent findByDeviceIdTop(Long deviceId);
+
+    DeviceEvent findByStatusAndDeviceId(Long deviceId, Integer status);
 }

+ 5 - 0
wsm-admin-service/src/main/java/com/wsm/admin/service/impl/DeviceEventServiceImpl.java

@@ -33,4 +33,9 @@ public class DeviceEventServiceImpl extends BaseServiceImpl<DeviceEvent, Long> i
     public DeviceEvent findByDeviceIdTop(Long deviceId) {
         return deviceEventDao.findByDeviceIdTop(deviceId);
     }
+
+    @Override
+    public DeviceEvent findByStatusAndDeviceId(Long deviceId, Integer status) {
+        return deviceEventDao.findByStatusAndDeviceId( deviceId,  status);
+    }
 }

+ 2 - 1
wsm-admin-web/src/main/java/com/wsm/admin/api/ApiDeviceController.java

@@ -14,6 +14,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -38,8 +39,8 @@ public class ApiDeviceController extends BaseController {
     public AjaxJson list() {
         List<Device> list = (List) redisTemplate.opsForValue().get(MsgCode.REDIS_CYCLE_DEVICE_LIST);
         if (list == null) {
-            logger.info("list size: ");
             list = deviceService.findAll();
+            logger.warn("list device size: " + list.size());
             redisTemplate.opsForValue().set(MsgCode.REDIS_CYCLE_DEVICE_LIST, list, 24, TimeUnit.HOURS);
 
         }

+ 6 - 4
wsm-admin-web/src/main/java/com/wsm/admin/api/DeviceEventController.java

@@ -104,11 +104,11 @@ public class DeviceEventController extends BaseController {
         if (event != null){
           event.setUpdateTime(new Date());
           event.setHandleStatus(deviceEvent.getHandleStatus());
-//          deviceEventService.save(event)
-          event = deviceEventService.update(event);
+          if (deviceEvent.getHandleStatus() == 3) {
+              // 更新事件状态
+              event.setStatus(0);
 
-          // 3: 已处理
-          if (event.getHandleStatus() == 3) {
+              // 更新设备状态
               Device device = event.getDevice();
               device.setStatus((byte)0);
               device.setUpdateTime(new Date());
@@ -117,6 +117,8 @@ public class DeviceEventController extends BaseController {
               // 删除缓存
               redisTemplate.delete(MsgCode.REDIS_CYCLE_DEVICE_LIST);
           }
+          deviceEventService.update(event);
+
         }
 
 

+ 13 - 7
wsm-admin-web/src/main/java/com/wsm/admin/device/init/TaskSchedule.java

@@ -38,12 +38,12 @@ public class TaskSchedule {
 
 
     /**
-     * 每天1:10检查未开始的活动
+     * 每天1检查未开始的活动
      * 每天24点执行一次  @Scheduled(cron=" 0 0 0 * * ? ") @Scheduled(cron=" * 1  * * ? ")
      *
      * Scheduled(cron=" 10 0/5 * * * ? ")   //每五分钟执行一次
      */
-    @Scheduled(cron=" 10 0 10 * * ? ")
+    @Scheduled(cron=" 0 0 1 * * ? ")
 //    @Scheduled(cron=" 10 0/5 * * * ? ")
     public void checkDeviceId(){
         logger.info("run checkDeviceId");
@@ -55,16 +55,22 @@ public class TaskSchedule {
             // 如果不存在
             if (!b) {
                 logger.info("离线设备号:" + d.getDeviceId());
-                DeviceEvent event = new DeviceEvent();
-//                DeviceEvent event = deviceEventService.findByDeviceIdTop(d.getId());
+
+                // 设备离线,如果有离线设备事件没有处理,就更新时间,让它置顶。若没有,就创建一个事件. 3:故障
+
+                DeviceEvent event = deviceEventService.findByStatusAndDeviceId(d.getId(), 3);
+                if (event == null) {
                     event = new DeviceEvent();
                     event.setCreateTime(new Date());
                     event.setDevice(d);
                     event.setPostTime(new Date());
+                    // 事件异常状态: 0-正常  1-报警  2-低压  3-故障
+                    event.setStatus(3);
+                    // 处理状态:0-待解决, 1-已立案,2-出警中,3-已解决
+                    event.setHandleStatus((byte)0);
+                    event.setContent("设备离线");
+                }
 
-                // 0:报警 待解决
-                event.setHandleStatus((byte)0);
-                event.setContent("设备离线");
                 event.setUpdateTime(new Date());
                 deviceEventService.save(event);
 

+ 6 - 4
wsm-admin-web/src/main/java/com/wsm/admin/device/mqtt/PushCallback.java

@@ -103,6 +103,7 @@ public class PushCallback implements MqttCallback {
         // 用来心跳检测
         if (TopicCode.TOPIC_DEV_MSG.equals(topic)) {
             redisUtil.set(devid,"heartbeat", Long.parseLong("25"));
+            log.warn("heartbeat device id: {}", devid);
             return;
         }
 
@@ -118,6 +119,7 @@ public class PushCallback implements MqttCallback {
         Device device = deviceService.findByDeviceId(devid);
         if (device != null && alarmType.equals(TYPE_FIRE)) {
             // 报警
+            Integer status = 1;
             device.setStatus((byte)1);
             device.setUpdateTime(new Date());
             deviceService.update(device);
@@ -126,13 +128,13 @@ public class PushCallback implements MqttCallback {
             redisUtil.delete(MsgCode.REDIS_CYCLE_DEVICE_LIST);
 
             // 记录报警详情
-            DeviceEvent event = new DeviceEvent();
-//            DeviceEvent event = deviceEventService.findByDeviceIdTop(device.getId());
-//            if (event == null) {
+            DeviceEvent event = deviceEventService.findByStatusAndDeviceId(device.getId(), status);
+            if (event == null) {
                 event = new DeviceEvent();
                 event.setDevice(device);
                 event.setCreateTime(new Date());
-//            }
+            }
+            event.setStatus(status);
             event.setHandleStatus((byte)0);
             event.setContent(alarmTypeName);
             event.setUpdateTime(new Date());

+ 3 - 1
wsm-admin-web/src/main/java/com/wsm/admin/device/tcp/ConvertData.java

@@ -35,6 +35,8 @@ public class ConvertData {
 		ruleList.add(new Rule("纬度方向", "string", 112, 114));
 		ruleList.add(new Rule("纬度", "string", 114, 134));
 		ruleList.add(new Rule("status", null, 134, 142));
+		ruleList.add(new Rule("type", "int", 142, 144));
+
 	}
 
 	public Map<String, Object> convert(String date) {
@@ -75,7 +77,7 @@ public class ConvertData {
 	}
 
 	public static void main(String[] args) {
-		String d = "010001004A0000A3010044514300010003020100000001130C020B0B1EFFF3000000F100000000000000000000453131332E3536393639354E3032322E3337363735390000000000000000000000004140";
+		String d = "010001004A0000A3010044514300010003020100000001130C12031632FFF6000000FA00000000000000000000453131332E3536393635364E3032322E3337363732360000000055550000000000006140";
 		System.out.println(d.length());
 		ConvertData cd = new ConvertData();
 		Map<String, Object> hm = cd.convert(d);

+ 46 - 12
wsm-admin-web/src/main/java/com/wsm/admin/device/tcp/NettyServerHandler.java

@@ -66,10 +66,21 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 	 */
 	private static final Integer CHECK_DATA_LENGTH = 162;
 
-	private static final double JG_ERC40 = 10;
+	private static final double JG_ERC40 = 18;
 
 	private static final double JG_ERC90 = 70;
 
+	/**
+	 * 心跳
+	 */
+	private static final int CODE_HEARTBEAT = 85;
+
+
+	/**
+	 * 报警
+	 */
+	private static final int CODE_ALARM = 0;
+
 
 
 
@@ -122,16 +133,27 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 
 			if (StringUtils.isNotEmpty(inString) && inString.length() == CHECK_DATA_LENGTH){
 
-				// 这应该是心跳,直接结束
-//				if (inString.contains(JG_HEARTBEAT)) {
-//					return;
-//				}
 
 				ConvertData cd = new ConvertData();
 				Map<String, Object> hm = cd.convert(inString);
 
 
 				log.info("解析json: {}", JSON.toJSONString(hm));
+
+				int type = (int)hm.get("type");
+				// 设备id
+				String sn2 = hm.get("sn2").toString();
+
+
+				// 心跳检测
+				if (type == CODE_HEARTBEAT) {
+					redisUtil.set(sn2,"heartbeat", Long.parseLong("25"));
+					log.warn("heartbeat device id: {}", sn2);
+					return;
+
+				}
+
+
 				String status = hm.get("status").toString();
 
 				// 计算井盖角度
@@ -140,8 +162,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 				double angle = angle(Double.parseDouble(ox), Double.parseDouble(oy));
 				log.info("井盖开启角度: {} 度", Math.round(angle));
 
-				// 设备id
-				String sn2 = hm.get("sn2").toString();
+
 				Device device = deviceService.findByDeviceId(sn2);
 				log.warn("JG设备号: {}", sn2);
 
@@ -163,31 +184,43 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 				//报警开关
 				boolean needEvent = false;
 				String content = "";
+				Integer devideStatus = null;
 
+
+				// 状态类型:0-正常  1-报警  2-低压  3-故障
 				if (device != null) {
+
+					needEvent = true;
+
 					// 低电压
 					if (JG_LOW_PRESSURE_CODE.equals(status)) {
 						device.setStatus((byte)2);
+						devideStatus = 2;
+						content = deviceStatusMap.get(JG_LOW_PRESSURE_CODE);
 
 						// 故障
 					} else if (JG_LOW_PRESSURE_OFF.equals(status)) {
 						device.setStatus((byte)3);
+						devideStatus = 3;
+						content = deviceStatusMap.get(JG_LOW_PRESSURE_OFF);
 
 						// 报警
 					} else if (JG_NORMAL.equals(status)) {
 						device.setStatus((byte)1);
-						needEvent = true;
 
 						//处理报警类型
+						devideStatus = 1;
 						// 大于40度
 
 						if (angle > JG_ERC40) {
 							// 报警
 							content = deviceStatusMap.get("ERC40");
 
+
 						}
 						if (angle > JG_ERC90) {
 							content = deviceStatusMap.get("ERC90");
+
 						}
 					}
 					device.setUpdateTime(new Date());
@@ -200,13 +233,14 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 
 					// 记录报警详情
 					if (needEvent) {
-						DeviceEvent event  = new DeviceEvent();
-//						DeviceEvent event = deviceEventService.findByDeviceIdTop(device.getId());
-//						if (event == null) {
+						DeviceEvent event = deviceEventService.findByStatusAndDeviceId(device.getId(), devideStatus);
+						if (event == null) {
 							event = new DeviceEvent();
 							event.setDevice(device);
 							event.setCreateTime(new Date());
-//						}
+						}
+						event.setStatus(devideStatus);
+						// 处理状态:0-待解决, 1-已立案,2-出警中,3-已解决
 						event.setHandleStatus((byte)0);
 						event.setContent(content);
 						event.setUpdateTime(new Date());

+ 17 - 1
wsm-application/src/main/resources/static/js/map.js

@@ -41,6 +41,22 @@ function init(){
         geocoder.getLocation($("#addressSearch").val());
     });
 
+    /*地图坐标查询*/
+    $("#searchCoordBtn").click(function () {
+        var vlat = $("#latitude").val();
+        var vlng = $("#longitude").val();
+
+        // 根据坐标获取位置
+        var vlatLng = new qq.maps.LatLng(vlat, vlng);
+
+        if (geocoder == null) {
+            geocoder = getGeocoder();
+        }
+        // 显示位置
+        geocoder.getAddress(vlatLng);
+
+    });
+
 }
 
 function getGeocoder(){
@@ -61,7 +77,7 @@ function getGeocoder(){
             var marker = new qq.maps.Marker({
                 map:map,
                 position: result.detail.location,
-                draggable: false,
+                draggable: false
             });
             markersArray.push(marker);
         }

+ 24 - 3
wsm-application/src/main/resources/templates/admin/device/form.html

@@ -30,8 +30,8 @@
         <div class="layui-card-body">
             <form class="layui-form" action="/admin/device/save" method="post">
                 <input type="hidden" id="deviceId" name="id" th:value="${device.id}">
-                <input type="hidden" id="longitude" name="longitude" th:value="${device.longitude}">
-                <input type="hidden" id="latitude" name="latitude" th:value="${device.latitude}">
+                <!--<input type="hidden" id="longitude" name="longitude" th:value="${device.longitude}">-->
+                <!--<input type="hidden" id="latitude" name="latitude" th:value="${device.latitude}">-->
                 <div class="layui-form-item">
                     <label class="layui-form-label">设备号</label>
                     <div class="layui-input-inline">
@@ -55,12 +55,33 @@
                         </select>
                     </div>
                 </div>
+
+
+                <div class="layui-form-item">
+                    <label class="layui-form-label">坐标</label>
+                    <div class="layui-input-block">
+                        <div class="layui-col-xs6">
+                            <div class="layui-col-xs6">
+                                <input  type="text"   lay-verify="required" id="longitude" name="longitude" th:value="${device.longitude}"
+                                        lay-vertype="tips" placeholder="请输入经度" class="layui-input" >
+                            </div>
+                            <div class="layui-col-xs6">
+                                <input  type="text"  lay-verify="required" id="latitude" name="latitude" th:value="${device.latitude}"
+                                        lay-vertype="tips" placeholder="请输入纬度" class="layui-input" >
+                            </div>
+                        </div>
+                        <div class="layui-col-xs3" style="text-align: left;">
+                            <button id="searchCoordBtn" type="button" class="layui-btn">查找</button>
+                        </div>
+                    </div>
+                </div>
+
                 <div class="layui-form-item">
                     <label class="layui-form-label">地址</label>
                     <div class="layui-input-block">
                         <div class="layui-col-xs6">
                             <input id="addressSearch" type="text" name="address" th:value="${device.address}" lay-verify="required"
-                                lay-vertype="tips" class="layui-input" >
+                                   lay-vertype="tips" class="layui-input" >
                         </div>
                         <div class="layui-col-xs3" style="text-align: left;">
                             <button id="searchBtn" type="button" class="layui-btn">查找</button>

+ 5 - 0
wsm-application/src/main/resources/templates/admin/device/list.html

@@ -41,6 +41,7 @@
         <th lay-data="{field:'id', sort: true}">Id</th>
         <th lay-data="{field:'deviceId', sort: true}">设备id</th>
         <th lay-data="{field:'deviceType', sort: true}">设备类型</th>
+        <th lay-data="{field:'status', sort: true}">设备状态</th>
         <th lay-data="{field:'opt', fixed: 'right', align:'center', width:300}">操作</th>
     </tr>
     </thead>
@@ -50,6 +51,10 @@
         <td th:text="${device.deviceId}"></td>
         <td th:if="${device.deviceType=='YG'}">烟感</td>
         <td th:if="${device.deviceType=='JG'}">井盖</td>
+        <td th:if="${device.status== 0}">正常</td>
+        <td th:if="${device.status== 1}">报警</td>
+        <td th:if="${device.status== 2}">低压</td>
+        <td th:if="${device.status== 3}">故障</td>
         <td>
             <a class="layui-btn layui-btn-xs" lay-event="detail">
                 <i class="layui-icon">&#xe642;</i>编辑</a>

+ 9 - 7
wsm-application/src/main/resources/templates/admin/deviceEvent/form.html

@@ -28,12 +28,13 @@
     <div class="layui-card">
         <div class="layui-card-header">查看设备告警</div>
         <div class="layui-card-body">
-            <form class="layui-form" action="" method="post">
+            <form class="layui-form" action="/admin/deviceEvent/save" method="post">
+                <input type="hidden" id="deviceEventId" name="id" th:value="${deviceEvent.id}">
                 <div class="layui-form-item">
                     <label class="layui-form-label">设备id</label>
                     <div class="layui-input-inline">
                         <input type="text" name="deviceId" th:value="${device.deviceId}" lay-verify="required"
-                               lay-vertype="tips" class="layui-input" readonly>
+                               lay-vertype="tips" class="layui-input" readonly disabled="disabled">
                     </div>
                 </div>
                 <div class="layui-form-item">
@@ -55,7 +56,8 @@
                 <div class="layui-form-item">
                     <label class="layui-form-label">处理状态</label>
                     <div class="layui-input-inline">
-                        <select name="handleStatus" lay-verify="required" readonly disabled="disabled">
+                        <!--<select name="handleStatus" lay-verify="required" readonly disabled="disabled">-->
+                        <select name="handleStatus" lay-verify="required" >
                             <option value="0" th:selected="${#strings.equals(deviceEvent.handleStatus,'0')}">待解决</option>
                             <option value="1" th:selected="${#strings.equals(deviceEvent.handleStatus,'1')}">已立案</option>
                             <option value="2" th:selected="${#strings.equals(deviceEvent.handleStatus,'2')}">出警中</option>
@@ -75,7 +77,7 @@
                     <div class="layui-input-block">
                         <div class="layui-col-xs6">
                             <input id="addressSearch" type="text" name="address" th:value="${device.address}" lay-verify="required"
-                                lay-vertype="tips" class="layui-input" readonly>
+                                   lay-vertype="tips" class="layui-input" readonly>
                         </div>
                     </div>
                 </div>
@@ -87,8 +89,8 @@
                 </div>
                 <div class="layui-form-item">
                     <div class="layui-input-block">
-                        <!--<button class="layui-btn" lay-submit lay-filter="save"
-                                th:text="${#strings.equals(device.id,null)?'保存':'修改'}"></button>-->
+                        <button class="layui-btn" lay-submit lay-filter="save"
+                                th:text="${#strings.equals(device.id,null)?'保存':'修改'}"></button>
                         <a href="javascript:history.back()" type="button" class="layui-btn layui-btn-primary">返回</a>
                     </div>
                 </div>
@@ -117,7 +119,7 @@
                 success: function (data) {
                     if (data.code == 0) {
                         layer.msg(data.msg, {icon: 1, time: 1000}, function () {
-                            location.href = "/admin/device/list";
+                            location.href = "/admin/deviceEvent/list";
                         });
                     } else {
                         layer.msg(data.msg, {time: 1000});

+ 3 - 1
wsm-application/src/main/resources/templates/admin/deviceEvent/list.html

@@ -41,7 +41,8 @@
         <th lay-data="{field:'id', sort: true}">Id</th>
         <th lay-data="{field:'content', sort: true}">告警内容</th>
         <th lay-data="{field:'handleStatus', sort: true}">处理状态</th>
-        <th lay-data="{field:'createTime', sort: true}">时间</th>
+        <th lay-data="{field:'createTime', sort: true}">创建时间</th>
+        <th lay-data="{field:'updateTime', sort: true}">更新时间</th>
         <th lay-data="{field:'opt', fixed: 'right', align:'center', width:300}">操作</th>
     </tr>
     </thead>
@@ -54,6 +55,7 @@
         <td th:if="${deviceEvent.handleStatus==2}">出警中</td>
         <td th:if="${deviceEvent.handleStatus==3}">已解决</td>
         <td th:text="${#dates.format(deviceEvent.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
+        <td th:text="${#dates.format(deviceEvent.updateTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
         <td>
             <a shiro:hasPermission="admin:deviceEvent:edit" class="layui-btn layui-btn-xs" lay-event="detail">
                 <i class="layui-icon">&#xe642;</i>查看</a>