13 Commits

10 changed files with 183 additions and 59 deletions

View File

@ -53,7 +53,7 @@ public class ReceiverMessageHandler implements MessageHandler {
if(StringUtils.isNotBlank(deviceImei)){
String queueKey = MqttMessageQueueConstants.MQTT_MESSAGE_QUEUE_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 ;
RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1", Duration.ofSeconds(360));

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.vo.OnlineStatusVo;
import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
import org.springframework.beans.factory.annotation.Autowired;
@ -24,20 +25,16 @@ public class OnlineStatusTask {
// 使用cron表达式每分钟的第0秒执行
@Scheduled(cron = "0 */3 * * * ?")
public void cronTask() {
QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
List<Device> devices = deviceMapper.selectList(queryWrapper);
devices.forEach(item -> {
List<OnlineStatusVo> onlineStatusVos = deviceMapper.queryOnlineStatusList();
onlineStatusVos.forEach(item -> {
String onlineStatusKey = GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + item.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX;
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)){
UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", item.getId());
updateWrapper.set("online_status", 0);
deviceMapper.update(updateWrapper);
}
}
});
}
}

View File

@ -59,9 +59,15 @@ public class DeviceGeoFenceController extends BaseController {
@PostMapping("/export")
public void export(DeviceGeoFenceBo bo, HttpServletResponse response) {
List<DeviceGeoFenceVo> list = deviceGeoFenceService.queryList(bo);
// 设置区域类型名称
list.forEach(item -> {
item.setAreaTypeNameByAreaType();
item.setIsActiveNameByIsActive(); // 添加这行来设置激活状态名称
});
ExcelUtil.exportExcel(list, "电子围栏", DeviceGeoFenceVo.class, response);
}
/**
* 获取电子围栏详细信息
*

View File

@ -179,7 +179,7 @@ public class DeviceBizService {
}
Device device = devices.get(0);
if (device.getBindingStatus() != null && device.getBindingStatus() == BindingStatusEnum.BOUND.getCode()) {
throw new RuntimeException("设备已绑定");
throw new RuntimeException("设备已经被绑定");
}
QueryWrapper<AppDeviceBindRecord> bindRecordQueryWrapper = new QueryWrapper<>();
@ -189,7 +189,6 @@ public class DeviceBizService {
if (appDeviceBindRecord != null) {
UpdateWrapper<AppDeviceBindRecord> deviceUpdateWrapper = new UpdateWrapper<>();
deviceUpdateWrapper.eq("device_id", device.getId())
.set("binding_status", BindingStatusEnum.BOUND.getCode())
.set("binding_user_id", userId)
.set("communication_mode", 0)
.set("update_time", new Date())
@ -283,6 +282,25 @@ public class DeviceBizService {
.set("binding_time", null);
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{
QueryWrapper<AppDeviceBindRecord> bindRecordQueryWrapper = new QueryWrapper<>();
bindRecordQueryWrapper.eq("device_id", device.getId());
@ -296,25 +314,17 @@ public class DeviceBizService {
.set("binding_status", BindingStatusEnum.UNBOUND.getCode())
.set("binding_time", null);
deviceMapper.update(null, deviceUpdateWrapper);
}
QueryWrapper<AppDeviceBindRecord> brqWrapper = new QueryWrapper<>();
brqWrapper.eq("device_id", device.getId());
if(userId != null){
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 ->
@ -384,7 +394,7 @@ public class DeviceBizService {
String deviceImei = device.getDeviceImei();
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)) {
return null;
}

View File

@ -394,6 +394,26 @@ public class RedisUtils {
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;
}
}
/**

View File

@ -1,6 +1,7 @@
package com.fuyuanshen.equipment.domain.vo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
import cn.idev.excel.annotation.ExcelProperty;
@ -15,7 +16,6 @@ import java.io.Serializable;
import java.util.Date;
/**
* 电子围栏视图对象 device_geo_fence
*
@ -33,7 +33,7 @@ public class DeviceGeoFenceVo implements Serializable {
/**
* 围栏唯一标识
*/
@ExcelProperty(value = "围栏唯一标识")
// @ExcelProperty(value = "围栏唯一标识")
private Long id;
/**
@ -42,22 +42,22 @@ public class DeviceGeoFenceVo implements Serializable {
@ExcelProperty(value = "围栏名称")
private String name;
/**
* 围栏描述
*/
@ExcelProperty(value = "围栏描述")
private String description;
/**
* 围栏区域类型, 0 POLYGON, 1 CIRCLE
*/
@ExcelProperty(value = "围栏区域类型, 0 POLYGON, 1 CIRCLE")
// @ExcelProperty(value = "围栏区域类型")
private Integer areaType;
/**
* 围栏区域类型, 0 POLYGON 多边形, 1 CIRCLE 圆形
*/
@ExcelProperty(value = "围栏区域类型")
private String areaTypeName;
/**
* 围栏坐标数据
*/
@ExcelProperty(value = "围栏坐标数据")
// @ExcelProperty(value = "围栏坐标数据")
private String coordinates;
/**
@ -69,26 +69,78 @@ public class DeviceGeoFenceVo implements Serializable {
/**
* 是否激活
*/
@ExcelProperty(value = "是否激活")
// @ExcelProperty(value = "是否激活")
private Long isActive;
/**
* 激活状态名称
*/
@ExcelProperty(value = "是否激活")
private String isActiveName;
/**
* 围栏描述
*/
@ExcelProperty(value = "围栏描述")
private String description;
/**
* 创建人
*/
@ExcelProperty(value = "创建人")
// @ExcelProperty(value = "创建人")
private Long createBy;
/**
* 创建时间
*/
@ExcelProperty(value = "创建时间")
// @ExcelProperty(value = "创建时间")
private Date createTime;
/**
* 更新时间
*/
@ExcelProperty(value = "更新时间")
// @ExcelProperty(value = "更新时间")
private Date updateTime;
public void setAreaTypeNameByAreaType() {
if (this.areaType != null) {
switch (this.areaType) {
case 0:
this.areaTypeName = "多边形";
break;
case 1:
this.areaTypeName = "圆形";
break;
default:
this.areaTypeName = "未知";
break;
}
} else {
this.areaTypeName = "未知";
}
}
public void setIsActiveNameByIsActive() {
if (this.isActive != null) {
switch (this.isActive.intValue()) {
case 0:
this.isActiveName = "未激活";
break;
case 1:
this.isActiveName = "已激活";
break;
default:
this.isActiveName = "未知";
break;
}
} else {
this.isActiveName = "未知";
}
}
}

View File

@ -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;
}

