Exemplo n.º 1
0
def all_protocols_servient():
    """Returns a Servient configured to use all available protocol bindings."""

    servient = Servient(catalogue_port=None)

    http_port = find_free_port()
    http_server = HTTPServer(port=http_port)
    servient.add_server(http_server)

    ws_port = find_free_port()
    ws_server = WebsocketServer(port=ws_port)
    servient.add_server(ws_server)

    if is_coap_supported():
        from wotpy.protocols.coap.server import CoAPServer
        coap_port = find_free_port()
        coap_server = CoAPServer(port=coap_port)
        servient.add_server(coap_server)

    if is_mqtt_supported():
        from wotpy.protocols.mqtt.server import MQTTServer
        from tests.protocols.mqtt.broker import get_test_broker_url, is_test_broker_online
        if is_test_broker_online():
            mqtt_server = MQTTServer(broker_url=get_test_broker_url())
            servient.add_server(mqtt_server)

    @tornado.gen.coroutine
    def start():
        raise tornado.gen.Return((yield servient.start()))

    wot = tornado.ioloop.IOLoop.current().run_sync(start)

    td_dict = {
        "id": uuid.uuid4().urn,
        "title": uuid.uuid4().hex,
        "properties": {
            uuid.uuid4().hex: {
                "observable": True,
                "type": "string"
            }
        }
    }

    td = ThingDescription(td_dict)

    exposed_thing = wot.produce(td.to_str())
    exposed_thing.expose()

    yield servient

    @tornado.gen.coroutine
    def shutdown():
        yield servient.shutdown()

    tornado.ioloop.IOLoop.current().run_sync(shutdown)
Exemplo n.º 2
0
def test_all_protocols_combined(all_protocols_servient):
    """Protocol bindings work as expected when multiple
    servers are combined within the same Servient."""

    exposed_thing = next(all_protocols_servient.exposed_things)
    td = ThingDescription.from_thing(exposed_thing.thing)

    clients = [WebsocketClient(), HTTPClient()]

    if is_coap_supported():
        from wotpy.protocols.coap.client import CoAPClient
        clients.append(CoAPClient())

    if is_mqtt_supported():
        from tests.protocols.mqtt.broker import is_test_broker_online
        from wotpy.protocols.mqtt.client import MQTTClient
        if is_test_broker_online():
            clients.append(MQTTClient())

    prop_name = next(six.iterkeys(td.properties))

    @tornado.gen.coroutine
    def read_property(the_client):
        prop_value = Faker().sentence()

        curr_value = yield the_client.read_property(td, prop_name)
        assert curr_value != prop_value

        yield exposed_thing.properties[prop_name].write(prop_value)

        curr_value = yield the_client.read_property(td, prop_name)
        assert curr_value == prop_value

    @tornado.gen.coroutine
    def write_property(the_client):
        updated_value = Faker().sentence()

        curr_value = yield exposed_thing.properties[prop_name].read()
        assert curr_value != updated_value

        yield the_client.write_property(td, prop_name, updated_value)

        curr_value = yield exposed_thing.properties[prop_name].read()
        assert curr_value == updated_value

    @tornado.gen.coroutine
    def test_coroutine():
        for client in clients:
            yield read_property(client)
            yield write_property(client)

    run_test_coroutine(test_coroutine)
Exemplo n.º 3
0
    def _build_default_clients(self):
        """Builds the default Protocol Binding clients."""

        self._clients = self._clients if self._clients else {}

        conf = self._clients_config if self._clients_config else {}

        self._clients.update({
            Protocols.WEBSOCKETS: WebsocketClient(**conf.get(Protocols.WEBSOCKETS, {})),
            Protocols.HTTP: HTTPClient(**conf.get(Protocols.HTTP, {}))
        })

        if is_coap_supported():
            from wotpy.protocols.coap.client import CoAPClient
            self._clients.update(
                {Protocols.COAP: CoAPClient(**conf.get(Protocols.COAP, {}))})

        if is_mqtt_supported():
            from wotpy.protocols.mqtt.client import MQTTClient
            self._clients.update(
                {Protocols.MQTT: MQTTClient(**conf.get(Protocols.MQTT, {}))})
Exemplo n.º 4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
MQTT Protocol Binding implementation.

.. autosummary::
    :toctree: _mqtt

    wotpy.protocols.mqtt.handlers
    wotpy.protocols.mqtt.client
    wotpy.protocols.mqtt.enums
    wotpy.protocols.mqtt.runner
    wotpy.protocols.mqtt.server
"""

from wotpy.support import is_mqtt_supported

if is_mqtt_supported() is False:
    raise NotImplementedError("MQTT binding is not supported in this platform")
Exemplo n.º 5
0
test_requires = [
    'pytest>=3.6.1,<4.0.0', 'pytest-cov>=2.5.1,<2.6.0',
    'pytest-rerunfailures>=4.1,<5.0', 'mock>=2.0,<3.0', 'tox>=3.0,<4.0',
    'faker>=0.8.15,<0.9', 'Sphinx>=1.7.5,<2.0.0',
    'sphinx-rtd-theme>=0.4.0,<0.5.0', 'futures>=3.1.1,<4.0.0',
    'pyOpenSSL>=18.0.0,<19.0.0', 'coveralls>=1.0,<2.0', 'coverage>=5.0<6.0',
    'autopep8>=1.4,<2.0', 'rope>=0.14.0,<1.0'
]

if sys.version_info[0] is 3:
    test_requires.append("bump2version>=1.0,<2.0")

if is_coap_supported():
    install_requires.append('aiocoap[linkheader]==0.4a1')

if is_mqtt_supported():
    install_requires.append('hbmqtt>=0.9.4,<1.0')

if is_dnssd_supported():
    install_requires.append('zeroconf>=0.21.3,<0.22.0')
    test_requires.append('aiozeroconf==0.1.8')

this_dir = path.abspath(path.dirname(__file__))

with open(path.join(this_dir, 'README.md')) as fh:
    long_description = fh.read()

setup(name='wotpy',
      version=__version__,
      description=
      'Python implementation of a W3C WoT Runtime and the WoT Scripting API',
Exemplo n.º 6
0
import pytest
import tornado.gen
import tornado.ioloop
from faker import Faker

from wotpy.support import is_mqtt_supported
from wotpy.wot.dictionaries.interaction import ActionFragmentDict, EventFragmentDict, PropertyFragmentDict
from wotpy.wot.exposed.thing import ExposedThing
from wotpy.wot.servient import Servient
from wotpy.wot.td import ThingDescription
from wotpy.wot.thing import Thing

collect_ignore = []

if not is_mqtt_supported():
    logging.warning("Skipping MQTT tests due to unsupported platform")
    collect_ignore += ["test_server.py", "test_client.py"]


@pytest.fixture(params=[{"property_callback_ms": None}])
def mqtt_server(request):
    """Builds a MQTTServer instance that contains an ExposedThing."""

    from wotpy.protocols.mqtt.server import MQTTServer
    from tests.protocols.mqtt.broker import get_test_broker_url

    exposed_thing = ExposedThing(servient=Servient(),
                                 thing=Thing(id=uuid.uuid4().urn))

    exposed_thing.add_property(uuid.uuid4().hex,