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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!/usr/bin/python
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 return some rdf about the environment, e.g. the current time,
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 daytime/night, overall modes like 'maintenance mode', etc
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7 import sys, datetime, cyclone.web
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 from twisted.internet import reactor
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 from rdflib import Namespace, Literal
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
12 sys.path.append("/my/site/magma")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
13 from stategraph import StateGraph
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 sys.path.append("/my/proj/homeauto/lib")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
17
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
18 ROOM = Namespace("http://projects.bigasterisk.com/room/")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
19 DEV = Namespace("http://projects.bigasterisk.com/device/")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
20
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
21 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23 self.write('this is envgraph: <a href="graph">rdf</a>')
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
24
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
25 class GraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
26 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
27 g = StateGraph(ROOM.environment)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
28 now = datetime.datetime.now(tzlocal())
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
29
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
40
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
41 self.set_header('Content-type', 'application/x-trig')
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
42 self.write(g.asTrig())
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
43
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44 class Application(cyclone.web.Application):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
45 def __init__(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
46 handlers = [
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
47 (r"/", Index),
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
48 (r'/graph', GraphHandler),
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
49 ]
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
50 cyclone.web.Application.__init__(self, handlers)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
51
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
52 if __name__ == '__main__':
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
53 reactor.listenTCP(9075, Application())
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
54 reactor.run()