通讯组件接口调整
parent
5af991f401
commit
34f071cfe2
|
@ -25,10 +25,14 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||
@Slf4j
|
||||
public class ApiTool {
|
||||
|
||||
private static final Vertx vertx;
|
||||
private static final WebClient client;
|
||||
private final Vertx vertx;
|
||||
private final WebClient client;
|
||||
|
||||
static {
|
||||
private String host;
|
||||
private int port;
|
||||
private int timeout;
|
||||
|
||||
public ApiTool() {
|
||||
if (Vertx.currentContext() == null) {
|
||||
vertx = Vertx.vertx();
|
||||
} else {
|
||||
|
@ -41,24 +45,20 @@ public class ApiTool {
|
|||
client = WebClient.create(vertx, options);
|
||||
}
|
||||
|
||||
private static String host;
|
||||
private static int port;
|
||||
private static int timeout;
|
||||
|
||||
public static void config(String host, int port, int timeout) {
|
||||
ApiTool.host = host;
|
||||
ApiTool.port = port;
|
||||
ApiTool.timeout = timeout;
|
||||
public void config(String host, int port, int timeout) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
private static String getPath(String path) {
|
||||
private String getPath(String path) {
|
||||
return Paths.get(Constants.API.DEVICE_BASE, path).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的设备列表
|
||||
*/
|
||||
public static ApiResponse getDevices(String token) {
|
||||
public ApiResponse getDevices(String token) {
|
||||
HttpRequest<Buffer> request = client
|
||||
.post(port, host, getPath(Constants.API.DEVICE_LIST
|
||||
.replace("{size}", "1000")
|
||||
|
@ -69,7 +69,7 @@ public class ApiTool {
|
|||
/**
|
||||
* 获取设备详情
|
||||
*/
|
||||
public static ApiResponse getDeviceDetail(String token, String deviceId) {
|
||||
public ApiResponse getDeviceDetail(String token, String deviceId) {
|
||||
HttpRequest<Buffer> request = client
|
||||
.get(port, host, getPath(Constants.API.DEVICE_DETAIL
|
||||
.replace("{deviceId}", deviceId)));
|
||||
|
@ -79,7 +79,7 @@ public class ApiTool {
|
|||
/**
|
||||
* 设置属性
|
||||
*/
|
||||
public static ApiResponse setProperties(String token, String deviceId, Map<String, Object> properties) {
|
||||
public ApiResponse setProperties(String token, String deviceId, Map<String, Object> properties) {
|
||||
HttpRequest<Buffer> request = client
|
||||
.post(port, host, getPath(Constants.API.DEVICE_SET_PROPERTIES
|
||||
.replace("{deviceId}", deviceId)));
|
||||
|
@ -89,7 +89,7 @@ public class ApiTool {
|
|||
/**
|
||||
* 调用服务
|
||||
*/
|
||||
public static ApiResponse invokeService(String token, String deviceId, String service, Map<String, Object> params) {
|
||||
public ApiResponse invokeService(String token, String deviceId, String service, Map<String, Object> params) {
|
||||
HttpRequest<Buffer> request = client
|
||||
.post(port, host, getPath(Constants.API.DEVICE_INVOKE_SERVICE
|
||||
.replace("{deviceId}", deviceId)
|
||||
|
@ -97,7 +97,7 @@ public class ApiTool {
|
|||
return send(token, HttpMethod.POST, request, params);
|
||||
}
|
||||
|
||||
private static ApiResponse send(String token, HttpMethod method, HttpRequest<Buffer> request, Map<String, Object> params) {
|
||||
private ApiResponse send(String token, HttpMethod method, HttpRequest<Buffer> request, Map<String, Object> params) {
|
||||
request = request
|
||||
.timeout(timeout)
|
||||
.putHeader("wrap-response", "json")
|
||||
|
@ -146,7 +146,7 @@ public class ApiTool {
|
|||
return apiResponse.get();
|
||||
}
|
||||
|
||||
public static void log(String msg) {
|
||||
public void log(String msg) {
|
||||
log.info(msg);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import cc.iotkit.common.exception.BizException;
|
|||
import cc.iotkit.comp.CompConfig;
|
||||
import cc.iotkit.comp.IComponent;
|
||||
import cc.iotkit.comps.config.ComponentConfig;
|
||||
import cc.iotkit.comps.service.DeviceBehaviourService;
|
||||
import cc.iotkit.dao.ProtocolComponentRepository;
|
||||
import cc.iotkit.model.protocol.ProtocolComponent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -32,6 +33,8 @@ public class BizComponentManager {
|
|||
private ComponentConfig componentConfig;
|
||||
@Autowired
|
||||
private ProtocolComponentRepository componentRepository;
|
||||
@Autowired
|
||||
private DeviceBehaviourService deviceBehaviourService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
@ -65,6 +68,7 @@ public class BizComponentManager {
|
|||
String componentScript = FileUtils.readFileToString(path.
|
||||
resolve(ProtocolComponent.SCRIPT_FILE_NAME).toFile(), "UTF-8");
|
||||
componentInstance.setScript(componentScript);
|
||||
componentInstance.putScriptEnv("deviceBehaviour", deviceBehaviourService);
|
||||
} catch (IOException e) {
|
||||
throw new BizException("get component script error", e);
|
||||
}
|
||||
|
|
|
@ -126,10 +126,13 @@ public class DeviceComponentManager {
|
|||
if (component == null) {
|
||||
return;
|
||||
}
|
||||
component.setHandler(
|
||||
new DeviceMessageHandler(this, component,
|
||||
component.getScript(), component.getConverter(),
|
||||
deviceBehaviourService));
|
||||
DeviceMessageHandler messageHandler = new DeviceMessageHandler(this, component,
|
||||
component.getScript(), component.getConverter(),
|
||||
deviceBehaviourService);
|
||||
messageHandler.putScriptEnv("apiTool", new ApiTool());
|
||||
messageHandler.putScriptEnv("deviceBehaviour", deviceBehaviourService);
|
||||
|
||||
component.setHandler(messageHandler);
|
||||
component.start();
|
||||
states.put(id, true);
|
||||
}
|
||||
|
|
|
@ -196,6 +196,11 @@ public class DeviceMessageHandler implements IMessageHandler {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putScriptEnv(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Action {
|
||||
public static final String TYPE_ACK = "ack";
|
||||
|
|
|
@ -250,4 +250,12 @@ public class DeviceBehaviourService {
|
|||
log.error("send thing model message error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供给js调用的方法
|
||||
*/
|
||||
public void reportMessage(String jsonMsg) {
|
||||
ThingModelMessage message = JsonUtil.parse(jsonMsg, ThingModelMessage.class);
|
||||
reportMessage(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,4 +38,8 @@ public abstract class AbstractDeviceComponent implements IDeviceComponent {
|
|||
public CompConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putScriptEnv(String key, Object value) {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,4 +15,9 @@ public interface IComponent {
|
|||
void setScript(String script);
|
||||
|
||||
String getScript();
|
||||
|
||||
/**
|
||||
* 添加脚本环境变量
|
||||
*/
|
||||
void putScriptEnv(String key, Object value);
|
||||
}
|
||||
|
|
|
@ -7,4 +7,9 @@ import java.util.Map;
|
|||
public interface IMessageHandler {
|
||||
|
||||
ReceiveResult onReceive(Map<String, Object> head, String type, String msg);
|
||||
|
||||
/**
|
||||
* 添加脚本环境变量
|
||||
*/
|
||||
void putScriptEnv(String key, Object value);
|
||||
}
|
||||
|
|
|
@ -128,6 +128,11 @@ public class HttpBizComponent implements IComponent {
|
|||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putScriptEnv(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
backendServer.close();
|
||||
|
|
|
@ -11,8 +11,8 @@ public class Application {
|
|||
public static void main(String[] args) throws IOException {
|
||||
|
||||
if (args.length == 0) {
|
||||
Mqtt.broker = "tcp://127.0.0.1:1883";
|
||||
// Mqtt.broker = "tcp://120.76.96.206:1883";
|
||||
// Mqtt.broker = "tcp://127.0.0.1:1883";
|
||||
Mqtt.broker = "tcp://120.76.96.206:1883";
|
||||
} else {
|
||||
Mqtt.broker = args[0];
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import cc.iotkit.common.utils.JsonUtil;
|
|||
import cc.iotkit.comp.AbstractDeviceComponent;
|
||||
import cc.iotkit.comp.CompConfig;
|
||||
import cc.iotkit.comp.model.DeviceState;
|
||||
import cc.iotkit.converter.Device;
|
||||
import cc.iotkit.converter.DeviceMessage;
|
||||
import cc.iotkit.converter.ThingService;
|
||||
import cc.iotkit.model.device.message.ThingModelMessage;
|
||||
|
|
Loading…
Reference in New Issue