feat:【IoT 物联网】优化获取认证信息的逻辑

This commit is contained in:
YunaiV 2025-06-14 09:48:05 +08:00
parent 7b114e1986
commit 72245b5b0d
5 changed files with 37 additions and 44 deletions

View File

@ -167,12 +167,11 @@ public class IotDeviceController {
return success(true);
}
// TODO @haohao是不是默认详情接口不返回 secret然后这个接口用于统一返回然后接口名可以更通用一点
@GetMapping("/mqtt-connection-params")
@Operation(summary = "获取 MQTT 连接参数")
@PreAuthorize("@ss.hasPermission('iot:device:mqtt-connection-params')")
public CommonResult<IotDeviceMqttConnectionParamsRespVO> getMqttConnectionParams(@RequestParam("deviceId") Long deviceId) {
return success(deviceService.getMqttConnectionParams(deviceId));
@GetMapping("/get-auth-info")
@Operation(summary = "获得设备连接信息")
@PreAuthorize("@ss.hasPermission('iot:device:auth-info')")
public CommonResult<IotDeviceAuthInfoRespVO> getDeviceAuthInfo(@RequestParam("id") Long id) {
return success(deviceService.getDeviceAuthInfo(id));
}
// TODO @haohao可以使用 @RequestParam("productKey") String productKey, @RequestParam("deviceNames") List<String> deviceNames 来接收哇

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.iot.controller.admin.device.vo.device;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
@Schema(description = "管理后台 - IoT 设备认证信息 Response VO")
@Data
public class IotDeviceAuthInfoRespVO {
@Schema(description = "客户端 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123.device001")
@NotBlank(message = "客户端 ID 不能为空")
private String clientId;
@Schema(description = "用户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "device001&product123")
@NotBlank(message = "用户名不能为空")
private String username;
@Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1a2b3c4d5e6f7890abcdef1234567890")
@NotBlank(message = "密码不能为空")
private String password;
}

View File

@ -1,25 +0,0 @@
package cn.iocoder.yudao.module.iot.controller.admin.device.vo.device;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "管理后台 - IoT 设备 MQTT 连接参数 Response VO")
@Data
@ExcelIgnoreUnannotated
public class IotDeviceMqttConnectionParamsRespVO {
@Schema(description = "MQTT 客户端 ID", example = "24602")
@ExcelProperty("MQTT 客户端 ID")
private String mqttClientId;
@Schema(description = "MQTT 用户名", example = "芋艿")
@ExcelProperty("MQTT 用户名")
private String mqttUsername;
@Schema(description = "MQTT 密码")
@ExcelProperty("MQTT 密码")
private String mqttPassword;
}

View File

@ -216,12 +216,12 @@ public interface IotDeviceService {
Long getDeviceCount(@Nullable LocalDateTime createTime);
/**
* MQTT 连接参数
* 得设备认证信息
*
* @param deviceId 设备 ID
* @param id 设备编号
* @return MQTT 连接参数
*/
IotDeviceMqttConnectionParamsRespVO getMqttConnectionParams(Long deviceId);
IotDeviceAuthInfoRespVO getDeviceAuthInfo(Long id);
/**
* 获得各个产品下的设备数量 Map

View File

@ -409,17 +409,13 @@ public class IotDeviceServiceImpl implements IotDeviceService {
return respVO;
}
// TODO @芋艿改成通用的
@Override
public IotDeviceMqttConnectionParamsRespVO getMqttConnectionParams(Long deviceId) {
IotDeviceDO device = validateDeviceExists(deviceId);
// MqttSignResult mqttSignResult = MqttSignUtils.calculate(device.getProductKey(), device.getDeviceName(),
// device.getDeviceSecret());
// return new IotDeviceMqttConnectionParamsRespVO()
// .setMqttClientId(mqttSignResult.getClientId())
// .setMqttUsername(mqttSignResult.getUsername())
// .setMqttPassword(mqttSignResult.getPassword());
return null;
public IotDeviceAuthInfoRespVO getDeviceAuthInfo(Long id) {
IotDeviceDO device = validateDeviceExists(id);
// 使用 IotDeviceAuthUtils 生成认证信息
IotDeviceAuthUtils.AuthInfo authInfo = IotDeviceAuthUtils.getAuthInfo(
device.getProductKey(), device.getDeviceName(), device.getDeviceSecret());
return BeanUtils.toBean(authInfo, IotDeviceAuthInfoRespVO.class);
}
private void deleteDeviceCache(IotDeviceDO device) {