|
@@ -4,12 +4,14 @@ import cn.hfln.framework.redis.util.RedisUtil;
|
|
import com.hfln.device.common.constant.redis.RedisCacheConstant;
|
|
import com.hfln.device.common.constant.redis.RedisCacheConstant;
|
|
import com.hfln.device.domain.constant.DeviceConstants;
|
|
import com.hfln.device.domain.constant.DeviceConstants;
|
|
import com.hfln.device.domain.entity.Device;
|
|
import com.hfln.device.domain.entity.Device;
|
|
|
|
+import com.hfln.device.domain.gateway.DeviceGateway;
|
|
import com.hfln.device.domain.service.DeviceManagerService;
|
|
import com.hfln.device.domain.service.DeviceManagerService;
|
|
import com.hfln.device.domain.service.DeviceStatusService;
|
|
import com.hfln.device.domain.service.DeviceStatusService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
@@ -27,12 +29,15 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
|
|
*/
|
|
*/
|
|
private final Map<String, Device> deviceCache = new ConcurrentHashMap<>();
|
|
private final Map<String, Device> deviceCache = new ConcurrentHashMap<>();
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private DeviceStatusService deviceStatusService;
|
|
|
|
|
|
+// @Autowired
|
|
|
|
+// private DeviceStatusService deviceStatusService;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private RedisUtil redisService;
|
|
private RedisUtil redisService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private DeviceGateway deviceGateway;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public Optional<Device> getDeviceFromCache(String devId) {
|
|
public Optional<Device> getDeviceFromCache(String devId) {
|
|
|
|
|
|
@@ -119,7 +124,7 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
|
|
device.setAlarmInterval((long) deviceMap.get("alarmInterval"));
|
|
device.setAlarmInterval((long) deviceMap.get("alarmInterval"));
|
|
}
|
|
}
|
|
if (deviceMap.get("alarmAck") != null) {
|
|
if (deviceMap.get("alarmAck") != null) {
|
|
- device.setAlarmAck((boolean) deviceMap.get("alarmAck"));
|
|
|
|
|
|
+ device.setAlarmAck(Boolean.parseBoolean((String) deviceMap.get("alarmAck")));
|
|
}
|
|
}
|
|
if (deviceMap.get("lastAlarmAckTime") != null) {
|
|
if (deviceMap.get("lastAlarmAckTime") != null) {
|
|
device.setLastAlarmAckTime((long) deviceMap.get("lastAlarmAckTime"));
|
|
device.setLastAlarmAckTime((long) deviceMap.get("lastAlarmAckTime"));
|
|
@@ -131,6 +136,23 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
|
|
device.setFalling((int) deviceMap.get("falling"));
|
|
device.setFalling((int) deviceMap.get("falling"));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ Map<String, Long> lastFallTimeByEvent = new HashMap<>();
|
|
|
|
+ if (deviceMap.get("no_fall") != null) {
|
|
|
|
+ lastFallTimeByEvent.put("no_fall", (Long) deviceMap.get("no_fall"));
|
|
|
|
+ }
|
|
|
|
+ if (deviceMap.get("fall_detected") != null) {
|
|
|
|
+ lastFallTimeByEvent.put("fall_detected", (Long) deviceMap.get("fall_detected"));
|
|
|
|
+ }
|
|
|
|
+ // fall_confirmed
|
|
|
|
+ if (deviceMap.get("fall_confirmed") != null) {
|
|
|
|
+ lastFallTimeByEvent.put("fall_confirmed", (Long) deviceMap.get("fall_confirmed"));
|
|
|
|
+ }
|
|
|
|
+ // fall_calling
|
|
|
|
+ if (deviceMap.get("fall_calling") != null) {
|
|
|
|
+ lastFallTimeByEvent.put("fall_calling", (Long) deviceMap.get("fall_calling"));
|
|
|
|
+ }
|
|
|
|
+ device.setLastFallTime(lastFallTimeByEvent);
|
|
|
|
+
|
|
return Optional.ofNullable(device);
|
|
return Optional.ofNullable(device);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -200,6 +222,16 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
|
|
deviceMap.put("alarmAckInterval", device.getAlarmAckInterval());
|
|
deviceMap.put("alarmAckInterval", device.getAlarmAckInterval());
|
|
// 最后上报跌倒信息 跌倒状态
|
|
// 最后上报跌倒信息 跌倒状态
|
|
deviceMap.put("falling", device.getFalling());
|
|
deviceMap.put("falling", device.getFalling());
|
|
|
|
+ if (!CollectionUtils.isEmpty(device.getLastFallTimeByEvent())) {
|
|
|
|
+ // 没有检测到跌倒
|
|
|
|
+ deviceMap.put("no_fall", device.getLastFallTimeByEvent().get("no_fall"));
|
|
|
|
+ // 检测到跌倒
|
|
|
|
+ deviceMap.put("fall_detected", device.getLastFallTimeByEvent().get("fall_detected"));
|
|
|
|
+ // 跌倒确认
|
|
|
|
+ deviceMap.put("fall_confirmed", device.getLastFallTimeByEvent().get("fall_confirmed"));
|
|
|
|
+ // 跌倒呼救
|
|
|
|
+ deviceMap.put("fall_calling", device.getLastFallTimeByEvent().get("fall_calling"));
|
|
|
|
+ }
|
|
|
|
|
|
// 设备调试参数
|
|
// 设备调试参数
|
|
// deviceMap.put(RedisCacheConstant.KEY_DEVICE_DEBUG_PARAM_PRE + "", device.getDebugParam(""));
|
|
// deviceMap.put(RedisCacheConstant.KEY_DEVICE_DEBUG_PARAM_PRE + "", device.getDebugParam(""));
|
|
@@ -212,12 +244,174 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void updateDeviceInCache(Device device) {
|
|
public void updateDeviceInCache(Device device) {
|
|
- deviceCache.put(device.getDevId(), device);
|
|
|
|
|
|
+
|
|
|
|
+ if (!redisService.sIsMember(RedisCacheConstant.KEY_DEVICE_ID, device.getDevId())) {
|
|
|
|
+ log.info("Device not found in cache: {}", device.getDevId());
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 设置redis 存储设备的 hash对象的属性
|
|
|
|
+ Map<String, Object> deviceMap = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ // 设置设备属性
|
|
|
|
+ if (StringUtils.hasText(device.getDevId())) {
|
|
|
|
+ deviceMap.put("devId", device.getDevId());
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.hasText(device.getDevType())) {
|
|
|
|
+ deviceMap.put("devType", device.getDevType());
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.hasText(device.getSoftware())) {
|
|
|
|
+ deviceMap.put("software", device.getSoftware());
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.hasText(device.getHardware())) {
|
|
|
|
+ deviceMap.put("hardware", device.getHardware());
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.hasText(device.getBluVer())) {
|
|
|
|
+ deviceMap.put("bluVer", device.getBluVer());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 添加网络信息
|
|
|
|
+ if (device.getNetwork() != null) {
|
|
|
|
+ if (StringUtils.hasText(device.getNetwork().getSsid())) {
|
|
|
|
+ deviceMap.put("ssid", device.getNetwork().getSsid());
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.hasText(device.getNetwork().getIp())) {
|
|
|
|
+ deviceMap.put("ip", device.getNetwork().getIp());
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.hasText(device.getNetwork().getPassword())) {
|
|
|
|
+ deviceMap.put("password", device.getNetwork().getPassword());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 添加安装参数
|
|
|
|
+ if (device.getInstallParam() != null) {
|
|
|
|
+ if (StringUtils.hasText(device.getInstallParam().getMountPlain())) {
|
|
|
|
+ deviceMap.put("mountPlain", device.getInstallParam().getMountPlain());
|
|
|
|
+ }
|
|
|
|
+ if (device.getInstallParam().getIsCeiling() != null) {
|
|
|
|
+ deviceMap.put("isCeiling", device.getInstallParam().getIsCeiling());
|
|
|
|
+ }
|
|
|
|
+ if (device.getInstallParam().getHeight() != null) {
|
|
|
|
+ deviceMap.put("height", device.getInstallParam().getHeight());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 添加跟踪区域
|
|
|
|
+ if (device.getInstallParam().getTrackingRegion() != null) {
|
|
|
|
+ if (device.getInstallParam().getTrackingRegion().getStartX() != null) {
|
|
|
|
+ deviceMap.put("startX", device.getInstallParam().getTrackingRegion().getStartX());
|
|
|
|
+ }
|
|
|
|
+ if (device.getInstallParam().getTrackingRegion().getStartY() != null) {
|
|
|
|
+ deviceMap.put("startY", device.getInstallParam().getTrackingRegion().getStartY());
|
|
|
|
+ }
|
|
|
|
+ if (device.getInstallParam().getTrackingRegion().getStartZ() != null) {
|
|
|
|
+ deviceMap.put("startZ", device.getInstallParam().getTrackingRegion().getStartZ());
|
|
|
|
+ }
|
|
|
|
+ if (device.getInstallParam().getTrackingRegion().getStopX() != null) {
|
|
|
|
+ deviceMap.put("stopX", device.getInstallParam().getTrackingRegion().getStopX());
|
|
|
|
+ }
|
|
|
|
+ if (device.getInstallParam().getTrackingRegion().getStopY() != null) {
|
|
|
|
+ deviceMap.put("stopY", device.getInstallParam().getTrackingRegion().getStopY());
|
|
|
|
+ }
|
|
|
|
+ if (device.getInstallParam().getTrackingRegion().getStopZ() != null) {
|
|
|
|
+ deviceMap.put("stopZ", device.getInstallParam().getTrackingRegion().getStopZ());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 实时点位
|
|
|
|
+ // List<Float> targets = new ArrayList<>();
|
|
|
|
+ // String targetsStr = JsonUtil.toJson(targets);
|
|
|
|
+// deviceMap.put("targets", targetsStr);
|
|
|
|
+
|
|
|
|
+ // 在线状态
|
|
|
|
+ if (device.getOnline() != null) {
|
|
|
|
+ deviceMap.put("online", device.getOnline());
|
|
|
|
+ }
|
|
|
|
+ // 最后心跳时间戳
|
|
|
|
+ if (device.getKeepaliveTime() != null) {
|
|
|
|
+ deviceMap.put("keepAliveTime", device.getKeepaliveTime());
|
|
|
|
+ }
|
|
|
|
+ // 超时时间 默认90秒
|
|
|
|
+ if (device.getExpireTime() != null) {
|
|
|
|
+ deviceMap.put("expireTime", device.getExpireTime());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 跌倒信息
|
|
|
|
+ // 最后上报跌倒时间
|
|
|
|
+ if (device.getLastReportFallTime() != null) {
|
|
|
|
+ deviceMap.put("lastReportFallTime", device.getLastReportFallTime());
|
|
|
|
+ }
|
|
|
|
+ // 告警时间间隔(毫秒),默认30秒
|
|
|
|
+ if (device.getAlarmInterval() != null) {
|
|
|
|
+ deviceMap.put("alarmInterval", device.getAlarmInterval());
|
|
|
|
+ }
|
|
|
|
+ // 告警确认
|
|
|
|
+ if (device.getAlarmAck() != null) {
|
|
|
|
+ deviceMap.put("alarmAck", String.valueOf(device.getAlarmAck()));
|
|
|
|
+ }
|
|
|
|
+ // 最后告警确认时间
|
|
|
|
+ if (device.getLastAlarmAckTime() != null) {
|
|
|
|
+ deviceMap.put("lastAlarmAckTime", device.getLastAlarmAckTime());
|
|
|
|
+ }
|
|
|
|
+ // 告警确认时间间间隔(毫秒),默认5分钟
|
|
|
|
+ if (device.getAlarmAckInterval() != null) {
|
|
|
|
+ deviceMap.put("alarmAckInterval", device.getAlarmAckInterval());
|
|
|
|
+ }
|
|
|
|
+ // 最后上报跌倒信息 跌倒状态
|
|
|
|
+ if (device.getFalling() != null) {
|
|
|
|
+ deviceMap.put("falling", device.getFalling());
|
|
|
|
+ }
|
|
|
|
+ if (!CollectionUtils.isEmpty(device.getLastFallTimeByEvent())) {
|
|
|
|
+ Map<String, Long> fallTimeMap = device.getLastFallTimeByEvent();
|
|
|
|
+ if (fallTimeMap != null) {
|
|
|
|
+ // 没有检测到跌倒
|
|
|
|
+ if (fallTimeMap.containsKey("no_fall") && fallTimeMap.get("no_fall") != null) {
|
|
|
|
+ deviceMap.put("no_fall", fallTimeMap.get("no_fall"));
|
|
|
|
+ }
|
|
|
|
+ // 检测到跌倒
|
|
|
|
+ if (fallTimeMap.containsKey("fall_detected") && fallTimeMap.get("fall_detected") != null) {
|
|
|
|
+ deviceMap.put("fall_detected", fallTimeMap.get("fall_detected"));
|
|
|
|
+ }
|
|
|
|
+ // 跌倒确认
|
|
|
|
+ if (fallTimeMap.containsKey("fall_confirmed") && fallTimeMap.get("fall_confirmed") != null) {
|
|
|
|
+ deviceMap.put("fall_confirmed", fallTimeMap.get("fall_confirmed"));
|
|
|
|
+ }
|
|
|
|
+ // 跌倒呼救
|
|
|
|
+ if (fallTimeMap.containsKey("fall_calling") && fallTimeMap.get("fall_calling") != null) {
|
|
|
|
+ deviceMap.put("fall_calling", fallTimeMap.get("fall_calling"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 设备调试参数
|
|
|
|
+// deviceMap.put(RedisCacheConstant.KEY_DEVICE_DEBUG_PARAM_PRE + "", device.getDebugParam(""));
|
|
|
|
+
|
|
|
|
+ redisService.hSetAll(RedisCacheConstant.KEY_DEVICE_pre + device.getDevId(), deviceMap);
|
|
log.debug("Device updated in cache: {}", device.getDevId());
|
|
log.debug("Device updated in cache: {}", device.getDevId());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void updateDeviceKeepAliveInCache(String devId, Long keepaliveTime) {
|
|
|
|
+// deviceCache.put(device.getDevId(), device);
|
|
|
|
+ if (redisService.sIsMember(RedisCacheConstant.KEY_DEVICE_ID, devId)) {
|
|
|
|
+ redisService.hSet(RedisCacheConstant.KEY_DEVICE_pre + devId, "keepaliveTime", keepaliveTime);
|
|
|
|
+ }
|
|
|
|
+ log.debug("Device updated in cache: {}", devId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void updateDeviceAliveStatusInCache(String devId) {
|
|
|
|
+ Map<String, Object> updateDeviceMap = new HashMap<>();
|
|
|
|
+ updateDeviceMap.put("online", 1);
|
|
|
|
+ updateDeviceMap.put("keepAliveTime", System.currentTimeMillis());
|
|
|
|
+ redisService.hSetAll(RedisCacheConstant.KEY_DEVICE_pre + devId, updateDeviceMap);
|
|
|
|
+ }
|
|
|
|
+
|
|
public void updateDeviceMapInCache(String devId, Map<String, Object> map) {
|
|
public void updateDeviceMapInCache(String devId, Map<String, Object> map) {
|
|
|
|
|
|
|
|
+ if (CollectionUtils.isEmpty( map)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
redisService.hSetAll(RedisCacheConstant.KEY_DEVICE_pre + devId, map);
|
|
redisService.hSetAll(RedisCacheConstant.KEY_DEVICE_pre + devId, map);
|
|
log.debug("Device updated in cache: {}", devId);
|
|
log.debug("Device updated in cache: {}", devId);
|
|
}
|
|
}
|
|
@@ -242,13 +436,20 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
-// for (Object devIdObj : devIdSet) {
|
|
|
|
-// Object onlineObj = redisService.hGet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "online");
|
|
|
|
-// if (onlineObj != null && Integer.parseInt(onlineObj.toString()) == 1) {
|
|
|
|
-// Long lastKeepAliveTime = redisService.hGet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "keepAliveTime");
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-//
|
|
|
|
|
|
+ for (Object devIdObj : devIdSet) {
|
|
|
|
+ Object onlineObj = redisService.hGet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "online");
|
|
|
|
+ if (onlineObj != null && Integer.parseInt(onlineObj.toString()) == 1) {
|
|
|
|
+ Long lastKeepAliveTime = (Long) redisService.hGet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "keepAliveTime");
|
|
|
|
+ // 如果设备无保活时间或超时,则设置为离线
|
|
|
|
+ if (lastKeepAliveTime == null || (currentTimeMillis - lastKeepAliveTime) > timeoutMillis) {
|
|
|
|
+ log.info("Device keepalive timeout: {}, last keepalive: {}", devIdObj, lastKeepAliveTime);
|
|
|
|
+ redisService.hSet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "online", 0);
|
|
|
|
+ // todo 更新数据库 设备状态
|
|
|
|
+ deviceGateway.updateDeviceOnlineStatus((String) devIdObj, 0);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
// deviceCache.forEach((devId, device) -> {
|
|
// deviceCache.forEach((devId, device) -> {
|
|
// // 只检查在线设备
|
|
// // 只检查在线设备
|
|
// if (device.getOnline() != null && device.getOnline() == 1) {
|
|
// if (device.getOnline() != null && device.getOnline() == 1) {
|