DeviceChannelServiceImpl.java 15 KB

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