changeset 489:a0afc44cec32

old event notifier tool. this should be done in reasoning/ now. Ignore-this: 8e4b6aa1dbffb0ec5d4e6239059c74af
author drewp@bigasterisk.com
date Sun, 21 Apr 2019 01:28:47 -0700
parents 6c5bc38e4676
children 8443e6b6e91e
files service/frontdooropen.py
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/frontdooropen.py	Sun Apr 21 01:28:47 2019 -0700
@@ -0,0 +1,27 @@
+#!/my/proj/homeauto/service/frontDoorArduino/bin/python
+from pymongo import Connection
+from dateutil.tz import tzlocal
+import restkit, time, datetime
+from web.utils import datestr
+
+c3po = restkit.Resource('http://bang:9040/')
+sensor = Connection("bang", 27017, tz_aware=True)['house']['sensor']
+
+lastSent = None
+
+while True:
+    q = {"name":"frontDoor", "state":"open"}
+    if lastSent is not None:
+        q['t'] = {"$gt":lastSent}
+
+    for row in sensor.find(q).sort([('t',-1)]).limit(1):
+        t = row['t'].astimezone(tzlocal())
+        msg = "front door opened at %s" % t.replace(microsecond=0).time().isoformat()
+        if lastSent is not None:
+            msg += " (previous was %s)" % datestr(lastSent, datetime.datetime.now(tzlocal()))
+        for u in ["http://bigasterisk.com/foaf.rdf#drewp", "http://bigasterisk.com/kelsi/foaf.rdf#kelsi"]:
+            c3po.post(path='', payload=dict(msg=msg, user=u, mode='email'))
+        
+        lastSent = row['t']
+        
+    time.sleep(1)