Commit dd3b8c25e59395807f84161d0fb18c3d8a34f4ec

Authored by Lawrence
1 parent c223d4e4

支持移动位置和GPS报警信息处理

src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java
... ... @@ -10,19 +10,14 @@ import javax.sip.SipException;
10 10 import javax.sip.message.Request;
11 11 import javax.sip.message.Response;
12 12  
13   -import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
14   -import org.dom4j.Document;
15   -import org.dom4j.DocumentException;
16   -import org.dom4j.Element;
17   -import org.dom4j.io.SAXReader;
18   -import org.slf4j.Logger;
19   -import org.slf4j.LoggerFactory;
20   -import org.springframework.beans.factory.annotation.Autowired;
21   -
  13 +import com.genersoft.iot.vmp.common.StreamInfo;
22 14 import com.genersoft.iot.vmp.common.VideoManagerConstants;
  15 +import com.genersoft.iot.vmp.conf.UserSetup;
  16 +import com.genersoft.iot.vmp.gb28181.bean.BaiduPoint;
23 17 import com.genersoft.iot.vmp.gb28181.bean.Device;
24 18 import com.genersoft.iot.vmp.gb28181.bean.DeviceAlarm;
25 19 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  20 +import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
26 21 import com.genersoft.iot.vmp.gb28181.bean.RecordInfo;
27 22 import com.genersoft.iot.vmp.gb28181.bean.RecordItem;
28 23 import com.genersoft.iot.vmp.gb28181.event.DeviceOffLineDetector;
... ... @@ -32,18 +27,31 @@ import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
32 27 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
33 28 import com.genersoft.iot.vmp.gb28181.transmit.request.SIPRequestAbstractProcessor;
34 29 import com.genersoft.iot.vmp.gb28181.utils.DateUtil;
  30 +import com.genersoft.iot.vmp.gb28181.utils.NumericUtil;
35 31 import com.genersoft.iot.vmp.gb28181.utils.XmlUtil;
  32 +import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
36 33 import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  34 +import com.genersoft.iot.vmp.utils.GpsUtil;
  35 +import com.genersoft.iot.vmp.utils.SpringBeanFactory;
37 36 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
  37 +
  38 +import org.dom4j.Document;
  39 +import org.dom4j.DocumentException;
  40 +import org.dom4j.Element;
  41 +import org.dom4j.io.SAXReader;
  42 +import org.slf4j.Logger;
  43 +import org.slf4j.LoggerFactory;
38 44 import org.springframework.util.StringUtils;
39   -import com.genersoft.iot.vmp.common.StreamInfo;
40 45 /**
41 46 * @Description:MESSAGE请求处理器
42 47 * @author: swwheihei
43 48 * @date: 2020年5月3日 下午5:32:41
44 49 */
  50 +
45 51 public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
46 52  
  53 + private UserSetup userSetup = (UserSetup) SpringBeanFactory.getBean("userSetup");
  54 +
47 55 private final static Logger logger = LoggerFactory.getLogger(MessageRequestProcessor.class);
48 56  
49 57 private SIPCommander cmder;
... ... @@ -71,7 +79,7 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
71 79 private static final String MESSAGE_MEDIA_STATUS = "MediaStatus";
72 80 // private static final String MESSAGE_BROADCAST = "Broadcast";
73 81 // private static final String MESSAGE_DEVICE_STATUS = "DeviceStatus";
74   - // private static final String MESSAGE_MOBILE_POSITION = "MobilePosition";
  82 + private static final String MESSAGE_MOBILE_POSITION = "MobilePosition";
