Mercurial > code > home > repos > homeauto
annotate service/xidle/xidle.py @ 432:f134b64a0ab7
py3, rfid-console rename
Ignore-this: 1b28d912e8847685a87c0c9ccb703608
author | drewp@bigasterisk.com |
---|---|
date | Sun, 07 Apr 2019 03:58:51 -0700 |
parents | f1177213fe22 |
children | 3df357924f1d |
rev | line source |
---|---|
53 | 1 #!bin/python |
69 | 2 from __future__ import division |
53 | 3 """ |
4 X server idle time is now available over http! | |
361 | 5 |
6 Note: HD-4110 webcams stop X from going idle by sending events | |
7 constantly. Run this to fix: | |
8 | |
9 xinput disable "HP Webcam HD-4110" | |
53 | 10 """ |
11 | |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
12 import time |
361 | 13 import sys, socket, json, os |
53 | 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 | 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 | 19 |
361 | 20 import actmon |
53 | 21 # another option: http://thp.io/2007/09/x11-idle-time-and-focused-window-in.html |
22 | |
23 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
24 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
25 | |
361 | 26 sys.path.append('../../lib') |
27 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler | |
53 | 28 |
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 | 31 |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
32 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
|
33 |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
34 actmon.get_idle_time() # fail if we can't get the display or something |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
35 |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
36 class Root(cyclone.web.RequestHandler): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
37 def get(self): |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
38 actmon.get_idle_time() # fail if we can't get the display or something |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
39 self.write(''' |
53 | 40 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
|
41 <a href="graph">rdf graph</a> available.''' % host) |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
42 |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
43 class Idle(cyclone.web.RequestHandler): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
44 def get(self): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
45 self.set_header('Content-type', 'application/json') |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
46 self.write(json.dumps({"idleMs" : actmon.get_idle_time()})) |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
47 |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
48 class Poller(object): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
49 def __init__(self): |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
50 self.points = [] |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
51 self.lastSent = None |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
52 self.lastSentTime = 0 |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
53 task.LoopingCall(self.poll).start(5) |
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 def poll(self): |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
56 ms = actmon.get_idle_time() |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
57 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
|
58 subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host) |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
59 masterGraph.patchObject(ctx, subj, ROOM['idleTimeMs'], Literal(ms)) |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
60 masterGraph.patchObject(ctx, subj, ROOM['idleTimeMinutes'], |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
61 Literal(ms / 1000 / 60)) |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
62 |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
63 lastMinActive = ms < 60 * 1000 |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
64 now = int(time.time()) |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
65 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
|
66 self.points.append({"measurement": "presence", |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
67 "tags": {"host": host, "sensor": "xidle"}, |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
68 "fields": {"value": 1 if lastMinActive else 0}, |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
69 "time": now}) |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
70 self.lastSent = lastMinActive |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
71 self.lastSentTime = now |
69 | 72 |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
73 try: |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
74 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
|
75 except influxdb.exceptions.InfluxDBServerError as e: |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
76 print repr(e) |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
77 reactor.crash() |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
78 self.points = [] |
53 | 79 |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
80 |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
81 masterGraph = PatchableGraph() |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
82 poller = Poller() |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
83 |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
84 reactor.listenTCP(9107, cyclone.web.Application([ |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
85 (r'/', Root), |
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
86 (r'/idle', Idle), |
363
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
87 (r'/graph', CycloneGraphHandler, {'masterGraph': masterGraph}), |
f1177213fe22
xidle support graph/events streams, use actmod module for the input now
drewp@bigasterisk.com
parents:
361
diff
changeset
|
88 (r'/graph/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}), |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
89 ]), interface='::') |
53 | 90 |
292
105969d248d6
rewrite xidle to cyclone. add bg updating graph
drewp@bigasterisk.com
parents:
215
diff
changeset
|
91 reactor.run() |