def __init__(self, address, service): threading.Thread.__init__(self) self.address = address self.service = service self.params = { 'macaddr': '70:1e:bb:88:89:bc', 'firmwarename': 'ev3dev', 'menuversion': version.split('-')[0], } self.updateConfiguration(); self.registered = False self.running = True logger.info('thread created')
def main(): logger = logging.getLogger('robertalab') logger.info('--- starting ---') params = { 'macaddr': '70:1e:bb:88:89:bc', 'firmwarename': 'ev3dev', 'menuversion': version.split('-')[0], } headers = {'Content-Type': 'application/json'} try: with open('.robertalab.json') as cfg_file: cfg = json.load(cfg_file) except IOError as e: cfg = { 'target': 'http://lab.open-roberta.org/', # address and port of server } os.system('setterm -cursor off') hal = Hal(None, None) updateConfiguration(params) drawUI(hal, params) registered = False while not hal.isKeyPressed('back'): try: if registered: params['cmd'] = 'push' timeout = 15 else: params['cmd'] = 'register' timeout = 330 params['brickname'] = socket.gethostname() params['battery'] = getBatteryVoltage() # TODO: what about /api/v1/pushcmd logger.info('sending: %s' % params['cmd']) req = urllib2.Request('%s/pushcmd' % cfg['target'], headers=headers) response = urllib2.urlopen(req, json.dumps(params), timeout=timeout) reply = json.loads(response.read()) logger.info('response: %s' % json.dumps(reply)) cmd = reply['cmd'] if cmd == 'repeat': hal.drawText('registered', 0, 2) if not registered: hal.playFile(2) registered = True elif cmd == 'abort': break elif cmd == 'download': hal.drawText('executing ...', 0, 3) # TODO: url is not part of reply :/ # TODO: we should receive a digest for the download (md5sum) so that # we can verify the download req = urllib2.Request('%s/download' % cfg['target'], headers=headers) response = urllib2.urlopen(req, json.dumps(params), timeout=timeout) logger.info('response: %s' % json.dumps(reply)) hdr = response.info().getheader('Content-Disposition') filename = '/tmp/%s' % hdr.split('=')[1] if hdr else 'unknown' with open(filename, 'w') as prog: prog.write(response.read()) logger.info('code downloaded to: %s' % filename) # new process #res = subprocess.call(["python", filename], env={"PYTHONPATH":"$PYTONPATH:."}) #logger.info('execution result: %d' % res) # eval from file, see http://bugs.python.org/issue14049 # NOTE: all the globals in the generated code will override gloabls we use here! try: execfile(filename, globals(), globals()) logger.info('execution finished') except: logger.exception("Ooops:") drawUI(hal, params) hal.drawText('registered', 0, 2) elif cmd == 'update': # FIXME: # fetch new files (menu/hal) # then restart: # os.execv(__file__, sys.argv) # check if we need to close files (logger?) pass else: logger.warning('unhandled command: %s' % cmd) except urllib2.HTTPError as e: logger.error("HTTPError(%s): %s" % (e.code, e.reason)) break except urllib2.URLError as e: # [Errno 111] Connection refused> logger.error("URLError: %s" % e.reason) break except socket.timeout: pass except: logger.exception("Ooops:") os.system('setterm -cursor on') logger.info('--- done ---') logging.shutdown()
def main(): logger = logging.getLogger('robertalab') logger.info('--- starting ---') params = { 'macaddr': '70:1e:bb:88:89:bc', 'firmwarename': 'ev3dev', 'menuversion': version.split('-')[0], } headers = { 'Content-Type': 'application/json' } try: with open('.robertalab.json') as cfg_file: cfg = json.load(cfg_file) except IOError as e: cfg = { 'target': 'http://lab.open-roberta.org/', # address and port of server } os.system('setterm -cursor off') hal = Hal(None, None) updateConfiguration(params) drawUI(hal, params) registered = False while not hal.isKeyPressed('back'): try: if registered: params['cmd'] = 'push' timeout = 15 else: params['cmd'] = 'register' timeout = 330 params['brickname'] = socket.gethostname() params['battery'] = getBatteryVoltage() # TODO: what about /api/v1/pushcmd logger.info('sending: %s' % params['cmd']) req = urllib2.Request('%s/pushcmd' % cfg['target'], headers=headers) response = urllib2.urlopen(req, json.dumps(params), timeout=timeout) reply = json.loads(response.read()) logger.info('response: %s' % json.dumps(reply)) cmd = reply['cmd'] if cmd == 'repeat': hal.drawText('registered', 0, 2) if not registered: hal.playFile(2) registered = True elif cmd == 'abort': break elif cmd == 'download': hal.drawText('executing ...', 0, 3) # TODO: url is not part of reply :/ # TODO: we should receive a digest for the download (md5sum) so that # we can verify the download req = urllib2.Request('%s/download' % cfg['target'], headers=headers) response = urllib2.urlopen(req, json.dumps(params), timeout=timeout) logger.info('response: %s' % json.dumps(reply)) hdr = response.info().getheader('Content-Disposition') filename = '/tmp/%s' % hdr.split('=')[1] if hdr else 'unknown' with open(filename, 'w') as prog: prog.write(response.read()) logger.info('code downloaded to: %s' % filename) # new process #res = subprocess.call(["python", filename], env={"PYTHONPATH":"$PYTONPATH:."}) #logger.info('execution result: %d' % res) # eval from file, see http://bugs.python.org/issue14049 # NOTE: all the globals in the generated code will override gloabls we use here! try: execfile(filename, globals(), globals()) logger.info('execution finished') except: logger.exception("Ooops:") drawUI(hal, params) hal.drawText('registered', 0, 2) elif cmd == 'update': # FIXME: # fetch new files (menu/hal) # then restart: # os.execv(__file__, sys.argv) # check if we need to close files (logger?) pass else: logger.warning('unhandled command: %s' % cmd) except urllib2.HTTPError as e: logger.error("HTTPError(%s): %s" % (e.code, e.reason)) break except urllib2.URLError as e: # [Errno 111] Connection refused> logger.error("URLError: %s" % e.reason) break except socket.timeout: pass except: logger.exception("Ooops:") os.system('setterm -cursor on') logger.info('--- done ---') logging.shutdown()