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

Commit

Permalink
Merge branch 'master' into xiPy_preparation
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
atigyi committed Jan 22, 2016
2 parents fd98594 + a1e76ac commit b03f35e
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.pyc
*.pyc
.ropeproject/
121 changes: 121 additions & 0 deletions xively_pub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/python
# Copyright (c) 2003-2015, LogMeIn, Inc. All rights reserved.
# This is part of Xively Python library.
import sys
import os
import codecs
import time
import random
import math
from datetime import datetime

from xi_client.xively_client import XivelyClient
from xi_client.xively_connection_parameters import XivelyConnectionParameters
from xi_client.xively_error_codes import XivelyErrorCodes as xec

credentialsfilepath = os.path.expanduser('~/Desktop/MQTTCredentials.txt')
topicfilepath = os.path.expanduser('~/Desktop/MQTTTopic.txt')

This comment has been minimized.

Copy link
@milgra

milgra Feb 8, 2016

do we want wired-in paths?

This comment has been minimized.

Copy link
@atigyi

atigyi Feb 8, 2016

Author

good catch, although this is not part of the final release, sorry for including other stuffs too.

The way this was implemented is: I've made changes on this repo (xively-getting-started) and initialised another repo xiPy (now xiPy-deprecated). And during this init I cut this code you just commented because is not necessary. Only the "core" py Xively is copied.


retrys_number = 3
username = None
password = None
test_topic = None

def pi_gen():
iteration = 0
count_inside = 0

while True:
for i in range(0, 100):
iteration += 1

d = math.hypot( random.random(), random.random() )

if d < 1:
count_inside += 1

yield iteration, str( 4.0 * count_inside / iteration )

def retry_number_gen():
try_number = 0

while try_number < retrys_number:
try_number += 1
yield try_number

get_connect_try_number = retry_number_gen()
get_pi_evaluation = pi_gen()

def publish_message(client, topic):
iterations, value = next(get_pi_evaluation)
message = "Hello through Xively with pi estimation after iteration: " \
+ str(iterations) + " value: " + value + " and timestamp = " \
+ str(datetime.now()) + "!!!"
client.publish(topic, message, 0, False)

def on_connect_finished(client,result):
print("on_connect_finished",str(result))

if result == xec.XI_STATE_OK :
print( "connected, starting to publish" )
publish_message(client, test_topic)

else :
try:
connect_try_number = next(get_connect_try_number)
except StopIteration:
print("Couldnt connect to the endpoint in %d tries, shutting down." % retrys_number)
sys.exit(-1)
print("Connection try %d/%d" % (connect_try_number, retrys_number))
print("Connection error :" , result)
print("Reconnecting to the broker ... ")
client.connect(params)

def on_disconnect_finished(client,result):
print("on_disconnect_finished",result)

def on_publish_finished(client,message):
print("on_publish_finished")
time.sleep( 1 )
publish_message(client, test_topic)

def u2a( data ):
return str( codecs.decode( codecs.encode( data, 'ascii', 'ignore' ), 'ascii', 'ignore' ) )

if __name__ == '__main__':
doExit = False
client = XivelyClient()
client.on_connect_finished = on_connect_finished
client.on_disconnect_finished = on_disconnect_finished
client.on_publish_finished = on_publish_finished

params = XivelyConnectionParameters()

try:
with codecs.open(credentialsfilepath, 'r', encoding='utf8') as credsfile:
password = u2a(credsfile.readline().rstrip('\n'))
username = u2a(credsfile.readline().rstrip('\n'))
except (IOError, OSError) as e:
print( e )
sys.exit( 0 )

print( "Read username and password from the file %s" %(credentialsfilepath))
print ( "Username = %s" %(username))
print ( "Password = %s" %(password))

try:
with codecs.open(topicfilepath, 'r', encoding='UTF-8') as topicfile:
test_topic = u2a(topicfile.readline().rstrip('\n'))
except (IOError, OSError) as e:
print( e )
sys.exit( 0 )

print( "Read topic name from the file %s" %(topicfilepath))
print ( "Topic = %s" %(test_topic))

params.client_id = username
params.username = username
params.password = password

print( "Connecting to the correct broker ... ")
client.connect(params)
19 changes: 1 addition & 18 deletions client.py → xively_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def on_connect_finished(client,result):
connect_try_number = next(get_connect_try_number)
except StopIteration:
print("Couldnt connect to the endpoint in %d tries, shutting down." % retrys_number)
sys.exit( -1 )
sys.exit(-1)
print("Connection try %d/%d" % (connect_try_number, retrys_number))
print("Connection error :" , result)
print("Reconnecting to the broker ... ")
Expand All @@ -49,30 +49,15 @@ def on_disconnect_finished(client,result):
print("on_disconnect_finished",result)


def on_publish_finished(client,message):
print("on_publish_finished")


def on_subscribe_finished(client,mid,granted_qos):
global test_topic

print("on_subscribe_finished " )
print( "publishing to topic" )
client.publish( test_topic, "Hello through xively!!!", 0 , False )


def on_unsubscribe_finished(client,mid):
print("on_unsubscribe_finished")
print( "disconnecting" )
client.disconnect()


def on_message_received(client,message):
global test_topic

print("on_message_received",str(message))
print( "unsubscribing from topic" )
client.unsubscribe( test_topic )

def u2a( data ):
return str( codecs.decode( codecs.encode( data, 'ascii', 'ignore' ), 'ascii', 'ignore' ) )
Expand All @@ -82,9 +67,7 @@ def u2a( data ):
client = XivelyClient()
client.on_connect_finished = on_connect_finished
client.on_disconnect_finished = on_disconnect_finished
client.on_publish_finished = on_publish_finished
client.on_subscribe_finished = on_subscribe_finished
client.on_unsubscribe_finished = on_unsubscribe_finished
client.on_message_received = on_message_received

params = XivelyConnectionParameters()
Expand Down

0 comments on commit b03f35e

Please sign in to comment.