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]
}
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]
}
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;
}
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]
}
Aggregations