【发布时间】:2020-11-20 21:13:32
【问题描述】:
我的 dynamodb 表将时间戳(在 YYYY-MM-DD HH:MN:SS 中)作为 PrimaryKey 列,将温度作为排序键,而在数据 {“湿度”:42 ,“位置”:“房间”,“温度”: “恒温器”:}
在 boto3 python 中,我需要根据时间戳(现在和 15 分钟前)进行扫描,如果差异(温度 - 恒温器)> 5 超过 10 次,则返回 thermostat-5 并且如果(温度 - 恒温器)
import boto3
import math
import json
import time
import dateutil.tz
from datetime import datetime,timedelta
from dateutil import tz
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= "TableDynamo"
Primary_Column_Name = 'timestamp'
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
south = dateutil.tz.gettz('Asia/Kolkata')
now = datetime.now(tz=south)
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":'+ str(thermonew) + '}'
print(tosensor)
#response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
return
elif response['Count'] < 10:
print('{"thermostat":' + str(thermostatVal) + '}')
return
【问题讨论】:
-
请展示你的作品
-
def lambda_handler(event, context): Fifth_min_ago = now - timedelta(seconds=900) now = now.strftime('%F %T') Fifth_min_ago = Fifth_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: thermonew = thermostatVal + 5 tosensor = '{"thermostat":'+ str( thermonew) + '}' print(tosensor) return elif response['Count']
标签: python amazon-dynamodb boto3