# 上位机心跳 以及定位信息发送部分
This commit is contained in:
@@ -2,6 +2,7 @@ package com.maibu.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -14,12 +15,19 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.maibu.constant.CommandConstant;
|
||||
import com.maibu.constant.Constant;
|
||||
import com.maibu.core.business.Device;
|
||||
import com.maibu.core.enums.*;
|
||||
import com.maibu.core.host.HeartBeatDTO;
|
||||
import com.maibu.dto.DeviceStatusChangeDTO;
|
||||
import com.maibu.memory.DeviceSessionManager;
|
||||
import com.maibu.utils.CommandUtils;
|
||||
import com.maibu.utils.json.JsonUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.Constants;
|
||||
@@ -37,9 +45,6 @@ import com.maibu.core.business.ErrorIdentificationStandard;
|
||||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||||
import com.maibu.core.business.device.NettyDevice;
|
||||
import com.maibu.core.business.inter.WebsocketMesDispather;
|
||||
import com.maibu.core.enums.CompareEnum;
|
||||
import com.maibu.core.enums.ErrorSource;
|
||||
import com.maibu.core.enums.RespondCode;
|
||||
import com.maibu.core.redis.RedisCache;
|
||||
import com.maibu.dto.DeviceErrorPushDTO;
|
||||
import com.maibu.mapper.ErrorIdentificationStandardMapper;
|
||||
@@ -74,6 +79,9 @@ public class DeviceThreadService {
|
||||
@Autowired
|
||||
private AlertPushService alertPushService;
|
||||
|
||||
@Autowired
|
||||
private IDeviceService deviceService;
|
||||
|
||||
@Value("${mqtt.client-id}")
|
||||
private String mqttClientId;
|
||||
|
||||
@@ -201,7 +209,7 @@ public class DeviceThreadService {
|
||||
log.info("推送错误信息 device:{},content:{}", deviceId, dto);
|
||||
});
|
||||
}
|
||||
} catch (MqttException e) {
|
||||
} catch (Exception e) {
|
||||
log.error("推送错误信息失败 device:{}error:{}", deviceId, e.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -270,19 +278,58 @@ public class DeviceThreadService {
|
||||
List<HeartBeatDTO> heartBeatDTOS = GlobalMemory.customMqttDeviceMonitor.getAllHeartBeat();
|
||||
if (!CollectionUtils.isEmpty(heartBeatDTOS)) {
|
||||
heartBeatDTOS.forEach(x -> {
|
||||
String deviceId = x.getSn();
|
||||
Long lastActiveTime = x.getSysTimestamp();
|
||||
NettyDevice device = sessionManager.getDevice(x.getSn());
|
||||
NettyDevice nettyDevice = sessionManager.getDevice(x.getSn());
|
||||
if (nowTime - lastActiveTime >= Constant.hostOfflineInterval) {
|
||||
//超时了判断离线
|
||||
if (device != null) {
|
||||
device.status(0);
|
||||
if (nettyDevice != null) {
|
||||
nettyDevice.status(0);
|
||||
//todo 推送设备离线
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
|
||||
|
||||
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());
|
||||
deviceService.insertDeviceBySelf(device);
|
||||
} else {
|
||||
device.setStatus(3);
|
||||
device.setOnlineStatus(DeviceStatus.online.getType());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
deviceService.updateDeviceBySelf(device);
|
||||
}
|
||||
// 推送json 找到设备绑定的主机进行推送
|
||||
// List<NettyDevice> 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -290,8 +337,6 @@ public class DeviceThreadService {
|
||||
log.error("执行监测心跳线程错误:{}", e.getMessage());
|
||||
}
|
||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user