async def _broadcast(app: web.Application, sid: str, exchange: str, routing: str, interval: int ): publisher = events.get_publisher(app) spark_status = status.get_status(app) ctrl = device.get_controller(app) last_broadcast_ok = True while True: try: await spark_status.connected.wait() await asyncio.sleep(interval) obj = await ctrl.read_object({OBJECT_SID_KEY: sid}) await publisher.publish( exchange=exchange, routing=routing, message=obj[OBJECT_DATA_KEY] ) if not last_broadcast_ok: LOGGER.info(f'Remote publisher [{routing}] resumed Ok') last_broadcast_ok = True except CancelledError: break except Exception as ex: if last_broadcast_ok: warnings.warn(f'Remote publisher [{routing}] encountered an error: {strex(ex)}') last_broadcast_ok = False
async def do_command(request: web.Request) -> web.Response: """ --- summary: Do a specific command tags: - Debug operationId: controller.spark.debug.do produces: - application/json parameters: - in: body name: body description: command required: try schema: type: object properties: command: type: string example: list_objects data: type: object example: {"group_id":0} """ request_args = await request.json() command = request_args['command'] data = request_args['data'] func = getattr(device.get_controller(request.app), command) return web.json_response(await func(**data))
def test_main(mocker, app): mocker.patch(TESTED + '.service.run') mocker.patch(TESTED + '.service.create_app').return_value = app main.main() assert None not in [ commander.get_commander(app), datastore.get_datastore(app), device.get_controller(app), events.get_listener(app), broadcaster.get_broadcaster(app) ]
async def test_slave_all(app, client, created, object_args, dummy_listener): ctrl = device.get_controller(app) await client.post('/remote/slave', json={ 'id': object_args[OBJECT_SID_KEY], 'key': 'testface', 'translations': {} }) # No translation table: everything is used data = object_args[OBJECT_DATA_KEY] data['address'] = 'aa'.rjust(16, '0') await dummy_listener.on_message(None, 'testface', deepcopy(data)) updated = await ctrl.read_object({OBJECT_SID_KEY: object_args[OBJECT_SID_KEY]}) assert updated[OBJECT_DATA_KEY]['address'] == 'aa'.rjust(16, '0')
async def _receive(app: web.Application, sid: str, translations: dict, subscription: events.EventSubscription, routing: str, incoming: dict ): ctrl = device.get_controller(app) obj = await ctrl.read_object({OBJECT_SID_KEY: sid}) existing = obj[OBJECT_DATA_KEY] LOGGER.debug(f'existing={existing}\nincoming={incoming}') if translations: for remote_path, local_path in translations.items(): with suppress(KeyError): val = dpath.util.get(incoming, glob.escape(remote_path)) dpath.util.new(existing, local_path, val) else: # No translations -> use the remote object in its entirety obj[OBJECT_DATA_KEY] = incoming await ctrl.write_object(obj)
def ctrl(app): return device.get_controller(app)
def __init__(self, app: web.Application, wait_sync=True): self._wait_sync = wait_sync self._status = status.get_status(app) self._ctrl: device.SparkController = device.get_controller(app) self._store: twinkeydict.TwinKeyDict = datastore.get_datastore(app)