From 5d901b5e3f033e8b04e53420d68626cbd87431c8 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期五, 06 五月 2022 10:12:34 +0800
Subject: [PATCH] 使用阿里代码规范。规范代码写法

---
 src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceQuery.java |  199 +++++++++++++++++++++++++++++++------------------
 1 files changed, 127 insertions(+), 72 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceQuery.java b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceQuery.java
index d9357d2..d6e614d 100644
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceQuery.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceQuery.java
@@ -1,19 +1,22 @@
 package com.genersoft.iot.vmp.vmanager.gb28181.device;
 
 import com.alibaba.fastjson.JSONObject;
+import com.genersoft.iot.vmp.conf.DynamicTask;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
-import com.genersoft.iot.vmp.gb28181.event.DeviceOffLineDetector;
+import com.genersoft.iot.vmp.gb28181.bean.SubscribeHolder;
+import com.genersoft.iot.vmp.gb28181.bean.SyncStatus;
+import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
+import com.genersoft.iot.vmp.gb28181.task.impl.CatalogSubscribeTask;
+import com.genersoft.iot.vmp.gb28181.task.impl.MobilePositionSubscribeTask;
 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.service.IDeviceService;
 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
-import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
-import com.genersoft.iot.vmp.vmanager.bean.DeviceChannelTree;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
 import com.github.pagehelper.PageInfo;
-import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
@@ -27,8 +30,8 @@
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.context.request.async.DeferredResult;
 
-import java.util.List;
-import java.util.UUID;
+import javax.sip.DialogState;
+import java.util.*;
 
 @Api(tags = "鍥芥爣璁惧鏌ヨ", value = "鍥芥爣璁惧鏌ヨ")
 @SuppressWarnings("rawtypes")
@@ -40,7 +43,7 @@
 	private final static Logger logger = LoggerFactory.getLogger(DeviceQuery.class);
 	
 	@Autowired
-	private IVideoManagerStorager storager;
+	private IVideoManagerStorage storager;
 
 	@Autowired
 	private IRedisCatchStorage redisCatchStorage;
@@ -50,12 +53,15 @@
 	
 	@Autowired
 	private DeferredResultHolder resultHolder;
-	
-	@Autowired
-	private DeviceOffLineDetector offLineDetector;
 
 	@Autowired
 	private IDeviceService deviceService;
