Commit 78d4dbe3 by huangjy

fix: 启动前删除key

parent 31ef190f
......@@ -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;
}
......
......@@ -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("空间")) {
Long maxSize = 10L;
long 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);
......
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 sign in to comment