42
42
import com .google .api .services .cloudiot .v1 .model .SetIamPolicyRequest ;
43
43
import com .google .api .services .cloudiot .v1 .model .UnbindDeviceFromGatewayRequest ;
44
44
import com .google .api .services .cloudiot .v1 .model .UnbindDeviceFromGatewayResponse ;
45
+
45
46
import com .google .cloud .Role ;
46
47
import com .google .cloud .pubsub .v1 .TopicAdminClient ;
47
48
import com .google .common .io .Files ;
57
58
import java .util .Arrays ;
58
59
import java .util .Base64 ;
59
60
import java .util .List ;
60
- import org .apache .commons .cli .HelpFormatter ;
61
- import org .eclipse .paho .client .mqttv3 .MqttClient ;
62
- import org .eclipse .paho .client .mqttv3 .MqttException ;
63
- import org .eclipse .paho .client .mqttv3 .MqttMessage ;
64
61
62
+ import org .apache .commons .cli .HelpFormatter ;
65
63
/**
66
64
* Example of using Cloud IoT device manager API to administer devices, registries and projects.
67
65
*
93
91
* </code>
94
92
* </pre>
95
93
*/
94
+
96
95
public class DeviceRegistryExample {
97
96
98
97
static final String APP_NAME = "DeviceRegistryExample" ;
@@ -780,7 +779,7 @@ public static void setIamPermissions(
780
779
// [END iot_set_iam_policy]
781
780
782
781
/** Send a command to a device. * */
783
- // [START send_command ]
782
+ // [START iot_send_command ]
784
783
public static void sendCommand (
785
784
String deviceId , String projectId , String cloudRegion , String registryName , String data )
786
785
throws GeneralSecurityException , IOException {
@@ -817,12 +816,12 @@ public static void sendCommand(
817
816
818
817
System .out .println ("Command response: " + res .toString ());
819
818
}
820
- // [END send_command ]
819
+ // [END iot_send_command ]
821
820
822
821
public static void bindDeviceToGateway (
823
822
String projectId , String cloudRegion , String registryName , String deviceId , String gatewayId )
824
823
throws GeneralSecurityException , IOException {
825
- // [START bind_device_to_gateway ]
824
+ // [START iot_bind_device_to_gateway ]
826
825
createDevice (projectId , cloudRegion , registryName , deviceId );
827
826
828
827
GoogleCredential credential =
@@ -851,13 +850,13 @@ public static void bindDeviceToGateway(
851
850
.execute ();
852
851
853
852
System .out .println (String .format ("Device bound: %s" , response .toPrettyString ()));
854
- // [END bind_device_to_gateway ]
853
+ // [END iot_bind_device_to_gateway ]
855
854
}
856
855
857
856
public static void unbindDeviceFromGateway (
858
857
String projectId , String cloudRegion , String registryName , String deviceId , String gatewayId )
859
858
throws GeneralSecurityException , IOException {
860
- // [START unbind_device_from_gateway ]
859
+ // [START iot_unbind_device_from_gateway ]
861
860 GoogleCredential credential =
862
861
GoogleCredential .getApplicationDefault ().createScoped (CloudIotScopes .all ());
863
862
JsonFactory jsonFactory = JacksonFactory .getDefaultInstance ();
@@ -884,39 +883,14 @@ public static void unbindDeviceFromGateway(
884
883
.execute ();
885
884
886
885
System .out .println (String .format ("Device unbound: %s" , response .toPrettyString ()));
887
- // [END unbind_device_from_gateway]
888
- }
889
-
890
- public static void attachDeviceToGateway (MqttClient client , String deviceId )
891
- throws MqttException {
892
- // [START attach_device]
893
- final String attachTopic = String .format ("/devices/%s/attach" , deviceId );
894
- System .out .println (String .format ("Attaching: %s" , attachTopic ));
895
- String attachPayload = "{}" ;
896
- MqttMessage message = new MqttMessage (attachPayload .getBytes ());
897
- message .setQos (1 );
898
- client .publish (attachTopic , message );
899
- // [END attach_device]
900
- }
901
-
902
- /** Detaches a bound device from the Gateway. */
903
- public static void detachDeviceFromGateway (MqttClient client , String deviceId )
904
- throws MqttException {
905
- // [START detach_device]
906
- final String detachTopic = String .format ("/devices/%s/detach" , deviceId );
907
- System .out .println (String .format ("Detaching: %s" , detachTopic ));
908
- String attachPayload = "{}" ;
909
- MqttMessage message = new MqttMessage (attachPayload .getBytes ());
910
- message .setQos (1 );
911
- client .publish (detachTopic , message );
912
- // [END detach_device]
886
+ // [END iot_unbind_device_from_gateway]
913
887
}
914
888
915
889
/** Create a device to bind to a gateway. */
916
890
public static void createDevice (
917
891
String projectId , String cloudRegion , String registryName , String deviceId )
918
892
throws GeneralSecurityException , IOException {
919
- // [START create_device ]
893
+ // [START iot_create_device ]
920
894
GoogleCredential credential =
921
895
GoogleCredential .getApplicationDefault ().createScoped (CloudIotScopes .all ());
922
896
JsonFactory jsonFactory = JacksonFactory .getDefaultInstance ();
@@ -971,7 +945,7 @@ public static void createDevice(
971
945
.execute ();
972
946
973
947
System .out .println ("Created device: " + createdDevice .toPrettyString ());
974
- // [END create_device ]
948
+ // [END iot_create_device ]
975
949
}
976
950
977
951
/** Create a gateway to bind devices to. */
@@ -983,7 +957,7 @@ public static void createGateway(
983
957
String certificateFilePath ,
984
958
String algorithm )
985
959
throws GeneralSecurityException , IOException {
986
- // [START create_gateway ]
960
+ // [START iot_create_gateway ]
987
961
GoogleCredential credential =
988
962
GoogleCredential .getApplicationDefault ().createScoped (CloudIotScopes .all ());
989
963
JsonFactory jsonFactory = JacksonFactory .getDefaultInstance ();
@@ -1030,12 +1004,12 @@ public static void createGateway(
1030
1004
.execute ();
1031
1005
1032
1006
System .out .println ("Created gateway: " + createdDevice .toPrettyString ());
1033
- // [END create_gateway ]
1007
+ // [END iot_create_gateway ]
1034
1008
}
1035
1009
1036
1010
public static void listGateways (String projectId , String cloudRegion , String registryName )
1037
1011
throws IOException , GeneralSecurityException {
1038
- // [START list_gateways ]
1012
+ // [START iot_list_gateways ]
1039
1013
GoogleCredential credential =
1040
1014
GoogleCredential .getApplicationDefault ().createScoped (CloudIotScopes .all ());
1041
1015
JsonFactory jsonFactory = JacksonFactory .getDefaultInstance ();
@@ -1049,42 +1023,38 @@ public static void listGateways(String projectId, String cloudRegion, String reg
1049
1023
String .format (
1050
1024
"projects/%s/locations/%s/registries/%s" , projectId , cloudRegion , registryName );
1051
1025
1052
- List <Device > devices =
1026
+ List <Device > gateways =
1053
1027
service
1054
1028
.projects ()
1055
1029
.locations ()
1056
1030
.registries ()
1057
1031
.devices ()
1058
1032
.list (registryPath )
1059
- .setFieldMask ( "config,gatewayConfig " )
1033
+ .setGatewayListOptionsGatewayType ( "GATEWAY " )
1060
1034
.execute ()
1061
1035
.getDevices ();
1062
1036
1063
- if (devices != null ) {
1064
- System .out .println ("Found " + devices .size () + " devices" );
1065
- for (Device d : devices ) {
1066
- if (d .getGatewayConfig () != null
1067
- && d .getGatewayConfig ().getGatewayType () != null
1068
- && d .getGatewayConfig ().getGatewayType ().equals ("GATEWAY" )) {
1069
- System .out .println ("Id: " + d .getId ());
1070
- if (d .getConfig () != null ) {
1071
- // Note that this will show the device config in Base64 encoded format.
1072
- System .out .println ("Config: " + d .getGatewayConfig ().toPrettyString ());
1073
- }
1074
- System .out .println ();
1037
+ if (gateways != null ) {
1038
+ System .out .println ("Found " + gateways .size () + " devices" );
1039
+ for (Device d : gateways ) {
1040
+ System .out .println ("Id: " + d .getId ());
1041
+ if (d .getConfig () != null ) {
1042
+ // Note that this will show the device config in Base64 encoded format.
1043
+ System .out .println ("Config: " + d .getGatewayConfig ().toPrettyString ());
1075
1044
}
1045
+ System .out .println ();
1076
1046
}
1077
1047
} else {
1078
1048
System .out .println ("Registry has no devices." );
1079
1049
}
1080
- // [END list_gateways ]
1050
+ // [END iot_list_gateways ]
1081
1051
}
1082
1052
1083
1053
/** List devices bound to a gateway. */
1084
1054
public static void listDevicesForGateway (
1085
1055
String projectId , String cloudRegion , String registryName , String gatewayId )
1086
1056
throws IOException , GeneralSecurityException {
1087
- // [START list_devices_for_gateway ]
1057
+ // [START iot_list_devices_for_gateway ]
1088
1058
GoogleCredential credential =
1089
1059
GoogleCredential .getApplicationDefault ().createScoped (CloudIotScopes .all ());
1090
1060
JsonFactory jsonFactory = JacksonFactory .getDefaultInstance ();
@@ -1094,11 +1064,6 @@ public static void listDevicesForGateway(
1094
1064
.setApplicationName (APP_NAME )
1095
1065
.build ();
1096
1066
1097
- final String gatewayPath =
1098
- String .format (
1099
- "projects/%s/locations/%s/registries/%s/devices/%s" ,
1100
- projectId , cloudRegion , registryName , gatewayId );
1101
-
1102
1067
final String registryPath =
1103
1068
String .format (
1104
1069
"projects/%s/locations/%s/registries/%s" , projectId , cloudRegion , registryName );
@@ -1122,7 +1087,7 @@ public static void listDevicesForGateway(
1122
1087
} else {
1123
1088
System .out .println ("Gateway has no bound devices." );
1124
1089
}
1125
- // [END list_devices_for_gateway ]
1090
+ // [END iot_list_devices_for_gateway ]
1126
1091
}
1127
1092
1128
1093
/** Entry poit for CLI. */
@@ -1253,6 +1218,15 @@ public static void main(String[] args) throws Exception {
1253
1218
options .role );
1254
1219
}
1255
1220
break ;
1221
+ case "list-gateways" :
1222
+ System .out .println ("Listing gateways: " );
1223
+ listGateways (options .projectId , options .cloudRegion , options .registryName );
1224
+ break ;
1225
+ case "list-devices-for-gateway" :
1226
+ System .out .println ("Listing devices for a gateway: " );
1227
+ listDevicesForGateway (
1228
+ options .projectId , options .cloudRegion , options .registryName , options .gatewayId );
1229
+ break ;
1256
1230
case "send-command" :
1257
1231
System .out .println ("Sending command to device:" );
1258
1232
sendCommand (
0 commit comments