【功能新增】IoT:验证通过独立、内嵌模式的调用

This commit is contained in:
YunaiV
2025-01-21 19:38:41 +08:00
parent a152f6d98f
commit 916024b891
18 changed files with 294 additions and 63 deletions

View File

@@ -124,7 +124,7 @@
<!-- 其他依赖项 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- PF4J Spring 集成 -->
<dependency>

View File

@@ -1,13 +1,26 @@
package cn.iocoder.yudao.module.iot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// TODO @haohao建议包名cn.iocoder.yudao.module.iot.plugin.${pluginName},例如说 http。然后子包如下
// config方配置类以及 HttpVertxPlugin 初始化
// service放 HttpVertxHandler 逻辑;
@SpringBootApplication
public class HttpPluginSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(HttpPluginSpringbootApplication.class, args);
// SpringApplication.run(HttpPluginSpringbootApplication.class, args);
SpringApplication application = new SpringApplication(HttpPluginSpringbootApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
}
}
}
// TODO @haohao如下是 sdk 的包cn.iocoder.yudao.module.iot.plugin.sdk
// 1. api 包:实现 DeviceDataApi 接口,通过 resttemplate 调用
// 2. config 包:初始化 DeviceDataApi 等等
// 3. 其中 resttemplate 调用的后端地址通过每个服务的 application.yaml 进行注入

View File

@@ -1,23 +1,59 @@
package cn.iocoder.yudao.module.iot.config;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
import cn.iocoder.yudao.module.iot.api.device.dto.DeviceDataCreateReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceEventReportReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStatusUpdateReqDTO;
import org.pf4j.DefaultPluginManager;
import org.pf4j.PluginWrapper;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
// TODO 芋艿:临时实现;
@Configuration
public class TestConfiguration {
// @Resource
// private RestTemplate restTemplate;
// TODO 芋艿:这里,后续看看怎么创建好点
@Bean
public DeviceDataApi deviceDataApi() {
public RestTemplate restTemplate() {
return new RestTemplateBuilder().build();
}
@Bean
public DeviceDataApi deviceDataApi(RestTemplate restTemplate) {
return new DeviceDataApi() {
@Override
public void saveDeviceData(DeviceDataCreateReqDTO createDTO) {
System.out.println("saveDeviceData");
public CommonResult<Boolean> updateDeviceStatus(IotDeviceStatusUpdateReqDTO updateReqDTO) {
// TODO haohao待实现
return null;
}
@Override
public CommonResult<Boolean> reportDeviceEventData(IotDeviceEventReportReqDTO reportReqDTO) {
// TODO haohao待实现
return null;
}
@Override
public CommonResult<Boolean> reportDevicePropertyData(IotDevicePropertyReportReqDTO reportReqDTO) {
// TODO haohao待完整实现
String url = "http://127.0.0.1:48080/rpc-api/iot/device-data/report-property";
try {
restTemplate.postForObject(url, reportReqDTO, CommonResult.class);
return success(true);
} catch (Exception e) {
e.printStackTrace();
return CommonResult.error(400, "error");
}
}
};

View File

@@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.iot.service;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
import cn.iocoder.yudao.module.iot.api.device.dto.DeviceDataCreateReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
import io.vertx.core.Handler;
import io.vertx.ext.web.RequestBody;
import io.vertx.ext.web.RoutingContext;
@@ -46,12 +46,12 @@ public class HttpVertxHandler implements Handler<RoutingContext> {
try {
// 调用主程序的接口保存数据
DeviceDataCreateReqDTO createDTO = DeviceDataCreateReqDTO.builder()
IotDevicePropertyReportReqDTO createDTO = IotDevicePropertyReportReqDTO.builder()
.productKey(productKey)
.deviceName(deviceName)
.message(jsonData.toString())
.params(jsonData) // TODO 芋艿:这块要优化
.build();
deviceDataApi.saveDeviceData(createDTO);
deviceDataApi.reportDevicePropertyData(createDTO);
// 构造成功响应内容
JSONObject successRes = createResponseJson(