|
@@ -1,14 +1,16 @@
|
|
-package cc.iotkit.comp.biz;
|
|
|
|
|
|
+package cc.iotkit.comps;
|
|
|
|
|
|
import cc.iotkit.common.Constants;
|
|
import cc.iotkit.common.Constants;
|
|
import io.vertx.core.Vertx;
|
|
import io.vertx.core.Vertx;
|
|
import io.vertx.core.buffer.Buffer;
|
|
import io.vertx.core.buffer.Buffer;
|
|
|
|
+import io.vertx.core.http.HttpMethod;
|
|
import io.vertx.ext.web.client.HttpRequest;
|
|
import io.vertx.ext.web.client.HttpRequest;
|
|
import io.vertx.ext.web.client.WebClient;
|
|
import io.vertx.ext.web.client.WebClient;
|
|
import io.vertx.ext.web.client.WebClientOptions;
|
|
import io.vertx.ext.web.client.WebClientOptions;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.Data;
|
|
import lombok.Data;
|
|
import lombok.NoArgsConstructor;
|
|
import lombok.NoArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -20,6 +22,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|
/**
|
|
/**
|
|
* 平台API调用工具类
|
|
* 平台API调用工具类
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
public class ApiTool {
|
|
public class ApiTool {
|
|
|
|
|
|
private static final Vertx vertx;
|
|
private static final Vertx vertx;
|
|
@@ -48,17 +51,19 @@ public class ApiTool {
|
|
ApiTool.timeout = timeout;
|
|
ApiTool.timeout = timeout;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static String getPath(String path) {
|
|
|
|
+ return Paths.get(Constants.API.DEVICE_BASE, path).toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取用户的设备列表
|
|
* 获取用户的设备列表
|
|
*/
|
|
*/
|
|
public static ApiResponse getDevices(String token) {
|
|
public static ApiResponse getDevices(String token) {
|
|
HttpRequest<Buffer> request = client
|
|
HttpRequest<Buffer> request = client
|
|
- .post(port, host, Paths.get(Constants.API.DEVICE_BASE, Constants.API.DEVICE_LIST
|
|
|
|
- .replace("size", "1000")
|
|
|
|
- .replace("page", "1")).toString())
|
|
|
|
- .timeout(timeout)
|
|
|
|
- .putHeader("authorization", "Bearer " + token);
|
|
|
|
- return sendJson(request, new HashMap<>());
|
|
|
|
|
|
+ .post(port, host, getPath(Constants.API.DEVICE_LIST
|
|
|
|
+ .replace("{size}", "1000")
|
|
|
|
+ .replace("{page}", "1")));
|
|
|
|
+ return send(token, HttpMethod.POST, request, new HashMap<>());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -66,11 +71,9 @@ public class ApiTool {
|
|
*/
|
|
*/
|
|
public static ApiResponse getDeviceDetail(String token, String deviceId) {
|
|
public static ApiResponse getDeviceDetail(String token, String deviceId) {
|
|
HttpRequest<Buffer> request = client
|
|
HttpRequest<Buffer> request = client
|
|
- .post(port, host, Paths.get(Constants.API.DEVICE_BASE, Constants.API.DEVICE_DETAIL
|
|
|
|
- .replace("deviceId", deviceId)).toString())
|
|
|
|
- .timeout(timeout)
|
|
|
|
- .putHeader("authorization", "Bearer " + token);
|
|
|
|
- return sendJson(request, new HashMap<>());
|
|
|
|
|
|
+ .get(port, host, getPath(Constants.API.DEVICE_DETAIL
|
|
|
|
+ .replace("{deviceId}", deviceId)));
|
|
|
|
+ return send(token, HttpMethod.GET, request, new HashMap<>());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -78,11 +81,9 @@ public class ApiTool {
|
|
*/
|
|
*/
|
|
public static ApiResponse setProperties(String token, String deviceId, Map<String, Object> properties) {
|
|
public static ApiResponse setProperties(String token, String deviceId, Map<String, Object> properties) {
|
|
HttpRequest<Buffer> request = client
|
|
HttpRequest<Buffer> request = client
|
|
- .post(port, host, Paths.get(Constants.API.DEVICE_BASE, Constants.API.DEVICE_SET_PROPERTIES
|
|
|
|
- .replace("deviceId", deviceId)).toString())
|
|
|
|
- .timeout(timeout)
|
|
|
|
- .putHeader("authorization", "Bearer " + token);
|
|
|
|
- return sendJson(request, properties);
|
|
|
|
|
|
+ .post(port, host, getPath(Constants.API.DEVICE_SET_PROPERTIES
|
|
|
|
+ .replace("{deviceId}", deviceId)));
|
|
|
|
+ return send(token, HttpMethod.POST, request, properties);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -90,48 +91,70 @@ public class ApiTool {
|
|
*/
|
|
*/
|
|
public static ApiResponse invokeService(String token, String deviceId, String service, Map<String, Object> params) {
|
|
public static ApiResponse invokeService(String token, String deviceId, String service, Map<String, Object> params) {
|
|
HttpRequest<Buffer> request = client
|
|
HttpRequest<Buffer> request = client
|
|
- .post(port, host, Paths.get(Constants.API.DEVICE_BASE, Constants.API.DEVICE_INVOKE_SERVICE
|
|
|
|
- .replace("deviceId", deviceId)
|
|
|
|
- .replace("service", service)).toString())
|
|
|
|
|
|
+ .post(port, host, getPath(Constants.API.DEVICE_INVOKE_SERVICE
|
|
|
|
+ .replace("{deviceId}", deviceId)
|
|
|
|
+ .replace("{service}", service)));
|
|
|
|
+ return send(token, HttpMethod.POST, request, params);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static ApiResponse send(String token, HttpMethod method, HttpRequest<Buffer> request, Map<String, Object> params) {
|
|
|
|
+ request = request
|
|
.timeout(timeout)
|
|
.timeout(timeout)
|
|
|
|
+ .putHeader("wrap-response", "json")
|
|
.putHeader("authorization", "Bearer " + token);
|
|
.putHeader("authorization", "Bearer " + token);
|
|
- return sendJson(request, params);
|
|
|
|
- }
|
|
|
|
|
|
|
|
- private static ApiResponse sendJson(HttpRequest<Buffer> request, Map<String, Object> params) {
|
|
|
|
- AtomicReference<ApiResponse> apiResponse = new AtomicReference<>(new ApiResponse(500, "", null));
|
|
|
|
|
|
+ AtomicReference<ApiResponse> apiResponse = new AtomicReference<>(
|
|
|
|
+ new ApiResponse(500, "", null, System.currentTimeMillis()));
|
|
try {
|
|
try {
|
|
//转为同步模式便于提供给js调用
|
|
//转为同步模式便于提供给js调用
|
|
CountDownLatch wait = new CountDownLatch(1);
|
|
CountDownLatch wait = new CountDownLatch(1);
|
|
- request.sendJson(params)
|
|
|
|
- .onSuccess((response) -> {
|
|
|
|
- System.out.println(response.bodyAsString());
|
|
|
|
- apiResponse.set(response.bodyAsJson(ApiResponse.class));
|
|
|
|
- wait.countDown();
|
|
|
|
- })
|
|
|
|
- .onFailure((err) -> {
|
|
|
|
- err.printStackTrace();
|
|
|
|
- wait.countDown();
|
|
|
|
- });
|
|
|
|
|
|
+
|
|
|
|
+ if (method == HttpMethod.POST) {
|
|
|
|
+ request.sendJson(params)
|
|
|
|
+ .onSuccess((response) -> {
|
|
|
|
+ System.out.println(response.bodyAsString());
|
|
|
|
+ apiResponse.set(response.bodyAsJson(ApiResponse.class));
|
|
|
|
+ wait.countDown();
|
|
|
|
+ })
|
|
|
|
+ .onFailure((err) -> {
|
|
|
|
+ err.printStackTrace();
|
|
|
|
+ wait.countDown();
|
|
|
|
+ });
|
|
|
|
+ } else if (method == HttpMethod.GET) {
|
|
|
|
+ request.send()
|
|
|
|
+ .onSuccess((response) -> {
|
|
|
|
+ apiResponse.set(response.bodyAsJson(ApiResponse.class));
|
|
|
|
+ wait.countDown();
|
|
|
|
+ })
|
|
|
|
+ .onFailure((err) -> {
|
|
|
|
+ err.printStackTrace();
|
|
|
|
+ wait.countDown();
|
|
|
|
+ });
|
|
|
|
+ }
|
|
if (wait.await(timeout, TimeUnit.MILLISECONDS)) {
|
|
if (wait.await(timeout, TimeUnit.MILLISECONDS)) {
|
|
return apiResponse.get();
|
|
return apiResponse.get();
|
|
} else {
|
|
} else {
|
|
- apiResponse.get().setCode(500);
|
|
|
|
|
|
+ apiResponse.get().setStatus(500);
|
|
apiResponse.get().setMessage("request timeout");
|
|
apiResponse.get().setMessage("request timeout");
|
|
}
|
|
}
|
|
} catch (Throwable e) {
|
|
} catch (Throwable e) {
|
|
- apiResponse.get().setCode(500);
|
|
|
|
|
|
+ apiResponse.get().setStatus(500);
|
|
apiResponse.get().setMessage(e.getMessage());
|
|
apiResponse.get().setMessage(e.getMessage());
|
|
}
|
|
}
|
|
return apiResponse.get();
|
|
return apiResponse.get();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static void log(String msg) {
|
|
|
|
+ log.info(msg);
|
|
|
|
+ }
|
|
|
|
+
|
|
@Data
|
|
@Data
|
|
@NoArgsConstructor
|
|
@NoArgsConstructor
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
public static class ApiResponse {
|
|
public static class ApiResponse {
|
|
- private int code;
|
|
|
|
|
|
+ private int status;
|
|
private String message;
|
|
private String message;
|
|
private Object data;
|
|
private Object data;
|
|
|
|
+ private long timestamp;
|
|
}
|
|
}
|
|
}
|
|
}
|