DeviceServiceImpl.java 25 KB

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