Browse Source

feat:双屏版电梯屏,实时转发电梯运行状态至电梯屏

lihao16 3 months ago
parent
commit
95038a8ab6

+ 28 - 0
elevator-data-collect/src/main/java/com/inspur/elevator_data_collect/dao/DeviceStatisticsDayInfoDao.java

@@ -0,0 +1,28 @@
+package com.inspur.elevator_data_collect.dao;
+
+import com.inspur.elevator_data_collect.domain.DeviceStatisticsDayInfo;
+import org.apache.ibatis.annotations.Mapper;
+import org.springframework.stereotype.Repository;
+
+/**
+ * 设备日统计信息Dao
+ * @author lihao16
+ */
+@Repository
+@Mapper
+public interface DeviceStatisticsDayInfoDao {
+
+    /**
+     * 查询指定设备统计信息
+     * @param deviceId
+     * @return
+     */
+    DeviceStatisticsDayInfo selectSumStatisticsInfo(String deviceId);
+
+    /**
+     * 查询摄像头所在电梯关联的电梯屏设备SN
+     * @param cameraId
+     * @return
+     */
+    String selectScreenSnByCameraId(String cameraId);
+}

+ 55 - 0
elevator-data-collect/src/main/java/com/inspur/elevator_data_collect/domain/DeviceStatisticsDayInfo.java

@@ -0,0 +1,55 @@
+package com.inspur.elevator_data_collect.domain;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * 运行数据-运行统计
+ *
+ * @author lihao16
+ */
+@Data
+public class DeviceStatisticsDayInfo implements Serializable {
+
+    /**
+     * 终端唯一编号
+     */
+    private String deviceId;
+
+    /**
+     * 乘客人次
+     */
+    private Integer personCount;
+
+    /**
+     * 运行次数
+     */
+    private Integer runningCount;
+
+    /**
+     * 运行距离(米)
+     */
+    private Float runningDistance;
+
+    /**
+     * 运行时间(秒)
+     */
+    private Float runningTime;
+
+    /**
+     * 健康度
+     */
+    private Float healthyScore;
+
+    /**
+     * 舒适度
+     */
+    private Float comfortScore;
+
+    /**
+     * 字典elevator_status,电梯状态:0已停运,1维保中,2年检中,3急修中,4正常,5发生故障
+     */
+    private Integer elevatorStatus;
+
+}

+ 81 - 1
elevator-data-collect/src/main/java/com/inspur/elevator_data_collect/service/WriteMongoService.java

@@ -2,12 +2,19 @@ package com.inspur.elevator_data_collect.service;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.inspur.elevator_data_collect.dao.DeviceStatisticsDayInfoDao;
+import com.inspur.elevator_data_collect.domain.DeviceStatisticsDayInfo;
+import com.inspur.elevator_data_collect.mqtt.MqttGateway;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author liuxu
@@ -15,12 +22,38 @@ import java.util.Date;
  * mongo写入服务
  */
 @Service
