Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
chejia-smart-service
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ec8a63ca
authored
Apr 13, 2021
by
wnb0128
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
车家智能数据服务 kafka消息推送--think.wang
1 parent
a6a686c9
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
71 additions
and
14 deletions
pom.xml
src/main/java/com/glsx/smart/config/DeviceAddressProducer.java
src/main/java/com/glsx/smart/controller/IndexController.java
src/main/java/com/glsx/smart/controller/UserKafkaController.java
src/main/java/com/glsx/smart/pojo/pb/InstanceFence/Fence.java
src/main/java/com/glsx/smart/service/impl/UserKafkaServiceImpl.java
pom.xml
View file @
ec8a63c
...
@@ -104,5 +104,12 @@
...
@@ -104,5 +104,12 @@
</exclusions>
</exclusions>
</dependency>
</dependency>
<!-- protocol buf -->
<dependency>
<groupId>
com.google.protobuf
</groupId>
<artifactId>
protobuf-java
</artifactId>
<version>
2.5.0
</version>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
\ No newline at end of file
\ No newline at end of file
src/main/java/com/glsx/smart/config/DeviceAddressProducer.java
View file @
ec8a63c
package
com
.
glsx
.
smart
.
config
;
package
com
.
glsx
.
smart
.
config
;
import
com.alibaba.fastjson.JSON
;
import
com.glsx.smart.pojo.pb.InstanceFence.Fence
;
import
com.glsx.smart.pojo.DeviceMessage
;
import
kafka.javaapi.producer.Producer
;
import
kafka.javaapi.producer.Producer
;
import
kafka.producer.KeyedMessage
;
import
kafka.producer.KeyedMessage
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -32,23 +31,64 @@ public class DeviceAddressProducer {
...
@@ -32,23 +31,64 @@ public class DeviceAddressProducer {
* 推送设备经纬度数据
* 推送设备经纬度数据
* @param deviceId
* @param deviceId
* @param lngAndLat
* @param lngAndLat
* @param
type 1新增 2更新 3删除
* @param
value 1新增 2删除 4更新
*/
*/
public
void
pushUserDeviceData
(
String
deviceId
,
String
lngAndLat
,
Integer
typ
e
)
{
public
void
pushUserDeviceData
(
String
deviceId
,
String
lngAndLat
,
Integer
valu
e
)
{
//kafka 发送消息数据封装
//kafka 发送消息数据封装
DeviceMessage
data
=
new
DeviceMessage
(
);
String
[]
strArr
=
lngAndLat
.
split
(
":"
);
data
.
setDeviceId
(
deviceId
);
Fence
.
GpsLoc
gpsLoc
=
this
.
buildGpsLoc
(
strArr
[
0
],
strArr
[
1
]
);
data
.
setLngAndLat
(
lngAndLat
);
Fence
.
FenceDeviceFamilyInfo
deviceFamilyInfo
=
this
.
buildFenceDevice
(
deviceId
,
gpsLoc
);
data
.
setType
(
type
);
Fence
.
OptFamilyFenceInfo
familyFenceInfo
=
this
.
buildFamilyFence
(
value
,
deviceFamilyInfo
);
try
{
try
{
String
topic
=
kafkaConfig
.
getDeviceTopic
();
String
topic
=
kafkaConfig
.
getDeviceTopic
();
String
sendMessage
=
JSON
.
toJSONString
(
data
);
log
.
info
(
"推送kafka topic【{}】的数据:{} {} {}"
,
topic
,
deviceId
,
lngAndLat
,
value
);
log
.
info
(
"推送kafka topic【{}】的数据:{}"
,
topic
,
sendMessage
);
KeyedMessage
<
byte
[],
byte
[]>
message
=
new
KeyedMessage
<>(
topic
,
deviceId
.
getBytes
(),
familyFenceInfo
.
toByteArray
());
KeyedMessage
<
byte
[],
byte
[]>
message
=
new
KeyedMessage
<>(
topic
,
deviceId
.
getBytes
(),
sendMessage
.
getBytes
());
producer
.
send
(
message
);
producer
.
send
(
message
);
}
catch
(
Exception
e
){
}
catch
(
Exception
e
){
log
.
error
(
"消息推送kafka失败:{}"
,
e
);
log
.
error
(
"消息推送kafka失败:{}"
,
e
);
}
}
}
}
/**
* 围栏设备经纬度数据封装
* @param lngStr
* @param latStr
* @return
*/
private
Fence
.
GpsLoc
buildGpsLoc
(
String
lngStr
,
String
latStr
){
double
lng
=
Double
.
valueOf
(
lngStr
)
*
1000000
;
double
lat
=
Double
.
valueOf
(
latStr
)
*
1000000
;
Fence
.
GpsLoc
.
Builder
gpsLocBuilder
=
Fence
.
GpsLoc
.
newBuilder
();
gpsLocBuilder
.
setLng
((
int
)
lng
);
gpsLocBuilder
.
setLat
((
int
)
lat
);
return
gpsLocBuilder
.
build
();
}
/**
* 围栏设备数据封装
* @param deviceId
* @param gpsLoc
* @return
*/
private
Fence
.
FenceDeviceFamilyInfo
buildFenceDevice
(
String
deviceId
,
Fence
.
GpsLoc
gpsLoc
){
Fence
.
FenceDeviceFamilyInfo
.
Builder
fenceDeviceBuilder
=
Fence
.
FenceDeviceFamilyInfo
.
newBuilder
();
fenceDeviceBuilder
.
setSn
(
deviceId
);
fenceDeviceBuilder
.
addGpslocs
(
gpsLoc
);
return
fenceDeviceBuilder
.
build
();
}
/**
* 围栏整个数据封装
* @param value
* @param familyInfo
* @return
*/
private
Fence
.
OptFamilyFenceInfo
buildFamilyFence
(
Integer
value
,
Fence
.
FenceDeviceFamilyInfo
familyInfo
){
Fence
.
OptFamilyFenceInfo
.
Builder
familyFenceBuilder
=
Fence
.
OptFamilyFenceInfo
.
newBuilder
();
familyFenceBuilder
.
addFamilyfenceInfos
(
familyInfo
);
familyFenceBuilder
.
setOperationType
(
Fence
.
OperationType
.
valueOf
(
value
));
return
familyFenceBuilder
.
build
();
}
}
}
src/main/java/com/glsx/smart/controller/IndexController.java
View file @
ec8a63c
package
com
.
glsx
.
smart
.
controller
;
package
com
.
glsx
.
smart
.
controller
;
import
com.glsx.smart.config.DeviceAddressProducer
;
import
com.glsx.smart.dao.entity.SmartProduct
;
import
com.glsx.smart.dao.entity.SmartProduct
;
import
com.glsx.smart.pojo.pb.InstanceFence.Fence
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
/**
/**
...
@@ -16,9 +19,14 @@ import org.springframework.web.bind.annotation.*;
...
@@ -16,9 +19,14 @@ import org.springframework.web.bind.annotation.*;
@RestController
@RestController
public
class
IndexController
{
public
class
IndexController
{
@Autowired
private
DeviceAddressProducer
addressProducer
;
@ResponseBody
@ResponseBody
@RequestMapping
(
value
=
"/home"
,
method
=
{
RequestMethod
.
GET
,
RequestMethod
.
POST
})
@RequestMapping
(
value
=
"/home"
,
method
=
{
RequestMethod
.
GET
,
RequestMethod
.
POST
})
public
String
home
(){
public
String
home
(){
addressProducer
.
pushUserDeviceData
(
"37717106ecfabc73037f"
,
"78.1233:89.3222"
,
Fence
.
OperationType
.
ADD_VALUE
);
return
"hello, welcome to home!"
;
return
"hello, welcome to home!"
;
}
}
...
...
src/main/java/com/glsx/smart/controller/UserKafkaController.java
View file @
ec8a63c
...
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
...
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import
com.glsx.smart.config.DeviceAddressProducer
;
import
com.glsx.smart.config.DeviceAddressProducer
;
import
com.glsx.smart.dao.entity.SmartScene
;
import
com.glsx.smart.dao.entity.SmartScene
;
import
com.glsx.smart.dao.entity.SmartUserRelation
;
import
com.glsx.smart.dao.entity.SmartUserRelation
;
import
com.glsx.smart.pojo.pb.InstanceFence.Fence
;
import
com.glsx.smart.service.*
;
import
com.glsx.smart.service.*
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.apache.commons.collections.CollectionUtils
;
...
@@ -73,7 +74,7 @@ public class UserKafkaController {
...
@@ -73,7 +74,7 @@ public class UserKafkaController {
if
(
CollectionUtils
.
isNotEmpty
(
deviceList
)){
if
(
CollectionUtils
.
isNotEmpty
(
deviceList
)){
for
(
String
deviceId
:
deviceList
)
{
for
(
String
deviceId
:
deviceList
)
{
//更新设备家位置
//更新设备家位置
addressProducer
.
pushUserDeviceData
(
deviceId
,
lngAndLat
,
2
);
addressProducer
.
pushUserDeviceData
(
deviceId
,
lngAndLat
,
Fence
.
OperationType
.
UPDATE_VALUE
);
}
}
}
}
}
}
...
@@ -96,7 +97,7 @@ public class UserKafkaController {
...
@@ -96,7 +97,7 @@ public class UserKafkaController {
sceneDataService
.
getBySceneId
(
scene
.
getId
()));
sceneDataService
.
getBySceneId
(
scene
.
getId
()));
if
(
CollectionUtils
.
isNotEmpty
(
deviceList
))
{
if
(
CollectionUtils
.
isNotEmpty
(
deviceList
))
{
for
(
String
deviceId
:
deviceList
)
{
for
(
String
deviceId
:
deviceList
)
{
addressProducer
.
pushUserDeviceData
(
deviceId
,
null
,
3
);
//删除设备信息
addressProducer
.
pushUserDeviceData
(
deviceId
,
null
,
Fence
.
OperationType
.
DEL_VALUE
);
//删除设备信息
}
}
}
}
//删除用户场景数据
//删除用户场景数据
...
...
src/main/java/com/glsx/smart/pojo/pb/InstanceFence/Fence.java
0 → 100644
View file @
ec8a63c
This diff could not be displayed because it is too large.
src/main/java/com/glsx/smart/service/impl/UserKafkaServiceImpl.java
View file @
ec8a63c
...
@@ -9,6 +9,7 @@ import com.glsx.smart.dao.entity.SmartScene;
...
@@ -9,6 +9,7 @@ import com.glsx.smart.dao.entity.SmartScene;
import
com.glsx.smart.dao.entity.SmartSceneData
;
import
com.glsx.smart.dao.entity.SmartSceneData
;
import
com.glsx.smart.dao.entity.SmartUserRelation
;
import
com.glsx.smart.dao.entity.SmartUserRelation
;
import
com.glsx.smart.dao.mapper.SmartUserRelationMapper
;
import
com.glsx.smart.dao.mapper.SmartUserRelationMapper
;
import
com.glsx.smart.pojo.pb.InstanceFence.Fence
;
import
com.glsx.smart.service.UserKafkaService
;
import
com.glsx.smart.service.UserKafkaService
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.dubbo.config.annotation.DubboReference
;
import
org.apache.dubbo.config.annotation.DubboReference
;
...
@@ -61,7 +62,7 @@ public class UserKafkaServiceImpl implements UserKafkaService {
...
@@ -61,7 +62,7 @@ public class UserKafkaServiceImpl implements UserKafkaService {
String
lngAndLat
=
userAddress
.
getLng
()
+
":"
+
userAddress
.
getLat
();
String
lngAndLat
=
userAddress
.
getLng
()
+
":"
+
userAddress
.
getLat
();
log
.
info
(
"推送的设备:{} 及其经纬度:{}"
,
deviceList
.
toString
(),
lngAndLat
);
log
.
info
(
"推送的设备:{} 及其经纬度:{}"
,
deviceList
.
toString
(),
lngAndLat
);
for
(
String
deviceId
:
deviceList
)
{
for
(
String
deviceId
:
deviceList
)
{
addressProducer
.
pushUserDeviceData
(
deviceId
,
lngAndLat
,
1
);
addressProducer
.
pushUserDeviceData
(
deviceId
,
lngAndLat
,
Fence
.
OperationType
.
ADD_VALUE
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment