Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
rx
Commits
e735cee6
Commit
e735cee6
authored
Aug 22, 2024
by
wujipeng
Browse files
mqtt接收消息
parent
fd432e21
Pipeline
#246
canceled with stages
Changes
6
Pipelines
1
Show whitespace changes
Inline
Side-by-side
pom.xml
View file @
e735cee6
...
...
@@ -129,6 +129,11 @@
<artifactId>
pegdown
</artifactId>
<version>
1.4.2
</version>
</dependency>
<!-- mongodb -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-mongodb
</artifactId>
</dependency>
</dependencies>
<repositories>
...
...
src/main/java/com/ruzhoukj/rx/common/mqtt/MqttSubscriberClient.java
View file @
e735cee6
package
com.ruzhoukj.rx.common.mqtt
;
import
com.ruzhoukj.rx.dao.DeviceDao
;
import
com.ruzhoukj.rx.pojo.entity.DataPoint
;
import
com.ruzhoukj.rx.pojo.entity.DeviceData
;
import
com.ruzhoukj.rx.pojo.entity.DeviceEntity
;
import
com.ruzhoukj.rx.pojo.entity.Fields
;
import
lombok.extern.slf4j.Slf4j
;
import
org.eclipse.paho.client.mqttv3.*
;
...
...
@@ -27,6 +29,9 @@ public class MqttSubscriberClient {
@Resource
private
MqttClient
mqttClient
;
@Resource
DeviceDao
deviceDao
;
@Value
(
"${mqtt.topic}"
)
private
String
topic
;
...
...
@@ -46,18 +51,21 @@ public class MqttSubscriberClient {
try
{
DeviceData
deviceData
=
DeviceData
.
fromJson
(
new
String
(
message
.
getPayload
()));
S
ystem
.
out
.
println
(
"Device ID: "
+
deviceData
.
getDeviceID
()
)
;
S
tring
deviceId
=
deviceData
.
getDeviceID
();
for
(
DataPoint
dp
:
deviceData
.
getData
())
{
System
.
out
.
println
(
"Timestamp: "
+
dp
.
getTimestamp
());
DeviceEntity
deviceEntity
=
new
DeviceEntity
();
deviceEntity
.
setDeviceID
(
deviceId
);
deviceEntity
.
setTimestamp
(
dp
.
getTimestamp
());
Fields
fields
=
dp
.
getFields
();
System
.
out
.
println
(
"ZU: "
+
fields
.
getZu
());
System
.
out
.
println
(
"ZB: "
+
fields
.
getZb
());
System
.
out
.
println
(
"VT: "
+
fields
.
getVt
());
System
.
out
.
println
(
"SVT: "
+
fields
.
getSvt
());
System
.
out
.
println
(
"ZT: "
+
fields
.
getZt
());
System
.
out
.
println
(
"SIN: "
+
fields
.
getSin
());
System
.
out
.
println
(
"TEM: "
+
fields
.
getTem
());
System
.
out
.
println
(
"HUM: "
+
fields
.
getHum
());
deviceEntity
.
setZu
(
fields
.
getZu
());
deviceEntity
.
setZb
(
fields
.
getZb
());
deviceEntity
.
setVt
(
fields
.
getVt
());
deviceEntity
.
setSvt
(
fields
.
getSvt
());
deviceEntity
.
setZt
(
fields
.
getZt
());
deviceEntity
.
setSin
(
fields
.
getSin
());
deviceEntity
.
setTem
(
fields
.
getTem
());
deviceEntity
.
setHum
(
fields
.
getHum
());
deviceDao
.
insert
(
deviceEntity
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
...
...
src/main/java/com/ruzhoukj/rx/dao/DeviceDao.java
0 → 100644
View file @
e735cee6
package
com.ruzhoukj.rx.dao
;
import
com.ruzhoukj.rx.pojo.entity.DeviceEntity
;
import
org.springframework.data.mongodb.repository.MongoRepository
;
import
org.springframework.stereotype.Component
;
/**
* @author 邬吉朋
*/
@Component
public
interface
DeviceDao
extends
MongoRepository
<
DeviceEntity
,
String
>
{
}
src/main/java/com/ruzhoukj/rx/pojo/entity/DeviceEntity.java
0 → 100644
View file @
e735cee6
package
com.ruzhoukj.rx.pojo.entity
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.Data
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
import
java.util.Date
;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-22 12:58
*/
@Document
(
"gsd"
)
@Data
public
class
DeviceEntity
{
@Id
private
String
id
;
private
String
deviceID
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
timestamp
;
private
double
zu
;
private
double
zb
;
private
double
vt
;
private
double
svt
;
private
int
zt
;
private
int
sin
;
private
int
tem
;
private
int
hum
;
}
src/main/java/com/ruzhoukj/rx/pojo/entity/User.java
View file @
e735cee6
package
com.ruzhoukj.rx.pojo.entity
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
java.io.Serializable
;
/**
* Created with IntelliJ IDEA.
* User: QianMo
* Date: 2020/8/26
* Time: 14:09
* 接受登录参数实体类
* @author 邬吉朋
*/
@Data
@ApiModel
(
value
=
"用户登录对象"
,
description
=
"用户登录对象"
)
public
class
User
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@ApiModelProperty
(
value
=
"用户名"
)
...
...
src/main/resources/application.yml
View file @
e735cee6
...
...
@@ -7,6 +7,14 @@ spring:
password
:
ruzhoukj@2022
driver-class-name
:
com.mysql.cj.jdbc.Driver
url
:
jdbc:mysql://192.168.10.3:3307/ops?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
data
:
mongodb
:
database
:
device_data
username
:
ruzhoukj
password
:
ruzhoukj@2023
port
:
27017
host
:
192.168.10.3
authentication-database
:
admin
mqtt
:
...
...
@@ -24,3 +32,5 @@ jwt:
config
:
key
:
test
ttl
:
5400000
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