diff --git a/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java b/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java index c620d22..e0efb01 100644 --- a/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java +++ b/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java @@ -8,7 +8,7 @@ public class DeviceMapPushDTO { private double lat; private double lng; private double y; //yaw - private double f; //fix_status + private String f; //fix_status private double a; //altitude private String s;//status 运行状态 private Long t; //taskId 任务id diff --git a/maibu-common/src/main/java/com/maibu/enums/DeviceStatusEnum.java b/maibu-common/src/main/java/com/maibu/enums/DeviceStatusEnum.java index db8bfc8..78c86e2 100644 --- a/maibu-common/src/main/java/com/maibu/enums/DeviceStatusEnum.java +++ b/maibu-common/src/main/java/com/maibu/enums/DeviceStatusEnum.java @@ -11,12 +11,12 @@ import lombok.Getter; @Getter @AllArgsConstructor public enum DeviceStatusEnum { - - offline("0", "离线"), - idle("1", "空闲中"), - working("2", "工作中"), - charging("3", "充电中"), - error("4", "故障"); + + working("1", "工作中"), + idle("2", "空闲中"), + offline("3", "离线"), + charging("4", "充电中"), + error("5", "故障"); String code; String message; diff --git a/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java b/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java index 2efa4c0..cf59cf7 100644 --- a/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java +++ b/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java @@ -229,7 +229,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler { dto.setId(deviceId); // dto.setB(); // dto.setHa(); - dto.setF(Double.parseDouble(fix_status.getValue())); + dto.setF(fix_status.getValue()); dto.setS(status.getValue()); dto.setLat(Double.parseDouble(lat.getValue())); dto.setLng(Double.parseDouble(lng.getValue())); diff --git a/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java b/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java index 99fb903..11d63e0 100644 --- a/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java +++ b/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java @@ -284,9 +284,17 @@ public class DeviceThreadService { List heartBeatDTOS = GlobalMemory.customMqttDeviceMonitor.getAllHeartBeat(); if (!CollectionUtils.isEmpty(heartBeatDTOS)) { heartBeatDTOS.forEach(x -> { + if (x == null) { + return; + } String deviceId = x.getSn(); Long lastActiveTime = x.getSysTimestamp(); NettyDevice nettyDevice = sessionManager.getDevice(x.getSn()); + + if (lastActiveTime == null) { + log.error("设备心跳时间为空,deviceId={}", deviceId); + return; + } if (nowTime - lastActiveTime >= Constant.hostOfflineInterval) { // 超时了判断离线 if (nettyDevice != null) { @@ -342,56 +350,45 @@ public class DeviceThreadService { } } else { + Device device = deviceService.selectDeviceBySerialNumber(deviceId); + if (device == null) { + device = new Device(); + device.setStatus(3); + device.setOnlineStatus(DeviceStatus.online.getType()); + device.setSerialNumber(deviceId); + device.setDeviceName(deviceId); + device.setProductId(-1L); + device.setTenantId(-1L); + device.setFirmwareVersion(BigDecimal.valueOf(1.0)); + device.setProductName("割草机产品MC700"); + device.setCreateTime(LocalDateTime.now()); + device.setUpdateTime(LocalDateTime.now()); + device.setHasHost(true); + deviceService.insertDeviceBySelf(device); + } else { + device.setStatus(3); + device.setOnlineStatus(DeviceStatus.online.getType()); + device.setUpdateTime(LocalDateTime.now()); + device.setHasHost(true); + deviceService.updateDeviceBySelf(device); + } if (nettyDevice == null) { sessionManager.registerSlaveDevice(deviceId, ConnectorStatus.ACTIVE, null); - Device device = deviceService.selectDeviceBySerialNumber(deviceId); - if (device == null) { - device = new Device(); - device.setStatus(3); - device.setOnlineStatus(DeviceStatus.online.getType()); - device.setSerialNumber(deviceId); - device.setDeviceName(deviceId); - device.setProductId(-1L); - device.setTenantId(-1L); - device.setFirmwareVersion(BigDecimal.valueOf(1.0)); - device.setProductName("割草机产品MC700"); - device.setCreateTime(LocalDateTime.now()); - device.setUpdateTime(LocalDateTime.now()); - device.setHasHost(true); - deviceService.insertDeviceBySelf(device); - } else { - device.setStatus(3); - device.setOnlineStatus(DeviceStatus.online.getType()); - device.setUpdateTime(LocalDateTime.now()); - device.setHasHost(true); - deviceService.updateDeviceBySelf(device); + NettyDevice slaveDevice = sessionManager.getDevice(deviceId); + if (slaveDevice != null) { + slaveDevice.setOnlineStatus(1); + slaveDevice.setDevice(device); } - // 推送json 找到设备绑定的主机进行推送 - // List controlMasters = - // sessionManager.getAllSlaveControl(device.getSerialNumber()); - // if (!CollectionUtils.isEmpty(controlMasters)) { - // controlMasters.forEach(x -> { - // DeviceStatusChangeDTO changeDTO = new DeviceStatusChangeDTO(); - // changeDTO.setDeviceId(data); - // changeDTO.setStatus(DeviceStatus.online.getCode()); - // changeDTO.setEvent(RespondCode.device_status_changed); - // ByteBuf buf = ctx.alloc().buffer(); - // CommandUtils.buildCommand(buf, changeDTO, CommandConstant.interaction); - // String content = buf.toString(CharsetUtil.UTF_8); - // logger.info("推送下位机登录状态消息:{}", content); - // if (x.getChannel() != null && x.getChannel().isActive()) { - // x.getChannel().writeAndFlush(buf); - // } - // }); } else { // todo 推送设备上线 nettyDevice.status(1); + nettyDevice.setDevice(device); } } }); } } catch (Exception e) { - log.error("执行监测心跳线程错误:{}", e.getMessage()); + log.error("执行监测心跳线程错误", e); } }, 0, 500, TimeUnit.MILLISECONDS); }