判断设备离线时发送状态查询确定设备真实状态,确定离线再设置为离线

master
lin 2025-03-13 11:23:49 +08:00
parent 108cdf1a13
commit 5539f52d13
3 changed files with 35 additions and 17 deletions

View File

@ -101,7 +101,7 @@ public interface IDeviceService {
* *
* @param device * @param device
*/ */
void checkDeviceStatus(Device device); Boolean getDeviceStatus(Device device);
/** /**
* IP * IP

View File

@ -40,10 +40,12 @@ import org.springframework.util.Assert;
import javax.sip.InvalidArgumentException; import javax.sip.InvalidArgumentException;
import javax.sip.SipException; import javax.sip.SipException;
import javax.validation.constraints.NotNull;
import java.text.ParseException; import java.text.ParseException;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -199,12 +201,17 @@ public class DeviceServiceImpl implements IDeviceService {
log.warn("[设备不存在] device{}", deviceId); log.warn("[设备不存在] device{}", deviceId);
return; return;
} }
// TODO 主动查询设备状态
// 主动查询设备状态
Boolean deviceStatus = getDeviceStatus(device);
if (deviceStatus != null && deviceStatus) {
log.info("[设备离线] 主动探测发现设备在线,暂不处理 device{}", deviceId);
online(device, null);
return;
}
log.info("[设备离线] {}, device{} 心跳间隔: {},心跳超时次数: {} 上次心跳时间:{} 上次注册时间: {}", reason, deviceId, log.info("[设备离线] {}, device{} 心跳间隔: {},心跳超时次数: {} 上次心跳时间:{} 上次注册时间: {}", reason, deviceId,
device.getHeartBeatInterval(), device.getHeartBeatCount(), device.getKeepaliveTime(), device.getRegisterTime()); device.getHeartBeatInterval(), device.getHeartBeatCount(), device.getKeepaliveTime(), device.getRegisterTime());
String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + deviceId; String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + deviceId;
dynamicTask.stop(registerExpireTaskKey); dynamicTask.stop(registerExpireTaskKey);
if (device.isOnLine()) { if (device.isOnLine()) {
if (userSetting.getDeviceStatusNotify()) { if (userSetting.getDeviceStatusNotify()) {
@ -411,15 +418,23 @@ public class DeviceServiceImpl implements IDeviceService {
} }
@Override @Override
public void checkDeviceStatus(Device device) { public Boolean getDeviceStatus(@NotNull Device device) {
if (device == null || !device.isOnLine()) { SynchronousQueue<String> queue = new SynchronousQueue<>();
return;
}
try { try {
sipCommander.deviceStatusQuery(device, null); sipCommander.deviceStatusQuery(device, ((code, msg, data) -> {
} catch (InvalidArgumentException | SipException | ParseException e) { queue.offer(msg);
}));
String data = queue.poll(10, TimeUnit.SECONDS);
if (data != null && "ONLINE".equalsIgnoreCase(data.trim())) {
return Boolean.TRUE;
}else {
return Boolean.FALSE;
}
} catch (InvalidArgumentException | SipException | ParseException | InterruptedException e) {
log.error("[命令发送失败] 设备状态查询: {}", e.getMessage()); log.error("[命令发送失败] 设备状态查询: {}", e.getMessage());
} }
return null;
} }
@Override @Override
@ -843,9 +858,17 @@ public class DeviceServiceImpl implements IDeviceService {
callback.run(result.getCode(), result.getMsg(), result.getData()); callback.run(result.getCode(), result.getMsg(), result.getData());
return; return;
} }
try { try {
sipCommander.deviceStatusQuery(device, callback); sipCommander.deviceStatusQuery(device, (code, msg, data) -> {
if ("ONLINE".equalsIgnoreCase(data.trim())) {
online(device, null);
}else {
offline(device.getDeviceId(), "设备状态查询结果:" + data.trim());
}
if (callback != null) {
callback.run(code, msg, data);
}
});
} catch (InvalidArgumentException | SipException | ParseException e) { } catch (InvalidArgumentException | SipException | ParseException e) {
log.error("[命令发送失败] 获取设备状态: {}", e.getMessage()); log.error("[命令发送失败] 获取设备状态: {}", e.getMessage());
callback.run(ErrorCode.ERROR100.getCode(), "命令发送: " + e.getMessage(), null); callback.run(ErrorCode.ERROR100.getCode(), "命令发送: " + e.getMessage(), null);

View File

@ -58,11 +58,6 @@ public class DeviceStatusResponseMessageHandler extends SIPRequestProcessorParen
log.debug(json.toJSONString()); log.debug(json.toJSONString());
} }
String text = onlineElement.getText(); String text = onlineElement.getText();
if ("ONLINE".equalsIgnoreCase(text.trim())) {
deviceService.online(device, null);
}else {
deviceService.offline(device.getDeviceId(), "设备状态查询结果:" + text.trim());
}
responseMessageHandler.handMessageEvent(element, text); responseMessageHandler.handMessageEvent(element, text);
} }