in gateway_watchdog/src_get_wireless_gateway_statistics_lambda/lambda.py [0:0]
def handler(event, context):
logger.info("Received event: %s" % json.dumps(event))
# Check if all the necessary params are included and return an error ststus otherwise
for i in OBLIGATORY_PARAMETERS:
if i not in event:
logger.error(f"Parameter {i} missing ")
return {
"status": 500,
"errormessage": f"Parameter {i} missing"
}
gateway_ids = []
errors = []
try:
if ("GatewayId" in event):
gateway_ids = [event["GatewayId"]]
else:
gateway_ids = map(lambda entry: entry["Id"], client_iotwireless.list_wireless_gateways()["WirelessGatewayList"])
if TEST_MODE and ("test" in event):
logger.info(f"Test event data {event.get('test')}")
put_events_message(event.get("test").get("gatewayid"),
connection_status=event.get("test").get("connection_status"), last_uplink_received_timestamp_ms=int(event.get("test").get("last_uplink_received_timestamp_ms")))
# Iterate over all wireless gateways
for gateway_id in gateway_ids:
logger.info(f"Processing gateway with id {gateway_id}")
# Retrieve gateway statistics
response = client_iotwireless.get_wireless_gateway_statistics(WirelessGatewayId=gateway_id)
logger.info(f"Gateway statistics: {response}")
if ("ConnectionStatus" in response):
updated_connection_status = response.get("ConnectionStatus")
updated_last_uplink_received_timestamp_ms = round(dateutil.parser.isoparse(response.get("LastUplinkReceivedAt")).timestamp() * 1000)
logger.info(f"Gateway {gateway_id}, Last uplink ts={updated_last_uplink_received_timestamp_ms}, connection status={updated_connection_status}")
put_events_message(gateway_id,
connection_status=updated_connection_status, last_uplink_received_timestamp_ms=updated_last_uplink_received_timestamp_ms)
else:
logger.info(f"Gateway {gateway_id} is lacking 'ConnectionStastus', must has never yet connected. Ignoring it.")
result = {
"status": 200,
"timestamp_ms": str(round(time.time())),
"errors": errors
}
return result
except Exception as e:
exception_type, exception_value, exception_traceback = sys.exc_info()
traceback_string = traceback.format_exception(
exception_type, exception_value, exception_traceback)
logger.error("Error: " + str(e))
errors.append({
"errormessage": str(e),
"traceback": traceback_string
})
result = {
"status": 500,
"timestamp_ms": str(round(time.time())),
"errors": errors
}
return result