FridgeAlarm2/FridgeAlarm.py

31 lines
1016 B
Python
Raw Normal View History

2015-07-10 21:51:33 +02:00
import pymongo
import datetime
client = pymongo.MongoClient('172.16.2.15')
db = client.iot
iot = db.iot
now1 = datetime.datetime.now()
now = now1.replace(now1.year, now1.month, now1.day, now1.hour, now1.minute, now1.second, 0)
midnight = now.replace(now.year, now.month, now.day, 0,0,0,0)
seconds = (now - midnight).seconds
intervalBegin = seconds - 15 * 60
if intervalBegin > 0:
pipeline = [
{'$match': {'metadata.device': 'ModbusHub',
'metadata.day': midnight,
'metadata.seconds': {'$gt': intervalBegin, '$lt': seconds}
}},
{'$sort': {'seconds': 1}},
{'$project': {'_id': 0, 'ts': '$metadata.timestamp', 't': '$data.t2'}}
]
cursor = iot.aggregate(pipeline, useCursor=True)
for item in cursor:
print(item)
# {'$group': {'_id': 0,
# 'avgTemp': {'$avg': '$data.t2'}}}