annotate service/shuttlepro/shuttleservice.py @ 1749:4b29ce991e59

cloudfree plug sends mqtt metrics, which we export to victoriametrics
author drewp@bigasterisk.com
date Sun, 28 Apr 2024 16:01:38 -0700
parents 68a7a15b0eb5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
130
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
1 import sys, time
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
2 import cyclone.web, cyclone.httpclient, cyclone.websocket
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
3 sys.path.append("../../lib")
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
4 from logsetup import log
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
5 from cycloneerr import PrettyErrorHandler
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
6 from twisted.internet import reactor, threads
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
7 from shuttlepro import powermate
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
8 sys.path.append("/my/site/magma")
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
9 from stategraph import StateGraph
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
10
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
11 from rdflib import Namespace, Graph, Literal
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
12 SHUTTLEPRO = Namespace("http://bigasterisk.com/room/livingRoom/shuttlepro/")
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
13 ROOM = Namespace("http://projects.bigasterisk.com/room/")
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
14
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
15 def sendOneShotGraph(g):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
16 if not g:
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
17 return
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
18
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
19 nt = g.serialize(format='nt')
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
20 cyclone.httpclient.fetch(
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
21 "http://bang:9071/oneShot",
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
22 method='POST',
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
23 postdata=nt,
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
24 timeout=1,
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
25 headers={'Content-Type': ['text/n3']},
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
26 ).addErrback(log.error)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
27
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
28 class Index(PrettyErrorHandler, cyclone.web.StaticFileHandler):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
29 def get(self, *args, **kw):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
30 self.settings.poller.assertCurrent()
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
31 return cyclone.web.StaticFileHandler.get(self, *args, **kw)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
32
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
33 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
34 def get(self):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
35 p = self.settings.poller
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
36 g = StateGraph(ROOM.environment)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
37 g.add((SHUTTLEPRO['shuttle'], ROOM['angle'],
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
38 Literal(p.currentShuttleAngle)))
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
39 g.add((SHUTTLEPRO['dial'], ROOM['totalDialMovement'],
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
40 Literal(p.totalDialMovement)))
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
41 self.set_header('Content-type', 'application/x-trig')
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
42 self.write(g.asTrig())
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
43
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
44 class Poller(object):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
45 def __init__(self, dev="/dev/input/by-id/usb-Contour_Design_ShuttlePRO-event-if00"):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
46 self.lastUpdateTime = 0
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
47 self.p = powermate(dev, self.onEvent)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
48 self.updateLoop()
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
49 self.currentShuttleAngle = 0
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
50 self.totalDialMovement = 0
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
51
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
52 def assertCurrent(self):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
53 ago = time.time() - self.lastUpdateTime
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
54 if ago > 60: # this may have to go up depending on how read_next times out
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
55 raise ValueError("last usb update was %s sec ago" % ago)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
56
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
57 def onEvent(self, what):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
58 print 'onEvent', what
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
59 g = Graph()
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
60 if 'key' in what:
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
61 g.add((SHUTTLEPRO['button%s' % what['key']['button']],
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
62 ROOM['state'],
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
63 ROOM['press'] if what['key']['press'] else ROOM['release']))
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
64 elif 'shuttle' in what:
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
65 # this will send lots of repeats. It's really not a one-shot at all.
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
66 g.add((SHUTTLEPRO['shuttle'], ROOM['position'],
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
67 Literal(what['shuttle'])))
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
68 self.currentShuttleAngle = what['shuttle']
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
69 elif 'dial' in what:
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
70 g.add((SHUTTLEPRO['dial'], ROOM['change'],
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
71 ROOM['clockwise'] if what['dial'] == 1 else
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
72 ROOM['counterclockwise']))
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
73 self.totalDialMovement += what['dial']
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
74 sendOneShotGraph(g)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
75
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
76 def updateLoop(self, *prevResults):
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
77 self.lastUpdateTime = time.time()
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
78 threads.deferToThread(
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
79 self.p.read_next
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
80 ).addCallback(self.updateLoop)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
81
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
82 if __name__ == '__main__':
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
83 from twisted.python import log as twlog
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
84 twlog.startLogging(sys.stdout)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
85 port = 9103
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
86 poller = Poller()
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
87 reactor.listenTCP(port, cyclone.web.Application(handlers=[
131
drewp@bigasterisk.com
parents: 130
diff changeset
88 (r'/()', Index, {"path" : ".", "default_filename" : "index.html"}),
130
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
89 (r'/graph', GraphResource),
131
drewp@bigasterisk.com
parents: 130
diff changeset
90 # serves this source code too
drewp@bigasterisk.com
parents: 130
diff changeset
91 (r'/(.*)', cyclone.web.StaticFileHandler, {"path" : "."})
130
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
92 ], poller=poller))
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
93 log.info("serving on %s" % port)
57ae7dc0f417 new shuttlepro web service with /graph
drewp@bigasterisk.com
parents:
diff changeset
94 reactor.run()