Receiving Data

Setting up a Receiver

Finally we have reached a stage where we are ready to receive all the valuable information. This can be set up on any device across the world, but in my case, since I have a spare RPi lying around, I will utilise this. You should just as easily be able to receive messages on another board, such as UDOO Dual Basic, or even a Windows PC.

Bear in mind that the receiving device only needs to receive messages and not files, as the messages will contain links to any files that have been uploaded to service bus. The files can be viewed from the browser and downloaded from there.

All that needs to be done is to write a python script that will continually receive messages, and log details of the received message onto a table.

Create a new python script in the scripts folder.

cd
cd scripts
sudo nano table_recv_cont.py
#!/usr/bin/python

# Steven Johnston www.johnston.ac
# Umang Rajdev unr1g12@soton.ac.uk 29/08/2014

# Python script to receive a new subscription message on Azure Service Bus
# This script uses the peek_lock receiving method. The message will be received, locked,
# and deleted from the topic once the process is completed.
# It records details of the received message on a table
# Will continue to try receiving messages, until ended by Cntrl + C (linux)

# Import required libraries. Ensure Azure Python APK is installed
# sys required to take input from command line
from azure.servicebus import *
from azure.storage import *
import datetime
import sys
import time

args = sys.argv

# Specify Service Bus address. Change this to your required address.
bus_service = ServiceBusService(service_namespace='<insert name>', account_key='<insert key>', issuer='<insert issuer>')

# Specify Table address. Change this to your required address.
table_service = TableService(account_name='<insert account name>', account_key='<insert key>')

while True:
# Receive messages from specified topic and subscription
    msg = bus_service.peek_lock_subscription_message(args[1], args[2])
    print msg.body
# get sequence number and lock token, as these are required inputs for deleting
    sequence_number = msg.broker_properties['SequenceNumber']
    print sequence_number
    lock_token = msg.broker_properties['LockToken']
    print lock_token

#Process the message
    now = datetime.datetime.now()
    cur_date=str(now.year) + str(now.month) + str(now.day)
    print ("Current date is " + cur_date)
    print msg.custom_properties
    localdate = str(msg.custom_properties['localdate'])
    time_str = msg.custom_properties['time_str']
    """clock = msg.custom_properties['time']
    print repr(clock)
    print type(clock)
    y =  time.localtime(clock)
    print y
    localdate = str(y.tm_year)+str(y.tm_mon)+str(y.tm_mday)
    print localdate"""
# Set the table properties
    task = Entity()
    task.PartitionKey = 'motionlog' + localdate
    print task.PartitionKey
    task.RowKey = time_str
    print repr(task.RowKey)
    task.description = str(msg.body)
# Upload the details for the received message to a table
    table_service.insert_entity('motionlog', task)
    
#delete the message from the queue
    bus_service.delete_subscription_message(args[1], args[2], sequence_number, lock_token)
    print "Message " + msg.body + " deleted from the subscription " + args[2] + " in topic " + args[1]

Save the file.

All that you need to do to receive messages now is type:

python /home/pi/scripts/table_recv_cont.py motion all

Replace ‘’”motion” with the name of your topic, and “all: with the name of the subscription.

13
Figure 13: Receiving messages

This completes the entire set up. You are now ready to send and receive messages and data from remote locations through the Azure Service Bus.

Leave a Reply

Your email address will not be published. Required fields are marked *