Mercurial > code > home > repos > homeauto
annotate service/environment/environment.py @ 939:f2a2d0cc22b4
more demo statements for use in examples
Ignore-this: e0e940387c56a720293a4727b02397b4
darcs-hash:20131013072331-312f9-ef155ca0296f4dc77bf558d6ad77fd32811c93a6
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 13 Oct 2013 00:23:31 -0700 |
parents | 351292938d7c |
children | 2200d6530a5d |
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 GraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler): | |
22 def get(self): | |
23 g = StateGraph(ROOM.environment) | |
24 now = datetime.datetime.now(tzlocal()) | |
25 | |
26 g.add((DEV.environment, ROOM.localHour, Literal(now.hour))) | |
939
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
27 g.add((DEV.environment, ROOM.localTimeToMinute, |
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
28 Literal(now.strftime("%H:%M")))) |
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
29 g.add((DEV.environment, ROOM.localTimeToSecond, |
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
30 Literal(now.strftime("%H:%M:%S")))) |
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, |
939
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
39 ROOM['withinTwilight'] if isWithinTwilight(now) else |
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
40 ROOM['daytime'])) |
805 | 41 |
42 self.set_header('Content-type', 'application/x-trig') | |
43 self.write(g.asTrig()) | |
44 | |
45 class Application(cyclone.web.Application): | |
46 def __init__(self): | |
47 handlers = [ | |
939
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
48 (r"/()", cyclone.web.StaticFileHandler, |
f2a2d0cc22b4
more demo statements for use in examples
drewp <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
49 {"path": ".", "default_filename": "index.html"}), |
805 | 50 (r'/graph', GraphHandler), |
51 ] | |
52 cyclone.web.Application.__init__(self, handlers) | |
53 | |
54 if __name__ == '__main__': | |
55 reactor.listenTCP(9075, Application()) | |
56 reactor.run() |