comparison service/environment/environment.py @ 1243:b63c35e13e18

environment graph service standardization Ignore-this: 3aeb86c03902af984a871c9c2d2dd7cd darcs-hash:d609c91b384e6d59a76babb6666c109653e31dd8
author drewp <drewp@bigasterisk.com>
date Thu, 18 Apr 2019 09:12:26 -0700
parents 4d36cae32a4c
children a93fbf0d0daa
comparison
equal deleted inserted replaced
1242:24c004aac998 1243:b63c35e13e18
2 """ 2 """
3 return some rdf about the environment, e.g. the current time, 3 return some rdf about the environment, e.g. the current time,
4 daytime/night, overall modes like 'maintenance mode', etc 4 daytime/night, overall modes like 'maintenance mode', etc
5 5
6 """ 6 """
7 import sys, datetime, cyclone.web 7 import sys, datetime, cyclone.web, logging
8 from twisted.internet import reactor, task 8 from docopt import docopt
9 from twisted.internet import reactor, task, defer
9 from dateutil.tz import tzlocal 10 from dateutil.tz import tzlocal
10 from dateutil.relativedelta import relativedelta, FR 11 from dateutil.relativedelta import relativedelta, FR
11 from rdflib import Namespace, Literal 12 from rdflib import Namespace, Literal
12 sys.path.append("/my/proj/homeauto/lib") 13 from greplin import scales
14 from greplin.scales.cyclonehandler import StatsHandler
13 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler 15 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler
14 from twilight import isWithinTwilight 16 from twilight import isWithinTwilight
17 from logsetup import log, enableTwistedLog
15 18
16 from rdfdoc import Doc 19 from rdfdoc import Doc
17 20
18 ROOM = Namespace("http://projects.bigasterisk.com/room/") 21 ROOM = Namespace("http://projects.bigasterisk.com/room/")
19 DEV = Namespace("http://projects.bigasterisk.com/device/") 22 DEV = Namespace("http://projects.bigasterisk.com/device/")
20 23
24 STATS = scales.collection('/root',
25 scales.PmfStat('update'),
26 )
27
28 @STATS.update.time()
21 def update(masterGraph): 29 def update(masterGraph):
22 stmt = lambda s, p, o: masterGraph.patchObject(ROOM.environment, s, p, o) 30 stmt = lambda s, p, o: masterGraph.patchObject(ROOM.environment, s, p, o)
23 31
24 now = datetime.datetime.now(tzlocal()) 32 now = datetime.datetime.now(tzlocal())
25 33
45 stmt(DEV.calendar, ROOM.twilight, 53 stmt(DEV.calendar, ROOM.twilight,
46 ROOM['withinTwilight'] if isWithinTwilight(now) else ROOM['daytime']) 54 ROOM['withinTwilight'] if isWithinTwilight(now) else ROOM['daytime'])
47 55
48 56
49 def main(): 57 def main():
50 from twisted.python import log as twlog 58 arg = docopt("""
51 twlog.startLogging(sys.stderr) 59 Usage: environment.py [options]
60
61 -v Verbose
62 """)
63 log.setLevel(logging.INFO)
64 if arg['-v']:
65 enableTwistedLog()
66 log.setLevel(logging.DEBUG)
67 defer.setDebugging(True)
68
52 masterGraph = PatchableGraph() 69 masterGraph = PatchableGraph()
53 70
54 class Application(cyclone.web.Application): 71 class Application(cyclone.web.Application):
55 def __init__(self): 72 def __init__(self):
56 handlers = [ 73 handlers = [
60 (r'/graph', 77 (r'/graph',
61 CycloneGraphHandler, {'masterGraph': masterGraph}), 78 CycloneGraphHandler, {'masterGraph': masterGraph}),
62 (r'/graph/events', 79 (r'/graph/events',
63 CycloneGraphEventsHandler, {'masterGraph': masterGraph}), 80 CycloneGraphEventsHandler, {'masterGraph': masterGraph}),
64 (r'/doc', Doc), # to be shared 81 (r'/doc', Doc), # to be shared
82 (r'/stats/(.*)', StatsHandler, {'serverName': 'environment'}),
65 ] 83 ]
66 cyclone.web.Application.__init__(self, handlers, 84 cyclone.web.Application.__init__(self, handlers,
67 masterGraph=masterGraph) 85 masterGraph=masterGraph)
68 task.LoopingCall(update, masterGraph).start(1) 86 task.LoopingCall(update, masterGraph).start(1)
69 reactor.listenTCP(9075, Application()) 87 reactor.listenTCP(9075, Application())