|
|
@@ -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();
|
|
|
}
|
|
|
}
|