Search in sources :

Example 6 with MainApiException

use of io.swagger.server.api.MainApiException in project azure-iot-sdk-java by Azure.

the class ModuleGlue method waitForDesiredPropertyPatch.

public void waitForDesiredPropertyPatch(String connectionId, Handler<AsyncResult<Object>> handler) {
    System.out.printf("waitForDesiredPropertyPatch with %s%n", connectionId);
    ModuleClient client = getClient(connectionId);
    if (client == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid connection id")));
    } else {
        this._deviceTwinPropertyCallback.setHandler(res -> {
            if (res.succeeded()) {
                JsonObject obj = (JsonObject) res.result();
                Object desiredProps = obj.getJsonObject("properties").getJsonObject("desired");
                handler.handle(Future.succeededFuture(desiredProps));
            } else {
                handler.handle(res);
            }
        });
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) MainApiException(io.swagger.server.api.MainApiException)

Example 7 with MainApiException

use of io.swagger.server.api.MainApiException in project azure-iot-sdk-java by Azure.

the class ServiceGlue method invokeMethodCommon.

private void invokeMethodCommon(String connectionId, String deviceId, String moduleId, Object methodInvokeParameters, Handler<AsyncResult<Object>> handler) {
    System.out.printf("invoking method on %s with deviceId = %s moduleId = %s%n", connectionId, deviceId, moduleId);
    System.out.println(methodInvokeParameters);
    DeviceMethod client = getClient(connectionId);
    if (client == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid connection id")));
    } else {
        JsonObject params = (JsonObject) methodInvokeParameters;
        String methodName = params.getString("methodName");
        String payload = params.getString("payload");
        Long responseTimeout = params.getLong("responseTimeoutInSeconds", 0L);
        Long connectionTimeout = params.getLong("connectTimeoutInSeconds", 0L);
        MethodResult result = null;
        System.out.printf("invoking%n");
        try {
            if (moduleId == null) {
                result = client.invoke(deviceId, methodName, responseTimeout, connectionTimeout, payload);
            } else {
                result = client.invoke(deviceId, moduleId, methodName, responseTimeout, connectionTimeout, payload);
            }
        } catch (IotHubException e) {
            handler.handle(Future.failedFuture(e));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.printf("invoke returned%n");
        System.out.println(result);
        handler.handle(Future.succeededFuture(result));
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) MainApiException(io.swagger.server.api.MainApiException) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)

Example 8 with MainApiException

use of io.swagger.server.api.MainApiException in project azure-iot-sdk-java by Azure.

the class RegistryGlue method sendModuleTwinPatch.

public void sendModuleTwinPatch(String connectionId, String deviceId, String moduleId, Object props, Handler<AsyncResult<Void>> handler) {
    System.out.printf("sendModuleTwinPatch called for %s with deviceId = %s and moduleId = %s%n", connectionId, deviceId, moduleId);
    System.out.println(props.toString());
    DeviceTwin client = getClient(connectionId);
    if (client == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid connection id")));
    } else {
        DeviceTwinDevice twin = new DeviceTwinDevice(deviceId, moduleId);
        Set<Pair> newProps = new HashSet<>();
        Map<String, Object> desiredProps = ((JsonObject) props).getJsonObject("properties").getJsonObject("desired").getMap();
        for (String key : desiredProps.keySet()) {
            newProps.add(new Pair(key, desiredProps.get(key)));
        }
        twin.setDesiredProperties(newProps);
        try {
            client.updateTwin(twin);
        } catch (IotHubException | IOException e) {
            handler.handle(Future.failedFuture(e));
        }
        handler.handle(Future.succeededFuture());
    }
}
Also used : WrappedDeviceTwinDevice(io.swagger.server.api.verticle.WrappedDeviceTwinDevice) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) JsonObject(io.vertx.core.json.JsonObject) MainApiException(io.swagger.server.api.MainApiException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet)

Aggregations

MainApiException (io.swagger.server.api.MainApiException)8 JsonObject (io.vertx.core.json.JsonObject)6 IOException (java.io.IOException)5 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)4 MethodRequest (com.microsoft.azure.sdk.iot.device.edge.MethodRequest)3 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)3 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)2 ConnectResponse (io.swagger.server.api.model.ConnectResponse)2 WrappedDeviceTwinDevice (io.swagger.server.api.verticle.WrappedDeviceTwinDevice)2 com.microsoft.azure.sdk.iot.device (com.microsoft.azure.sdk.iot.device)1 DeviceMethodCallback (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback)1 DeviceMethodData (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData)1 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)1 TwinPropertyCallBack (com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack)1 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)1 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)1 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)1 Certificate (io.swagger.server.api.model.Certificate)1