Mercurial > code > home > repos > homeauto
changeset 292:105969d248d6
rewrite xidle to cyclone. add bg updating graph
Ignore-this: 5e02bc3572723517fb5ed3aa6971805a
author | drewp@bigasterisk.com |
---|---|
date | Mon, 01 Aug 2016 02:24:50 -0700 |
parents | 299ddd7e2070 |
children | fc0e42933baa |
files | service/xidle/pydeps service/xidle/xidle.py |
diffstat | 2 files changed, 58 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/service/xidle/pydeps Wed Jul 20 23:52:03 2016 -0700 +++ b/service/xidle/pydeps Mon Aug 01 02:24:50 2016 -0700 @@ -1,6 +1,6 @@ -bottle==0.11.4 -gunicorn==0.17.2 python-dateutil==2.1 rdflib==3.2.3 https://bebop.bigasterisk.com/python/PyXSS.tar.gz +influxdb==3.0.0 +cyclone==1.1
--- a/service/xidle/xidle.py Wed Jul 20 23:52:03 2016 -0700 +++ b/service/xidle/xidle.py Mon Aug 01 02:24:50 2016 -0700 @@ -4,9 +4,12 @@ X server idle time is now available over http! """ -from bottle import run, get, put, request, response -import subprocess, sys, socket +import time +import sys, socket, json from rdflib import Namespace, URIRef, Literal +from influxdb import InfluxDBClient +import cyclone.web +from twisted.internet import reactor, task # from http://bebop.bigasterisk.com/python/ import xss @@ -19,29 +22,61 @@ from stategraph import StateGraph host = socket.gethostname() +client = InfluxDBClient('bang6', 9060, 'root', 'root', 'main') -@get("/") -def index(): - xss.get_info() # fail if we can't get the display or something - return ''' +class Root(cyclone.web.RequestHandler): + def get(self): + xss.get_info() # fail if we can't get the display or something + self.write(''' Get the <a href="idle">X idle time</a> on %s. - <a href="graph">rdf graph</a> available.''' % host + <a href="graph">rdf graph</a> available.''' % host) + +class Idle(cyclone.web.RequestHandler): + def get(self): + self.set_header('Content-type', 'application/json') + self.write(json.dumps({"idleMs" : xss.get_info().idle})) + +class Graph(cyclone.web.RequestHandler): + def get(self): + self.set_header('Content-type', 'application/x-trig') -@get("/idle") -def monitor(): - return {"idleMs" : xss.get_info().idle} + g = StateGraph(ctx=DEV['xidle/%s' % host]) + + ms = xss.get_info().idle + subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host) + g.add((subj, ROOM['idleTimeMs'], Literal(ms))) + g.add((subj, ROOM['idleTimeMinutes'], Literal(ms / 1000 / 60))) + + self.write(g.asTrig()) -@get("/graph") -def graph(): - g = StateGraph(ctx=DEV['xidle/%s' % host]) +class Poller(object): + def __init__(self): + self.points = [] + self.lastSent = None + self.lastSentTime = 0 + task.LoopingCall(self.poll).start(5) + + def poll(self): + ms = xss.get_info().idle + lastMinActive = ms < 60 * 1000 + now = int(time.time()) + if self.lastSent != lastMinActive or now > self.lastSentTime + 3600: + self.points.append({"measurement": "presence", + "tags": {"host": host, "sensor": "xidle"}, + "fields": {"value": 1 if lastMinActive else 0}, + "time": now}) + self.lastSent = lastMinActive + self.lastSentTime = now - ms = xss.get_info().idle - subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host) - g.add((subj, ROOM['idleTimeMs'], Literal(ms))) - g.add((subj, ROOM['idleTimeMinutes'], Literal(ms / 1000 / 60))) + client.write_points(self.points, time_precision='s') + self.points = [] - response.set_header('Content-type', 'application/x-trig') - return g.asTrig() +poller = Poller() + +reactor.listenTCP(9107, cyclone.web.Application([ + (r'/', Root), + (r'/idle', Idle), + (r'/graph', Graph), +]), interface='::') -run(host="[::]", server='gunicorn', port=9107, quiet=True) - +reactor.run()