Skip to content

Commit

Permalink
Enhance test client (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
sikmir authored Nov 18, 2022
1 parent 77daefd commit 76dd25f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 71 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Python ###
*.egg-info/

### direnv ###
.direnv
.envrc
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reference [MQTT-RPC](https://github.com/wirenboard/mqtt-rpc) protocol implementation.
74 changes: 74 additions & 0 deletions bin/mqtt-rpc-client
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
python-mqttrpc (1.1.4) stable; urgency=medium

* Add simple client to installation

-- Nikolay Korotkiy <nikolay.korotkiy@wirenboard.com> Fri, 04 Nov 2022 21:13:00 +0300

python-mqttrpc (1.1.3) stable; urgency=medium

* add compatibility with python 3.10
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions debian/python3-mqttrpc.install
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/mqtt-rpc-client /usr/bin
3 changes: 1 addition & 2 deletions mqttrpc/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import six

try:
import mosquitto
Expand Down Expand Up @@ -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('/','_')
Expand Down
68 changes: 0 additions & 68 deletions test_client.py

This file was deleted.

0 comments on commit 76dd25f

Please sign in to comment.