dtype.org
Log in

DSmartHome-api Lambda handler

From dtype.org
Revision as of 21:52, 30 July 2017 by Drew (talk | contribs) (Created page with " <nowiki> import boto3 import json import time print('Loading function') dynamodb = boto3.resource('dynamodb') def respond(err, res=None): return { 'statusCode...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

import boto3
import json
import time

print('Loading function')
dynamodb = boto3.resource('dynamodb')

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')
    
    table.update_item(
        Key={
            'location': '829Seabury',
            'device': event['device']
        },
        UpdateExpression="set latest = :latest, latesttime = :latesttime",
        ExpressionAttributeValues={
            ':latest': event['value'],
            ':latesttime': int(time.time())
        },
        ReturnValues="UPDATED_NEW"
    )