【新增功能】IoT: HTTP 插件模块

This commit is contained in:
安浩浩
2024-12-20 18:57:40 +08:00
parent b2434b7b41
commit a2532013ec
33 changed files with 690 additions and 304 deletions

View File

@@ -0,0 +1,25 @@
package cn.iocoder.yudao.module.iot.api.device;
import cn.iocoder.yudao.module.iot.api.DeviceDataApi;
import cn.iocoder.yudao.module.iot.service.device.IotDeviceDataService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
/**
* 设备数据 API 实现类
*/
@Service
@Validated
public class DeviceDataApiImpl implements DeviceDataApi {
@Resource
private IotDeviceDataService iotDeviceDataService;
@Override
public void saveDeviceData(String productKey, String deviceName, String message) {
iotDeviceDataService.saveDeviceData(productKey, deviceName, message);
}
}

View File

@@ -0,0 +1 @@
package cn.iocoder.yudao.module.iot.api;

View File

@@ -1,41 +0,0 @@
/*
* Copyright (C) 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.iocoder.yudao.module.iot.controller.admin.plugininfo;
import cn.iocoder.yudao.module.iot.api.Greeting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 打招呼 测试用例
*/
@Component
public class Greetings {
@Autowired
private List<Greeting> greetings;
public Integer printGreetings() {
System.out.printf("找到扩展点的 %d 个扩展 '%s'%n", greetings.size(), Greeting.class.getName());
for (Greeting greeting : greetings) {
System.out.println(">>> " + greeting.getGreeting());
}
return greetings.size();
}
}

View File

@@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.iot.controller.admin.plugininfo;
import jakarta.annotation.Resource;
import org.pf4j.spring.SpringPluginManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@@ -25,12 +24,8 @@ import java.util.stream.Collectors;
@RequestMapping("/iot/plugins")
public class PluginController {
@Resource
private ApplicationContext applicationContext;
@Resource
private SpringPluginManager springPluginManager;
@Resource
private Greetings greetings;
@Value("${pf4j.pluginsDir}")
private String pluginsDir;
@@ -73,10 +68,8 @@ public class PluginController {
return ResponseEntity.ok("插件上传并加载成功");
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("上传插件时发生错误: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("加载插件时发生错误: " + e.getMessage());
}
}
@@ -120,15 +113,4 @@ public class PluginController {
return ResponseEntity.ok(plugins);
}
/**
* 打印问候语
*
* @return 问候语数量
*/
@PermitAll
@GetMapping("/printGreetings")
public ResponseEntity<Integer> printGreetings() {
Integer count = greetings.printGreetings();
return ResponseEntity.ok(count);
}
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright (C) 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.iocoder.yudao.module.iot.controller.admin.plugininfo;
import cn.iocoder.yudao.module.iot.api.Greeting;
import org.pf4j.Extension;
import org.springframework.stereotype.Component;
/**
* 打招呼 测试用例
*/
@Extension
@Component
public class WhazzupGreeting implements Greeting {
@Override
public String getGreeting() {
return "Whazzup";
}
}

View File

@@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import com.alibaba.excel.annotation.*;

View File

@@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import jakarta.validation.constraints.*;
@Schema(description = "管理后台 - IoT 插件实例新增/修改 Request VO")

View File

@@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.iot.framework.plugin;
import cn.iocoder.yudao.module.iot.api.DeviceDataApi;
import cn.iocoder.yudao.module.iot.api.ServiceRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@Slf4j
@Configuration
public class ServiceRegistryConfiguration {
@Resource
private DeviceDataApi deviceDataApi;
@PostConstruct
public void init() {
// 将主程序中的 DeviceDataApi 实例注册到 ServiceRegistry
ServiceRegistry.registerService(DeviceDataApi.class, deviceDataApi);
log.info("[init][将 DeviceDataApi 实例注册到 ServiceRegistry 中]");
}
/**
* 定义一个标记用的 Bean用于表示 ServiceRegistry 已初始化完成
*/
@Bean("serviceRegistryInitializedMarker")
public Object serviceRegistryInitializedMarker() {
// 返回任意对象即可这里返回null都可以但最好返回个实际对象
return new Object();
}
}

View File

@@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.iot.framework.plugin;
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.Greetings;
import org.pf4j.spring.SpringPluginManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -10,14 +9,9 @@ import org.springframework.context.annotation.DependsOn;
public class SpringConfiguration {
@Bean
@DependsOn("serviceRegistryInitializedMarker")
public SpringPluginManager pluginManager() {
return new SpringPluginManager();
}
@Bean
@DependsOn("pluginManager")
public Greetings greetings() {
return new Greetings();
}
}

View File

@@ -20,7 +20,8 @@ public interface IotDeviceDataService {
*
* @param productKey 产品 key
* @param deviceName 设备名称
* @param message 消息
* @param message 消息
* <p>JSON 格式,参见 <a href="https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services?spm=a2c4g.11186623.0.0.3a3335aeUdzkz2#concept-mvc-4tw-y2b">...</a>
*/
void saveDeviceData(String productKey, String deviceName, String message);

View File

@@ -22,9 +22,7 @@ import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;
@@ -220,24 +218,24 @@ public class PluginInfoServiceImpl implements PluginInfoService {
pluginInfoMapper.updateById(pluginInfoDo);
}
@PostConstruct
public void init() {
Executors.newSingleThreadScheduledExecutor().schedule(this::startPlugins, 3, TimeUnit.SECONDS);
}
@SneakyThrows
private void startPlugins() {
for (PluginInfoDO pluginInfoDO : pluginInfoMapper.selectList()) {
if (!IotPluginStatusEnum.RUNNING.getStatus().equals(pluginInfoDO.getStatus())) {
continue;
}
log.info("start plugin:{}", pluginInfoDO.getPluginId());
try {
pluginManager.startPlugin(pluginInfoDO.getPluginId());
} catch (Exception e) {
log.error("start plugin error", e);
}
}
}
// @PostConstruct
// public void init() {
// Executors.newSingleThreadScheduledExecutor().schedule(this::startPlugins, 3, TimeUnit.SECONDS);
// }
//
// @SneakyThrows
// private void startPlugins() {
// for (PluginInfoDO pluginInfoDO : pluginInfoMapper.selectList()) {
// if (!IotPluginStatusEnum.RUNNING.getStatus().equals(pluginInfoDO.getStatus())) {
// continue;
// }
// log.info("start plugin:{}", pluginInfoDO.getPluginId());
// try {
// pluginManager.startPlugin(pluginInfoDO.getPluginId());
// } catch (Exception e) {
// log.error("start plugin error", e);
// }
// }
// }
}

View File

@@ -122,7 +122,7 @@ public class IotProductServiceImpl implements IotProductService {
thingModelFunctionService.createSuperTableDataModel(id);
// 3.2 创建物模型日志超级表数据模型
thingModelMessageService.createSuperTable(id);
}x
}
productMapper.updateById(updateObj);
}