dtype.org
Log in

Difference between revisions of "DSmartHome-api Lambda handler"

From dtype.org
(Created page with " <nowiki> import boto3 import json import time print('Loading function') dynamodb = boto3.resource('dynamodb') def respond(err, res=None): return { 'statusCode...")
 
 
Line 8: Line 8:
 
print('Loading function')
 
print('Loading function')
 
dynamodb = boto3.resource('dynamodb')
 
dynamodb = boto3.resource('dynamodb')
 +
location = '829Seabury'
  
 
def respond(err, res=None):
 
def respond(err, res=None):
Line 20: Line 21:
 
def lambda_handler(event, context):
 
def lambda_handler(event, context):
 
     table = dynamodb.Table('dsmarthome')
 
     table = dynamodb.Table('dsmarthome')
 +
   
 +
    currenttime = int(time.time())
 +
   
 +
    # see if state has changed
 +
    response = table.get_item(
 +
        Key={
 +
            'location': location,
 +
            'device': event['device']
 +
        }
 +
    )
 +
    item = response['Item']
 +
    if (item['latest'] != event['value']):
 +
            # state has changed, write previous state
 +
            table.update_item(
 +
                Key={
 +
                    'location': location,
 +
                    'device': event['device']
 +
                },
 +
                UpdateExpression="set previous = :previous, changetime = :changetime",
 +
                ExpressionAttributeValues={
 +
                    ':previous': item['latest'],
 +
                    ':changetime': currenttime
 +
                },
 +
                ReturnValues="UPDATED_NEW"
 +
            )
 
      
 
      
 
     table.update_item(
 
     table.update_item(
 
         Key={
 
         Key={
             'location': '829Seabury',
+
             'location': location,
 
             'device': event['device']
 
             'device': event['device']
 
         },
 
         },
Line 29: Line 55:
 
         ExpressionAttributeValues={
 
         ExpressionAttributeValues={
 
             ':latest': event['value'],
 
             ':latest': event['value'],
             ':latesttime': int(time.time())
+
             ':latesttime': currenttime
 
         },
 
         },
 
         ReturnValues="UPDATED_NEW"
 
         ReturnValues="UPDATED_NEW"
 
     )
 
     )
 
 
</nowiki>
 
</nowiki>

Latest revision as of 22:28, 30 July 2017


import boto3
import json
import time

print('Loading function')
dynamodb = boto3.resource('dynamodb')
location = '829Seabury'

def respond(err, res=None):
    return {
        'statusCode': '400' if err else '200',
        'body': err.message if err else json.dumps(res),
        'headers': {
            'Content-Type': 'application/json',
        },
    }

def lambda_handler(event, context):
    table = dynamodb.Table('dsmarthome')
    
    currenttime = int(time.time())
    
    # see if state has changed
    response = table.get_item(
        Key={
            'location': location,
            'device': event['device']
        }
    )
    item = response['Item']
    if (item['latest'] != event['value']):
            # state has changed, write previous state
            table.update_item(
                Key={
                    'location': location,
                    'device': event['device']
                },
                UpdateExpression="set previous = :previous, changetime = :changetime",
                ExpressionAttributeValues={
                    ':previous': item['latest'],
                    ':changetime': currenttime
                },
                ReturnValues="UPDATED_NEW"
            )
    
    table.update_item(
        Key={
            'location': location,
            'device': event['device']
        },
        UpdateExpression="set latest = :latest, latesttime = :latesttime",
        ExpressionAttributeValues={
            ':latest': event['value'],
            ':latesttime': currenttime
        },
        ReturnValues="UPDATED_NEW"
    )