Compare commits
9 Commits
5d42f8a1e1
...
07e60cf7f0
| Author | SHA1 | Date | |
|---|---|---|---|
| 07e60cf7f0 | |||
| 467a7befb1 | |||
| a85e74c5e6 | |||
| da0833a400 | |||
| 1d50981a84 | |||
| 09cbdf267b | |||
| 156fdbb53b | |||
| 3246127893 | |||
| dab0440128 |
@ -53,7 +53,7 @@ public class ReceiverMessageHandler implements MessageHandler {
|
|||||||
if(StringUtils.isNotBlank(deviceImei)){
|
if(StringUtils.isNotBlank(deviceImei)){
|
||||||
String queueKey = MqttMessageQueueConstants.MQTT_MESSAGE_QUEUE_KEY;
|
String queueKey = MqttMessageQueueConstants.MQTT_MESSAGE_QUEUE_KEY;
|
||||||
String dedupKey = MqttMessageQueueConstants.MQTT_MESSAGE_DEDUP_KEY;
|
String dedupKey = MqttMessageQueueConstants.MQTT_MESSAGE_DEDUP_KEY;
|
||||||
RedisUtils.offerDeduplicated(queueKey,dedupKey,deviceImei, Duration.ofHours(24));
|
RedisUtils.offerDeduplicated(queueKey,dedupKey,deviceImei, Duration.ofSeconds(900));
|
||||||
//在线状态
|
//在线状态
|
||||||
String deviceOnlineStatusRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX+ deviceImei + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX ;
|
String deviceOnlineStatusRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX+ deviceImei + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX ;
|
||||||
RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1", Duration.ofSeconds(360));
|
RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1", Duration.ofSeconds(360));
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|||||||
import com.fuyuanshen.common.core.utils.StringUtils;
|
import com.fuyuanshen.common.core.utils.StringUtils;
|
||||||
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
||||||
import com.fuyuanshen.equipment.domain.Device;
|
import com.fuyuanshen.equipment.domain.Device;
|
||||||
|
import com.fuyuanshen.equipment.domain.vo.OnlineStatusVo;
|
||||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||||
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -24,20 +25,16 @@ public class OnlineStatusTask {
|
|||||||
// 使用cron表达式,每分钟的第0秒执行
|
// 使用cron表达式,每分钟的第0秒执行
|
||||||
@Scheduled(cron = "0 */3 * * * ?")
|
@Scheduled(cron = "0 */3 * * * ?")
|
||||||
public void cronTask() {
|
public void cronTask() {
|
||||||
QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
|
List<OnlineStatusVo> onlineStatusVos = deviceMapper.queryOnlineStatusList();
|
||||||
List<Device> devices = deviceMapper.selectList(queryWrapper);
|
onlineStatusVos.forEach(item -> {
|
||||||
devices.forEach(item -> {
|
|
||||||
String onlineStatusKey = GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + item.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX;
|
String onlineStatusKey = GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + item.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX;
|
||||||
String status = RedisUtils.getCacheObject(onlineStatusKey);
|
String status = RedisUtils.getCacheObject(onlineStatusKey);
|
||||||
String onlineStatus = item.getOnlineStatus()==null?"0" : item.getOnlineStatus().toString();
|
|
||||||
if("1".equals(onlineStatus) || "2".equals(onlineStatus)){
|
|
||||||
if(StringUtils.isBlank(status) || "0".equals(status)){
|
if(StringUtils.isBlank(status) || "0".equals(status)){
|
||||||
UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
|
UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
|
||||||
updateWrapper.eq("id", item.getId());
|
updateWrapper.eq("id", item.getId());
|
||||||
updateWrapper.set("online_status", 0);
|
updateWrapper.set("online_status", 0);
|
||||||
deviceMapper.update(updateWrapper);
|
deviceMapper.update(updateWrapper);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -179,7 +179,7 @@ public class DeviceBizService {
|
|||||||
}
|
}
|
||||||
Device device = devices.get(0);
|
Device device = devices.get(0);
|
||||||
if (device.getBindingStatus() != null && device.getBindingStatus() == BindingStatusEnum.BOUND.getCode()) {
|
if (device.getBindingStatus() != null && device.getBindingStatus() == BindingStatusEnum.BOUND.getCode()) {
|
||||||
throw new RuntimeException("设备已绑定");
|
throw new RuntimeException("设备已经被绑定!");
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryWrapper<AppDeviceBindRecord> bindRecordQueryWrapper = new QueryWrapper<>();
|
QueryWrapper<AppDeviceBindRecord> bindRecordQueryWrapper = new QueryWrapper<>();
|
||||||
@ -189,7 +189,6 @@ public class DeviceBizService {
|
|||||||
if (appDeviceBindRecord != null) {
|
if (appDeviceBindRecord != null) {
|
||||||
UpdateWrapper<AppDeviceBindRecord> deviceUpdateWrapper = new UpdateWrapper<>();
|
UpdateWrapper<AppDeviceBindRecord> deviceUpdateWrapper = new UpdateWrapper<>();
|
||||||
deviceUpdateWrapper.eq("device_id", device.getId())
|
deviceUpdateWrapper.eq("device_id", device.getId())
|
||||||
.set("binding_status", BindingStatusEnum.BOUND.getCode())
|
|
||||||
.set("binding_user_id", userId)
|
.set("binding_user_id", userId)
|
||||||
.set("communication_mode", 0)
|
.set("communication_mode", 0)
|
||||||
.set("update_time", new Date())
|
.set("update_time", new Date())
|
||||||
@ -283,6 +282,25 @@ public class DeviceBizService {
|
|||||||
.set("binding_time", null);
|
.set("binding_time", null);
|
||||||
deviceMapper.update(null, deviceUpdateWrapper);
|
deviceMapper.update(null, deviceUpdateWrapper);
|
||||||
}
|
}
|
||||||
|
QueryWrapper<AppDeviceBindRecord> brqWrapper = new QueryWrapper<>();
|
||||||
|
brqWrapper.eq("device_id", device.getId());
|
||||||
|
brqWrapper.eq("binding_user_id", userId);
|
||||||
|
List<AppDeviceBindRecord> appDeviceBindRecordList = appDeviceBindRecordMapper.selectList(brqWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(appDeviceBindRecordList)) {
|
||||||
|
appDeviceBindRecordList.forEach(appDeviceBindRecord ->
|
||||||
|
appDeviceBindRecordMapper.deleteById(appDeviceBindRecord.getId()));
|
||||||
|
}
|
||||||
|
AppUserVo appUserVo = appUserMapper.selectVoById(userId);
|
||||||
|
if(appUserVo != null){
|
||||||
|
QueryWrapper<AppDeviceShare> appDeviceShareQueryWrapper = new QueryWrapper<>();
|
||||||
|
appDeviceShareQueryWrapper.eq("device_id", device.getId());
|
||||||
|
appDeviceShareQueryWrapper.eq("phonenumber", appUserVo.getPhonenumber());
|
||||||
|
List<AppDeviceShare> appDeviceShareList = appDeviceShareMapper.selectList(appDeviceShareQueryWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(appDeviceShareList)) {
|
||||||
|
appDeviceShareList.forEach(appDeviceShare ->
|
||||||
|
appDeviceShareMapper.deleteById(appDeviceShare.getId()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
QueryWrapper<AppDeviceBindRecord> bindRecordQueryWrapper = new QueryWrapper<>();
|
QueryWrapper<AppDeviceBindRecord> bindRecordQueryWrapper = new QueryWrapper<>();
|
||||||
bindRecordQueryWrapper.eq("device_id", device.getId());
|
bindRecordQueryWrapper.eq("device_id", device.getId());
|
||||||
@ -296,25 +314,17 @@ public class DeviceBizService {
|
|||||||
.set("binding_status", BindingStatusEnum.UNBOUND.getCode())
|
.set("binding_status", BindingStatusEnum.UNBOUND.getCode())
|
||||||
.set("binding_time", null);
|
.set("binding_time", null);
|
||||||
deviceMapper.update(null, deviceUpdateWrapper);
|
deviceMapper.update(null, deviceUpdateWrapper);
|
||||||
}
|
|
||||||
|
|
||||||
QueryWrapper<AppDeviceBindRecord> brqWrapper = new QueryWrapper<>();
|
QueryWrapper<AppDeviceBindRecord> brqWrapper = new QueryWrapper<>();
|
||||||
brqWrapper.eq("device_id", device.getId());
|
brqWrapper.eq("device_id", device.getId());
|
||||||
if(userId != null){
|
|
||||||
brqWrapper.eq("binding_user_id", userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<AppDeviceBindRecord> appDeviceBindRecordList = appDeviceBindRecordMapper.selectList(brqWrapper);
|
List<AppDeviceBindRecord> appDeviceBindRecordList = appDeviceBindRecordMapper.selectList(brqWrapper);
|
||||||
if (CollectionUtil.isNotEmpty(appDeviceBindRecordList)) {
|
if (CollectionUtil.isNotEmpty(appDeviceBindRecordList)) {
|
||||||
appDeviceBindRecordList.forEach(appDeviceBindRecord ->
|
appDeviceBindRecordList.forEach(appDeviceBindRecord ->
|
||||||
appDeviceBindRecordMapper.deleteById(appDeviceBindRecord.getId()));
|
appDeviceBindRecordMapper.deleteById(appDeviceBindRecord.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
AppUserVo appUserVo = appUserMapper.selectVoById(userId);
|
|
||||||
if(appUserVo != null){
|
|
||||||
QueryWrapper<AppDeviceShare> appDeviceShareQueryWrapper = new QueryWrapper<>();
|
QueryWrapper<AppDeviceShare> appDeviceShareQueryWrapper = new QueryWrapper<>();
|
||||||
appDeviceShareQueryWrapper.eq("device_id", device.getId());
|
appDeviceShareQueryWrapper.eq("device_id", device.getId());
|
||||||
appDeviceShareQueryWrapper.eq("phonenumber", appUserVo.getPhonenumber());
|
|
||||||
List<AppDeviceShare> appDeviceShareList = appDeviceShareMapper.selectList(appDeviceShareQueryWrapper);
|
List<AppDeviceShare> appDeviceShareList = appDeviceShareMapper.selectList(appDeviceShareQueryWrapper);
|
||||||
if (CollectionUtil.isNotEmpty(appDeviceShareList)) {
|
if (CollectionUtil.isNotEmpty(appDeviceShareList)) {
|
||||||
appDeviceShareList.forEach(appDeviceShare ->
|
appDeviceShareList.forEach(appDeviceShare ->
|
||||||
@ -384,7 +394,7 @@ public class DeviceBizService {
|
|||||||
|
|
||||||
String deviceImei = device.getDeviceImei();
|
String deviceImei = device.getDeviceImei();
|
||||||
String a = GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX+ deviceImei + DEVICE_LOCATION_KEY_PREFIX + ":history";
|
String a = GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX+ deviceImei + DEVICE_LOCATION_KEY_PREFIX + ":history";
|
||||||
Collection<String> list = RedisUtils.zRangeByScore(a, startTime, endTime);
|
Collection<String> list = RedisUtils.zRangeByScoreDesc(a, startTime, endTime);
|
||||||
if (CollectionUtil.isEmpty(list)) {
|
if (CollectionUtil.isEmpty(list)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -394,6 +394,26 @@ public class RedisUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 根据时间范围查询Sorted Set中的元素并降序排列
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @param startTime 开始时间戳
|
||||||
|
* @param endTime 结束时间戳
|
||||||
|
* @return 指定时间范围内的元素集合(降序)
|
||||||
|
*/
|
||||||
|
public static Collection<String> zRangeByScoreDesc(String key, Long startTime, Long endTime) {
|
||||||
|
try {
|
||||||
|
RScoredSortedSet<String> sortedSet = CLIENT.getScoredSortedSet(key);
|
||||||
|
// 使用 valueRangeReversed 方法获取降序排列的结果
|
||||||
|
return sortedSet.valueRangeReversed(startTime, true, endTime, true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 记录错误日志(如果项目中有日志工具的话)
|
||||||
|
// log.error("根据时间范围查询Sorted Set中的元素失败: key={}, startTime={}, endTime={}, error={}",
|
||||||
|
// key, startTime, endTime, e.getMessage(), e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.fuyuanshen.equipment.domain.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class OnlineStatusVo implements Serializable {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在线状态(0离线,1在线)
|
||||||
|
*/
|
||||||
|
private Integer onlineStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备imei
|
||||||
|
*/
|
||||||
|
private String deviceImei;
|
||||||
|
}
|
||||||
@ -135,4 +135,6 @@ public interface DeviceMapper extends BaseMapper<Device> {
|
|||||||
* @return 设备使用频次统计列表
|
* @return 设备使用频次统计列表
|
||||||
*/
|
*/
|
||||||
List<DeviceUsageFrequencyVo> getDeviceUsageFrequency(@Param("days") int days);
|
List<DeviceUsageFrequencyVo> getDeviceUsageFrequency(@Param("days") int days);
|
||||||
|
|
||||||
|
List<OnlineStatusVo> queryOnlineStatusList();
|
||||||
}
|
}
|
||||||
@ -311,7 +311,7 @@
|
|||||||
AND a.device_name like concat('%',#{bo.deviceName},'%')
|
AND a.device_name like concat('%',#{bo.deviceName},'%')
|
||||||
</if>
|
</if>
|
||||||
<if test="bo.deviceMac != null and bo.deviceMac != ''">
|
<if test="bo.deviceMac != null and bo.deviceMac != ''">
|
||||||
AND a.device_mac = #{bo.deviceMac}
|
AND a.device_mac like concat('%',#{bo.deviceMac},'%')
|
||||||
</if>
|
</if>
|
||||||
<if test="bo.deviceImei != null and bo.deviceImei != ''">
|
<if test="bo.deviceImei != null and bo.deviceImei != ''">
|
||||||
and a.device_imei like concat('%',#{bo.deviceImei}, '%')
|
and a.device_imei like concat('%',#{bo.deviceImei}, '%')
|
||||||
@ -490,5 +490,9 @@
|
|||||||
FROM device
|
FROM device
|
||||||
WHERE device_imei = #{deviceImei}
|
WHERE device_imei = #{deviceImei}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="queryOnlineStatusList" resultType="com.fuyuanshen.equipment.domain.vo.OnlineStatusVo">
|
||||||
|
SELECT a.id, a.online_status AS onlineStatus, a.device_imei AS deviceImei
|
||||||
|
FROM device a left join device_type b on a.device_type = b.id where b.communication_mode in (0, 2) and a.online_status in (1,2)
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user