Ver Fonte

增加协议网关模块

xiwa há 3 anos atrás
pai
commit
bd06dae340

+ 1 - 0
pom.xml

@@ -11,6 +11,7 @@
         <module>manager</module>
         <module>dao</module>
         <module>tppa-server</module>
+        <module>protocol</module>
     </modules>
     <parent>
         <groupId>org.springframework.boot</groupId>

+ 43 - 0
protocol/pom.xml

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>iotkit-parent</artifactId>
+        <groupId>cc.iotkit</groupId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>protocol</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context-support</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 17 - 0
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;
+}

+ 46 - 0
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);
+
+
+}

+ 24 - 0
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;
+
+}

+ 30 - 0
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;
+}

+ 18 - 0
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<String,Object> label;
+}

+ 9 - 0
protocol/src/main/java/cc/iotkit/protocol/Result.java

@@ -0,0 +1,9 @@
+package cc.iotkit.protocol;
+
+import lombok.Data;
+
+@Data
+public class Result {
+
+
+}

+ 24 - 0
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);
+    }
+
+}

+ 66 - 0
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) {
+    }
+}

+ 1 - 0
protocol/src/main/resources/spring.factories

@@ -0,0 +1 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration= cc.iotkit.protocol.config.ProtocolConfiguration

+ 1 - 1
rule-engine/src/main/resources/spring.factories

@@ -1 +1 @@
-org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.danbay.iot.commons.logger.config.LoggerAutoConfiguration
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=cc.iotkit.ruleengine.config.RuleConfiguration