async def _get_value(self, key) -> Any: """ Get a value from the map of the downlink using a given key, after it has been synced. :param key - The key of the entry. :return: - The current value of the downlink. """ return self._map.get(key, (Value.absent(), Value.absent()))[1]
def map(self, key: Any) -> [Value, dict]: if self.model is None: return Value.absent() else: if key is None: return self.model.map.values() else: return self.model.map.get(key, Value.absent())
def _map(self, key: Any) -> [Value, dict]: if self._model is None: return Value.absent() else: if key is None: return list(self._model._map.values()) else: return self._model._map.get( key, (Value.absent(), Value.absent()))[1]
def _write_value(self, value: Value) -> '_OutputMessage': if isinstance(value, _Record): return self._write_record(value) elif isinstance(value, Text): return self._write_text(value.get_string_value()) elif isinstance(value, Num): return self._write_number(value.get_num_value()) elif isinstance(value, Bool): return self._write_bool(value.get_bool_value()) elif isinstance(value, _Absent): return self._write_absent()
async def write_value(self, value: Value) -> 'OutputMessage': if isinstance(value, Record): return await self.write_record(value) elif isinstance(value, Text): return await self.write_text(value.get_string_value()) elif isinstance(value, Num): return await self.write_number(value.get_num_value()) elif isinstance(value, Bool): return await self.write_bool(value.get_bool_value()) elif isinstance(value, Absent): return await self.write_absent()
async def __receive_remove(self, message: '_Envelope') -> None: key = RecordConverter.get_converter().record_to_object( message._body._get_head().value._get_head().value, self.downlink_manager.registered_classes, self.downlink_manager.strict) recon_key = Recon.to_string( message._body._get_head().value._get_head().value) old_value = self._map.pop(recon_key, (Value.absent(), Value.absent()))[1] await self.downlink_manager._subscribers_did_remove(key, old_value)
async def _register_manager(self, manager: '_DownlinkManager') -> None: await self._assign_manager(manager) if manager._is_open: await self._execute_did_set(self._model._value, Value.absent()) self._initialised.set()
async def register_manager(self, manager: 'DownlinkManager') -> None: await self.assign_manager(manager) if manager.is_open: await self.execute_did_set(self.model.value, Value.absent()) self.initialised.set()
async def _register_manager(self, manager: '_DownlinkManager') -> None: await self._assign_manager(manager) if manager._is_open: for key, value in self._model._map.values(): await self._execute_did_update(key, value, Value.absent()) self._initialised.set()
def test_link_addressed_form_mold_empty(self): # Given envelope = None form = _SyncRequestForm() # When actual = form._mold(envelope) # Then self.assertEqual(Value.extant(), actual)
def test_lane_addressed_form_mold_empty(self): # Given envelope = None form = SyncedResponseForm() # When actual = form.mold(envelope) # Then self.assertEqual(Value.extant(), actual)
async def get_value(self, key=None) -> Any: """ Get the value of the downlink after it has been synced. :return: - The current value of the downlink. """ await self.synced.wait() if key is None: return self.map.values() else: return self.map.get(key, [Value.absent()])[0]
async def receive_event(self, message: Envelope): if message.body == Absent.get_absent(): event = Value.absent() elif isinstance(message.body, (Text, Num, Bool)): event = message.body else: converter = RecordConverter.get_converter() event = converter.record_to_object( message.body, self.downlink_manager.registered_classes, self.downlink_manager.strict) await self.downlink_manager.subscribers_on_event(event)
async def __receive_update(self, message: 'Envelope') -> None: key = RecordConverter.get_converter().record_to_object( message.body.get_head().value.get_head().value, self.downlink_manager.registered_classes, self.downlink_manager.strict) value = RecordConverter.get_converter().record_to_object( message.body.get_body(), self.downlink_manager.registered_classes, self.downlink_manager.strict) recon_key = await Recon.to_string( message.body.get_head().value.get_head().value) old_value = self.map.get(recon_key, [Value.absent()])[0] self.map[recon_key] = (key, value) await self.downlink_manager.subscribers_did_update( key, value, old_value)
async def __set_value(self, message: 'Envelope') -> None: """ Set the value of the the downlink and trigger the `did_set` callback of the downlink subscribers. :param message: - The message from the remote agent. :return: """ old_value = self.value if message.body == Absent.get_absent(): self.value = Value.absent() elif isinstance(message.body, (Text, Num, Bool)): self.value = message.body else: converter = RecordConverter.get_converter() self.value = converter.record_to_object( message.body, self.downlink_manager.registered_classes, self.downlink_manager.strict) await self.downlink_manager.subscribers_did_set(self.value, old_value)
def value(self) -> 'Any': if self.model is None: return Value.absent() else: return self.model.value
def __init__(self, node_uri: str, lane_uri: str, body: _Item = Value.absent()) -> None: super().__init__(node_uri, lane_uri, tag='event', form=_EventMessageForm(), body=body)
def __init__(self, client: 'SwimClient') -> None: super().__init__(client) self.value = Value.absent() self.synced = asyncio.Event()
def _create_attr(key: Any, value: Any = Value.extant()) -> 'Attr': return Attr.create_attr(key, value)
# Copyright 2015-2020 SWIM.AI inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import time from swimai import SwimClient from swimai.structures import Value if __name__ == '__main__': with SwimClient() as swim_client: host_uri = 'ws://localhost:9001' node_uri = '/unit/foo' for i in range(0, 10): swim_client.command(host_uri, node_uri, 'publish', Value.absent()) time.sleep(5) print('Stopping the client in 2 seconds') time.sleep(2)
def __init__(self, node_uri: str, lane_uri: str, prio: float, rate: float, tag: str, form: '_Form', body: _Item = Value.absent()) -> None: super().__init__(node_uri, lane_uri, tag, form, body) self._prio = prio self._rate = rate
def __init__(self, node_uri: str, lane_uri: str, tag: str, form: '_Form', body=Value.absent()) -> None: super().__init__(node_uri, lane_uri, tag, form, body)
def __init__(self, node_uri: str, lane_uri: str, prio: float = 0.0, rate: float = 0.0, body: _Item = Value.absent()) -> None: super().__init__(node_uri, lane_uri, prio, rate, tag='sync', form=_SyncRequestForm(), body=body)
host_uri = 'warp://localhost:9001' building_uri = '/building/swim' first_room_uri = '/swim/1' second_room_uri = '/swim/2' third_room_uri = '/swim/3' map_downlink = swim_client.downlink_map() map_downlink.set_host_uri(host_uri) map_downlink.set_node_uri(building_uri) map_downlink.set_lane_uri('lights') map_downlink.did_update(custom_did_update) map_downlink.open() time.sleep(2) swim_client.command(host_uri, first_room_uri, "toggleLights", Value.absent()) swim_client.command(host_uri, second_room_uri, "toggleLights", Value.absent()) swim_client.command(host_uri, third_room_uri, "toggleLights", Value.absent()) swim_client.command(host_uri, second_room_uri, "toggleLights", Value.absent()) swim_client.command(host_uri, second_room_uri, "toggleLights", Value.absent()) swim_client.command(host_uri, third_room_uri, "toggleLights", Value.absent()) print('Stopping the client in 2 seconds') time.sleep(2)
def __init__(self, node_uri: str, lane_uri: str, body: _Item = Value.absent()) -> None: super().__init__(node_uri, lane_uri, tag='synced', form=_SyncedResponseForm(), body=body)
def __init__(self, node_uri: str, lane_uri: str, body: Item = Value.absent()) -> None: super().__init__(node_uri, lane_uri, tag='command', form=CommandMessageForm(), body=body)
def __init__(self, node_uri: str, lane_uri: str, tag: str, form: '_Form', body: _Item = Value.absent()) -> None: self._node_uri = node_uri self._lane_uri = lane_uri self._tag = tag self._form = form self._body = body
def __init__(self, node_uri: str, lane_uri: str, tag: str, form: 'Form', body: Item = Value.absent()) -> None: self.node_uri = node_uri self.lane_uri = lane_uri self.tag = tag self.form = form self.body = body
def __init__(self, node_uri: str, lane_uri: str, prio: float = 0.0, rate: float = 0.0, body: _Item = Value.absent()) -> None: super().__init__(node_uri, lane_uri, prio, rate, tag='unlinked', form=_UnlinkedResponseForm(), body=body)