Mercurial > code > home > repos > homeauto
annotate service/xidle/xidle.py @ 690:3df357924f1d
xidle rewrite for docker, py3
Ignore-this: a866446f3f42069092dc40b48f12d46b
author | drewp@bigasterisk.com |
---|---|
date | Wed, 29 Jan 2020 01:02:06 -0800 |
parents | f1177213fe22 |
children | 2500a3ee9102 |
rev | line source |
---|---|
53 | 1 """ |
2 X server idle time is now available over http! | |
361 | 3 |
4 Note: HD-4110 webcams stop X from going idle by sending events | |
5 constantly. Run this to fix: | |
6 | |
7 xinput disable "HP Webcam HD-4110" | |
53 | 8 """ |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
9 import time |
690 | 10 import socket, json, os |
53 | 11 from rdflib import Namespace, URIRef, Literal |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
12 from influxdb import InfluxDBClient |
361 | 13 import influxdb.exceptions |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
14 import cyclone.web |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
15 from twisted.internet import reactor, task |
690 | 16 from standardservice.logsetup import log, verboseLogging |
17 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler | |
53 | 18 |
19 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
20 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
21 | |
22 host = socket.gethostname() | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
23 client = InfluxDBClient('bang6', 9060, 'root', 'root', 'main') |
53 | 24 |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
25 os.environ['DISPLAY'] = ':0.0' |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
26 |
690 | 27 import pxss |
28 # another option: http://thp.io/2007/09/x11-idle-time-and-focused-window-in.html | |
29 | |
30 def get_idle_time(): | |
31 return pxss.get_info().idle | |
32 | |
33 get_idle_time() # fail if we can't get the display or something | |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
34 |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
35 class Root(cyclone.web.RequestHandler): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
36 def get(self): |
690 | 37 get_idle_time() # fail if we can't get the display or something |
38 self.set_header('content-type', 'text/html') | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
39 self.write(''' |
690 | 40 <!doctype html> |
41 <html> | |
42 <head> | |
43 <title>xidle</title> | |
44 <meta charset="utf-8" /> | |
45 <script src="/lib/polymer/1.0.9/webcomponentsjs/webcomponents.min.js"></script> | |
46 <script src="/lib/require/require-2.3.3.js"></script> | |
47 <script src="/rdf/common_paths_and_ns.js"></script> | |
48 | |
49 <link rel="import" href="/rdf/streamed-graph.html"> | |
50 <link rel="import" href="/lib/polymer/1.0.9/polymer/polymer.html"> | |
51 | |
52 <meta name="mobile-web-app-capable" content="yes"> | |
53 <meta name="viewport" content="width=device-width, initial-scale=1"> | |
54 </head> | |
55 <body> | |
56 <template id="t" is="dom-bind"> | |
57 | |
53 | 58 Get the <a href="idle">X idle time</a> on %s. |
690 | 59 |
60 <streamed-graph url="graph/xidle/events" graph="{{graph}}"></streamed-graph> | |
61 <div id="out"></div> | |
62 <script type="module" src="/rdf/streamed_graph_view.js"></script> | |
63 </template> | |
64 <style> | |
65 .served-resources { | |
66 margin-top: 4em; | |
67 border-top: 1px solid gray; | |
68 padding-top: 1em; | |
69 } | |
70 .served-resources a { | |
71 padding-right: 2em; | |
72 } | |
73 </style> | |
74 | |
75 <div class="served-resources"> | |
76 <a href="stats/">/stats/</a> | |
77 <a href="graph/dpms">/graph/dpms</a> | |
78 <a href="graph/dpms/events">/graph/dpms/events</a> | |
79 <a href="idle">/idle</a> | |
80 </div> | |
81 | |
82 </body> | |
83 </html> | |
84 ''' % host) | |
85 | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
86 class Idle(cyclone.web.RequestHandler): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
87 def get(self): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
88 self.set_header('Content-type', 'application/json') |
690 | 89 self.write(json.dumps({"idleMs" : get_idle_time()})) |
90 | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
91 class Poller(object): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
92 def __init__(self): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
93 self.points = [] |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
94 self.lastSent = None |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
95 self.lastSentTime = 0 |
690 | 96 self.lastGraphSent = None |
97 self.lastGraphSentTime = 0 | |
98 task.LoopingCall(self.poll).start(1) | |
99 | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
100 def poll(self): |
690 | 101 ms = get_idle_time() |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
102 ctx = DEV['xidle/%s' % host] |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
103 subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host) |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
104 lastMinActive = ms < 60 * 1000 |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
105 now = int(time.time()) |
690 | 106 |
107 nextGraphUpdate = self.lastGraphSentTime + min(10, ms / 1000 / 2) | |
108 if self.lastGraphSent != lastMinActive or now > nextGraphUpdate: | |
109 masterGraph.patchObject(ctx, subj, ROOM['idleTimeMs'], Literal(ms)) | |
110 masterGraph.patchObject(ctx, subj, ROOM['idleTimeMinutes'], | |
111 Literal(round(ms / 1000 / 60, 2))) | |
112 self.lastGraphSent = lastMinActive | |
113 self.lastGraphSentTime = now | |
114 | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
115 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
|
116 self.points.append({"measurement": "presence", |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
117 "tags": {"host": host, "sensor": "xidle"}, |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
118 "fields": {"value": 1 if lastMinActive else 0}, |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
119 "time": now}) |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
120 self.lastSent = lastMinActive |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
121 self.lastSentTime = now |
69 | 122 |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
123 try: |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
124 client.write_points(self.points, time_precision='s') |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
125 except influxdb.exceptions.InfluxDBServerError as e: |
690 | 126 log.error(repr(e)) |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
127 reactor.crash() |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
128 self.points = [] |
53 | 129 |
690 | 130 verboseLogging(False) |
131 | |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
132 masterGraph = PatchableGraph() |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
133 poller = Poller() |
690 | 134 |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
135 reactor.listenTCP(9107, cyclone.web.Application([ |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
136 (r'/', Root), |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
137 (r'/idle', Idle), |
690 | 138 (r'/graph/xidle', CycloneGraphHandler, {'masterGraph': masterGraph}), |
139 (r'/graph/xidle/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}), | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
140 ]), interface='::') |
53 | 141 |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
142 reactor.run() |