From bd06dae340b5e3167e4b2100742ebef10dc6710e Mon Sep 17 00:00:00 2001 From: xiwa Date: Wed, 9 Mar 2022 18:45:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=8F=E8=AE=AE=E7=BD=91?= =?UTF-8?q?=E5=85=B3=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + protocol/pom.xml | 43 ++++++++++++ .../cc/iotkit/protocol/DeregisterInfo.java | 17 +++++ .../cc/iotkit/protocol/DeviceBehaviour.java | 46 +++++++++++++ .../cc/iotkit/protocol/DeviceMessage.java | 24 +++++++ .../java/cc/iotkit/protocol/OtaMessage.java | 30 +++++++++ .../java/cc/iotkit/protocol/RegisterInfo.java | 18 +++++ .../main/java/cc/iotkit/protocol/Result.java | 9 +++ .../config/ProtocolConfiguration.java | 24 +++++++ .../protocol/impl/DeviceBehaviourImpl.java | 66 +++++++++++++++++++ protocol/src/main/resources/spring.factories | 1 + .../src/main/resources/spring.factories | 2 +- 12 files changed, 280 insertions(+), 1 deletion(-) create mode 100644 protocol/pom.xml create mode 100644 protocol/src/main/java/cc/iotkit/protocol/DeregisterInfo.java create mode 100644 protocol/src/main/java/cc/iotkit/protocol/DeviceBehaviour.java create mode 100644 protocol/src/main/java/cc/iotkit/protocol/DeviceMessage.java create mode 100644 protocol/src/main/java/cc/iotkit/protocol/OtaMessage.java create mode 100644 protocol/src/main/java/cc/iotkit/protocol/RegisterInfo.java create mode 100644 protocol/src/main/java/cc/iotkit/protocol/Result.java create mode 100644 protocol/src/main/java/cc/iotkit/protocol/config/ProtocolConfiguration.java create mode 100644 protocol/src/main/java/cc/iotkit/protocol/impl/DeviceBehaviourImpl.java create mode 100755 protocol/src/main/resources/spring.factories diff --git a/pom.xml b/pom.xml index f653e772..e2ed65a0 100755 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ manager dao tppa-server + protocol org.springframework.boot diff --git a/protocol/pom.xml b/protocol/pom.xml new file mode 100644 index 00000000..30293718 --- /dev/null +++ b/protocol/pom.xml @@ -0,0 +1,43 @@ + + + + iotkit-parent + cc.iotkit + 0.0.1-SNAPSHOT + + 4.0.0 + + protocol + + + 8 + 8 + + + + + + org.springframework + spring-context-support + + + + org.springframework.boot + spring-boot + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + org.projectlombok + lombok + + + + + \ No newline at end of file diff --git a/protocol/src/main/java/cc/iotkit/protocol/DeregisterInfo.java b/protocol/src/main/java/cc/iotkit/protocol/DeregisterInfo.java new file mode 100644 index 00000000..4e8f03ed --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/DeregisterInfo.java @@ -0,0 +1,17 @@ +package cc.iotkit.protocol; + +import lombok.Data; + + +/** + * 注销信息 + */ +@Data +public class DeregisterInfo { + + private String productKey; + + private String deviceName; + + private boolean cascade; +} diff --git a/protocol/src/main/java/cc/iotkit/protocol/DeviceBehaviour.java b/protocol/src/main/java/cc/iotkit/protocol/DeviceBehaviour.java new file mode 100644 index 00000000..78a996bb --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/DeviceBehaviour.java @@ -0,0 +1,46 @@ +package cc.iotkit.protocol; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +/** + * 设备行为接口 + */ +public interface DeviceBehaviour { + + /** + * 设备注册 + */ + @PostMapping("/register") + Result register(@RequestBody RegisterInfo info); + + /** + * 设备注销 + */ + @PostMapping("/deregister") + Result deregister(@RequestBody DeregisterInfo info); + + /** + * 设备上线 + */ + @PostMapping("/online") + void online(String productKey, String deviceName); + + /** + * 设备离线 + */ + @PostMapping("/offline") + void offline(String productKey, String deviceName); + + /** + * 设备消息上报 + */ + void messageReport(DeviceMessage msg); + + /** + * OTA消息上报 + */ + void otaProgressReport(OtaMessage msg); + + +} diff --git a/protocol/src/main/java/cc/iotkit/protocol/DeviceMessage.java b/protocol/src/main/java/cc/iotkit/protocol/DeviceMessage.java new file mode 100644 index 00000000..9a8daca5 --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/DeviceMessage.java @@ -0,0 +1,24 @@ +package cc.iotkit.protocol; + +import lombok.Data; + +/** + * 设备消息 + */ +@Data +public class DeviceMessage { + + public static final String TYPE_REQUEST = "request"; + public static final String TYPE_ACK = "ack"; + + private String productKey; + + private String deviceName; + + private String type; + + private String mid; + + private String content; + +} diff --git a/protocol/src/main/java/cc/iotkit/protocol/OtaMessage.java b/protocol/src/main/java/cc/iotkit/protocol/OtaMessage.java new file mode 100644 index 00000000..65ad1be2 --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/OtaMessage.java @@ -0,0 +1,30 @@ +package cc.iotkit.protocol; + +import lombok.Data; + +/** + * OTA消息 + */ +@Data +public class OtaMessage { + + private final String TYPE_PROGRESS="progress"; + private final String TYPE_RESULT="result"; + + private final String PROGRESS_DOWNLOADING="downloading"; + private final String PROGRESS_DOWNLOADED="downloaded"; + private final String PROGRESS_UPGRADING="upgrading"; + private final String PROGRESS_UPGRADED="upgraded"; + + private String productKey; + + private String deviceName; + + private String type; + + private String jobId; + + private String progress; + + private String result; +} diff --git a/protocol/src/main/java/cc/iotkit/protocol/RegisterInfo.java b/protocol/src/main/java/cc/iotkit/protocol/RegisterInfo.java new file mode 100644 index 00000000..b4f324fa --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/RegisterInfo.java @@ -0,0 +1,18 @@ +package cc.iotkit.protocol; + +import lombok.Data; + +import java.util.Map; + +/** + * 注册信息 + */ +@Data +public class RegisterInfo { + + private String productKey; + + private String deviceName; + + private Map label; +} diff --git a/protocol/src/main/java/cc/iotkit/protocol/Result.java b/protocol/src/main/java/cc/iotkit/protocol/Result.java new file mode 100644 index 00000000..a9566b2a --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/Result.java @@ -0,0 +1,9 @@ +package cc.iotkit.protocol; + +import lombok.Data; + +@Data +public class Result { + + +} diff --git a/protocol/src/main/java/cc/iotkit/protocol/config/ProtocolConfiguration.java b/protocol/src/main/java/cc/iotkit/protocol/config/ProtocolConfiguration.java new file mode 100644 index 00000000..c385afb6 --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/config/ProtocolConfiguration.java @@ -0,0 +1,24 @@ +package cc.iotkit.protocol.config; + +import cc.iotkit.protocol.DeviceBehaviour; +import cc.iotkit.protocol.impl.DeviceBehaviourImpl; +import feign.Client; +import feign.Contract; +import feign.codec.Decoder; +import feign.codec.Encoder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ProtocolConfiguration { + + @Value("${protocol.server}") + private String protocolServer; + + @Bean + public DeviceBehaviour getDeviceBehaviour(Decoder decoder, Encoder encoder, Client client, Contract contract) { + return new DeviceBehaviourImpl(protocolServer, decoder, encoder, client, contract); + } + +} diff --git a/protocol/src/main/java/cc/iotkit/protocol/impl/DeviceBehaviourImpl.java b/protocol/src/main/java/cc/iotkit/protocol/impl/DeviceBehaviourImpl.java new file mode 100644 index 00000000..91722fba --- /dev/null +++ b/protocol/src/main/java/cc/iotkit/protocol/impl/DeviceBehaviourImpl.java @@ -0,0 +1,66 @@ +package cc.iotkit.protocol.impl; + +import cc.iotkit.protocol.*; +import feign.Client; +import feign.Contract; +import feign.Feign; +import feign.codec.Decoder; +import feign.codec.Encoder; +import org.springframework.cloud.openfeign.FeignClientsConfiguration; +import org.springframework.context.annotation.Import; + +@Import(FeignClientsConfiguration.class) +public class DeviceBehaviourImpl implements DeviceBehaviour { + + private final Feign.Builder builder; + + private final String protocolServer; + + private DeviceBehaviour target; + + public DeviceBehaviourImpl(String protocolServer, Decoder decoder, + Encoder encoder, Client client, Contract contract) { + this.protocolServer = protocolServer; + this.builder = Feign.builder() + .client(client) + .encoder(encoder) + .decoder(decoder) + .contract(contract); + } + + private DeviceBehaviour behaviour() { + if (target == null) { + target = this.builder.target(DeviceBehaviour.class, protocolServer); + } + return target; + } + + @Override + public Result register(RegisterInfo info) { + return behaviour().register(info); + } + + @Override + public Result deregister(DeregisterInfo info) { + return behaviour().deregister(info); + } + + @Override + public void online(String productKey, String deviceName) { + behaviour().online(productKey, deviceName); + } + + @Override + public void offline(String productKey, String deviceName) { + behaviour().offline(productKey, deviceName); + } + + @Override + public void messageReport(DeviceMessage msg) { + + } + + @Override + public void otaProgressReport(OtaMessage msg) { + } +} diff --git a/protocol/src/main/resources/spring.factories b/protocol/src/main/resources/spring.factories new file mode 100755 index 00000000..77623732 --- /dev/null +++ b/protocol/src/main/resources/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration= cc.iotkit.protocol.config.ProtocolConfiguration \ No newline at end of file diff --git a/rule-engine/src/main/resources/spring.factories b/rule-engine/src/main/resources/spring.factories index 557a2014..f80a18ec 100755 --- a/rule-engine/src/main/resources/spring.factories +++ b/rule-engine/src/main/resources/spring.factories @@ -1 +1 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.danbay.iot.commons.logger.config.LoggerAutoConfiguration \ No newline at end of file +org.springframework.boot.autoconfigure.EnableAutoConfiguration=cc.iotkit.ruleengine.config.RuleConfiguration