Mercurial > code > home > repos > homeauto
annotate service/environment/environment.py @ 738:4167101b816f
build update, use logging lib
Ignore-this: 4e8ca41fbac2056010d9b6d96d3cdfc7
author | drewp@bigasterisk.com |
---|---|
date | Mon, 10 Feb 2020 00:03:38 -0800 |
parents | 23b972df1856 |
children | 3cfd3693a4ac |
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 """ | |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
7 import sys, datetime, cyclone.web, logging |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
8 from docopt import docopt |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
9 from twisted.internet import reactor, task, defer |
0 | 10 from dateutil.tz import tzlocal |
71 | 11 from dateutil.relativedelta import relativedelta, FR |
0 | 12 from rdflib import Namespace, Literal |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
13 from greplin import scales |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
14 from greplin.scales.cyclonehandler import StatsHandler |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
15 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler |
71 | 16 from twilight import isWithinTwilight |
737 | 17 from standardservice.logsetup import log, verboseLogging |
0 | 18 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
19 from rdfdoc import Doc |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
20 |
0 | 21 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
22 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
23 | |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
24 STATS = scales.collection('/root', |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
25 scales.PmfStat('update'), |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
26 ) |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
27 |
737 | 28 class CycloneGraphEventsHandlerWithCors(CycloneGraphEventsHandler): |
29 def flush(self): | |
30 self.set_header("Access-Control-Allow-Origin", "*") | |
31 return CycloneGraphEventsHandler.flush(self) | |
32 | |
33 | |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
34 @STATS.update.time() |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
35 def update(masterGraph): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
36 stmt = lambda s, p, o: masterGraph.patchObject(ROOM.environment, s, p, o) |
723 | 37 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
38 now = datetime.datetime.now(tzlocal()) |
0 | 39 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
40 stmt(DEV.environment, ROOM.localHour, Literal(now.hour)) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
41 stmt(DEV.environment, ROOM.localTimeToMinute, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
42 Literal(now.strftime("%H:%M"))) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
43 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
44 stmt(DEV.environment, ROOM.localTimeToSecond, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
45 Literal(now.strftime("%H:%M:%S"))) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
46 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
47 stmt(DEV.environment, ROOM.localDayOfWeek, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
48 Literal(now.strftime("%A"))) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
49 stmt(DEV.environment, ROOM.localMonthDay, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
50 Literal(now.strftime("%B %e"))) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
51 stmt(DEV.environment, ROOM.localDate, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
52 Literal(now.strftime("%Y-%m-%d"))) |
71 | 53 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
54 for offset in range(-12, 7): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
55 d = now.date() + datetime.timedelta(days=offset) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
56 if d == d + relativedelta(day=31, weekday=FR(-1)): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
57 stmt(DEV.calendar, ROOM.daysToLastFridayOfMonth, Literal(offset)) |
71 | 58 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
59 stmt(DEV.calendar, ROOM.twilight, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
60 ROOM['withinTwilight'] if isWithinTwilight(now) else ROOM['daytime']) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
61 |
723 | 62 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
63 def main(): |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
64 arg = docopt(""" |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
65 Usage: environment.py [options] |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
66 |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
67 -v Verbose |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
68 """) |
738 | 69 verboseLogging(arg['-v']) |
70 | |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
71 masterGraph = PatchableGraph() |
160
b5d6d9a6211f
add more date strings. Accept-header support
drewp@bigasterisk.com
parents:
136
diff
changeset
|
72 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
73 class Application(cyclone.web.Application): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
74 def __init__(self): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
75 handlers = [ |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
76 (r"/()", |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
77 cyclone.web.StaticFileHandler, |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
78 {"path": ".", "default_filename": "index.html"}), |
722
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
440
diff
changeset
|
79 (r'/graph/environment', |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
80 CycloneGraphHandler, {'masterGraph': masterGraph}), |
722
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
440
diff
changeset
|
81 (r'/graph/environment/events', |
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
440
diff
changeset
|
82 CycloneGraphEventsHandlerWithCors, {'masterGraph': masterGraph}), |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
83 (r'/doc', Doc), # to be shared |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
84 (r'/stats/(.*)', StatsHandler, {'serverName': 'environment'}), |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
85 ] |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
86 cyclone.web.Application.__init__(self, handlers, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
87 masterGraph=masterGraph) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
88 task.LoopingCall(update, masterGraph).start(1) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
89 reactor.listenTCP(9075, Application()) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
90 reactor.run() |
0 | 91 |
92 if __name__ == '__main__': | |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
93 main() |