53
|
1 #!bin/python
|
69
|
2 from __future__ import division
|
53
|
3 """
|
|
4 X server idle time is now available over http!
|
|
5 """
|
|
6
|
|
7 from bottle import run, get, put, request, response
|
|
8 import subprocess, sys, socket
|
|
9 from rdflib import Namespace, URIRef, Literal
|
|
10
|
|
11 # from http://bebop.bigasterisk.com/python/
|
|
12 import xss
|
|
13 # another option: http://thp.io/2007/09/x11-idle-time-and-focused-window-in.html
|
|
14
|
|
15 DEV = Namespace("http://projects.bigasterisk.com/device/")
|
|
16 ROOM = Namespace("http://projects.bigasterisk.com/room/")
|
|
17
|
|
18 sys.path.append("/my/site/magma")
|
|
19 from stategraph import StateGraph
|
|
20
|
|
21 host = socket.gethostname()
|
|
22
|
|
23 @get("/")
|
|
24 def index():
|
|
25 xss.get_info() # fail if we can't get the display or something
|
|
26 return '''
|
|
27 Get the <a href="idle">X idle time</a> on %s.
|
|
28 <a href="graph">rdf graph</a> available.''' % host
|
|
29
|
|
30 @get("/idle")
|
|
31 def monitor():
|
|
32 return {"idleMs" : xss.get_info().idle}
|
|
33
|
|
34 @get("/graph")
|
|
35 def graph():
|
|
36 g = StateGraph(ctx=DEV['xidle/%s' % host])
|
69
|
37
|
|
38 ms = xss.get_info().idle
|
|
39 subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host)
|
|
40 g.add((subj, ROOM['idleTimeMs'], Literal(ms)))
|
|
41 g.add((subj, ROOM['idleTimeMinutes'], Literal(ms / 1000 / 60)))
|
53
|
42
|
|
43 response.set_header('Content-type', 'application/x-trig')
|
|
44 return g.asTrig()
|
|
45
|
|
46 run(host="0.0.0.0", server='gunicorn', port=9107, quiet=True)
|
|
47
|