Commit 72245b5b0d7d24397cfcf9438c1cfa44577a0be6

Authored by YunaiV
1 parent 7b114e19

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

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceController.java
... ... @@ -167,12 +167,11 @@ public class IotDeviceController {
167 167 return success(true);
168 168 }
169 169  
170   - // TODO @haohao:是不是默认详情接口,不返回 secret,然后这个接口,用于统一返回。然后接口名可以更通用一点。
171   - @GetMapping("/mqtt-connection-params")
172   - @Operation(summary = "获取 MQTT 连接参数")
173   - @PreAuthorize("@ss.hasPermission('iot:device:mqtt-connection-params')")
174   - public CommonResult<IotDeviceMqttConnectionParamsRespVO> getMqttConnectionParams(@RequestParam("deviceId") Long deviceId) {
175   - return success(deviceService.getMqttConnectionParams(deviceId));
  170 + @GetMapping("/get-auth-info")
  171 + @Operation(summary = "获得设备连接信息")
  172 + @PreAuthorize("@ss.hasPermission('iot:device:auth-info')")
  173 + public CommonResult<IotDeviceAuthInfoRespVO> getDeviceAuthInfo(@RequestParam("id") Long id) {
  174 + return success(deviceService.getDeviceAuthInfo(id));
176 175 }
177 176  
178 177 // TODO @haohao:可以使用 @RequestParam("productKey") String productKey, @RequestParam("deviceNames") List<String> deviceNames 来接收哇?
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/device/IotDeviceAuthInfoRespVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.device.vo.device;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import jakarta.validation.constraints.NotBlank;
  5 +import lombok.Data;
  6 +
  7 +@Schema(description = "管理后台 - IoT 设备认证信息 Response VO")
  8 +@Data
  9 +public class IotDeviceAuthInfoRespVO {
  10 +
  11 + @Schema(description = "客户端 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123.device001")
  12 + @NotBlank(message = "客户端 ID 不能为空")
  13 + private String clientId;
  14 +
  15 + @Schema(description = "用户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "device001&product123")
  16 + @NotBlank(message = "用户名不能为空")
  17 + private String username;
  18 +
  19 + @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1a2b3c4d5e6f7890abcdef1234567890")
  20 + @NotBlank(message = "密码不能为空")
  21 + private String password;
  22 +
  23 +}
0 24 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/device/IotDeviceMqttConnectionParamsRespVO.java deleted
1   -package cn.iocoder.yudao.module.iot.controller.admin.device.vo.device;
2   -
3   -import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
4   -import com.alibaba.excel.annotation.ExcelProperty;
5   -import io.swagger.v3.oas.annotations.media.Schema;
6   -import lombok.Data;
7   -
8   -@Schema(description = "管理后台 - IoT 设备 MQTT 连接参数 Response VO")
9   -@Data
10   -@ExcelIgnoreUnannotated
11   -public class IotDeviceMqttConnectionParamsRespVO {
12   -
13   - @Schema(description = "MQTT 客户端 ID", example = "24602")
14   - @ExcelProperty("MQTT 客户端 ID")
15   - private String mqttClientId;
16   -
17   - @Schema(description = "MQTT 用户名", example = "芋艿")
18   - @ExcelProperty("MQTT 用户名")
19   - private String mqttUsername;
20   -
21   - @Schema(description = "MQTT 密码")
22   - @ExcelProperty("MQTT 密码")
23   - private String mqttPassword;
24   -
25   -}
26 0 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java
... ... @@ -216,12 +216,12 @@ public interface IotDeviceService {
216 216 Long getDeviceCount(@Nullable LocalDateTime createTime);
217 217  
218 218 /**
219   - * 获取 MQTT 连接参数
  219 + * 获得设备认证信息
220 220 *
221   - * @param deviceId 设备 ID
  221 + * @param id 设备编号
222 222 * @return MQTT 连接参数
223 223 */
224   - IotDeviceMqttConnectionParamsRespVO getMqttConnectionParams(Long deviceId);
  224 + IotDeviceAuthInfoRespVO getDeviceAuthInfo(Long id);
225 225  
226 226 /**
227 227 * 获得各个产品下的设备数量 Map
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java
... ... @@ -409,17 +409,13 @@ public class IotDeviceServiceImpl implements IotDeviceService {
409 409 return respVO;
410 410 }
411 411  
412   - // TODO @芋艿:改成通用的;
413 412 @Override
414   - public IotDeviceMqttConnectionParamsRespVO getMqttConnectionParams(Long deviceId) {
415   - IotDeviceDO device = validateDeviceExists(deviceId);
416   -// MqttSignResult mqttSignResult = MqttSignUtils.calculate(device.getProductKey(), device.getDeviceName(),
417   -// device.getDeviceSecret());
418   -// return new IotDeviceMqttConnectionParamsRespVO()
419   -// .setMqttClientId(mqttSignResult.getClientId())
420   -// .setMqttUsername(mqttSignResult.getUsername())
421   -// .setMqttPassword(mqttSignResult.getPassword());
422   - return null;
  413 + public IotDeviceAuthInfoRespVO getDeviceAuthInfo(Long id) {
  414 + IotDeviceDO device = validateDeviceExists(id);
  415 + // 使用 IotDeviceAuthUtils 生成认证信息
  416 + IotDeviceAuthUtils.AuthInfo authInfo = IotDeviceAuthUtils.getAuthInfo(
  417 + device.getProductKey(), device.getDeviceName(), device.getDeviceSecret());
  418 + return BeanUtils.toBean(authInfo, IotDeviceAuthInfoRespVO.class);
423 419 }
424 420  
425 421 private void deleteDeviceCache(IotDeviceDO device) {
... ...