Mercurial > code > home > repos > homeauto
annotate service/environment/environment.py @ 136:2200d6530a5d
testing rdfs:comment display on enironment's graph viewer
Ignore-this: d25c57b80a1dc4180b348d64f1546cd6
author | drewp@bigasterisk.com |
---|---|
date | Sun, 13 Oct 2013 12:03:26 -0700 |
parents | 86345d1d8514 |
children | b5d6d9a6211f |
rev | line source |
---|---|
0 | 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 | |
71 | 10 from dateutil.relativedelta import relativedelta, FR |
0 | 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 | |
71 | 16 from twilight import isWithinTwilight |
0 | 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))) | |
134
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
27 g.add((DEV.environment, ROOM.localTimeToMinute, |
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
28 Literal(now.strftime("%H:%M")))) |
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
29 g.add((DEV.environment, ROOM.localTimeToSecond, |
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
30 Literal(now.strftime("%H:%M:%S")))) |
71 | 31 |
32 for offset in range(-12, 7): | |
33 d = now.date() + datetime.timedelta(days=offset) | |
34 if d == d + relativedelta(day=31, weekday=FR(-1)): | |
35 g.add((DEV.calendar, ROOM.daysToLastFridayOfMonth, | |
36 Literal(offset))) | |
37 | |
38 g.add((DEV.calendar, ROOM.twilight, | |
134
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
39 ROOM['withinTwilight'] if isWithinTwilight(now) else |
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
40 ROOM['daytime'])) |
0 | 41 |
42 self.set_header('Content-type', 'application/x-trig') | |
43 self.write(g.asTrig()) | |
136
2200d6530a5d
testing rdfs:comment display on enironment's graph viewer
drewp@bigasterisk.com
parents:
134
diff
changeset
|
44 |
2200d6530a5d
testing rdfs:comment display on enironment's graph viewer
drewp@bigasterisk.com
parents:
134
diff
changeset
|
45 from rdfdoc import Doc |
0 | 46 |
47 class Application(cyclone.web.Application): | |
48 def __init__(self): | |
49 handlers = [ | |
134
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
50 (r"/()", cyclone.web.StaticFileHandler, |
86345d1d8514
more demo statements for use in examples
drewp@bigasterisk.com
parents:
71
diff
changeset
|
51 {"path": ".", "default_filename": "index.html"}), |
0 | 52 (r'/graph', GraphHandler), |
136
2200d6530a5d
testing rdfs:comment display on enironment's graph viewer
drewp@bigasterisk.com
parents:
134
diff
changeset
|
53 (r'/doc', Doc), # to be shared |
0 | 54 ] |
55 cyclone.web.Application.__init__(self, handlers) | |
56 | |
57 if __name__ == '__main__': | |
58 reactor.listenTCP(9075, Application()) | |
59 reactor.run() |