75 83 // private static final String MESSAGE_MOBILE_POSITION_INTERVAL = "Interval";
76 84  
77 85 /**
... ... @@ -106,10 +114,74 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
106 114 }else if (MESSAGE_MEDIA_STATUS.equals(cmd)) {
107 115 logger.info("接收到MediaStatus消息");
108 116 processMessageMediaStatus(evt);
  117 + } else if (MESSAGE_MOBILE_POSITION.equals(cmd)) {
  118 + logger.info("接收到MobilePosition消息");
  119 + processMessageMobilePosition(evt);
109 120 } else {
110 121 logger.info("接收到消息:" + cmd);
  122 + responseAck(evt);
111 123 }
112   - } catch (DocumentException e) {
  124 + } catch (DocumentException | SipException |InvalidArgumentException | ParseException e) {
  125 + e.printStackTrace();
  126 + }
  127 + }
  128 +
  129 + /**
  130 + * 处理MobilePosition移动位置消息
  131 + *
  132 + * @param evt
  133 + */
  134 + private void processMessageMobilePosition(RequestEvent evt) {
  135 + try {
  136 + //回复 200 OK
  137 + Element rootElement = getRootElement(evt);
  138 + MobilePosition mobilePosition = new MobilePosition();
  139 + Element deviceIdElement = rootElement.element("DeviceID");
  140 + String deviceId = deviceIdElement.getTextTrim().toString();
  141 + Device device = storager.queryVideoDevice(deviceId);
  142 + if (device != null) {
  143 + if (!StringUtils.isEmpty(device.getName())) {
  144 + mobilePosition.setDeviceName(device.getName());
  145 + }
  146 + }
  147 + mobilePosition.setDeviceId(XmlUtil.getText(rootElement, "DeviceID"));
  148 + mobilePosition.setTime(XmlUtil.getText(rootElement, "Time"));
  149 + mobilePosition.setLongitude(Double.parseDouble(XmlUtil.getText(rootElement, "Longitude")));
  150 + mobilePosition.setLatitude(Double.parseDouble(XmlUtil.getText(rootElement, "Latitude")));
  151 + if (NumericUtil.isDouble(XmlUtil.getText(rootElement, "Speed"))) {
  152 + mobilePosition.setSpeed(Double.parseDouble(XmlUtil.getText(rootElement, "Speed")));
  153 + } else {
  154 + mobilePosition.setSpeed(0.0);
  155 + }
  156 + if (NumericUtil.isDouble(XmlUtil.getText(rootElement, "Direction"))) {
  157 + mobilePosition.setDirection(Double.parseDouble(XmlUtil.getText(rootElement, "Direction")));
  158 + } else {
  159 + mobilePosition.setDirection(0.0);
  160 + }
  161 + if (NumericUtil.isDouble(XmlUtil.getText(rootElement, "Altitude"))) {
  162 + mobilePosition.setAltitude(Double.parseDouble(XmlUtil.getText(rootElement, "Altitude")));
  163 + } else {
  164 + mobilePosition.setAltitude(0.0);
  165 + }
  166 + mobilePosition.setReportSource("Mobile Position");
  167 + BaiduPoint bp = new BaiduPoint();
  168 + bp = GpsUtil.Wgs84ToBd09(String.valueOf(mobilePosition.getLongitude()), String.valueOf(mobilePosition.getLatitude()));
  169 + logger.info("百度坐标:" + bp.getBdLng() + ", " + bp.getBdLat());
  170 + mobilePosition.setGeodeticSystem("BD-09");
  171 + mobilePosition.setCnLng(bp.getBdLng());
  172 + mobilePosition.setCnLat(bp.getBdLat());
  173 + if (!userSetup.getSavePositionHistory()) {
  174 + storager.clearMobilePositionsByDeviceId(deviceId);
  175 + }
  176 + storager.insertMobilePosition(mobilePosition);
  177 + // List<MobilePosition> all= storager.queryMobilePositions(deviceId, "2021-01-23T00:00:00", "2021-02-28T23:59:59");
  178 + // all= storager.queryMobilePositions(deviceId, null, "2021-01-24T23:59:59");
  179 + // all= storager.queryMobilePositions(deviceId, "2021-01-24T00:00:00", null);
  180 + // //logger.debug(all.toString());
  181 + // MobilePosition mp = storager.queryLatestPosition(deviceId);
  182 + // logger.debug("最新位置:" + mp.getLongitude() + ", " + mp.getLatitude());
  183 + responseAck(evt);
  184 + } catch (DocumentException | SipException | InvalidArgumentException | ParseException e) {
113 185 e.printStackTrace();
114 186 }
115 187 }
... ... @@ -123,7 +195,7 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
123 195 try {
124 196 Element rootElement = getRootElement(evt);
125 197 Element deviceIdElement = rootElement.element("DeviceID");
126   - String deviceId = deviceIdElement.getText().toString();
  198 + String deviceId = deviceIdElement.getTextTrim().toString();
127 199  
128 200 Device device = storager.queryVideoDevice(deviceId);
129 201 if (device == null) {
... ... @@ -180,11 +252,11 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
180 252 if (channelDeviceElement == null) {
181 253 continue;
182 254 }
183   - String channelDeviceId = channelDeviceElement.getText();
  255 + String channelDeviceId = channelDeviceElement.getTextTrim();
184 256 Element channdelNameElement = itemDevice.element("Name");
185 257 String channelName = channdelNameElement != null ? channdelNameElement.getTextTrim().toString() : "";
186 258 Element statusElement = itemDevice.element("Status");
187   - String status = statusElement != null ? statusElement.getText().toString() : "ON";
  259 + String status = statusElement != null ? statusElement.getTextTrim().toString() : "ON";
188 260 DeviceChannel deviceChannel = new DeviceChannel();
189 261 deviceChannel.setName(channelName);
190 262 deviceChannel.setChannelId(channelDeviceId);
... ... @@ -238,15 +310,15 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
238 310 deviceChannel.setPort(Integer.parseInt(XmlUtil.getText(itemDevice, "Port")));
239 311 }
240 312 deviceChannel.setPassword(XmlUtil.getText(itemDevice, "Password"));
241   - if (XmlUtil.getText(itemDevice, "Longitude") == null || XmlUtil.getText(itemDevice, "Longitude") == "") {
242   - deviceChannel.setLongitude(0.00);
243   - } else {
  313 + if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Longitude"))) {
244 314 deviceChannel.setLongitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Longitude")));
245   - }
246   - if (XmlUtil.getText(itemDevice, "Latitude") == null || XmlUtil.getText(itemDevice, "Latitude") =="") {
247   - deviceChannel.setLatitude(0.00);
248 315 } else {
  316 + deviceChannel.setLongitude(0.00);
  317 + }
  318 + if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Latitude"))) {
249 319 deviceChannel.setLatitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Latitude")));
  320 + } else {
  321 + deviceChannel.setLatitude(0.00);
250 322 }
251 323 if (XmlUtil.getText(itemDevice, "PTZType") == null || XmlUtil.getText(itemDevice, "PTZType") == "") {
252 324 deviceChannel.setPTZType(0);
... ... @@ -274,8 +346,7 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
274 346 }
275 347  
276 348 /***
277   - * 收到alarm设备报警信息 处理
278   - *
  349 + * alarm设备报警信息处理
279 350 * @param evt
280 351 */
281 352 private void processMessageAlarm(RequestEvent evt) {
... ... @@ -286,11 +357,8 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
286 357  
287 358 Device device = storager.queryVideoDevice(deviceId);
288 359 if (device == null) {
289   - // TODO 也可能是通道
290   - // storager.queryChannel(deviceId)
291 360 return;
292 361 }
293   -
294 362 DeviceAlarm deviceAlarm = new DeviceAlarm();
295 363 deviceAlarm.setDeviceId(deviceId);
296 364 deviceAlarm.setAlarmPriority(XmlUtil.getText(rootElement, "AlarmPriority"));
... ... @@ -301,26 +369,37 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
301 369 } else {
302 370 deviceAlarm.setAlarmDescription(XmlUtil.getText(rootElement, "AlarmDescription"));
303 371 }
304   - if (XmlUtil.getText(rootElement, "Longitude") == null || XmlUtil.getText(rootElement, "Longitude") == "") {
305   - deviceAlarm.setLongitude(0.00);
306   - } else {
  372 + if (NumericUtil.isDouble(XmlUtil.getText(rootElement, "Longitude"))) {
307 373 deviceAlarm.setLongitude(Double.parseDouble(XmlUtil.getText(rootElement, "Longitude")));
308   - }
309   - if (XmlUtil.getText(rootElement, "Latitude") == null || XmlUtil.getText(rootElement, "Latitude") =="") {
310   - deviceAlarm.setLatitude(0.00);
311 374 } else {
  375 + deviceAlarm.setLongitude(0.00);
  376 + }
  377 + if (NumericUtil.isDouble(XmlUtil.getText(rootElement, "Latitude"))) {
312 378 deviceAlarm.setLatitude(Double.parseDouble(XmlUtil.getText(rootElement, "Latitude")));
  379 + } else {
  380 + deviceAlarm.setLatitude(0.00);
  381 + }
  382 +
  383 + if ( deviceAlarm.getAlarmMethod().equals("4")) {
  384 + MobilePosition mobilePosition = new MobilePosition();
  385 + mobilePosition.setDeviceId(deviceAlarm.getDeviceId());
  386 + mobilePosition.setTime(deviceAlarm.getAlarmTime());
  387 + mobilePosition.setLongitude(deviceAlarm.getLongitude());
  388 + mobilePosition.setLatitude(deviceAlarm.getLatitude());
  389 + mobilePosition.setReportSource("GPS Alarm");
  390 + BaiduPoint bp = new BaiduPoint();
  391 + bp = GpsUtil.Wgs84ToBd09(String.valueOf(mobilePosition.getLongitude()), String.valueOf(mobilePosition.getLatitude()));
  392 + logger.info("百度坐标:" + bp.getBdLng() + ", " + bp.getBdLat());
  393 + mobilePosition.setGeodeticSystem("BD-09");
  394 + mobilePosition.setCnLng(bp.getBdLng());
  395 + mobilePosition.setCnLat(bp.getBdLat());
  396 + if (!userSetup.getSavePositionHistory()) {
  397 + storager.clearMobilePositionsByDeviceId(deviceId);
  398 + }
  399 + storager.insertMobilePosition(mobilePosition);
313 400 }
  401 + // TODO: 需要实现存储报警信息、报警分类
314 402  
315   - // device.setName(XmlUtil.getText(rootElement, "DeviceName"));
316   - // device.setManufacturer(XmlUtil.getText(rootElement, "Manufacturer"));
317   - // device.setModel(XmlUtil.getText(rootElement, "Model"));
318   - // device.setFirmware(XmlUtil.getText(rootElement, "Firmware"));
319   - // if (StringUtils.isEmpty(device.getStreamMode())) {
320   - // device.setStreamMode("UDP");
321   - // }
322   - // storager.updateDevice(device);
323   - //cmder.catalogQuery(device, null);
324 403 // 回复200 OK
325 404 responseAck(evt);
326 405 if (offLineDetector.isOnline(deviceId)) {
... ... @@ -350,7 +429,6 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
350 429 } else {
351 430 }
352 431 }
353   -
354 432 } catch (ParseException | SipException | InvalidArgumentException | DocumentException e) {
355 433 e.printStackTrace();
356 434 }
... ... @@ -458,7 +536,11 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
458 536 }
459 537 }
460 538  
461   -
  539 + /**
  540 + * 收到MediaStatus消息处理
  541 + *
  542 + * @param evt
  543 + */
462 544 private void processMessageMediaStatus(RequestEvent evt){
463 545 try {
464 546 // 回复200 OK
... ...