async def dev_service(): # cdbus hw setup sock = CDWebSocket(ws_ns, 'dev') while True: dat, src = await sock.recvfrom() print('dev ser', dat) if dat['action'] == 'get': ports = get_ports() if csa['dev']: await sock.sendto( { 'ports': ports, 'port': csa['dev'].portstr, 'online': csa['dev'].online, 'net': csa['net'], 'mac': csa['mac'] }, src) else: await sock.sendto( { 'ports': ports, 'port': None, 'online': False, 'net': csa['net'], 'mac': csa['mac'] }, src) elif dat['action'] == 'open' and not csa['dev']: try: if dat['bridge']: csa['dev'] = CDBusBridge(dat['port']) else: csa['dev'] = CDBusSerial(dat['port'], baud=dat['baud']) await sock.sendto('successed', src) except Exception as err: print('open dev err:', err) await sock.sendto(f'err: {err}', src) elif dat['action'] == 'close' and csa['dev']: print('stop dev') csa['dev'].stop() print('stop finished') csa['dev'] = None await sock.sendto('successed', src) elif dat['action'] == 'set_local': print('set_local') csa['net'] = dat['net'] csa['mac'] = dat['mac'] await sock.sendto('successed', src) else: await sock.sendto('err: dev: unknown cmd', src)
async def file_service(): # config r/w for cfg in os.listdir('configs'): if cfg.endswith('.json'): csa['cfgs'].append(cfg) sock = CDWebSocket(ws_ns, 'file') while True: dat, src = await sock.recvfrom() print('file ser', dat) if dat['action'] == 'get_cfgs': await sock.sendto(csa['cfgs'], src) elif dat['action'] == 'get_cfg': with open(os.path.join('configs', dat['cfg'])) as c_file: c = json5.load(c_file) await sock.sendto(c, src) elif dat['action'] == 'get_bin': with open(dat['path'], 'rb') as b_file: await sock.sendto(b_file.read(), src) else: await sock.sendto('err: file: unknown cmd', src)
async def file_service(): # config r/w for cfg in os.listdir('configs'): if cfg.endswith('.json'): csa['cfgs'].append(cfg) sock = CDWebSocket(ws_ns, 'file') while True: dat, src = await sock.recvfrom() logger.debug(f'file ser: {dat}') if dat['action'] == 'get_cfgs': await sock.sendto(csa['cfgs'], src) elif dat['action'] == 'get_cfg': with open(os.path.join('configs', dat['cfg'])) as c_file: c = json5.load(c_file) await sock.sendto(c, src) elif dat['action'] == 'get_ihex': ret = [] ih = IntelHex() try: ih.loadhex(dat['path']) segs = ih.segments() logger.info( f'parse ihex file, segments: {[list(map(hex, l)) for l in segs]} (end addr inclusive)' ) for seg in segs: s = [seg[0], ih.tobinstr(seg[0], size=seg[1] - seg[0])] ret.append(s) except Exception as err: logger.error(f'parse ihex file error: {err}') await sock.sendto(ret, src) else: await sock.sendto('err: file: unknown cmd', src)
c = json5.load(c_file) await sock.sendto(c, src) elif dat['action'] == 'get_bin': with open(dat['path'], 'rb') as b_file: await sock.sendto(b_file.read(), src) else: await sock.sendto('err: file: unknown cmd', src) async def open_brower(): proc = await asyncio.create_subprocess_shell( '/opt/google/chrome/chrome --app=http://localhost:8910') await proc.communicate() #proc = await asyncio.create_subprocess_shell('chromium --app=http://localhost:8910') #await proc.communicate() print('open brower done.') if __name__ == "__main__": start_web(None) csa['proxy'] = CDWebSocket(ws_ns, 'proxy') csa['async_loop'] = asyncio.get_event_loop() asyncio.get_event_loop().create_task(file_service()) asyncio.get_event_loop().create_task(dev_service()) asyncio.get_event_loop().create_task(cdbus_proxy_service()) #asyncio.get_event_loop().create_task(open_brower()) print('Please open url: http://localhost:8910') asyncio.get_event_loop().run_forever()