diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceConfig.java b/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceConfig.java index c469dabc..17640b39 100755 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceConfig.java @@ -16,6 +16,7 @@ import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder; import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage; import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander; import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; +import com.genersoft.iot.vmp.vmanager.bean.WVPResult; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.security.SecurityRequirement; @@ -102,24 +103,20 @@ public class DeviceConfig { @Parameter(name = "channelId", description = "通道国标编号", required = true) @Parameter(name = "configType", description = "配置类型") @GetMapping("/query/{deviceId}/{configType}") - public DeferredResult configDownloadApi(@PathVariable String deviceId, - @PathVariable String configType, - @RequestParam(required = false) String channelId) { + public DeferredResult> configDownloadApi(@PathVariable String deviceId, + @PathVariable String configType, + @RequestParam(required = false) String channelId) { if (log.isDebugEnabled()) { log.debug("设备状态查询API调用"); } Device device = deviceService.getDeviceByDeviceId(deviceId); Assert.notNull(device, "设备不存在"); - DeferredResult result = deviceService.deviceConfigQuery(device, channelId, configType); + DeferredResult> result = deviceService.deviceConfigQuery(device, channelId, configType); result.onTimeout(() -> { - log.warn("获取设备配置超时"); - JSONObject json = new JSONObject(); - json.put("DeviceID", device.getDeviceId()); - json.put("Status", "Timeout"); - json.put("Description", "操作超时"); - result.setResult(json.toString()); + log.warn("[获取设备配置] 超时, {}", device.getDeviceId()); + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "超时")); }); return result; } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceControl.java b/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceControl.java index a9daa543..2ea58b53 100755 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceControl.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceControl.java @@ -78,17 +78,17 @@ public class DeviceControl { @Parameter(name = "channelId", description = "通道国标编号", required = true) @Parameter(name = "recordCmdStr", description = "命令, 可选值:Record(手动录像),StopRecord(停止手动录像)", required = true) @GetMapping("/record/{deviceId}/{recordCmdStr}") - public DeferredResult recordApi(@PathVariable String deviceId, + public DeferredResult> recordApi(@PathVariable String deviceId, @PathVariable String recordCmdStr, String channelId) { if (log.isDebugEnabled()) { log.debug("开始/停止录像API调用"); } Device device = deviceService.getDeviceByDeviceId(deviceId); Assert.notNull(device, "设备不存在"); - DeferredResult result = deviceService.record(device, channelId, recordCmdStr); + DeferredResult> result = deviceService.record(device, channelId, recordCmdStr); result.onTimeout(() -> { log.warn("[开始/停止录像] 操作超时, 设备未返回应答指令, {}", deviceId); - result.setResult("操作超时, 设备未应答"); + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); }); return result; } @@ -156,23 +156,14 @@ public class DeviceControl { @Parameter(name = "deviceId", description = "设备国标编号", required = true) @Parameter(name = "channelId", description = "通道国标编号") @GetMapping("/i_frame/{deviceId}") - public JSONObject iFrame(@PathVariable String deviceId, + public void iFrame(@PathVariable String deviceId, @RequestParam(required = false) String channelId) { if (log.isDebugEnabled()) { log.debug("强制关键帧API调用"); } Device device = deviceService.getDeviceByDeviceId(deviceId); - try { - cmder.iFrameCmd(device, channelId); - } catch (InvalidArgumentException | SipException | ParseException e) { - log.error("[命令发送失败] 强制关键帧: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); - } - JSONObject json = new JSONObject(); - json.put("DeviceID", deviceId); - json.put("ChannelID", channelId); - json.put("Result", "OK"); - return json; + Assert.notNull(device, "设备不存在"); + deviceService.iFrame(device, channelId); } /** @@ -195,34 +186,15 @@ public class DeviceControl { @RequestParam(required = false) Integer resetTime, @RequestParam(required = false) Integer presetIndex) { if (log.isDebugEnabled()) { - log.debug("报警复位API调用"); + log.debug("看守位控制API调用"); } - String key = DeferredResultHolder.CALLBACK_CMD_DEVICECONTROL + (ObjectUtils.isEmpty(channelId) ? deviceId : channelId); - String uuid = UUID.randomUUID().toString(); Device device = deviceService.getDeviceByDeviceId(deviceId); - try { - cmder.homePositionCmd(device, channelId, enabled, resetTime, presetIndex, event -> { - RequestMessage msg = new RequestMessage(); - msg.setId(uuid); - msg.setKey(key); - msg.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), String.format("操作失败,错误码: %s, %s", event.statusCode, event.msg))); - resultHolder.invokeResult(msg); - },null); - } catch (InvalidArgumentException | SipException | ParseException e) { - log.error("[命令发送失败] 看守位控制: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); - } - DeferredResult> result = new DeferredResult<>(3 * 1000L); + Assert.notNull(device, "设备不存在"); + DeferredResult> result = deviceService.homePosition(device, channelId, enabled, resetTime, presetIndex); result.onTimeout(() -> { - log.warn(String.format("看守位控制操作超时, 设备未返回应答指令")); - // 释放rtpserver - RequestMessage msg = new RequestMessage(); - msg.setId(uuid); - msg.setKey(key); - msg.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); //("看守位控制操作超时, 设备未返回应答指令"); - resultHolder.invokeResult(msg); + log.warn("[看守位控制] 操作超时, 设备未返回应答指令, {}", deviceId); + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); }); - resultHolder.put(key, uuid, result); return result; } @@ -259,21 +231,8 @@ public class DeviceControl { log.debug(String.format("设备拉框放大 API调用,deviceId:%s ,channelId:%s ,length:%d ,width:%d ,midpointx:%d ,midpointy:%d ,lengthx:%d ,lengthy:%d",deviceId, channelId, length, width, midpointx, midpointy,lengthx, lengthy)); } Device device = deviceService.getDeviceByDeviceId(deviceId); - StringBuffer cmdXml = new StringBuffer(200); - cmdXml.append("\r\n"); - cmdXml.append("" + length+ "\r\n"); - cmdXml.append("" + width+ "\r\n"); - cmdXml.append("" + midpointx+ "\r\n"); - cmdXml.append("" + midpointy+ "\r\n"); - cmdXml.append("" + lengthx+ "\r\n"); - cmdXml.append("" + lengthy+ "\r\n"); - cmdXml.append("\r\n"); - try { - cmder.dragZoomCmd(device, channelId, cmdXml.toString()); - } catch (InvalidArgumentException | SipException | ParseException e) { - log.error("[命令发送失败] 拉框放大: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); - } + Assert.notNull(device, "设备不存在"); + deviceService.dragZoomIn(device, channelId, length, width, midpointx, midpointy, lengthx,lengthy); } /** @@ -311,20 +270,7 @@ public class DeviceControl { log.debug(String.format("设备拉框缩小 API调用,deviceId:%s ,channelId:%s ,length:%d ,width:%d ,midpointx:%d ,midpointy:%d ,lengthx:%d ,lengthy:%d",deviceId, channelId, length, width, midpointx, midpointy,lengthx, lengthy)); } Device device = deviceService.getDeviceByDeviceId(deviceId); - StringBuffer cmdXml = new StringBuffer(200); - cmdXml.append("\r\n"); - cmdXml.append("" + length+ "\r\n"); - cmdXml.append("" + width+ "\r\n"); - cmdXml.append("" + midpointx+ "\r\n"); - cmdXml.append("" + midpointy+ "\r\n"); - cmdXml.append("" + lengthx+ "\r\n"); - cmdXml.append("" + lengthy+ "\r\n"); - cmdXml.append("\r\n"); - try { - cmder.dragZoomCmd(device, channelId, cmdXml.toString()); - } catch (InvalidArgumentException | SipException | ParseException e) { - log.error("[命令发送失败] 拉框缩小: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); - } + Assert.notNull(device, "设备不存在"); + deviceService.dragZoomOut(device, channelId, length, width, midpointx, midpointy, lengthx,lengthy); } } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceQuery.java b/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceQuery.java index fdcf5b4d..b768a7ac 100755 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceQuery.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/controller/DeviceQuery.java @@ -312,40 +312,18 @@ public class DeviceQuery { @Operation(summary = "设备状态查询", security = @SecurityRequirement(name = JwtUtils.HEADER)) @Parameter(name = "deviceId", description = "设备国标编号", required = true) @GetMapping("/devices/{deviceId}/status") - public DeferredResult> deviceStatusApi(@PathVariable String deviceId) { + public DeferredResult> deviceStatusApi(@PathVariable String deviceId) { if (log.isDebugEnabled()) { log.debug("设备状态查询API调用"); } Device device = deviceService.getDeviceByDeviceId(deviceId); - String uuid = UUID.randomUUID().toString(); - String key = DeferredResultHolder.CALLBACK_CMD_DEVICESTATUS + deviceId; - DeferredResult> result = new DeferredResult>(2*1000L); - if(device == null) { - result.setResult(new ResponseEntity(String.format("设备%s不存在", deviceId),HttpStatus.OK)); - return result; - } - try { - cmder.deviceStatusQuery(device, event -> { - RequestMessage msg = new RequestMessage(); - msg.setId(uuid); - msg.setKey(key); - msg.setData(String.format("获取设备状态失败,错误码: %s, %s", event.statusCode, event.msg)); - resultHolder.invokeResult(msg); - }); - } catch (InvalidArgumentException | SipException | ParseException e) { - log.error("[命令发送失败] 获取设备状态: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); - } - result.onTimeout(()->{ - log.warn(String.format("获取设备状态超时")); - // 释放rtpserver - RequestMessage msg = new RequestMessage(); - msg.setId(uuid); - msg.setKey(key); - msg.setData("Timeout. Device did not response to this command."); - resultHolder.invokeResult(msg); + Assert.notNull(device, "设备不存在"); + + DeferredResult> result = deviceService.deviceStatus(device); + result.onTimeout(() -> { + log.warn("[设备状态查询] 操作超时, 设备未返回应答指令, {}", deviceId); + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); }); - resultHolder.put(DeferredResultHolder.CALLBACK_CMD_DEVICESTATUS + deviceId, uuid, result); return result; } @@ -369,7 +347,7 @@ public class DeviceQuery { @Parameter(name = "startTime", description = "报警发生起始时间") @Parameter(name = "endTime", description = "报警发生终止时间") @GetMapping("/alarm/{deviceId}") - public DeferredResult> alarmApi(@PathVariable String deviceId, + public DeferredResult> alarmApi(@PathVariable String deviceId, @RequestParam(required = false) String startPriority, @RequestParam(required = false) String endPriority, @RequestParam(required = false) String alarmMethod, @@ -380,6 +358,16 @@ public class DeviceQuery { log.debug("设备报警查询API调用"); } Device device = deviceService.getDeviceByDeviceId(deviceId); + Assert.notNull(device, "设备不存在"); + + DeferredResult> result = deviceService.deviceStatus(device); + result.onTimeout(() -> { + log.warn("[设备报警查询] 操作超时, 设备未返回应答指令, {}", deviceId); + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); + }); + return result; + + String key = DeferredResultHolder.CALLBACK_CMD_ALARM + deviceId; String uuid = UUID.randomUUID().toString(); try { diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/service/IDeviceService.java b/src/main/java/com/genersoft/iot/vmp/gb28181/service/IDeviceService.java index 43e262e9..60cadd56 100755 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/service/IDeviceService.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/service/IDeviceService.java @@ -170,15 +170,26 @@ public interface IDeviceService { WVPResult devicesSync(Device device); - DeferredResult deviceBasicConfig(Device device, String channelId, String name, String expiration, String heartBeatInterval, String heartBeatCount); + DeferredResult> deviceBasicConfig(Device device, String channelId, String name, String expiration, String heartBeatInterval, String heartBeatCount); - DeferredResult deviceConfigQuery(Device device, String channelId, String configType); + DeferredResult> deviceConfigQuery(Device device, String channelId, String configType); void teleboot(Device device); - DeferredResult record(Device device, String channelId, String recordCmdStr); + DeferredResult> record(Device device, String channelId, String recordCmdStr); DeferredResult> guard(Device device, String guardCmdStr); DeferredResult> resetAlarm(Device device, String channelId, String alarmMethod, String alarmType); + + void iFrame(Device device, String channelId); + + DeferredResult> homePosition(Device device, String channelId, Boolean enabled, Integer resetTime, Integer presetIndex); + + void dragZoomIn(Device device, String channelId, int length, int width, int midpointx, int midpointy, int lengthx, int lengthy); + + void dragZoomOut(Device device, String channelId, int length, int width, int midpointx, int midpointy, int lengthx, int lengthy); + + DeferredResult> deviceStatus(Device device); + } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/DeviceServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/DeviceServiceImpl.java index 5d9c0b9c..d7034ac8 100755 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/DeviceServiceImpl.java @@ -36,6 +36,7 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -493,9 +494,7 @@ public class DeviceServiceImpl implements IDeviceService { @Transactional public boolean delete(String deviceId) { Device device = getDeviceByDeviceIdFromDb(deviceId); - if (device == null) { - throw new ControllerException(ErrorCode.ERROR100.getCode(), "未找到设备:" + deviceId); - } + Assert.notNull(device, "未找到设备"); platformChannelMapper.delChannelForDeviceId(deviceId); deviceChannelMapper.cleanChannelsByDeviceId(device.getId()); deviceMapper.del(deviceId); @@ -619,7 +618,7 @@ public class DeviceServiceImpl implements IDeviceService { // 已存在则返回进度 if (isSyncRunning(device.getDeviceId())) { SyncStatus channelSyncStatus = getChannelSyncStatus(device.getDeviceId()); - WVPResult wvpResult = new WVPResult(); + WVPResult wvpResult = new WVPResult(); if (channelSyncStatus.getErrorMsg() != null) { wvpResult.setCode(ErrorCode.ERROR100.getCode()); wvpResult.setMsg(channelSyncStatus.getErrorMsg()); @@ -641,46 +640,46 @@ public class DeviceServiceImpl implements IDeviceService { } @Override - public DeferredResult deviceBasicConfig(Device device, String channelId, String name, String expiration, + public DeferredResult> deviceBasicConfig(Device device, String channelId, String name, String expiration, String heartBeatInterval, String heartBeatCount) { if (!userSetting.getServerId().equals(device.getServerId())) { - String result = redisRpcService.deviceBasicConfig(device.getServerId(), device, channelId, name, expiration, + WVPResult result = redisRpcService.deviceBasicConfig(device.getServerId(), device, channelId, name, expiration, heartBeatInterval, heartBeatCount); - DeferredResult deferredResult = new DeferredResult(3 * 1000L); + DeferredResult> deferredResult = new DeferredResult<>(3 * 1000L); deferredResult.setResult(result); return deferredResult; } - DeferredResult result = new DeferredResult(3 * 1000L); + DeferredResult> result = new DeferredResult<>(3 * 1000L); try { sipCommander.deviceBasicConfigCmd(device, channelId, name, expiration, heartBeatInterval, heartBeatCount, event -> { - result.setResult(String.format("设备配置操作失败,错误码: %s, %s", event.statusCode, event.msg)); + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时")); }); } catch (InvalidArgumentException | SipException | ParseException e) { log.error("[命令发送失败] 设备配置: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送: " + e.getMessage()); } return result; } @Override - public DeferredResult deviceConfigQuery(Device device, String channelId, String configType) { + public DeferredResult> deviceConfigQuery(Device device, String channelId, String configType) { if (!userSetting.getServerId().equals(device.getServerId())) { - String result = redisRpcService.deviceConfigQuery(device.getServerId(), device, channelId, configType); - DeferredResult deferredResult = new DeferredResult(3 * 1000L); + WVPResult result = redisRpcService.deviceConfigQuery(device.getServerId(), device, channelId, configType); + DeferredResult> deferredResult = new DeferredResult<>(3 * 1000L); deferredResult.setResult(result); return deferredResult; } - DeferredResult result = new DeferredResult(3 * 1000L); + DeferredResult> result = new DeferredResult<>(3 * 1000L); try { sipCommander.deviceConfigQuery(device, channelId, configType, event -> { - result.setResult(String.format("获取设备配置失败,错误码: %s, %s", event.statusCode, event.msg)); + result.setResult(WVPResult.fail(ErrorCode.ERROR100)); }); } catch (InvalidArgumentException | SipException | ParseException e) { log.error("[命令发送失败] 获取设备配置: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送: " + e.getMessage()); } return result; } @@ -700,23 +699,23 @@ public class DeviceServiceImpl implements IDeviceService { } @Override - public DeferredResult record(Device device, String channelId, String recordCmdStr) { + public DeferredResult> record(Device device, String channelId, String recordCmdStr) { if (!userSetting.getServerId().equals(device.getServerId())) { - String result = redisRpcService.recordControl(device.getServerId(), device, channelId, recordCmdStr); - DeferredResult deferredResult = new DeferredResult(3 * 1000L); + WVPResult result = redisRpcService.recordControl(device.getServerId(), device, channelId, recordCmdStr); + DeferredResult> deferredResult = new DeferredResult<>(3 * 1000L); deferredResult.setResult(result); return deferredResult; } - DeferredResult result = new DeferredResult<>(3 * 1000L); + DeferredResult> result = new DeferredResult<>(3 * 1000L); try { sipCommander.recordCmd(device, channelId, recordCmdStr, event -> { - result.setResult(String.format("开始/停止录像操作失败,错误码: %s, %s", event.statusCode, event.msg)); + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), String.format("开始/停止录像操作失败,错误码: %s, %s", event.statusCode, event.msg))); },null); } catch (InvalidArgumentException | SipException | ParseException e) { log.error("[命令发送失败] 开始/停止录像: {}", e.getMessage()); - throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送: " + e.getMessage()); } return result; @@ -765,4 +764,111 @@ public class DeviceServiceImpl implements IDeviceService { return result; } + + @Override + public void iFrame(Device device, String channelId) { + if (!userSetting.getServerId().equals(device.getServerId())) { + redisRpcService.iFrame(device.getServerId(), device, channelId); + return; + } + + try { + sipCommander.iFrameCmd(device, channelId); + } catch (InvalidArgumentException | SipException | ParseException e) { + log.error("[命令发送失败] 强制关键帧操作: {}", e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送: " + e.getMessage()); + } + } + + @Override + public DeferredResult> homePosition(Device device, String channelId, Boolean enabled, Integer resetTime, Integer presetIndex) { + if (!userSetting.getServerId().equals(device.getServerId())) { + WVPResult result = redisRpcService.homePosition(device.getServerId(), device, channelId, enabled, resetTime, presetIndex); + DeferredResult> deferredResult = new DeferredResult<>(3 * 1000L); + deferredResult.setResult(result); + return deferredResult; + } + + DeferredResult> result = new DeferredResult<>(3 * 1000L); + + try { + sipCommander.homePositionCmd(device, channelId, enabled, resetTime, presetIndex, event -> { + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), String.format("操作失败,错误码: %s, %s", event.statusCode, event.msg))); + },null); + } catch (InvalidArgumentException | SipException | ParseException e) { + log.error("[命令发送失败] 看守位控制: {}", e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); + } + return result; + } + + @Override + public void dragZoomIn(Device device, String channelId, int length, int width, int midpointx, int midpointy, int lengthx, int lengthy) { + if (!userSetting.getServerId().equals(device.getServerId())) { + redisRpcService.dragZoomIn(device.getServerId(), device, channelId, length, width, midpointx, midpointy, lengthx, lengthy); + return; + } + + StringBuffer cmdXml = new StringBuffer(200); + cmdXml.append("\r\n"); + cmdXml.append("" + length+ "\r\n"); + cmdXml.append("" + width+ "\r\n"); + cmdXml.append("" + midpointx+ "\r\n"); + cmdXml.append("" + midpointy+ "\r\n"); + cmdXml.append("" + lengthx+ "\r\n"); + cmdXml.append("" + lengthy+ "\r\n"); + cmdXml.append("\r\n"); + try { + sipCommander.dragZoomCmd(device, channelId, cmdXml.toString()); + } catch (InvalidArgumentException | SipException | ParseException e) { + log.error("[命令发送失败] 拉框放大: {}", e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); + } + } + + @Override + public void dragZoomOut(Device device, String channelId, int length, int width, int midpointx, int midpointy, int lengthx, int lengthy) { + if (!userSetting.getServerId().equals(device.getServerId())) { + redisRpcService.dragZoomOut(device.getServerId(), device, channelId, length, width, midpointx, midpointy, lengthx, lengthy); + return; + } + + StringBuffer cmdXml = new StringBuffer(200); + cmdXml.append("\r\n"); + cmdXml.append("" + length+ "\r\n"); + cmdXml.append("" + width+ "\r\n"); + cmdXml.append("" + midpointx+ "\r\n"); + cmdXml.append("" + midpointy+ "\r\n"); + cmdXml.append("" + lengthx+ "\r\n"); + cmdXml.append("" + lengthy+ "\r\n"); + cmdXml.append("\r\n"); + try { + sipCommander.dragZoomCmd(device, channelId, cmdXml.toString()); + } catch (InvalidArgumentException | SipException | ParseException e) { + log.error("[命令发送失败] 拉框放大: {}", e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); + } + } + + @Override + public DeferredResult> deviceStatus(Device device) { + + if (!userSetting.getServerId().equals(device.getServerId())) { + WVPResult result = redisRpcService.deviceStatus(device.getServerId(), device); + DeferredResult> deferredResult = new DeferredResult<>(3 * 1000L); + deferredResult.setResult(result); + return deferredResult; + } + + DeferredResult> result = new DeferredResult<>(2*1000L); + try { + sipCommander.deviceStatusQuery(device, event -> { + result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), String.format("操作失败,错误码: %s, %s", event.statusCode, event.msg))); + }); + } catch (InvalidArgumentException | SipException | ParseException e) { + log.error("[命令发送失败] 获取设备状态: {}", e.getMessage()); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); + } + return result; + } } diff --git a/src/main/java/com/genersoft/iot/vmp/service/redisMsg/IRedisRpcService.java b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/IRedisRpcService.java index eb44b204..e7f9d6e8 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/redisMsg/IRedisRpcService.java +++ b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/IRedisRpcService.java @@ -40,13 +40,25 @@ public interface IRedisRpcService { SyncStatus getChannelSyncStatus(String serverId, String deviceId); - String deviceBasicConfig(String serverId, Device device, String channelId, String name, String expiration, String heartBeatInterval, String heartBeatCount); + WVPResult deviceBasicConfig(String serverId, Device device, String channelId, String name, String expiration, String heartBeatInterval, String heartBeatCount); - String deviceConfigQuery(String serverId, Device device, String channelId, String configType); + WVPResult deviceConfigQuery(String serverId, Device device, String channelId, String configType); void teleboot(String serverId, Device device); - String recordControl(String serverId, Device device, String channelId, String recordCmdStr); + WVPResult recordControl(String serverId, Device device, String channelId, String recordCmdStr); WVPResult guard(String serverId, Device device, String guardCmdStr); + + WVPResult resetAlarm(String serverId, Device device, String channelId, String alarmMethod, String alarmType); + + void iFrame(String serverId, Device device, String channelId); + + WVPResult homePosition(String serverId, Device device, String channelId, Boolean enabled, Integer resetTime, Integer presetIndex); + + void dragZoomIn(String serverId, Device device, String channelId, int length, int width, int midpointx, int midpointy, int lengthx, int lengthy); + + void dragZoomOut(String serverId, Device device, String channelId, int length, int width, int midpointx, int midpointy, int lengthx, int lengthy); + + WVPResult deviceStatus(String serverId, Device device); } diff --git a/src/main/java/com/genersoft/iot/vmp/service/redisMsg/control/RedisRpcDeviceController.java b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/control/RedisRpcDeviceController.java index 10067fc1..0a4867e6 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/redisMsg/control/RedisRpcDeviceController.java +++ b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/control/RedisRpcDeviceController.java @@ -107,22 +107,18 @@ public class RedisRpcDeviceController extends RpcController { response.setBody("param error"); return response; } - DeferredResult deferredResult = deviceService.deviceBasicConfig(device, channelId, name, expiration, heartBeatInterval, heartBeatInterval); + DeferredResult> deferredResult = deviceService.deviceBasicConfig(device, channelId, name, + expiration, heartBeatInterval, heartBeatInterval); deferredResult.onCompletion(() ->{ - String resultStr = (String)deferredResult.getResult(); response.setStatusCode(ErrorCode.SUCCESS.getCode()); - response.setBody(resultStr); + response.setBody(deferredResult.getResult()); // 手动发送结果 sendResponse(response); }); deferredResult.onTimeout(() -> { log.warn(String.format("设备配置操作超时, 设备未返回应答指令")); - JSONObject json = new JSONObject(); - json.put("DeviceID", device.getDeviceId()); - json.put("Status", "Timeout"); - json.put("Description", "设备配置操作超时, 设备未返回应答指令"); response.setStatusCode(ErrorCode.SUCCESS.getCode()); - response.setBody(json); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); // 手动发送结果 sendResponse(response); }); @@ -144,22 +140,17 @@ public class RedisRpcDeviceController extends RpcController { response.setBody("param error"); return response; } - DeferredResult deferredResult = deviceService.deviceConfigQuery(device, channelId, configType); + DeferredResult> deferredResult = deviceService.deviceConfigQuery(device, channelId, configType); deferredResult.onCompletion(() ->{ - String resultStr = (String)deferredResult.getResult(); response.setStatusCode(ErrorCode.SUCCESS.getCode()); - response.setBody(resultStr); + response.setBody(deferredResult.getResult()); // 手动发送结果 sendResponse(response); }); deferredResult.onTimeout(() -> { - log.warn(String.format("设备配置操作超时, 设备未返回应答指令")); - JSONObject json = new JSONObject(); - json.put("DeviceID", device.getDeviceId()); - json.put("Status", "Timeout"); - json.put("Description", "设备配置操作超时, 设备未返回应答指令"); + log.warn("[设备配置]操作超时, 设备未返回应答指令, {}", deviceId); response.setStatusCode(ErrorCode.SUCCESS.getCode()); - response.setBody(json); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), "超时")); // 手动发送结果 sendResponse(response); }); @@ -182,11 +173,11 @@ public class RedisRpcDeviceController extends RpcController { deviceService.teleboot(device); }catch (ControllerException e) { response.setStatusCode(e.getCode()); - response.setBody(e.getMsg()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); return response; } response.setStatusCode(ErrorCode.SUCCESS.getCode()); - response.setBody(ErrorCode.SUCCESS.getMsg()); + response.setBody(WVPResult.success()); return response; } @@ -206,28 +197,22 @@ public class RedisRpcDeviceController extends RpcController { return response; } try { - DeferredResult deferredResult = deviceService.record(device, channelId, recordCmdStr); + DeferredResult> deferredResult = deviceService.record(device, channelId, recordCmdStr); deferredResult.onCompletion(() ->{ - String resultStr = (String)deferredResult.getResult(); response.setStatusCode(ErrorCode.SUCCESS.getCode()); - response.setBody(resultStr); + response.setBody(deferredResult.getResult()); // 手动发送结果 sendResponse(response); }); deferredResult.onTimeout(() -> { - log.warn("设备录像控制操作超时, 设备未返回应答指令"); - JSONObject json = new JSONObject(); - json.put("DeviceID", device.getDeviceId()); - json.put("Status", "Timeout"); - json.put("Description", "设备录像控制操作超时, 设备未返回应答指令"); response.setStatusCode(ErrorCode.SUCCESS.getCode()); - response.setBody(json); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); // 手动发送结果 sendResponse(response); }); }catch (ControllerException e) { response.setStatusCode(e.getCode()); - response.setBody(e.getMsg()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); sendResponse(response); } return null; @@ -256,7 +241,7 @@ public class RedisRpcDeviceController extends RpcController { sendResponse(response); }); deferredResult.onTimeout(() -> { - log.warn("设备录像控制操作超时, 设备未返回应答指令"); + log.warn("[布防/撤防]操作超时, 设备未返回应答指令"); response.setStatusCode(ErrorCode.SUCCESS.getCode()); response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); // 手动发送结果 @@ -264,12 +249,210 @@ public class RedisRpcDeviceController extends RpcController { }); }catch (ControllerException e) { response.setStatusCode(e.getCode()); - response.setBody(e.getMsg()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); + sendResponse(response); + } + return null; + } + + @RedisRpcMapping("resetAlarm") + public RedisRpcResponse resetAlarm(RedisRpcRequest request) { + JSONObject paramJson = JSONObject.parseObject(request.getParam().toString()); + String deviceId = paramJson.getString("deviceId"); + String channelId = paramJson.getString("channelId"); + String alarmMethod = paramJson.getString("alarmMethod"); + String alarmType = paramJson.getString("alarmType"); + + Device device = deviceService.getDeviceByDeviceId(deviceId); + + RedisRpcResponse response = request.getResponse(); + if (device == null || !userSetting.getServerId().equals(device.getServerId())) { + response.setStatusCode(ErrorCode.ERROR400.getCode()); + response.setBody("param error"); + return response; + } + try { + DeferredResult> deferredResult = deviceService.resetAlarm(device, channelId, alarmMethod, alarmType); + deferredResult.onCompletion(() ->{ + response.setStatusCode(ErrorCode.SUCCESS.getCode()); + response.setBody(deferredResult.getResult()); + // 手动发送结果 + sendResponse(response); + }); + deferredResult.onTimeout(() -> { + log.warn("[报警重置] 操作超时, 设备未返回应答指令"); + response.setStatusCode(ErrorCode.SUCCESS.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); + // 手动发送结果 + sendResponse(response); + }); + }catch (ControllerException e) { + response.setStatusCode(e.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); + sendResponse(response); + } + return null; + } + + @RedisRpcMapping("iFrame") + public RedisRpcResponse iFrame(RedisRpcRequest request) { + JSONObject paramJson = JSONObject.parseObject(request.getParam().toString()); + String deviceId = paramJson.getString("deviceId"); + String channelId = paramJson.getString("channelId"); + + Device device = deviceService.getDeviceByDeviceId(deviceId); + + RedisRpcResponse response = request.getResponse(); + if (device == null || !userSetting.getServerId().equals(device.getServerId())) { + response.setStatusCode(ErrorCode.ERROR400.getCode()); + response.setBody("param error"); + return response; + } + try { + deviceService.iFrame(device, channelId); + }catch (ControllerException e) { + response.setStatusCode(e.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); + sendResponse(response); + } + return null; + } + + @RedisRpcMapping("homePosition") + public RedisRpcResponse homePosition(RedisRpcRequest request) { + JSONObject paramJson = JSONObject.parseObject(request.getParam().toString()); + String deviceId = paramJson.getString("deviceId"); + String channelId = paramJson.getString("channelId"); + + Boolean enabled = paramJson.getBoolean("enabled"); + Integer resetTime = paramJson.getInteger("resetTime"); + Integer presetIndex = paramJson.getInteger("presetIndex"); + + Device device = deviceService.getDeviceByDeviceId(deviceId); + + RedisRpcResponse response = request.getResponse(); + if (device == null || !userSetting.getServerId().equals(device.getServerId())) { + response.setStatusCode(ErrorCode.ERROR400.getCode()); + response.setBody("param error"); + return response; + } + try { + DeferredResult> deferredResult = deviceService.homePosition(device, channelId, enabled, resetTime, presetIndex); + deferredResult.onCompletion(() ->{ + response.setStatusCode(ErrorCode.SUCCESS.getCode()); + response.setBody(deferredResult.getResult()); + // 手动发送结果 + sendResponse(response); + }); + deferredResult.onTimeout(() -> { + log.warn("[看守位控制] 操作超时, 设备未返回应答指令"); + response.setStatusCode(ErrorCode.SUCCESS.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); + // 手动发送结果 + sendResponse(response); + }); + }catch (ControllerException e) { + response.setStatusCode(e.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); + sendResponse(response); + } + return null; + } + + @RedisRpcMapping("dragZoomIn") + public RedisRpcResponse dragZoomIn(RedisRpcRequest request) { + JSONObject paramJson = JSONObject.parseObject(request.getParam().toString()); + String deviceId = paramJson.getString("deviceId"); + String channelId = paramJson.getString("channelId"); + Integer length = paramJson.getInteger("length"); + Integer width = paramJson.getInteger("width"); + Integer midpointx = paramJson.getInteger("midpointx"); + Integer midpointy = paramJson.getInteger("midpointy"); + Integer lengthx = paramJson.getInteger("lengthx"); + Integer lengthy = paramJson.getInteger("lengthy"); + + Device device = deviceService.getDeviceByDeviceId(deviceId); + + RedisRpcResponse response = request.getResponse(); + if (device == null || !userSetting.getServerId().equals(device.getServerId())) { + response.setStatusCode(ErrorCode.ERROR400.getCode()); + response.setBody("param error"); + return response; + } + try { + deviceService.dragZoomIn(device, channelId, length, width, midpointx, midpointy, lengthx, lengthy); + }catch (ControllerException e) { + response.setStatusCode(e.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); + sendResponse(response); + } + return null; + } + + @RedisRpcMapping("dragZoomOut") + public RedisRpcResponse dragZoomOut(RedisRpcRequest request) { + JSONObject paramJson = JSONObject.parseObject(request.getParam().toString()); + String deviceId = paramJson.getString("deviceId"); + String channelId = paramJson.getString("channelId"); + Integer length = paramJson.getInteger("length"); + Integer width = paramJson.getInteger("width"); + Integer midpointx = paramJson.getInteger("midpointx"); + Integer midpointy = paramJson.getInteger("midpointy"); + Integer lengthx = paramJson.getInteger("lengthx"); + Integer lengthy = paramJson.getInteger("lengthy"); + + Device device = deviceService.getDeviceByDeviceId(deviceId); + + RedisRpcResponse response = request.getResponse(); + if (device == null || !userSetting.getServerId().equals(device.getServerId())) { + response.setStatusCode(ErrorCode.ERROR400.getCode()); + response.setBody("param error"); + return response; + } + try { + deviceService.dragZoomOut(device, channelId, length, width, midpointx, midpointy, lengthx, lengthy); + }catch (ControllerException e) { + response.setStatusCode(e.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); + sendResponse(response); + } + return null; + } + + @RedisRpcMapping("deviceStatus") + public RedisRpcResponse deviceStatus(RedisRpcRequest request) { + String deviceId = request.getParam().toString(); + + Device device = deviceService.getDeviceByDeviceId(deviceId); + + RedisRpcResponse response = request.getResponse(); + if (device == null || !userSetting.getServerId().equals(device.getServerId())) { + response.setStatusCode(ErrorCode.ERROR400.getCode()); + response.setBody("param error"); + return response; + } + try { + DeferredResult> deferredResult = deviceService.deviceStatus(device); + deferredResult.onCompletion(() ->{ + response.setStatusCode(ErrorCode.SUCCESS.getCode()); + response.setBody(deferredResult.getResult()); + // 手动发送结果 + sendResponse(response); + }); + deferredResult.onTimeout(() -> { + log.warn("[获取设备状态] 操作超时, 设备未返回应答指令"); + response.setStatusCode(ErrorCode.SUCCESS.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), "操作超时, 设备未应答")); + // 手动发送结果 + sendResponse(response); + }); + }catch (ControllerException e) { + response.setStatusCode(e.getCode()); + response.setBody(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMsg())); sendResponse(response); } return null; } - } diff --git a/src/main/java/com/genersoft/iot/vmp/service/redisMsg/service/RedisRpcServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/service/RedisRpcServiceImpl.java index 169d4c04..d365d47e 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/redisMsg/service/RedisRpcServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/service/RedisRpcServiceImpl.java @@ -267,7 +267,7 @@ public class RedisRpcServiceImpl implements IRedisRpcService { } @Override - public String deviceBasicConfig(String serverId, Device device, String channelId, String name, String expiration, + public WVPResult deviceBasicConfig(String serverId, Device device, String channelId, String name, String expiration, String heartBeatInterval, String heartBeatCount) { JSONObject jsonObject = new JSONObject(); jsonObject.put("device", device.getDeviceId()); @@ -279,11 +279,11 @@ public class RedisRpcServiceImpl implements IRedisRpcService { RedisRpcRequest request = buildRequest("device/deviceBasicConfig", jsonObject); request.setToId(serverId); RedisRpcResponse response = redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); - return response.getBody().toString(); + return JSON.parseObject(response.getBody().toString(), WVPResult.class); } @Override - public String deviceConfigQuery(String serverId, Device device, String channelId, String configType) { + public WVPResult deviceConfigQuery(String serverId, Device device, String channelId, String configType) { JSONObject jsonObject = new JSONObject(); jsonObject.put("device", device.getDeviceId()); jsonObject.put("channelId", channelId); @@ -291,7 +291,7 @@ public class RedisRpcServiceImpl implements IRedisRpcService { RedisRpcRequest request = buildRequest("device/deviceConfigQuery", jsonObject); request.setToId(serverId); RedisRpcResponse response = redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); - return response.getBody().toString(); + return JSON.parseObject(response.getBody().toString(), WVPResult.class); } @Override @@ -305,7 +305,7 @@ public class RedisRpcServiceImpl implements IRedisRpcService { } @Override - public String recordControl(String serverId, Device device, String channelId, String recordCmdStr) { + public WVPResult recordControl(String serverId, Device device, String channelId, String recordCmdStr) { JSONObject jsonObject = new JSONObject(); jsonObject.put("device", device.getDeviceId()); jsonObject.put("channelId", channelId); @@ -313,7 +313,7 @@ public class RedisRpcServiceImpl implements IRedisRpcService { RedisRpcRequest request = buildRequest("device/record", jsonObject); request.setToId(serverId); RedisRpcResponse response = redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); - return response.getBody().toString(); + return JSON.parseObject(response.getBody().toString(), WVPResult.class); } @Override @@ -326,4 +326,82 @@ public class RedisRpcServiceImpl implements IRedisRpcService { RedisRpcResponse response = redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); return JSON.parseObject(response.getBody().toString(), WVPResult.class); } + + @Override + public WVPResult resetAlarm(String serverId, Device device, String channelId, String alarmMethod, String alarmType) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("device", device.getDeviceId()); + jsonObject.put("channelId", channelId); + jsonObject.put("alarmMethod", alarmMethod); + jsonObject.put("alarmType", alarmType); + RedisRpcRequest request = buildRequest("device/resetAlarm", jsonObject); + request.setToId(serverId); + RedisRpcResponse response = redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); + return JSON.parseObject(response.getBody().toString(), WVPResult.class); + } + + @Override + public void iFrame(String serverId, Device device, String channelId) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("device", device.getDeviceId()); + jsonObject.put("channelId", channelId); + RedisRpcRequest request = buildRequest("device/iFrame", jsonObject); + request.setToId(serverId); + redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); + } + + @Override + public WVPResult homePosition(String serverId, Device device, String channelId, Boolean enabled, Integer resetTime, Integer presetIndex) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("device", device.getDeviceId()); + jsonObject.put("channelId", channelId); + jsonObject.put("enabled", enabled); + jsonObject.put("resetTime", resetTime); + jsonObject.put("presetIndex", presetIndex); + RedisRpcRequest request = buildRequest("device/homePosition", jsonObject); + request.setToId(serverId); + RedisRpcResponse response = redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); + return JSON.parseObject(response.getBody().toString(), WVPResult.class); + } + + @Override + public void dragZoomIn(String serverId, Device device, String channelId, int length, int width, int midpointx, + int midpointy, int lengthx, int lengthy) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("device", device.getDeviceId()); + jsonObject.put("channelId", channelId); + jsonObject.put("length", length); + jsonObject.put("width", width); + jsonObject.put("midpointx", midpointx); + jsonObject.put("midpointy", midpointy); + jsonObject.put("lengthx", lengthx); + jsonObject.put("lengthy", lengthy); + RedisRpcRequest request = buildRequest("device/dragZoomIn", jsonObject); + request.setToId(serverId); + redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); + } + + @Override + public void dragZoomOut(String serverId, Device device, String channelId, int length, int width, int midpointx, int midpointy, int lengthx, int lengthy) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("device", device.getDeviceId()); + jsonObject.put("channelId", channelId); + jsonObject.put("length", length); + jsonObject.put("width", width); + jsonObject.put("midpointx", midpointx); + jsonObject.put("midpointy", midpointy); + jsonObject.put("lengthx", lengthx); + jsonObject.put("lengthy", lengthy); + RedisRpcRequest request = buildRequest("device/dragZoomOut", jsonObject); + request.setToId(serverId); + redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); + } + + @Override + public WVPResult deviceStatus(String serverId, Device device) { + RedisRpcRequest request = buildRequest("device/deviceStatus", device.getDeviceId()); + request.setToId(serverId); + RedisRpcResponse response = redisRpcConfig.request(request, 50, TimeUnit.MILLISECONDS); + return JSON.parseObject(response.getBody().toString(), WVPResult.class); + } }