Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
项目统一框架
amos-boot-biz
Commits
e0922755
Commit
e0922755
authored
Jun 03, 2023
by
caotao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
车辆轨迹按照10分钟划分逻辑优化提交
parent
359d290c
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
101 additions
and
124 deletions
+101
-124
CarIotNewListener.java
...a/com/yeejoin/equipmanage/listener/CarIotNewListener.java
+29
-114
IWlCarMileageService.java
...com/yeejoin/equipmanage/service/IWlCarMileageService.java
+2
-1
WlCarMileageServiceImpl.java
...oin/equipmanage/service/impl/WlCarMileageServiceImpl.java
+7
-0
ThreadCar.java
...c/main/java/com/yeejoin/equipmanage/thread/ThreadCar.java
+58
-9
CarUtils.java
...src/main/java/com/yeejoin/equipmanage/utils/CarUtils.java
+5
-0
No files found.
amos-boot-module/amos-boot-module-biz/amos-boot-module-equip-biz/src/main/java/com/yeejoin/equipmanage/listener/CarIotNewListener.java
View file @
e0922755
...
...
@@ -9,9 +9,12 @@ import com.yeejoin.equipmanage.common.utils.CoordinateUtil;
import
com.yeejoin.equipmanage.fegin.IotFeign
;
import
com.yeejoin.equipmanage.service.ICarService
;
import
com.yeejoin.equipmanage.service.IWlCarMileageService
;
import
com.yeejoin.equipmanage.service.impl.WlCarMileageServiceImpl
;
import
com.yeejoin.equipmanage.thread.ThreadCar
;
import
org.eclipse.paho.client.mqttv3.MqttException
;
import
org.eclipse.paho.client.mqttv3.MqttMessage
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.typroject.tyboot.component.emq.EmqKeeper
;
import
org.typroject.tyboot.component.emq.EmqxListener
;
...
...
@@ -38,7 +41,8 @@ public class CarIotNewListener extends EmqxListener {
@Autowired
private
IotFeign
iotFeign
;
@Value
(
"${mileage.clippingtime}"
)
private
Long
clipping_time
;
private
final
String
GUIDE_KEY
=
"813684495d9a3981dd2c7694916fe404"
;
private
final
String
GUIDE_URL
=
"https://restapi.amap.com/v3/geocode/regeo?"
;
...
...
@@ -50,34 +54,40 @@ public class CarIotNewListener extends EmqxListener {
// 用于根据topicId 也就是物联设备id 存储对应的线程来进行计时
private
static
HashMap
<
String
,
ThreadCar
>
deviceInfo
=
new
HashMap
();
private
static
HashMap
<
String
,
String
>
deviceLastInfo
=
new
HashMap
();
@Override
public
void
processMessage
(
String
topic
,
MqttMessage
message
)
throws
Exception
{
System
.
out
.
println
(
topic
);
System
.
out
.
println
(
message
);
String
measurement
=
topic
.
split
(
"/"
)[
0
];
String
deviceName
=
topic
.
split
(
"/"
)[
1
];
String
iotCode
=
measurement
+
deviceName
;
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
message
.
toString
());
//如果map中已经存在该设备或者该设备有但是线程已经执行了
if
((!
deviceInfo
.
containsKey
(
topic
))
||
(
deviceInfo
.
containsKey
(
topic
)
&&
deviceInfo
.
get
(
topic
)
==
null
))
{
ThreadCar
threadCar
=
new
ThreadCar
(
topic
,
jsonObject
);
ThreadCar
threadCar
=
new
ThreadCar
(
topic
,
jsonObject
,
this
.
iWlCarMileageService
,
this
.
iotFeign
,
this
.
iCarService
,
this
.
emqkeeper
,
clipping_time
);
deviceInfo
.
put
(
topic
,
threadCar
);
threadCar
.
start
();
}
else
{
Long
startTime
=
new
Date
().
getTime
();
Long
endTime
=
jsonObject
.
getLong
(
"time"
);
if
((
startTime
-
endTime
)
<=
600000
){
}
String
coordinate
=
jsonObject
.
getDoubleValue
(
"FireCar_Longitude"
)
+
String
.
valueOf
(
jsonObject
.
getDoubleValue
(
"FireCar_Latitude"
));
if
(!
deviceLastInfo
.
containsKey
(
topic
)){
deviceLastInfo
.
put
(
topic
,
coordinate
);
}
Long
currentTime
=
new
Date
().
getTime
();
Long
endTime
=
jsonObject
.
getLong
(
"time"
);
// if ((startTime - endTime) <= 600000) {
if
(((
currentTime
-
endTime
)
<=
clipping_time
)&&(!
deviceLastInfo
.
get
(
topic
).
equals
(
coordinate
)))
{
try
{
deviceInfo
.
get
(
topic
).
interrupt
();
ThreadCar
threadCar
=
new
ThreadCar
(
topic
,
jsonObject
);
}
catch
(
Exception
e
)
{
}
ThreadCar
threadCar
=
new
ThreadCar
(
topic
,
jsonObject
,
this
.
iWlCarMileageService
,
this
.
iotFeign
,
this
.
iCarService
,
this
.
emqkeeper
,
clipping_time
);
deviceInfo
.
put
(
topic
,
threadCar
);
threadCar
.
start
();
}
}
if
(
jsonObject
.
containsKey
(
"startUp"
)
||
jsonObject
.
containsKey
(
"FireCar_Longitude"
))
{
System
.
out
.
println
(
topic
);
System
.
out
.
println
(
message
);
String
measurement
=
topic
.
split
(
"/"
)[
0
];
String
deviceName
=
topic
.
split
(
"/"
)[
1
];
String
iotCode
=
measurement
+
deviceName
;
if
(
jsonObject
.
containsKey
(
"startUp"
))
{
if
(
jsonObject
.
getBooleanValue
(
"startUp"
))
{
if
(
iWlCarMileageService
.
getUncompleteMileagByIotCode
(
iotCode
))
{
WlCarMileage
wlCarMileage
=
new
WlCarMileage
();
wlCarMileage
.
setIotCode
(
iotCode
);
wlCarMileage
.
setDate
(
new
Date
());
...
...
@@ -98,108 +108,12 @@ public class CarIotNewListener extends EmqxListener {
e
.
printStackTrace
();
iWlCarMileageService
.
save
(
wlCarMileage
);
}
}
else
{
// 获取结束坐标
WlCarMileage
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
)
{
// 获取最后一个有坐标的数据
JSONObject
lastObj
=
null
;
// 过滤空坐标
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
(
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
());
try
{
iWlCarMileageService
.
updateById
(
last
);
}
catch
(
Exception
e
)
{
iWlCarMileageService
.
updateById
(
last
);
}
}
}
}
else
if
(
jsonObject
.
containsKey
(
"FireCar_Longitude"
))
{
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
);
emqkeeper
.
getMqttClient
().
publish
(
"car/location"
,
mqttMessage
);
}
}
}
}
}
public
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"
);
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
{
...
...
@@ -244,4 +158,5 @@ public class CarIotNewListener extends EmqxListener {
calendar
.
set
(
Calendar
.
HOUR
,
calendar
.
get
(
Calendar
.
HOUR
)
+
8
);
return
calendar
.
getTime
();
}
}
amos-boot-module/amos-boot-module-biz/amos-boot-module-equip-biz/src/main/java/com/yeejoin/equipmanage/service/IWlCarMileageService.java
View file @
e0922755
...
...
@@ -30,6 +30,7 @@ public interface IWlCarMileageService extends IService<WlCarMileage> {
* 里程切分(0点若里程未结束,自动切分设置结束信息,并开始新里程)
*/
void
mileageSegmentation
();
//根据iot编码查询是否有未结束里程
Boolean
getUncompleteMileagByIotCode
(
String
iotCode
);
}
amos-boot-module/amos-boot-module-biz/amos-boot-module-equip-biz/src/main/java/com/yeejoin/equipmanage/service/impl/WlCarMileageServiceImpl.java
View file @
e0922755
...
...
@@ -3,6 +3,7 @@ package com.yeejoin.equipmanage.service.impl;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
...
...
@@ -384,6 +385,12 @@ public class WlCarMileageServiceImpl extends ServiceImpl<WlCarMileageMapper, WlC
log
.
info
(
"轨迹切分任务执行完成.............."
);
}
@Override
public
Boolean
getUncompleteMileagByIotCode
(
String
iotCode
)
{
Integer
integer
=
this
.
count
(
new
QueryWrapper
<
WlCarMileage
>().
select
(
"1"
).
lambda
().
eq
(
WlCarMileage:
:
getIotCode
,
iotCode
).
isNull
(
WlCarMileage:
:
getEndTime
));
return
integer
<=
0
;
}
private
String
getAddress
(
double
longitude
,
double
lantitude
)
{
StringBuilder
api
=
new
StringBuilder
(
GUIDE_ADDRESS_URL
);
...
...
amos-boot-module/amos-boot-module-biz/amos-boot-module-equip-biz/src/main/java/com/yeejoin/equipmanage/thread/ThreadCar.java
View file @
e0922755
package
com
.
yeejoin
.
equipmanage
.
thread
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.yeejoin.equipmanage.common.entity.Car
;
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.ICarService
;
import
com.yeejoin.equipmanage.service.IWlCarMileageService
;
import
com.yeejoin.equipmanage.utils.CarUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.eclipse.paho.client.mqttv3.MqttException
;
import
org.eclipse.paho.client.mqttv3.MqttMessage
;
import
org.typroject.tyboot.component.emq.EmqKeeper
;
import
org.typroject.tyboot.core.restful.utils.ResponseModel
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Timer
;
import
java.util.concurrent.TimeUnit
;
public
class
ThreadCar
extends
Thread
{
@Autowired
private
IWlCarMileageService
iWlCarMileageService
;
@Autowired
private
IotFeign
iotFeign
;
private
String
topic
;
private
Long
clippingTime
;
ICarService
iCarService
;
private
EmqKeeper
emqkeeper
;
private
JSONObject
jsonObject
;
public
ThreadCar
(
String
topic
,
JSONObject
jsonObject
){
public
ThreadCar
(
String
topic
,
JSONObject
jsonObject
,
IWlCarMileageService
iWlCarMileageService
,
IotFeign
iotFeign
,
ICarService
iCarService
,
EmqKeeper
emqkeeper
,
Long
clippingTime
)
{
this
.
topic
=
topic
;
this
.
jsonObject
=
jsonObject
;
this
.
iWlCarMileageService
=
iWlCarMileageService
;
this
.
iotFeign
=
iotFeign
;
this
.
iCarService
=
iCarService
;
this
.
emqkeeper
=
emqkeeper
;
this
.
clippingTime
=
clippingTime
;
}
@Override
public
void
run
()
{
//toDo
// 获取最后一个有坐标的数据
JSONObject
lastObj
=
null
;
WlCarMileage
last
=
null
;
WlCarMileage
last
=
null
;
try
{
T
hread
.
sleep
(
600000
);
T
imeUnit
.
MILLISECONDS
.
sleep
(
Long
.
valueOf
((
long
)
(
clippingTime
))
);
//业务处理
//如果十分钟没有坐标,则需要设置结束标记
// 获取结束坐标
...
...
@@ -79,7 +94,7 @@ public class ThreadCar extends Thread {
// 230215180624
// Date endTime =UTCToCST(lastObj.getString("time"));
Date
endTime
=
new
Date
(
jsonObject
.
getLong
(
"time"
)
);
Date
endTime
=
new
Date
();
long
takeTime
=
(
endTime
.
getTime
()
/
1000
*
1000
)
-
(
last
.
getStartTime
().
getTime
()
/
1000
*
1000
);
last
.
setEndLongitude
(
endLongitude
);
last
.
setEndLatitude
(
endLatitude
);
...
...
@@ -98,10 +113,44 @@ public class ThreadCar extends Thread {
}
last
.
setTravel
(
new
BigDecimal
(
travel
/
1000
).
setScale
(
1
,
BigDecimal
.
ROUND_HALF_UP
).
doubleValue
());
iWlCarMileageService
.
updateById
(
last
);
this
.
pushCarInfoToMap
(
jsonObject
,
topic
);
this
.
interrupt
();
}
}
catch
(
Exception
exception
)
{
iWlCarMileageService
.
updateById
(
last
);
}
}
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
);
}
}
}
}
}
...
...
amos-boot-module/amos-boot-module-biz/amos-boot-module-equip-biz/src/main/java/com/yeejoin/equipmanage/utils/CarUtils.java
View file @
e0922755
package
com
.
yeejoin
.
equipmanage
.
utils
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.yeejoin.equipmanage.common.entity.Car
;
import
org.eclipse.paho.client.mqttv3.MqttException
;
import
org.eclipse.paho.client.mqttv3.MqttMessage
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment