DeviceServiceImpl.java 27 KB

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