diff --git a/pom.xml b/pom.xml index 85ab415a..9ff7fea9 100755 --- a/pom.xml +++ b/pom.xml @@ -247,6 +247,12 @@ ${project.version} + + cc.iotkit + converter + ${project.version} + + diff --git a/protocol-gateway/component/pom.xml b/protocol-gateway/component/pom.xml index 03ab87dc..d57e08c0 100755 --- a/protocol-gateway/component/pom.xml +++ b/protocol-gateway/component/pom.xml @@ -24,6 +24,11 @@ common + + cc.iotkit + converter + + \ No newline at end of file diff --git a/protocol-gateway/component/src/main/java/cc/iotkit/comp/AbstractComponent.java b/protocol-gateway/component/src/main/java/cc/iotkit/comp/AbstractComponent.java index 56cc5d96..e08be90b 100755 --- a/protocol-gateway/component/src/main/java/cc/iotkit/comp/AbstractComponent.java +++ b/protocol-gateway/component/src/main/java/cc/iotkit/comp/AbstractComponent.java @@ -1,10 +1,13 @@ package cc.iotkit.comp; +import cc.iotkit.converter.Converter; import lombok.Data; @Data -public class AbstractComponent { +public abstract class AbstractComponent implements Component { - protected MessageHandler messageHandler; + protected MessageHandler handler; + + protected Converter converter; } diff --git a/protocol-gateway/component/src/main/java/cc/iotkit/comp/Component.java b/protocol-gateway/component/src/main/java/cc/iotkit/comp/Component.java index f5f45693..2de633f4 100755 --- a/protocol-gateway/component/src/main/java/cc/iotkit/comp/Component.java +++ b/protocol-gateway/component/src/main/java/cc/iotkit/comp/Component.java @@ -1,5 +1,7 @@ package cc.iotkit.comp; +import cc.iotkit.converter.Converter; + public interface Component { void create(String config); @@ -12,4 +14,8 @@ public interface Component { void setHandler(MessageHandler handler); + void setConverter(Converter converter); + + Converter getConverter(); + } diff --git a/protocol-gateway/component/src/main/java/cc/iotkit/comp/ComponentManager.java b/protocol-gateway/component/src/main/java/cc/iotkit/comp/ComponentManager.java index fed3f3b5..f12db384 100755 --- a/protocol-gateway/component/src/main/java/cc/iotkit/comp/ComponentManager.java +++ b/protocol-gateway/component/src/main/java/cc/iotkit/comp/ComponentManager.java @@ -22,7 +22,7 @@ public class ComponentManager { if (component == null) { return; } - component.setHandler(new MessageHandler(script)); + component.setHandler(new MessageHandler(script, component.getConverter())); component.start(); } diff --git a/protocol-gateway/component/src/main/java/cc/iotkit/comp/MessageHandler.java b/protocol-gateway/component/src/main/java/cc/iotkit/comp/MessageHandler.java index 43de863a..f039f83b 100755 --- a/protocol-gateway/component/src/main/java/cc/iotkit/comp/MessageHandler.java +++ b/protocol-gateway/component/src/main/java/cc/iotkit/comp/MessageHandler.java @@ -2,6 +2,7 @@ package cc.iotkit.comp; import cc.iotkit.common.utils.JsonUtil; import cc.iotkit.comp.model.RegisterInfo; +import cc.iotkit.converter.Converter; import jdk.nashorn.api.scripting.NashornScriptEngine; import jdk.nashorn.api.scripting.ScriptObjectMirror; import lombok.Data; @@ -18,9 +19,12 @@ public class MessageHandler { private final String script; + private final Converter converter; + @SneakyThrows - public MessageHandler(String script) { + public MessageHandler(String script, Converter converter) { this.script = script; + this.converter = converter; engine.eval(script); } @@ -40,11 +44,14 @@ public class MessageHandler { if (rstType == null) { return; } + //取脚本执行后返回的数据 Object data = obj.get("data"); if ("register".equals(rstType)) { + //注册数据 RegisterInfo regInfo = getData(data, RegisterInfo.class); } else if ("report".equals(rstType)) { + //上报数据 } diff --git a/protocol-gateway/converter/pom.xml b/protocol-gateway/converter/pom.xml new file mode 100644 index 00000000..0c843b96 --- /dev/null +++ b/protocol-gateway/converter/pom.xml @@ -0,0 +1,28 @@ + + + + protocol-gateway + cc.iotkit + 0.0.1-SNAPSHOT + + 4.0.0 + + converter + + + 8 + 8 + + + + + + org.projectlombok + lombok + + + + + \ No newline at end of file diff --git a/protocol-gateway/converter/src/main/java/cc/iotkit/converter/Converter.java b/protocol-gateway/converter/src/main/java/cc/iotkit/converter/Converter.java new file mode 100644 index 00000000..abafa4e8 --- /dev/null +++ b/protocol-gateway/converter/src/main/java/cc/iotkit/converter/Converter.java @@ -0,0 +1,8 @@ +package cc.iotkit.converter; + +public interface Converter { + + ThingModelMessage decode(String msg); + + +} diff --git a/protocol-gateway/converter/src/main/java/cc/iotkit/converter/ScriptConverter.java b/protocol-gateway/converter/src/main/java/cc/iotkit/converter/ScriptConverter.java new file mode 100644 index 00000000..1be49df2 --- /dev/null +++ b/protocol-gateway/converter/src/main/java/cc/iotkit/converter/ScriptConverter.java @@ -0,0 +1,9 @@ +package cc.iotkit.converter; + +public class ScriptConverter implements Converter { + + public ThingModelMessage decode(String msg) { + return null; + } + +} diff --git a/protocol-gateway/converter/src/main/java/cc/iotkit/converter/ThingModelMessage.java b/protocol-gateway/converter/src/main/java/cc/iotkit/converter/ThingModelMessage.java new file mode 100755 index 00000000..605c5ebb --- /dev/null +++ b/protocol-gateway/converter/src/main/java/cc/iotkit/converter/ThingModelMessage.java @@ -0,0 +1,62 @@ +package cc.iotkit.converter; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.HashMap; +import java.util.Map; + +/** + * 物模型消息 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class ThingModelMessage { + + private String productKey; + + private String deviceName; + + private String mid; + + private String identifier; + + private Map data; + + /** + * 时间戳,设备上的事件或数据产生的本地时间 + */ + private Long occur; + + /** + * 消息上报时间 + */ + private Long time; + + public static ThingModelMessage from(Map map) { + ThingModelMessage message = new ThingModelMessage(); + message.setProductKey(getStr(map, "productKey")); + message.setDeviceName(getStr(map, "deviceName")); + message.setMid(getStr(map, "mid")); + message.setIdentifier(getStr(map, "identifier")); + Object data = map.get("data"); + if (data instanceof Map) { + message.setData((Map) data); + } else { + message.setData(new HashMap<>()); + } + return message; + } + + private static String getStr(Map map, String key) { + Object val = map.get(key); + if (val == null) { + return null; + } + return val.toString(); + } +} diff --git a/protocol-gateway/mqtt-component/src/main/java/cc/iotkit/comp/mqtt/MqttComponent.java b/protocol-gateway/mqtt-component/src/main/java/cc/iotkit/comp/mqtt/MqttComponent.java index 550194dd..dd6c5329 100755 --- a/protocol-gateway/mqtt-component/src/main/java/cc/iotkit/comp/mqtt/MqttComponent.java +++ b/protocol-gateway/mqtt-component/src/main/java/cc/iotkit/comp/mqtt/MqttComponent.java @@ -24,7 +24,7 @@ public class MqttComponent extends AbstractComponent { public void start() { try { - Future future = vertx.deployVerticle(new MqttVerticle(mqttConfig, getMessageHandler())); + Future future = vertx.deployVerticle(new MqttVerticle(mqttConfig, getHandler())); future.onSuccess((s -> { deployedId = s; countDownLatch.countDown(); diff --git a/protocol-gateway/pom.xml b/protocol-gateway/pom.xml index 3e9bf227..3fab41c3 100755 --- a/protocol-gateway/pom.xml +++ b/protocol-gateway/pom.xml @@ -15,6 +15,7 @@ gateway-client protocol-server decode-function + converter