DeviceChannelServiceImpl.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.common.StreamInfo;
  3. import com.genersoft.iot.vmp.gb28181.bean.Device;
  4. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  5. import com.genersoft.iot.vmp.gb28181.utils.Coordtransform;
  6. import com.genersoft.iot.vmp.service.IDeviceChannelService;
  7. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  8. import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
  9. import com.genersoft.iot.vmp.storager.dao.DeviceMapper;
  10. import com.genersoft.iot.vmp.utils.DateUtil;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. /**
  19. * @author lin
  20. */
  21. @Service
  22. public class DeviceChannelServiceImpl implements IDeviceChannelService {
  23. private final static Logger logger = LoggerFactory.getLogger(DeviceChannelServiceImpl.class);
  24. @Autowired
  25. private IRedisCatchStorage redisCatchStorage;
  26. @Autowired
  27. private DeviceChannelMapper channelMapper;
  28. @Autowired
  29. private DeviceMapper deviceMapper;
  30. @Override
  31. public DeviceChannel updateGps(DeviceChannel deviceChannel, Device device) {
  32. if (deviceChannel.getLongitude()*deviceChannel.getLatitude() > 0) {
  33. if (device == null) {
  34. device = deviceMapper.getDeviceByDeviceId(deviceChannel.getDeviceId());
  35. }
  36. if ("WGS84".equals(device.getGeoCoordSys())) {
  37. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  38. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  39. Double[] position = Coordtransform.WGS84ToGCJ02(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  40. deviceChannel.setLongitudeGcj02(position[0]);
  41. deviceChannel.setLatitudeGcj02(position[1]);
  42. }else if ("GCJ02".equals(device.getGeoCoordSys())) {
  43. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  44. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  45. Double[] position = Coordtransform.GCJ02ToWGS84(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  46. deviceChannel.setLongitudeWgs84(position[0]);
  47. deviceChannel.setLatitudeWgs84(position[1]);
  48. }else {
  49. deviceChannel.setLongitudeGcj02(0.00);
  50. deviceChannel.setLatitudeGcj02(0.00);
  51. deviceChannel.setLongitudeWgs84(0.00);
  52. deviceChannel.setLatitudeWgs84(0.00);
  53. }
  54. }else {
  55. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  56. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  57. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  58. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  59. }
  60. return deviceChannel;
  61. }
  62. @Override
  63. public void updateChannel(String deviceId, DeviceChannel channel) {
  64. String channelId = channel.getChannelId();
  65. channel.setDeviceId(deviceId);
  66. StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
  67. if (streamInfo != null) {
  68. channel.setStreamId(streamInfo.getStream());
  69. }
  70. String now = DateUtil.getNow();
  71. channel.setUpdateTime(now);
  72. DeviceChannel deviceChannel = channelMapper.queryChannel(deviceId, channelId);
  73. channel = updateGps(channel, null);
  74. if (deviceChannel == null) {
  75. channel.setCreateTime(now);
  76. channelMapper.add(channel);
  77. }else {
  78. channelMapper.update(channel);
  79. }
  80. channelMapper.updateChannelSubCount(deviceId,channel.getParentId());
  81. }
  82. @Override
  83. public int updateChannels(String deviceId, List<DeviceChannel> channels) {
  84. List<DeviceChannel> addChannels = new ArrayList<>();
  85. List<DeviceChannel> updateChannels = new ArrayList<>();
  86. HashMap<String, DeviceChannel> channelsInStore = new HashMap<>();
  87. Device device = deviceMapper.getDeviceByDeviceId(deviceId);
  88. if (channels != null && channels.size() > 0) {
  89. List<DeviceChannel> channelList = channelMapper.queryChannels(deviceId, null, null, null, null);
  90. if (channelList.size() == 0) {
  91. for (DeviceChannel channel : channels) {
  92. channel.setDeviceId(deviceId);
  93. StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channel.getChannelId());
  94. if (streamInfo != null) {
  95. channel.setStreamId(streamInfo.getStream());
  96. }
  97. String now = DateUtil.getNow();
  98. channel.setUpdateTime(now);
  99. channel.setCreateTime(now);
  100. channel = updateGps(channel, device);
  101. addChannels.add(channel);
  102. }
  103. }else {
  104. for (DeviceChannel deviceChannel : channelList) {
  105. channelsInStore.put(deviceChannel.getChannelId(), deviceChannel);
  106. }
  107. for (DeviceChannel channel : channels) {
  108. channel.setDeviceId(deviceId);
  109. StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channel.getChannelId());
  110. if (streamInfo != null) {
  111. channel.setStreamId(streamInfo.getStream());
  112. }
  113. String now = DateUtil.getNow();
  114. channel.setUpdateTime(now);
  115. channel = updateGps(channel, device);
  116. if (channelsInStore.get(channel.getChannelId()) != null) {
  117. updateChannels.add(channel);
  118. }else {
  119. addChannels.add(channel);
  120. channel.setCreateTime(now);
  121. }
  122. }
  123. }
  124. int limitCount = 300;
  125. if (addChannels.size() > 0) {
  126. if (addChannels.size() > limitCount) {
  127. for (int i = 0; i < addChannels.size(); i += limitCount) {
  128. int toIndex = i + limitCount;
  129. if (i + limitCount > addChannels.size()) {
  130. toIndex = addChannels.size();
  131. }
  132. channelMapper.batchAdd(addChannels.subList(i, toIndex));
  133. }
  134. }else {
  135. channelMapper.batchAdd(addChannels);
  136. }
  137. }
  138. if (updateChannels.size() > 0) {
  139. if (updateChannels.size() > limitCount) {
  140. for (int i = 0; i < updateChannels.size(); i += limitCount) {
  141. int toIndex = i + limitCount;
  142. if (i + limitCount > updateChannels.size()) {
  143. toIndex = updateChannels.size();
  144. }
  145. channelMapper.batchUpdate(updateChannels.subList(i, toIndex));
  146. }
  147. }else {
  148. channelMapper.batchUpdate(updateChannels);
  149. }
  150. }
  151. }
  152. return addChannels.size() + updateChannels.size();
  153. }
  154. }