2025-11-3 zwy 新增state标记,redis中增加设备的实时状态,机场内飞行器的状态信息websocket发给前端;
| | |
| | | package com.ard.sample.component.websocket.enums; |
| | | |
| | | public enum MessageType { |
| | | STATE("state"), |
| | | STATUS("status"), |
| | | CONTROL("control"), |
| | | PROGRESS("progress"), |
| | |
| | | Payload payload = webSocketMessage.getPayload(); |
| | | |
| | | // 验证消息类型是否合法 |
| | | if (!Arrays.asList("status", "control", "progress").contains(type)) { |
| | | if (!Arrays.asList("status", "control", "progress", "state").contains(type)) { |
| | | session.sendMessage(new TextMessage("不支持的消息类型:" + type)); |
| | | return; |
| | | } |
| | |
| | | String deviceSn = receiver.getGateway(); |
| | | if(method.equals("cover_close")){ |
| | | _deviceService.updateWorkType(deviceSn, 2L); |
| | | RedisOpsUtils.setWithExpire(RedisConst.OSD_STATUS_PREFIX + deviceSn, 2, RedisConst.DEVICE_ALIVE_SECOND); |
| | | } |
| | | String sn = RedisOpsUtils.get(key).toString(); |
| | | receiver.setType(MessageType.PROGRESS.getType()); |
| | |
| | | // 更新设备列表状态--飞行器开机中... |
| | | if(method.equals("drone_open")){ |
| | | _deviceService.updateWorkType(device.getChildDeviceSn(), 7L); |
| | | RedisOpsUtils.setWithExpire(RedisConst.OSD_STATUS_PREFIX + device.getChildDeviceSn(), 7, RedisConst.DEVICE_ALIVE_SECOND); |
| | | } |
| | | |
| | | String dockProgress =""; |
| | |
| | | import com.ard.sample.component.mqtt.model.CommonTopicReceiver; |
| | | |
| | | import com.ard.sample.component.websocket.config.ConcurrentWebSocketSession; |
| | | import com.ard.sample.component.websocket.enums.MessageType; |
| | | import com.ard.sample.component.websocket.handler.DockWebSocketHandler; |
| | | import com.ard.sample.component.websocket.model.BizCodeEnum; |
| | | import com.ard.sample.component.websocket.model.CustomWebSocketMessage; |
| | | import com.ard.sample.manage.model.dto.DeviceDTO; |
| | |
| | | import com.ard.sample.manage.model.enums.DeviceDomainEnum; |
| | | import com.ard.sample.manage.model.receiver.OsdPayloadReceiver; |
| | | import com.ard.sample.manage.model.receiver.OsdSubDeviceReceiver; |
| | | import com.ard.sample.manage.service.IDeviceService; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | |
| | | @Service |
| | | @Slf4j |
| | | public class DeviceOSDServiceImpl extends AbstractTSAService { |
| | | |
| | | @Autowired |
| | | private IDeviceService _deviceService; |
| | | |
| | | @Autowired |
| | | private DockWebSocketHandler _dockWebSocketHandler; |
| | | |
| | | protected DeviceOSDServiceImpl(@Autowired @Qualifier("dockOSDServiceImpl") AbstractTSAService tsaService) { |
| | | super(tsaService); |
| | |
| | | |
| | | OsdSubDeviceReceiver data = mapper.convertValue(receiver.getData(), OsdSubDeviceReceiver.class); |
| | | List<DevicePayloadDTO> payloadsList = device.getPayloadsList(); |
| | | String dockOsd = ""; |
| | | receiver.setType(MessageType.STATUS.getType()+"-aircraft"); |
| | | try { |
| | | dockOsd=mapper.writeValueAsString(receiver); |
| | | JsonNode jsonNode = mapper.readTree(dockOsd); |
| | | if(jsonNode.has("data") && jsonNode.get("data").has("mode_code")) { |
| | | Long code = jsonNode.get("data").get("mode_code").asLong(); |
| | | if("0,1,2,3,4,5,8,9,10,11,13,14".contains(code.toString())){ |
| | | _deviceService.updateWorkType(device.getDeviceSn(),code); |
| | | RedisOpsUtils.setWithExpire(RedisConst.OSD_STATUS_PREFIX + device.getDeviceSn(), code, RedisConst.DEVICE_ALIVE_SECOND); |
| | | } |
| | | } |
| | | Map<String, Object> receiverData = (Map<String, Object>) receiver.getData(); |
| | | data.setPayloads(payloadsList.stream() |
| | | .map(payload -> mapper.convertValue( |
| | |
| | | OsdPayloadReceiver.class)) |
| | | .collect(Collectors.toList())); |
| | | |
| | | } catch (NullPointerException e) { |
| | | } catch (NullPointerException | JsonProcessingException e) { |
| | | log.warn("Please remount the payload, or restart the drone. Otherwise the data of the payload will not be received."); |
| | | } |
| | | |
| | | RedisOpsUtils.setWithExpire(RedisConst.OSD_PREFIX + device.getDeviceSn(), data, RedisConst.DEVICE_ALIVE_SECOND); |
| | | wsMessage.getData().setHost(data); |
| | | |
| | | // 发送给websocket客户端 |
| | | _dockWebSocketHandler.sendMessageToSubscribers(device.getDeviceSn(), MessageType.STATUS.getType(), dockOsd); |
| | | |
| | | sendMessageService.sendBatch(webSessions, wsMessage); |
| | | this.pushTelemetryData(data, device.getDeviceSn()); |
| | | } |
| | |
| | | package com.ard.sample.manage.service.impl; |
| | | |
| | | import com.ard.common.core.redis.RedisConst; |
| | | import com.ard.common.core.redis.RedisOpsUtils; |
| | | import com.ard.sample.component.mqtt.model.CommonTopicReceiver; |
| | | import com.ard.sample.component.websocket.config.ConcurrentWebSocketSession; |
| | | import com.ard.sample.component.websocket.enums.MessageType; |
| | |
| | | //只要状态 "0":"作业准备中","1":"飞行作业中","5":"任务空闲" |
| | | if(code == 0 || code == 1 || code == 5) { |
| | | _deviceService.updateWorkType(deviceSn,code); |
| | | RedisOpsUtils.setWithExpire(RedisConst.OSD_STATUS_PREFIX + deviceSn, code, RedisConst.DEVICE_ALIVE_SECOND); |
| | | } |
| | | } |
| | | } |
| | |
| | | if(inDock.equals(1L) && deviceOnlineStatus.equals(0L)) |
| | | {// 飞行器在舱内且关机状态为6 |
| | | _deviceService.updateWorkType(childDeviceSn,6L); |
| | | RedisOpsUtils.setWithExpire(RedisConst.OSD_STATUS_PREFIX + device.getChildDeviceSn(), 6, RedisConst.DEVICE_ALIVE_SECOND); |
| | | } |
| | | } |
| | | receiver.setType(MessageType.STATUS.getType()+"-network"); |
| | |
| | | package com.ard.sample.manage.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.ard.common.core.redis.RedisConst; |
| | | import com.ard.common.core.redis.RedisOpsUtils; |
| | | import com.ard.sample.component.websocket.enums.MessageType; |
| | | import com.ard.sample.component.websocket.handler.DockWebSocketHandler; |
| | | import com.ard.sample.manage.model.dto.DeviceDTO; |
| | | import com.ard.sample.manage.service.IDeviceService; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | public class DockOnlineServiceImpl { |
| | | |
| | | @Autowired |
| | | private IDeviceService _deviceService; |
| | | |
| | | @Autowired |
| | | private DockWebSocketHandler _dockWebSocketHandler; |
| | | |
| | | @Autowired |
| | | protected ObjectMapper _objectMapper; |
| | | |
| | | /** |
| | | * 定时任务,每5秒取一次机场设备的在线状态,如已离线更新状态 |
| | |
| | | // 更新机场设备状态:"3"-"设备已离线", |
| | | if(!_deviceService.checkDeviceOnline(device.getDeviceSn())){ |
| | | _deviceService.updateWorkType(device.getDeviceSn(), 3L); |
| | | RedisOpsUtils.setWithExpire(RedisConst.OSD_STATUS_PREFIX + device.getDeviceSn(), 3, RedisConst.DEVICE_ALIVE_SECOND); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 定时任务,每5秒读一次redis的机场设备的状态,并发送到websocket客户端 |
| | | * */ |
| | | @Scheduled(cron = "0/5 * * * * ?") |
| | | public void sendDeviceStatus(){ |
| | | // 发送设备状态到websocket客户端 |
| | | List<DeviceDTO> devicesList = _deviceService.getDevicesTopoForWeb(); |
| | | for (DeviceDTO device : devicesList) { |
| | | String dockStatus = ""; |
| | | String droneStatus = ""; |
| | | // 判断redis中是否存在设备状态数据 |
| | | if(RedisOpsUtils.checkExist(RedisConst.OSD_STATUS_PREFIX + device.getDeviceSn())){ |
| | | dockStatus = String.valueOf(RedisOpsUtils.get(RedisConst.OSD_STATUS_PREFIX + device.getDeviceSn())); |
| | | } |
| | | if(RedisOpsUtils.checkExist(RedisConst.OSD_STATUS_PREFIX + device.getChildDeviceSn())){ |
| | | droneStatus = String.valueOf(RedisOpsUtils.get(RedisConst.OSD_STATUS_PREFIX + device.getChildDeviceSn()));; |
| | | } |
| | | String json = getJSONString(device.getDeviceSn(), dockStatus, device.getChildDeviceSn(), droneStatus); |
| | | |
| | | // 发送给websocket客户端 |
| | | _dockWebSocketHandler.sendMessageToSubscribers(device.getDeviceSn(), MessageType.STATE.getType(), json); |
| | | } |
| | | } |
| | | |
| | | public String getJSONString(String dockSn, String dockStatus, String droneSn, String droneStatus){ |
| | | Map<String, Object> json = new HashMap<>(); |
| | | json.put("code", 200); |
| | | json.put("type", MessageType.STATE.getType()); |
| | | json.put("message", "success"); |
| | | |
| | | Map<String, String> data = new HashMap<>(); |
| | | data.put("docksn", dockSn); |
| | | data.put("state", dockStatus); |
| | | |
| | | Map<String, String> data1 = new HashMap<>(); |
| | | data1.put("droneSn", droneSn); |
| | | data1.put("state", droneStatus); |
| | | |
| | | List<Map<String, String>> lstMap = new ArrayList<>(); |
| | | lstMap.add(data); |
| | | lstMap.add(data1); |
| | | |
| | | json.put("data", lstMap); |
| | | |
| | | // 将Map转换为JSON字符串 |
| | | String jsonString = ""; |
| | | try{ |
| | | jsonString = _objectMapper.writeValueAsString(json); |
| | | } |
| | | catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return jsonString; |
| | | } |
| | | } |