Mercurial > code > home > repos > homeauto
annotate service/dpms/dpms_service.py @ 1694:73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
https://bigasterisk.com/post/inference/2021-09-27_11-11.png
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Sep 2021 11:22:09 -0700 |
parents | d6996175ea78 |
children |
rev | line source |
---|---|
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
1 #!bin/python |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
3 """ |
83
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
4 may need this: |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
5 ps axf | grep /run/gdm |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
6 18339 tty7 Ss+ 0:00 \_ /usr/bin/X :0 -background none -verbose -auth /run/gdm/auth-for-gdm-iQoCDZ/database -nolisten tcp vt7 |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
7 eval xauth add `sudo xauth -f /run/gdm/auth-for-gdm-iQoCDZ/database list :0` |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
8 |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
9 """ |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
10 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
11 from twisted.internet import reactor, task |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
12 import cyclone.web |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
13 from influxdb import InfluxDBClient |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
14 import subprocess, sys, socket, time, os |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
15 from rdflib import Namespace, URIRef |
1682 | 16 from standardservice.logsetup import log, verboseLogging |
17 from cycloneerr import PrettyErrorHandler | |
18 import dpms | |
464 | 19 |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
20 DEV = Namespace("http://projects.bigasterisk.com/device/") |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
21 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
464 | 23 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
24 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
25 influx = InfluxDBClient('bang6', 9060, 'root', 'root', 'main') |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
26 |
1682 | 27 host = socket.gethostname() |
464 | 28 |
1682 | 29 os.environ['DISPLAY'] = ':0.0' |
30 d = dpms.DPMS() | |
31 | |
464 | 32 |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
33 def getMonitorState(): |
1682 | 34 return 'on' if d.Info()[0] == dpms.DPMSModeOn else 'off' |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
35 |
1682 | 36 class Root(PrettyErrorHandler, cyclone.web.RequestHandler): |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
37 def get(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
38 getMonitorState() # to make it fail if xset isn't working |
1682 | 39 self.set_header('content-type', 'text/html') |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
40 self.write(''' |
1682 | 41 <!doctype html> |
42 <html> | |
43 <head> | |
44 <title>dpms</title> | |
45 <meta charset="utf-8" /> | |
46 <script src="/lib/polymer/1.0.9/webcomponentsjs/webcomponents.min.js"></script> | |
47 <script src="/lib/require/require-2.3.3.js"></script> | |
48 <script src="/rdf/common_paths_and_ns.js"></script> | |
49 | |
50 <link rel="import" href="/rdf/streamed-graph.html"> | |
51 <link rel="import" href="/lib/polymer/1.0.9/polymer/polymer.html"> | |
52 | |
53 <meta name="mobile-web-app-capable" content="yes"> | |
54 <meta name="viewport" content="width=device-width, initial-scale=1"> | |
55 </head> | |
56 <body> | |
57 <template id="t" is="dom-bind"> | |
58 | |
59 Get and put the <a href="monitor">monitor power</a> with dpms. | |
60 | |
61 <streamed-graph url="graph/dpms/events" graph="{{graph}}"></streamed-graph> | |
62 <div id="out"></div> | |
63 <script type="module" src="/rdf/streamed_graph_view.js"></script> | |
64 </template> | |
65 <style> | |
66 .served-resources { | |
67 margin-top: 4em; | |
68 border-top: 1px solid gray; | |
69 padding-top: 1em; | |
70 } | |
71 .served-resources a { | |
72 padding-right: 2em; | |
73 } | |
74 </style> | |
75 | |
76 <div class="served-resources"> | |
77 <a href="stats/">/stats/</a> | |
78 <a href="graph/dpms">/graph/dpms</a> | |
79 <a href="graph/dpms/events">/graph/dpms/events</a> | |
80 <a href="monitor">/monitor (put)</a> | |
81 </div> | |
82 | |
83 </body> | |
84 </html> | |
85 ''') | |
86 | |
87 | |
88 class Monitor(PrettyErrorHandler, cyclone.web.RequestHandler): | |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
89 def get(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
90 self.set_header('content-type', 'text/plain') |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
91 self.write(getMonitorState()) |
723 | 92 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
93 def put(self): |
1682 | 94 body = self.request.body.decode('ascii').strip() |
95 if body not in ['on', 'off']: | |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
96 raise NotImplementedError("body must be 'on' or 'off'") |
1682 | 97 d.ForceLevel(dpms.DPMSModeOff if body == 'off' else dpms.DPMSModeOn) |
98 self.set_status(204) | |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
99 |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
100 class Poller(object): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
101 def __init__(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
102 self.lastSent = None |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
103 self.lastSentTime = 0 |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
104 task.LoopingCall(self.poll).start(5) |
723 | 105 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
106 def poll(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
107 now = int(time.time()) |
464 | 108 state = getMonitorState() |
109 | |
110 ctx=DEV['dpms/%s' % host] | |
111 masterGraph.patchObject( | |
112 ctx, | |
113 URIRef("http://bigasterisk.com/host/%s/monitor" % host), | |
114 ROOM['powerStateMeasured'], | |
115 ROOM[getMonitorState()]) | |
723 | 116 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
117 if state != self.lastSent or (now > self.lastSentTime + 3600): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
118 influx.write_points([ |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
119 {'measurement': 'power', |
464 | 120 'tags': {'device': '%sMonitor' % host}, |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
121 'fields': {'value': 1 if state == 'on' else 0}, |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
122 'time': now |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
123 }], time_precision='s') |
723 | 124 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
125 self.lastSent = state |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
126 self.lastSentTime = now |
83
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
127 |
1682 | 128 verboseLogging(False) |
129 | |
464 | 130 masterGraph = PatchableGraph() |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
131 poller = Poller() |
723 | 132 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
133 reactor.listenTCP(9095, cyclone.web.Application([ |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
134 (r'/', Root), |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
135 (r'/monitor', Monitor), |
722
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
464
diff
changeset
|
136 (r'/graph/dpms', CycloneGraphHandler, {'masterGraph': masterGraph}), |
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
464
diff
changeset
|
137 (r'/graph/dpms/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}), |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
138 ]), interface='::') |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
139 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
140 reactor.run() |