def __init__(self, path): # needs /etc/dbus-1/system.d/openroberta.conf bus_name = dbus.service.BusName('org.openroberta.lab', bus=dbus.SystemBus()) dbus.service.Object.__init__(self, bus_name, path) logger.info('object registered') self.status('disconnected') self.hal = Hal(None, None) self.hal.clearDisplay() self.thread = None
from ev3dev import ev3 as ev3dev import math import os class BreakOutOfALoop(Exception): pass class ContinueLoop(Exception): pass _brickConfiguration = { 'wheel-diameter': 5.6, 'track-width': 18.0, 'actors': { }, 'sensors': { }, } hal = Hal(_brickConfiguration) item = [0, 0, 0] item2 = [True, True, True] item3 = ["1", "2", "3"] item4 = ['white', 'white', 'white'] item5 = [None, None, None] item6 = 0 item7 = True item8 = "123" item9 = 'white' item10 = None def run(): global item, item2, item8, item7, item9, item4, item3, item10, item6, item5 item[0] = 0 item[-1 -0] = 0
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()