+
+	@Autowired
+	private DynamicTask dynamicTask;
+
+	@Autowired
+	private SubscribeHolder subscribeHolder;
 
 	/**
 	 * 浣跨敤ID鏌ヨ鍥芥爣璁惧
@@ -118,12 +124,14 @@
 			@ApiImplicitParam(name="query", value = "鏌ヨ鍐呭" ,dataTypeClass = String.class),
 			@ApiImplicitParam(name="online", value = "鏄惁鍦ㄧ嚎"  ,dataTypeClass = Boolean.class),
 			@ApiImplicitParam(name="channelType", value = "璁惧/瀛愮洰褰�-> false/true" ,dataTypeClass = Boolean.class),
+			@ApiImplicitParam(name="catalogUnderDevice", value = "鏄惁鐩村睘涓庤澶囩殑鐩綍" ,dataTypeClass = Boolean.class),
 	})
 	public ResponseEntity<PageInfo> channels(@PathVariable String deviceId,
 											   int page, int count,
 											   @RequestParam(required = false) String query,
 											   @RequestParam(required = false) Boolean online,
-											   @RequestParam(required = false) Boolean channelType) {
+											   @RequestParam(required = false) Boolean channelType,
+											   @RequestParam(required = false) Boolean catalogUnderDevice) {
 //		if (logger.isDebugEnabled()) {
 //			logger.debug("鏌ヨ瑙嗛璁惧閫氶亾API璋冪敤");
 //		}
@@ -131,7 +139,7 @@
 			query = null;
 		}
 
-		PageInfo pageResult = storager.queryChannelsByDeviceId(deviceId, query, channelType, online, page, count);
+		PageInfo pageResult = storager.queryChannelsByDeviceId(deviceId, query, channelType, online, catalogUnderDevice, page, count);
 		return new ResponseEntity<>(pageResult,HttpStatus.OK);
 	}
 
@@ -145,48 +153,27 @@
 			@ApiImplicitParam(name="deviceId", value = "璁惧id", required = true, dataTypeClass = String.class),
 	})
 	@PostMapping("/devices/{deviceId}/sync")
-	public DeferredResult<ResponseEntity<Device>> devicesSync(@PathVariable String deviceId){
+	public WVPResult<SyncStatus> devicesSync(@PathVariable String deviceId){
 		
 		if (logger.isDebugEnabled()) {
 			logger.debug("璁惧閫氶亾淇℃伅鍚屾API璋冪敤锛宒eviceId锛�" + deviceId);
 		}
 		Device device = storager.queryVideoDevice(deviceId);
-		String key = DeferredResultHolder.CALLBACK_CMD_CATALOG + deviceId;
-		String uuid = UUID.randomUUID().toString();
-		// 榛樿瓒呮椂鏃堕棿涓�30鍒嗛挓
-		DeferredResult<ResponseEntity<Device>> result = new DeferredResult<ResponseEntity<Device>>(30*60*1000L);
-		result.onTimeout(()->{
-			logger.warn("璁惧[{}]閫氶亾淇℃伅鍚屾瓒呮椂", deviceId);
-			// 閲婃斁rtpserver
-			RequestMessage msg = new RequestMessage();
-			msg.setKey(key);
-			msg.setId(uuid);
-			WVPResult<Object> wvpResult = new WVPResult<>();
-			wvpResult.setCode(-1);
-			wvpResult.setData(device);
-			wvpResult.setMsg("鏇存柊瓒呮椂");
-			msg.setData(wvpResult);
-			resultHolder.invokeAllResult(msg);
-
-		});
-		// 绛夊緟鍏朵粬鐩稿悓璇锋眰杩斿洖鏃朵竴璧疯繑鍥�
-		if (resultHolder.exist(key, null)) {
-			return result;
+		boolean status = deviceService.isSyncRunning(deviceId);
+		// 宸插瓨鍦ㄥ垯杩斿洖杩涘害
+		if (status) {
+			WVPResult<SyncStatus> wvpResult = new WVPResult<>();
+			wvpResult.setCode(0);
+			SyncStatus channelSyncStatus = deviceService.getChannelSyncStatus(deviceId);
+			wvpResult.setData(channelSyncStatus);
+			return wvpResult;
 		}
-        cmder.catalogQuery(device, event -> {
-			RequestMessage msg = new RequestMessage();
-			msg.setKey(key);
-			msg.setId(uuid);
-			WVPResult<Object> wvpResult = new WVPResult<>();
-			wvpResult.setCode(-1);
-			wvpResult.setData(device);
-			wvpResult.setMsg(String.format("鍚屾閫氶亾澶辫触锛岄敊璇爜锛� %s, %s", event.statusCode, event.msg));
-			msg.setData(wvpResult);
-			resultHolder.invokeAllResult(msg);
-		});
+		deviceService.sync(device);
 
-        resultHolder.put(key, uuid, result);
-        return result;
+		WVPResult<SyncStatus> wvpResult = new WVPResult<>();
+		wvpResult.setCode(0);
+		wvpResult.setMsg("寮�濮嬪悓姝�");
+		return wvpResult;
 	}
 
 	/**
@@ -204,14 +191,18 @@
 		if (logger.isDebugEnabled()) {
 			logger.debug("璁惧淇℃伅鍒犻櫎API璋冪敤锛宒eviceId锛�" + deviceId);
 		}
-		
-		if (offLineDetector.isOnline(deviceId)) {
-			return new ResponseEntity<String>("涓嶅厑璁稿垹闄ゅ湪绾胯澶囷紒", HttpStatus.NOT_ACCEPTABLE);
-		}
+
 		// 娓呴櫎redis璁板綍
 		boolean isSuccess = storager.delete(deviceId);
 		if (isSuccess) {
 			redisCatchStorage.clearCatchByDeviceId(deviceId);
+			// 鍋滄姝よ澶囩殑璁㈤槄鏇存柊
+			Set<String> allKeys = dynamicTask.getAllKeys();
+			for (String key : allKeys) {
+				if (key.startsWith(deviceId)) {
+					dynamicTask.stop(key);
+				}
+			}
 			JSONObject json = new JSONObject();
 			json.put("deviceId", deviceId);
 			return new ResponseEntity<>(json.toString(),HttpStatus.OK);
@@ -239,7 +230,7 @@
 			@ApiImplicitParam(name="page", value = "褰撳墠椤�", required = true, dataTypeClass = Integer.class),
 			@ApiImplicitParam(name="count", value = "姣忛〉鏉℃暟", required = true, dataTypeClass = Integer.class),
 			@ApiImplicitParam(name="query", value = "鏌ヨ鍐呭", dataTypeClass = String.class),
-			@ApiImplicitParam(name="online", value = "鏄惁鍦ㄧ嚎", dataTypeClass = String.class),
+			@ApiImplicitParam(name="online", value = "鏄惁鍦ㄧ嚎", dataTypeClass = Boolean.class),
 			@ApiImplicitParam(name="channelType", value = "閫氶亾绫诲瀷锛� 瀛愮洰褰�", dataTypeClass = Boolean.class),
 	})
 	@GetMapping("/sub_channels/{deviceId}/{channelId}/channels")
@@ -248,7 +239,7 @@
 												  int page,
 												  int count,
 												  @RequestParam(required = false) String query,
-												  @RequestParam(required = false) String online,
+												  @RequestParam(required = false) Boolean online,
 												  @RequestParam(required = false) Boolean channelType){
 
 //		if (logger.isDebugEnabled()) {
@@ -315,24 +306,50 @@
 
 		if (device != null && device.getDeviceId() != null) {
 			Device deviceInStore = storager.queryVideoDevice(device.getDeviceId());
-			if (!StringUtils.isEmpty(device.getName())) deviceInStore.setName(device.getName());
-			if (!StringUtils.isEmpty(device.getCharset())) deviceInStore.setCharset(device.getCharset());
-			if (!StringUtils.isEmpty(device.getMediaServerId())) deviceInStore.setMediaServerId(device.getMediaServerId());
-
-			if ((deviceInStore.getSubscribeCycleForCatalog() <=0 && device.getSubscribeCycleForCatalog() > 0)
-					|| deviceInStore.getSubscribeCycleForCatalog() != device.getSubscribeCycleForCatalog()) {
-				deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
-				// 寮�鍚闃�
-				deviceService.addCatalogSubscribe(deviceInStore);
+			if (!StringUtils.isEmpty(device.getName())) {
+				deviceInStore.setName(device.getName());
 			}
-			if (deviceInStore.getSubscribeCycleForCatalog() > 0 && device.getSubscribeCycleForCatalog() <= 0) {
-				deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
-				// 鍙栨秷璁㈤槄
-				deviceService.removeCatalogSubscribe(deviceInStore);
+			if (!StringUtils.isEmpty(device.getCharset())) {
+				deviceInStore.setCharset(device.getCharset());
+			}
+			if (!StringUtils.isEmpty(device.getMediaServerId())) {
+				deviceInStore.setMediaServerId(device.getMediaServerId());
 			}
 
-			storager.updateDevice(deviceInStore);
-			cmder.deviceInfoQuery(deviceInStore);
+			//  鐩綍璁㈤槄鐩稿叧鐨勪俊鎭�
+			if (device.getSubscribeCycleForCatalog() > 0) {
+				if (deviceInStore.getSubscribeCycleForCatalog() == 0 || deviceInStore.getSubscribeCycleForCatalog() != device.getSubscribeCycleForCatalog()) {
+					deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
+					// 寮�鍚闃�
+					deviceService.addCatalogSubscribe(deviceInStore);
+				}
+			}else if (device.getSubscribeCycleForCatalog() == 0) {
+				if (deviceInStore.getSubscribeCycleForCatalog() != 0) {
+					deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
+					// 鍙栨秷璁㈤槄
+					deviceService.removeCatalogSubscribe(deviceInStore);
+				}
+			}
+
+			// 绉诲姩浣嶇疆璁㈤槄鐩稿叧鐨勪俊鎭�
+			if (device.getSubscribeCycleForMobilePosition() > 0) {
+				if (deviceInStore.getSubscribeCycleForMobilePosition() == 0 || deviceInStore.getSubscribeCycleForMobilePosition() != device.getSubscribeCycleForMobilePosition()) {
+					deviceInStore.setMobilePositionSubmissionInterval(device.getMobilePositionSubmissionInterval());
+					deviceInStore.setSubscribeCycleForMobilePosition(device.getSubscribeCycleForMobilePosition());
+					// 寮�鍚闃�
+					deviceService.addMobilePositionSubscribe(deviceInStore);
+				}
+			}else if (device.getSubscribeCycleForMobilePosition() == 0) {
+				if (deviceInStore.getSubscribeCycleForMobilePosition() != 0) {
+					// 鍙栨秷璁㈤槄
+					deviceService.removeMobilePositionSubscribe(deviceInStore);
+				}
+			}
+
+			// TODO 鎶ヨ璁㈤槄鐩稿叧鐨勪俊鎭�
+
+			storager.updateDevice(device);
+			cmder.deviceInfoQuery(device);
 		}
 		WVPResult<String> result = new WVPResult<>();
 		result.setCode(0);
@@ -434,9 +451,47 @@
 		return result;
 	}
 
-	@GetMapping("/{deviceId}/tree")
-	@ApiOperation(value = "閫氶亾鏍戝舰缁撴瀯", notes = "閫氶亾鏍戝舰缁撴瀯")
-	public WVPResult<List<DeviceChannelTree>> tree(@PathVariable String deviceId) {
-		return WVPResult.Data(storager.tree(deviceId));
+
+	@GetMapping("/{deviceId}/sync_status")
+	@ApiOperation(value = "鑾峰彇閫氶亾鍚屾杩涘害", notes = "鑾峰彇閫氶亾鍚屾杩涘害")
+	public WVPResult<SyncStatus> getSyncStatus(@PathVariable String deviceId) {
+		SyncStatus channelSyncStatus = deviceService.getChannelSyncStatus(deviceId);
+		WVPResult<SyncStatus> wvpResult = new WVPResult<>();
+		if (channelSyncStatus == null) {
+			wvpResult.setCode(-1);
+			wvpResult.setMsg("鍚屾灏氭湭寮�濮�");
+		}else {
+			wvpResult.setCode(0);
+			wvpResult.setData(channelSyncStatus);
+			if (channelSyncStatus.getErrorMsg() != null) {
+				wvpResult.setMsg(channelSyncStatus.getErrorMsg());
+			}
+		}
+		return wvpResult;
+	}
+
+	@GetMapping("/{deviceId}/subscribe_info")
+	@ApiOperation(value = "鑾峰彇璁惧鐨勮闃呯姸鎬�", notes = "鑾峰彇璁惧鐨勮闃呯姸鎬�")
+	public WVPResult<Map<String, String>> getSubscribeInfo(@PathVariable String deviceId) {
+		Set<String> allKeys = dynamicTask.getAllKeys();
+		Map<String, String> dialogStateMap = new HashMap<>();
+		for (String key : allKeys) {
+			if (key.startsWith(deviceId)) {
+				ISubscribeTask subscribeTask = (ISubscribeTask)dynamicTask.get(key);
+				DialogState dialogState = subscribeTask.getDialogState();
+				if (dialogState == null) {
+					continue;
+				}
+				if (subscribeTask instanceof CatalogSubscribeTask) {
+					dialogStateMap.put("catalog", dialogState.toString());
+				}else if (subscribeTask instanceof MobilePositionSubscribeTask) {
+					dialogStateMap.put("mobilePosition", dialogState.toString());
+				}
+			}
+		}
+		WVPResult<Map<String, String>> wvpResult = new WVPResult<>();
+		wvpResult.setCode(0);
+		wvpResult.setData(dialogStateMap);
+		return wvpResult;
 	}
 }

--
Gitblit v1.8.0