Search in sources :

Example 46 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createDevice.

/**
 * Create a device to bind to a gateway.
 */
protected static void createDevice(String projectId, String cloudRegion, String registryName, String deviceId) throws GeneralSecurityException, IOException {
    // [START iot_create_device]
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    List<Device> devices = service.projects().locations().registries().devices().list(registryPath).setFieldMask("config,gatewayConfig").execute().getDevices();
    if (devices != null) {
        System.out.println("Found " + devices.size() + " devices");
        for (Device d : devices) {
            if ((d.getId() != null && d.getId().equals(deviceId)) || (d.getName() != null && d.getName().equals(deviceId))) {
                System.out.println("Device exists, skipping.");
                return;
            }
        }
    }
    System.out.println("Creating device with id: " + deviceId);
    Device device = new Device();
    device.setId(deviceId);
    GatewayConfig gwConfig = new GatewayConfig();
    gwConfig.setGatewayType("NON_GATEWAY");
    gwConfig.setGatewayAuthMethod("ASSOCIATION_ONLY");
    device.setGatewayConfig(gwConfig);
    Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
    System.out.println("Created device: " + createdDevice.toPrettyString());
// [END iot_create_device]
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) GatewayConfig(com.google.api.services.cloudiot.v1.model.GatewayConfig)

Example 47 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method clearRegistry.

// [END iot_delete_registry]
/**
 * clearRegistry
 *
 * <ul>
 *   <li>Registries can't be deleted if they contain devices,
 *   <li>Gateways (a type of device) can't be deleted if they have bound devices
 *   <li>Devices can't be deleted if bound to gateways...
 * </ul>
 *
 * To completely remove a registry, you must unbind all devices from gateways, then remove all
 * devices in a registry before removing the registry. As pseudocode: <code>
 *   ForAll gateways
 *     ForAll devicesBoundToGateway
 *       unbindDeviceFromGateway
 *   ForAll devices
 *     Delete device by ID
 *   Delete registry
 *  </code>
 */
// [START iot_clear_registry]
protected static void clearRegistry(String cloudRegion, String projectId, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    CloudIot.Projects.Locations.Registries regAlias = service.projects().locations().registries();
    CloudIot.Projects.Locations.Registries.Devices devAlias = regAlias.devices();
    ListDevicesResponse listGatewaysRes = devAlias.list(registryPath).setGatewayListOptionsGatewayType("GATEWAY").execute();
    List<Device> gateways = listGatewaysRes.getDevices();
    // Unbind all devices from all gateways
    if (gateways != null) {
        System.out.println("Found " + gateways.size() + " devices");
        for (Device g : gateways) {
            String gatewayId = g.getId();
            System.out.println("Id: " + gatewayId);
            ListDevicesResponse res = devAlias.list(registryPath).setGatewayListOptionsAssociationsGatewayId(gatewayId).execute();
            List<Device> deviceNumIds = res.getDevices();
            if (deviceNumIds != null) {
                System.out.println("Found " + deviceNumIds.size() + " devices");
                for (Device device : deviceNumIds) {
                    String deviceId = device.getId();
                    System.out.println(String.format("ID: %s", deviceId));
                    // Remove any bindings from the device
                    UnbindDeviceFromGatewayRequest request = new UnbindDeviceFromGatewayRequest();
                    request.setDeviceId(deviceId);
                    request.setGatewayId(gatewayId);
                    regAlias.unbindDeviceFromGateway(registryPath, request).execute();
                }
            } else {
                System.out.println("Gateway has no bound devices.");
            }
        }
    }
    // Remove all devices from the regsitry
    List<Device> devices = devAlias.list(registryPath).execute().getDevices();
    if (devices != null) {
        System.out.println("Found " + devices.size() + " devices");
        for (Device d : devices) {
            String deviceId = d.getId();
            String devicePath = String.format("%s/devices/%s", registryPath, deviceId);
            service.projects().locations().registries().devices().delete(devicePath).execute();
        }
    }
    // Delete the registry
    service.projects().locations().registries().delete(registryPath).execute();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) UnbindDeviceFromGatewayRequest(com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest) ListDevicesResponse(com.google.api.services.cloudiot.v1.model.ListDevicesResponse) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 48 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method listDevicesForGateway.

