Browse Source

增加移动位置API接口支持

pull/47/head
Lawrence 4 years ago
parent
commit
636b7c5475
  1. 27
      src/main/java/com/genersoft/iot/vmp/storager/IVideoManagerStorager.java
  2. 33
      src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMobilePositionMapper.java
  3. 47
      src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java
  4. 68
      src/main/java/com/genersoft/iot/vmp/utils/GpsUtil.java
  5. 118
      src/main/java/com/genersoft/iot/vmp/vmanager/MobilePosition/MobilePositionController.java

27
src/main/java/com/genersoft/iot/vmp/storager/IVideoManagerStorager.java

@ -4,6 +4,7 @@ import java.util.List;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
import com.github.pagehelper.PageInfo;
/**
@ -151,4 +152,30 @@ public interface IVideoManagerStorager {
*/
void cleanChannelsForDevice(String deviceId);
/**
* 添加Mobile Position设备移动位置
* @param MobilePosition
* @return
*/
public boolean insertMobilePosition(MobilePosition mobilePosition);
/**
* 查询移动位置轨迹
* @param deviceId
* @param startTime
* @param endTime
*/
public List<MobilePosition> queryMobilePositions(String deviceId, String startTime, String endTime);
/**
* 查询最新移动位置
* @param deviceId
*/
public MobilePosition queryLatestPosition(String deviceId);
/**
* 删除指定设备的所有移动位置
* @param deviceId
*/
public int clearMobilePositionsByDeviceId(String deviceId);
}

33
src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMobilePositionMapper.java

@ -0,0 +1,33 @@
package com.genersoft.iot.vmp.storager.dao;
import java.util.List;
import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
import org.apache.ibatis.annotations.*;
//import org.springframework.stereotype.Repository;
@Mapper
//@Repository
public interface DeviceMobilePositionMapper {
@Insert("INSERT INTO device_mobile_position (deviceId, deviceName, time, longitude, latitude, altitude, speed, direction, reportSource, geodeticSystem, cnLng, cnLat) " +
"VALUES ('${deviceId}', '${deviceName}', '${time}', ${longitude}, ${latitude}, ${altitude}, ${speed}, ${direction}, '${reportSource}', '${geodeticSystem}', '${cnLng}', '${cnLat}')")
int insertNewPosition(MobilePosition mobilePosition);
@Select(value = {" <script>" +
"SELECT * FROM device_mobile_position" +
" WHERE deviceId = #{deviceId} " +
"<if test=\"startTime != null\"> AND time&gt;=#{startTime}</if>" +
"<if test=\"endTime != null\"> AND time&lt;=#{endTime}</if>" +
" ORDER BY time ASC" +
" </script>"})
List<MobilePosition> queryPositionByDeviceIdAndTime(String deviceId, String startTime, String endTime);
@Select("SELECT * FROM device_mobile_position WHERE deviceId = #{deviceId}" +
" ORDER BY time DESC LIMIT 1")
MobilePosition queryLatestPositionByDevice(String deviceId);
@Delete("DELETE FROM device_mobile_position WHERE deviceId = #{deviceId}")
int clearMobilePositionsByDeviceId(String deviceId);
}

47
src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java

@ -3,8 +3,10 @@ package com.genersoft.iot.vmp.storager.impl;
import java.util.*;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
import com.genersoft.iot.vmp.storager.dao.DeviceMapper;
import com.genersoft.iot.vmp.storager.dao.DeviceMobilePositionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
@ -27,7 +29,10 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager {
private DeviceMapper deviceMapper;
@Autowired
private DeviceChannelMapper deviceChannelMapper;
private DeviceChannelMapper deviceChannelMapper;
@Autowired
private DeviceMobilePositionMapper deviceMobilePositionMapper;
/**
@ -200,11 +205,49 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager {
return deviceMapper.update(device) > 0;
}
/**
* 清空通道
* @param deviceId
*/
@Override
public void cleanChannelsForDevice(String deviceId) {
int result = deviceChannelMapper.cleanChannelsByDeviceId(deviceId);
}
/**
* 添加Mobile Position设备移动位置
* @param MobilePosition
*/
@Override
public synchronized boolean insertMobilePosition(MobilePosition mobilePosition) {
return deviceMobilePositionMapper.insertNewPosition(mobilePosition) > 0;
}
/**
* 查询移动位置轨迹
* @param deviceId
* @param startTime
* @param endTime
*/
@Override
public synchronized List<MobilePosition> queryMobilePositions(String deviceId, String startTime, String endTime) {
return deviceMobilePositionMapper.queryPositionByDeviceIdAndTime(deviceId, startTime, endTime);
}
/**
* 查询最新移动位置
* @param deviceId
*/
@Override
public MobilePosition queryLatestPosition(String deviceId) {
return deviceMobilePositionMapper.queryLatestPositionByDevice(deviceId);
}
/**
* 删除指定设备的所有移动位置
* @param deviceId
*/
public int clearMobilePositionsByDeviceId(String deviceId) {
return deviceMobilePositionMapper.clearMobilePositionsByDeviceId(deviceId);
}
}

68
src/main/java/com/genersoft/iot/vmp/utils/GpsUtil.java

