From 383b86963ba228cdcc91040c60093ab0514c0d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Bataille?= Date: Thu, 22 Apr 2021 06:18:02 +0200 Subject: [PATCH 1/2] fix: exception do not always have a message attribute --- examples/TrafficLight/lightController.py | 4 ++-- examples/TrafficLight/trafficLight.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/TrafficLight/lightController.py b/examples/TrafficLight/lightController.py index ab8dd6b..c85c106 100644 --- a/examples/TrafficLight/lightController.py +++ b/examples/TrafficLight/lightController.py @@ -133,13 +133,13 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): except DiscoveryInvalidRequestException as e: print("Invalid discovery request detected!") print("Type: " + str(type(e))) - print("Error message: " + e.message) + print("Error message: " + str(e)) print("Stopping...") break except BaseException as e: print("Error in discovery!") print("Type: " + str(type(e))) - print("Error message: " + e.message) + print("Error message: " + str(e)) retryCount -= 1 print("\n" + str(retryCount) + "/" + str(MAX_DISCOVERY_RETRIES) + " retries left\n") print("Backing off...\n") diff --git a/examples/TrafficLight/trafficLight.py b/examples/TrafficLight/trafficLight.py index 9870b7e..3e58755 100644 --- a/examples/TrafficLight/trafficLight.py +++ b/examples/TrafficLight/trafficLight.py @@ -155,13 +155,13 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): except DiscoveryInvalidRequestException as e: print("Invalid discovery request detected!") print("Type: " + str(type(e))) - print("Error message: " + e.message) + print("Error message: " + str(e)) print("Stopping...") break except BaseException as e: print("Error in discovery!") print("Type: " + str(type(e))) - print("Error message: " + e.message) + print("Error message: " + str(e)) retryCount -= 1 print("\n" + str(retryCount) + "/" + str(MAX_DISCOVERY_RETRIES) + " retries left\n") print("Backing off...\n") From 543822078a897bd2ebbfae1cdb9cddf2573dc1f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Bataille?= Date: Thu, 22 Apr 2021 06:21:18 +0200 Subject: [PATCH 2/2] fix: do not rely on first GGC address. Try them all Fixes #13 --- examples/TrafficLight/lightController.py | 40 +++++++++++++++--------- examples/TrafficLight/trafficLight.py | 40 +++++++++++++++--------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/examples/TrafficLight/lightController.py b/examples/TrafficLight/lightController.py index c85c106..79b311d 100644 --- a/examples/TrafficLight/lightController.py +++ b/examples/TrafficLight/lightController.py @@ -63,9 +63,9 @@ def isIpAddress(value): # function reads host GGC ip address from filePath -def getGGCAddr(filePath): +def getGGCAddresses(filePath): f = open(filePath, "r") - return f.readline() + return f.readline().split(',') # Used to discover GGC group CA and end point. After discovering it persists in GROUP_PATH @@ -98,7 +98,7 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): groupId, ca = caList[0] coreInfo = coreList[0] print("Discovered GGC: " + coreInfo.coreThingArn + " from Group: " + groupId) - hostAddr = "" + hostAddr = [] # In this example Ip detector lambda is turned on which reports # the GGC hostAddr to the CIS (Connectivity Information Service) that stores the @@ -106,15 +106,11 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): # This is the information used by discovery and the list of host addresses # could be outdated or wrong and you would normally want to # validate it in a better way. - # For simplicity, we will assume the first host address that looks like an ip - # is the right one to connect to GGC. - # Note: this can also be set manually via the update-connectivity-info CLI for addr in coreInfo.connectivityInfoList: - hostAddr = addr.host - if isIpAddress(hostAddr): - break + if isIpAddress(addr.host): + hostAddr.append(addr.host) - print("Discovered GGC Host Address: " + hostAddr) + print("Discovered GGC Host Addresses: " + ','.join(hostAddr)) print("Now we persist the connectivity/identity information...") groupCA = GROUP_PATH + CA_NAME ggcHostPath = GROUP_PATH + GGC_ADDR_NAME @@ -124,7 +120,7 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): groupCAFile.write(ca) groupCAFile.close() groupHostFile = open(ggcHostPath, "w") - groupHostFile.write(hostAddr) + groupHostFile.write(','.join(hostAddr)) groupHostFile.close() discovered = True @@ -188,8 +184,8 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): # read GGC Host Address from file ggcAddrPath = GROUP_PATH + GGC_ADDR_NAME rootCAPath = GROUP_PATH + CA_NAME -ggcAddr = getGGCAddr(ggcAddrPath) -print("GGC Host Address: " + ggcAddr) +ggcAddrs = getGGCAddresses(ggcAddrPath) +print("GGC Host Address: " + ','.join(ggcAddrs)) print("GGC Group CA Path: " + rootCAPath) print("Private Key of lightController thing Path: " + privateKeyPath) print("Certificate of lightController thing Path: " + certificatePath) @@ -198,7 +194,6 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): # Init AWSIoTMQTTShadowClient myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient(clientId) -myAWSIoTMQTTShadowClient.configureEndpoint(ggcAddr, 8883) myAWSIoTMQTTShadowClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath) # AWSIoTMQTTShadowClient configuration @@ -207,7 +202,22 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): myAWSIoTMQTTShadowClient.configureMQTTOperationTimeout(5) # 5 sec # Connect to AWS IoT -myAWSIoTMQTTShadowClient.connect() +connected = False +for addr in ggcAddrs: + try: + print("Trying to connect to GGC Host Address: " + addr) + myAWSIoTMQTTShadowClient.configureEndpoint(addr, 8883) + myAWSIoTMQTTShadowClient.connect() + connected = True + except BaseException as e: + print("Error while connecting") + print("Address " + addr + " is not accessible") + print("Type: " + str(type(e))) + print("Error message: " + str(e)) + +if not connected: + print("Could not connect to any Greengrass Core address") + sys.exit(-2) # Create a deviceShadow with persistent subscription deviceShadowHandler = myAWSIoTMQTTShadowClient.createShadowHandlerWithName(thingName, True) diff --git a/examples/TrafficLight/trafficLight.py b/examples/TrafficLight/trafficLight.py index 3e58755..4cdf25c 100644 --- a/examples/TrafficLight/trafficLight.py +++ b/examples/TrafficLight/trafficLight.py @@ -84,9 +84,9 @@ def isIpAddress(value): # function reads host GGC ip address from filePath -def getGGCAddr(filePath): +def getGGCAddresses(filePath): f = open(filePath, "r") - return f.readline() + return f.readline().split(',') # Used to discover GGC group CA and end point. After discovering it persists in GROUP_PATH @@ -119,7 +119,7 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): groupId, ca = caList[0] coreInfo = coreList[0] print("Discovered GGC: " + coreInfo.coreThingArn + " from Group: " + groupId) - hostAddr = "" + hostAddr = [] # In this example Ip detector lambda is turned on which reports # the GGC hostAddr to the CIS (Connectivity Information Service) that stores the @@ -127,15 +127,11 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): # This is the information used by discovery and the list of host addresses # could be outdated or wrong and you would normally want to # validate it in a better way. - # For simplicity, we will assume the first host address that looks like an ip - # is the right one to connect to GGC. - # Note: this can also be set manually via the update-connectivity-info CLI for addr in coreInfo.connectivityInfoList: - hostAddr = addr.host - if isIpAddress(hostAddr): - break + if isIpAddress(addr.host): + hostAddr.append(addr.host) - print("Discovered GGC Host Address: " + hostAddr) + print("Discovered GGC Host Addresses: " + ','.join(hostAddr)) print("Now we persist the connectivity/identity information...") groupCA = GROUP_PATH + CA_NAME @@ -146,7 +142,7 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): groupCAFile.write(ca) groupCAFile.close() groupHostFile = open(ggcHostPath, "w") - groupHostFile.write(hostAddr) + groupHostFile.write(','.join(hostAddr)) groupHostFile.close() discovered = True @@ -210,8 +206,8 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): # read GGC Host Address from file ggcAddrPath = GROUP_PATH + GGC_ADDR_NAME rootCAPath = GROUP_PATH + CA_NAME -ggcAddr = getGGCAddr(ggcAddrPath) -print("GGC Host Address: " + ggcAddr) +ggcAddrs = getGGCAddresses(ggcAddrPath) +print("GGC Host Addresses: " + ','.join(ggcAddrs)) print("GGC Group CA Path: " + rootCAPath) print("Private Key of trafficLight thing Path: " + privateKeyPath) print("Certificate of trafficLight thing Path: " + certificatePath) @@ -220,7 +216,6 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): # Init AWSIoTMQTTShadowClient myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient(clientId) -myAWSIoTMQTTShadowClient.configureEndpoint(ggcAddr, 8883) myAWSIoTMQTTShadowClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath) # AWSIoTMQTTShadowClient configuration @@ -229,7 +224,22 @@ def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId): myAWSIoTMQTTShadowClient.configureMQTTOperationTimeout(5) # 5 sec # Connect to AWS IoT -myAWSIoTMQTTShadowClient.connect() +connected = False +for addr in ggcAddrs: + try: + print("Trying to connect to GGC Host Address: " + addr) + myAWSIoTMQTTShadowClient.configureEndpoint(addr, 8883) + myAWSIoTMQTTShadowClient.connect() + connected = True + except BaseException as e: + print("Error while connecting") + print("Address " + addr + " is not accessible") + print("Type: " + str(type(e))) + print("Error message: " + str(e)) + +if not connected: + print("Could not connect to any Greengrass Core address") + sys.exit(-2) # Create a deviceShadow with persistent subscription deviceShadowHandler = myAWSIoTMQTTShadowClient.createShadowHandlerWithName(thingName, True)