Mercurial > code > home > repos > homeauto
changeset 1190:463e53246e9a
rename dpms
Ignore-this: b53e4ad5e3bd79252deba2a18200a2a
darcs-hash:48198fad3bece9ae6a910e807f2fc039b05463d5
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Wed, 02 Jan 2019 11:16:32 -0800 |
parents | c1cf544711da |
children | 5f699ea349a2 |
files | service/dpms/dpms.py service/dpms/dpms_service.py |
diffstat | 2 files changed, 117 insertions(+), 117 deletions(-) [+] |
line wrap: on
line diff
--- a/service/dpms/dpms.py Tue Jan 01 10:15:25 2019 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -#!bin/python - -""" -sample supervisord block - -[program:dpms_9095] -directory=/my/proj/homeauto/service/dpms -command=/my/proj/homeauto/service/dpms/bin/python dpms.py -user=drewp - -On one box, this goes super slow when avahi daemon is running. Maybe -it's for an attempted dns lookup of the requesting IP address, which I -wish I could switch off. - --- - -may need this: -ps axf | grep /run/gdm -18339 tty7 Ss+ 0:00 \_ /usr/bin/X :0 -background none -verbose -auth /run/gdm/auth-for-gdm-iQoCDZ/database -nolisten tcp vt7 -eval xauth add `sudo xauth -f /run/gdm/auth-for-gdm-iQoCDZ/database list :0` - -""" - -from twisted.internet import reactor, task -import cyclone.web -from influxdb import InfluxDBClient -import subprocess, sys, socket, time, os -from rdflib import Namespace, URIRef -DEV = Namespace("http://projects.bigasterisk.com/device/") -ROOM = Namespace("http://projects.bigasterisk.com/room/") - -sys.path.append("/my/site/magma") -from stategraph import StateGraph -sys.path.append("../../lib") -from localdisplay import setDisplayToLocalX - -influx = InfluxDBClient('bang6', 9060, 'root', 'root', 'main') - -def getMonitorState(): - out = subprocess.check_output(['xset', 'q']) - for line in out.splitlines(): - line = line.strip() - if line == 'Monitor is On': - return 'on' - elif line in ['Monitor is Off', 'Monitor is in Suspend', 'Monitor is in Standby']: - return 'off' - raise NotImplementedError("no matching monitor line in xset output") - -class Root(cyclone.web.RequestHandler): - def get(self): - getMonitorState() # to make it fail if xset isn't working - self.write(''' - Get and put the <a href="monitor">monitor power</a> with dpms. - <a href="graph">rdf graph</a> available.''') - -class Monitor(cyclone.web.RequestHandler): - def get(self): - self.set_header('content-type', 'text/plain') - self.write(getMonitorState()) - - def put(self): - body = self.request.body.strip() - if body in ['on', 'off']: - subprocess.check_call(['xset', 'dpms', 'force', body]) - self.set_status(204) - else: - raise NotImplementedError("body must be 'on' or 'off'") - - -class Graph(cyclone.web.RequestHandler): - def get(self): - host = socket.gethostname() - g = StateGraph(ctx=DEV['dpms/%s' % host]) - g.add((URIRef("http://bigasterisk.com/host/%s/monitor" % host), - ROOM['powerStateMeasured'], - ROOM[getMonitorState()])) - - self.set_header('Content-type', 'application/x-trig') - self.write(g.asTrig()) - - -class Poller(object): - def __init__(self): - self.lastSent = None - self.lastSentTime = 0 - task.LoopingCall(self.poll).start(5) - - def poll(self): - now = int(time.time()) - try: - state = getMonitorState() - except subprocess.CalledProcessError, e: - print repr(e) - os.abort() - if state != self.lastSent or (now > self.lastSentTime + 3600): - influx.write_points([ - {'measurement': 'power', - 'tags': {'device': '%sMonitor' % socket.gethostname()}, - 'fields': {'value': 1 if state == 'on' else 0}, - 'time': now - }], time_precision='s') - - self.lastSent = state - self.lastSentTime = now - -setDisplayToLocalX() -poller = Poller() - -reactor.listenTCP(9095, cyclone.web.Application([ - (r'/', Root), - (r'/monitor', Monitor), - (r'/graph', Graph), -]), interface='::') - -reactor.run() - -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/dpms/dpms_service.py Wed Jan 02 11:16:32 2019 -0800 @@ -0,0 +1,117 @@ +#!bin/python + +""" +sample supervisord block + +[program:dpms_9095] +directory=/my/proj/homeauto/service/dpms +command=/my/proj/homeauto/service/dpms/bin/python dpms.py +user=drewp + +On one box, this goes super slow when avahi daemon is running. Maybe +it's for an attempted dns lookup of the requesting IP address, which I +wish I could switch off. + +-- + +may need this: +ps axf | grep /run/gdm +18339 tty7 Ss+ 0:00 \_ /usr/bin/X :0 -background none -verbose -auth /run/gdm/auth-for-gdm-iQoCDZ/database -nolisten tcp vt7 +eval xauth add `sudo xauth -f /run/gdm/auth-for-gdm-iQoCDZ/database list :0` + +""" + +from twisted.internet import reactor, task +import cyclone.web +from influxdb import InfluxDBClient +import subprocess, sys, socket, time, os +from rdflib import Namespace, URIRef +DEV = Namespace("http://projects.bigasterisk.com/device/") +ROOM = Namespace("http://projects.bigasterisk.com/room/") + +sys.path.append("/my/site/magma") +from stategraph import StateGraph +sys.path.append("../../lib") +from localdisplay import setDisplayToLocalX + +influx = InfluxDBClient('bang6', 9060, 'root', 'root', 'main') + +def getMonitorState(): + out = subprocess.check_output(['xset', 'q']) + for line in out.splitlines(): + line = line.strip() + if line == 'Monitor is On': + return 'on' + elif line in ['Monitor is Off', 'Monitor is in Suspend', 'Monitor is in Standby']: + return 'off' + raise NotImplementedError("no matching monitor line in xset output") + +class Root(cyclone.web.RequestHandler): + def get(self): + getMonitorState() # to make it fail if xset isn't working + self.write(''' + Get and put the <a href="monitor">monitor power</a> with dpms. + <a href="graph">rdf graph</a> available.''') + +class Monitor(cyclone.web.RequestHandler): + def get(self): + self.set_header('content-type', 'text/plain') + self.write(getMonitorState()) + + def put(self): + body = self.request.body.strip() + if body in ['on', 'off']: + subprocess.check_call(['xset', 'dpms', 'force', body]) + self.set_status(204) + else: + raise NotImplementedError("body must be 'on' or 'off'") + + +class Graph(cyclone.web.RequestHandler): + def get(self): + host = socket.gethostname() + g = StateGraph(ctx=DEV['dpms/%s' % host]) + g.add((URIRef("http://bigasterisk.com/host/%s/monitor" % host), + ROOM['powerStateMeasured'], + ROOM[getMonitorState()])) + + self.set_header('Content-type', 'application/x-trig') + self.write(g.asTrig()) + + +class Poller(object): + def __init__(self): + self.lastSent = None + self.lastSentTime = 0 + task.LoopingCall(self.poll).start(5) + + def poll(self): + now = int(time.time()) + try: + state = getMonitorState() + except subprocess.CalledProcessError, e: + print repr(e) + os.abort() + if state != self.lastSent or (now > self.lastSentTime + 3600): + influx.write_points([ + {'measurement': 'power', + 'tags': {'device': '%sMonitor' % socket.gethostname()}, + 'fields': {'value': 1 if state == 'on' else 0}, + 'time': now + }], time_precision='s') + + self.lastSent = state + self.lastSentTime = now + +setDisplayToLocalX() +poller = Poller() + +reactor.listenTCP(9095, cyclone.web.Application([ + (r'/', Root), + (r'/monitor', Monitor), + (r'/graph', Graph), +]), interface='::') + +reactor.run() + +