use of com.microsoft.azure.sdk.iot.service.ProxyOptions in project azure-iot-sdk-java by Azure.
the class DeviceMethod method invokeMethod.
/**
* Directly invokes a method on the device and return its result.
*
* @param url is the path where the request is send to.
* @param methodName is the name of the method that shall be invoked on the device.
* @param responseTimeoutInSeconds is the maximum waiting time for a response from the device in seconds.
* @param connectTimeoutInSeconds is the maximum waiting time for a response from the connection in seconds.
* @param payload is the the method parameter.
* @return the status and payload resulted from the method invoke.
* @throws IotHubException This exception is thrown if the response verification failed.
* @throws IOException This exception is thrown if the IO operation failed.
*/
private synchronized MethodResult invokeMethod(URL url, String methodName, Long responseTimeoutInSeconds, Long connectTimeoutInSeconds, Object payload) throws IotHubException, IOException {
MethodParser methodParser = new MethodParser(methodName, responseTimeoutInSeconds, connectTimeoutInSeconds, payload);
String json = methodParser.toJson();
if (json == null) {
throw new IllegalArgumentException("MethodParser return null Json");
}
ProxyOptions proxyOptions = options.getProxyOptions();
Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.POST, json.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
MethodParser methodParserResponse = new MethodParser();
methodParserResponse.fromJson(new String(response.getBody(), StandardCharsets.UTF_8));
return new MethodResult(methodParserResponse.getStatus(), methodParserResponse.getPayload());
}
Aggregations