Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance test client #3

Merged
merged 15 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.