Mercurial > code > home > repos > homeauto
annotate service/dpms/dpms_service.py @ 1581:137ba2d6d016 0.5.0
release 0.5.0
author | drewp@bigasterisk.com |
---|---|
date | Sun, 29 Aug 2021 13:36:08 -0700 |
parents | b87b6e9cedb2 |
children | d6996175ea78 |
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 """ |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
4 sample supervisord block |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
5 |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
6 [program:dpms_9095] |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
7 directory=/my/proj/homeauto/service/dpms |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
8 command=/my/proj/homeauto/service/dpms/bin/python dpms.py |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
9 user=drewp |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
10 |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
11 On one box, this goes super slow when avahi daemon is running. Maybe |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
12 it's for an attempted dns lookup of the requesting IP address, which I |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
13 wish I could switch off. |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
14 |
83
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
15 -- |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
16 |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
17 may need this: |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
18 ps axf | grep /run/gdm |
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
19 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
|
20 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
|
21 |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
22 """ |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
23 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
24 from twisted.internet import reactor, task |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
25 import cyclone.web |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
26 from influxdb import InfluxDBClient |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
27 import subprocess, sys, socket, time, os |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
28 from rdflib import Namespace, URIRef |
464 | 29 from logsetup import log, enableTwistedLog |
30 | |
31 from dpms import DPMS, DPMSModeOn | |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
32 DEV = Namespace("http://projects.bigasterisk.com/device/") |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
33 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
34 |
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
35 sys.path.append("/my/site/magma") |
464 | 36 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler |
37 #sys.path.append("../../lib") | |
38 #from localdisplay import setDisplayToLocalX | |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
39 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
40 influx = InfluxDBClient('bang6', 9060, 'root', 'root', 'main') |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
41 |
464 | 42 os.environ['DISPLAY'] = ':0.0' |
43 | |
44 dpms = DPMS() | |
45 host = socket.gethostname() | |
46 | |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
47 def getMonitorState(): |
464 | 48 level, enabled = dpms.Info() |
49 return 'on' if level == DPMSModeOn else 'off' | |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
50 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
51 class Root(cyclone.web.RequestHandler): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
52 def get(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
53 getMonitorState() # to make it fail if xset isn't working |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
54 self.write(''' |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
55 Get and put the <a href="monitor">monitor power</a> with dpms. |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
56 <a href="graph">rdf graph</a> available.''') |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
57 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
58 class Monitor(cyclone.web.RequestHandler): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
59 def get(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
60 self.set_header('content-type', 'text/plain') |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
61 self.write(getMonitorState()) |
723 | 62 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
63 def put(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
64 body = self.request.body.strip() |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
65 if body in ['on', 'off']: |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
66 subprocess.check_call(['xset', 'dpms', 'force', body]) |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
67 self.set_status(204) |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
68 else: |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
69 raise NotImplementedError("body must be 'on' or 'off'") |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
70 |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
71 class Poller(object): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
72 def __init__(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
73 self.lastSent = None |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
74 self.lastSentTime = 0 |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
75 task.LoopingCall(self.poll).start(5) |
723 | 76 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
77 def poll(self): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
78 now = int(time.time()) |
464 | 79 state = getMonitorState() |
80 | |
81 ctx=DEV['dpms/%s' % host] | |
82 masterGraph.patchObject( | |
83 ctx, | |
84 URIRef("http://bigasterisk.com/host/%s/monitor" % host), | |
85 ROOM['powerStateMeasured'], | |
86 ROOM[getMonitorState()]) | |
723 | 87 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
88 if state != self.lastSent or (now > self.lastSentTime + 3600): |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
89 influx.write_points([ |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
90 {'measurement': 'power', |
464 | 91 'tags': {'device': '%sMonitor' % host}, |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
92 'fields': {'value': 1 if state == 'on' else 0}, |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
93 'time': now |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
94 }], time_precision='s') |
723 | 95 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
96 self.lastSent = state |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
97 self.lastSentTime = now |
83
add8a36e54bd
dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents:
32
diff
changeset
|
98 |
464 | 99 masterGraph = PatchableGraph() |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
100 poller = Poller() |
723 | 101 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
102 reactor.listenTCP(9095, cyclone.web.Application([ |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
103 (r'/', Root), |
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
104 (r'/monitor', Monitor), |
722
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
464
diff
changeset
|
105 (r'/graph/dpms', CycloneGraphHandler, {'masterGraph': masterGraph}), |
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
464
diff
changeset
|
106 (r'/graph/dpms/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}), |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
107 ]), interface='::') |
32
a6d7c3a96684
new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff
changeset
|
108 |
317
629cd8c36831
rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents:
215
diff
changeset
|
109 reactor.run() |