Commit 638976dac81a7098b0e8d563379a05e7fd4d6c8b

Authored by YunaiV
1 parent a9733b4d

【代码评审】IoT:整体实现

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/control/IotDeviceUpstreamServiceImpl.java
... ... @@ -280,11 +280,11 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService {
280 280 sendDeviceMessage(message, device);
281 281 }
282 282  
  283 + // TODO @芋艿:后续需要考虑,http 的认证
283 284 @Override
284 285 public boolean authenticateEmqxConnection(IotDeviceEmqxAuthReqDTO authReqDTO) {
285 286 log.info("[authenticateEmqxConnection][认证 Emqx 连接: {}]", authReqDTO);
286   - // 1. 校验设备是否存在
287   - // username 格式:${DeviceName}&${ProductKey}
  287 + // 1.1 校验设备是否存在。username 格式:${DeviceName}&${ProductKey}
288 288 String[] usernameParts = authReqDTO.getUsername().split("&");
289 289 if (usernameParts.length != 2) {
290 290 log.error("[authenticateEmqxConnection][认证失败,username 格式不正确]");
... ... @@ -292,17 +292,19 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService {
292 292 }
293 293 String deviceName = usernameParts[0];
294 294 String productKey = usernameParts[1];
295   - IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache(
296   - productKey, deviceName);
  295 + // 1.2 获得设备
  296 + IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache(productKey, deviceName);
297 297 if (device == null) {
298   - log.error("[authenticateEmqxConnection][设备({}/{}) 不存在]",
299   - productKey, deviceName);
  298 + log.error("[authenticateEmqxConnection][设备({}/{}) 不存在]", productKey, deviceName);
300 299 return false;
301 300 }
  301 + // TODO @haohao:需要记录,记录设备的最后时间
  302 +
302 303 // 2. 校验密码
303 304 String deviceSecret = device.getDeviceSecret();
304 305 String clientId = authReqDTO.getClientId();
305 306 MqttSignResult sign = MqttSignUtils.calculate(productKey, deviceName, deviceSecret, clientId);
  307 + // TODO 建议,先失败,return false;
306 308 if (StrUtil.equals(sign.getPassword(), authReqDTO.getPassword())) {
307 309 log.info("[authenticateEmqxConnection][认证成功]");
308 310 return true;
... ...