DeviceServiceImpl.java 25 KB

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