Compare commits
5 Commits
9cd3b8e496
...
dev_new_co
| Author | SHA1 | Date | |
|---|---|---|---|
| 13eefaf152 | |||
| 4fad6cc929 | |||
| 99d4be4ee4 | |||
| eb1884b972 | |||
| c69e89fa7d |
@@ -86,7 +86,7 @@ public class Device extends BaseDO {
|
||||
*/
|
||||
@ApiModelProperty("固件版本")
|
||||
@Excel(name = "固件版本")
|
||||
private BigDecimal firmwareVersion;
|
||||
private String firmwareVersion;
|
||||
|
||||
/**
|
||||
* WIFI固件版本
|
||||
|
||||
@@ -29,4 +29,8 @@ public class DeviceOtaFirmware extends BaseDO {
|
||||
|
||||
private String deviceType;
|
||||
|
||||
private String fileSize;
|
||||
|
||||
private String description;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
package com.maibu.core.ota;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.maibu.core.domain.BaseDO;
|
||||
import com.maibu.core.enums.OtaOperate;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@Data
|
||||
@@ -24,15 +20,18 @@ public class DeviceOtaOperateRecord extends BaseDO {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private OtaOperate operateType; //操作类型
|
||||
private OtaOperate operateType; // 操作类型
|
||||
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private List<String> deviceModels; //文件名
|
||||
private String fileName; // 名称
|
||||
|
||||
private String fileName; //名称
|
||||
private Long firmwareId; // id
|
||||
|
||||
private Long firmwareId; //id
|
||||
private String deviceId; // 名称
|
||||
|
||||
private String deviceType;
|
||||
|
||||
private String preVersion;
|
||||
|
||||
private String nowVersion;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,15 +2,15 @@ package com.maibu.mqtt;
|
||||
|
||||
public class MqttTopic {
|
||||
|
||||
/** @deprecated 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
/** 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
public static final String DEVICE_STATUS_TOPIC = "device/%s/realTimeMessage";
|
||||
|
||||
public static final String DEVICE_LOCATION_TOPIC = "device/%s/locationMessage";
|
||||
/** @deprecated 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
/** 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
public static final String DEVICE_TASK_STATUS_TOPIC = "task/%s/status";
|
||||
/** @deprecated 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
/** 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
public static final String DEVICE_TASK_ARRIVE_TOPIC = "task/%s/arrive";
|
||||
/** @deprecated 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
/** 通用设备主题,建议使用 MOWER_* 系列主题 */
|
||||
public static final String DEVICE_ERROR_PUSH_TOPIC = "device/%s/error";
|
||||
public static final String DEVICE_ROUTE_PUSH_TOPIC = "device/%s/route/post";
|
||||
|
||||
|
||||
@@ -220,7 +220,7 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
|
||||
device.setDeviceName(data);
|
||||
device.setProductId(-1L);
|
||||
device.setTenantId(-1L);
|
||||
device.setFirmwareVersion(BigDecimal.valueOf(1.0));
|
||||
device.setFirmwareVersion("1.0");
|
||||
device.setProductName("割草机产品MC700");
|
||||
device.setCreateTime(LocalDateTime.now());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
@@ -361,7 +361,7 @@ public class DeviceThreadService {
|
||||
device.setDeviceName(deviceId);
|
||||
device.setProductId(-1L);
|
||||
device.setTenantId(-1L);
|
||||
device.setFirmwareVersion(BigDecimal.valueOf(1.0));
|
||||
device.setFirmwareVersion("1.0");
|
||||
device.setProductName("割草机产品MC700");
|
||||
device.setCreateTime(LocalDateTime.now());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
@@ -43,19 +43,19 @@ public class DeviceOtaController extends BaseController {
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
|
||||
String fileName = "ota/firmware" + firmware.getVersion();
|
||||
String fileName = "ota/firmware/" + firmware.getVersion();
|
||||
|
||||
// 上传到 MinIO
|
||||
minioUtil.uploadFile(fileName, inputStream, "image/png");
|
||||
minioUtil.uploadFile(fileName, inputStream, "application/octet-stream");
|
||||
|
||||
// 获取访问 URL
|
||||
String url = minioUtil.getPublicUrl(fileName);
|
||||
System.out.println("✅ 图片上传成功,访问地址: " + url);
|
||||
System.out.println("固件上传成功,访问地址: " + url);
|
||||
|
||||
// 这里可以把 URL 存到 workRecord 里
|
||||
firmware.setMinioObjectName(fileName);
|
||||
firmware.setFileUrl(url);
|
||||
boolean b = service.saveFirmware(firmware);
|
||||
boolean b = service.saveFirmware(firmware, file.getSize());
|
||||
DeviceOtaOperateRecord record = new DeviceOtaOperateRecord();
|
||||
record.setFileName(firmware.getVersion());
|
||||
record.setFirmwareId(firmware.getId());
|
||||
|
||||
@@ -11,5 +11,5 @@ public class DeviceOtaOperateDTO {
|
||||
|
||||
private OtaOperate operateType;
|
||||
private Long firmwareId;
|
||||
private List<String> deviceIds;
|
||||
private List<String> deviceModels;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
package com.maibu.service.impl;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.maibu.core.business.Device;
|
||||
import com.maibu.core.business.device.NettyDevice;
|
||||
import com.maibu.core.domain.AjaxResult;
|
||||
import com.maibu.core.enums.OtaOperate;
|
||||
import com.maibu.core.ota.DeviceOtaFirmware;
|
||||
import com.maibu.core.ota.DeviceOtaOperateRecord;
|
||||
import com.maibu.dto.BaseQueryDTO;
|
||||
@@ -11,17 +20,11 @@ import com.maibu.dto.DeviceOtaOperateDTO;
|
||||
import com.maibu.mapper.DeviceOtaFirmwareMapper;
|
||||
import com.maibu.mapper.DeviceOtaOperateRecordMapper;
|
||||
import com.maibu.memory.DeviceSessionManager;
|
||||
import com.maibu.service.IDeviceService;
|
||||
import com.maibu.utils.MinioUtil;
|
||||
import com.maibu.utils.SecurityUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -39,12 +42,39 @@ public class DeviceOtaService {
|
||||
@Autowired
|
||||
private DeviceSessionManager deviceSessionManager;
|
||||
|
||||
@Autowired
|
||||
private IDeviceService deviceService;
|
||||
|
||||
private static final int BATCH_SIZE = 5;
|
||||
|
||||
public boolean saveFirmware(DeviceOtaFirmware firmware) {
|
||||
public boolean saveFirmware(DeviceOtaFirmware firmware, Long fileSize) {
|
||||
|
||||
firmware.setCreateTime(LocalDateTime.now());
|
||||
firmware.setCreateBy(SecurityUtils.getUsername());
|
||||
firmware.setFileSize(formatFileSize(fileSize));
|
||||
|
||||
return deviceOtaFirmwareMapper.saveOrUpdate(firmware);
|
||||
}
|
||||
|
||||
public static String formatFileSize(long size) {
|
||||
if (size < 1024) {
|
||||
return size + " B";
|
||||
}
|
||||
|
||||
double kb = size / 1024.0;
|
||||
if (kb < 1024) {
|
||||
return String.format("%.2f KB", kb);
|
||||
}
|
||||
|
||||
double mb = kb / 1024.0;
|
||||
if (mb < 1024) {
|
||||
return String.format("%.2f MB", mb);
|
||||
}
|
||||
|
||||
double gb = mb / 1024.0;
|
||||
return String.format("%.2f GB", gb);
|
||||
}
|
||||
|
||||
public List<DeviceOtaFirmware> listFirmware() {
|
||||
LambdaQueryWrapper<DeviceOtaFirmware> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.orderByDesc(DeviceOtaFirmware::getCreateTime);
|
||||
@@ -55,12 +85,11 @@ public class DeviceOtaService {
|
||||
return deviceOtaFirmwareMapper.selectById(id);
|
||||
}
|
||||
|
||||
|
||||
public AjaxResult upgrade(DeviceOtaOperateDTO dto) {
|
||||
if (dto.getFirmwareId() == null) {
|
||||
return AjaxResult.error("固件ID不能为空");
|
||||
}
|
||||
if (CollectionUtils.isEmpty(dto.getDeviceIds())) {
|
||||
if (CollectionUtils.isEmpty(dto.getDeviceModels())) {
|
||||
return AjaxResult.error("请选择要升级的设备");
|
||||
}
|
||||
|
||||
@@ -69,21 +98,14 @@ public class DeviceOtaService {
|
||||
return AjaxResult.error("固件不存在");
|
||||
}
|
||||
|
||||
DeviceOtaOperateRecord record = new DeviceOtaOperateRecord();
|
||||
record.setOperateType(OtaOperate.upgrade);
|
||||
record.setFirmwareId(firmware.getId());
|
||||
record.setFileName(firmware.getFileName());
|
||||
record.setDeviceType(firmware.getDeviceType());
|
||||
record.setDeviceModels(new ArrayList<>());
|
||||
record.setCreateTime(LocalDateTime.now());
|
||||
record.setCreateBy(SecurityUtils.getUsername());
|
||||
List<DeviceOtaOperateRecord> records = new ArrayList<>();
|
||||
|
||||
int totalDevices = dto.getDeviceIds().size();
|
||||
int totalDevices = dto.getDeviceModels().size();
|
||||
int successCount = 0;
|
||||
int offlineCount = 0;
|
||||
|
||||
for (int i = 0; i < totalDevices; i += BATCH_SIZE) {
|
||||
List<String> batch = dto.getDeviceIds().subList(i, Math.min(i + BATCH_SIZE, totalDevices));
|
||||
List<String> batch = dto.getDeviceModels().subList(i, Math.min(i + BATCH_SIZE, totalDevices));
|
||||
log.info("OTA 灰度升级 第 {} 批, 本批设备数: {}", (i / BATCH_SIZE) + 1, batch.size());
|
||||
|
||||
for (String deviceId : batch) {
|
||||
@@ -96,7 +118,24 @@ public class DeviceOtaService {
|
||||
|
||||
try {
|
||||
device.upgrade(deviceId, firmware);
|
||||
record.getDeviceModels().add(deviceId);
|
||||
|
||||
Device updatedDevice = device.getDevice();
|
||||
|
||||
DeviceOtaOperateRecord record = new DeviceOtaOperateRecord();
|
||||
record.setOperateType(dto.getOperateType());
|
||||
record.setFirmwareId(firmware.getId());
|
||||
record.setFileName(firmware.getFileName());
|
||||
record.setDeviceType(firmware.getDeviceType());
|
||||
record.setCreateTime(LocalDateTime.now());
|
||||
record.setCreateBy(SecurityUtils.getUsername());
|
||||
record.setDeviceId(deviceId);
|
||||
record.setPreVersion(updatedDevice.getFirmwareVersion());
|
||||
record.setNowVersion(firmware.getVersion());
|
||||
records.add(record);
|
||||
|
||||
updatedDevice.setFirmwareVersion(firmware.getVersion());
|
||||
deviceService.updateDeviceBySelf(updatedDevice);
|
||||
|
||||
successCount++;
|
||||
log.info("OTA 升级指令已下发: deviceId={}, version={}", deviceId, firmware.getVersion());
|
||||
} catch (Exception e) {
|
||||
@@ -105,9 +144,7 @@ public class DeviceOtaService {
|
||||
}
|
||||
}
|
||||
|
||||
if (!record.getDeviceModels().isEmpty()) {
|
||||
deviceOtaOperateRecordMapper.insert(record);
|
||||
}
|
||||
deviceOtaOperateRecordMapper.saveOrUpdateBatch(records);
|
||||
|
||||
String msg = String.format("升级指令下发完成: 成功=%d, 离线=%d, 总计=%d",
|
||||
successCount, offlineCount, totalDevices);
|
||||
@@ -115,7 +152,6 @@ public class DeviceOtaService {
|
||||
return AjaxResult.success(msg);
|
||||
}
|
||||
|
||||
|
||||
public int deleteFirmware(List<Long> ids) {
|
||||
if (CollectionUtils.isEmpty(ids)) {
|
||||
return 0;
|
||||
@@ -154,10 +190,7 @@ public class DeviceOtaService {
|
||||
|
||||
public List<DeviceOtaOperateRecord> selectListByDeviceId(String deviceId) {
|
||||
LambdaQueryWrapper<DeviceOtaOperateRecord> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.apply(
|
||||
"JSON_CONTAINS(device_models, {0})",
|
||||
"\"" + deviceId + "\""
|
||||
);
|
||||
wrapper.eq(DeviceOtaOperateRecord::getDeviceId, deviceId);
|
||||
return deviceOtaOperateRecordMapper.selectList(wrapper);
|
||||
}
|
||||
|
||||
|
||||
@@ -98,8 +98,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
r.role_level,
|
||||
GROUP_CONCAT(srm.menu_id) AS menu_list
|
||||
FROM sys_role r
|
||||
LEFT JOIN sys_user_role ur ON ur.role_id = r.role_id
|
||||
LEFT JOIN sys_user u ON u.user_id = ur.user_id
|
||||
<!-- LEFT JOIN sys_user_role ur ON ur.role_id = r.role_id
|
||||
LEFT JOIN sys_user u ON u.user_id = ur.user_id -->
|
||||
LEFT JOIN sys_role_menu srm ON srm.role_id = r.role_id
|
||||
where r.del_flag = 0
|
||||
<if test="roleLevel != null and roleLevel != 0">
|
||||
|
||||
Reference in New Issue
Block a user