Skip to content

Commit 317bcc0

Browse files
authored
Merge pull request #1 from myDevicesIoT/feature/testclient
Added script for testing client library.
2 parents f73b0ed + db17588 commit 317bcc0

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

cayenne/client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,23 @@ class CayenneMQTTClient:
124124
connected = False
125125
reconnect = False
126126
on_message = None
127-
128-
def begin(self, username, password, clientid):
127+
128+
def begin(self, username, password, clientid, hostname='mqtt.mydevices.com', port=1883):
129129
"""Initializes the client and connects to Cayenne.
130130
131131
username is the Cayenne username.
132132
password is the Cayenne password.
133133
clientID is the Cayennne client ID for the device.
134+
hostname is the MQTT broker hostname.
135+
port is the MQTT broker port.
134136
"""
135137
self.rootTopic = "v1/%s/things/%s" % (username, clientid)
136138
self.client = mqtt.Client(client_id=clientid, clean_session=True, userdata=self)
137139
self.client.on_connect = on_connect
138140
self.client.on_disconnect = on_disconnect
139141
self.client.on_message = on_message
140142
self.client.username_pw_set(username, password)
141-
hostname = "mqtt.mydevices.com"
142-
self.client.connect(hostname, 1883, 60)
143+
self.client.connect(hostname, port, 60)
143144
print("Connecting to %s..." % hostname)
144145

145146
def loop(self):

tests/Test-01-TestClient.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python
2+
import cayenne.client
3+
import time
4+
import argparse
5+
import sys
6+
import traceback
7+
8+
9+
parser = argparse.ArgumentParser(description='Cayenne MQTT Test.')
10+
parser.add_argument('--host', help='hostname', default='mqtt.mydevices.com')
11+
parser.add_argument('--port', help='port', default=1883)
12+
parser.add_argument('--username', help='username', default='username')
13+
parser.add_argument('--password', help='password', default='password')
14+
parser.add_argument('--clientID', help='clientID', default='clientID')
15+
16+
args = parser.parse_args()
17+
print args
18+
19+
try:
20+
done = False
21+
# The callback for when a message is received from Cayenne.
22+
def on_message(message):
23+
if message.msg_id == "senderror":
24+
# Test sending an error string.
25+
return "error response"
26+
if message.msg_id == "done":
27+
#The "done" message should be the last message so we set the done flag
28+
global done
29+
done = True
30+
31+
client = cayenne.client.CayenneMQTTClient()
32+
client.on_message = on_message
33+
client.begin(args.username, args.password, args.clientID, args.host, args.port)
34+
start = time.time()
35+
while not client.connected:
36+
client.loop()
37+
38+
print("Test publishing data")
39+
client.virtualWrite(0, 0)
40+
client.celsiusWrite(1, 1)
41+
client.fahrenheitWrite(2, 2)
42+
client.kelvinWrite(3, 3)
43+
client.luxWrite(4, 4)
44+
client.pascalWrite(5, 5)
45+
client.hectoPascalWrite(6, 6)
46+
47+
print("Test receiving commands")
48+
client.mqttPublish(client.rootTopic + '/cmd/10', 'senderror,0')
49+
client.mqttPublish(client.rootTopic + '/cmd/11', 'sendok,1')
50+
client.mqttPublish(client.rootTopic + '/cmd/12', 'done,1')
51+
52+
start = time.time()
53+
while True:
54+
loop_start = time.time()
55+
client.loop()
56+
if done and (time.time() - loop_start >= 1):
57+
break
58+
if (time.time() - start >= 10):
59+
raise Exception("Timed out while waiting for commands")
60+
61+
except:
62+
print(str(traceback.format_exc()))
63+
print('Tests failed with exception.')
64+
sys.exit(1)
65+
66+
print('Tests finished without errors')
67+
sys.exit(0)
68+

0 commit comments

Comments
 (0)