Commit 2d18e218c7dfb6f21c23c99c19387309f2d52238
1 parent
6a7aa3c3
【代码新增】IoT:增加 IotDeviceDownstreamHandler 接口的定义,以及 IotDeviceDownstreamServer 的部分实现
Showing
27 changed files
with
369 additions
and
54 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/IotDeviceUpstreamApi.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.api.device; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceEventReportReqDTO; | |
| 5 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; | |
| 6 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 4 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEventReportReqDTO; | |
| 5 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO; | |
| 6 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 7 | 7 | import cn.iocoder.yudao.module.iot.enums.ApiConstants; |
| 8 | 8 | import jakarta.validation.Valid; |
| 9 | 9 | import org.springframework.web.bind.annotation.PostMapping; | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/downstream/IotDeviceDownstreamAbstractReqDTO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.api.device.dto.control.downstream; | |
| 2 | + | |
| 3 | +import jakarta.validation.constraints.NotEmpty; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * IoT 设备下行的抽象 Request DTO | |
| 8 | + * | |
| 9 | + * @author 芋道源码 | |
| 10 | + */ | |
| 11 | +@Data | |
| 12 | +public abstract class IotDeviceDownstreamAbstractReqDTO { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * 请求编号 | |
| 16 | + */ | |
| 17 | + private String requestId; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * 产品标识 | |
| 21 | + */ | |
| 22 | + @NotEmpty(message = "产品标识不能为空") | |
| 23 | + private String productKey; | |
| 24 | + /** | |
| 25 | + * 设备名称 | |
| 26 | + */ | |
| 27 | + @NotEmpty(message = "设备名称不能为空") | |
| 28 | + private String deviceName; | |
| 29 | + | |
| 30 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/downstream/IotDevicePropertyGetReqDTO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.api.device.dto.control.downstream; | |
| 2 | + | |
| 3 | +import jakarta.validation.constraints.NotEmpty; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +import java.util.List; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * IoT 设备【属性】获取 Request DTO | |
| 10 | + * | |
| 11 | + * @author 芋道源码 | |
| 12 | + */ | |
| 13 | +@Data | |
| 14 | +public class IotDevicePropertyGetReqDTO extends IotDeviceDownstreamAbstractReqDTO { | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * 属性标识数组 | |
| 18 | + */ | |
| 19 | + @NotEmpty(message = "属性标识数组不能为空") | |
| 20 | + private List<String> identifiers; | |
| 21 | + | |
| 22 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/downstream/IotDevicePropertySetReqDTO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.api.device.dto.control.downstream; | |
| 2 | + | |
| 3 | +import jakarta.validation.constraints.NotEmpty; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +import java.util.Map; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * IoT 设备【属性】设置 Request DTO | |
| 10 | + * | |
| 11 | + * @author 芋道源码 | |
| 12 | + */ | |
| 13 | +@Data | |
| 14 | +public class IotDevicePropertySetReqDTO extends IotDeviceDownstreamAbstractReqDTO { | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * 属性参数 | |
| 18 | + */ | |
| 19 | + @NotEmpty(message = "属性参数不能为空") | |
| 20 | + private Map<String, Object> properties; | |
| 21 | + | |
| 22 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/downstream/IotDeviceServiceInvokeReqDTO.java
0 → 100644
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/IotDeviceEventReportReqDTO.java renamed to yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/upstream/IotDeviceEventReportReqDTO.java
| 1 | -package cn.iocoder.yudao.module.iot.api.device.dto; | |
| 1 | +package cn.iocoder.yudao.module.iot.api.device.dto.control.upstream; | |
| 2 | 2 | |
| 3 | 3 | import jakarta.validation.constraints.NotEmpty; |
| 4 | 4 | import lombok.Data; |
| ... | ... | @@ -6,7 +6,7 @@ import lombok.Data; |
| 6 | 6 | import java.util.Map; |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * IoT 设备【事件】数据上报 Request DTO | |
| 9 | + * IoT 设备【事件】上报 Request DTO | |
| 10 | 10 | */ |
| 11 | 11 | @Data |
| 12 | 12 | public class IotDeviceEventReportReqDTO extends IotDeviceUpstreamAbstractReqDTO { | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/IotDevicePropertyReportReqDTO.java renamed to yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/upstream/IotDevicePropertyReportReqDTO.java
| 1 | -package cn.iocoder.yudao.module.iot.api.device.dto; | |
| 1 | +package cn.iocoder.yudao.module.iot.api.device.dto.control.upstream; | |
| 2 | 2 | |
| 3 | 3 | import jakarta.validation.constraints.NotEmpty; |
| 4 | 4 | import lombok.Data; |
| ... | ... | @@ -6,7 +6,7 @@ import lombok.Data; |
| 6 | 6 | import java.util.Map; |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * IoT 设备【属性】数据上报 Request DTO | |
| 9 | + * IoT 设备【属性】上报 Request DTO | |
| 10 | 10 | */ |
| 11 | 11 | @Data |
| 12 | 12 | public class IotDevicePropertyReportReqDTO extends IotDeviceUpstreamAbstractReqDTO { | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/IotDeviceStateUpdateReqDTO.java renamed to yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/upstream/IotDeviceStateUpdateReqDTO.java
| 1 | -package cn.iocoder.yudao.module.iot.api.device.dto; | |
| 1 | +package cn.iocoder.yudao.module.iot.api.device.dto.control.upstream; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStateEnum; |
| ... | ... | @@ -6,7 +6,7 @@ import jakarta.validation.constraints.NotNull; |
| 6 | 6 | import lombok.Data; |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * IoT 设备状态更新 Request DTO | |
| 9 | + * IoT 设备【状态】更新 Request DTO | |
| 10 | 10 | */ |
| 11 | 11 | @Data |
| 12 | 12 | public class IotDeviceStateUpdateReqDTO extends IotDeviceUpstreamAbstractReqDTO { | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/IotDeviceUpstreamAbstractReqDTO.java renamed to yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/control/upstream/IotDeviceUpstreamAbstractReqDTO.java
| 1 | -package cn.iocoder.yudao.module.iot.api.device.dto; | |
| 1 | +package cn.iocoder.yudao.module.iot.api.device.dto.control.upstream; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.json.databind.TimestampLocalDateTimeSerializer; |
| 4 | 4 | import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ... | ... |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/device/dto/package-info.java
0 → 100644
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceUpstreamApiImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.api.device; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceEventReportReqDTO; | |
| 5 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; | |
| 6 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 4 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEventReportReqDTO; | |
| 5 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO; | |
| 6 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 7 | 7 | import cn.iocoder.yudao.module.iot.service.device.control.IotDeviceUpstreamService; |
| 8 | 8 | import org.springframework.validation.annotation.Validated; |
| 9 | 9 | import org.springframework.web.bind.annotation.RestController; | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/job/device/IotDeviceOfflineCheckJob.java
| ... | ... | @@ -5,7 +5,7 @@ import cn.hutool.core.util.IdUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 6 | 6 | import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; |
| 7 | 7 | import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; |
| 8 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 8 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 9 | 9 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| 10 | 10 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStateEnum; |
| 11 | 11 | import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/consumer/device/IotDeviceOnlineMessageConsumer.java
| ... | ... | @@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.iot.mq.consumer.device; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.IdUtil; |
| 4 | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 5 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 6 | 6 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| 7 | 7 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageIdentifierEnum; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageTypeEnum; | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/control/IotDeviceDownstreamServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.service.device.control; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.control.IotDeviceSimulationUpstreamReqVO; |
| 4 | +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; | |
| 5 | +import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageIdentifierEnum; | |
| 6 | +import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageTypeEnum; | |
| 7 | +import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; | |
| 8 | +import jakarta.annotation.Resource; | |
| 4 | 9 | import lombok.extern.slf4j.Slf4j; |
| 5 | 10 | import org.springframework.stereotype.Service; |
| 6 | 11 | import org.springframework.validation.annotation.Validated; |
| 7 | 12 | |
| 13 | +import java.util.Objects; | |
| 14 | + | |
| 8 | 15 | /** |
| 9 | 16 | * 设备下行 Service 实现类 |
| 10 | 17 | * |
| ... | ... | @@ -15,9 +22,53 @@ import org.springframework.validation.annotation.Validated; |
| 15 | 22 | @Slf4j |
| 16 | 23 | public class IotDeviceDownstreamServiceImpl implements IotDeviceDownstreamService { |
| 17 | 24 | |
| 25 | + @Resource | |
| 26 | + private IotDeviceService deviceService; | |
| 27 | + | |
| 18 | 28 | @Override |
| 19 | 29 | public void simulationDeviceDownstream(IotDeviceSimulationUpstreamReqVO downstreamReqVO) { |
| 30 | + // 校验设备是否存在 | |
| 31 | + IotDeviceDO device = deviceService.validateDeviceExists(downstreamReqVO.getId()); | |
| 32 | + // TODO 芋艿:父设备的处理 | |
| 33 | + IotDeviceDO parentDevice = null; | |
| 34 | + | |
| 35 | + // 服务调用 | |
| 36 | + if (Objects.equals(downstreamReqVO.getType(), IotDeviceMessageTypeEnum.SERVICE.getType())) { | |
| 37 | + invokeDeviceService(downstreamReqVO, device, parentDevice); | |
| 38 | + return; | |
| 39 | + } | |
| 40 | + // 属性相关 | |
| 41 | + if (Objects.equals(downstreamReqVO.getType(), IotDeviceMessageTypeEnum.PROPERTY.getType())) | |
| 42 | + // 属性设置 | |
| 43 | + if (Objects.equals(downstreamReqVO.getIdentifier(), IotDeviceMessageIdentifierEnum.PROPERTY_SET.getIdentifier())) { | |
| 44 | + setDeviceProperty(downstreamReqVO, device, parentDevice); | |
| 45 | + return; | |
| 46 | + } | |
| 47 | + // 属性设置 | |
| 48 | + if (Objects.equals(downstreamReqVO.getIdentifier(), IotDeviceMessageIdentifierEnum.PROPERTY_GET.getIdentifier())) { | |
| 49 | + getDeviceProperty(downstreamReqVO, device, parentDevice); | |
| 50 | + return; | |
| 51 | + } | |
| 52 | + // TODO 芋艿:ota 升级 | |
| 53 | + // TODO 芋艿:配置下发 | |
| 54 | + } | |
| 55 | + | |
| 56 | + private void invokeDeviceService(IotDeviceSimulationUpstreamReqVO downstreamReqVO, | |
| 57 | + IotDeviceDO device, IotDeviceDO parentDevice) { | |
| 58 | + // 校验服务是否存在 | |
| 59 | + // TODO 芋艿:这里需要校验服务是否存在 | |
| 60 | + // 调用服务 | |
| 61 | + // TODO 芋艿:这里需要调用服务 | |
| 62 | + } | |
| 63 | + | |
| 64 | + private void setDeviceProperty(IotDeviceSimulationUpstreamReqVO downstreamReqVO, | |
| 65 | + IotDeviceDO device, IotDeviceDO parentDevice) { | |
| 66 | + // TODO 芋艿:这里需要设置设备属性 | |
| 67 | + } | |
| 20 | 68 | |
| 69 | + private void getDeviceProperty(IotDeviceSimulationUpstreamReqVO downstreamReqVO, | |
| 70 | + IotDeviceDO device, IotDeviceDO parentDevice) { | |
| 71 | + // TODO 芋艿:这里需要获取设备属性 | |
| 21 | 72 | } |
| 22 | 73 | |
| 23 | 74 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/control/IotDeviceUpstreamService.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.service.device.control; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceEventReportReqDTO; | |
| 4 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; | |
| 5 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 3 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEventReportReqDTO; | |
| 4 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO; | |
| 5 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 6 | 6 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.control.IotDeviceSimulationUpstreamReqVO; |
| 7 | 7 | import jakarta.validation.Valid; |
| 8 | 8 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/control/IotDeviceUpstreamServiceImpl.java
| ... | ... | @@ -7,10 +7,10 @@ import cn.hutool.core.util.StrUtil; |
| 7 | 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| 9 | 9 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 10 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceEventReportReqDTO; | |
| 11 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; | |
| 12 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 13 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceUpstreamAbstractReqDTO; | |
| 10 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEventReportReqDTO; | |
| 11 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO; | |
| 12 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 13 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceUpstreamAbstractReqDTO; | |
| 14 | 14 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.control.IotDeviceSimulationUpstreamReqVO; |
| 15 | 15 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| 16 | 16 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageIdentifierEnum; | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml
| ... | ... | @@ -18,6 +18,7 @@ |
| 18 | 18 | </description> |
| 19 | 19 | |
| 20 | 20 | <dependencies> |
| 21 | + <!-- TODO 芋艿:依赖整理 --> | |
| 21 | 22 | <dependency> |
| 22 | 23 | <groupId>cn.iocoder.boot</groupId> |
| 23 | 24 | <artifactId>yudao-common</artifactId> |
| ... | ... | @@ -29,27 +30,26 @@ |
| 29 | 30 | <version>${revision}</version> |
| 30 | 31 | </dependency> |
| 31 | 32 | |
| 32 | - <!-- 其他依赖项 --> | |
| 33 | 33 | <dependency> |
| 34 | - <groupId>org.springframework.boot</groupId> | |
| 35 | - <artifactId>spring-boot-starter-web</artifactId> | |
| 34 | + <groupId>org.projectlombok</groupId> | |
| 35 | + <artifactId>lombok</artifactId> | |
| 36 | 36 | </dependency> |
| 37 | 37 | |
| 38 | - <!-- PF4J --> | |
| 39 | 38 | <dependency> |
| 40 | - <groupId>org.pf4j</groupId> | |
| 41 | - <artifactId>pf4j-spring</artifactId> | |
| 42 | - <exclusions> | |
| 43 | - <exclusion> | |
| 44 | - <groupId>org.slf4j</groupId> | |
| 45 | - <artifactId>slf4j-log4j12</artifactId> | |
| 46 | - </exclusion> | |
| 47 | - </exclusions> | |
| 39 | + <groupId>org.springframework</groupId> | |
| 40 | + <artifactId>spring-web</artifactId> | |
| 48 | 41 | </dependency> |
| 49 | 42 | |
| 43 | + <!-- 其他依赖项 --> | |
| 50 | 44 | <dependency> |
| 51 | - <groupId>org.projectlombok</groupId> | |
| 52 | - <artifactId>lombok</artifactId> | |
| 45 | + <groupId>org.springframework.boot</groupId> | |
| 46 | + <artifactId>spring-boot-starter</artifactId> | |
| 47 | + </dependency> | |
| 48 | + | |
| 49 | + <!-- Vert.x Web --> | |
| 50 | + <dependency> | |
| 51 | + <groupId>io.vertx</groupId> | |
| 52 | + <artifactId>vertx-web</artifactId> | |
| 53 | 53 | </dependency> |
| 54 | 54 | </dependencies> |
| 55 | 55 | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/config/YudaoDeviceDataApiAutoConfiguration.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.plugin.common.config; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; |
| 4 | -import cn.iocoder.yudao.module.iot.plugin.common.api.DeviceDataApiClient; | |
| 4 | +import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamHandler; | |
| 5 | +import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamServer; | |
| 6 | +import cn.iocoder.yudao.module.iot.plugin.common.core.upstream.IotDeviceUpstreamClient; | |
| 5 | 7 | import org.springframework.beans.factory.annotation.Value; |
| 6 | 8 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 7 | 9 | import org.springframework.boot.web.client.RestTemplateBuilder; |
| ... | ... | @@ -10,6 +12,7 @@ import org.springframework.web.client.RestTemplate; |
| 10 | 12 | |
| 11 | 13 | import java.time.Duration; |
| 12 | 14 | |
| 15 | +// TODO @芋艿:配置类的名字 | |
| 13 | 16 | /** |
| 14 | 17 | * 设备数据 API 初始化器 |
| 15 | 18 | * |
| ... | ... | @@ -31,8 +34,8 @@ public class YudaoDeviceDataApiAutoConfiguration { |
| 31 | 34 | public RestTemplate restTemplate() { |
| 32 | 35 | // 如果你有更多的自定义需求,比如连接池、超时时间等,可以在这里设置 |
| 33 | 36 | return new RestTemplateBuilder() |
| 34 | - .setConnectTimeout(Duration.ofMillis(5000)) // 设置连接超时时间 | |
| 35 | - .setReadTimeout(Duration.ofMillis(5000)) // 设置读取超时时间 | |
| 37 | + .connectTimeout(Duration.ofMillis(5000)) // 设置连接超时时间 | |
| 38 | + .readTimeout(Duration.ofMillis(5000)) // 设置读取超时时间 | |
| 36 | 39 | .build(); |
| 37 | 40 | } |
| 38 | 41 | |
| ... | ... | @@ -44,7 +47,13 @@ public class YudaoDeviceDataApiAutoConfiguration { |
| 44 | 47 | */ |
| 45 | 48 | @Bean |
| 46 | 49 | public IotDeviceUpstreamApi deviceDataApi(RestTemplate restTemplate) { |
| 47 | - return new DeviceDataApiClient(restTemplate, deviceDataUrl); | |
| 50 | + return new IotDeviceUpstreamClient(restTemplate, deviceDataUrl); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Bean(initMethod = "start", destroyMethod = "stop") | |
| 54 | + @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | |
| 55 | + public IotDeviceDownstreamServer deviceDownstreamServer(IotDeviceDownstreamHandler deviceDownstreamHandler) { | |
| 56 | + return new IotDeviceDownstreamServer(deviceDownstreamHandler); | |
| 48 | 57 | } |
| 49 | 58 | |
| 50 | 59 | } |
| 51 | 60 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/downstream/IotDeviceDownstreamHandler.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.plugin.common.core.downstream; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 4 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertyGetReqDTO; | |
| 5 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertySetReqDTO; | |
| 6 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceServiceInvokeReqDTO; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * IoT 设备下行处理器 | |
| 10 | + * | |
| 11 | + * 目的:每个 plugin 需要实现,用于处理 server 下行的指令(请求),从而实现从 server => plugin => device 的下行流程 | |
| 12 | + * | |
| 13 | + * @author 芋道源码 | |
| 14 | + */ | |
| 15 | +public interface IotDeviceDownstreamHandler { | |
| 16 | + | |
| 17 | + CommonResult<Boolean> invokeDeviceService(IotDeviceServiceInvokeReqDTO invokeReqDTO); | |
| 18 | + | |
| 19 | + CommonResult<Boolean> getDeviceProperty(IotDevicePropertyGetReqDTO getReqDTO); | |
| 20 | + | |
| 21 | + CommonResult<Boolean> setDeviceProperty(IotDevicePropertySetReqDTO setReqDTO); | |
| 22 | + | |
| 23 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/downstream/IotDeviceDownstreamServer.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.plugin.common.core.downstream; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.router.IotDeviceServiceInvokeVertxHandler; | |
| 4 | +import io.vertx.core.Vertx; | |
| 5 | +import io.vertx.core.http.HttpServer; | |
| 6 | +import io.vertx.ext.web.Router; | |
| 7 | +import io.vertx.ext.web.handler.BodyHandler; | |
| 8 | +import lombok.extern.slf4j.Slf4j; | |
| 9 | + | |
| 10 | +@Slf4j | |
| 11 | +public class IotDeviceDownstreamServer { | |
| 12 | + | |
| 13 | + private final Vertx vertx; | |
| 14 | + private final HttpServer server; | |
| 15 | + | |
| 16 | + public IotDeviceDownstreamServer(IotDeviceDownstreamHandler deviceDownstreamHandler) { | |
| 17 | + // 创建 Vertx 实例 | |
| 18 | + this.vertx = Vertx.vertx(); | |
| 19 | + // 创建 Router 实例 | |
| 20 | + Router router = Router.router(vertx); | |
| 21 | + router.route().handler(BodyHandler.create()); // 处理 Body | |
| 22 | + router.post(IotDeviceServiceInvokeVertxHandler.PATH).handler( | |
| 23 | + new IotDeviceServiceInvokeVertxHandler(deviceDownstreamHandler)); // 处理 Service Invoke | |
| 24 | + // 创建 HttpServer 实例 | |
| 25 | + this.server = vertx.createHttpServer().requestHandler(router); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public void start() { | |
| 29 | + log.info("[start][开始启动]"); | |
| 30 | + server.listen(0) // 通过 0 自动选择端口 | |
| 31 | + .toCompletionStage() | |
| 32 | + .toCompletableFuture() | |
| 33 | + .join(); | |
| 34 | + log.info("[start][启动完成,端口({})]", this.server.actualPort()); | |
| 35 | + } | |
| 36 | + | |
| 37 | + public void stop() { | |
| 38 | + log.info("[stop][开始关闭]"); | |
| 39 | + try { | |
| 40 | + // 关闭 HTTP 服务器 | |
| 41 | + if (server != null) { | |
| 42 | + server.close() | |
| 43 | + .toCompletionStage() | |
| 44 | + .toCompletableFuture() | |
| 45 | + .join(); | |
| 46 | + } | |
| 47 | + | |
| 48 | + // 关闭 Vertx 实例 | |
| 49 | + if (vertx != null) { | |
| 50 | + vertx.close() | |
| 51 | + .toCompletionStage() | |
| 52 | + .toCompletableFuture() | |
| 53 | + .join(); | |
| 54 | + } | |
| 55 | + log.info("[stop][关闭完成]"); | |
| 56 | + } catch (Exception e) { | |
| 57 | + log.error("[stop][关闭异常]", e); | |
| 58 | + throw new RuntimeException(e); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * 获得端口 | |
| 64 | + * | |
| 65 | + * @return 端口 | |
| 66 | + */ | |
| 67 | + public int getPort() { | |
| 68 | + return this.server.actualPort(); | |
| 69 | + } | |
| 70 | + | |
| 71 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/downstream/router/IotDeviceServiceInvokeVertxHandler.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.plugin.common.core.downstream.router; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamHandler; | |
| 4 | +import io.vertx.core.Handler; | |
| 5 | +import io.vertx.ext.web.RoutingContext; | |
| 6 | +import lombok.RequiredArgsConstructor; | |
| 7 | +import lombok.extern.slf4j.Slf4j; | |
| 8 | + | |
| 9 | +@Slf4j | |
| 10 | +@RequiredArgsConstructor | |
| 11 | +public class IotDeviceServiceInvokeVertxHandler implements Handler<RoutingContext> { | |
| 12 | + | |
| 13 | + public static final String PATH = "/sys/:productKey/:deviceName/thing/service/:identifier"; | |
| 14 | + | |
| 15 | + private final IotDeviceDownstreamHandler deviceDownstreamHandler; | |
| 16 | + | |
| 17 | + @Override | |
| 18 | + public void handle(RoutingContext routingContext) { | |
| 19 | + // TODO 芋艿:这里没实现 | |
| 20 | + deviceDownstreamHandler.invokeDeviceService(null); | |
| 21 | + } | |
| 22 | + | |
| 23 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/api/DeviceDataApiClient.java renamed to yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/upstream/IotDeviceUpstreamClient.java
| 1 | -package cn.iocoder.yudao.module.iot.plugin.common.api; | |
| 1 | +package cn.iocoder.yudao.module.iot.plugin.common.core.upstream; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; |
| 5 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceEventReportReqDTO; | |
| 6 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; | |
| 7 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 5 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEventReportReqDTO; | |
| 6 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO; | |
| 7 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 8 | 8 | import lombok.extern.slf4j.Slf4j; |
| 9 | 9 | import org.springframework.web.client.RestTemplate; |
| 10 | 10 | |
| 11 | 11 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 12 | 12 | |
| 13 | -// TODO @haohao:类注释,写一下,比较好 | |
| 14 | -// TODO @haohao:类名要改下 | |
| 13 | +/** | |
| 14 | + * 设备数据 Upstream 上行客户端 | |
| 15 | + * | |
| 16 | + * 通过 HTTP 调用远程的 IotDeviceUpstreamApi 接口 | |
| 17 | + * | |
| 18 | + * @author haohao | |
| 19 | + */ | |
| 15 | 20 | @Slf4j |
| 16 | -public class DeviceDataApiClient implements IotDeviceUpstreamApi { | |
| 21 | +public class IotDeviceUpstreamClient implements IotDeviceUpstreamApi { | |
| 17 | 22 | |
| 18 | 23 | public static final String URL_PREFIX = "/rpc-api/iot/device/upstream"; |
| 19 | 24 | |
| ... | ... | @@ -22,7 +27,7 @@ public class DeviceDataApiClient implements IotDeviceUpstreamApi { |
| 22 | 27 | |
| 23 | 28 | // 可以通过构造器把 RestTemplate 和 baseUrl 注入进来 |
| 24 | 29 | // TODO @haohao:可以用 lombok 简化 |
| 25 | - public DeviceDataApiClient(RestTemplate restTemplate, String deviceDataUrl) { | |
| 30 | + public IotDeviceUpstreamClient(RestTemplate restTemplate, String deviceDataUrl) { | |
| 26 | 31 | this.restTemplate = restTemplate; |
| 27 | 32 | this.deviceDataUrl = deviceDataUrl; |
| 28 | 33 | } | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml
| ... | ... | @@ -121,12 +121,20 @@ |
| 121 | 121 | </build> |
| 122 | 122 | |
| 123 | 123 | <dependencies> |
| 124 | + <!-- TODO 芋艿:优化下 --> | |
| 124 | 125 | <!-- 项目依赖 --> |
| 125 | 126 | <dependency> |
| 126 | 127 | <groupId>cn.iocoder.boot</groupId> |
| 127 | 128 | <artifactId>yudao-module-iot-plugin-common</artifactId> |
| 128 | 129 | <version>${revision}</version> |
| 129 | 130 | </dependency> |
| 131 | + | |
| 132 | + <!-- 其他依赖项 --> | |
| 133 | + <dependency> | |
| 134 | + <groupId>org.springframework.boot</groupId> | |
| 135 | + <artifactId>spring-boot-starter-web</artifactId> | |
| 136 | + </dependency> | |
| 137 | + | |
| 130 | 138 | <!-- Vert.x Web --> |
| 131 | 139 | <dependency> |
| 132 | 140 | <groupId>io.vertx</groupId> | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/HttpVertxPluginConfiguration.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.plugin.http.config; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; |
| 4 | -import cn.iocoder.yudao.module.iot.plugin.http.service.HttpVertxHandler; | |
| 4 | +import cn.iocoder.yudao.module.iot.plugin.http.framework.upstream.HttpVertxHandler; | |
| 5 | 5 | import io.vertx.core.Vertx; |
| 6 | 6 | import io.vertx.ext.web.Router; |
| 7 | 7 | import io.vertx.ext.web.handler.BodyHandler; |
| ... | ... | @@ -34,7 +34,7 @@ public class HttpVertxPluginConfiguration { |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | 36 | * 创建路由 |
| 37 | - * | |
| 37 | + * | |
| 38 | 38 | * @param vertx Vertx 实例 |
| 39 | 39 | * @param httpVertxHandler HttpVertxHandler 实例 |
| 40 | 40 | * @return Router 实例 |
| ... | ... | @@ -68,7 +68,7 @@ public class HttpVertxPluginConfiguration { |
| 68 | 68 | /** |
| 69 | 69 | * 定义一个 VertxService 来管理服务器启动逻辑 |
| 70 | 70 | * 无论是独立运行还是插件方式,都可以共用此类 |
| 71 | - * | |
| 71 | + * | |
| 72 | 72 | * @param vertx Vertx 实例 |
| 73 | 73 | * @param router Router 实例 |
| 74 | 74 | * @return VertxService 实例 | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/framework/downstream/IotDeviceDownstreamHandlerImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.plugin.http.framework.downstream; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 4 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertyGetReqDTO; | |
| 5 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertySetReqDTO; | |
| 6 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceServiceInvokeReqDTO; | |
| 7 | +import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamHandler; | |
| 8 | +import org.springframework.stereotype.Component; | |
| 9 | + | |
| 10 | +@Component // TODO @芋艿:后续统一处理 | |
| 11 | +public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandler { | |
| 12 | + | |
| 13 | + @Override | |
| 14 | + public CommonResult<Boolean> invokeDeviceService(IotDeviceServiceInvokeReqDTO invokeReqDTO) { | |
| 15 | + // TODO @芋艿:待实现 | |
| 16 | + System.out.println(); | |
| 17 | + return null; | |
| 18 | + } | |
| 19 | + | |
| 20 | + @Override | |
| 21 | + public CommonResult<Boolean> getDeviceProperty(IotDevicePropertyGetReqDTO getReqDTO) { | |
| 22 | + // TODO @芋艿:待实现 | |
| 23 | + System.out.println(); | |
| 24 | + return null; | |
| 25 | + } | |
| 26 | + | |
| 27 | + @Override | |
| 28 | + public CommonResult<Boolean> setDeviceProperty(IotDevicePropertySetReqDTO setReqDTO) { | |
| 29 | + // TODO @芋艿:待实现 | |
| 30 | + System.out.println(); | |
| 31 | + return null; | |
| 32 | + } | |
| 33 | + | |
| 34 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/framework/package-info.java
0 → 100644
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/service/HttpVertxHandler.java renamed to yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/framework/upstream/HttpVertxHandler.java
| 1 | -package cn.iocoder.yudao.module.iot.plugin.http.service; | |
| 1 | +package cn.iocoder.yudao.module.iot.plugin.http.framework.upstream; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.IdUtil; |
| 4 | 4 | import cn.hutool.json.JSONObject; |
| 5 | 5 | import cn.hutool.json.JSONUtil; |
| 6 | 6 | import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; |
| 7 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; | |
| 8 | -import cn.iocoder.yudao.module.iot.api.device.dto.IotDeviceStateUpdateReqDTO; | |
| 7 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO; | |
| 8 | +import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; | |
| 9 | 9 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStateEnum; |
| 10 | 10 | import io.vertx.core.Handler; |
| 11 | 11 | import io.vertx.ext.web.RequestBody; | ... | ... |