DeviceServiceImpl.java 26 KB

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