|
@@ -0,0 +1,798 @@
|
|
|
+package com.hfln.device.application.service.impl;
|
|
|
+
|
|
|
+import com.hfln.device.application.service.DeviceEventService;
|
|
|
+import com.hfln.device.domain.constant.EventConstants;
|
|
|
+import com.hfln.device.domain.entity.Device;
|
|
|
+import com.hfln.device.domain.gateway.DeviceGateway;
|
|
|
+import com.hfln.device.domain.gateway.MqttGateway;
|
|
|
+import com.hfln.device.domain.service.AlarmService;
|
|
|
+import com.hfln.device.domain.service.BehaviorAnalysisService;
|
|
|
+import com.hfln.device.domain.service.DeviceManagerService;
|
|
|
+import com.hfln.device.domain.service.PointCloudProcessService;
|
|
|
+import com.hfln.device.domain.vo.BehaviorPattern;
|
|
|
+import com.hfln.device.domain.vo.PoseAnalysisResult;
|
|
|
+import com.hfln.device.domain.vo.TargetPoint;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 设备事件服务实现类
|
|
|
+ * 应用层服务,负责协调领域服务和基础设施服务
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class DeviceEventServiceImpl implements DeviceEventService {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(DeviceEventServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceGateway deviceGateway;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MqttGateway mqttGateway;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceManagerService deviceManagerService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PointCloudProcessService pointCloudProcessService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmService alarmService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BehaviorAnalysisService behaviorAnalysisService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean registerDevice(Device device) {
|
|
|
+ log.info("注册设备: deviceId={}", device.getDevId());
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 检查设备是否存在
|
|
|
+ Optional<Device> existingDeviceOpt = deviceManagerService.getDeviceFromCache(device.getDevId());
|
|
|
+
|
|
|
+ if (existingDeviceOpt.isPresent()) {
|
|
|
+ log.info("设备已存在,更新设备信息: {}", device.getDevId());
|
|
|
+
|
|
|
+ // 获取现有设备
|
|
|
+ Device existingDevice = existingDeviceOpt.get();
|
|
|
+
|
|
|
+ // 更新设备信息
|
|
|
+ if (device.getDevType() != null) {
|
|
|
+ existingDevice.setDevType(device.getDevType());
|
|
|
+ }
|
|
|
+ if (device.getSoftware() != null) {
|
|
|
+ existingDevice.setSoftware(device.getSoftware());
|
|
|
+ }
|
|
|
+ if (device.getHardware() != null) {
|
|
|
+ existingDevice.setHardware(device.getHardware());
|
|
|
+ }
|
|
|
+ if (device.getOnline() != null) {
|
|
|
+ existingDevice.updateOnlineStatus(device.getOnline());
|
|
|
+ }
|
|
|
+ if (device.getKeepaliveTime() != null) {
|
|
|
+ existingDevice.updateKeepAliveTime(device.getKeepaliveTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设备网络信息
|
|
|
+ if (device.getNetwork() != null) {
|
|
|
+ if (existingDevice.getNetwork() == null) {
|
|
|
+ existingDevice.setNetwork(new Device.NetworkInfo());
|
|
|
+ }
|
|
|
+ if (device.getNetwork().getSsid() != null) {
|
|
|
+ existingDevice.getNetwork().setSsid(device.getNetwork().getSsid());
|
|
|
+ }
|
|
|
+ if (device.getNetwork().getPassword() != null) {
|
|
|
+ existingDevice.getNetwork().setPassword(device.getNetwork().getPassword());
|
|
|
+ }
|
|
|
+ if (device.getNetwork().getIp() != null) {
|
|
|
+ existingDevice.getNetwork().setIp(device.getNetwork().getIp());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设备安装参数
|
|
|
+ if (device.getInstallParam() != null) {
|
|
|
+ if (existingDevice.getInstallParam() == null) {
|
|
|
+ existingDevice.setInstallParam(new Device.InstallParam());
|
|
|
+ }
|
|
|
+ if (device.getInstallParam().getMountPlain() != null) {
|
|
|
+ existingDevice.getInstallParam().setMountPlain(device.getInstallParam().getMountPlain());
|
|
|
+ }
|
|
|
+ if (device.getInstallParam().getHeight() != null) {
|
|
|
+ existingDevice.getInstallParam().setHeight(device.getInstallParam().getHeight());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新跟踪区域
|
|
|
+ if (device.getInstallParam().getTrackingRegion() != null) {
|
|
|
+ if (existingDevice.getInstallParam().getTrackingRegion() == null) {
|
|
|
+ existingDevice.getInstallParam().setTrackingRegion(new Device.TrackingRegion());
|
|
|
+ }
|
|
|
+ Device.TrackingRegion sourceRegion = device.getInstallParam().getTrackingRegion();
|
|
|
+ Device.TrackingRegion targetRegion = existingDevice.getInstallParam().getTrackingRegion();
|
|
|
+
|
|
|
+ if (sourceRegion.getStartX() != null) {
|
|
|
+ targetRegion.setStartX(sourceRegion.getStartX());
|
|
|
+ }
|
|
|
+ if (sourceRegion.getStartY() != null) {
|
|
|
+ targetRegion.setStartY(sourceRegion.getStartY());
|
|
|
+ }
|
|
|
+ if (sourceRegion.getStartZ() != null) {
|
|
|
+ targetRegion.setStartZ(sourceRegion.getStartZ());
|
|
|
+ }
|
|
|
+ if (sourceRegion.getStopX() != null) {
|
|
|
+ targetRegion.setStopX(sourceRegion.getStopX());
|
|
|
+ }
|
|
|
+ if (sourceRegion.getStopY() != null) {
|
|
|
+ targetRegion.setStopY(sourceRegion.getStopY());
|
|
|
+ }
|
|
|
+ if (sourceRegion.getStopZ() != null) {
|
|
|
+ targetRegion.setStopZ(sourceRegion.getStopZ());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设备
|
|
|
+ deviceGateway.updateDevice(existingDevice);
|
|
|
+
|
|
|
+ // 更新设备缓存
|
|
|
+ deviceManagerService.updateDeviceInCache(existingDevice);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ log.info("设备不存在,创建新设备: {}", device.getDevId());
|
|
|
+
|
|
|
+ // 保存设备
|
|
|
+ Device savedDevice = deviceGateway.saveDevice(device);
|
|
|
+
|
|
|
+ if (savedDevice != null) {
|
|
|
+ // 添加到缓存
|
|
|
+ deviceManagerService.updateDeviceInCache(savedDevice);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ log.warn("设备注册失败: deviceId={}", device.getDevId());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("设备注册异常: deviceId={}, error={}", device.getDevId(), e.getMessage(), e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleDeviceLogin(String deviceId, Map<String, Object> deviceInfo) {
|
|
|
+ log.info("处理设备登录事件: deviceId={}", deviceId);
|
|
|
+
|
|
|
+ // 检查设备是否存在
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ Device device = null;
|
|
|
+
|
|
|
+ // 如果设备不存在,则创建设备
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.info("设备不存在,创建新设备: {}", deviceId);
|
|
|
+ deviceGateway.createDevice(deviceId, deviceInfo);
|
|
|
+
|
|
|
+ // 获取新创建的设备
|
|
|
+ device = deviceGateway.getDeviceById(deviceId);
|
|
|
+ } else {
|
|
|
+ log.info("设备已存在,更新设备信息: {}", deviceId);
|
|
|
+ // 获取现有设备
|
|
|
+ device = deviceOpt.get();
|
|
|
+
|
|
|
+ if (device != null) {
|
|
|
+ // 更新设备信息
|
|
|
+ // 从deviceInfo中提取信息更新设备
|
|
|
+ if (deviceInfo.containsKey("dev_type")) {
|
|
|
+ device.setDevType((String) deviceInfo.get("dev_type"));
|
|
|
+ }
|
|
|
+ if (deviceInfo.containsKey("software")) {
|
|
|
+ device.setSoftware((String) deviceInfo.get("software"));
|
|
|
+ }
|
|
|
+ if (deviceInfo.containsKey("hardware")) {
|
|
|
+ device.setHardware((String) deviceInfo.get("hardware"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新网络信息
|
|
|
+ if (deviceInfo.containsKey("network")) {
|
|
|
+ Map<String, Object> networkInfo = (Map<String, Object>) deviceInfo.get("network");
|
|
|
+ if (device.getNetwork() == null) {
|
|
|
+ device.setNetwork(new Device.NetworkInfo());
|
|
|
+ }
|
|
|
+ if (networkInfo.containsKey("ssid")) {
|
|
|
+ device.getNetwork().setSsid((String) networkInfo.get("ssid"));
|
|
|
+ }
|
|
|
+ if (networkInfo.containsKey("password")) {
|
|
|
+ device.getNetwork().setPassword((String) networkInfo.get("password"));
|
|
|
+ }
|
|
|
+ if (networkInfo.containsKey("ip")) {
|
|
|
+ device.getNetwork().setIp((String) networkInfo.get("ip"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新安装参数
|
|
|
+ if (deviceInfo.containsKey("radar_param")) {
|
|
|
+ Map<String, Object> radarParam = (Map<String, Object>) deviceInfo.get("radar_param");
|
|
|
+ if (device.getInstallParam() == null) {
|
|
|
+ device.setInstallParam(new Device.InstallParam());
|
|
|
+ }
|
|
|
+ if (radarParam.containsKey("mount_plain")) {
|
|
|
+ device.getInstallParam().setMountPlain((String) radarParam.get("mount_plain"));
|
|
|
+ }
|
|
|
+ if (radarParam.containsKey("height")) {
|
|
|
+ device.getInstallParam().setHeight(((Number) radarParam.get("height")).floatValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新跟踪区域
|
|
|
+ if (radarParam.containsKey("tracking_region")) {
|
|
|
+ Map<String, Object> trackingRegion = (Map<String, Object>) radarParam.get("tracking_region");
|
|
|
+ if (device.getInstallParam().getTrackingRegion() == null) {
|
|
|
+ device.getInstallParam().setTrackingRegion(new Device.TrackingRegion());
|
|
|
+ }
|
|
|
+ Device.TrackingRegion region = device.getInstallParam().getTrackingRegion();
|
|
|
+
|
|
|
+ if (trackingRegion.containsKey("start_x")) {
|
|
|
+ region.setStartX(((Number) trackingRegion.get("start_x")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("start_y")) {
|
|
|
+ region.setStartY(((Number) trackingRegion.get("start_y")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("start_z")) {
|
|
|
+ region.setStartZ(((Number) trackingRegion.get("start_z")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("stop_x")) {
|
|
|
+ region.setStopX(((Number) trackingRegion.get("stop_x")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("stop_y")) {
|
|
|
+ region.setStopY(((Number) trackingRegion.get("stop_y")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("stop_z")) {
|
|
|
+ region.setStopZ(((Number) trackingRegion.get("stop_z")).intValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设备信息
|
|
|
+ deviceGateway.updateDevice(device);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果获取到设备,更新在线状态和缓存
|
|
|
+ if (device != null) {
|
|
|
+ // 更新设备在线状态和保活时间
|
|
|
+ device.updateOnlineStatus(1);
|
|
|
+ device.updateKeepAliveTime(System.currentTimeMillis());
|
|
|
+
|
|
|
+ // 更新设备缓存
|
|
|
+ deviceManagerService.updateDeviceInCache(device);
|
|
|
+
|
|
|
+ // 更新设备在线状态
|
|
|
+ deviceGateway.updateDeviceOnlineStatus(deviceId, 1);
|
|
|
+
|
|
|
+ // 发送登录响应
|
|
|
+ mqttGateway.sendDeviceLoginResponse(deviceId, 0);
|
|
|
+
|
|
|
+ // 发送设备状态更新
|
|
|
+ mqttGateway.sendDeviceStatusMessage(device);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleDeviceKeepAlive(String deviceId) {
|
|
|
+ log.info("处理设备保活事件: deviceId={}", deviceId);
|
|
|
+
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+
|
|
|
+ if (deviceOpt.isPresent()) {
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 更新设备保活时间
|
|
|
+ device.updateKeepAliveTime(System.currentTimeMillis());
|
|
|
+
|
|
|
+ // 如果设备离线,更新为在线
|
|
|
+ if (device.getOnline() == null || device.getOnline() != 1) {
|
|
|
+ device.updateOnlineStatus(1);
|
|
|
+
|
|
|
+ // 更新设备在线状态
|
|
|
+ deviceGateway.updateDeviceOnlineStatus(deviceId, 1);
|
|
|
+
|
|
|
+ // 发送设备状态更新
|
|
|
+ mqttGateway.sendDeviceStatusMessage(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设备缓存
|
|
|
+ deviceManagerService.updateDeviceInCache(device);
|
|
|
+
|
|
|
+ // 更新数据库中的保活时间
|
|
|
+ deviceGateway.updateDeviceKeepAliveTime(deviceId, System.currentTimeMillis());
|
|
|
+
|
|
|
+ // 发送保活响应
|
|
|
+ mqttGateway.sendDeviceKeepAliveResponse(deviceId, 0);
|
|
|
+ } else {
|
|
|
+ log.warn("设备不存在,无法处理保活事件: {}", deviceId);
|
|
|
+
|
|
|
+ // 创建一个临时设备对象,用于发送保活响应
|
|
|
+ Device device = new Device(deviceId);
|
|
|
+ device.setOnline(1);
|
|
|
+ device.setKeepaliveTime(System.currentTimeMillis());
|
|
|
+
|
|
|
+ // 添加到缓存
|
|
|
+ deviceManagerService.updateDeviceInCache(device);
|
|
|
+
|
|
|
+ // 发送保活响应
|
|
|
+ mqttGateway.sendDeviceKeepAliveResponse(deviceId, 1); // 1表示设备未注册
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleAlarmAck(String deviceId, Long eventId) {
|
|
|
+ log.info("处理告警确认: deviceId={}, eventId={}", deviceId, eventId);
|
|
|
+
|
|
|
+ // 使用领域服务确认告警
|
|
|
+ boolean updated = alarmService.acknowledgeAlarm(eventId, null);
|
|
|
+
|
|
|
+ // 发送告警确认消息
|
|
|
+ mqttGateway.sendAlarmAckMessage(deviceId, eventId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleDeviceFallEvent(String deviceId, int pose, List<Float> targetPoint) {
|
|
|
+ log.info("处理设备跌倒事件: deviceId={}, pose={}", deviceId, pose);
|
|
|
+
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.warn("设备不存在,无法处理跌倒事件: {}", deviceId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+
|
|
|
+ // 使用领域服务处理跌倒告警
|
|
|
+ Long eventId = alarmService.handleFallAlarm(deviceId, pose, targetPoint, timestamp);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleFallEvent(String deviceId, String event, Integer pose, List<Float> targetPoint) {
|
|
|
+ log.info("处理跌倒事件: deviceId={}, event={}, pose={}", deviceId, event, pose);
|
|
|
+
|
|
|
+ // 调用事件处理方法
|
|
|
+ handleEvent(deviceId, event, pose, targetPoint);
|
|
|
+
|
|
|
+ // 如果是确认跌倒事件,处理跌倒告警
|
|
|
+ if ("fall_confirmed".equals(event)) {
|
|
|
+ handleDeviceFallEvent(deviceId, pose, targetPoint);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleDeviceStatusUpdate(String deviceId, boolean online, String devType, String software,
|
|
|
+ String hardware, Map<String, Object> network, Map<String, Object> radarParam) {
|
|
|
+ log.info("处理设备状态更新: deviceId={}, online={}", deviceId, online);
|
|
|
+
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ Device device = null;
|
|
|
+
|
|
|
+ if (deviceOpt.isPresent()) {
|
|
|
+ device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 更新设备在线状态
|
|
|
+ device.updateOnlineStatus(online ? 1 : 0);
|
|
|
+
|
|
|
+ // 更新设备信息
|
|
|
+ device.setDevType(devType);
|
|
|
+ device.setSoftware(software);
|
|
|
+ device.setHardware(hardware);
|
|
|
+
|
|
|
+ // 更新网络信息
|
|
|
+ if (network != null) {
|
|
|
+ if (device.getNetwork() == null) {
|
|
|
+ device.setNetwork(new Device.NetworkInfo());
|
|
|
+ }
|
|
|
+ if (network.containsKey("ssid")) {
|
|
|
+ device.getNetwork().setSsid((String) network.get("ssid"));
|
|
|
+ }
|
|
|
+ if (network.containsKey("password")) {
|
|
|
+ device.getNetwork().setPassword((String) network.get("password"));
|
|
|
+ }
|
|
|
+ if (network.containsKey("ip")) {
|
|
|
+ device.getNetwork().setIp((String) network.get("ip"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新安装参数
|
|
|
+ if (radarParam != null) {
|
|
|
+ if (device.getInstallParam() == null) {
|
|
|
+ device.setInstallParam(new Device.InstallParam());
|
|
|
+ }
|
|
|
+ if (radarParam.containsKey("mount_plain")) {
|
|
|
+ device.getInstallParam().setMountPlain((String) radarParam.get("mount_plain"));
|
|
|
+ }
|
|
|
+ if (radarParam.containsKey("height")) {
|
|
|
+ device.getInstallParam().setHeight(((Number) radarParam.get("height")).floatValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新跟踪区域
|
|
|
+ if (radarParam.containsKey("tracking_region")) {
|
|
|
+ Map<String, Object> trackingRegion = (Map<String, Object>) radarParam.get("tracking_region");
|
|
|
+ if (device.getInstallParam().getTrackingRegion() == null) {
|
|
|
+ device.getInstallParam().setTrackingRegion(new Device.TrackingRegion());
|
|
|
+ }
|
|
|
+ Device.TrackingRegion region = device.getInstallParam().getTrackingRegion();
|
|
|
+
|
|
|
+ if (trackingRegion.containsKey("start_x")) {
|
|
|
+ region.setStartX(((Number) trackingRegion.get("start_x")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("start_y")) {
|
|
|
+ region.setStartY(((Number) trackingRegion.get("start_y")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("start_z")) {
|
|
|
+ region.setStartZ(((Number) trackingRegion.get("start_z")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("stop_x")) {
|
|
|
+ region.setStopX(((Number) trackingRegion.get("stop_x")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("stop_y")) {
|
|
|
+ region.setStopY(((Number) trackingRegion.get("stop_y")).intValue());
|
|
|
+ }
|
|
|
+ if (trackingRegion.containsKey("stop_z")) {
|
|
|
+ region.setStopZ(((Number) trackingRegion.get("stop_z")).intValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设备缓存
|
|
|
+ deviceManagerService.updateDeviceInCache(device);
|
|
|
+
|
|
|
+ // 更新设备信息
|
|
|
+ deviceGateway.updateDevice(device);
|
|
|
+
|
|
|
+ // 更新设备在线状态
|
|
|
+ deviceGateway.updateDeviceOnlineStatus(deviceId, online ? 1 : 0);
|
|
|
+
|
|
|
+ // 发送设备状态更新
|
|
|
+ mqttGateway.sendDeviceStatusMessage(device);
|
|
|
+ } else {
|
|
|
+ log.warn("设备不存在,无法更新状态: {}", deviceId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleCloudPoint(String deviceId, List<List<Float>> pointCloud, List<Float> targetPoint) {
|
|
|
+ log.info("处理点云数据: deviceId={}, pointCloudSize={}", deviceId, pointCloud.size());
|
|
|
+
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.warn("设备不存在,无法处理点云数据: {}", deviceId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 使用点云处理服务分析姿态
|
|
|
+ int pose = pointCloudProcessService.analyzePose(pointCloud);
|
|
|
+
|
|
|
+ // 发送实时位置姿态消息
|
|
|
+ mqttGateway.sendRealtimePoseMessage(deviceId, pose, targetPoint);
|
|
|
+
|
|
|
+ // 分析姿态行为
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ // 创建目标点对象
|
|
|
+ TargetPoint target = TargetPoint.fromList(targetPoint);
|
|
|
+ target.setTimestamp(timestamp);
|
|
|
+ List<TargetPoint> targetPoints = Collections.singletonList(target);
|
|
|
+
|
|
|
+ // 创建姿态分析结果
|
|
|
+ PoseAnalysisResult poseResult = PoseAnalysisResult.createDefault(deviceId, pose);
|
|
|
+ poseResult.setTimestamp(timestamp);
|
|
|
+
|
|
|
+ // 分析行为
|
|
|
+ BehaviorPattern poseBehavior = behaviorAnalysisService.analyzeBehavior(device, poseResult, targetPoints);
|
|
|
+
|
|
|
+ // 如果是跌倒姿态,处理跌倒事件
|
|
|
+ if (pose == 2) { // 假设2表示跌倒姿态
|
|
|
+ handleDeviceFallEvent(deviceId, pose, targetPoint);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleRealtimePosition(String deviceId, Integer pose, List<List<Float>> targets) {
|
|
|
+ log.info("处理实时位置姿态: deviceId={}, pose={}, targetsSize={}", deviceId, pose, targets.size());
|
|
|
+
|
|
|
+ // 发送实时位置姿态消息
|
|
|
+ if (!targets.isEmpty()) {
|
|
|
+ mqttGateway.sendRealtimePoseMessage(deviceId, pose, targets.get(0));
|
|
|
+ } else {
|
|
|
+ mqttGateway.sendRealtimePoseMessage(deviceId, pose, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleEvent(String deviceId, String event, Integer pose, List<Float> targetPoint) {
|
|
|
+ log.info("处理事件消息: deviceId={}, event={}, pose={}", deviceId, event, pose);
|
|
|
+
|
|
|
+ // 记录事件
|
|
|
+ deviceGateway.recordEvent(deviceId, event, pose, targetPoint);
|
|
|
+
|
|
|
+ // 发送事件消息
|
|
|
+ mqttGateway.sendEventMessage(deviceId, pose, targetPoint, event);
|
|
|
+
|
|
|
+ // 如果是跌倒事件,处理跌倒逻辑
|
|
|
+ if (EventConstants.EventType.FALL_EVENT.equals(event)) {
|
|
|
+ handleDeviceFallEvent(deviceId, pose, targetPoint);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleExistEvent(String deviceId, String event) {
|
|
|
+ log.info("处理存在事件: deviceId={}, event={}", deviceId, event);
|
|
|
+
|
|
|
+ // 记录存在事件
|
|
|
+ deviceGateway.recordExistEvent(deviceId, event);
|
|
|
+
|
|
|
+ // 发送存在事件消息
|
|
|
+ mqttGateway.sendExistenceMessage(deviceId, event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleAlarmEvent(String deviceId, String desc, String table, Integer tableId) {
|
|
|
+ log.info("处理告警事件: deviceId={}, desc={}, table={}, tableId={}", deviceId, desc, table, tableId);
|
|
|
+
|
|
|
+ // 记录告警事件
|
|
|
+ deviceGateway.recordAlarmEvent(deviceId, desc, table, tableId);
|
|
|
+
|
|
|
+ // 发送告警事件消息
|
|
|
+ mqttGateway.sendAlarmEventMessage(deviceId, desc, table, tableId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleReportAlarmParam(Map<String, Object> globalConfig) {
|
|
|
+ log.info("处理上报告警参数: {}", globalConfig);
|
|
|
+
|
|
|
+ // 保存告警参数配置
|
|
|
+ deviceGateway.saveAlarmConfig(globalConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleSetAlarmParamAck(Integer code, Map<String, Object> globalConfig) {
|
|
|
+ log.info("处理设置告警参数确认: code={}, config={}", code, globalConfig);
|
|
|
+
|
|
|
+ // 如果设置成功,更新配置
|
|
|
+ if (code == 0) {
|
|
|
+ deviceGateway.saveAlarmConfig(globalConfig);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean recordDeviceStayTime(String deviceId, Long enterTime, Long leaveTime, String stayTime) {
|
|
|
+ log.info("记录设备停留时间: deviceId={}, enterTime={}, leaveTime={}", deviceId, enterTime, leaveTime);
|
|
|
+
|
|
|
+ // 调用领域层网关记录设备停留时间
|
|
|
+ return deviceGateway.recordDeviceStayTime(deviceId, enterTime, leaveTime, stayTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean recordDeviceRetentionAlarm(String deviceId, Long alarmTime, String alarmType, String description) {
|
|
|
+ log.info("记录设备滞留告警: deviceId={}, alarmTime={}, alarmType={}", deviceId, alarmTime, alarmType);
|
|
|
+
|
|
|
+ // 调用领域层网关记录设备滞留告警
|
|
|
+ return deviceGateway.recordDeviceRetentionAlarm(deviceId, alarmTime, alarmType, description);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean updateDeviceOnlineStatus(String deviceId, Integer online) {
|
|
|
+ log.info("更新设备在线状态: deviceId={}, online={}", deviceId, online);
|
|
|
+
|
|
|
+ // 调用领域层网关更新设备在线状态
|
|
|
+ return deviceGateway.updateDeviceOnlineStatus(deviceId, online);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean updateDeviceKeepAliveTime(String deviceId, Long keepaliveTime) {
|
|
|
+ log.info("更新设备保活时间: deviceId={}, keepaliveTime={}", deviceId, keepaliveTime);
|
|
|
+
|
|
|
+ // 调用领域层网关更新设备保活时间
|
|
|
+ return deviceGateway.updateDeviceKeepAliveTime(deviceId, keepaliveTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void sendAlarmEventMessage(String deviceId, String description, String table, int tableId) {
|
|
|
+ log.info("发送告警事件消息: deviceId={}, description={}, table={}, tableId={}",
|
|
|
+ deviceId, description, table, tableId);
|
|
|
+
|
|
|
+ // 调用领域层网关发送告警事件消息
|
|
|
+ mqttGateway.sendAlarmEventMessage(deviceId, description, table, tableId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleBehaviorAnalysis(String deviceId, BehaviorPattern pattern) {
|
|
|
+ log.info("处理行为分析结果: deviceId={}, pattern={}", deviceId, pattern);
|
|
|
+
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.warn("设备不存在,无法处理行为分析结果: {}", deviceId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 保存行为模式到设备实体
|
|
|
+ device.addBehaviorPattern(pattern);
|
|
|
+
|
|
|
+ // 如果是异常行为,处理告警
|
|
|
+ if (pattern.getBehaviorType() != null && pattern.getBehaviorType() == 3) { // 3表示异常活动
|
|
|
+ alarmService.handleBehaviorAlarm(deviceId, pattern);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleActivityBehavior(String deviceId, Integer activityLevel, Long duration, List<Float> location, Long timestamp) {
|
|
|
+ log.info("处理设备活动行为: deviceId={}, activityLevel={}, duration={}", deviceId, activityLevel, duration);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.warn("设备不存在,无法处理活动行为: {}", deviceId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 创建目标点对象
|
|
|
+ TargetPoint target = TargetPoint.fromList(location);
|
|
|
+ target.setTimestamp(timestamp);
|
|
|
+ List<TargetPoint> targetPoints = Collections.singletonList(target);
|
|
|
+
|
|
|
+ // 使用领域服务分析活动行为
|
|
|
+ BehaviorPattern pattern = behaviorAnalysisService.detectActivityBehavior(device, targetPoints);
|
|
|
+ if (pattern != null) {
|
|
|
+ pattern.setActivityLevel(activityLevel);
|
|
|
+ pattern.setDuration(duration);
|
|
|
+ pattern.setTimestamp(timestamp);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录活动行为数据
|
|
|
+ Map<String, Object> activityData = new HashMap<>();
|
|
|
+ activityData.put("activity_level", activityLevel);
|
|
|
+ activityData.put("duration", duration);
|
|
|
+ activityData.put("timestamp", timestamp);
|
|
|
+
|
|
|
+ if (location != null && !location.isEmpty()) {
|
|
|
+ activityData.put("location", location);
|
|
|
+ }
|
|
|
+
|
|
|
+ deviceGateway.recordBehaviorData(deviceId, "activity", activityData);
|
|
|
+
|
|
|
+ // 如果是异常行为,处理告警
|
|
|
+ if (pattern != null && pattern.isAbnormalActivity()) {
|
|
|
+ alarmService.handleBehaviorAlarm(deviceId, pattern);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理设备活动行为异常: {}", e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleRestBehavior(String deviceId, Long duration, List<Float> location, String areaName, Long timestamp) {
|
|
|
+ log.info("处理设备休息行为: deviceId={}, duration={}, areaName={}", deviceId, duration, areaName);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.warn("设备不存在,无法处理休息行为: {}", deviceId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 创建目标点对象
|
|
|
+ TargetPoint target = TargetPoint.fromList(location);
|
|
|
+ target.setTimestamp(timestamp);
|
|
|
+ List<TargetPoint> targetPoints = Collections.singletonList(target);
|
|
|
+
|
|
|
+ // 使用领域服务分析休息行为
|
|
|
+ BehaviorPattern pattern = behaviorAnalysisService.detectRestBehavior(device, targetPoints, timestamp);
|
|
|
+ if (pattern != null) {
|
|
|
+ pattern.setDuration(duration);
|
|
|
+ pattern.setAreaName(areaName);
|
|
|
+ pattern.setTimestamp(timestamp);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录休息行为数据
|
|
|
+ Map<String, Object> restData = new HashMap<>();
|
|
|
+ restData.put("duration", duration);
|
|
|
+ restData.put("timestamp", timestamp);
|
|
|
+
|
|
|
+ if (location != null && !location.isEmpty()) {
|
|
|
+ restData.put("location", location);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (areaName != null) {
|
|
|
+ restData.put("area_name", areaName);
|
|
|
+ }
|
|
|
+
|
|
|
+ deviceGateway.recordBehaviorData(deviceId, "rest", restData);
|
|
|
+
|
|
|
+ // 如果是异常行为,处理告警
|
|
|
+ if (pattern != null && pattern.isAbnormalActivity()) {
|
|
|
+ alarmService.handleBehaviorAlarm(deviceId, pattern);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理设备休息行为异常: {}", e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleActivityHeatmap(String deviceId, List<List<Float>> heatmapData, Long timestamp) {
|
|
|
+ log.info("处理活动热力图: deviceId={}, heatmapDataSize={}", deviceId, heatmapData.size());
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.warn("设备不存在,无法处理活动热力图: {}", deviceId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 分析活动热力图
|
|
|
+ List<List<Float>> analyzedHeatmap = behaviorAnalysisService.analyzeActivityHeatmap(device, 60); // 分析最近60分钟的数据
|
|
|
+
|
|
|
+ // 记录热力图数据
|
|
|
+ Map<String, Object> heatmapInfo = new HashMap<>();
|
|
|
+ heatmapInfo.put("heatmap_data", heatmapData);
|
|
|
+ heatmapInfo.put("timestamp", timestamp);
|
|
|
+
|
|
|
+ deviceGateway.recordBehaviorData(deviceId, "activity_heatmap", heatmapInfo);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理活动热力图异常: {}", e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handlePoseDistribution(String deviceId, Map<String, Object> distribution, Long timestamp) {
|
|
|
+ log.info("处理姿态分布: deviceId={}, distribution={}", deviceId, distribution);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取设备
|
|
|
+ Optional<Device> deviceOpt = deviceManagerService.getDeviceFromCache(deviceId);
|
|
|
+ if (!deviceOpt.isPresent()) {
|
|
|
+ log.warn("设备不存在,无法处理姿态分布: {}", deviceId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Device device = deviceOpt.get();
|
|
|
+
|
|
|
+ // 记录姿态分布数据
|
|
|
+ Map<String, Object> poseData = new HashMap<>(distribution);
|
|
|
+ poseData.put("timestamp", timestamp);
|
|
|
+
|
|
|
+ deviceGateway.recordBehaviorData(deviceId, "pose_distribution", poseData);
|
|
|
+
|
|
|
+ // 更新设备姿态统计
|
|
|
+ device.updatePose(Integer.parseInt(distribution.getOrDefault("pose", "0").toString()), timestamp);
|
|
|
+ deviceManagerService.updateDeviceInCache(device);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理姿态分布异常: {}", e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|