|
@@ -41,6 +41,7 @@ public abstract class RefreshableOfflineCheckTask implements EnvironmentAware, A
|
|
|
|
|
|
|
|
private final Set<String> offlineDeviceSet = ConcurrentHashMap.newKeySet();
|
|
private final Set<String> offlineDeviceSet = ConcurrentHashMap.newKeySet();
|
|
|
private final Set<String> onlineDeviceSet = ConcurrentHashMap.newKeySet();
|
|
private final Set<String> onlineDeviceSet = ConcurrentHashMap.newKeySet();
|
|
|
|
|
+ private final Set<String> dbOfflineDeviceSet = ConcurrentHashMap.newKeySet();
|
|
|
private final Object[] deviceLocks = new Object[128];
|
|
private final Object[] deviceLocks = new Object[128];
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
@@ -139,36 +140,44 @@ public abstract class RefreshableOfflineCheckTask implements EnvironmentAware, A
|
|
|
int timeoutMinutes = effectiveOfflineTimeoutMinutes();
|
|
int timeoutMinutes = effectiveOfflineTimeoutMinutes();
|
|
|
log.info("离线巡检开始: collector={}, timeoutMinutes={}, memoryDevices={}", collector(), timeoutMinutes, deviceLastReceiveTimeMap.size());
|
|
log.info("离线巡检开始: collector={}, timeoutMinutes={}, memoryDevices={}", collector(), timeoutMinutes, deviceLastReceiveTimeMap.size());
|
|
|
if (lastReceiptService != null) {
|
|
if (lastReceiptService != null) {
|
|
|
- lastReceiptService.restore(collector(), deviceLastReceiveTimeMap);
|
|
|
|
|
|
|
+ lastReceiptService.restore(collector(), deviceLastReceiveTimeMap, dbOfflineDeviceSet);
|
|
|
}
|
|
}
|
|
|
Date now = new Date();
|
|
Date now = new Date();
|
|
|
long timeoutMs = (long) timeoutMinutes * 60 * 1000;
|
|
long timeoutMs = (long) timeoutMinutes * 60 * 1000;
|
|
|
for (Map.Entry<String, Date> entry : deviceLastReceiveTimeMap.entrySet()) {
|
|
for (Map.Entry<String, Date> entry : deviceLastReceiveTimeMap.entrySet()) {
|
|
|
- synchronized (deviceLock(entry.getKey())) {
|
|
|
|
|
|
|
+ String deviceCode = entry.getKey();
|
|
|
|
|
+ synchronized (deviceLock(deviceCode)) {
|
|
|
// 与在线恢复串行,且重新读取时间,避免旧快照把刚收到上报的设备置离线。
|
|
// 与在线恢复串行,且重新读取时间,避免旧快照把刚收到上报的设备置离线。
|
|
|
- Date latest = deviceLastReceiveTimeMap.get(entry.getKey());
|
|
|
|
|
|
|
+ Date latest = deviceLastReceiveTimeMap.get(deviceCode);
|
|
|
if (latest == null) {
|
|
if (latest == null) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
+ boolean dbOffline = dbOfflineDeviceSet.contains(deviceCode);
|
|
|
if (now.getTime() - latest.getTime() <= timeoutMs) {
|
|
if (now.getTime() - latest.getTime() <= timeoutMs) {
|
|
|
- // 离线设备若最后采集时间仍在超时窗口内(如超时参数调大),自动恢复在线。
|
|
|
|
|
- if (offlineDeviceSet.remove(entry.getKey())) {
|
|
|
|
|
- if (updateDeviceOnlineStatus(entry.getKey(), 1)) {
|
|
|
|
|
- onlineDeviceSet.add(entry.getKey());
|
|
|
|
|
- log.info("设备 {} 已恢复在线(最后采集时间在{}分钟超时窗口内)", entry.getKey(), timeoutMinutes);
|
|
|
|
|
- } else {
|
|
|
|
|
- offlineDeviceSet.add(entry.getKey());
|
|
|
|
|
|
|
+ // 以数据库状态为准:离线设备最后采集时间仍在窗口内时自动恢复在线。
|
|
|
|
|
+ if (dbOffline || offlineDeviceSet.contains(deviceCode)) {
|
|
|
|
|
+ if (updateDeviceOnlineStatus(deviceCode, 1)) {
|
|
|
|
|
+ offlineDeviceSet.remove(deviceCode);
|
|
|
|
|
+ dbOfflineDeviceSet.remove(deviceCode);
|
|
|
|
|
+ onlineDeviceSet.add(deviceCode);
|
|
|
|
|
+ log.info("设备 {} 已恢复在线(最后采集时间在{}分钟超时窗口内)", deviceCode, timeoutMinutes);
|
|
|
}
|
|
}
|
|
|
|
|
+ } else {
|
|
|
|
|
+ offlineDeviceSet.remove(deviceCode);
|
|
|
}
|
|
}
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
- if (offlineDeviceSet.add(entry.getKey())) {
|
|
|
|
|
- if (!updateDeviceOnlineStatus(entry.getKey(), 0)) {
|
|
|
|
|
- offlineDeviceSet.remove(entry.getKey());
|
|
|
|
|
|
|
+ if (dbOffline) {
|
|
|
|
|
+ offlineDeviceSet.add(deviceCode);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (offlineDeviceSet.add(deviceCode)) {
|
|
|
|
|
+ if (!updateDeviceOnlineStatus(deviceCode, 0)) {
|
|
|
|
|
+ offlineDeviceSet.remove(deviceCode);
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
- onlineDeviceSet.remove(entry.getKey());
|
|
|
|
|
- log.info("设备 {} 已离线(超过{}分钟未收到数据)", entry.getKey(), timeoutMinutes);
|
|
|
|
|
|
|
+ onlineDeviceSet.remove(deviceCode);
|
|
|
|
|
+ log.info("设备 {} 已离线(超过{}分钟未收到数据)", deviceCode, timeoutMinutes);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|