【发布时间】:2020-09-26 10:25:58
【问题描述】:
在我的 python lambda 代码中,基于主键作为“deviceId”和排序键时间戳(采用 YYYY-MM-DD HH:MN:SS 格式)扫描 dynamodb,我需要扫描每一个最后 15 分钟的数据(从现在的时间)。我在孟买地区 (ap-south-1),如何在以下 lambda 代码中设置本地时区。因为它正在选择默认的UTC日期时间。理想情况下,lambda必须扫描&如果返回计数> = 10,则恒温器+5。
import boto3
import math
import json
import time
from datetime import datetime,timedelta
from dateutil.tz import tzlocal
from boto3.dynamodb.conditions import Key, Attr
client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
#table_name= "thermostat_dynamo"
table_name= "newsensor"
Primary_Column_Name = 'deviceId'
table = dynamodb.Table(table_name)
#key_param = "thermostat"
#thermostatVal = table.get_item(Key={key_param:event[key_param]}) ## get record from dynamodb for this sensor
thermostatVal= 77
#now = datetime.now()
now = datetime.now(tzlocal())
fifteen_min_ago = now - timedelta(seconds=900)
now = now.strftime('%F %T')
fifteen_min_ago = fifteen_min_ago.strftime('%F %T')
fe = Key('timeStamp').between(fifteen_min_ago,now);
response = table.scan(FilterExpression=fe & Attr('temperature').lt(thermostatVal))
if response['Count'] == 10:
#return thermostatVal+5
thermonew = thermostatVal + 5
tosensor = '{"thermostat":'+'"%s"}' %thermonew
print(tosensor)
#response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
return
elif response['Count'] < 10:
#tosensor = '{"thermostat":'+'"%s"}' %thermostatVal
print('{"thermostat":'+'"%s"}' %thermostatVal)
#response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
return
【问题讨论】:
标签: python amazon-web-services aws-lambda