Skip to content
This repository was archived by the owner on Aug 4, 2020. It is now read-only.

Commit b03f35e

Browse files
committed
Merge branch 'master' into xiPy_preparation
* master: [feat] added pi estimation for pub demo [fix] added .gitignore for tmp files and folders [feat] added separate version for pub and sub operations # Conflicts: # .gitignore
2 parents fd98594 + a1e76ac commit b03f35e

File tree

3 files changed

+124
-19
lines changed

3 files changed

+124
-19
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.pyc
1+
*.pyc
2+
.ropeproject/

xively_pub.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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)

client.py xively_sub.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def on_connect_finished(client,result):
3939
connect_try_number = next(get_connect_try_number)
4040
except StopIteration:
4141
print("Couldnt connect to the endpoint in %d tries, shutting down." % retrys_number)
42-
sys.exit( -1 )
42+
sys.exit(-1)
4343
print("Connection try %d/%d" % (connect_try_number, retrys_number))
4444
print("Connection error :" , result)
4545
print("Reconnecting to the broker ... ")
@@ -49,30 +49,15 @@ def on_disconnect_finished(client,result):
4949
print("on_disconnect_finished",result)
5050

5151

52-
def on_publish_finished(client,message):
53-
print("on_publish_finished")
54-
55-
5652
def on_subscribe_finished(client,mid,granted_qos):
5753
global test_topic
5854

5955
print("on_subscribe_finished " )
60-
print( "publishing to topic" )
61-
client.publish( test_topic, "Hello through xively!!!", 0 , False )
62-
63-
64-
def on_unsubscribe_finished(client,mid):
65-
print("on_unsubscribe_finished")
66-
print( "disconnecting" )
67-
client.disconnect()
68-
6956

7057
def on_message_received(client,message):
7158
global test_topic
7259

7360
print("on_message_received",str(message))
74-
print( "unsubscribing from topic" )
75-
client.unsubscribe( test_topic )
7661

7762
def u2a( data ):
7863
return str( codecs.decode( codecs.encode( data, 'ascii', 'ignore' ), 'ascii', 'ignore' ) )
@@ -82,9 +67,7 @@ def u2a( data ):
8267
client = XivelyClient()
8368
client.on_connect_finished = on_connect_finished
8469
client.on_disconnect_finished = on_disconnect_finished
85-
client.on_publish_finished = on_publish_finished
8670
client.on_subscribe_finished = on_subscribe_finished
87-
client.on_unsubscribe_finished = on_unsubscribe_finished
8871
client.on_message_received = on_message_received
8972

9073
params = XivelyConnectionParameters()

0 commit comments

Comments
 (0)