-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcp_interface.py
212 lines (162 loc) · 6.81 KB
/
gcp_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import json
from google.cloud import pubsub_v1
from loguru import logger
from vm import VM
# Start the VM and add rules from DB
rule_vm = VM()
rule_vm.sync_rules()
def slide_pod_callback(message):
device_id = message.attributes["deviceId"]
# Parse the incoming message, if the message is parsable
# only then run dependent device rules.
try:
raw_string = message.data.decode("utf-8")
# logger.debug(f"Raw Message from {device_id} -> {raw_string}")
data_packet = json.loads(raw_string)
except json.JSONDecodeError:
logger.error(f"Unable to decode JSON message from {device_id}")
return
device_id = message.attributes["deviceId"]
# Execute all rules that depend on the state of given device_id
rule_vm.execute_all_dependent_rules(device_id)
# Acknowledge Cloud PubSub message
message.ack()
def surge_pod_1p_callback(message):
device_id = message.attributes["deviceId"]
# Parse the incoming message, if the message is parsable
# only then run dependent device rules.
try:
raw_string = message.data.decode("utf-8")
# logger.debug(f"Raw Message from {device_id} -> {raw_string}")
data_packet = json.loads(raw_string)
except json.JSONDecodeError:
logger.error(f"Unable to decode JSON message from {device_id}")
return
# Execute all rules that depend on the state of given device_id
rule_vm.execute_all_dependent_rules(device_id)
# Acknowledge Cloud PubSub message
message.ack()
def surge_pod_3p_callback(message):
device_id = message.attributes["deviceId"]
# Parse the incoming message, if the message is parsable
# only then run dependent device rules.
try:
raw_string = message.data.decode("utf-8")
# logger.debug(f"Raw Message from {device_id} -> {raw_string}")
data_packet = json.loads(raw_string)
except json.JSONDecodeError:
logger.error(f"Unable to decode JSON message from {device_id}")
return
# Execute all rules that depend on the state of given device_id
rule_vm.execute_all_dependent_rules(device_id)
# Acknowledge Cloud PubSub message
message.ack()
def sense_pod_callback(message):
device_id = message.attributes["deviceId"]
# Parse the incoming message, if the message is parsable
# only then run dependent device rules.
try:
raw_string = message.data.decode("utf-8")
# logger.debug(f"Raw Message from {device_id} -> {raw_string}")
data_packet = json.loads(raw_string)
except json.JSONDecodeError:
logger.error(f"Unable to decode JSON message from {device_id}")
return
# Execute all rules that depend on the state of given device_id
rule_vm.execute_all_dependent_rules(device_id)
# Acknowledge Cloud PubSub message
message.ack()
def switch_pod_1chpm_callback(message):
# Parse the incoming message, if the message is parsable
# only then run dependent device rules.
device_id = message.attributes["deviceId"]
try:
raw_string = message.data.decode("utf-8")
# logger.debug(f"Raw Message from {device_id} -> {raw_string}")
data_packet = json.loads(raw_string)
except json.JSONDecodeError:
logger.error(f"Unable to decode JSON message from {device_id}")
return
# Execute all rules that depend on the state of given device_id
rule_vm.execute_all_dependent_rules(device_id)
# Acknowledge Cloud PubSub message
message.ack()
def switch_pod_4ch_callback(message):
# Parse the incoming message, if the message is parsable
# only then run dependent device rules.
device_id = message.attributes["deviceId"]
try:
raw_string = message.data.decode("utf-8")
# logger.debug(f"Raw Message from {device_id} -> {raw_string}")
data_packet = json.loads(raw_string)
except json.JSONDecodeError:
logger.error(f"Unable to decode JSON message from {device_id}")
return
# Execute all rules that depend on the state of given device_id
rule_vm.execute_all_dependent_rules(device_id)
# Acknowledge Cloud PubSub message
message.ack()
project_id = "podnet-switch"
subscriber = pubsub_v1.SubscriberClient()
flow_control = pubsub_v1.types.FlowControl(max_messages=10)
# Slide Pod subscription
slide_pod_sub = subscriber.subscription_path(project_id, "slide-pod-state-sub")
slide_pod_future = subscriber.subscribe(
slide_pod_sub, callback=slide_pod_callback, flow_control=flow_control
)
logger.info(f"Listening for messages on {slide_pod_sub}...")
# Surge Pod 1 Phase subscription
surge_pod_1p_sub = subscriber.subscription_path(project_id, "surge-pod-1p-state-sub")
surge_pod_1p_future = subscriber.subscribe(
surge_pod_1p_sub, callback=surge_pod_1p_callback, flow_control=flow_control
)
logger.info(f"Listening for messages on {surge_pod_1p_sub}...")
# Surge Pod 3 Phase subscription
surge_pod_3p_sub = subscriber.subscription_path(project_id, "surge-pod-3p-state-sub")
surge_pod_3p_future = subscriber.subscribe(
surge_pod_3p_sub, callback=surge_pod_3p_callback, flow_control=flow_control
)
logger.info(f"Listening for messages on {surge_pod_3p_sub}...")
# Sense Pod subscription
sense_pod_sub = subscriber.subscription_path(project_id, "sense-pod-state-sub")
sense_pod_future = subscriber.subscribe(
sense_pod_sub, callback=sense_pod_callback, flow_control=flow_control
)
logger.info(f"Listening for messages on {sense_pod_sub}...")
# Switch Pod 1 Channel with PM subscription
switch_pod_1chpm_sub = subscriber.subscription_path(
project_id, "switch-pod-1chpm-state-sub"
)
switch_pod_1chpm_future = subscriber.subscribe(
switch_pod_1chpm_sub, callback=switch_pod_1chpm_callback, flow_control=flow_control
)
logger.info(f"Listening for messages on {switch_pod_1chpm_sub}...")
# Switch Pod 4 Channel subscription
switch_pod_4ch_sub = subscriber.subscription_path(
project_id, "switch-pod-4ch-state-sub"
)
switch_pod_4ch_future = subscriber.subscribe(
switch_pod_4ch_sub, callback=switch_pod_4ch_callback, flow_control=flow_control
)
logger.info(f"Listening for messages on {switch_pod_4ch_sub}...")
# Section responsible for pulling messages from PubSub
with subscriber:
try:
slide_pod_future.result()
surge_pod_1p_future.result()
surge_pod_3p_future.result()
sense_pod_future.result()
switch_pod_1chpm_future.result()
switch_pod_4ch_future.result()
except TimeoutError as e:
logger.error(f"Request timed out. Error: {e}")
except Exception as ex:
logger.error(
f"Some error happened in the underlying execution. PubSub Callback Error: {ex}"
)
slide_pod_future.cancel()
surge_pod_1p_future.cancel()
surge_pod_3p_future.cancel()
sense_pod_future.cancel()
switch_pod_1chpm_future.cancel()
switch_pod_4ch_future.cancel()