Ejemplo n.º 1
0
 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())
Ejemplo n.º 2
0
    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]
Ejemplo n.º 3
0
 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]
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
    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()
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
0
    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]
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 def value(self) -> 'Any':
     if self.model is None:
         return Value.absent()
     else:
         return self.model.value
Ejemplo n.º 14
0
 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)
Ejemplo n.º 15
0
 def __init__(self, client: 'SwimClient') -> None:
     super().__init__(client)
     self.value = Value.absent()
     self.synced = asyncio.Event()
Ejemplo n.º 16
0
 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)
Ejemplo n.º 17
0
 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
Ejemplo n.º 18
0
 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
Ejemplo n.º 19
0
 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)
Ejemplo n.º 20
0
 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)
Ejemplo n.º 21
0
        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)
Ejemplo n.º 22
0
 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
Ejemplo n.º 23
0
#  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)
Ejemplo n.º 24
0
 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)