Commit dc746016 authored by caotao's avatar caotao

车辆轨迹按照10分钟划分初版代码提交

parent 7adf00a6
......@@ -14,8 +14,8 @@ import java.util.Map;
/**
* @author DELL
*/
@FeignClient(name = "${iot.vehicle.track}", path = "iot", configuration = {FeignConfiguration.class})
//@FeignClient(name = "AMOS-API-IOT", path = "iot", configuration = { FeignConfiguration.class }, url = "http://39.98.224.23:33001")
//@FeignClient(name = "${iot.vehicle.track}", path = "iot", configuration = {FeignConfiguration.class})
@FeignClient(name = "AMOS-API-IOT", path = "iot", configuration = { FeignConfiguration.class }, url = "http://39.98.224.23:33001")
public interface IotFeign {
@RequestMapping(value = "/v1/livedata/list", method = RequestMethod.GET, consumes = "application/json")
......
package com.yeejoin.equipmanage.thread;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yeejoin.equipmanage.common.entity.WlCarMileage;
import com.yeejoin.equipmanage.common.utils.CoordinateUtil;
import com.yeejoin.equipmanage.fegin.IotFeign;
import com.yeejoin.equipmanage.service.IWlCarMileageService;
import com.yeejoin.equipmanage.utils.CarUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.typroject.tyboot.core.restful.utils.ResponseModel;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ThreadCar extends Thread {
@Autowired
private IWlCarMileageService iWlCarMileageService;
@Autowired
private IotFeign iotFeign;
private String topic;
private JSONObject jsonObject;
public ThreadCar(String topic, JSONObject jsonObject){
this.topic = topic;
this.jsonObject = jsonObject;
}
@Override
public void run() {
//toDo
// 获取最后一个有坐标的数据
JSONObject lastObj = null;
WlCarMileage last= null;
try {
Thread.sleep(600000);
//业务处理
//如果十分钟没有坐标,则需要设置结束标记
// 获取结束坐标
String measurement = topic.split("/")[0];
String deviceName = topic.split("/")[1];
String iotCode = measurement + deviceName;
last = iWlCarMileageService
.getOne(new LambdaQueryWrapper<WlCarMileage>().eq(WlCarMileage::getIotCode, iotCode)
.isNull(WlCarMileage::getEndLongitude).isNull(WlCarMileage::getEndLatitude)
.orderByDesc(WlCarMileage::getStartTime).last("limit 1"));
ResponseModel<List<Object>> result = iotFeign.getLiveData(measurement, deviceName,
last.getStartTime(), new Date(new Date().getTime() + 2000));
List<Object> list = result.getResult();
if (list != null && list.size() > 0) {
// 过滤空坐标
List<Object> filterList = new ArrayList<Object>();
for (int i = 0; i < list.size(); i++) {
JSONObject Obj = JSONObject.parseObject(JSONObject.toJSONString(list.get(i)));
if (Obj.get("FireCar_Longitude") != null && Obj.get("FireCar_Latitude") != null
&& Obj.getDoubleValue("FireCar_Longitude") != 0
&& Obj.getDoubleValue("FireCar_Latitude") != 0) {
filterList.add(list.get(i));
// 获取第一个不为空的坐标
if (lastObj == null) {
lastObj = Obj;
}
}
}
// JSONObject lastObj =
// JSONObject.parseObject(JSONObject.toJSONString(list.get(list.size() - 1)));
if (lastObj == null) {
lastObj = new JSONObject();
lastObj.put("FireCar_Longitude", 0.0);
lastObj.put("FireCar_Latitude", 0.0);
lastObj.put("time", 0);
lastObj.put("FireCar_Speed", 0);
}
double endLongitude = lastObj.getDoubleValue("FireCar_Longitude");
double endLatitude = lastObj.getDoubleValue("FireCar_Latitude");
// 230215180624
// Date endTime =UTCToCST(lastObj.getString("time"));
Date endTime = new Date(jsonObject.getLong("time"));
long takeTime = (endTime.getTime() / 1000 * 1000) - (last.getStartTime().getTime() / 1000 * 1000);
last.setEndLongitude(endLongitude);
last.setEndLatitude(endLatitude);
last.setEndTime(endTime);
last.setEndName(CarUtils.getAddress(endLongitude, endLatitude));
last.setEndSpeed(lastObj.getIntValue("FireCar_Speed"));
last.setTakeTime(takeTime);
double travel = 0.0;
// 获取里程
for (int i = 0; i < filterList.size() - 1; i++) {
JSONObject start = JSONObject.parseObject(JSONObject.toJSONString(filterList.get(i)));
JSONObject end = JSONObject.parseObject(JSONObject.toJSONString(filterList.get(i + 1)));
travel += CoordinateUtil.distance(start.getDoubleValue("FireCar_Latitude"),
start.getDoubleValue("FireCar_Longitude"), end.getDoubleValue("FireCar_Latitude"),
end.getDoubleValue("FireCar_Longitude"));
}
last.setTravel(new BigDecimal(travel / 1000).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
iWlCarMileageService.updateById(last);
this.interrupt();
}
} catch (Exception exception) {
iWlCarMileageService.updateById(last);
}
}
}
package com.yeejoin.equipmanage.utils;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class CarUtils {
private static final String GUIDE_KEY = "813684495d9a3981dd2c7694916fe404";
private static final String GUIDE_URL = "https://restapi.amap.com/v3/geocode/regeo?";
public static String getAddress(double longitude, double lantitude) {
StringBuilder api = new StringBuilder(GUIDE_URL);
api.append("key=").append(GUIDE_KEY).append("&location=").append(longitude).append(",").append(lantitude)
.append("&radius=1000").append("&batch=false").append("&extensions=base").append("&roadlevel=0")
.append("&batch=false");
StringBuilder res = new StringBuilder();
BufferedReader in = null;
try {
System.out.println(api.toString());
URL url = new URL(api.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
res.append(line).append("\n");
}
JSONObject object = JSONObject.parseObject(res.toString());
System.out.println(object);
JSONObject regeocode = object.getJSONObject("regeocode");
String address = regeocode.getString("formatted_address");
if ("[]".equals(address)) {
System.out.println("===============无效坐标:" + longitude + "," + lantitude);
address = "无效坐标";
}
res = new StringBuilder(address);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return res.toString();
}
}
......@@ -2,6 +2,7 @@ package com.yeejoin;
import com.yeejoin.amos.boot.biz.common.utils.oConvertUtils;
import com.yeejoin.equipmanage.listener.CarIotListener;
import com.yeejoin.equipmanage.listener.CarIotNewListener;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
......@@ -19,6 +20,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;
import org.typroject.tyboot.component.emq.EmqKeeper;
......@@ -46,8 +48,12 @@ public class AmostEquipApplication {
@Autowired
private EmqKeeper emqKeeper;
// @Autowired
// private CarIotListener carIotListener;
@Autowired
private CarIotListener carIotListener;
private CarIotNewListener carIotNewListener;
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext context = SpringApplication.run(AmostEquipApplication.class, args);
......@@ -74,6 +80,6 @@ public class AmostEquipApplication {
*/
@Bean
void initMqtt() throws MqttException {
emqKeeper.getMqttClient().subscribe("+/+/property", 1, carIotListener);
emqKeeper.getMqttClient().subscribe("+/+/property", 1, carIotNewListener);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment