in traffic-light-example-python/lightController.py [0:0]
def discoverGGC(host, iotCAPath, certificatePath, privateKeyPath, clientId):
# Progressive back off core
backOffCore = ProgressiveBackOffCore()
# Discover GGCs
discoveryInfoProvider = DiscoveryInfoProvider()
discoveryInfoProvider.configureEndpoint(host)
discoveryInfoProvider.configureCredentials(iotCAPath, certificatePath, privateKeyPath)
discoveryInfoProvider.configureTimeout(10) # 10 sec
print("Iot end point: " + host)
print("Iot CA Path: " + iotCAPath)
print("GGAD cert path: " + certificatePath)
print("GGAD private key path: " + privateKeyPath)
print("GGAD thing name : " + clientId)
retryCount = MAX_DISCOVERY_RETRIES
discovered = False
groupCA = None
coreInfo = None
while retryCount != 0:
try:
discoveryInfo = discoveryInfoProvider.discover(clientId)
caList = discoveryInfo.getAllCas()
coreList = discoveryInfo.getAllCores()
# In this example we have one core
# So we only pick the first ca and core info
groupId, ca = caList[0]
coreInfo = coreList[0]
print("Discovered GGC: " + coreInfo.coreThingArn + " from Group: " + groupId)
hostAddr = ""
# In this example Ip detector lambda is turned on which reports
# the GGC hostAddr to the CIS (Connectivity Information Service) that stores the
# connectivity information for the AWS Greengrass core associated with your group.
# 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
print("Discovered GGC Host Address: " + hostAddr)
print("Now we persist the connectivity/identity information...")
groupCA = GROUP_PATH + CA_NAME
ggcHostPath = GROUP_PATH + GGC_ADDR_NAME
if not os.path.exists(GROUP_PATH):
os.makedirs(GROUP_PATH)
groupCAFile = open(groupCA, "w")
groupCAFile.write(ca)
groupCAFile.close()
groupHostFile = open(ggcHostPath, "w")
groupHostFile.write(hostAddr)
groupHostFile.close()
discovered = True
print("Now proceed to the connecting flow...")
break
except DiscoveryInvalidRequestException as e:
print("Invalid discovery request detected!")
print("Type: " + str(type(e)))
print("Error message: " + e.message)
print("Stopping...")
break
except BaseException as e:
print("Error in discovery!")
print("Type: " + str(type(e)))
print("Error message: " + e.message)
retryCount -= 1
print("\n"+str(retryCount) + "/" + str(MAX_DISCOVERY_RETRIES) + " retries left\n")
print("Backing off...\n")
backOffCore.backOff()
if not discovered:
print("Discovery failed after " + str(MAX_DISCOVERY_RETRIES) + " retries. Exiting...\n")
sys.exit(-1)