Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try all core ips #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions examples/TrafficLight/lightController.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -98,23 +98,19 @@ 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
# 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
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
Expand All @@ -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
Expand All @@ -133,13 +129,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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
44 changes: 27 additions & 17 deletions examples/TrafficLight/trafficLight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,23 +119,19 @@ 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
# 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
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
Expand All @@ -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
Expand All @@ -155,13 +151,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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down