Commit 472648e2 authored by caotao's avatar caotao

收到的坐标未能及时更新到地图问题修改

parent 7b8f0c87
...@@ -66,28 +66,29 @@ public class CarIotNewListener extends EmqxListener { ...@@ -66,28 +66,29 @@ public class CarIotNewListener extends EmqxListener {
String iotCode = measurement + deviceName; String iotCode = measurement + deviceName;
JSONObject jsonObject = JSONObject.parseObject(message.toString()); JSONObject jsonObject = JSONObject.parseObject(message.toString());
if (jsonObject.containsKey("FireCar_Longitude")) { if (jsonObject.containsKey("FireCar_Longitude")) {
this.updateCarLocation(jsonObject,iotCode);
//如果map中已经存在该设备或者该设备有但是线程已经执行了 //如果map中已经存在该设备或者该设备有但是线程已经执行了
if ((!deviceInfo.containsKey(topic)) || (deviceInfo.containsKey(topic) && deviceInfo.get(topic) == null)) { if ((!deviceInfo.containsKey(iotCode)) || (deviceInfo.containsKey(iotCode) && deviceInfo.get(iotCode) == null)) {
ThreadCar threadCar = new ThreadCar(topic, jsonObject, this.iWlCarMileageService, this.iotFeign, this.iCarService, this.emqkeeper, clipping_time); ThreadCar threadCar = new ThreadCar(iotCode, jsonObject, this.iWlCarMileageService, this.iotFeign, this.iCarService, this.emqkeeper, clipping_time);
deviceInfo.put(topic, threadCar); deviceInfo.put(iotCode, threadCar);
threadCar.start(); threadCar.start();
} }
String coordinate = jsonObject.getDoubleValue("FireCar_Longitude") + String.valueOf(jsonObject.getDoubleValue("FireCar_Latitude")); String coordinate = jsonObject.getDoubleValue("FireCar_Longitude") + String.valueOf(jsonObject.getDoubleValue("FireCar_Latitude"));
if (!deviceLastInfo.containsKey(topic)) { if (!deviceLastInfo.containsKey(iotCode)) {
deviceLastInfo.put(topic, coordinate); deviceLastInfo.put(iotCode, coordinate);
} }
Long currentTime = new Date().getTime(); Long currentTime = new Date().getTime();
Long endTime = jsonObject.getLong("time"); Long endTime = jsonObject.getLong("time");
// if ((startTime - endTime) <= 600000) { // if ((startTime - endTime) <= 600000) {
if (((currentTime - endTime) <= clipping_time) && (!deviceLastInfo.get(topic).equals(coordinate))) { if (((currentTime - endTime) <= clipping_time) && (!deviceLastInfo.get(iotCode).equals(coordinate))) {
try { try {
deviceInfo.get(topic).interrupt(); deviceInfo.get(iotCode).interrupt();
} catch (Exception e) { } catch (Exception e) {
} }
ThreadCar threadCar = new ThreadCar(topic, jsonObject, this.iWlCarMileageService, this.iotFeign, this.iCarService, this.emqkeeper, clipping_time); ThreadCar threadCar = new ThreadCar(iotCode, jsonObject, this.iWlCarMileageService, this.iotFeign, this.iCarService, this.emqkeeper, clipping_time);
deviceInfo.put(topic, threadCar); deviceInfo.put(iotCode, threadCar);
//更新车辆的最新坐标数据 //更新车辆的最新坐标数据
deviceLastInfo.put(topic, coordinate); deviceLastInfo.put(iotCode, coordinate);
threadCar.start(); threadCar.start();
} }
if (iWlCarMileageService.getUncompleteMileagByIotCode(iotCode)) { if (iWlCarMileageService.getUncompleteMileagByIotCode(iotCode)) {
...@@ -162,5 +163,37 @@ public class CarIotNewListener extends EmqxListener { ...@@ -162,5 +163,37 @@ public class CarIotNewListener extends EmqxListener {
calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8); calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
return calendar.getTime(); return calendar.getTime();
} }
public void updateCarLocation(JSONObject jsonObject, String iotCode) {
if (jsonObject.containsKey("FireCar_Longitude") && jsonObject.containsKey("FireCar_Latitude")) {
// 获取开始坐标
double startLongitude = jsonObject.getDoubleValue("FireCar_Longitude");
double startLatitude = jsonObject.getDoubleValue("FireCar_Latitude");
int direction = jsonObject.getIntValue("direction");
// 地图推送消息
Car car = iCarService.getOne(new LambdaQueryWrapper<Car>().eq(Car::getIotCode, iotCode));
if (car != null && startLongitude != 0 && startLatitude != 0) {
JSONArray sendArr = new JSONArray();
JSONObject sendObj = new JSONObject();
sendObj.put("id", String.valueOf(car.getId()));
sendObj.put("direction", direction);
sendObj.put("longitude", String.valueOf(startLongitude));
sendObj.put("latitude", String.valueOf(startLatitude));
sendObj.put("carNum", car.getCarNum());
sendObj.put("bizOrgName", car.getBizOrgName());
sendArr.add(sendObj);
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setPayload(sendArr.toJSONString().getBytes());
car.setLongitude(startLongitude);
car.setLatitude(startLatitude);
iCarService.updateById(car);
System.out.println("-----------推送车辆位置消息到到地图成功--------");
try {
emqkeeper.getMqttClient().publish("car/location", mqttMessage);
} catch (MqttException e) {
throw new RuntimeException(e);
}
}
}
}
} }
...@@ -113,7 +113,6 @@ public class ThreadCar extends Thread { ...@@ -113,7 +113,6 @@ public class ThreadCar extends Thread {
} }
last.setTravel(new BigDecimal(travel / 1000).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()); last.setTravel(new BigDecimal(travel / 1000).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
iWlCarMileageService.updateById(last); iWlCarMileageService.updateById(last);
this.pushCarInfoToMap(jsonObject, topic);
System.out.println("============================================================更新结束坐标成功==========:"+topic); System.out.println("============================================================更新结束坐标成功==========:"+topic);
this.interrupt(); this.interrupt();
} }
...@@ -124,37 +123,5 @@ public class ThreadCar extends Thread { ...@@ -124,37 +123,5 @@ public class ThreadCar extends Thread {
} }
} }
public void pushCarInfoToMap(JSONObject jsonObject, String iotCode) {
if (jsonObject.containsKey("FireCar_Longitude") && jsonObject.containsKey("FireCar_Latitude")) {
// 获取开始坐标
double startLongitude = jsonObject.getDoubleValue("FireCar_Longitude");
double startLatitude = jsonObject.getDoubleValue("FireCar_Latitude");
int direction = jsonObject.getIntValue("direction");
// 地图推送消息
Car car = iCarService.getOne(new LambdaQueryWrapper<Car>().eq(Car::getIotCode, iotCode));
if (car != null && startLongitude != 0 && startLatitude != 0) {
JSONArray sendArr = new JSONArray();
JSONObject sendObj = new JSONObject();
sendObj.put("id", String.valueOf(car.getId()));
sendObj.put("direction", direction);
sendObj.put("longitude", String.valueOf(startLongitude));
sendObj.put("latitude", String.valueOf(startLatitude));
sendObj.put("carNum", car.getCarNum());
sendObj.put("bizOrgName", car.getBizOrgName());
sendArr.add(sendObj);
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setPayload(sendArr.toJSONString().getBytes());
car.setLongitude(startLongitude);
car.setLatitude(startLatitude);
iCarService.updateById(car);
try {
emqkeeper.getMqttClient().publish("car/location", mqttMessage);
} catch (MqttException e) {
throw new RuntimeException(e);
}
}
}
}
} }
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