DeviceServiceImpl.java 27 KB

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