Search in sources :

Example 6 with DeviceParser

use of com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser in project azure-iot-sdk-java by Azure.

the class RegistryManagerTest method commonModuleExpectations.

private void commonModuleExpectations(String connectionString, String deviceId, String moduleId) throws Exception {
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = iotHubConnectionString;
            iotHubConnectionString.getHostName();
            result = "aaa.bbb.ccc";
            IotHubConnectionString.getUrlModule(anyString, deviceId, moduleId);
            result = mockUrl;
            mockHttpRequest.send();
            result = mockHttpResponse;
            IotHubExceptionManager.httpResponseVerification((HttpResponse) any);
            mockHttpResponse.getBody();
            result = moduleJson.getBytes(StandardCharsets.UTF_8);
            Deencapsulation.invoke(module, "toDeviceParser");
            result = new DeviceParser();
        }
    };
}
Also used : DeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser)

Example 7 with DeviceParser

use of com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser in project azure-iot-sdk-java by Azure.

the class RegistryManagerTest method commonExpectations.

private void commonExpectations(String connectionString, String deviceId) throws Exception {
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = iotHubConnectionString;
            iotHubConnectionString.getHostName();
            result = "aaa.bbb.ccc";
            IotHubConnectionString.getUrlDevice(anyString, deviceId);
            result = mockUrl;
            mockHttpRequest.send();
            result = mockHttpResponse;
            IotHubExceptionManager.httpResponseVerification((HttpResponse) any);
            mockHttpResponse.getBody();
            result = deviceJson.getBytes(StandardCharsets.UTF_8);
            Deencapsulation.invoke(device, "toDeviceParser");
            result = new DeviceParser();
        }
    };
}
Also used : DeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser)

Example 8 with DeviceParser

use of com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser in project azure-iot-sdk-java by Azure.

the class RegistryManager method getDevices.

/**
 * Get list of devices
 * @deprecated as of release 1.12.0. Please use
 * {@link com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin#queryTwin(String sqlQuery, Integer pageSize)}
 * to query for all devices.
 *
 * @param maxCount The requested count of devices
 * @return The array of requested device objects
 * @throws IOException This exception is thrown if the IO operation failed
 * @throws IotHubException This exception is thrown if the response verification failed
 */
@Deprecated
public ArrayList<Device> getDevices(Integer maxCount) throws IOException, IotHubException, JsonSyntaxException {
    if (maxCount < 1) {
        throw new IllegalArgumentException("maxCount cannot be less then 1");
    }
    URL url = IotHubConnectionString.getUrlDeviceList(this.hostName, maxCount);
    HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0]);
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    try (JsonReader jsonReader = Json.createReader(new StringReader(bodyStr))) {
        ArrayList<Device> deviceList = new ArrayList<>();
        JsonArray deviceArray = jsonReader.readArray();
        for (int i = 0; i < deviceArray.size(); i++) {
            JsonObject jsonObject = deviceArray.getJsonObject(i);
            Device iotHubDevice = new Device(new DeviceParser(jsonObject.toString()));
            deviceList.add(iotHubDevice);
        }
        return deviceList;
    }
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) DeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser) ArrayList(java.util.ArrayList) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) JsonObject(javax.json.JsonObject) URL(java.net.URL) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader)

Example 9 with DeviceParser

use of com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser in project azure-iot-sdk-java by Azure.

the class RegistryManager method getModulesOnDevice.

/**
 * Get modules data by device Id from IotHub
 *
 * @param deviceId The id of requested device
 * @return The module objects on the specific device
 * @throws IOException This exception is thrown if the IO operation failed
 * @throws IotHubException This exception is thrown if the response verification failed
 */
public List<Module> getModulesOnDevice(String deviceId) throws IOException, IotHubException, JsonSyntaxException {
    if (Tools.isNullOrEmpty(deviceId)) {
        throw new IllegalArgumentException("deviceId cannot be null or empty");
    }
    URL url = IotHubConnectionString.getUrlModulesOnDevice(this.hostName, deviceId);
    HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0]);
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    try (JsonReader jsonReader = Json.createReader(new StringReader(bodyStr))) {
        List<Module> moduleList = new ArrayList<>();
        JsonArray deviceArray = jsonReader.readArray();
        for (int i = 0; i < deviceArray.size(); i++) {
            JsonObject jsonObject = deviceArray.getJsonObject(i);
            Module iotHubModule = new Module(new DeviceParser(jsonObject.toString()));
            moduleList.add(iotHubModule);
        }
        return moduleList;
    }
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) DeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser) ArrayList(java.util.ArrayList) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) JsonObject(javax.json.JsonObject) URL(java.net.URL) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader)

Example 10 with DeviceParser

use of com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser in project azure-iot-sdk-java by Azure.

the class RegistryManager method addModule.

/**
 * Add module using the given Module object
 * Return with the response module object from IotHub
 *
 * @param module The module object to add
 * @return The module object for the requested operation
 * @throws IOException This exception is thrown if the IO operation failed
 * @throws IotHubException This exception is thrown if the response verification failed
 */
public Module addModule(Module module) throws IOException, IotHubException, JsonSyntaxException {
    if (module == null) {
        throw new IllegalArgumentException("module cannot be null");
    }
    String moduleJson = module.toDeviceParser().toJson();
    URL url = IotHubConnectionString.getUrlModule(this.hostName, module.getDeviceId(), module.getId());
    HttpRequest request = CreateRequest(url, HttpMethod.PUT, moduleJson.getBytes(StandardCharsets.UTF_8));
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    return new Module(new DeviceParser(bodyStr));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) DeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Aggregations

DeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser)10 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)8 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)8 URL (java.net.URL)8 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 JsonArray (javax.json.JsonArray)2 JsonObject (javax.json.JsonObject)2 JsonReader (javax.json.JsonReader)2