comparison service/arduinoNode/arduinoNode.py @ 170:376599552a4c

polymer board debug page with working output widgets Ignore-this: 3157d0c47a91afe47b30a5f182629d93
author drewp@bigasterisk.com
date Mon, 13 Apr 2015 23:30:12 -0700
parents d228105749ac
children f81c4d3d774b
comparison
equal deleted inserted replaced
169:d228105749ac 170:376599552a4c
3 arduino-mk 3 arduino-mk
4 indent 4 indent
5 """ 5 """
6 from __future__ import division 6 from __future__ import division
7 import glob, sys, logging, subprocess, socket, os, hashlib, time, tempfile 7 import glob, sys, logging, subprocess, socket, os, hashlib, time, tempfile
8 import shutil 8 import shutil, json
9 import serial 9 import serial
10 import cyclone.web 10 import cyclone.web
11 from rdflib import Graph, Namespace, URIRef, Literal, RDF 11 from rdflib import Graph, Namespace, URIRef, Literal, RDF
12 from rdflib.parser import StringInputSource 12 from rdflib.parser import StringInputSource
13 from twisted.internet import reactor, task 13 from twisted.internet import reactor, task
70 70
71 self._statementsFromInputs = {} # input uri: latest statements 71 self._statementsFromInputs = {} # input uri: latest statements
72 72
73 self.open() 73 self.open()
74 74
75 def description(self):
76 """for web page"""
77 return {
78 'uri': self.uri,
79 'dev': self.dev,
80 'baudrate': self.baudrate,
81 'devices': [d.description() for d in self._devs],
82 }
83
75 def open(self): 84 def open(self):
76 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, 85 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate,
77 timeout=2) 86 timeout=2)
78 87
79 def startPolling(self): 88 def startPolling(self):
283 with open(workDir + '/main.ino', 'w') as main: 292 with open(workDir + '/main.ino', 'w') as main:
284 main.write(code) 293 main.write(code)
285 294
286 subprocess.check_call(['make', 'upload'], cwd=workDir) 295 subprocess.check_call(['make', 'upload'], cwd=workDir)
287 296
288
289 class Index(cyclone.web.RequestHandler):
290 def get(self):
291 self.set_header("Content-Type", "text/html")
292 self.write(open("index.html").read())
293 297
294 class GraphPage(cyclone.web.RequestHandler): 298 class GraphPage(cyclone.web.RequestHandler):
295 def get(self): 299 def get(self):
296 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host']) 300 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host'])
297 301
328 class OutputPage(cyclone.web.RequestHandler): 332 class OutputPage(cyclone.web.RequestHandler):
329 def post(self): 333 def post(self):
330 stmts = list(rdfGraphBody(self.request.body, self.request.headers)) 334 stmts = list(rdfGraphBody(self.request.body, self.request.headers))
331 for b in self.settings.boards: 335 for b in self.settings.boards:
332 b.outputStatements(stmts) 336 b.outputStatements(stmts)
333 337
338 class Boards(cyclone.web.RequestHandler):
339 def get(self):
340
341 self.set_header('Content-type', 'application/json')
342 self.write(json.dumps({
343 'boards': [b.description() for b in self.settings.boards]
344 }, indent=2))
345
334 def currentSerialDevices(): 346 def currentSerialDevices():
335 log.info('find connected boards') 347 log.info('find connected boards')
336 return glob.glob('/dev/serial/by-id/*') 348 return glob.glob('/dev/serial/by-id/*')
337 349
338 def main(): 350 def main():
360 from twisted.python import log as twlog 372 from twisted.python import log as twlog
361 twlog.startLogging(sys.stdout) 373 twlog.startLogging(sys.stdout)
362 374
363 log.setLevel(logging.DEBUG) 375 log.setLevel(logging.DEBUG)
364 reactor.listenTCP(9059, cyclone.web.Application([ 376 reactor.listenTCP(9059, cyclone.web.Application([
365 (r"/", Index), 377 (r"/()", cyclone.web.StaticFileHandler, {
378 "path": "static", "default_filename": "index.html"}),
379 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}),
380 (r'/boards', Boards),
366 (r"/graph", GraphPage), 381 (r"/graph", GraphPage),
367 (r'/output', OutputPage), 382 (r'/output', OutputPage),
368 (r'/arduinoCode', ArduinoCode), 383 (r'/arduinoCode', ArduinoCode),
369 (r'/dot', Dot), 384 (r'/dot', Dot),
370 ], config=config, boards=boards)) 385 ], config=config, boards=boards))