view service/environment/environment.py @ 134:86345d1d8514

more demo statements for use in examples Ignore-this: e0e940387c56a720293a4727b02397b4
author drewp@bigasterisk.com
date Sun, 13 Oct 2013 00:23:31 -0700
parents 5c9927555df6
children 2200d6530a5d
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 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)))
        g.add((DEV.environment, ROOM.localTimeToMinute,
               Literal(now.strftime("%H:%M"))))
        g.add((DEV.environment, ROOM.localTimeToSecond,
               Literal(now.strftime("%H:%M:%S"))))

        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"/()", cyclone.web.StaticFileHandler,
             {"path": ".", "default_filename": "index.html"}),
            (r'/graph', GraphHandler),
        ]
        cyclone.web.Application.__init__(self, handlers)

if __name__ == '__main__':
    reactor.listenTCP(9075, Application())
    reactor.run()