DeviceChannelServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.baomidou.dynamic.datasource.annotation.DS;
  4. import com.genersoft.iot.vmp.common.InviteInfo;
  5. import com.genersoft.iot.vmp.common.InviteSessionType;
  6. import com.genersoft.iot.vmp.conf.UserSetting;
  7. import com.genersoft.iot.vmp.gb28181.bean.Device;
  8. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  9. import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
  10. import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
  11. import com.genersoft.iot.vmp.gb28181.utils.Coordtransform;
  12. import com.genersoft.iot.vmp.service.IDeviceChannelService;
  13. import com.genersoft.iot.vmp.service.IInviteStreamService;
  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.DeviceMobilePositionMapper;
  18. import com.genersoft.iot.vmp.utils.DateUtil;
  19. import com.genersoft.iot.vmp.vmanager.bean.ResourceBaseInfo;
  20. import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.util.ObjectUtils;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.concurrent.CopyOnWriteArrayList;
  31. /**
  32. * @author lin
  33. */
  34. @Service
  35. @DS("master")
  36. public class DeviceChannelServiceImpl implements IDeviceChannelService {
  37. private final static Logger logger = LoggerFactory.getLogger(DeviceChannelServiceImpl.class);
  38. @Autowired
  39. private EventPublisher eventPublisher;
  40. @Autowired
  41. private IInviteStreamService inviteStreamService;
  42. @Autowired
  43. private DeviceChannelMapper channelMapper;
  44. @Autowired
  45. private DeviceMapper deviceMapper;
  46. @Autowired
  47. private DeviceMobilePositionMapper deviceMobilePositionMapper;
  48. @Autowired
  49. private UserSetting userSetting;
  50. @Autowired
  51. private IRedisCatchStorage redisCatchStorage;
  52. @Override
  53. public DeviceChannel updateGps(DeviceChannel deviceChannel, Device device) {
  54. if (deviceChannel.getLongitude()*deviceChannel.getLatitude() > 0) {
  55. if (device == null) {
  56. device = deviceMapper.getDeviceByDeviceId(deviceChannel.getDeviceId());
  57. }
  58. if ("WGS84".equals(device.getGeoCoordSys())) {
  59. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  60. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  61. Double[] position = Coordtransform.WGS84ToGCJ02(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  62. deviceChannel.setLongitudeGcj02(position[0]);
  63. deviceChannel.setLatitudeGcj02(position[1]);
  64. }else if ("GCJ02".equals(device.getGeoCoordSys())) {
  65. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  66. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  67. Double[] position = Coordtransform.GCJ02ToWGS84(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  68. deviceChannel.setLongitudeWgs84(position[0]);
  69. deviceChannel.setLatitudeWgs84(position[1]);
  70. }else {
  71. deviceChannel.setLongitudeGcj02(0.00);
  72. deviceChannel.setLatitudeGcj02(0.00);
  73. deviceChannel.setLongitudeWgs84(0.00);
  74. deviceChannel.setLatitudeWgs84(0.00);
  75. }
  76. }else {
  77. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  78. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  79. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  80. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  81. }
  82. return deviceChannel;
  83. }
  84. @Override
  85. public void updateChannel(String deviceId, DeviceChannel channel) {
  86. String channelId = channel.getChannelId();
  87. channel.setDeviceId(deviceId);
  88. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
  89. if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
  90. channel.setStreamId(inviteInfo.getStreamInfo().getStream());
  91. }
  92. String now = DateUtil.getNow();
  93. channel.setUpdateTime(now);
  94. DeviceChannel deviceChannel = channelMapper.queryChannel(deviceId, channelId);
  95. channel = updateGps(channel, null);
  96. if (deviceChannel == null) {
  97. channel.setCreateTime(now);
  98. channelMapper.add(channel);
  99. }else {
  100. channelMapper.update(channel);
  101. }
  102. channelMapper.updateChannelSubCount(deviceId,channel.getParentId());
  103. }
  104. @Override
  105. public int updateChannels(String deviceId, List<DeviceChannel> channels) {
  106. List<DeviceChannel> addChannels = new ArrayList<>();
  107. List<DeviceChannel> updateChannels = new ArrayList<>();
  108. HashMap<String, DeviceChannel> channelsInStore = new HashMap<>();
  109. Device device = deviceMapper.getDeviceByDeviceId(deviceId);
  110. if (channels != null && channels.size() > 0) {
  111. List<DeviceChannel> channelList = channelMapper.queryChannels(deviceId, null, null, null, null,null);
  112. if (channelList.size() == 0) {
  113. for (DeviceChannel channel : channels) {
  114. channel.setDeviceId(deviceId);
  115. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channel.getChannelId());
  116. if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
  117. channel.setStreamId(inviteInfo.getStreamInfo().getStream());
  118. }
  119. String now = DateUtil.getNow();
  120. channel.setUpdateTime(now);
  121. channel.setCreateTime(now);
  122. channel = updateGps(channel, device);
  123. addChannels.add(channel);
  124. }
  125. }else {
  126. for (DeviceChannel deviceChannel : channelList) {
  127. channelsInStore.put(deviceChannel.getChannelId(), deviceChannel);
  128. }
  129. for (DeviceChannel channel : channels) {
  130. channel.setDeviceId(deviceId);
  131. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channel.getChannelId());
  132. if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
  133. channel.setStreamId(inviteInfo.getStreamInfo().getStream());
  134. }
  135. String now = DateUtil.getNow();
  136. channel.setUpdateTime(now);
  137. channel = updateGps(channel, device);
  138. if (channelsInStore.get(channel.getChannelId()) != null) {
  139. updateChannels.add(channel);
  140. }else {
  141. addChannels.add(channel);
  142. channel.setCreateTime(now);
  143. }
  144. }
  145. }
  146. int limitCount = 50;
  147. if (addChannels.size() > 0) {
  148. if (addChannels.size() > limitCount) {
  149. for (int i = 0; i < addChannels.size(); i += limitCount) {
  150. int toIndex = i + limitCount;
  151. if (i + limitCount > addChannels.size()) {
  152. toIndex = addChannels.size();
  153. }
  154. channelMapper.batchAdd(addChannels.subList(i, toIndex));
  155. }
  156. }else {
  157. channelMapper.batchAdd(addChannels);
  158. }
  159. }
  160. if (updateChannels.size() > 0) {
  161. if (updateChannels.size() > limitCount) {
  162. for (int i = 0; i < updateChannels.size(); i += limitCount) {
  163. int toIndex = i + limitCount;
  164. if (i + limitCount > updateChannels.size()) {
  165. toIndex = updateChannels.size();
  166. }
  167. channelMapper.batchUpdate(updateChannels.subList(i, toIndex));
  168. }
  169. }else {
  170. channelMapper.batchUpdate(updateChannels);
  171. }
  172. }
  173. }
  174. return addChannels.size() + updateChannels.size();
  175. }
  176. @Override
  177. public ResourceBaseInfo getOverview() {
  178. int online = channelMapper.getOnlineCount();
  179. int total = channelMapper.getAllChannelCount();
  180. return new ResourceBaseInfo(total, online);
  181. }
  182. @Override
  183. public List<ChannelReduce> queryAllChannelList(String platformId) {
  184. return channelMapper.queryChannelListInAll(null, null, null, platformId, null);
  185. }
  186. @Override
  187. public boolean updateAllGps(Device device) {
  188. List<DeviceChannel> deviceChannels = channelMapper.getChannelsWithoutTransform(device.getDeviceId());
  189. List<DeviceChannel> result = new CopyOnWriteArrayList<>();
  190. if (deviceChannels.size() == 0) {
  191. return true;
  192. }
  193. String now = DateUtil.getNow();
  194. deviceChannels.parallelStream().forEach(deviceChannel -> {
  195. deviceChannel.setUpdateTime(now);
  196. result.add(updateGps(deviceChannel, device));
  197. });
  198. int limitCount = 50;
  199. if (result.size() > limitCount) {
  200. for (int i = 0; i < result.size(); i += limitCount) {
  201. int toIndex = i + limitCount;
  202. if (i + limitCount > result.size()) {
  203. toIndex = result.size();
  204. }
  205. channelMapper.batchUpdate(result.subList(i, toIndex));
  206. }
  207. }else {
  208. channelMapper.batchUpdate(result);
  209. }
  210. return true;
  211. }
  212. @Override
  213. public List<Device> getDeviceByChannelId(String channelId) {
  214. return channelMapper.getDeviceByChannelId(channelId);
  215. }
  216. @Override
  217. public int deleteChannels(List<DeviceChannel> deleteChannelList) {
  218. return channelMapper.batchDel(deleteChannelList);
  219. }
  220. @Override
  221. public int channelsOnline(List<DeviceChannel> channels) {
  222. return channelMapper.batchOnline(channels);
  223. }
  224. @Override
  225. public void online(DeviceChannel channel) {
  226. channelMapper.online(channel.getDeviceId(), channel.getChannelId());
  227. }
  228. @Override
  229. public int channelsOffline(List<DeviceChannel> channels) {
  230. return channelMapper.batchOffline(channels);
  231. }
  232. @Override
  233. public void offline(DeviceChannel channel) {
  234. channelMapper.offline(channel.getDeviceId(), channel.getChannelId());
  235. }
  236. @Override
  237. public void delete(DeviceChannel channel) {
  238. channelMapper.del(channel.getDeviceId(), channel.getChannelId());
  239. }
  240. @Override
  241. public DeviceChannel getOne(String deviceId, String channelId){
  242. return channelMapper.queryChannel(deviceId, channelId);
  243. }
  244. @Override
  245. public void batchUpdateChannel(List<DeviceChannel> channels) {
  246. String now = DateUtil.getNow();
  247. for (DeviceChannel channel : channels) {
  248. channel.setUpdateTime(now);
  249. }
  250. channelMapper.batchUpdate(channels);
  251. for (DeviceChannel channel : channels) {
  252. if (channel.getParentId() != null) {
  253. channelMapper.updateChannelSubCount(channel.getDeviceId(), channel.getParentId());
  254. }
  255. }
  256. }
  257. @Override
  258. public void batchAddChannel(List<DeviceChannel> channels) {
  259. channelMapper.batchAdd(channels);
  260. for (DeviceChannel channel : channels) {
  261. if (channel.getParentId() != null) {
  262. channelMapper.updateChannelSubCount(channel.getDeviceId(), channel.getParentId());
  263. }
  264. }
  265. }
  266. @Override
  267. public void updateChannelStreamIdentification(DeviceChannel channel) {
  268. assert !ObjectUtils.isEmpty(channel.getDeviceId());
  269. assert !ObjectUtils.isEmpty(channel.getStreamIdentification());
  270. if (ObjectUtils.isEmpty(channel.getStreamIdentification())) {
  271. logger.info("[重置通道码流类型] 设备: {}, 码流: {}", channel.getDeviceId(), channel.getStreamIdentification());
  272. }else {
  273. logger.info("[更新通道码流类型] 设备: {}, 通道:{}, 码流: {}", channel.getDeviceId(), channel.getChannelId(),
  274. channel.getStreamIdentification());
  275. }
  276. channelMapper.updateChannelStreamIdentification(channel);
  277. }
  278. @Override
  279. public List<DeviceChannel> queryChaneListByDeviceId(String deviceId) {
  280. return channelMapper.queryAllChannels(deviceId);
  281. }
  282. @Override
  283. public void updateChannelGPS(Device device, DeviceChannel deviceChannel, MobilePosition mobilePosition) {
  284. if (userSetting.getSavePositionHistory()) {
  285. deviceMobilePositionMapper.insertNewPosition(mobilePosition);
  286. }
  287. if (deviceChannel.getChannelId().equals(deviceChannel.getDeviceId())) {
  288. deviceChannel.setChannelId(null);
  289. }
  290. if (deviceChannel.getGpsTime() == null) {
  291. deviceChannel.setGpsTime(DateUtil.getNow());
  292. }
  293. int updated = channelMapper.updatePosition(deviceChannel);
  294. if (updated == 0) {
  295. return;
  296. }
  297. List<DeviceChannel> deviceChannels = new ArrayList<>();
  298. if (deviceChannel.getChannelId() == null) {
  299. // 有的设备这里上报的deviceId与通道Id是一样,这种情况更新设备下的全部通道
  300. List<DeviceChannel> deviceChannelsInDb = queryChaneListByDeviceId(device.getDeviceId());
  301. deviceChannels.addAll(deviceChannelsInDb);
  302. }else {
  303. deviceChannels.add(deviceChannel);
  304. }
  305. if (deviceChannels.isEmpty()) {
  306. return;
  307. }
  308. if (deviceChannels.size() > 100) {
  309. logger.warn("[更新通道位置信息后发送通知] 设备可能是平台,上报的位置信息未标明通道编号," +
  310. "导致所有通道被更新位置, deviceId:{}", device.getDeviceId());
  311. }
  312. for (DeviceChannel channel : deviceChannels) {
  313. // 向关联了该通道并且开启移动位置订阅的上级平台发送移动位置订阅消息
  314. mobilePosition.setChannelId(channel.getChannelId());
  315. try {
  316. eventPublisher.mobilePositionEventPublish(mobilePosition);
  317. }catch (Exception e) {
  318. logger.error("[向上级转发移动位置失败] ", e);
  319. }
  320. // 发送redis消息。 通知位置信息的变化
  321. JSONObject jsonObject = new JSONObject();
  322. jsonObject.put("time", DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(mobilePosition.getTime()));
  323. jsonObject.put("serial", mobilePosition.getDeviceId());
  324. jsonObject.put("code", mobilePosition.getChannelId());
  325. jsonObject.put("longitude", mobilePosition.getLongitude());
  326. jsonObject.put("latitude", mobilePosition.getLatitude());
  327. jsonObject.put("altitude", mobilePosition.getAltitude());
  328. jsonObject.put("direction", mobilePosition.getDirection());
  329. jsonObject.put("speed", mobilePosition.getSpeed());
  330. redisCatchStorage.sendMobilePositionMsg(jsonObject);
  331. }
  332. }
  333. @Override
  334. @Transactional
  335. public void batchUpdateChannelGPS(List<DeviceChannel> channelList) {
  336. for (DeviceChannel deviceChannel : channelList) {
  337. deviceChannel.setUpdateTime(DateUtil.getNow());
  338. if (deviceChannel.getGpsTime() == null) {
  339. deviceChannel.setGpsTime(DateUtil.getNow());
  340. }
  341. }
  342. int count = 1000;
  343. if (channelList.size() > count) {
  344. for (int i = 0; i < channelList.size(); i+=count) {
  345. int toIndex = i+count;
  346. if ( i + count > channelList.size()) {
  347. toIndex = channelList.size();
  348. }
  349. List<DeviceChannel> channels = channelList.subList(i, toIndex);
  350. channelMapper.batchUpdatePosition(channels);
  351. }
  352. }else {
  353. channelMapper.batchUpdatePosition(channelList);
  354. }
  355. }
  356. @Override
  357. @Transactional
  358. public void batchAddMobilePosition(List<MobilePosition> mobilePositions) {
  359. // int count = 500;
  360. // if (mobilePositions.size() > count) {
  361. // for (int i = 0; i < mobilePositions.size(); i+=count) {
  362. // int toIndex = i+count;
  363. // if ( i + count > mobilePositions.size()) {
  364. // toIndex = mobilePositions.size();
  365. // }
  366. // List<MobilePosition> mobilePositionsSub = mobilePositions.subList(i, toIndex);
  367. // deviceMobilePositionMapper.batchadd(mobilePositionsSub);
  368. // }
  369. // }else {
  370. // deviceMobilePositionMapper.batchadd(mobilePositions);
  371. // }
  372. deviceMobilePositionMapper.batchadd(mobilePositions);
  373. }
  374. }