annotate service/dpms/dpms.py @ 888:ced063266d96

dpms: more monitor states. separate powerState and powerStateMeasured Ignore-this: d9f21d0921c3f72ab31ac2a4c1e073cf darcs-hash:20130802145731-312f9-ac0cda43abb761c4956c50c9fd13621ddab8ce20
author drewp <drewp@bigasterisk.com>
date Fri, 02 Aug 2013 07:57:31 -0700
parents d4c057c508f9
children 1e94d074f642
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
837
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 #!bin/python
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 """
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 sample supervisord block
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 [program:dpms_9095]
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 directory=/my/proj/homeauto/service/dpms
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 command=/my/proj/homeauto/service/dpms/bin/python dpms.py
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 user=drewp
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 On one box, this goes super slow when avahi daemon is running. Maybe
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 it's for an attempted dns lookup of the requesting IP address, which I
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13 wish I could switch off.
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14
888
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
15 --
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
16
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
17 may need this:
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
18 ps axf | grep /run/gdm
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
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
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
20 eval xauth add `sudo xauth -f /run/gdm/auth-for-gdm-iQoCDZ/database list :0`
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
21
837
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 """
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 from bottle import run, get, put, request, response
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 import subprocess, sys, socket
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 from rdflib import Namespace, URIRef
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 DEV = Namespace("http://projects.bigasterisk.com/device/")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 ROOM = Namespace("http://projects.bigasterisk.com/room/")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 sys.path.append("/my/site/magma")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 from stategraph import StateGraph
888
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
32 sys.path.append("../../lib")
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
33 from localdisplay import setDisplayToLocalX
837
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 def getMonitorState():
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 out = subprocess.check_output(['xset', 'q'])
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 for line in out.splitlines():
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 line = line.strip()
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 if line == 'Monitor is On':
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 response.set_header('content-type', 'text/plain')
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 return 'on'
888
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
42 elif line in ['Monitor is Off', 'Monitor is in Suspend', 'Monitor is in Standby']:
837
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 response.set_header('content-type', 'text/plain')
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44 return 'off'
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 raise NotImplementedError("no matching monitor line in xset output")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 @get("/")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48 def index():
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49 getMonitorState() # to make it fail if xset isn't working
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 return '''
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 Get and put the <a href="monitor">monitor power</a> with dpms.
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 <a href="graph">rdf graph</a> available.'''
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 @get("/monitor")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55 def monitor():
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 return getMonitorState()
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58 @put("/monitor")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 def putMonitor():
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 body = request.body.read().strip()
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 if body in ['on', 'off']:
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 subprocess.check_call(['xset', 'dpms', 'force', body])
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 response.status = 204
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64 else:
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 raise NotImplementedError("body must be 'on' or 'off'")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 return ''
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 @get("/graph")
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 def graph():
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 host = socket.gethostname()
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 g = StateGraph(ctx=DEV['dpms/%s' % host])
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 g.add((URIRef("http://bigasterisk.com/host/%s/monitor" % host),
888
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
73 ROOM['powerStateMeasured'],
837
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 ROOM[getMonitorState()]))
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 response.set_header('Content-type', 'application/x-trig')
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 return g.asTrig()
888
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
78
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
79 setDisplayToLocalX()
837
d4c057c508f9 new service over the dpms monitor on/off control
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80
888
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
81 run(host="0.0.0.0", server='gunicorn', port=9095, quiet=True)
ced063266d96 dpms: more monitor states. separate powerState and powerStateMeasured
drewp <drewp@bigasterisk.com>
parents: 837
diff changeset
82