@ -0,0 +1,68 @@
package com.genersoft.iot.vmp.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Base64;
import com.genersoft.iot.vmp.gb28181.bean.BaiduPoint;
public class GpsUtil {
public static BaiduPoint Wgs84ToBd09(String xx, String yy) {
try {
Socket s = new Socket("api.map.baidu.com", 80);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
OutputStream out = s.getOutputStream();
StringBuffer sb = new StringBuffer("GET /ag/coord/convert?from=0&to=4");
sb.append("&x=" + xx + "&y=" + yy);
sb.append("&callback=BMap.Convertor.cbk_3976 HTTP/1.1\r\n");
sb.append("User-Agent: Java/1.6.0_20\r\n");
sb.append("Host: api.map.baidu.com:80\r\n");
sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
sb.append("Connection: Close\r\n");
sb.append("\r\n");
out.write(sb.toString().getBytes());
String json = "";
String tmp = "";
while ((tmp = br.readLine()) != null) {
// System.out.println(tmp);
json += tmp;
}
s.close();
int start = json.indexOf("cbk_3976");
int end = json.lastIndexOf("}");
if (start != -1 && end != -1 && json.contains("\"x\":\"")) {
json = json.substring(start, end);
String[] point = json.split(",");
String x = point[1].split(":")[1].replace("\"", "");
String y = point[2].split(":")[1].replace("\"", "");
BaiduPoint bdPoint= new BaiduPoint();
bdPoint.setBdLng(new String(decode(x)));
bdPoint.setBdLat(new String(decode(y)));
return bdPoint;
//return (new String(decode(x)) + "," + new String(decode(y)));
} else {
System.out.println("gps坐标无效!!");
}
out.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* BASE64解码
* @param str
* @return string
*/
public static byte[] decode(String str) {
byte[] bt = null;
final Base64.Decoder decoder = Base64.getDecoder();
bt = decoder.decode(str); // .decodeBuffer(str);
return bt;
}
}

118
src/main/java/com/genersoft/iot/vmp/vmanager/MobilePosition/MobilePositionController.java

@ -0,0 +1,118 @@
package com.genersoft.iot.vmp.vmanager.MobilePosition;
import java.util.List;
import javax.sip.message.Response;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import com.github.pagehelper.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
@CrossOrigin
@RestController
@RequestMapping("/api")
public class MobilePositionController {
private final static Logger logger = LoggerFactory.getLogger(MobilePositionController.class);
@Autowired
private IVideoManagerStorager storager;
@Autowired
private SIPCommander cmder;
@Autowired
private DeferredResultHolder resultHolder;
@GetMapping("/positions/{deviceId}/history")
public ResponseEntity<List<MobilePosition>> positions(@PathVariable String deviceId,
@RequestParam(required = false) String start,
@RequestParam(required = false) String end) {
if (logger.isDebugEnabled()) {
logger.debug("查询设备" + deviceId + "的历史轨迹");
}
if (StringUtil.isEmpty(start)) {
start = null;
}
if (StringUtil.isEmpty(end)) {
end = null;
}
List<MobilePosition> result = storager.queryMobilePositions(deviceId, start, end);
return new ResponseEntity<>(result, HttpStatus.OK);
}
@GetMapping("/positions/{deviceId}/latest")
public ResponseEntity<MobilePosition> latestPosition(@PathVariable String deviceId) {
if (logger.isDebugEnabled()) {
logger.debug("查询设备" + deviceId + "的最新位置");
}
MobilePosition result = storager.queryLatestPosition(deviceId);
return new ResponseEntity<>(result, HttpStatus.OK);
}
@GetMapping("/positions/{deviceId}/realtime")
public DeferredResult<ResponseEntity<MobilePosition>> realTimePosition(@PathVariable String deviceId) {
Device device = storager.queryVideoDevice(deviceId);
cmder.mobilePostitionQuery(device, event -> {
Response response = event.getResponse();
RequestMessage msg = new RequestMessage();
msg.setId(DeferredResultHolder.CALLBACK_CMD_MOBILEPOSITION + deviceId);
msg.setData(String.format("获取移动位置信息失败,错误码: %s, %s", response.getStatusCode(), response.getReasonPhrase()));
resultHolder.invokeResult(msg);
});
DeferredResult<ResponseEntity<MobilePosition>> result = new DeferredResult<ResponseEntity<MobilePosition>>(5*1000L);
result.onTimeout(()->{
logger.warn(String.format("获取移动位置信息超时"));
// 释放rtpserver
RequestMessage msg = new RequestMessage();
msg.setId(DeferredResultHolder.CALLBACK_CMD_CATALOG+deviceId);
msg.setData("Timeout");
resultHolder.invokeResult(msg);
});
resultHolder.put(DeferredResultHolder.CALLBACK_CMD_CATALOG+deviceId, result);
return result;
}
@GetMapping("/positions/{deviceId}/subscribe")
public ResponseEntity<String> positionSubscribe(@PathVariable String deviceId,
@RequestParam String expires,
@RequestParam String interval) {
String msg = ((expires.equals("0")) ? "取消" : "") + "订阅设备" + deviceId + "的移动位置";
if (logger.isDebugEnabled()) {
logger.debug(msg);
}
if (StringUtil.isEmpty(interval)) {
interval = "5";
}
Device device = storager.queryVideoDevice(deviceId);
String result = msg;
if (cmder.mobilePositionSubscribe(device, Integer.parseInt(expires), Integer.parseInt(interval))) {
result += ",成功";
} else {
result += ",失败";
}
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
Loading…
Cancel
Save