|
| 1 | +#!/usr/bin/python |
| 2 | +# Copyright (c) 2003-2015, LogMeIn, Inc. All rights reserved. |
| 3 | +# This is part of Xively Python library. |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import codecs |
| 7 | +import time |
| 8 | +import random |
| 9 | +import math |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +from xi_client.xively_client import XivelyClient |
| 13 | +from xi_client.xively_connection_parameters import XivelyConnectionParameters |
| 14 | +from xi_client.xively_error_codes import XivelyErrorCodes as xec |
| 15 | + |
| 16 | +credentialsfilepath = os.path.expanduser('~/Desktop/MQTTCredentials.txt') |
| 17 | +topicfilepath = os.path.expanduser('~/Desktop/MQTTTopic.txt') |
| 18 | + |
| 19 | +retrys_number = 3 |
| 20 | +username = None |
| 21 | +password = None |
| 22 | +test_topic = None |
| 23 | + |
| 24 | +def pi_gen(): |
| 25 | + iteration = 0 |
| 26 | + count_inside = 0 |
| 27 | + |
| 28 | + while True: |
| 29 | + for i in range(0, 100): |
| 30 | + iteration += 1 |
| 31 | + |
| 32 | + d = math.hypot( random.random(), random.random() ) |
| 33 | + |
| 34 | + if d < 1: |
| 35 | + count_inside += 1 |
| 36 | + |
| 37 | + yield iteration, str( 4.0 * count_inside / iteration ) |
| 38 | + |
| 39 | +def retry_number_gen(): |
| 40 | + try_number = 0 |
| 41 | + |
| 42 | + while try_number < retrys_number: |
| 43 | + try_number += 1 |
| 44 | + yield try_number |
| 45 | + |
| 46 | +get_connect_try_number = retry_number_gen() |
| 47 | +get_pi_evaluation = pi_gen() |
| 48 | + |
| 49 | +def publish_message(client, topic): |
| 50 | + iterations, value = next(get_pi_evaluation) |
| 51 | + message = "Hello through Xively with pi estimation after iteration: " \ |
| 52 | + + str(iterations) + " value: " + value + " and timestamp = " \ |
| 53 | + + str(datetime.now()) + "!!!" |
| 54 | + client.publish(topic, message, 0, False) |
| 55 | + |
| 56 | +def on_connect_finished(client,result): |
| 57 | + print("on_connect_finished",str(result)) |
| 58 | + |
| 59 | + if result == xec.XI_STATE_OK : |
| 60 | + print( "connected, starting to publish" ) |
| 61 | + publish_message(client, test_topic) |
| 62 | + |
| 63 | + else : |
| 64 | + try: |
| 65 | + connect_try_number = next(get_connect_try_number) |
| 66 | + except StopIteration: |
| 67 | + print("Couldnt connect to the endpoint in %d tries, shutting down." % retrys_number) |
| 68 | + sys.exit(-1) |
| 69 | + print("Connection try %d/%d" % (connect_try_number, retrys_number)) |
| 70 | + print("Connection error :" , result) |
| 71 | + print("Reconnecting to the broker ... ") |
| 72 | + client.connect(params) |
| 73 | + |
| 74 | +def on_disconnect_finished(client,result): |
| 75 | + print("on_disconnect_finished",result) |
| 76 | + |
| 77 | +def on_publish_finished(client,message): |
| 78 | + print("on_publish_finished") |
| 79 | + time.sleep( 1 ) |
| 80 | + publish_message(client, test_topic) |
| 81 | + |
| 82 | +def u2a( data ): |
| 83 | + return str( codecs.decode( codecs.encode( data, 'ascii', 'ignore' ), 'ascii', 'ignore' ) ) |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + doExit = False |
| 87 | + client = XivelyClient() |
| 88 | + client.on_connect_finished = on_connect_finished |
| 89 | + client.on_disconnect_finished = on_disconnect_finished |
| 90 | + client.on_publish_finished = on_publish_finished |
| 91 | + |
| 92 | + params = XivelyConnectionParameters() |
| 93 | + |
| 94 | + try: |
| 95 | + with codecs.open(credentialsfilepath, 'r', encoding='utf8') as credsfile: |
| 96 | + password = u2a(credsfile.readline().rstrip('\n')) |
| 97 | + username = u2a(credsfile.readline().rstrip('\n')) |
| 98 | + except (IOError, OSError) as e: |
| 99 | + print( e ) |
| 100 | + sys.exit( 0 ) |
| 101 | + |
| 102 | + print( "Read username and password from the file %s" %(credentialsfilepath)) |
| 103 | + print ( "Username = %s" %(username)) |
| 104 | + print ( "Password = %s" %(password)) |
| 105 | + |
| 106 | + try: |
| 107 | + with codecs.open(topicfilepath, 'r', encoding='UTF-8') as topicfile: |
| 108 | + test_topic = u2a(topicfile.readline().rstrip('\n')) |
| 109 | + except (IOError, OSError) as e: |
| 110 | + print( e ) |
| 111 | + sys.exit( 0 ) |
| 112 | + |
| 113 | + print( "Read topic name from the file %s" %(topicfilepath)) |
| 114 | + print ( "Topic = %s" %(test_topic)) |
| 115 | + |
| 116 | + params.client_id = username |
| 117 | + params.username = username |
| 118 | + params.password = password |
| 119 | + |
| 120 | + print( "Connecting to the correct broker ... ") |
| 121 | + client.connect(params) |
0 commit comments