通讯组件修改

V0.5.x
xiwa 2022-03-22 19:05:54 +08:00
parent e4cb721d0e
commit 55154d615f
12 changed files with 140 additions and 5 deletions

View File

@ -247,6 +247,12 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>converter</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

View File

@ -24,6 +24,11 @@
<artifactId>common</artifactId> <artifactId>common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>converter</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,10 +1,13 @@
package cc.iotkit.comp; package cc.iotkit.comp;
import cc.iotkit.converter.Converter;
import lombok.Data; import lombok.Data;
@Data @Data
public class AbstractComponent { public abstract class AbstractComponent implements Component {
protected MessageHandler messageHandler; protected MessageHandler handler;
protected Converter converter;
} }

View File

@ -1,5 +1,7 @@
package cc.iotkit.comp; package cc.iotkit.comp;
import cc.iotkit.converter.Converter;
public interface Component { public interface Component {
void create(String config); void create(String config);
@ -12,4 +14,8 @@ public interface Component {
void setHandler(MessageHandler handler); void setHandler(MessageHandler handler);
void setConverter(Converter converter);
Converter getConverter();
} }

View File

@ -22,7 +22,7 @@ public class ComponentManager {
if (component == null) { if (component == null) {
return; return;
} }
component.setHandler(new MessageHandler(script)); component.setHandler(new MessageHandler(script, component.getConverter()));
component.start(); component.start();
} }

View File

@ -2,6 +2,7 @@ package cc.iotkit.comp;
import cc.iotkit.common.utils.JsonUtil; import cc.iotkit.common.utils.JsonUtil;
import cc.iotkit.comp.model.RegisterInfo; import cc.iotkit.comp.model.RegisterInfo;
import cc.iotkit.converter.Converter;
import jdk.nashorn.api.scripting.NashornScriptEngine; import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.ScriptObjectMirror; import jdk.nashorn.api.scripting.ScriptObjectMirror;
import lombok.Data; import lombok.Data;
@ -18,9 +19,12 @@ public class MessageHandler {
private final String script; private final String script;
private final Converter converter;
@SneakyThrows @SneakyThrows
public MessageHandler(String script) { public MessageHandler(String script, Converter converter) {
this.script = script; this.script = script;
this.converter = converter;
engine.eval(script); engine.eval(script);
} }
@ -40,11 +44,14 @@ public class MessageHandler {
if (rstType == null) { if (rstType == null) {
return; return;
} }
//取脚本执行后返回的数据
Object data = obj.get("data"); Object data = obj.get("data");
if ("register".equals(rstType)) { if ("register".equals(rstType)) {
//注册数据
RegisterInfo regInfo = getData(data, RegisterInfo.class); RegisterInfo regInfo = getData(data, RegisterInfo.class);
} else if ("report".equals(rstType)) { } else if ("report".equals(rstType)) {
//上报数据
} }

View File

@ -0,0 +1,28 @@
<?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>protocol-gateway</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>converter</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,8 @@
package cc.iotkit.converter;
public interface Converter {
ThingModelMessage decode(String msg);
}

View File

@ -0,0 +1,9 @@
package cc.iotkit.converter;
public class ScriptConverter implements Converter {
public ThingModelMessage decode(String msg) {
return null;
}
}

View File

@ -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<String, Object> 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<String, Object>) 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();
}
}

View File

@ -24,7 +24,7 @@ public class MqttComponent extends AbstractComponent {
public void start() { public void start() {
try { try {
Future<String> future = vertx.deployVerticle(new MqttVerticle(mqttConfig, getMessageHandler())); Future<String> future = vertx.deployVerticle(new MqttVerticle(mqttConfig, getHandler()));
future.onSuccess((s -> { future.onSuccess((s -> {
deployedId = s; deployedId = s;
countDownLatch.countDown(); countDownLatch.countDown();

View File

@ -15,6 +15,7 @@
<module>gateway-client</module> <module>gateway-client</module>
<module>protocol-server</module> <module>protocol-server</module>
<module>decode-function</module> <module>decode-function</module>
<module>converter</module>
</modules> </modules>
<properties> <properties>