annotate service/xidle/xidle.py @ 361:01817264cc3d

xidle to docker Ignore-this: 2c930853a8f7e7aa9b323f50c46b705d
author drewp@bigasterisk.com
date Sun, 09 Sep 2018 04:04:16 -0700
parents 105969d248d6
children f1177213fe22
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
1 #!bin/python
69
98c1cdd42bce add idleTimeMinutes to rdf graph
drewp@bigasterisk.com
parents: 53
diff changeset
2 from __future__ import division
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
3 """
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
4 X server idle time is now available over http!
361
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
5
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
6 Note: HD-4110 webcams stop X from going idle by sending events
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
7 constantly. Run this to fix:
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
8
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
9 xinput disable "HP Webcam HD-4110"
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
10 """
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
11
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
12 import time
361
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
13 import sys, socket, json, os
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
14 from rdflib import Namespace, URIRef, Literal
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
15 from influxdb import InfluxDBClient
361
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
16 import influxdb.exceptions
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
17 import cyclone.web
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
18 from twisted.internet import reactor, task
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
19
361
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
20 import actmon
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
21 # another option: http://thp.io/2007/09/x11-idle-time-and-focused-window-in.html
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
22
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
23 DEV = Namespace("http://projects.bigasterisk.com/device/")
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
24 ROOM = Namespace("http://projects.bigasterisk.com/room/")
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
25
361
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
26 sys.path.append('../../lib')
01817264cc3d xidle to docker
drewp@bigasterisk.com
parents: 292
diff changeset
27 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
28
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
29 host = socket.gethostname()
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
30 client = InfluxDBClient('bang6', 9060, 'root', 'root', 'main')
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
31
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
32 class Root(cyclone.web.RequestHandler):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
33 def get(self):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
34 xss.get_info() # fail if we can't get the display or something
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
35 self.write('''
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
36 Get the <a href="idle">X idle time</a> on %s.
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
37 <a href="graph">rdf graph</a> available.''' % host)
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
38
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
39 class Idle(cyclone.web.RequestHandler):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
40 def get(self):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
41 self.set_header('Content-type', 'application/json')
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
42 self.write(json.dumps({"idleMs" : xss.get_info().idle}))
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
43
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
44 class Graph(cyclone.web.RequestHandler):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
45 def get(self):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
46 self.set_header('Content-type', 'application/x-trig')
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
47
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
48 g = StateGraph(ctx=DEV['xidle/%s' % host])
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
49
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
50 ms = xss.get_info().idle
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
51 subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host)
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
52 g.add((subj, ROOM['idleTimeMs'], Literal(ms)))
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
53 g.add((subj, ROOM['idleTimeMinutes'], Literal(ms / 1000 / 60)))
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
54
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
55 self.write(g.asTrig())
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
56
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
57 class Poller(object):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
58 def __init__(self):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
59 self.points = []
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
60 self.lastSent = None
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
61 self.lastSentTime = 0
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
62 task.LoopingCall(self.poll).start(5)
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
63
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
64 def poll(self):
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
65 ms = xss.get_info().idle
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
66 lastMinActive = ms < 60 * 1000
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
67 now = int(time.time())
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
68 if self.lastSent != lastMinActive or now > self.lastSentTime + 3600:
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
69 self.points.append({"measurement": "presence",
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
70 "tags": {"host": host, "sensor": "xidle"},
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
71 "fields": {"value": 1 if lastMinActive else 0},
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
72 "time": now})
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
73 self.lastSent = lastMinActive
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
74 self.lastSentTime = now
69
98c1cdd42bce add idleTimeMinutes to rdf graph
drewp@bigasterisk.com
parents: 53
diff changeset
75
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
76 client.write_points(self.points, time_precision='s')
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
77 self.points = []
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
78
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
79 poller = Poller()
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
80
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
81 reactor.listenTCP(9107, cyclone.web.Application([
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
82 (r'/', Root),
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
83 (r'/idle', Idle),
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
84 (r'/graph', Graph),
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
85 ]), interface='::')
53
0141467893bf new xidle service
drewp@bigasterisk.com
parents:
diff changeset
86
292
105969d248d6 rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents: 215
diff changeset
87 reactor.run()