def test_servient_id(): """An MQTT server may be identified by a unique Servient ID to avoid topic collisions.""" broker_url = get_test_broker_url() mqtt_srv_01 = MQTTServer(broker_url=broker_url) mqtt_srv_02 = MQTTServer(broker_url=broker_url) mqtt_srv_03 = MQTTServer(broker_url=broker_url, servient_id=Faker().pystr()) assert mqtt_srv_01.servient_id and mqtt_srv_02.servient_id and mqtt_srv_03.servient_id assert mqtt_srv_01.servient_id == mqtt_srv_02.servient_id assert mqtt_srv_01.servient_id != mqtt_srv_03.servient_id @tornado.gen.coroutine def assert_ping_loop(srv, num_iters=10): for _ in range(num_iters): assert (yield _ping(srv, timeout=DEFAULT_PING_TIMEOUT)) yield tornado.gen.sleep(random.uniform(0.1, 0.3)) @tornado.gen.coroutine def test_coroutine(): yield [mqtt_srv_01.start(), mqtt_srv_03.start()] yield [assert_ping_loop(mqtt_srv_01), assert_ping_loop(mqtt_srv_03)] run_test_coroutine(test_coroutine)
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, PropertyFragmentDict({ "type": "string", "observable": True }), value=Faker().sentence()) exposed_thing.add_event(uuid.uuid4().hex, EventFragmentDict({"type": "number"})) action_name = uuid.uuid4().hex @tornado.gen.coroutine def handler(parameters): input_value = parameters.get("input") yield tornado.gen.sleep(random.random() * 0.1) raise tornado.gen.Return("{:f}".format(input_value)) exposed_thing.add_action( action_name, ActionFragmentDict({ "input": { "type": "number" }, "output": { "type": "string" } }), handler) server = MQTTServer(broker_url=get_test_broker_url(), **request.param) server.add_exposed_thing(exposed_thing) @tornado.gen.coroutine def start(): yield server.start() tornado.ioloop.IOLoop.current().run_sync(start) yield server @tornado.gen.coroutine def stop(): yield server.stop() tornado.ioloop.IOLoop.current().run_sync(stop)
async def main(): """Main entrypoint.""" LOGGER.info("Creating WebSocket server on: {}".format(PORT_WS)) ws_server = WebsocketServer(port=PORT_WS) LOGGER.info("Creating HTTP server on: {}".format(PORT_HTTP)) http_server = HTTPServer(port=PORT_HTTP) LOGGER.info("Creating MQTT server on broker: {}".format(MQTT_BROKER)) mqtt_server = MQTTServer(MQTT_BROKER) LOGGER.info( "Creating servient with TD catalogue on: {}".format(PORT_CATALOGUE)) servient = Servient(catalogue_port=PORT_CATALOGUE) servient.add_server(ws_server) servient.add_server(http_server) servient.add_server(mqtt_server) LOGGER.info("Starting servient") wot = await servient.start() LOGGER.info("Exposing System Monitor Thing") exposed_thing = wot.produce(json.dumps(DESCRIPTION)) exposed_thing.set_property_read_handler("cpuPercent", cpu_percent_handler) exposed_thing.properties["cpuThreshold"].write(DEFAULT_CPU_THRESHOLD) exposed_thing.expose() create_cpu_check_task(exposed_thing)
def test_start_stop(): """The MQTT server may be started and stopped.""" mqtt_server = MQTTServer(broker_url=get_test_broker_url()) @tornado.gen.coroutine def test_coroutine(): assert not (yield _ping(mqtt_server, timeout=DEFAULT_PING_TIMEOUT)) yield mqtt_server.start() assert (yield _ping(mqtt_server)) assert (yield _ping(mqtt_server)) yield mqtt_server.stop() yield mqtt_server.start() yield mqtt_server.stop() assert not (yield _ping(mqtt_server, timeout=DEFAULT_PING_TIMEOUT)) yield mqtt_server.stop() yield mqtt_server.start() yield mqtt_server.start() assert (yield _ping(mqtt_server)) run_test_coroutine(test_coroutine)
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)
def build_servient(parsed_args, clients_config=None): """Factory function to build a Servient with a set of servers depending on the input arguments.""" logger = logging.getLogger() logger.info("Creating servient with TD catalogue on: {}".format( parsed_args.port_catalogue)) servient = Servient(catalogue_port=parsed_args.port_catalogue, hostname=parsed_args.hostname, clients_config=clients_config) if parsed_args.port_ws > 0: logger.info("Creating WebSocket server on: {}".format( parsed_args.port_ws)) servient.add_server(WebsocketServer(port=parsed_args.port_ws)) if parsed_args.port_http > 0: logger.info("Creating HTTP server on: {}".format( parsed_args.port_http)) servient.add_server(HTTPServer(port=parsed_args.port_http)) if parsed_args.mqtt_broker: try: from wotpy.protocols.mqtt.server import MQTTServer logger.info("Creating MQTT server on broker: {}".format( parsed_args.mqtt_broker)) mqtt_server = MQTTServer(parsed_args.mqtt_broker, servient_id=servient.hostname) servient.add_server(mqtt_server) logger.info("MQTT server created with ID: {}".format( mqtt_server.servient_id)) except NotImplementedError as ex: logger.warning(ex) if parsed_args.port_coap > 0: try: from wotpy.protocols.coap.server import CoAPServer logger.info("Creating CoAP server on: {}".format( parsed_args.port_coap)) servient.add_server(CoAPServer(port=parsed_args.port_coap)) except NotImplementedError as ex: logger.warning(ex) return servient
def mqtt_servient(): """Returns a Servient that exposes a CoAP server and one ExposedThing.""" from wotpy.protocols.mqtt.server import MQTTServer from tests.protocols.mqtt.broker import get_test_broker_url server = MQTTServer(broker_url=get_test_broker_url()) servient = Servient(catalogue_port=None) servient.add_server(server) @tornado.gen.coroutine def start(): raise tornado.gen.Return((yield servient.start())) wot = tornado.ioloop.IOLoop.current().run_sync(start) property_name_01 = uuid.uuid4().hex action_name_01 = uuid.uuid4().hex event_name_01 = uuid.uuid4().hex td_dict = { "id": uuid.uuid4().urn, "name": uuid.uuid4().hex, "properties": { property_name_01: { "observable": True, "type": "string" } }, "actions": { action_name_01: { "input": { "type": "number" }, "output": { "type": "number" }, } }, "events": { event_name_01: { "type": "string" } }, } td = ThingDescription(td_dict) exposed_thing = wot.produce(td.to_str()) exposed_thing.expose() @tornado.gen.coroutine def action_handler(parameters): input_value = parameters.get("input") raise tornado.gen.Return(int(input_value) * 2) exposed_thing.set_action_handler(action_name_01, action_handler) yield servient @tornado.gen.coroutine def shutdown(): yield servient.shutdown() tornado.ioloop.IOLoop.current().run_sync(shutdown)