Mercurial > code > home > repos > homeauto
annotate service/environment/environment.py @ 876:351292938d7c
twilight computation for rules to use
Ignore-this: c40016acb3c3f957cb17d2b918698c77
darcs-hash:20130411043221-312f9-7af02fd32d1df6a01f151753150d2e4ff87a59c0
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Wed, 10 Apr 2013 21:32:21 -0700 |
parents | 9e99114dde57 |
children | 86345d1d8514 |
rev | line source |
---|---|
805 | 1 #!/usr/bin/python |
2 """ | |
3 return some rdf about the environment, e.g. the current time, | |
4 daytime/night, overall modes like 'maintenance mode', etc | |
5 | |
6 """ | |
7 import sys, datetime, cyclone.web | |
8 from twisted.internet import reactor | |
9 from dateutil.tz import tzlocal | |
876
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
10 from dateutil.relativedelta import relativedelta, FR |
805 | 11 from rdflib import Namespace, Literal |
12 sys.path.append("/my/site/magma") | |
13 from stategraph import StateGraph | |
14 sys.path.append("/my/proj/homeauto/lib") | |
15 from cycloneerr import PrettyErrorHandler | |
876
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
16 from twilight import isWithinTwilight |
805 | 17 |
18 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
19 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
20 | |
21 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | |
22 def get(self): | |
23 self.write('this is envgraph: <a href="graph">rdf</a>') | |
24 | |
25 class GraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler): | |
26 def get(self): | |
27 g = StateGraph(ROOM.environment) | |
28 now = datetime.datetime.now(tzlocal()) | |
29 | |
30 g.add((DEV.environment, ROOM.localHour, Literal(now.hour))) | |
876
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
31 |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
32 for offset in range(-12, 7): |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
33 d = now.date() + datetime.timedelta(days=offset) |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
34 if d == d + relativedelta(day=31, weekday=FR(-1)): |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
35 g.add((DEV.calendar, ROOM.daysToLastFridayOfMonth, |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
36 Literal(offset))) |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
37 |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
38 g.add((DEV.calendar, ROOM.twilight, |
351292938d7c
twilight computation for rules to use
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
39 ROOM['withinTwilight'] if isWithinTwilight(now) else ROOM['daytime'])) |
805 | 40 |
41 self.set_header('Content-type', 'application/x-trig') | |
42 self.write(g.asTrig()) | |
43 | |
44 class Application(cyclone.web.Application): | |
45 def __init__(self): | |
46 handlers = [ | |
47 (r"/", Index), | |
48 (r'/graph', GraphHandler), | |
49 ] | |
50 cyclone.web.Application.__init__(self, handlers) | |
51 | |
52 if __name__ == '__main__': | |
53 reactor.listenTCP(9075, Application()) | |
54 reactor.run() |