Search in sources :

Example 1 with MainApiException

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

the class ModuleGlue method invokeDeviceMethod.

public void invokeDeviceMethod(String connectionId, String deviceId, Object methodInvokeParameters, Handler<AsyncResult<Object>> handler) {
    ModuleClient 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");
        int responseTimeout = params.getInteger("responseTimeoutInSeconds", 0);
        int connectionTimeout = params.getInteger("connectTimeoutInSeconds", 0);
        MethodRequest request = new MethodRequest(methodName, payload, responseTimeout, connectionTimeout);
        try {
            MethodResult result = client.invokeMethod(deviceId, request);
            handler.handle(Future.succeededFuture(makeMethodResultThatEncodesCorrectly(result)));
        } catch (ModuleClientException e) {
            handler.handle(Future.failedFuture(e));
        }
    }
}
Also used : MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) JsonObject(io.vertx.core.json.JsonObject) MainApiException(io.swagger.server.api.MainApiException) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult)

Example 2 with MainApiException

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

the class ModuleGlue method invokeModuleMethod.

public void invokeModuleMethod(String connectionId, String deviceId, String moduleId, Object methodInvokeParameters, Handler<AsyncResult<Object>> handler) {
    ModuleClient 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");
        int responseTimeout = params.getInteger("responseTimeoutInSeconds", 0);
        int connectionTimeout = params.getInteger("connectTimeoutInSeconds", 0);
        MethodRequest request = new MethodRequest(methodName, payload, responseTimeout, connectionTimeout);
        try {
            MethodResult result = client.invokeMethod(deviceId, moduleId, request);
            handler.handle(Future.succeededFuture(makeMethodResultThatEncodesCorrectly(result)));
        } catch (ModuleClientException e) {
            handler.handle(Future.failedFuture(e));
        }
    }
}
Also used : MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) JsonObject(io.vertx.core.json.JsonObject) MainApiException(io.swagger.server.api.MainApiException) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult)

Example 3 with MainApiException

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

the class RegistryGlue method getModuleTwin.

public void getModuleTwin(String connectionId, String deviceId, String moduleId, Handler<AsyncResult<Object>> handler) {
    System.out.printf("getModuleTwin called for %s with deviceId = %s and moduleId = %s%n", connectionId, deviceId, moduleId);
    DeviceTwin client = getClient(connectionId);
    if (client == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid connection id")));
    } else {
        WrappedDeviceTwinDevice twin = new WrappedDeviceTwinDevice(deviceId, moduleId);
        try {
            client.getTwin(twin);
        } catch (IOException | IotHubException e) {
            handler.handle(Future.failedFuture(e));
        }
        handler.handle(Future.succeededFuture(twin.toJsonObject()));
    }
}
Also used : IOException(java.io.IOException) MainApiException(io.swagger.server.api.MainApiException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) WrappedDeviceTwinDevice(io.swagger.server.api.verticle.WrappedDeviceTwinDevice)

Example 4 with MainApiException

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

the class ModuleGlue method connect.

public void connect(String transportType, String connectionString, Certificate caCertificate, Handler<AsyncResult<ConnectResponse>> handler) {
    System.out.printf("Connect called with transport %s%n", transportType);
    IotHubClientProtocol protocol = this.transportFromString(transportType);
    if (protocol == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid transport")));
        return;
    }
    try {
        ModuleClient client = new ModuleClient(connectionString, protocol);
        String cert = caCertificate.getCert();
        if (cert != null && !cert.isEmpty()) {
            client.setOption("SetCertificateAuthority", cert);
        }
        client.open();
        this._clientCount++;
        String connectionId = "moduleClient_" + this._clientCount;
        this._map.put(connectionId, client);
        ConnectResponse cr = new ConnectResponse();
        cr.setConnectionId(connectionId);
        handler.handle(Future.succeededFuture(cr));
    } catch (ModuleClientException | URISyntaxException | IOException e) {
        handler.handle(Future.failedFuture(e));
    }
}
Also used : ConnectResponse(io.swagger.server.api.model.ConnectResponse) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) IOException(java.io.IOException) MainApiException(io.swagger.server.api.MainApiException)

Example 5 with MainApiException

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

the class ModuleGlue method connectFromEnvironment.

public void connectFromEnvironment(String transportType, Handler<AsyncResult<ConnectResponse>> handler) {
    System.out.printf("ConnectFromEnvironment called with transport %s%n", transportType);
    // This is the default URL stream handler factory
    URLStreamHandlerFactory fac = protocol -> {
        if (protocol.equals("http")) {
            return new sun.net.www.protocol.http.Handler();
        } else if (protocol.equals("https")) {
            return new sun.net.www.protocol.https.Handler();
        }
        return null;
    };
    try {
        /*
            This line of code used to be run in older versions of the SDK, and it caused bugs when this library builds
            a module client from environment through the edgelet in conjunction with any other library that called this API.
            This API can only be called once per JVM process, so we had to remove the call to this API from our SDK to maintain
            compatibility with other libraries that call this API. We call this API in this test now so that we guarantee
            that our module client code works even when this API is used beforehand to avoid regression
             */
        URL.setURLStreamHandlerFactory(fac);
    } catch (Error e) {
    // this function only throws if the factory has already been set, so we can ignore this error
    }
    IotHubClientProtocol protocol = this.transportFromString(transportType);
    if (protocol == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid transport")));
        return;
    }
    try {
        ModuleClient client = ModuleClient.createFromEnvironment(protocol);
        client.open();
        this._clientCount++;
        String connectionId = "moduleClient_" + this._clientCount;
        this._map.put(connectionId, client);
        ConnectResponse cr = new ConnectResponse();
        cr.setConnectionId(connectionId);
        handler.handle(Future.succeededFuture(cr));
    } catch (ModuleClientException | IOException e) {
        handler.handle(Future.failedFuture(e));
    }
}
Also used : Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) MainApiException(io.swagger.server.api.MainApiException) RoundtripMethodCallBody(io.swagger.server.api.model.RoundtripMethodCallBody) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) Json(io.vertx.core.json.Json) java.util(java.util) com.microsoft.azure.sdk.iot.device(com.microsoft.azure.sdk.iot.device) DeviceMethodCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback) TwinPropertyCallBack(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack) IOException(java.io.IOException) MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult) ConnectResponse(io.swagger.server.api.model.ConnectResponse) java.net(java.net) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) JsonObject(io.vertx.core.json.JsonObject) Certificate(io.swagger.server.api.model.Certificate) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ConnectResponse(io.swagger.server.api.model.ConnectResponse) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) Handler(io.vertx.core.Handler) IOException(java.io.IOException) java.net(java.net) MainApiException(io.swagger.server.api.MainApiException)

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