#update
This commit is contained in:
@@ -4,8 +4,6 @@ import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -29,7 +27,6 @@ import com.maibu.constant.NettyCacheKey;
|
||||
import com.maibu.core.business.Device;
|
||||
import com.maibu.core.business.DeviceRunStatistics;
|
||||
import com.maibu.core.business.DeviceRunningStatusHistory;
|
||||
import com.maibu.core.business.DeviceStatusRecordDTO;
|
||||
import com.maibu.core.business.ErrorIdentificationStandard;
|
||||
import com.maibu.core.business.RegionConsecutiveTaskRecord;
|
||||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||||
@@ -46,15 +43,12 @@ import com.maibu.core.enums.ActionType;
|
||||
import com.maibu.core.enums.CompareEnum;
|
||||
import com.maibu.core.enums.ConnectorStatus;
|
||||
import com.maibu.core.enums.DeviceStatus;
|
||||
import com.maibu.core.enums.ErrorSource;
|
||||
import com.maibu.core.enums.MesType;
|
||||
import com.maibu.core.enums.RespondCode;
|
||||
import com.maibu.core.enums.RuleType;
|
||||
import com.maibu.core.host.HeartBeatDTO;
|
||||
import com.maibu.core.host.LocationMessage;
|
||||
import com.maibu.core.host.VehicleMessage;
|
||||
import com.maibu.core.redis.RedisCache;
|
||||
import com.maibu.dto.DeviceErrorPushDTO;
|
||||
import com.maibu.mapper.DeviceRunStatisticsMapper;
|
||||
import com.maibu.mapper.ErrorIdentificationStandardMapper;
|
||||
import com.maibu.mapper.HostLocationMapper;
|
||||
@@ -120,48 +114,14 @@ public class DeviceThreadService {
|
||||
// 有还需要更新接受故障方式。
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
String key = NettyCacheKey.deviceRunningStatusKey;
|
||||
Collection<String> keys = redisCache.getListKeyByPrefix(key);
|
||||
if (!CollectionUtils.isEmpty(keys)) {
|
||||
List<DeviceErrorPushDTO> pushList = new ArrayList<>();
|
||||
keys.forEach(k -> {
|
||||
DeviceStatusRecordDTO recordDTO = redisCache.getCacheObject(k);
|
||||
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
|
||||
recordDTO.getHistories()
|
||||
.sort(Comparator.comparing(DeviceRunningStatusHistory::getCreateTime).reversed());
|
||||
DeviceRunningStatusHistory history = recordDTO.getHistories().get(0);
|
||||
|
||||
String[] parts = k.split(":");
|
||||
String deviceId = parts[1];
|
||||
NettyDevice slaveDevice = sessionManager.getDevice(deviceId);
|
||||
if (slaveDevice != null && slaveDevice.getOnlineStatus() == 1) {
|
||||
Long siteId = slaveDevice.getDevice().getSiteId();
|
||||
Long orgId = slaveDevice.getDevice().getOrgId();
|
||||
log.debug("compare siteId:{},orgId:{}", siteId, orgId);
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if (siteMemory != null) {
|
||||
List<AlarmMessage> compared = compareErrorStandard(siteId, orgId, history,
|
||||
siteMemory.standards.get(ErrorSource.MOWER));
|
||||
DeviceErrorPushDTO pushDTO = new DeviceErrorPushDTO();
|
||||
pushDTO.setDeviceId(deviceId);
|
||||
pushDTO.setTime(System.currentTimeMillis());
|
||||
pushDTO.setDetails(compared);
|
||||
pushDTO.setEvent(RespondCode.device_error_push);
|
||||
pushList.add(pushDTO);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// 按实际deviceId分组,推送错误信息
|
||||
Map<String, List<DeviceErrorPushDTO>> pushMap = pushList.stream()
|
||||
.collect(java.util.stream.Collectors.groupingBy(DeviceErrorPushDTO::getDeviceId));
|
||||
pushMap.forEach((deviceId, dtos) -> {
|
||||
// pushErrorMessage(deviceId, JSON.toJSONString(dtos));
|
||||
GlobalMemory.getAllSiteMemory().forEach(siteMemory -> {
|
||||
siteMemory.alarmMap.forEach((deviceId, dtos) -> {
|
||||
// todo 判断错误级别,是否需要推送邮件 或者钉钉。
|
||||
// 找到这个设备的场站 添加场站是否开启关闭 钉钉或者邮件推送,推送邮件 添加人员是否订阅邮件,钉钉是加入钉钉群
|
||||
alertPushService.messagePushHandler(deviceId, dtos);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("执行监测错误线程错误", e);
|
||||
}
|
||||
@@ -308,32 +268,46 @@ public class DeviceThreadService {
|
||||
}
|
||||
String deviceId = x.getSn();
|
||||
Long lastActiveTime = x.getSysTimestamp();
|
||||
NettyDevice nettyDevice = sessionManager.getDevice(x.getSn());
|
||||
|
||||
if (lastActiveTime == null) {
|
||||
log.error("设备心跳时间为空,deviceId={}", deviceId);
|
||||
return;
|
||||
}
|
||||
NettyDevice nettyDevice = sessionManager.getDevice(deviceId);
|
||||
Device device = null;
|
||||
if (nettyDevice == null) {
|
||||
sessionManager.registerSlaveDevice(deviceId, ConnectorStatus.ACTIVE, null);
|
||||
nettyDevice = sessionManager.getDevice(deviceId);
|
||||
if (nettyDevice != null) {
|
||||
device = loadDevice(device, deviceId);
|
||||
nettyDevice.setDevice(device);
|
||||
}
|
||||
} else {
|
||||
device = loadDevice(nettyDevice.getDevice(), deviceId);
|
||||
nettyDevice.setDevice(device);
|
||||
}
|
||||
if (nettyDevice == null || device == null) {
|
||||
return;
|
||||
}
|
||||
Long orgId = nettyDevice.getDevice().getOrgId();
|
||||
Long siteId = nettyDevice.getDevice().getSiteId();
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
|
||||
if (nowTime - lastActiveTime >= Constant.hostOfflineInterval) {
|
||||
// 超时了判断离线
|
||||
if (nettyDevice != null && nettyDevice.getOnlineStatus() != null
|
||||
&& nettyDevice.getOnlineStatus() == 1) {
|
||||
|
||||
Long orgId = nettyDevice.getDevice().getOrgId();
|
||||
Long siteId = nettyDevice.getDevice().getSiteId();
|
||||
|
||||
try {
|
||||
// 标记离线
|
||||
nettyDevice.status(DeviceStatus.offline.getType());
|
||||
updatelatestLocation(deviceId);
|
||||
Device device = deviceService.selectDeviceBySerialNumber(deviceId);
|
||||
if (device != null) {
|
||||
device.setOnlineStatus(DeviceStatus.offline.getType());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
deviceService.updateDeviceBySelf(device);
|
||||
}
|
||||
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if (siteMemory != null) {
|
||||
updateTotalArea(nettyDevice, siteMemory, deviceId);
|
||||
}
|
||||
@@ -347,7 +321,6 @@ public class DeviceThreadService {
|
||||
} finally {
|
||||
// 必须执行:清理实时缓存
|
||||
try {
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if (siteMemory != null) {
|
||||
siteMemory.removeMapPushDTO(deviceId);
|
||||
siteMemory.removeVehicleMessage(deviceId);
|
||||
@@ -358,48 +331,23 @@ 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("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);
|
||||
NettyDevice slaveDevice = sessionManager.getDevice(deviceId);
|
||||
if (slaveDevice != null) {
|
||||
slaveDevice.setOnlineStatus(1);
|
||||
slaveDevice.setDevice(device);
|
||||
statusChangePush(deviceId, device.getSiteId(), 1);
|
||||
}
|
||||
} else {
|
||||
|
||||
Integer lastOnlineStatus = nettyDevice.getOnlineStatus();
|
||||
if (lastOnlineStatus == null || lastOnlineStatus == 0) {
|
||||
statusChangePush(deviceId, device.getSiteId(), 1);
|
||||
Integer lastOnlineStatus = nettyDevice.getOnlineStatus();
|
||||
// 判断从离线到上线
|
||||
if (lastOnlineStatus == null || lastOnlineStatus == 0) {
|
||||
statusChangePush(deviceId, nettyDevice.getDevice().getSiteId(), 1);
|
||||
if (device != null) {
|
||||
device.setStatus(3);
|
||||
device.setOnlineStatus(DeviceStatus.online.getType());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
deviceService.updateDeviceBySelf(device);
|
||||
}
|
||||
// todo 推送设备上线
|
||||
nettyDevice.status(1);
|
||||
nettyDevice.setDevice(device);
|
||||
nettyDevice.setConnectorStatus(ConnectorStatus.ACTIVE);
|
||||
|
||||
}
|
||||
// todo 推送设备上线
|
||||
nettyDevice.status(1);
|
||||
nettyDevice.setDevice(device);
|
||||
nettyDevice.setConnectorStatus(ConnectorStatus.ACTIVE);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -409,6 +357,29 @@ public class DeviceThreadService {
|
||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public Device loadDevice(Device device, String deviceId) {
|
||||
if (device == null) {
|
||||
device = deviceService.selectDeviceBySerialNumber(deviceId);
|
||||
}
|
||||
// 新增设备s
|
||||
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("1.0");
|
||||
device.setProductName("割草机产品MC700");
|
||||
device.setCreateTime(LocalDateTime.now());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
device.setHasHost(true);
|
||||
deviceService.insertDeviceBySelf(device);
|
||||
}
|
||||
return device;
|
||||
}
|
||||
|
||||
public void updatelatestLocation(String deviceId) {
|
||||
// todo 推送设备离线
|
||||
String key = NettyCacheKey.latestPositonKey + deviceId;
|
||||
@@ -447,7 +418,7 @@ public class DeviceThreadService {
|
||||
} else {
|
||||
long time = System.currentTimeMillis()
|
||||
- nettyDevice.getLatestLoginTime();
|
||||
deviceRunStatistics.setTime(time / (1000 * 60)); // 累加时长,单位为分钟
|
||||
// deviceRunStatistics.setTime(time / (1000 * 60)); // 累加时长,单位为分钟
|
||||
}
|
||||
deviceRunStatisticsMapper.saveOrUpdate(deviceRunStatistics);
|
||||
}
|
||||
@@ -548,6 +519,8 @@ public class DeviceThreadService {
|
||||
String deviceId = x.getDeviceId();
|
||||
String value = classFieldFinding(key, x);
|
||||
boolean result = CompareUtils.compare(compareType, value, compareValue);
|
||||
log.debug("conditions:{},compareValue:{},value:{},result:{}", conditions, compareValue, value,
|
||||
result);
|
||||
if (result) {
|
||||
ActionConfig aConfig = rule.getActionConfig();
|
||||
if (aConfig != null && aConfig.getActionType() != null) {
|
||||
|
||||
Reference in New Issue
Block a user