|
| 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