device = DefaultDevice() device.load_from_config(config) print("Starting") while True: if not connected: try: sio.connect(url) print("Connected and working") connected = True except ConnectionError as e: print(e) print(f"Connection to {url} failed, try again after delay") connected = False try: start = time.time() data = device.get_sensor_values() print(data) if connected: print("Sending", data) sio.emit("new_data", {"data": data}, namespace="/sensor") end = time.time() duration = end - start waittime = max(0, delay - duration) sio.sleep(waittime) except BadNamespaceError: print("Namespace error, probably the hub is down. Keep on trying")
def test_automatic_mock_event_emission( server_url_fixture: str, mock_client_wait_timeout: float, mock_client_wait_interval: float, client: socketio.Client, request: pytest.FixtureRequest, ): server_url: str = request.getfixturevalue(server_url_fixture) new_message_event = "new message" new_message_mock_ack = Mock() @client.on(new_message_event) def _new_message_handler(data): jsonschema.validate(data, { "username": { "type": "string" }, "message": { "type": "string" } }) # Assert that message is of sentence format: assert data["message"].endswith(".") assert " " in data["message"] # Assert that username is a first name: assert data["username"].istitle() new_message_mock_ack(new_message_event) typing_event = "typing" typing_mock_ack = Mock() @client.on(typing_event) def _typing_handler(data): jsonschema.validate(data, {"username": {"type": "string"}}) # Assert that username is a first name: assert data["username"].istitle() typing_mock_ack(typing_event) user_joined_event = "user joined" user_joined_mock_ack = Mock() @client.on(user_joined_event) def _user_joined_handler(data): jsonschema.validate(data, { "username": { "type": "string" }, "numUsers": { "type": "integer" } }) # Assert that username is a first name: assert data["username"].istitle() user_joined_mock_ack(user_joined_event) client.connect(server_url, wait_timeout=mock_client_wait_timeout) # Wait for all messages to arrive: client.sleep(mock_client_wait_interval) new_message_mock_ack.assert_called_with(new_message_event) typing_mock_ack.assert_called_with(typing_event) user_joined_mock_ack.assert_called_with(user_joined_event)