-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
393 lines (337 loc) · 14.1 KB
/
main.py
File metadata and controls
393 lines (337 loc) · 14.1 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import os
import re
import time
import json
import requests
import threading
import awscrt.exceptions
import sys
import argparse
import yaml
import signal
import logging.config
from uuid import uuid4
from datetime import date
from MQTT.mqtt_callbacks import MqttCallbacks
from awscrt import io as aws_io, mqtt
from awsiot import mqtt_connection_builder, iotshadow
from MQTT.mqtt_device_shadows import DeviceShadows, LockedDeviceState
from Machine.monitoring import MachineStateMonitor
def manage_ctrlc(*args):
# Reset the shadow
global ds, exit_main, mqtt_connection
# Change shadow value to init
ds.change_shadow_value({"upload_enable": 0})
exit_main = True
disconnect_future = mqtt_connection.disconnect()
disconnect_future.result()
# Pressing Ctrl+C will call the function `manage_ctrlc` for child process wrap-up
signal.signal(signal.SIGINT, manage_ctrlc)
subscribe_receiving_event = threading.Event()
def get_adapter_ip_from_ssm(topic_ssm_params, ssm_params_payload):
global subscribe_receiving_event, callbacks
# Clear the event
subscribe_receiving_event.clear()
mqtt_connection.publish(
topic=topic_ssm_params,
payload=json.dumps(ssm_params_payload),
qos=mqtt.QoS.AT_LEAST_ONCE
)
# Wait until you receive a message
while not subscribe_receiving_event.is_set():
pass
adapter_ip_ssm = callbacks.params
return adapter_ip_ssm
def monitor_adapter_ip():
global config, client_id, machine_name
# Get the adapter IP address
# TODO: Improve the way Host is identified
start_looking = False
agent_conf_file = config["agent"]["cfg_file"]
with open(agent_conf_file, "r") as filehandle:
agent_conf = filehandle.readlines()
for conf in agent_conf:
if conf.strip() == machine_name:
start_looking = True
if start_looking:
if "Host" in conf:
adapter_ip = conf.strip().split("=")[-1].strip()
# Stop looking
start_looking = False
break
# Get the IP address from SSM
topic_ssm_params = config["SSM"]["topic_ssm_params"] + "/" + client_id
ssm_params_payload = {
"nodeID": config["SSM"]["nodeID"],
"execution_type": config["SSM"]["execution_type"],
"client_id": client_id
}
adapter_ip_ssm = get_adapter_ip_from_ssm(topic_ssm_params, ssm_params_payload)
# Parse the IP
if adapter_ip_ssm["Status"] == "connected":
try:
adapter_ip_ssm = adapter_ip_ssm["ssm_run_command"]["StandardOutputContent"].strip().split()[1]
except Exception as e:
logging.error("Cannot get the ip address for the agent from SSM with error message: {}".format(e))
exit_process(1)
validate_adapter_ip(adapter_ip, adapter_ip_ssm, agent_conf_file)
else:
logger.error("Adapter Offline")
exit_process(1)
def periodically_check_adapter_ip():
while True:
monitor_adapter_ip()
time.sleep(60 * 60 * 6)
def exit_process(code):
manage_ctrlc()
sys.exit(code)
def validate_adapter_ip(adapter_ip, adapter_ip_ssm, agent_cfg_path):
# Make sure both the arguments are valid ip addresses
pattern = r'^\d+$'
for number in adapter_ip.split("."):
if not bool(re.match(pattern, number)):
logger.warn("Invalid adapter ip addresses from agent_cfg")
exit_process(1)
for number in adapter_ip_ssm.split("."):
if not bool(re.match(pattern, number)):
logger.warn("Invalid adapter ip addresses from SSM manager")
exit_process(1)
if adapter_ip != adapter_ip_ssm:
with open(agent_cfg_path, "r") as filehandle:
modified_config = ""
for line in filehandle.readlines():
if "Host" in line:
new_line = line.replace(adapter_ip, adapter_ip_ssm)
modified_config += new_line
else:
modified_config += line
with open(agent_cfg_path, "w") as filehandle:
filehandle.write(modified_config)
logger.info(f"IP addresses have been updated from {adapter_ip} to {adapter_ip_ssm}")
time.sleep(60)
else:
logger.info("IP addresses matches")
return
def initialize_device_shadows(cp):
# Subscribe to all shadow topics
# Update
update_accepted_subscribed_future, _ = shadow_client.subscribe_to_update_named_shadow_accepted(
request=iotshadow.UpdateNamedShadowSubscriptionRequest(thing_name=cp["client_id"],
shadow_name=cp["shadow_thing_name"]),
qos=mqtt.QoS.AT_LEAST_ONCE,
callback=ds.on_update_shadow_accepted)
update_rejected_subscribed_future, _ = shadow_client.subscribe_to_update_named_shadow_rejected(
request=iotshadow.UpdateNamedShadowSubscriptionRequest(thing_name=cp["client_id"],
shadow_name=cp["shadow_thing_name"]),
qos=mqtt.QoS.AT_LEAST_ONCE,
callback=ds.on_update_shadow_rejected)
# Wait for subscriptions to succeed
update_accepted_subscribed_future.result()
update_rejected_subscribed_future.result()
# Get
get_accepted_subscribed_future, _ = shadow_client.subscribe_to_get_named_shadow_accepted(
request=iotshadow.GetNamedShadowSubscriptionRequest(thing_name=cp["client_id"],
shadow_name=cp["shadow_thing_name"]),
qos=mqtt.QoS.AT_LEAST_ONCE,
callback=ds.on_get_shadow_accepted)
get_rejected_subscribed_future, _ = shadow_client.subscribe_to_get_named_shadow_rejected(
request=iotshadow.GetNamedShadowSubscriptionRequest(thing_name=cp["client_id"],
shadow_name=cp["shadow_thing_name"]),
qos=mqtt.QoS.AT_LEAST_ONCE,
callback=ds.on_get_shadow_rejected)
# Wait for subscriptions to succeed
get_accepted_subscribed_future.result()
get_rejected_subscribed_future.result()
# Delta
delta_subscribed_future, _ = shadow_client.subscribe_to_named_shadow_delta_updated_events(
request=iotshadow.NamedShadowDeltaUpdatedSubscriptionRequest(thing_name=cp["client_id"],
shadow_name=cp["shadow_thing_name"]),
qos=mqtt.QoS.AT_LEAST_ONCE,
callback=ds.on_shadow_delta_updated)
# Wait for subscription to succeed
delta_subscribed_future.result()
# Get the current shadow state
with cp["locked_device_state"].lock:
token = str(uuid4())
# Publish to get current state
publish_get_future = shadow_client.publish_get_named_shadow(
request=iotshadow.GetNamedShadowRequest(thing_name=cp["client_id"], shadow_name=cp["shadow_thing_name"],
client_token=token),
qos=mqtt.QoS.AT_LEAST_ONCE
)
cp["locked_device_state"].request_tokens.add(token)
# Ensure the success of publish
publish_get_future.result()
if __name__ == '__main__':
# Parse Arguments
parser = argparse.ArgumentParser()
parser.add_argument("-cf", "--config", help="Path to the config file", required=True)
args = parser.parse_args()
# Read the configuration
with open(args.config, 'r') as filehandle:
config = yaml.load(filehandle, Loader=yaml.Loader)
# Configure logging
log_dir = config["logging"]["logging_directory"]
if not os.path.exists(log_dir):
os.makedirs(log_dir)
d = {
'version': 1,
'formatters': {
'detailed': {
'class': 'logging.Formatter',
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
}
},
'handlers': {
'file': {
'class': 'logging.FileHandler',
'filename': os.path.join(log_dir, f"{date.today().strftime('%Y%m%d')}.log"),
'mode': 'a',
'formatter': 'detailed',
},
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
},
},
'root': {
'level': 'INFO',
'handlers': ['file', 'console']
},
}
logging.config.dictConfig(d)
logger = logging.getLogger(__name__)
# Keep track of PIDs
metadir = os.path.join("/home/minlab/mtconnect-statusUpdate", "metadata")
if not os.path.exists(metadir):
os.makedirs(metadir)
metapath = os.path.join(metadir, "daq_run.meta")
with open(metapath, "w") as file_handle:
main_process_pid = os.getpid()
file_handle.write(str(main_process_pid))
# Keep track of Process ID
logger.info(f"PID:{main_process_pid}")
# Connect to AWS services
aws_config = config["AWS"]
endpoint = aws_config["endpoint_url"]
cert = aws_config["cert"]
key = aws_config["key"]
root_ca = aws_config["root_ca"]
client_id = aws_config["client_id"]
# Set up AWS logging
if not os.path.exists(aws_config["logging_directory"]):
os.makedirs(aws_config["logging_directory"])
aws_io.init_logging(aws_io.LogLevel.Warn, os.path.join(os.getcwd(), "logs", "aws", "dataLogger_warn.log"))
# Get the topic for connection
topic_status_upload = "status/" + client_id
topic_params_download = "params/" + client_id
# Get all the callbacks
set_aws_params = None
callbacks = MqttCallbacks(set_aws_params, logger, subscribe_receiving_event)
# Initialize AWS Connection
try:
# Establish MQTT Connection
mqtt_connection = mqtt_connection_builder.mtls_from_path(
endpoint=endpoint,
cert_filepath=cert,
pri_key_filepath=key,
ca_filepath=root_ca,
on_connection_interrupted=callbacks.on_connection_interrupted,
on_connection_resumed=callbacks.on_connection_resumed,
client_id=client_id,
clean_session=False,
keep_alive_secs=30,
on_connection_success=callbacks.on_connection_success,
on_connection_failure=callbacks.on_connection_failure,
on_connection_closed=callbacks.on_connection_closed)
logger.info("Connecting to {} with client ID '{}'...\n".format(
endpoint, client_id))
# Start the connection
connect_future = mqtt_connection.connect()
connect_future.result()
sys.stdout.write("Connection Successful!\n")
# Start the subscription
subscribe_future, packet_id = mqtt_connection.subscribe(
topic=topic_params_download,
qos=mqtt.QoS.AT_MOST_ONCE,
callback=callbacks.on_message_received)
subscribe_result = subscribe_future.result()
logger.info("Subscribed with {}\n".format(str(subscribe_result['qos'])))
except awscrt.exceptions.AwsCrtError as e:
logger.error(f"MQTT Connection to AWS failed with {e}")
sys.exit(1)
# Setup Device Shadows
# Device State of shadow
locked_device_state = LockedDeviceState()
connection_params = {
"mqtt_connection": mqtt_connection,
"client_id": client_id,
"shadow_thing_name": aws_config["shadow_name"],
"locked_device_state": locked_device_state
}
# Create Shadow Client
shadow_client = iotshadow.IotShadowClient(connection_params["mqtt_connection"])
ds = DeviceShadows(locked_device_state=connection_params["locked_device_state"],
client_id=connection_params["client_id"],
shadow_thing_name=connection_params["shadow_thing_name"],
shadow_client=shadow_client)
initialize_device_shadows(cp=connection_params)
# Get the name of machine for status update
machine_name = config["adapter"]["machine_name"]
# Monitor the IP address of the adapter
monitor_adapter_ip()
# Enable Periodic monitoring of IP address
monitor_ip_thread = threading.Thread(target=periodically_check_adapter_ip, daemon=True)
# Initialize Machine Monitoring
# Collect the params
devices_xml = config["adapter"]["devices_xml"]
machine_status = MachineStateMonitor(machine_name=machine_name, devices_xml=devices_xml)
# Make a http request - To check availability
url = config["agent"]["url"] + "/" + machine_name + "/current"
response = requests.get(url)
machine_status.update_machine_state(response)
if not machine_status.machine_availability:
logger.error("FANUC ROBONANO NOT AVAILABLE")
sys.exit(1)
# Start sending data every second
start_upload = False
start_timer = time.time()
# Initiate by stopping upload
ds.change_shadow_value({"upload_enable": 0})
exit_main = False
monitor_ip_thread.start()
while True:
# Get the shadow state
with locked_device_state.lock:
if locked_device_state.states["upload_enable"] == 1:
start_upload = True
else:
start_upload = False
if start_upload:
# Make requests to get machine status
response = requests.get(url)
machine_status.update_machine_state(response)
if not machine_status.machine_availability:
logger.error("FANUC ROBONANO NOT AVAILABLE")
start_upload = False
break
# Construct MQTT Messages
mqtt_connection.publish(
topic=topic_status_upload,
payload=json.dumps(machine_status.machine_params),
qos=mqtt.QoS.AT_LEAST_ONCE
)
# Sleep for a while
time.sleep(1.0)
end_timer = time.time()
# Shutdown data transfer after an hour
if end_timer - start_timer > 1800:
start_upload = False
ds.change_shadow_value({"upload_enable": 0})
else:
time.sleep(5.0)
start_timer = time.time()
# Break the loop and exit
if exit_main:
logger.info(f"Exiting process with PID-{main_process_pid}")
break