/**
 * List devices bound to a gateway.
 */
protected static void listDevicesForGateway(String projectId, String cloudRegion, String registryName, String gatewayId) throws IOException, GeneralSecurityException {
    // [START iot_list_devices_for_gateway]
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    List<Device> deviceNumIds = service.projects().locations().registries().devices().list(registryPath).setGatewayListOptionsAssociationsGatewayId(gatewayId).execute().getDevices();
    if (deviceNumIds != null) {
        System.out.println("Found " + deviceNumIds.size() + " devices");
        for (Device device : deviceNumIds) {
            System.out.println(String.format("ID: %s", device.getId()));
        }
    } else {
        System.out.println("Gateway has no bound devices.");
    }
// [END iot_list_devices_for_gateway]
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 49 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createDeviceWithNoAuth.

// [END iot_create_rsa_device]
// [START iot_create_unauth_device]
/**
 * Create a device that has no credentials.
 *
 * This is a valid way to construct a device, however until it is patched with a credential the
 * device will not be able to connect to Cloud IoT.
 */
protected static void createDeviceWithNoAuth(String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;
    System.out.println("Creating device with id: " + deviceId);
    Device device = new Device();
    device.setId(deviceId);
    device.setCredentials(new ArrayList<DeviceCredential>());
    Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
    System.out.println("Created device: " + createdDevice.toPrettyString());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Device(com.google.api.services.cloudiot.v1.model.Device) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 50 with Device

use of org.hl7.fhir.dstu3.model.Device in project synthea by synthetichealth.

the class FhirStu3 method device.

/**
 * Map the HealthRecord.Device into a FHIR Device and add it to the Bundle.
 *
 * @param rand Source of randomness to use when generating ids etc
 * @param personEntry    The Person entry.
 * @param bundle         Bundle to add to.
 * @param device         The device to add.
 * @return The added Entry.
 */
private static BundleEntryComponent device(RandomNumberGenerator rand, BundleEntryComponent personEntry, Bundle bundle, HealthRecord.Device device) {
    Device deviceResource = new Device();
    Device.DeviceUdiComponent udi = new Device.DeviceUdiComponent().setDeviceIdentifier(device.deviceIdentifier).setCarrierHRF(device.udi);
    deviceResource.setUdi(udi);
    deviceResource.setStatus(FHIRDeviceStatus.ACTIVE);
    if (device.manufacturer != null) {
        deviceResource.setManufacturer(device.manufacturer);
    }
    if (device.model != null) {
        deviceResource.setModel(device.model);
    }
    deviceResource.setManufactureDate(new Date(device.manufactureTime));
    deviceResource.setExpirationDate(new Date(device.expirationTime));
    deviceResource.setLotNumber(device.lotNumber);
    deviceResource.setType(mapCodeToCodeableConcept(device.codes.get(0), SNOMED_URI));
    deviceResource.setPatient(new Reference(personEntry.getFullUrl()));
    return newEntry(rand, bundle, deviceResource);
}
Also used : Device(org.hl7.fhir.dstu3.model.Device) Reference(org.hl7.fhir.dstu3.model.Reference) Date(java.util.Date)

Aggregations

Device (com.google.api.services.cloudiot.v1.model.Device)21 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)13 JsonFactory (com.google.api.client.json.JsonFactory)13 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)13 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)13 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)13 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)13 Test (org.junit.jupiter.api.Test)9 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)7 Device (org.hl7.fhir.r4.model.Device)7 Practitioner (org.hl7.fhir.r4.model.Practitioner)7 Reference (org.hl7.fhir.r4.model.Reference)7 ArrayList (java.util.ArrayList)6 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)6 Turtle (org.hl7.fhir.dstu3.utils.formats.Turtle)6 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)6 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)5 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)5 Coding (org.hl7.fhir.r4.model.Coding)5 Identifier (org.hl7.fhir.r4.model.Identifier)5