annotate service/dpms/dpms_service.py @ 410:a60155ded95f

reindent Ignore-this: 4d6ed319001e7781a67e8a820aa8972e
author drewp@bigasterisk.com
date Tue, 12 Mar 2019 00:34:00 -0700
parents 6c5303b85948
children 1007ca70c34d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
29 DEV = Namespace("http://projects.bigasterisk.com/device/")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
30 ROOM = Namespace("http://projects.bigasterisk.com/room/")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
31
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
32 sys.path.append("/my/site/magma")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
33 from stategraph import StateGraph
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
34 sys.path.append("../../lib")
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
35 from localdisplay import setDisplayToLocalX
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
36
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
37 influx = InfluxDBClient('bang6', 9060, 'root', 'root', 'main')
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
38
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
39 def getMonitorState():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
40 out = subprocess.check_output(['xset', 'q'])
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
41 for line in out.splitlines():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
42 line = line.strip()
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
43 if line == 'Monitor is On':
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
44 return 'on'
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
45 elif line in ['Monitor is Off', 'Monitor is in Suspend', 'Monitor is in Standby']:
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
46 return 'off'
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
47 raise NotImplementedError("no matching monitor line in xset output")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
48
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
49 class Root(cyclone.web.RequestHandler):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
50 def get(self):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
51 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
52 self.write('''
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
53 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
54 <a href="graph">rdf graph</a> available.''')
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
55
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
56 class Monitor(cyclone.web.RequestHandler):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
57 def get(self):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
58 self.set_header('content-type', 'text/plain')
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
59 self.write(getMonitorState())
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
60
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
61 def put(self):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
62 body = self.request.body.strip()
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
63 if body in ['on', 'off']:
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
64 subprocess.check_call(['xset', 'dpms', 'force', body])
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
65 self.set_status(204)
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
66 else:
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
67 raise NotImplementedError("body must be 'on' or 'off'")
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
68
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
69
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
70 class Graph(cyclone.web.RequestHandler):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
71 def get(self):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
72 host = socket.gethostname()
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
73 g = StateGraph(ctx=DEV['dpms/%s' % host])
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
74 g.add((URIRef("http://bigasterisk.com/host/%s/monitor" % host),
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
75 ROOM['powerStateMeasured'],
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
76 ROOM[getMonitorState()]))
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
77
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
78 self.set_header('Content-type', 'application/x-trig')
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
79 self.write(g.asTrig())
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
80
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
81
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
82 class Poller(object):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
83 def __init__(self):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
84 self.lastSent = None
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
85 self.lastSentTime = 0
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
86 task.LoopingCall(self.poll).start(5)
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
87
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
88 def poll(self):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
89 now = int(time.time())
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
90 try:
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
91 state = getMonitorState()
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
92 except subprocess.CalledProcessError, e:
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
93 print repr(e)
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
94 os.abort()
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
95 if state != self.lastSent or (now > self.lastSentTime + 3600):
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
96 influx.write_points([
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
97 {'measurement': 'power',
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
98 'tags': {'device': '%sMonitor' % socket.gethostname()},
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
99 'fields': {'value': 1 if state == 'on' else 0},
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
100 'time': now
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
101 }], time_precision='s')
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
102
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
103 self.lastSent = state
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
104 self.lastSentTime = now
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
105
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
106 setDisplayToLocalX()
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
107 poller = Poller()
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
108
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
109 reactor.listenTCP(9095, cyclone.web.Application([
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
110 (r'/', Root),
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
111 (r'/monitor', Monitor),
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
112 (r'/graph', Graph),
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
113 ]), interface='::')
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
114
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
115 reactor.run()
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
116
317
629cd8c36831 rewrite to cyclone. sends data to influx
drewp@bigasterisk.com
parents: 215
diff changeset
117