Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c630c60

Browse filesBrowse files
authored
Moves Gateway demo to MQTT folder (GoogleCloudPlatform#1983)
* Copies gateway demo to MQTT folder
1 parent 165b678 commit c630c60
Copy full SHA for c630c60

File tree

Expand file treeCollapse file tree

1 file changed

+167
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+167
-0
lines changed
+167Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# [START iot_gateway_demo]
16+
import csv
17+
import datetime
18+
import logging
19+
import os
20+
import sys
21+
import time
22+
23+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'manager')) # noqa
24+
25+
import cloudiot_mqtt_example
26+
import manager
27+
28+
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.CRITICAL)
29+
30+
cloud_region = 'us-central1'
31+
device_id_template = 'test-device-{}'
32+
gateway_id_template = 'test-gateway-{}'
33+
topic_id = 'test-device-events-{}'.format(int(time.time()))
34+
35+
ca_cert_path = 'resources/roots.pem'
36+
log_path = 'config_log.csv'
37+
rsa_cert_path = 'resources/rsa_cert.pem'
38+
rsa_private_path = 'resources/rsa_private.pem'
39+
40+
if ('GCLOUD_PROJECT' not in os.environ or
41+
'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ):
42+
print(
43+
'You must set GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS')
44+
quit()
45+
46+
project_id = os.environ['GCLOUD_PROJECT']
47+
service_account_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS']
48+
49+
pubsub_topic = 'projects/{}/topics/{}'.format(project_id, topic_id)
50+
registry_id = 'test-registry-{}'.format(int(time.time()))
51+
52+
base_url = 'https://console.cloud.google.com/iot/locations/{}'.format(
53+
cloud_region)
54+
edit_template = '{}/registries/{}?project={}'.format(
55+
base_url, '{}', '{}')
56+
57+
device_url_template = '{}/registries/{}/devices/{}?project={}'.format(
58+
base_url, '{}', '{}', '{}')
59+
60+
mqtt_bridge_hostname = 'mqtt.googleapis.com'
61+
mqtt_bridge_port = 8883
62+
63+
num_messages = 15
64+
jwt_exp_time = 20
65+
listen_time = 30
66+
67+
68+
if __name__ == '__main__':
69+
print("Running demo")
70+
71+
gateway_id = device_id_template.format('RS256')
72+
device_id = device_id_template.format('noauthbind')
73+
74+
# [START iot_gateway_demo_create_registry]
75+
print('Creating registry: {}'.format(registry_id))
76+
manager.create_registry(
77+
service_account_json, project_id, cloud_region, pubsub_topic,
78+
registry_id)
79+
# [END iot_gateway_demo_create_registry]
80+
81+
# [START iot_gateway_demo_create_gateway]
82+
print('Creating gateway: {}'.format(gateway_id))
83+
manager.create_gateway(
84+
service_account_json, project_id, cloud_region, registry_id,
85+
None, gateway_id, rsa_cert_path, 'RS256')
86+
# [END iot_gateway_demo_create_gateway]
87+
88+
# [START iot_gateway_demo_create_bound_device]
89+
print('Creating device to bind: {}'.format(device_id))
90+
manager.create_device(
91+
service_account_json, project_id, cloud_region, registry_id,
92+
device_id)
93+
# [END iot_gateway_demo_create_bound_device]
94+
95+
# [START iot_gateway_demo_bind_device]
96+
print('Binding device')
97+
manager.bind_device_to_gateway(
98+
service_account_json, project_id, cloud_region, registry_id,
99+
device_id, gateway_id)
100+
# [END iot_gateway_demo_bind_device]
101+
102+
# [START iot_gateway_demo_listen]
103+
print('Listening for messages for {} seconds'.format(listen_time))
104+
print('Try setting configuration in: ')
105+
print('\t{}'.format(edit_template.format(registry_id, project_id)))
106+
try:
107+
input("Press enter to continue")
108+
except SyntaxError:
109+
pass
110+
111+
def log_callback(client):
112+
def log_on_message(unused_client, unused_userdata, message):
113+
if not os.path.exists(log_path):
114+
with open(log_path, 'w') as csvfile:
115+
logwriter = csv.writer(csvfile, dialect='excel')
116+
logwriter.writerow(['time', 'topic', 'data'])
117+
118+
with open(log_path, 'a') as csvfile:
119+
logwriter = csv.writer(csvfile, dialect='excel')
120+
logwriter.writerow([
121+
datetime.datetime.now().isoformat(),
122+
message.topic,
123+
message.payload])
124+
125+
client.on_message = log_on_message
126+
127+
cloudiot_mqtt_example.listen_for_messages(
128+
service_account_json, project_id, cloud_region, registry_id,
129+
device_id, gateway_id, num_messages, rsa_private_path, 'RS256',
130+
ca_cert_path, mqtt_bridge_hostname, mqtt_bridge_port,
131+
jwt_exp_time, listen_time, log_callback)
132+
# [END iot_gateway_demo_listen]
133+
134+
# [START iot_gateway_demo_publish]
135+
print('Publishing messages demo')
136+
print('Publishing: {} messages'.format(num_messages))
137+
cloudiot_mqtt_example.send_data_from_bound_device(
138+
service_account_json, project_id, cloud_region, registry_id,
139+
device_id, gateway_id, num_messages, rsa_private_path, 'RS256',
140+
ca_cert_path, mqtt_bridge_hostname, mqtt_bridge_port,
141+
jwt_exp_time, "Hello from gateway_demo.py")
142+
143+
print('You can read the state messages for your device at this URL:')
144+
print('\t{}'.format(device_url_template).format(
145+
registry_id, device_id, project_id))
146+
try:
147+
input('Press enter to continue after reading the messages.')
148+
except SyntaxError:
149+
pass
150+
# [END iot_gateway_demo_publish]
151+
152+
# [START iot_gateway_demo_cleanup]
153+
# Clean up
154+
manager.unbind_device_from_gateway(
155+
service_account_json, project_id, cloud_region, registry_id,
156+
device_id, gateway_id)
157+
manager.delete_device(
158+
service_account_json, project_id, cloud_region, registry_id,
159+
device_id)
160+
manager.delete_device(
161+
service_account_json, project_id, cloud_region, registry_id,
162+
gateway_id)
163+
manager.delete_registry(
164+
service_account_json, project_id, cloud_region, registry_id)
165+
# [END iot_gateway_demo_cleanup]
166+
167+
# [END iot_gateway_demo]

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.