DeviceServiceImpl.java 26 KB

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