View File

@ -135,4 +135,6 @@ public interface DeviceMapper extends BaseMapper<Device> {
* @return 设备使用频次统计列表
*/
List<DeviceUsageFrequencyVo> getDeviceUsageFrequency(@Param("days") int days);
List<OnlineStatusVo> queryOnlineStatusList();
}

View File

@ -14,6 +14,7 @@ import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.stream.Collectors;
/**
@ -74,30 +75,39 @@ public class DeviceExportService {
}
// 在DeviceExportService中添加并发控制
private static final Semaphore imageLoadSemaphore = new Semaphore(5); // 最多同时加载5张图片
private void handleDevicePic(Device device, DeviceExcelExportDTO dto) {
String picUrl = device.getDevicePic();
log.info("处理设备图片设备ID: {}, 图片URL: {}", device.getId(), picUrl);
log.debug("处理设备图片设备ID: {}, 图片URL: {}", device.getId(), picUrl);
if (picUrl != null && !picUrl.trim().isEmpty()) {
try {
// 获取加载图片的许可
imageLoadSemaphore.acquire();
try {
// 自动将HTTP转换为HTTPS以避免重定向问题
if (picUrl.startsWith("http://")) {
picUrl = "https://" + picUrl.substring(7);
log.info("自动将HTTP转换为HTTPS: {}", picUrl);
log.debug("自动将HTTP转换为HTTPS: {}", picUrl);
}
// 尝试创建URL对象会自动验证格式
URL url = new URL(picUrl);
dto.setDevicePic(url);
log.info("成功设置设备图片URL到DTO");
log.debug("成功设置设备图片URL到DTO");
} finally {
// 释放许可
imageLoadSemaphore.release();
}
} catch (Exception e) {
// 不是有效URL时设置为null
log.info("设置设备图片失败设备ID: {}, URL: {}, 错误: {}", device.getId(), picUrl, e.getMessage());
log.warn("设置设备图片失败设备ID: {}, URL: {}, 错误: {}", device.getId(), picUrl, e.getMessage());
dto.setDevicePic(null);
}
} else {
log.info("设备没有设置图片设备ID: {}", device.getId());
log.debug("设备没有设置图片设备ID: {}", device.getId());
dto.setDevicePic(null);
}
}

View File

@ -311,7 +311,7 @@
AND a.device_name like concat('%',#{bo.deviceName},'%')
</if>
<if test="bo.deviceMac != null and bo.deviceMac != ''">
AND a.device_mac = #{bo.deviceMac}
AND a.device_mac like concat('%',#{bo.deviceMac},'%')
</if>
<if test="bo.deviceImei != null and bo.deviceImei != ''">
and a.device_imei like concat('%',#{bo.deviceImei}, '%')
@ -490,5 +490,9 @@
FROM device
WHERE device_imei = #{deviceImei}
</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>