+@Slf4j
 public class WriteMongoService {
 
     @Autowired
     private MongoTemplate mongoTemplate;
 
-    public void write(String payload, String collectionName){
+    @Autowired
+    private DeviceStatisticsDayInfoDao deviceStatisticsDayInfoDao;
+
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
+
+    @Autowired
+    private MqttGateway mqttGateway;
+
+    /**
+     * 统计信息缓存key
+     */
+    private static final String STATISTICS_INFO_KEY = "device_statistics_info:front:";
+
+    /**
+     * 摄像头关联电梯/电梯屏 缓存key
+     */
+    private static final String CAMERA_REL_SCREEN_KEY = "camera_rel_screen:";
+
+    /**
+     * APK mqtt消息订阅
+     */
+    private static final String FRONT_STATISTICS_INFO_TOPIC = "ele/event/device_statistics_info/front/";
+
+    public void write(String payload, String collectionName) {
+        // 1 实时运行状态写入Mongodb
         Date date = new Date();
         String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
         String dateString = new SimpleDateFormat("yyyyMMdd").format(date);
@@ -34,6 +67,53 @@ public class WriteMongoService {
         payloadJson.put("create_time", createTime);
 
         mongoTemplate.insert(payloadJson, collectionNameNew);
+        // 2 读取设备的运行总状态
+        String deviceId = payloadJson.getString("id");
+        JSONObject statisticsInfo = getDeviceStatisticsInfo(deviceId);
+        payloadJson.put("statisticsInfo", statisticsInfo);
+        // 3 发布mqtt消息至电梯屏应急呼叫应用
+        // 这里的deviceId是黑匣子的SN 需要根据关联电梯,再根据 获取电梯屏SN
+        String screenSN = getCameraRelDeviceId(deviceId);
+        if (StringUtils.isBlank(screenSN)) {
+            log.info("没有找到摄像头{}关联的电梯屏设备SN", deviceId);
+            return;
+        }
+        mqttGateway.sendToMqtt(FRONT_STATISTICS_INFO_TOPIC + screenSN, 0, payloadJson.toJSONString());
+        log.info("发布mqtt消息至电梯屏,{}应急呼叫应用:{}", screenSN, payloadJson.toJSONString());
+    }
 
+    private String getCameraRelDeviceId(String deviceId) {
+        // 这里的deviceId是黑匣子的SN 即摄像头的SN
+        String screenSN = (String) redisTemplate.opsForValue().get(CAMERA_REL_SCREEN_KEY + deviceId);
+        if (StringUtils.isNotBlank(screenSN)) {
+            log.info("get screen by redis cache screen : {}", screenSN);
+            return screenSN;
+        }
+        screenSN = deviceStatisticsDayInfoDao.selectScreenSnByCameraId(deviceId);
+        if (StringUtils.isNotBlank(screenSN)) {
+            // 为防止缓存穿透,设置缓存时间为随机6-12小时
+            long randomTime = (long) (Math.random() * 12 + 6);
+            redisTemplate.opsForValue().set(CAMERA_REL_SCREEN_KEY + deviceId, screenSN, randomTime, TimeUnit.HOURS);
+            return screenSN;
+        }
+        return screenSN;
+    }
+
+    private JSONObject getDeviceStatisticsInfo(String deviceId) {
+
+        JSONObject jsonResult = (JSONObject) redisTemplate.opsForValue().get(STATISTICS_INFO_KEY + deviceId);
+        if (jsonResult != null) {
+            log.info("get statistics info from redis cache, deviceId : {}", deviceId);
+            return jsonResult;
+        }
+        DeviceStatisticsDayInfo deviceStatisticsDayInfo = deviceStatisticsDayInfoDao.selectSumStatisticsInfo(deviceId);
+        if (null != deviceStatisticsDayInfo) {
+            jsonResult = (JSONObject) JSON.toJSON(deviceStatisticsDayInfo);
+            // 为防止缓存穿透,设置缓存时间为随机60-120分钟
+            long randomTime = (long) (Math.random() * 60 + 60);
+            redisTemplate.opsForValue().set(STATISTICS_INFO_KEY + deviceId, jsonResult, randomTime, TimeUnit.MINUTES);
+            return jsonResult;
+        }
+        return new JSONObject();
     }
 }

+ 38 - 0
elevator-data-collect/src/main/resources/mapper/DeviceStatisticsDayInfoDao.xml

@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.inspur.elevator_data_collect.dao.DeviceStatisticsDayInfoDao">
+    <resultMap id="BaseResultMap" type="com.inspur.elevator_data_collect.domain.DeviceStatisticsDayInfo">
+        <id column="device_id" jdbcType="VARCHAR" property="deviceId" />
+        <result column="person_count" jdbcType="INTEGER" property="personCount" />
+        <result column="running_count" jdbcType="INTEGER" property="runningCount" />
+        <result column="running_distance" jdbcType="REAL" property="runningDistance" />
+        <result column="running_time" jdbcType="REAL" property="runningTime" />
+        <result column="healthy_score" jdbcType="REAL" property="healthyScore" />
+        <result column="comfort_score" jdbcType="REAL" property="comfortScore" />
+    </resultMap>
+
+    <select id="selectSumStatisticsInfo" parameterType="java.lang.String" resultMap="BaseResultMap">
+        SELECT
+            device_id,
+            SUM(IFNULL(running_count, 0)) AS running_count,
+            SUM(IFNULL(running_distance, 0)) AS running_distance,
+            SUM(IFNULL(person_count, 0)) AS person_count,
+            SUM(IFNULL(running_time, 0)) AS running_time,
+            healthy_score,
+            comfort_score
+        FROM
+            device_statistics_day_info
+        WHERE
+             device_id = #{deviceId};
+    </select>
+
+    <select id="selectScreenSnByCameraId" parameterType="java.lang.String" resultType="java.lang.String">
+        SELECT
+            screen_id
+        FROM
+            ele_device_screen
+        WHERE
+            elevator_id = (SELECT elevator_id FROM ele_device_info WHERE device_id = #{cameraId})
+        LIMIT 1
+    </select>
+</mapper>

+ 1 - 4
elevator-hn-adapter/src/main/java/com/inspur/elevator/bean/elevator/DeviceEvent.java

@@ -96,9 +96,6 @@ public class DeviceEvent {
     /**
      * 亮度。0:偏暗,1:正常,2:偏亮
      */
-    /**
-     * 电梯内人数
-     */
     private Integer br;
     /**
      * 维保标志.“1”:维保中;“0”:未维保
@@ -108,4 +105,4 @@ public class DeviceEvent {
      * 备用电池电压
      */
     private Double bpv;
-}
+}