Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
黄嘉阳
/
iot-platform-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
78d4dbe3
authored
Dec 07, 2023
by
huangjy
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix: 启动前删除key
parent
31ef190f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
12 deletions
server-web/src/main/java/com/makeit/mqtt/MqttConfig.java
server-web/src/main/java/com/makeit/mqtt/PushCallback.java
server-web/src/main/java/com/makeit/mqtt/MqttConfig.java
View file @
78d4dbe3
...
...
@@ -5,8 +5,11 @@ import org.apache.commons.lang3.StringUtils;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
import
java.util.Objects
;
@Component
@ConfigurationProperties
(
"mqtt"
)
...
...
@@ -16,7 +19,12 @@ public class MqttConfig {
private
MqttPushClient
mqttPushClient
;
@Autowired
private
IotTokenService
iotTokenService
;
@Autowired
private
StringRedisTemplate
redisTemplate
;
public
static
final
String
DEVICE_BR_ANALYSIS
=
"device:brhr:analysis:"
;
public
static
final
String
DEVICE_SPACE_TEMP_DATA
=
"device:space:tempData:"
;
public
static
final
String
DEVICE_FALL_TEMP_DATA
=
"device:fall:tempData:"
;
private
String
username
;
private
String
password
;
private
String
hostUrl
;
...
...
@@ -52,6 +60,10 @@ public class MqttConfig {
mqttPushClient
.
connect
(
hostUrl
,
clientId
,
username
,
password
,
timeout
,
keepalive
);
// 订阅主题
mqttPushClient
.
subscribe
(
defaultTopic
,
0
);
redisTemplate
.
delete
(
Objects
.
requireNonNull
(
redisTemplate
.
keys
(
DEVICE_BR_ANALYSIS
+
"*"
)));
redisTemplate
.
delete
(
Objects
.
requireNonNull
(
redisTemplate
.
keys
(
DEVICE_SPACE_TEMP_DATA
+
"*"
)));
redisTemplate
.
delete
(
Objects
.
requireNonNull
(
redisTemplate
.
keys
(
DEVICE_FALL_TEMP_DATA
+
"*"
)));
return
mqttPushClient
;
}
...
...
server-web/src/main/java/com/makeit/mqtt/PushCallback.java
View file @
78d4dbe3
...
...
@@ -117,24 +117,23 @@ public class PushCallback implements MqttCallback {
// 缓存呼吸设备某段时间的数据,hash 最大长度 duration
long
duration
=
getSleepTimeActionDuration
();
Long
size
=
redisTemplate
.
opsForHash
().
size
(
DEVICE_BR_ANALYSIS
+
device
.
getDeviceId
());
if
(
size
.
equals
(
duration
*
60
))
{
redisTemplate
.
opsForHash
().
delete
(
DEVICE_BR_ANALYSIS
+
device
.
getDeviceId
(),
timestamp
-
duration
*
60
);
}
redisTemplate
.
opsForHash
().
put
(
DEVICE_BR_ANALYSIS
+
device
.
getDeviceId
(),
timestamp
,
JSON
.
toJSONString
(
device
.
getProperties
()));
redisTemplate
.
expire
(
DEVICE_BR_ANALYSIS
+
device
.
getDeviceId
(),
duration
,
TimeUnit
.
MINUTES
);
if
(
size
>
duration
*
60
)
{
redisTemplate
.
opsForHash
().
delete
(
DEVICE_BR_ANALYSIS
+
device
.
getDeviceId
(),
timestamp
-
duration
*
60
);
}
redisTemplate
.
opsForValue
().
set
(
DEVICE_BR_DATA
+
device
.
getDeviceId
(),
JSON
.
toJSONString
(
device
.
getProperties
()),
5000
,
TimeUnit
.
MILLISECONDS
);
}
if
(
REPORT_PROPERTY
.
equals
(
device
.
getMessageType
())
&&
headers
.
getProductName
().
contains
(
"空间"
))
{
L
ong
maxSize
=
10L
;
l
ong
maxSize
=
10L
;
Long
size
=
redisTemplate
.
opsForHash
().
size
(
DEVICE_SPACE_TEMP_DATA
+
device
.
getDeviceId
());
if
(
size
.
equals
(
maxSize
))
{
redisTemplate
.
opsForHash
().
delete
(
DEVICE_SPACE_TEMP_DATA
+
device
.
getDeviceId
(),
timestamp
-
maxSize
);
}
redisTemplate
.
opsForHash
().
put
(
DEVICE_SPACE_TEMP_DATA
+
device
.
getDeviceId
(),
timestamp
,
JSON
.
toJSONString
(
device
.
getProperties
()));
redisTemplate
.
expire
(
DEVICE_SPACE_TEMP_DATA
+
device
.
getDeviceId
(),
maxSize
+
1
,
TimeUnit
.
SECONDS
);
if
(
size
>
maxSize
)
{
redisTemplate
.
opsForHash
().
delete
(
DEVICE_SPACE_TEMP_DATA
+
device
.
getDeviceId
(),
timestamp
-
maxSize
);
}
redisTemplate
.
opsForValue
().
set
(
DEVICE_SPACE_DATA
+
device
.
getDeviceId
(),
JSON
.
toJSONString
(
device
.
getProperties
()),
5000
,
TimeUnit
.
MILLISECONDS
);
...
...
@@ -142,11 +141,11 @@ public class PushCallback implements MqttCallback {
if
(
REPORT_PROPERTY
.
equals
(
device
.
getMessageType
())
&&
headers
.
getProductName
().
contains
(
"跌倒"
))
{
Long
maxSize
=
10L
;
Long
size
=
redisTemplate
.
opsForHash
().
size
(
DEVICE_FALL_TEMP_DATA
+
device
.
getDeviceId
());
if
(
size
.
equals
(
maxSize
))
{
redisTemplate
.
opsForHash
().
delete
(
DEVICE_FALL_TEMP_DATA
+
device
.
getDeviceId
(),
timestamp
-
maxSize
);
}
redisTemplate
.
opsForHash
().
put
(
DEVICE_FALL_TEMP_DATA
+
device
.
getDeviceId
(),
timestamp
,
JSON
.
toJSONString
(
device
.
getProperties
()));
redisTemplate
.
expire
(
DEVICE_FALL_TEMP_DATA
+
device
.
getDeviceId
(),
maxSize
+
1
,
TimeUnit
.
SECONDS
);
if
(
size
>
maxSize
)
{
redisTemplate
.
opsForHash
().
delete
(
DEVICE_FALL_TEMP_DATA
+
device
.
getDeviceId
(),
timestamp
-
maxSize
);
}
redisTemplate
.
opsForValue
().
set
(
DEVICE_FALL_DATA
+
device
.
getDeviceId
(),
JSON
.
toJSONString
(
device
.
getProperties
()),
5000
,
TimeUnit
.
MILLISECONDS
);
...
...
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