annotate service/dpms/dpms.py @ 83:add8a36e54bd

dpms: more monitor states. separate powerState and powerStateMeasured Ignore-this: d9f21d0921c3f72ab31ac2a4c1e073cf
author drewp@bigasterisk.com
date Fri, 02 Aug 2013 07:57:31 -0700
parents a6d7c3a96684
children 1e94d074f642
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
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
24 from bottle import run, get, put, request, response
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
25 import subprocess, sys, socket
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
26 from rdflib import Namespace, URIRef
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
27 DEV = Namespace("http://projects.bigasterisk.com/device/")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
28 ROOM = Namespace("http://projects.bigasterisk.com/room/")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
29
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
30 sys.path.append("/my/site/magma")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
31 from stategraph import StateGraph
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
32 sys.path.append("../../lib")
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
33 from localdisplay import setDisplayToLocalX
32
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 def getMonitorState():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
36 out = subprocess.check_output(['xset', 'q'])
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
37 for line in out.splitlines():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
38 line = line.strip()
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
39 if line == 'Monitor is On':
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
40 response.set_header('content-type', 'text/plain')
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
41 return 'on'
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
42 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
43 response.set_header('content-type', 'text/plain')
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
44 return 'off'
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
45 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
46
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
47 @get("/")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
48 def index():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
49 getMonitorState() # to make it fail if xset isn't working
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
50 return '''
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
51 Get and put the <a href="monitor">monitor power</a> with dpms.
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
52 <a href="graph">rdf graph</a> available.'''
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
53
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
54 @get("/monitor")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
55 def monitor():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
56 return getMonitorState()
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
57
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
58 @put("/monitor")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
59 def putMonitor():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
60 body = request.body.read().strip()
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
61 if body in ['on', 'off']:
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
62 subprocess.check_call(['xset', 'dpms', 'force', body])
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
63 response.status = 204
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
64 else:
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
65 raise NotImplementedError("body must be 'on' or 'off'")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
66 return ''
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
67
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
68 @get("/graph")
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
69 def graph():
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
70 host = socket.gethostname()
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
71 g = StateGraph(ctx=DEV['dpms/%s' % host])
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
72 g.add((URIRef("http://bigasterisk.com/host/%s/monitor" % host),
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
73 ROOM['powerStateMeasured'],
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
74 ROOM[getMonitorState()]))
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
75
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
76 response.set_header('Content-type', 'application/x-trig')
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
77 return g.asTrig()
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
78
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
79 setDisplayToLocalX()
32
a6d7c3a96684 new service over the dpms monitor on/off control
drewp@bigasterisk.com
parents:
diff changeset
80
83
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
81 run(host="0.0.0.0", server='gunicorn', port=9095, quiet=True)
add8a36e54bd dpms: more monitor states. separate powerState and powerStateMeasured
drewp@bigasterisk.com
parents: 32
diff changeset
82