代码优化

This commit is contained in:
2025-07-31 18:09:20 +08:00
parent 94ac8454ec
commit 370783b457
8 changed files with 121 additions and 60 deletions

View File

@ -11,6 +11,6 @@ public class DeviceInstructDto {
/** /**
* 下发指令 * 下发指令
*/ */
private Object instructValue; private String instructValue;
} }

View File

@ -13,7 +13,6 @@ import com.fuyuanshen.app.domain.dto.APPReNameDTO;
import com.fuyuanshen.app.domain.dto.AppDeviceLogoUploadDto; import com.fuyuanshen.app.domain.dto.AppDeviceLogoUploadDto;
import com.fuyuanshen.app.domain.dto.DeviceInstructDto; import com.fuyuanshen.app.domain.dto.DeviceInstructDto;
import com.fuyuanshen.app.domain.vo.APPDeviceTypeVo; import com.fuyuanshen.app.domain.vo.APPDeviceTypeVo;
import com.fuyuanshen.app.domain.vo.AppDeviceBindRecordVo;
import com.fuyuanshen.app.domain.vo.AppDeviceDetailVo; import com.fuyuanshen.app.domain.vo.AppDeviceDetailVo;
import com.fuyuanshen.app.domain.vo.AppPersonnelInfoVo; import com.fuyuanshen.app.domain.vo.AppPersonnelInfoVo;
import com.fuyuanshen.app.mapper.AppDeviceBindRecordMapper; import com.fuyuanshen.app.mapper.AppDeviceBindRecordMapper;
@ -38,12 +37,9 @@ import com.fuyuanshen.equipment.enums.BindingStatusEnum;
import com.fuyuanshen.equipment.enums.CommunicationModeEnum; import com.fuyuanshen.equipment.enums.CommunicationModeEnum;
import com.fuyuanshen.equipment.mapper.DeviceMapper; import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper; import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.*;
import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHexToDecimal;
import com.fuyuanshen.equipment.utils.c.ReliableTextToBitmap; import com.fuyuanshen.equipment.utils.c.ReliableTextToBitmap;
import com.fuyuanshen.global.mqtt.config.MqttGateway; import com.fuyuanshen.global.mqtt.config.MqttGateway;
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
import com.fuyuanshen.global.mqtt.constants.MqttConstants; import com.fuyuanshen.global.mqtt.constants.MqttConstants;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -53,6 +49,11 @@ import org.springframework.web.multipart.MultipartFile;
import java.time.Duration; import java.time.Duration;
import java.util.*; import java.util.*;
import static com.fuyuanshen.common.core.constant.GlobalConstants.GLOBAL_REDIS_KEY;
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.buildArr;
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.generateFixedBitmapData;
import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHexToDecimal;
@Slf4j @Slf4j
@Service @Service
@ -117,6 +118,31 @@ public class AppDeviceBizService {
bo.setBindingUserId(userId); bo.setBindingUserId(userId);
} }
Page<AppDeviceVo> result = deviceMapper.queryAppBindDeviceList(pageQuery.build(), bo); Page<AppDeviceVo> result = deviceMapper.queryAppBindDeviceList(pageQuery.build(), bo);
List<AppDeviceVo> records = result.getRecords();
if(records != null && !records.isEmpty()){
records.forEach(item -> {
if(item.getCommunicationMode()!=null && item.getCommunicationMode() == 0){
//设备在线状态
String onlineStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX + item.getDeviceImei());
if(StringUtils.isNotBlank(onlineStatus)){
item.setOnlineStatus(1);
}else{
item.setOnlineStatus(0);
}
String deviceStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_STATUS_KEY_PREFIX + item.getDeviceImei());
// 获取电量
if(StringUtils.isNotBlank(deviceStatus)){
JSONObject jsonObject = JSONObject.parseObject(deviceStatus);
item.setBattery(jsonObject.getString("batteryPercentage"));
}else{
item.setBattery("0");
}
}
});
}
return TableDataInfo.build(result); return TableDataInfo.build(result);
} }
@ -275,42 +301,26 @@ public class AppDeviceBizService {
AppPersonnelInfoVo personnelInfoVo = MapstructUtils.convert(appPersonnelInfo, AppPersonnelInfoVo.class); AppPersonnelInfoVo personnelInfoVo = MapstructUtils.convert(appPersonnelInfo, AppPersonnelInfoVo.class);
vo.setPersonnelInfo(personnelInfoVo); vo.setPersonnelInfo(personnelInfoVo);
} }
return vo; //设备在线状态
String onlineStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX + device.getDeviceImei());
if(StringUtils.isNotBlank(onlineStatus)){
vo.setOnlineStatus(1);
}else{
vo.setOnlineStatus(0);
} }
String deviceStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_STATUS_KEY_PREFIX + device.getDeviceImei());
public static void main(String[] args) { // 获取电量
byte[] unitName = generateFixedBitmapData("富源晟科技", 120); if(StringUtils.isNotBlank(deviceStatus)){
byte[] position = generateFixedBitmapData("研发", 120); JSONObject jsonObject = JSONObject.parseObject(deviceStatus);
byte[] name = generateFixedBitmapData("张三", 120); vo.setMainLightMode(jsonObject.getString("mainLightMode"));
byte[] id = generateFixedBitmapData("123456", 120); vo.setLaserLightMode(jsonObject.getString("laserLightMode"));
// int[] intUnitNames = Bitmap80x12Generator.convertHexToDecimal(unitName); vo.setBatteryPercentage(jsonObject.getString("batteryPercentage"));
// int[] intPosition = Bitmap80x12Generator.convertHexToDecimal(position); vo.setChargeState(jsonObject.getString("chargeState"));
// int[] intNames = Bitmap80x12Generator.convertHexToDecimal(position); vo.setBatteryRemainingTime(jsonObject.getString("batteryRemainingTime"));
// int[] intIds = Bitmap80x12Generator.convertHexToDecimal(position); }else{
// Map<String, Object> map = new HashMap<>(); vo.setBatteryPercentage("0");
// map.put("instruct", 2); }
// System.out.println(JSON.toJSONString( map)); return vo;
// StringBuilder sb = new StringBuilder();
// sb.append("[")
// buildStr(unitName, sb);
// System.out.println(sb.toString());
// Object[] arr = new Object[]{2, Bitmap80x12Generator , Arrays.toString(name), Arrays.toString(id)};
// System.out.println(Arrays.deepToString(arr));
// int[] a = new int[]{6,6,6,6,6,6};
ArrayList<Integer> intData = new ArrayList<>();
intData.add(2);
buildArr(convertHexToDecimal(unitName), intData);
buildArr(convertHexToDecimal(position), intData);
buildArr(convertHexToDecimal(name), intData);
buildArr(convertHexToDecimal(id), intData);
intData.add(0);
intData.add(0);
intData.add(0);
intData.add(0);
Map<String, Object> map = new HashMap<>();
map.put("instruct", intData);
System.out.println(JSON.toJSONString(map));
} }
@ -373,9 +383,9 @@ public class AppDeviceBizService {
System.out.println("原始数据大小: " + largeData.length + " 字节"); System.out.println("原始数据大小: " + largeData.length + " 字节");
int[] ints = convertHexToDecimal(largeData); int[] ints = convertHexToDecimal(largeData);
RedisUtils.setCacheObject("app_logo_data:" + device.getDeviceImei(), Arrays.toString(ints), Duration.ofSeconds(30 * 60L)); RedisUtils.setCacheObject(GLOBAL_REDIS_KEY+"app_logo_data:" + device.getDeviceImei(), Arrays.toString(ints), Duration.ofSeconds(30 * 60L));
String data = RedisUtils.getCacheObject("app_logo_data:" + device.getDeviceImei()); String data = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY+"app_logo_data:" + device.getDeviceImei());
byte[] arr = ImageToCArrayConverter.convertStringToByteArray(data); byte[] arr = ImageToCArrayConverter.convertStringToByteArray(data);
byte[] specificChunk = ImageToCArrayConverter.getChunk(arr, 0, 512); byte[] specificChunk = ImageToCArrayConverter.getChunk(arr, 0, 512);
@ -410,7 +420,7 @@ public class AppDeviceBizService {
if(device == null){ if(device == null){
throw new ServiceException("设备不存在"); throw new ServiceException("设备不存在");
} }
Integer instructValue = (Integer) params.getInstructValue(); Integer instructValue = Integer.parseInt(params.getInstructValue());
ArrayList<Integer> intData = new ArrayList<>(); ArrayList<Integer> intData = new ArrayList<>();
intData.add(1); intData.add(1);
intData.add(instructValue); intData.add(instructValue);
@ -434,7 +444,7 @@ public class AppDeviceBizService {
if(device == null){ if(device == null){
throw new ServiceException("设备不存在"); throw new ServiceException("设备不存在");
} }
String instructValue = (String)params.getInstructValue(); String instructValue = params.getInstructValue();
ArrayList<Integer> intData = new ArrayList<>(); ArrayList<Integer> intData = new ArrayList<>();
intData.add(5); intData.add(5);
String[] values = instructValue.split("\\."); String[] values = instructValue.split("\\.");
@ -466,7 +476,7 @@ public class AppDeviceBizService {
if(device == null){ if(device == null){
throw new ServiceException("设备不存在"); throw new ServiceException("设备不存在");
} }
Integer instructValue = (Integer) params.getInstructValue(); Integer instructValue = Integer.parseInt(params.getInstructValue());
ArrayList<Integer> intData = new ArrayList<>(); ArrayList<Integer> intData = new ArrayList<>();
intData.add(4); intData.add(4);
intData.add(instructValue); intData.add(instructValue);

View File

@ -0,0 +1,12 @@
package com.fuyuanshen.global.mqtt.constants;
public class DeviceRedisKeyConstants {
// 将设备上报状态
public static final String DEVICE_STATUS_KEY_PREFIX = "device:status:";
// 在线状态
public static final String DEVICE_ONLINE_STATUS_KEY_PREFIX = "device:onlineStatus:";
// 将设备状态信息存储到Redis中
public static final String DEVICE_LOCATION_KEY_PREFIX = "device:location:";
// 存储到一个列表中,保留历史位置信息
public static final String DEVICE_LOCATION_HISTORY_KEY_PREFIX = "device:location:history:";
}

View File

@ -1,10 +1,12 @@
package com.fuyuanshen.global.mqtt.rule; package com.fuyuanshen.global.mqtt.rule;
import com.fuyuanshen.common.core.constant.GlobalConstants;
import com.fuyuanshen.common.json.utils.JsonUtils; import com.fuyuanshen.common.json.utils.JsonUtils;
import com.fuyuanshen.common.redis.utils.RedisUtils; import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.global.mqtt.base.MqttMessageRule; import com.fuyuanshen.global.mqtt.base.MqttMessageRule;
import com.fuyuanshen.global.mqtt.base.MqttRuleContext; import com.fuyuanshen.global.mqtt.base.MqttRuleContext;
import com.fuyuanshen.global.mqtt.config.MqttGateway; import com.fuyuanshen.global.mqtt.config.MqttGateway;
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants; import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -78,11 +80,14 @@ public class ActiveReportingDeviceDataRule implements MqttMessageRule {
deviceInfo.put("timestamp", System.currentTimeMillis()); deviceInfo.put("timestamp", System.currentTimeMillis());
// 将设备状态信息存储到Redis中 // 将设备状态信息存储到Redis中
String deviceRedisKey = "device:status:" + deviceImei; String deviceRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_STATUS_KEY_PREFIX + deviceImei;
String deviceInfoJson = JsonUtils.toJsonString(deviceInfo); String deviceInfoJson = JsonUtils.toJsonString(deviceInfo);
// 存储到Redis设置过期时间例如24小时 // 存储到Redis设置过期时间例如24小时
RedisUtils.setCacheObject(deviceRedisKey, deviceInfoJson, Duration.ofSeconds(24 * 60 * 60)); RedisUtils.setCacheObject(deviceRedisKey, deviceInfoJson);
//在线状态
String deviceOnlineStatusRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX + deviceImei;
RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1");
log.info("设备状态信息已异步发送到Redis: device={}, mainLightMode={}, laserLightMode={}, batteryPercentage={}", log.info("设备状态信息已异步发送到Redis: device={}, mainLightMode={}, laserLightMode={}, batteryPercentage={}",
deviceImei, mainLightMode, laserLightMode, batteryPercentage); deviceImei, mainLightMode, laserLightMode, batteryPercentage);

View File

@ -1,5 +1,6 @@
package com.fuyuanshen.global.mqtt.rule; package com.fuyuanshen.global.mqtt.rule;
import com.fuyuanshen.common.core.constant.GlobalConstants;
import com.fuyuanshen.common.core.utils.ImageToCArrayConverter; import com.fuyuanshen.common.core.utils.ImageToCArrayConverter;
import com.fuyuanshen.common.core.utils.StringUtils; import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.json.utils.JsonUtils; import com.fuyuanshen.common.json.utils.JsonUtils;
@ -26,13 +27,13 @@ import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHex
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
public class PersonnelInfoRule implements MqttMessageRule { public class DeviceBootLogoRule implements MqttMessageRule {
private final MqttGateway mqttGateway; private final MqttGateway mqttGateway;
@Override @Override
public String getCommandType() { public String getCommandType() {
return LightingCommandTypeConstants.PERSONNEL_INFO; return LightingCommandTypeConstants.BOOT_LOGO;
} }
@Override @Override
@ -43,7 +44,7 @@ public class PersonnelInfoRule implements MqttMessageRule {
return; return;
} }
String data = RedisUtils.getCacheObject("894078:app_logo_data:" + context.getDeviceImei()); String data = RedisUtils.getCacheObject(GlobalConstants.GLOBAL_REDIS_KEY+"app_logo_data:" + context.getDeviceImei());
if (StringUtils.isEmpty(data)) { if (StringUtils.isEmpty(data)) {
return; return;
} }

View File

@ -1,5 +1,6 @@
package com.fuyuanshen.global.mqtt.rule; package com.fuyuanshen.global.mqtt.rule;
import com.fuyuanshen.common.core.constant.GlobalConstants;
import com.fuyuanshen.common.core.utils.StringUtils; import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.json.utils.JsonUtils; import com.fuyuanshen.common.json.utils.JsonUtils;
import com.fuyuanshen.common.redis.utils.RedisUtils; import com.fuyuanshen.common.redis.utils.RedisUtils;
@ -8,6 +9,7 @@ import com.fuyuanshen.equipment.utils.map.LngLonUtil;
import com.fuyuanshen.global.mqtt.base.MqttMessageRule; import com.fuyuanshen.global.mqtt.base.MqttMessageRule;
import com.fuyuanshen.global.mqtt.base.MqttRuleContext; import com.fuyuanshen.global.mqtt.base.MqttRuleContext;
import com.fuyuanshen.global.mqtt.config.MqttGateway; import com.fuyuanshen.global.mqtt.config.MqttGateway;
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants; import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants;
import com.fuyuanshen.global.mqtt.constants.MqttConstants; import com.fuyuanshen.global.mqtt.constants.MqttConstants;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -118,12 +120,16 @@ public class LocationDataRule implements MqttMessageRule {
// 将位置信息存储到Redis中 // 将位置信息存储到Redis中
String redisKey = "device:location:" + deviceImei; String redisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_LOCATION_KEY_PREFIX + deviceImei;
String locationJson = JsonUtils.toJsonString(locationInfo); String locationJson = JsonUtils.toJsonString(locationInfo);
// 存储到Redis // 存储到Redis
RedisUtils.setCacheObject(redisKey, locationJson, Duration.ofSeconds(24 * 60 * 60)); RedisUtils.setCacheObject(redisKey, locationJson);
// 存储到一个列表中,保留历史位置信息
String locationHistoryKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_LOCATION_HISTORY_KEY_PREFIX + deviceImei;
RedisUtils.addCacheList(locationHistoryKey, locationJson);
RedisUtils.expire(locationHistoryKey, Duration.ofDays(90));
log.info("位置信息已异步发送到Redis: device={}, lat={}, lon={}", deviceImei, latitude, longitude); log.info("位置信息已异步发送到Redis: device={}, lat={}, lon={}", deviceImei, latitude, longitude);
} catch (Exception e) { } catch (Exception e) {
log.error("异步发送位置信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e); log.error("异步发送位置信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e);
@ -138,9 +144,11 @@ public class LocationDataRule implements MqttMessageRule {
ArrayList<Integer> intData = new ArrayList<>(); ArrayList<Integer> intData = new ArrayList<>();
intData.add(11); intData.add(11);
intData.add(Integer.parseInt(latArr[0])); intData.add(Integer.parseInt(latArr[0]));
intData.add(Integer.parseInt(latArr[1])); String str1 = latArr[1];
intData.add(Integer.parseInt(str1.substring(0,4)));
String str2 = lonArr[1];
intData.add(Integer.parseInt(lonArr[0])); intData.add(Integer.parseInt(lonArr[0]));
intData.add(Integer.parseInt(lonArr[1])); intData.add(Integer.parseInt(str2.substring(0,4)));
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("instruct", intData); map.put("instruct", intData);

View File

@ -16,11 +16,6 @@ public class AppDeviceDetailVo {
@ExcelProperty(value = "设备ID") @ExcelProperty(value = "设备ID")
private Long deviceId; private Long deviceId;
/**
* 手机号
*/
@ExcelProperty(value = "手机号")
private String phonenumber;
/** /**
* 设备名称 * 设备名称
@ -74,4 +69,25 @@ public class AppDeviceDetailVo {
* 发送信息 * 发送信息
*/ */
private String sendMsg; private String sendMsg;
//"{\"deviceImei\":\"AA\",\"mainLightMode\":\"1\",\"laserLightMode\":\"0\",\"batteryPercentage\":\"60\",\"chargeState\":\"1\",\"batteryRemainingTime\":\"200\",\"timestamp\":1753871635241}"
//设备主灯档位
private String mainLightMode;
//激光灯档位
private String laserLightMode;
//电量百分比
private String batteryPercentage;
//充电状态0没有充电1正在充电2为已充满
private String chargeState;
//电池剩余续航时间200分钟
private String batteryRemainingTime;
/**
* 在线状态(0离线1在线)
*/
private Integer onlineStatus;
} }

View File

@ -57,4 +57,13 @@ public class AppDeviceVo implements Serializable {
*/ */
private Date bindingTime; private Date bindingTime;
/**
* 在线状态(0离线1在线)
*/
private Integer onlineStatus;
/**
* 电量 百分比
*/
private String battery;
} }