Mercurial > code > home > repos > homeauto
annotate service/environment/environment.py @ 1708:3cfd3693a4ac
quick fixes for cyclone sse
author | drewp@bigasterisk.com |
---|---|
date | Wed, 17 Nov 2021 14:25:14 -0800 |
parents | 4167101b816f |
children |
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 |
1708 | 18 from patch_cyclone import patch_sse |
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 |
1708 | 20 patch_sse() |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
21 |
0 | 22 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
23 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
24 | |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
25 STATS = scales.collection('/root', |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
26 scales.PmfStat('update'), |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
27 ) |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
28 |
737 | 29 class CycloneGraphEventsHandlerWithCors(CycloneGraphEventsHandler): |
30 def flush(self): | |
1708 | 31 self.set_header(b"Access-Control-Allow-Origin", b"*") |
737 | 32 return CycloneGraphEventsHandler.flush(self) |
33 | |
34 | |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
35 @STATS.update.time() |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
36 def update(masterGraph): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
37 stmt = lambda s, p, o: masterGraph.patchObject(ROOM.environment, s, p, o) |
723 | 38 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
39 now = datetime.datetime.now(tzlocal()) |
0 | 40 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
41 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
|
42 stmt(DEV.environment, ROOM.localTimeToMinute, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
43 Literal(now.strftime("%H:%M"))) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
44 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
45 stmt(DEV.environment, ROOM.localTimeToSecond, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
46 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
|
47 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
48 stmt(DEV.environment, ROOM.localDayOfWeek, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
49 Literal(now.strftime("%A"))) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
50 stmt(DEV.environment, ROOM.localMonthDay, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
51 Literal(now.strftime("%B %e"))) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
52 stmt(DEV.environment, ROOM.localDate, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
53 Literal(now.strftime("%Y-%m-%d"))) |
71 | 54 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 stmt(DEV.calendar, ROOM.daysToLastFridayOfMonth, Literal(offset)) |
71 | 59 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
60 stmt(DEV.calendar, ROOM.twilight, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
61 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
|
62 |
723 | 63 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
64 def main(): |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
65 arg = docopt(""" |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
66 Usage: environment.py [options] |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
67 |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
68 -v Verbose |
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
69 """) |
738 | 70 verboseLogging(arg['-v']) |
71 | |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
72 masterGraph = PatchableGraph() |
160
b5d6d9a6211f
add more date strings. Accept-header support
drewp@bigasterisk.com
parents:
136
diff
changeset
|
73 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
74 class Application(cyclone.web.Application): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
75 def __init__(self): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
76 handlers = [ |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
77 (r"/()", |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
78 cyclone.web.StaticFileHandler, |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
79 {"path": ".", "default_filename": "index.html"}), |
722
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
440
diff
changeset
|
80 (r'/graph/environment', |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
81 CycloneGraphHandler, {'masterGraph': masterGraph}), |
722
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
440
diff
changeset
|
82 (r'/graph/environment/events', |
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
440
diff
changeset
|
83 CycloneGraphEventsHandlerWithCors, {'masterGraph': masterGraph}), |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
84 (r'/doc', Doc), # to be shared |
440
6304b0370491
environment graph service standardization
drewp@bigasterisk.com
parents:
224
diff
changeset
|
85 (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
|
86 ] |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
87 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
|
88 masterGraph=masterGraph) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
89 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
|
90 reactor.listenTCP(9075, Application()) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
91 reactor.run() |
0 | 92 |
93 if __name__ == '__main__': | |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
160
diff
changeset
|
94 main() |