DeviceChannelServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.common.InviteInfo;
  3. import com.genersoft.iot.vmp.common.InviteSessionType;
  4. import com.genersoft.iot.vmp.gb28181.bean.Device;
  5. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  6. import com.genersoft.iot.vmp.gb28181.utils.Coordtransform;
  7. import com.genersoft.iot.vmp.service.IDeviceChannelService;
  8. import com.genersoft.iot.vmp.service.IInviteStreamService;
  9. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  10. import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
  11. import com.genersoft.iot.vmp.storager.dao.DeviceMapper;
  12. import com.genersoft.iot.vmp.utils.DateUtil;
  13. import com.genersoft.iot.vmp.vmanager.bean.ResourceBaseInfo;
  14. import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.concurrent.CopyOnWriteArrayList;
  23. /**
  24. * @author lin
  25. */
  26. @Service
  27. public class DeviceChannelServiceImpl implements IDeviceChannelService {
  28. private final static Logger logger = LoggerFactory.getLogger(DeviceChannelServiceImpl.class);
  29. @Autowired
  30. private IRedisCatchStorage redisCatchStorage;
  31. @Autowired
  32. private IInviteStreamService inviteStreamService;
  33. @Autowired
  34. private DeviceChannelMapper channelMapper;
  35. @Autowired
  36. private DeviceMapper deviceMapper;
  37. @Override
  38. public DeviceChannel updateGps(DeviceChannel deviceChannel, Device device) {
  39. if (deviceChannel.getLongitude()*deviceChannel.getLatitude() > 0) {
  40. if (device == null) {
  41. device = deviceMapper.getDeviceByDeviceId(deviceChannel.getDeviceId());
  42. }
  43. if ("WGS84".equals(device.getGeoCoordSys())) {
  44. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  45. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  46. Double[] position = Coordtransform.WGS84ToGCJ02(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  47. deviceChannel.setLongitudeGcj02(position[0]);
  48. deviceChannel.setLatitudeGcj02(position[1]);
  49. }else if ("GCJ02".equals(device.getGeoCoordSys())) {
  50. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  51. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  52. Double[] position = Coordtransform.GCJ02ToWGS84(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  53. deviceChannel.setLongitudeWgs84(position[0]);
  54. deviceChannel.setLatitudeWgs84(position[1]);
  55. }else {
  56. deviceChannel.setLongitudeGcj02(0.00);
  57. deviceChannel.setLatitudeGcj02(0.00);
  58. deviceChannel.setLongitudeWgs84(0.00);
  59. deviceChannel.setLatitudeWgs84(0.00);
  60. }
  61. }else {
  62. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  63. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  64. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  65. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  66. }
  67. return deviceChannel;
  68. }
  69. @Override
  70. public void updateChannel(String deviceId, DeviceChannel channel) {
  71. String channelId = channel.getChannelId();
  72. channel.setDeviceId(deviceId);
  73. // StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
  74. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
  75. if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
  76. channel.setStreamId(inviteInfo.getStreamInfo().getStream());
  77. }
  78. String now = DateUtil.getNow();
  79. channel.setUpdateTime(now);
  80. DeviceChannel deviceChannel = channelMapper.queryChannel(deviceId, channelId);
  81. channel = updateGps(channel, null);
  82. if (deviceChannel == null) {
  83. channel.setCreateTime(now);
  84. channelMapper.add(channel);
  85. }else {
  86. channelMapper.update(channel);
  87. }
  88. channelMapper.updateChannelSubCount(deviceId,channel.getParentId());
  89. }
  90. @Override
  91. public int updateChannels(String deviceId, List<DeviceChannel> channels) {
  92. List<DeviceChannel> addChannels = new ArrayList<>();
  93. List<DeviceChannel> updateChannels = new ArrayList<>();
  94. HashMap<String, DeviceChannel> channelsInStore = new HashMap<>();
  95. Device device = deviceMapper.getDeviceByDeviceId(deviceId);
  96. if (channels != null && channels.size() > 0) {
  97. List<DeviceChannel> channelList = channelMapper.queryChannels(deviceId, null, null, null, null,null);
  98. if (channelList.size() == 0) {
  99. for (DeviceChannel channel : channels) {
  100. channel.setDeviceId(deviceId);
  101. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channel.getChannelId());
  102. if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
  103. channel.setStreamId(inviteInfo.getStreamInfo().getStream());
  104. }
  105. String now = DateUtil.getNow();
  106. channel.setUpdateTime(now);
  107. channel.setCreateTime(now);
  108. channel = updateGps(channel, device);
  109. addChannels.add(channel);
  110. }
  111. }else {
  112. for (DeviceChannel deviceChannel : channelList) {
  113. channelsInStore.put(deviceChannel.getChannelId(), deviceChannel);
  114. }
  115. for (DeviceChannel channel : channels) {
  116. channel.setDeviceId(deviceId);
  117. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channel.getChannelId());
  118. if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
  119. channel.setStreamId(inviteInfo.getStreamInfo().getStream());
  120. }
  121. String now = DateUtil.getNow();
  122. channel.setUpdateTime(now);
  123. channel = updateGps(channel, device);
  124. if (channelsInStore.get(channel.getChannelId()) != null) {
  125. updateChannels.add(channel);
  126. }else {
  127. addChannels.add(channel);
  128. channel.setCreateTime(now);
  129. }
  130. }
  131. }
  132. int limitCount = 300;
  133. if (addChannels.size() > 0) {
  134. if (addChannels.size() > limitCount) {
  135. for (int i = 0; i < addChannels.size(); i += limitCount) {
  136. int toIndex = i + limitCount;
  137. if (i + limitCount > addChannels.size()) {
  138. toIndex = addChannels.size();
  139. }
  140. channelMapper.batchAdd(addChannels.subList(i, toIndex));
  141. }
  142. }else {
  143. channelMapper.batchAdd(addChannels);
  144. }
  145. }
  146. if (updateChannels.size() > 0) {
  147. if (updateChannels.size() > limitCount) {
  148. for (int i = 0; i < updateChannels.size(); i += limitCount) {
  149. int toIndex = i + limitCount;
  150. if (i + limitCount > updateChannels.size()) {
  151. toIndex = updateChannels.size();
  152. }
  153. channelMapper.batchUpdate(updateChannels.subList(i, toIndex));
  154. }
  155. }else {
  156. channelMapper.batchUpdate(updateChannels);
  157. }
  158. }
  159. }
  160. return addChannels.size() + updateChannels.size();
  161. }
  162. @Override
  163. public ResourceBaseInfo getOverview() {
  164. int online = channelMapper.getOnlineCount();
  165. int total = channelMapper.getAllChannelCount();
  166. return new ResourceBaseInfo(total, online);
  167. }
  168. @Override
  169. public List<ChannelReduce> queryAllChannelList(String platformId) {
  170. return channelMapper.queryChannelListInAll(null, null, null, platformId, null);
  171. }
  172. @Override
  173. public boolean updateAllGps(Device device) {
  174. List<DeviceChannel> deviceChannels = channelMapper.getChannelsWithoutTransform(device.getDeviceId());
  175. List<DeviceChannel> result = new CopyOnWriteArrayList<>();
  176. if (deviceChannels.size() == 0) {
  177. return true;
  178. }
  179. String now = DateUtil.getNow();
  180. deviceChannels.parallelStream().forEach(deviceChannel -> {
  181. deviceChannel.setUpdateTime(now);
  182. result.add(updateGps(deviceChannel, device));
  183. });
  184. int limitCount = 300;
  185. if (result.size() > limitCount) {
  186. for (int i = 0; i < result.size(); i += limitCount) {
  187. int toIndex = i + limitCount;
  188. if (i + limitCount > result.size()) {
  189. toIndex = result.size();
  190. }
  191. channelMapper.batchUpdate(result.subList(i, toIndex));
  192. }
  193. }else {
  194. channelMapper.batchUpdate(result);
  195. }
  196. return true;
  197. }
  198. @Override
  199. public List<Device> getDeviceByChannelId(String channelId) {
  200. return channelMapper.getDeviceByChannelId(channelId);
  201. }
  202. @Override
  203. public int deleteChannels(List<DeviceChannel> deleteChannelList) {
  204. return channelMapper.batchDel(deleteChannelList);
  205. }
  206. @Override
  207. public int channelsOnline(List<DeviceChannel> channels) {
  208. return channelMapper.batchOnline(channels);
  209. }
  210. @Override
  211. public int channelsOffline(List<DeviceChannel> channels) {
  212. return channelMapper.batchOffline(channels);
  213. }
  214. @Override
  215. public DeviceChannel getOne(String deviceId, String channelId){
  216. return channelMapper.queryChannel(deviceId, channelId);
  217. }
  218. @Override
  219. public void batchUpdateChannel(List<DeviceChannel> channels) {
  220. channelMapper.batchUpdate(channels);
  221. for (DeviceChannel channel : channels) {
  222. if (channel.getParentId() != null) {
  223. channelMapper.updateChannelSubCount(channel.getDeviceId(), channel.getParentId());
  224. }
  225. }
  226. }
  227. @Override
  228. public void batchAddChannel(List<DeviceChannel> channels) {
  229. channelMapper.batchAdd(channels);
  230. for (DeviceChannel channel : channels) {
  231. if (channel.getParentId() != null) {
  232. channelMapper.updateChannelSubCount(channel.getDeviceId(), channel.getParentId());
  233. }
  234. }
  235. }
  236. }