DeviceServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.common.VideoManagerConstants;
  3. import com.genersoft.iot.vmp.conf.DynamicTask;
  4. import com.genersoft.iot.vmp.conf.UserSetting;
  5. import com.genersoft.iot.vmp.gb28181.bean.*;
  6. import com.genersoft.iot.vmp.gb28181.session.AudioBroadcastManager;
  7. import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
  8. import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
  9. import com.genersoft.iot.vmp.gb28181.task.impl.CatalogSubscribeTask;
  10. import com.genersoft.iot.vmp.gb28181.task.impl.MobilePositionSubscribeTask;
  11. import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
  12. import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd.CatalogResponseMessageHandler;
  13. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  14. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  15. import com.genersoft.iot.vmp.service.IDeviceChannelService;
  16. import com.genersoft.iot.vmp.service.IDeviceService;
  17. import com.genersoft.iot.vmp.service.IMediaServerService;
  18. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  19. import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
  20. import com.genersoft.iot.vmp.storager.dao.DeviceMapper;
  21. import com.genersoft.iot.vmp.storager.dao.PlatformChannelMapper;
  22. import com.genersoft.iot.vmp.utils.DateUtil;
  23. import com.genersoft.iot.vmp.vmanager.bean.BaseTree;
  24. import com.genersoft.iot.vmp.vmanager.bean.ResourceBaceInfo;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.transaction.TransactionDefinition;
  31. import org.springframework.transaction.TransactionStatus;
  32. import org.springframework.util.ObjectUtils;
  33. import javax.sip.InvalidArgumentException;
  34. import javax.sip.SipException;
  35. import java.text.ParseException;
  36. import java.time.Instant;
  37. import java.util.*;
  38. import java.util.concurrent.TimeUnit;
  39. /**
  40. * 设备业务(目录订阅)
  41. */
  42. @Service
  43. public class DeviceServiceImpl implements IDeviceService {
  44. private final static Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);
  45. @Autowired
  46. private DynamicTask dynamicTask;
  47. @Autowired
  48. private ISIPCommander sipCommander;
  49. @Autowired
  50. private CatalogResponseMessageHandler catalogResponseMessageHandler;
  51. @Autowired
  52. private IRedisCatchStorage redisCatchStorage;
  53. @Autowired
  54. private DeviceMapper deviceMapper;
  55. @Autowired
  56. private PlatformChannelMapper platformChannelMapper;
  57. @Autowired
  58. private IDeviceChannelService deviceChannelService;
  59. @Autowired
  60. private DeviceChannelMapper deviceChannelMapper;
  61. @Autowired
  62. DataSourceTransactionManager dataSourceTransactionManager;
  63. @Autowired
  64. TransactionDefinition transactionDefinition;
  65. @Autowired
  66. private UserSetting userSetting;
  67. @Autowired
  68. private ISIPCommander commander;
  69. @Autowired
  70. private VideoStreamSessionManager streamSession;
  71. @Autowired
  72. private IMediaServerService mediaServerService;
  73. @Autowired
  74. private AudioBroadcastManager audioBroadcastManager;
  75. @Autowired
  76. private ZLMRESTfulUtils zlmresTfulUtils;
  77. @Override
  78. public void online(Device device) {
  79. logger.info("[设备上线] deviceId:{}->{}:{}", device.getDeviceId(), device.getIp(), device.getPort());
  80. Device deviceInRedis = redisCatchStorage.getDevice(device.getDeviceId());
  81. Device deviceInDb = deviceMapper.getDeviceByDeviceId(device.getDeviceId());
  82. String now = DateUtil.getNow();
  83. if (deviceInRedis != null && deviceInDb == null) {
  84. // redis 存在脏数据
  85. redisCatchStorage.clearCatchByDeviceId(device.getDeviceId());
  86. }
  87. device.setUpdateTime(now);
  88. if (device.getKeepaliveIntervalTime() == 0) {
  89. // 默认心跳间隔60
  90. device.setKeepaliveIntervalTime(60);
  91. }
  92. // 第一次上线 或则设备之前是离线状态--进行通道同步和设备信息查询
  93. if (device.getCreateTime() == null) {
  94. device.setOnline(1);
  95. device.setCreateTime(now);
  96. logger.info("[设备上线,首次注册]: {},查询设备信息以及通道信息", device.getDeviceId());
  97. deviceMapper.add(device);
  98. redisCatchStorage.updateDevice(device);
  99. try {
  100. commander.deviceInfoQuery(device);
  101. } catch (InvalidArgumentException | SipException | ParseException e) {
  102. logger.error("[命令发送失败] 查询设备信息: {}", e.getMessage());
  103. }
  104. sync(device);
  105. }else {
  106. if(device.getOnline() == 0){
  107. device.setOnline(1);
  108. device.setCreateTime(now);
  109. deviceMapper.update(device);
  110. redisCatchStorage.updateDevice(device);
  111. if (userSetting.getSyncChannelOnDeviceOnline()) {
  112. logger.info("[设备上线,离线状态下重新注册]: {},查询设备信息以及通道信息", device.getDeviceId());
  113. try {
  114. commander.deviceInfoQuery(device);
  115. } catch (InvalidArgumentException | SipException | ParseException e) {
  116. logger.error("[命令发送失败] 查询设备信息: {}", e.getMessage());
  117. }
  118. sync(device);
  119. // TODO 如果设备下的通道级联到了其他平台,那么需要发送事件或者notify给上级平台
  120. }
  121. }else {
  122. if (deviceChannelMapper.queryAllChannels(device.getDeviceId()).size() == 0) {
  123. logger.info("[设备上线]: {},通道数为0,查询通道信息", device.getDeviceId());
  124. sync(device);
  125. }
  126. deviceMapper.update(device);
  127. redisCatchStorage.updateDevice(device);
  128. }
  129. }
  130. // 上线添加订阅
  131. if (device.getSubscribeCycleForCatalog() > 0) {
  132. // 查询在线设备那些开启了订阅,为设备开启定时的目录订阅
  133. addCatalogSubscribe(device);
  134. }
  135. if (device.getSubscribeCycleForMobilePosition() > 0) {
  136. addMobilePositionSubscribe(device);
  137. }
  138. // 刷新过期任务
  139. String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + device.getDeviceId();
  140. // 如果第一次注册那么必须在60 * 3时间内收到一个心跳,否则设备离线
  141. dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId()), device.getKeepaliveIntervalTime() * 1000 * 3);
  142. }
  143. @Override
  144. public void offline(String deviceId) {
  145. logger.error("[设备离线], device:{}", deviceId);
  146. Device device = deviceMapper.getDeviceByDeviceId(deviceId);
  147. if (device == null) {
  148. return;
  149. }
  150. String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + deviceId;
  151. dynamicTask.stop(registerExpireTaskKey);
  152. device.setOnline(0);
  153. redisCatchStorage.updateDevice(device);
  154. deviceMapper.update(device);
  155. //进行通道离线
  156. // deviceChannelMapper.offlineByDeviceId(deviceId);
  157. // 离线释放所有ssrc
  158. List<SsrcTransaction> ssrcTransactions = streamSession.getSsrcTransactionForAll(deviceId, null, null, null);
  159. if (ssrcTransactions != null && ssrcTransactions.size() > 0) {
  160. for (SsrcTransaction ssrcTransaction : ssrcTransactions) {
  161. mediaServerService.releaseSsrc(ssrcTransaction.getMediaServerId(), ssrcTransaction.getSsrc());
  162. mediaServerService.closeRTPServer(ssrcTransaction.getMediaServerId(), ssrcTransaction.getStream());
  163. streamSession.remove(deviceId, ssrcTransaction.getChannelId(), ssrcTransaction.getStream());
  164. }
  165. }
  166. // 移除订阅
  167. removeCatalogSubscribe(device);
  168. removeMobilePositionSubscribe(device);
  169. List<AudioBroadcastCatch> audioBroadcastCatches = audioBroadcastManager.get(deviceId);
  170. if (audioBroadcastCatches.size() > 0) {
  171. for (AudioBroadcastCatch audioBroadcastCatch : audioBroadcastCatches) {
  172. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(deviceId, audioBroadcastCatch.getChannelId(), null, null);
  173. if (sendRtpItem != null) {
  174. redisCatchStorage.deleteSendRTPServer(deviceId, sendRtpItem.getChannelId(), null, null);
  175. MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  176. Map<String, Object> param = new HashMap<>();
  177. param.put("vhost", "__defaultVhost__");
  178. param.put("app", sendRtpItem.getApp());
  179. param.put("stream", sendRtpItem.getStream());
  180. zlmresTfulUtils.stopSendRtp(mediaInfo, param);
  181. }
  182. audioBroadcastManager.del(deviceId, audioBroadcastCatch.getChannelId());
  183. }
  184. }
  185. }
  186. @Override
  187. public boolean addCatalogSubscribe(Device device) {
  188. if (device == null || device.getSubscribeCycleForCatalog() < 0) {
  189. return false;
  190. }
  191. logger.info("[添加目录订阅] 设备{}", device.getDeviceId());
  192. // 添加目录订阅
  193. CatalogSubscribeTask catalogSubscribeTask = new CatalogSubscribeTask(device, sipCommander, dynamicTask);
  194. // 刷新订阅
  195. int subscribeCycleForCatalog = Math.max(device.getSubscribeCycleForCatalog(),30);
  196. // 设置最小值为30
  197. dynamicTask.startCron(device.getDeviceId() + "catalog", catalogSubscribeTask, (subscribeCycleForCatalog -1) * 1000);
  198. return true;
  199. }
  200. @Override
  201. public boolean removeCatalogSubscribe(Device device) {
  202. if (device == null || device.getSubscribeCycleForCatalog() < 0) {
  203. return false;
  204. }
  205. logger.info("[移除目录订阅]: {}", device.getDeviceId());
  206. String taskKey = device.getDeviceId() + "catalog";
  207. if (device.getOnline() == 1) {
  208. Runnable runnable = dynamicTask.get(taskKey);
  209. if (runnable instanceof ISubscribeTask) {
  210. ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
  211. subscribeTask.stop();
  212. }
  213. }
  214. dynamicTask.stop(taskKey);
  215. return true;
  216. }
  217. @Override
  218. public boolean addMobilePositionSubscribe(Device device) {
  219. if (device == null || device.getSubscribeCycleForMobilePosition() < 0) {
  220. return false;
  221. }
  222. logger.info("[添加移动位置订阅] 设备{}", device.getDeviceId());
  223. // 添加目录订阅
  224. MobilePositionSubscribeTask mobilePositionSubscribeTask = new MobilePositionSubscribeTask(device, sipCommander, dynamicTask);
  225. // 设置最小值为30
  226. int subscribeCycleForCatalog = Math.max(device.getSubscribeCycleForMobilePosition(),30);
  227. // 刷新订阅
  228. dynamicTask.startCron(device.getDeviceId() + "mobile_position" , mobilePositionSubscribeTask, (subscribeCycleForCatalog) * 1000);
  229. return true;
  230. }
  231. @Override
  232. public boolean removeMobilePositionSubscribe(Device device) {
  233. if (device == null || device.getSubscribeCycleForCatalog() < 0) {
  234. return false;
  235. }
  236. logger.info("[移除移动位置订阅]: {}", device.getDeviceId());
  237. String taskKey = device.getDeviceId() + "mobile_position";
  238. if (device.getOnline() == 1) {
  239. Runnable runnable = dynamicTask.get(taskKey);
  240. if (runnable instanceof ISubscribeTask) {
  241. ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
  242. subscribeTask.stop();
  243. }
  244. }
  245. dynamicTask.stop(taskKey);
  246. return true;
  247. }
  248. @Override
  249. public SyncStatus getChannelSyncStatus(String deviceId) {
  250. return catalogResponseMessageHandler.getChannelSyncProgress(deviceId);
  251. }
  252. @Override
  253. public Boolean isSyncRunning(String deviceId) {
  254. return catalogResponseMessageHandler.isSyncRunning(deviceId);
  255. }
  256. @Override
  257. public void sync(Device device) {
  258. if (catalogResponseMessageHandler.isSyncRunning(device.getDeviceId())) {
  259. logger.info("开启同步时发现同步已经存在");
  260. return;
  261. }
  262. int sn = (int)((Math.random()*9+1)*100000);
  263. catalogResponseMessageHandler.setChannelSyncReady(device, sn);
  264. try {
  265. sipCommander.catalogQuery(device, sn, event -> {
  266. String errorMsg = String.format("同步通道失败,错误码: %s, %s", event.statusCode, event.msg);
  267. catalogResponseMessageHandler.setChannelSyncEnd(device.getDeviceId(), errorMsg);
  268. });
  269. } catch (SipException | InvalidArgumentException | ParseException e) {
  270. logger.error("[同步通道], 信令发送失败:{}", e.getMessage() );
  271. String errorMsg = String.format("同步通道失败,信令发送失败: %s", e.getMessage());
  272. catalogResponseMessageHandler.setChannelSyncEnd(device.getDeviceId(), errorMsg);
  273. }
  274. }
  275. @Override
  276. public Device getDevice(String deviceId) {
  277. Device device = redisCatchStorage.getDevice(deviceId);
  278. if (device == null) {
  279. device = deviceMapper.getDeviceByDeviceId(deviceId);
  280. if (device != null) {
  281. redisCatchStorage.updateDevice(device);
  282. }
  283. }
  284. return device;
  285. }
  286. @Override
  287. public List<Device> getAllOnlineDevice() {
  288. return deviceMapper.getOnlineDevices();
  289. }
  290. @Override
  291. public boolean expire(Device device) {
  292. Instant registerTimeDate = Instant.from(DateUtil.formatter.parse(device.getRegisterTime()));
  293. Instant expireInstant = registerTimeDate.plusMillis(TimeUnit.SECONDS.toMillis(device.getExpires()));
  294. return expireInstant.isBefore(Instant.now());
  295. }
  296. @Override
  297. public void checkDeviceStatus(Device device) {
  298. if (device == null || device.getOnline() == 0) {
  299. return;
  300. }
  301. try {
  302. sipCommander.deviceStatusQuery(device, null);
  303. } catch (InvalidArgumentException | SipException | ParseException e) {
  304. logger.error("[命令发送失败] 设备状态查询: {}", e.getMessage());
  305. }
  306. }
  307. @Override
  308. public Device getDeviceByHostAndPort(String host, int port) {
  309. return deviceMapper.getDeviceByHostAndPort(host, port);
  310. }
  311. @Override
  312. public void updateDevice(Device device) {
  313. String now = DateUtil.getNow();
  314. device.setUpdateTime(now);
  315. device.setCharset(device.getCharset().toUpperCase());
  316. device.setUpdateTime(DateUtil.getNow());
  317. if (deviceMapper.update(device) > 0) {
  318. redisCatchStorage.updateDevice(device);
  319. }
  320. }
  321. /**
  322. * 更新通道坐标系
  323. */
  324. private void updateDeviceChannelGeoCoordSys(Device device) {
  325. List<DeviceChannel> deviceChannels = deviceChannelMapper.getAllChannelWithCoordinate(device.getDeviceId());
  326. if (deviceChannels.size() > 0) {
  327. List<DeviceChannel> deviceChannelsForStore = new ArrayList<>();
  328. for (DeviceChannel deviceChannel : deviceChannels) {
  329. deviceChannelsForStore.add(deviceChannelService.updateGps(deviceChannel, device));
  330. }
  331. deviceChannelService.updateChannels(device.getDeviceId(), deviceChannelsForStore);
  332. }
  333. }
  334. @Override
  335. public List<BaseTree<DeviceChannel>> queryVideoDeviceTree(String deviceId, String parentId, boolean onlyCatalog) {
  336. Device device = deviceMapper.getDeviceByDeviceId(deviceId);
  337. if (device == null) {
  338. return null;
  339. }
  340. if (parentId == null || parentId.equals(deviceId)) {
  341. // 字根节点开始查询
  342. List<DeviceChannel> rootNodes = getRootNodes(deviceId, TreeType.CIVIL_CODE.equals(device.getTreeType()), true, !onlyCatalog);
  343. return transportChannelsToTree(rootNodes, "");
  344. }
  345. if (TreeType.CIVIL_CODE.equals(device.getTreeType())) {
  346. if (parentId.length()%2 != 0) {
  347. return null;
  348. }
  349. // 使用行政区划展示树
  350. // if (parentId.length() > 10) {
  351. // // TODO 可能是行政区划与业务分组混杂的情形
  352. // return null;
  353. // }
  354. if (parentId.length() == 10 ) {
  355. if (onlyCatalog) {
  356. return null;
  357. }
  358. // parentId为行业编码, 其下不会再有行政区划
  359. List<DeviceChannel> channels = deviceChannelMapper.getChannelsByCivilCode(deviceId, parentId);
  360. List<BaseTree<DeviceChannel>> trees = transportChannelsToTree(channels, parentId);
  361. return trees;
  362. }
  363. // 查询其下的行政区划和摄像机
  364. List<DeviceChannel> channelsForCivilCode = deviceChannelMapper.getChannelsWithCivilCodeAndLength(deviceId, parentId, parentId.length() + 2);
  365. if (!onlyCatalog) {
  366. List<DeviceChannel> channels = deviceChannelMapper.getChannelsByCivilCode(deviceId, parentId);
  367. for(DeviceChannel channel : channels) {
  368. boolean flag = false;
  369. for(DeviceChannel deviceChannel : channelsForCivilCode) {
  370. if(channel.getChannelId().equals(deviceChannel.getChannelId())) {
  371. flag = true;
  372. }
  373. }
  374. if(!flag) {
  375. channelsForCivilCode.add(channel);
  376. }
  377. }
  378. }
  379. List<BaseTree<DeviceChannel>> trees = transportChannelsToTree(channelsForCivilCode, parentId);
  380. return trees;
  381. }
  382. // 使用业务分组展示树
  383. if (TreeType.BUSINESS_GROUP.equals(device.getTreeType())) {
  384. if (parentId.length() < 14 ) {
  385. return null;
  386. }
  387. List<DeviceChannel> deviceChannels = deviceChannelMapper.queryChannels(deviceId, parentId, null, null, null,null);
  388. List<BaseTree<DeviceChannel>> trees = transportChannelsToTree(deviceChannels, parentId);
  389. return trees;
  390. }
  391. return null;
  392. }
  393. @Override
  394. public List<DeviceChannel> queryVideoDeviceInTreeNode(String deviceId, String parentId) {
  395. Device device = deviceMapper.getDeviceByDeviceId(deviceId);
  396. if (device == null) {
  397. return null;
  398. }
  399. if (parentId == null || parentId.equals(deviceId)) {
  400. // 字根节点开始查询
  401. List<DeviceChannel> rootNodes = getRootNodes(deviceId, TreeType.CIVIL_CODE.equals(device.getTreeType()), false, true);
  402. return rootNodes;
  403. }
  404. if (TreeType.CIVIL_CODE.equals(device.getTreeType())) {
  405. if (parentId.length()%2 != 0) {
  406. return null;
  407. }
  408. // 使用行政区划展示树
  409. if (parentId.length() > 10) {
  410. // TODO 可能是行政区划与业务分组混杂的情形
  411. return null;
  412. }
  413. if (parentId.length() == 10 ) {
  414. // parentId为行业编码, 其下不会再有行政区划
  415. List<DeviceChannel> channels = deviceChannelMapper.getChannelsByCivilCode(deviceId, parentId);
  416. return channels;
  417. }
  418. // 查询其下的行政区划和摄像机
  419. List<DeviceChannel> channels = deviceChannelMapper.getChannelsByCivilCode(deviceId, parentId);
  420. return channels;
  421. }
  422. // 使用业务分组展示树
  423. if (TreeType.BUSINESS_GROUP.equals(device.getTreeType())) {
  424. if (parentId.length() < 14 ) {
  425. return null;
  426. }
  427. List<DeviceChannel> deviceChannels = deviceChannelMapper.queryChannels(deviceId, parentId, null, null, null,null);
  428. return deviceChannels;
  429. }
  430. return null;
  431. }
  432. private List<BaseTree<DeviceChannel>> transportChannelsToTree(List<DeviceChannel> channels, String parentId) {
  433. if (channels == null) {
  434. return null;
  435. }
  436. List<BaseTree<DeviceChannel>> treeNotes = new ArrayList<>();
  437. if (channels.size() == 0) {
  438. return treeNotes;
  439. }
  440. for (DeviceChannel channel : channels) {
  441. BaseTree<DeviceChannel> node = new BaseTree<>();
  442. node.setId(channel.getChannelId());
  443. node.setDeviceId(channel.getDeviceId());
  444. node.setName(channel.getName());
  445. node.setPid(parentId);
  446. node.setBasicData(channel);
  447. node.setParent(false);
  448. if (channel.getChannelId().length() > 8) {
  449. String gbCodeType = channel.getChannelId().substring(10, 13);
  450. node.setParent(gbCodeType.equals(ChannelIdType.BUSINESS_GROUP) || gbCodeType.equals(ChannelIdType.VIRTUAL_ORGANIZATION) );
  451. }else {
  452. node.setParent(true);
  453. }
  454. treeNotes.add(node);
  455. }
  456. Collections.sort(treeNotes);
  457. return treeNotes;
  458. }
  459. private List<DeviceChannel> getRootNodes(String deviceId, boolean isCivilCode, boolean haveCatalog, boolean haveChannel) {
  460. if (!haveCatalog && !haveChannel) {
  461. return null;
  462. }
  463. List<DeviceChannel> result = new ArrayList<>();
  464. if (isCivilCode) {
  465. // 使用行政区划
  466. Integer length= deviceChannelMapper.getChannelMinLength(deviceId);
  467. if (length == null) {
  468. return null;
  469. }
  470. if (length <= 10) {
  471. if (haveCatalog) {
  472. List<DeviceChannel> provinceNode = deviceChannelMapper.getChannelsWithCivilCodeAndLength(deviceId, null, length);
  473. if (provinceNode != null && provinceNode.size() > 0) {
  474. result.addAll(provinceNode);
  475. }
  476. }
  477. if (haveChannel) {
  478. // 查询那些civilCode不在通道中的不规范通道,放置在根目录
  479. List<DeviceChannel> nonstandardNode = deviceChannelMapper.getChannelWithoutCiviCode(deviceId);
  480. if (nonstandardNode != null && nonstandardNode.size() > 0) {
  481. result.addAll(nonstandardNode);
  482. }
  483. }
  484. }else {
  485. if (haveChannel) {
  486. List<DeviceChannel> deviceChannels = deviceChannelMapper.queryChannels(deviceId, null, null, null, null,null);
  487. if (deviceChannels != null && deviceChannels.size() > 0) {
  488. result.addAll(deviceChannels);
  489. }
  490. }
  491. }
  492. }else {
  493. // 使用业务分组+虚拟组织
  494. // 只获取业务分组
  495. List<DeviceChannel> deviceChannels = deviceChannelMapper.getBusinessGroups(deviceId, ChannelIdType.BUSINESS_GROUP);
  496. if (deviceChannels != null && deviceChannels.size() > 0) {
  497. result.addAll(deviceChannels);
  498. }
  499. }
  500. return result;
  501. }
  502. @Override
  503. public boolean isExist(String deviceId) {
  504. return deviceMapper.getDeviceByDeviceId(deviceId) != null;
  505. }
  506. @Override
  507. public void addDevice(Device device) {
  508. device.setOnline(0);
  509. device.setCreateTime(DateUtil.getNow());
  510. device.setUpdateTime(DateUtil.getNow());
  511. deviceMapper.addCustomDevice(device);
  512. }
  513. @Override
  514. public void updateCustomDevice(Device device) {
  515. Device deviceInStore = deviceMapper.getDeviceByDeviceId(device.getDeviceId());
  516. if (deviceInStore == null) {
  517. logger.warn("更新设备时未找到设备信息");
  518. return;
  519. }
  520. if (!ObjectUtils.isEmpty(device.getName())) {
  521. deviceInStore.setName(device.getName());
  522. }
  523. if (!ObjectUtils.isEmpty(device.getCharset())) {
  524. deviceInStore.setCharset(device.getCharset());
  525. }
  526. if (!ObjectUtils.isEmpty(device.getMediaServerId())) {
  527. deviceInStore.setMediaServerId(device.getMediaServerId());
  528. }
  529. deviceInStore.setSdpIp(device.getSdpIp());
  530. deviceInStore.setCharset(device.getCharset());
  531. deviceInStore.setTreeType(device.getTreeType());
  532. // 目录订阅相关的信息
  533. if (device.getSubscribeCycleForCatalog() > 0) {
  534. if (deviceInStore.getSubscribeCycleForCatalog() == 0 || deviceInStore.getSubscribeCycleForCatalog() != device.getSubscribeCycleForCatalog()) {
  535. deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
  536. // 开启订阅
  537. addCatalogSubscribe(deviceInStore);
  538. }
  539. }else if (device.getSubscribeCycleForCatalog() == 0) {
  540. if (deviceInStore.getSubscribeCycleForCatalog() != 0) {
  541. deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
  542. // 取消订阅
  543. removeCatalogSubscribe(deviceInStore);
  544. }
  545. }
  546. // 移动位置订阅相关的信息
  547. if (device.getSubscribeCycleForMobilePosition() > 0) {
  548. if (deviceInStore.getSubscribeCycleForMobilePosition() == 0 || deviceInStore.getSubscribeCycleForMobilePosition() != device.getSubscribeCycleForMobilePosition()) {
  549. deviceInStore.setMobilePositionSubmissionInterval(device.getMobilePositionSubmissionInterval());
  550. deviceInStore.setSubscribeCycleForMobilePosition(device.getSubscribeCycleForMobilePosition());
  551. // 开启订阅
  552. addMobilePositionSubscribe(deviceInStore);
  553. }
  554. }else if (device.getSubscribeCycleForMobilePosition() == 0) {
  555. if (deviceInStore.getSubscribeCycleForMobilePosition() != 0) {
  556. // 取消订阅
  557. removeMobilePositionSubscribe(deviceInStore);
  558. }
  559. }
  560. // 坐标系变化,需要重新计算GCJ02坐标和WGS84坐标
  561. if (!deviceInStore.getGeoCoordSys().equals(device.getGeoCoordSys())) {
  562. updateDeviceChannelGeoCoordSys(device);
  563. }
  564. // 更新redis
  565. redisCatchStorage.updateDevice(device);
  566. deviceMapper.updateCustom(device);
  567. }
  568. @Override
  569. public boolean delete(String deviceId) {
  570. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  571. boolean result = false;
  572. try {
  573. platformChannelMapper.delChannelForDeviceId(deviceId);
  574. deviceChannelMapper.cleanChannelsByDeviceId(deviceId);
  575. if ( deviceMapper.del(deviceId) < 0 ) {
  576. //事务回滚
  577. dataSourceTransactionManager.rollback(transactionStatus);
  578. }
  579. result = true;
  580. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  581. }catch (Exception e) {
  582. dataSourceTransactionManager.rollback(transactionStatus);
  583. }
  584. return result;
  585. }
  586. @Override
  587. public ResourceBaceInfo getOverview() {
  588. return deviceMapper.getOverview();
  589. }
  590. }