Exemplo n.º 1
0
class DAMRWEB:
    def __init__(self, namespace, SAS_name, SAS_value):

        self.topic_vel = 'cmd_vel'
        self.topic_stop = 'motor_stop'

        self.namespace = namespace
        self.SAS_name = SAS_name
        self.SAS_value = SAS_value

        self.topic_options = Topic()
        self.topic_options.max_size_in_megabytes = '10'  #### Try : '1024'
        self.topic_options.default_message_time_to_live = 'PT5S'

        self.bus_service = ServiceBusService(
            service_namespace=self.namespace,
            shared_access_key_name=self.SAS_name,
            shared_access_key_value=self.SAS_value)

        self.cmd_values = {
            'N': "100 100",
            'S': '-100 -100',
            'W': '-50 50',
            'E': '50 -50'
        }
        self.bot_list = []

    #create topics of cmd_vel and motor stop
    def createLocalbot(self, bot_id):

        self.bus_service.create_topic((self.topic_vel + bot_id),
                                      self.topic_options)
        self.bus_service.create_topic((self.topic_stop + bot_id),
                                      self.topic_options)
        # self.bus_service.create_subscription((self.topic_vel+bot_id), bot_id)
        # self.bus_service.create_subscription((self.topic_stop+bot_id), bot_id)
        self.bot_list.append(bot_id)

        pass

    def remoteControl(self, bot_id, cmd_dirn):

        msg = Message(self.cmd_values[cmd_dirn].encode('utf-8'))
        self.bus_service.send_topic_message((self.topic_vel + bot_id), msg)

    def remoteControlStop(self, bot_id):

        msg = Message('0 0'.encode('utf-8'))
        self.bus_service.send_topic_message((self.topic_vel + bot_id), msg)

    def deleteTopic(self, bot_id):
        self.bus_service.delete_topic((self.topic_vel + bot_id))
        self.bus_service.delete_topic((self.topic_stop + bot_id))

    def cleanup(self):

        for bot in self.bot_list:
            self.deleteTopic(bot)
Exemplo n.º 2
0
import os
from azure.servicebus.control_client import ServiceBusService, Message, Topic
from pymongo import MongoClient
import json

mongo_conn_str = os.getenv('PROD_MONGODB')
shared_access_key = os.getenv('shared_access_key')
shared_access_value = os.getenv('shared_access_value')
service_namespace = os.getenv('service_namespace')
print(service_namespace)

bus_service = ServiceBusService(service_namespace=service_namespace, shared_access_key_name=shared_access_key,
                                shared_access_key_value=shared_access_value)
topic_name = "on-prem-test"  # the topic name already defined in azure
subscription = "heroku_consumer"  # unique name for this application/consumer
bus_service.create_topic(topic_name)
# create subscription
bus_service.create_subscription(topic_name, subscription)

# get subscription message
msg = bus_service.receive_subscription_message(
    topic_name, subscription, peek_lock=False)
msg_body = None
entity = None
if msg.body is None:
    print('No messages to retrieve')
    exit()
else:
    msg_body = msg.body.decode("utf-8")
    entity = json.loads(msg_body)
    print(msg_body)