Mercurial > code > home > repos > homeauto
view 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 |
line wrap: on
line source
#!/usr/bin/python """ return some rdf about the environment, e.g. the current time, daytime/night, overall modes like 'maintenance mode', etc """ import sys, datetime, cyclone.web from twisted.internet import reactor from dateutil.tz import tzlocal from dateutil.relativedelta import relativedelta, FR from rdflib import Namespace, Literal sys.path.append("/my/site/magma") from stategraph import StateGraph sys.path.append("/my/proj/homeauto/lib") from cycloneerr import PrettyErrorHandler from twilight import isWithinTwilight ROOM = Namespace("http://projects.bigasterisk.com/room/") DEV = Namespace("http://projects.bigasterisk.com/device/") class Index(PrettyErrorHandler, cyclone.web.RequestHandler): def get(self): self.write('this is envgraph: <a href="graph">rdf</a>') class GraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler): def get(self): g = StateGraph(ROOM.environment) now = datetime.datetime.now(tzlocal()) g.add((DEV.environment, ROOM.localHour, Literal(now.hour))) for offset in range(-12, 7): d = now.date() + datetime.timedelta(days=offset) if d == d + relativedelta(day=31, weekday=FR(-1)): g.add((DEV.calendar, ROOM.daysToLastFridayOfMonth, Literal(offset))) g.add((DEV.calendar, ROOM.twilight, ROOM['withinTwilight'] if isWithinTwilight(now) else ROOM['daytime'])) self.set_header('Content-type', 'application/x-trig') self.write(g.asTrig()) class Application(cyclone.web.Application): def __init__(self): handlers = [ (r"/", Index), (r'/graph', GraphHandler), ] cyclone.web.Application.__init__(self, handlers) if __name__ == '__main__': reactor.listenTCP(9075, Application()) reactor.run()