Search in sources :

Example 1 with GatewayConfig

use of com.google.api.services.cloudiot.v1.model.GatewayConfig in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createGateway.

/**
 * Create a gateway to bind devices to.
 */
protected static void createGateway(String projectId, String cloudRegion, String registryName, String gatewayId, String certificateFilePath, String algorithm) throws GeneralSecurityException, IOException {
    // [START iot_create_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);
    System.out.println("Creating gateway with id: " + gatewayId);
    Device device = new Device();
    device.setId(gatewayId);
    GatewayConfig gwConfig = new GatewayConfig();
    gwConfig.setGatewayType("GATEWAY");
    gwConfig.setGatewayAuthMethod("ASSOCIATION_ONLY");
    String keyFormat = "RSA_X509_PEM";
    if ("ES256".equals(algorithm)) {
        keyFormat = "ES256_PEM";
    }
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    byte[] keyBytes = java.nio.file.Files.readAllBytes(Paths.get(certificateFilePath));
    publicKeyCredential.setKey(new String(keyBytes, StandardCharsets.US_ASCII));
    publicKeyCredential.setFormat(keyFormat);
    DeviceCredential deviceCredential = new DeviceCredential();
    deviceCredential.setPublicKey(publicKeyCredential);
    device.setGatewayConfig(gwConfig);
    device.setCredentials(Collections.singletonList(deviceCredential));
    Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
    System.out.println("Created gateway: " + createdDevice.toPrettyString());
// [END iot_create_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) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) GatewayConfig(com.google.api.services.cloudiot.v1.model.GatewayConfig)

Example 2 with GatewayConfig

use of com.google.api.services.cloudiot.v1.model.GatewayConfig in project java-docs-samples by GoogleCloudPlatform.

the class CloudiotPubsubExampleServer method createDevice.

/**
 * Create a device to bind to a gateway.
 */
public static void createDevice(String projectId, String cloudRegion, String registryName, String deviceId) throws GeneralSecurityException, IOException {
    // [START 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 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 3 with GatewayConfig

use of com.google.api.services.cloudiot.v1.model.GatewayConfig in project udmi by faucetsdn.

the class CloudIotManager method getGatewayConfig.

private GatewayConfig getGatewayConfig(CloudDeviceSettings settings) {
    boolean isGateway = settings.proxyDevices != null;
    GatewayConfig gwConfig = new GatewayConfig();
    gwConfig.setGatewayType(isGateway ? "GATEWAY" : "NON_GATEWAY");
    gwConfig.setGatewayAuthMethod("ASSOCIATION_ONLY");
    return gwConfig;
}
Also used : GatewayConfig(com.google.api.services.cloudiot.v1.model.GatewayConfig)

Example 4 with GatewayConfig

use of com.google.api.services.cloudiot.v1.model.GatewayConfig 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)

Aggregations

GatewayConfig (com.google.api.services.cloudiot.v1.model.GatewayConfig)4 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)3 JsonFactory (com.google.api.client.json.JsonFactory)3 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)3 Device (com.google.api.services.cloudiot.v1.model.Device)3 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)3 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)1 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)1