From 76dd25fe6f31ec1fe75af348f561913d4df1c564 Mon Sep 17 00:00:00 2001 From: Nikolay Korotkiy Date: Fri, 18 Nov 2022 17:52:37 +0400 Subject: [PATCH] Enhance test client (#3) --- .gitignore | 6 +++ README.md | 1 + bin/mqtt-rpc-client | 74 ++++++++++++++++++++++++++++++++++ debian/changelog | 6 +++ debian/control | 2 +- debian/python3-mqttrpc.install | 1 + mqttrpc/client.py | 3 +- test_client.py | 68 ------------------------------- 8 files changed, 90 insertions(+), 71 deletions(-) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 bin/mqtt-rpc-client create mode 100644 debian/python3-mqttrpc.install delete mode 100644 test_client.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbdfe5e --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +### Python ### +*.egg-info/ + +### direnv ### +.direnv +.envrc diff --git a/README.md b/README.md new file mode 100644 index 0000000..c1152f4 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Reference [MQTT-RPC](https://github.com/wirenboard/mqtt-rpc) protocol implementation. diff --git a/bin/mqtt-rpc-client b/bin/mqtt-rpc-client new file mode 100644 index 0000000..11fe048 --- /dev/null +++ b/bin/mqtt-rpc-client @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import os +import json, time +import pprint +import argparse + +from urllib.parse import urlparse +from paho.mqtt import client as mqttclient +from mqttrpc.client import TMQTTRPCClient +from jsonrpc.exceptions import JSONRPCError +import paho_socket + + +def main(): + parser = argparse.ArgumentParser(description="Sample RPC client", add_help=False) + parser.add_argument( + "-b", "--broker_url", dest="broker_url", type=str, help="MQTT url", default="unix:///var/run/mosquitto/mosquitto.sock" + ) + parser.add_argument( + "-d", + "--driver", + dest="driver", + type=str, + help="Driver name" + ) + parser.add_argument( + "-s", + "--service", + dest="service", + type=str, + help="Service name" + ) + parser.add_argument( + "-m", + "--method", + dest="method", + type=str, + help="Method name" + ) + parser.add_argument( + "-a", "--args", dest="args", type=json.loads, help="Method arguments" + ) + parser.add_argument( + "-t", "--timeout", dest="timeout", type=int, help="Timeout", default=10 + ) + args = parser.parse_args() + + url = urlparse(args.broker_url) + client_id = "mqtt-rpc-client-%d" % os.getpid() + if url.scheme == "mqtt-tcp": + client = mqttclient.Client(client_id) + if url.username: + client.username_pw_set(url.username, url.password) + client.connect(url.hostname, url.port) + elif url.scheme == "unix": + client = paho_socket.Client(client_id) + client.sock_connect(url.path) + else: + print("Unkown mqtt url scheme") + exit(1) + client.loop_start() + + rpc_client = TMQTTRPCClient(client) + client.on_message = rpc_client.on_mqtt_message + + resp = rpc_client.call( + args.driver, args.service, args.method, args.args, args.timeout + ) + pprint.pprint(resp) + + +if __name__ == "__main__": + main() diff --git a/debian/changelog b/debian/changelog index ed5ede5..0367280 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +python-mqttrpc (1.1.4) stable; urgency=medium + + * Add simple client to installation + + -- Nikolay Korotkiy Fri, 04 Nov 2022 21:13:00 +0300 + python-mqttrpc (1.1.3) stable; urgency=medium * add compatibility with python 3.10 diff --git a/debian/control b/debian/control index 0194100..e5ced55 100644 --- a/debian/control +++ b/debian/control @@ -9,5 +9,5 @@ X-Python3-Version: >= 3.5 Package: python3-mqttrpc Architecture: all XB-Python-Version: ${python3:Version} -Depends: python3, ${misc:Depends}, ${shlibs:Depends}, python3-json-rpc +Depends: python3, ${misc:Depends}, ${shlibs:Depends}, python3-json-rpc, python3-paho-mqtt, python3-paho-socket, python3-mqttrpc Description: Reference MQTT-RPC implementation diff --git a/debian/python3-mqttrpc.install b/debian/python3-mqttrpc.install new file mode 100644 index 0000000..6fc2913 --- /dev/null +++ b/debian/python3-mqttrpc.install @@ -0,0 +1 @@ +bin/mqtt-rpc-client /usr/bin diff --git a/mqttrpc/client.py b/mqttrpc/client.py index 90c2101..0b828d7 100644 --- a/mqttrpc/client.py +++ b/mqttrpc/client.py @@ -1,5 +1,4 @@ import json -import six try: import mosquitto @@ -62,7 +61,7 @@ def __init__(self, client): self.counter = 0 self.futures = {} self.subscribes = set() - if six.PY3 and type(self.client._client_id) is bytes: + if type(self.client._client_id) is bytes: self.rpc_client_id = self.client._client_id.decode().replace('/','_') else: self.rpc_client_id = str(self.client._client_id).replace('/','_') diff --git a/test_client.py b/test_client.py deleted file mode 100644 index c976fe0..0000000 --- a/test_client.py +++ /dev/null @@ -1,68 +0,0 @@ -import json, time -import pprint -import argparse - - -try: - import mosquitto -except ImportError: - import paho.mqtt.client as mosquitto - - -from mqttrpc.client import TMQTTRPCClient -from jsonrpc.exceptions import JSONRPCError - - -def main(): - parser = argparse.ArgumentParser(description='Sample RPC client', add_help=False) - - parser.add_argument('-h', '--host', dest='host', type=str, - help='MQTT host', default='localhost') - - parser.add_argument('-u', '--username', dest='username', type=str, - help='MQTT username', default='') - - parser.add_argument('-P', '--password', dest='password', type=str, - help='MQTT password', default='') - - parser.add_argument('-p', '--port', dest='port', type=int, - help='MQTT port', default='1883') - - args = parser.parse_args() - client = mosquitto.Mosquitto() - - if args.username: - client.username_pw_set(args.username, args.password) - - client.connect(args.host, args.port) - client.loop_start() - - rpc_client = TMQTTRPCClient(client) - client.on_message = rpc_client.on_mqtt_message - - #~ resp = rpc_client.call('Driver', 'main', 'foobar', {'foo':'foo', 'bar':'bar'}) - - for i in xrange(10): - resp = rpc_client.call('db_logger', 'history', 'get_values', { - 'channels': [ - [ 'wb-w1', '00-1234566789' ], - [ 'wb-w1', '00' ], - [ 'wb-adc', 'Vin'], - ], - - 'timestamp' : { - 'gt': 1434728034 - }, - 'limit' : 60 - }, 10) - - print "got result!" - pprint.pprint(resp) - time.sleep(5) - - - - - -if __name__ == "__main__": - main()