in traffic-light-example-python/carAggregator.py [0:0]
def function_handler(event, context):
global totalTraffic
global totalGreenlights
global minCars
global maxCars
# grab the light status from the event
# Shadow JSON schema:
# { "state": { "desired": { "property":<R,G,Y> } } }
logger.info(event)
lightValue = event["current"]["state"]["reported"]["property"]
logger.info("reported light state: " + lightValue)
if lightValue == 'G':
logger.info("Green light")
# generate a random number of cars passing during this green light
cars = randint(1, 20)
# update stats
totalTraffic += cars
totalGreenlights+=1
if cars < minCars or minCars == -1:
minCars = cars
if cars > maxCars:
maxCars = cars
logger.info("Cars passed during green light: " + str(cars))
logger.info("Total Traffic: " + str(totalTraffic))
logger.info("Total Greenlights: " + str(totalGreenlights))
logger.info("Minimum Cars passing: " + str(minCars))
logger.info("Maximum Cars passing: " + str(maxCars))
# update car stats to dynamodb every 3 green lights
if totalGreenlights % 3 == 0:
global tableName
table = dynamodb.Table(tableName)
table.put_item(
Item={
'Time':str(datetime.utcnow()),
'TotalTraffic':totalTraffic,
'TotalGreenlights':totalGreenlights,
'MinCarsPassing':minCars,
'MaxCarsPassing':maxCars,
}
)
return