86 lines
2.9 KiB
Java
86 lines
2.9 KiB
Java
|
|
package com.ruoyi.device.controller;
|
|||
|
|
|
|||
|
|
import com.ruoyi.common.core.domain.R;
|
|||
|
|
import com.ruoyi.common.core.web.controller.BaseController;
|
|||
|
|
import com.ruoyi.device.domain.api.IThingsBoardDomain;
|
|||
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|||
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|||
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|||
|
|
import lombok.Data;
|
|||
|
|
import org.slf4j.Logger;
|
|||
|
|
import org.slf4j.LoggerFactory;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.web.bind.annotation.*;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* ThingsBoard设备属性管理Controller
|
|||
|
|
*
|
|||
|
|
* @author ruoyi
|
|||
|
|
* @date 2026-02-06
|
|||
|
|
*/
|
|||
|
|
@RestController
|
|||
|
|
@RequestMapping("/thingsboard")
|
|||
|
|
@Tag(name = "ThingsBoard设备属性管理", description = "提供设备属性的读写操作")
|
|||
|
|
public class ThingsBoardController extends BaseController {
|
|||
|
|
|
|||
|
|
private static final Logger log = LoggerFactory.getLogger(ThingsBoardController.class);
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
private IThingsBoardDomain thingsBoardDomain;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 设置设备属性
|
|||
|
|
*
|
|||
|
|
* @param request 设置属性请求
|
|||
|
|
* @return 操作结果
|
|||
|
|
*/
|
|||
|
|
@PostMapping("/attribute")
|
|||
|
|
@Operation(summary = "设置设备属性", description = "设置ThingsBoard设备的属性值")
|
|||
|
|
public R<Void> setDeviceAttribute(@RequestBody SetAttributeRequest request) {
|
|||
|
|
log.info("收到设置设备属性请求: deviceIotId={}, key={}, value={}",
|
|||
|
|
request.getDeviceIotId(), request.getKey(), request.getValue());
|
|||
|
|
|
|||
|
|
// 参数校验
|
|||
|
|
if (request.getDeviceIotId() == null || request.getDeviceIotId().trim().isEmpty()) {
|
|||
|
|
return R.fail("设备IOT ID不能为空");
|
|||
|
|
}
|
|||
|
|
if (request.getKey() == null || request.getKey().trim().isEmpty()) {
|
|||
|
|
return R.fail("属性键不能为空");
|
|||
|
|
}
|
|||
|
|
if (request.getValue() == null) {
|
|||
|
|
return R.fail("属性值不能为空");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 调用Domain层设置属性
|
|||
|
|
boolean success = thingsBoardDomain.setDeviceAttribute(
|
|||
|
|
request.getDeviceIotId(),
|
|||
|
|
request.getKey(),
|
|||
|
|
request.getValue()
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
if (success) {
|
|||
|
|
log.info("设备属性设置成功: deviceIotId={}, key={}",
|
|||
|
|
request.getDeviceIotId(), request.getKey());
|
|||
|
|
return R.ok();
|
|||
|
|
} else {
|
|||
|
|
log.error("设备属性设置失败: deviceIotId={}, key={}",
|
|||
|
|
request.getDeviceIotId(), request.getKey());
|
|||
|
|
return R.fail("设备属性设置失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 设置属性请求对象
|
|||
|
|
*/
|
|||
|
|
@Data
|
|||
|
|
public static class SetAttributeRequest {
|
|||
|
|
@Parameter(description = "设备IOT ID(ThingsBoard设备ID)", required = true)
|
|||
|
|
private String deviceIotId;
|
|||
|
|
|
|||
|
|
@Parameter(description = "属性键", required = true)
|
|||
|
|
private String key;
|
|||
|
|
|
|||
|
|
@Parameter(description = "属性值", required = true)
|
|||
|
|
private Object value;
|
|||
|
|
}
|
|||
|
|
}
|