Esempio n. 1
1
def send_temperature(temp):
    today = datetime.datetime.now()
    now = str(today)    
    sbs = ServiceBusService(service_namespace=sb_namespace, shared_access_key_name=key_name, shared_access_key_value=key_value)    
    print ('sending to Event Hub...@' + now)
    event_message = '{ "DeviceId": "' + device_id + '", "DeviceName": "' + device_name + '", "Temperature": "' + str(temp) + '", "TimeStamp": "' + now + '" }'
    print (event_message)
    sbs.send_event(hub_name, event_message)            
    print ('sent!')    
Esempio n. 2
0
 def __init__(self):
     self.bus_service = ServiceBusService(
         service_namespace='sb-test',
         shared_access_key_name='RepertorySniffSend',
         shared_access_key_value=
         'h64zPhep/+8Aocl9DM+sm7AtkzzTDbzIqHW65iGIul0=',
         host_base=".servicebus.chinacloudapi.cn")
Esempio n. 3
0
class SweetieMQ(object):

    bus_service = None
    topic = None

    def __init__(self, config):
        account_key = getattr(config, 'sb_account_key', None)
        if account_key is None:
            log.warn('sb_account_key is not set, skipping mq setup')
            return

        issuer = getattr(config, 'sb_issuer', 'owner')
        service_namespace = getattr(config, 'sb_namespace', 'jabber-messages')
        topic = getattr(config, 'sb_topic', 'test-topic')

        self.bus_service = ServiceBusService(service_namespace=service_namespace,\
                account_key=account_key, issuer=issuer)
        self.topic = topic

    def send(self, message):
        if self.bus_service is None:
            return
        log.debug('Sending message ' + str(message))
        msg = Message(message)
        try:
            self.bus_service.send_topic_message(self.topic, msg)
        except Exception as e:
            log.error("MESSAGE DELIVERY FAILED: " + str(e))
Esempio n. 4
0
 def __init__(self):
     self.bus_service = ServiceBusService(
         service_namespace='sb-test',
         shared_access_key_name='RepertorySniffListen',
         shared_access_key_value=
         'Z20KDwxQ1Lbppw3HaP1Wa6PJC94lBIVUEQiuELC7ejs=',
         host_base=".servicebus.chinacloudapi.cn")
class azure_service_bus_listener(object):
	
    def __init__(self, azure_settings):
        self.bus_service = ServiceBusService(
            service_namespace= azure_settings['name_space'],
            shared_access_key_name = azure_settings['key_name'],
            shared_access_key_value = azure_settings['key_value'])
		
        self.queue_name = azure_settings['queue_name']
	
    def wait_for_message(self, on_receive_target, on_timeout_target):
		# just in case it isn't there
        self.create_queue()
		
        message = self.bus_service.receive_queue_message(self.queue_name, peek_lock=False)
        
        if (message.body == None):
            print("[ASB_Listener]: No Message Received")
            on_timeout_target()
        else:
            message_string = message.body.decode('utf-8')
            on_receive_target(message_string)
		
    def create_queue(self):
        q_opt = Queue()
        q_opt.max_size_in_megabytes = '1024'
        q_opt.default_message_time_to_live = 'PT1M'
        self.bus_service.create_queue(self.queue_name, q_opt)
Esempio n. 6
0
 def __init__(self, event_hub_connection_string, keywords):
     m = self.connection_string_regex.match(event_hub_connection_string)
     self.sbs = ServiceBusService(m.group(1),
                                  shared_access_key_name=m.group(2),
                                  shared_access_key_value=m.group(3))
     self.keywords = keywords
     self.event_hub_name = m.group(4)
Esempio n. 7
0
class SweetieMQ(object):

    bus_service = None
    topic = None

    def __init__(self, config):
        account_key = getattr(config, 'sb_account_key', None)
        issuer = getattr(config, 'sb_issuer', None)
        if issuer is None:
            issuer = 'owner'
        if account_key is None:
            return

        self.bus_service = ServiceBusService(service_namespace='jabber-fimsquad',\
                account_key=account_key, issuer=issuer)
        self.topic = 'chat-general'

    def send(self, message):
        if self.bus_service is None:
            return
        log.debug('Sending message '+str(message))
        msg = Message(message)
        try:
            self.bus_service.send_topic_message(self.topic, msg)
        except Exception as e:
            log.error("MESSAGE DELIVERY FAILED: "+str(e))
Esempio n. 8
0
def receiver():
    bus_service = ServiceBusService(
        service_namespace='comp6905',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='rK2FMzVKvCjpad7xVSj1AB3hDimhxZq3WtEE4y28yaM=')
    table_service = TableService(
        account_name='comp6905kirk',
        account_key=
        'H1YuP8hBxJ2PKw2hoW4Dr+DMAMvKZ/nGhstHw+87mE+OSBTb23cBxhkUvILgKOHWHA3hi3oaoohwVkp6lOXOlA=='
    )
    while (True):
        msg = bus_service.receive_queue_message('queue1', peek_lock=False)
        msg1 = msg.body.decode("utf-8")
        print(msg1)
        parsed_json = json.loads(msg1)
        #print(parsed_json['UserId'
        task = {
            'PartitionKey': 'Zanko',
            'RowKey': parsed_json['TransactionID'],
            'UserId': parsed_json['UserId'],
            'SellerId': parsed_json['SellerID'],
            'ProductName': parsed_json['Product Name'],
            'SalePrice': parsed_json['Sale Price'],
            'TransactionDate': parsed_json['Transaction Date']
        }
        table_service.insert_entity('Requests', task)
Esempio n. 9
0
 def __init__(self, namespace, key, issuer, name):
     self.service = ServiceBusService(service_namespace=namespace,
                                      account_key=key,
                                      issuer=issuer)
     self.name = name
     self.max_retries = 3
     self.wait = lambda count: 1.0 * (2**count)
Esempio n. 10
0
class SendReceiveMsg(object):
    def __init__(self):
        self.bus_service = ServiceBusService(
            service_namespace='sb-test',
            shared_access_key_name='RepertorySniffSend',
            shared_access_key_value=
            'h64zPhep/+8Aocl9DM+sm7AtkzzTDbzIqHW65iGIul0=',
            host_base=".servicebus.chinacloudapi.cn")

    # def __init__(self):
    #     self.bus_service = ServiceBusService(
    #         service_namespace='sb-test',
    #         shared_access_key_name='adsniff',
    #         shared_access_key_value='wrUBhluj4k1Nw2L/RVK+GYQg7H1qiwFLCXFpkFsbwnI=',
    #         host_base=".servicebus.chinacloudapi.cn"
    #     )

    def send_msg(self):
        for i in range(10):
            if i % 2:
                msg = Message('Msg {0}'.format(i).encode('utf-8'),
                              custom_properties={'num': '1'})
            else:
                msg = Message('Msg {0}'.format(i).encode('utf-8'),
                              custom_properties={'num': '2'})
            self.bus_service.send_topic_message('repertorysniff', msg)
    def __init__(self, config):

        tree = ET.parse('SharedConfig.xml')
        self.myMachineName = tree.find('.//Instance').get("id")

        self.sms = ServiceManagementService(
            subscription_id=config.get("azure", "subscription_id"),
            cert_file=config.get("azure", "cert_file")
        );

        self.bus_service = ServiceBusService(
            service_namespace=config.get("azure", "bus_namespace"),
            shared_access_key_name=config.get("azure", "bus_shared_access_key_name"),
            shared_access_key_value=config.get("azure", "bus_shared_access_key_value"))

        self.command_queue = config.get("azure", "commandQueuePath")
        for tries in range(1,10):
            try:
                self.bus_service.create_queue(self.command_queue)
                break
            except:
                print "Esperando"
            
        self.status_topic = config.get("azure", "statusTopicPath")
        self.bus_service.create_queue(self.status_topic)

        self.storage = BlobService(account_name=config.get("azure", "account_name"),
                                   account_key=config.get("azure", "account_key"))

        self.algo_storage_name = config.get("azure", "algorithm_storage_name")
        self.storage.create_container(self.algo_storage_name, fail_on_exist=False)

        self.proj_storage_name = config.get("azure", "project_storage_name")
        self.storage.create_container(self.proj_storage_name, fail_on_exist=False)
Esempio n. 12
0
 def __init__(self):
     self.bus_service = ServiceBusService(
         service_namespace='sb-test',
         shared_access_key_name='adsniff',
         shared_access_key_value='wrUBhluj4k1Nw2L/RVK+GYQg7H1qiwFLCXFpkFsbwnI=',
         host_base=".servicebus.chinacloudapi.cn"
     )
Esempio n. 13
0
def setup_client():
    bus_service = ServiceBusService(
        service_namespace=SERVICE_BUS_NAME,
        shared_access_key_name=SHARED_ACCESS_POLICY_NAME,
        shared_access_key_value=SHARED_ACCESS_POLICY_KEY_VALUE)

    while True:
        msg = bus_service.receive_queue_message(QUEUE_NAME, peek_lock=False)
        if msg.body != None:
            print(msg.body)
            msg_dict = eval(str(msg.body, 'utf-8'))
            st=0
            if msg_dict["Bike State"]:
                st=1
            # bike=Bike(id=int(msg_dict["Sensor ID"].split(" ")[1]),lon=msg_dict["Longitude"],lat=msg_dict["Latitude"],status= st)
            device_id = int(msg_dict["Sensor ID"].split("_")[1])
            bike = Bike.query.get(device_id)
            # if bike is None:
            bike_new = Bike(device_id=device_id,lon=msg_dict["Longitude"],lat=msg_dict["Latitude"], status=st)
            # else:
            #     bike.lon = msg_dict["Longitude"]
            #     bike.lat = msg_dict["Latitude"]
            print(bike.to_dict())
            bike_new=conn.session.merge(bike_new)
            #conn.session.add(bike)
            conn.session.commit()
            print("___", conn)
        # else:
        #     print("There is no data in the queue.")
        time.sleep(30)
Esempio n. 14
0
def main():
  parser = argparse.ArgumentParser(description='Run automated diagnostics')
  parser.add_argument('-y', action='store_true', help='Skip prompts')
  parser.add_argument('--anonymous', action='store_true', help='No telemetry')
  parser.add_argument('--case', help='Link output with a case, for use with Azure Support')
  parser.add_argument('--human', action='store_true', help='Write human-readable text to stdout')
  parser.add_argument('--feedback', action='store_true', help='Provide suggestions and/or feedback')

  args=parser.parse_args()

  if(args.feedback):
    suggestion = raw_input("Suggestion (Press return to submit):  ")
    sbkey = '5QcCwDvDXEcrvJrQs/upAi+amTRMXSlGNtIztknUnAA='
    sbs = ServiceBusService('linuxdiagnostics-ns', shared_access_key_name='PublicProducer', shared_access_key_value=sbkey)
    event = {'event': 'feedback', 'feedback': suggestion}
    sbs.send_event('linuxdiagnostics',json.dumps(event))
    exit()

  if(args.anonymous):
    if(args.case == True):
      print "Cannot report case diagnostics in anonymous mode."
    else:
      Diagnostics(False,args.case).print_report(args.human)
  else:
    if(args.y):
      Diagnostics(True,args.case).print_report(args.human)
    else:
      prompt="In order to streamline support and drive continuous improvement, this script may transmit system data to Microsoft.\n Do you consent to this? (y/n):"
      consent=raw_input(prompt)
      if(consent=='y' or consent=='Y'):
        Diagnostics(True,args.case).print_report(args.human)
      else:
        print("To run without telemetry, use the --anonymous flag")
Esempio n. 15
0
def main():
    sbs = ServiceBusService(
        service_namespace='holEvents',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='ugV/8wxg/Z0ZoTWBZWRUP5j2cgaEDiJC26ZLuoshotY=')
    turn = 0
    while turn >= 0:
        t = rd(19.6, 67.6)
        ap = rd(1002.6, 101.1)
        rh = rd(54, 13.6)
        v = rd(83.5, 99.1)
        pe = rd(445.6, 61.1)

        now = datetime.now().strftime("%M")

        if turn == 0:
            time = now
        else:
            if now != time:

                data = {
                    "T": str(t),
                    "V": str(v),
                    "AP": str(ap),
                    "RH": str(rh),
                    "PE": str(pe)
                }

                body = str.encode(json.dumps(data))
                print(body)
                sbs.send_event('datatostream', body)

                time = now

        turn += 1
Esempio n. 16
0
 def __init__(self):
     self.bus_service = ServiceBusService(
         service_namespace='sb-test',
         shared_access_key_name='WifiSniffSend',
         shared_access_key_value=
         '23SmfgRdqHHTn7o38hG1wLDYskIF3T+DL29g54W/fhE=',
         host_base=".servicebus.chinacloudapi.cn")
Esempio n. 17
0
def handle_filings(
        handler,  #type: ()->void
        subscription,
        readonly_shared_access_key_value="Cb7VhLR6eJxsurCSPtXQHOJvlkU84CCCx2oB+T/so6Q=",
        service_bus_namespace='calcbench'):

    bus_service = ServiceBusService(
        service_namespace=service_bus_namespace,
        shared_access_key_name="public",
        shared_access_key_value=readonly_shared_access_key_value)
    while True:
        try:
            message = bus_service.receive_subscription_message(
                TOPIC, subscription)
        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception as e:
            warnings.warn(str(e))
        else:
            if (message.body):
                try:
                    filing = json.loads(message.body)
                    handler(filing)
                    message.delete()
                except Exception as e:
                    warnings.warn(str(e))
Esempio n. 18
0
def poll_service_bus(sb_connection_string, sb_queue_name, queue):
    SERVICE_NAMESPACE, SHARED_ACCESS_KEY_NAME, SHARED_ACCESS_KEY_VALUE = parse_connection_string(
        sb_connection_string)

    bus_service = ServiceBusService(
        service_namespace=SERVICE_NAMESPACE,
        shared_access_key_name=SHARED_ACCESS_KEY_NAME,
        shared_access_key_value=SHARED_ACCESS_KEY_VALUE)

    while True:
        timestamps = []
        device_ids = []
        telemetry_entities = []
        for _ in range(BATCH_SIZE):
            msg = bus_service.receive_queue_message(sb_queue_name,
                                                    peek_lock=False)
            enqueued_time = datetime.datetime.strptime(
                msg.broker_properties['EnqueuedTimeUtc'],
                '%a, %d %b %Y %H:%M:%S %Z')
            device_id = msg.custom_properties['iothub-connection-device-id']
            telemetry_entity = pickle.loads(msg.body)
            preprocess_telemetry_entity(telemetry_entity)
            timestamps.append(enqueued_time)
            device_ids.append(device_id)
            telemetry_entities.append(telemetry_entity)

        queue.put((timestamps, device_ids, telemetry_entities))
Esempio n. 19
0
 def __init__(self):
     self.bus_service = ServiceBusService(
         service_namespace='sb-test-demo',
         shared_access_key_name='RootManageSharedAccessKey',
         shared_access_key_value='u9oq3ZdjpzALZ+6xiJO3VPke/w3fe8qogVVb4y7WfSw=',
         host_base=".servicebus.chinacloudapi.cn"
     )
Esempio n. 20
0
 def __init__(self):
     self.bus_service = ServiceBusService(
         service_namespace='sb-test',
         shared_access_key_name='testorder-hureader',
         shared_access_key_value=
         'TgD2yvEdveW20KNEpsNQX+Eq2RTlBFrTM43Cmf+aAqc=',
         host_base=".servicebus.chinacloudapi.cn")
def queuehandler(table):
    with open('config.json', 'r') as f:
        config = json.load(f)
    count = 0
    table_key = config['STORAGE']['QUEUE_TABLE_KEY']  # Azure table name

    queue_namespace = config['QUEUE']['NAMESPACE']
    namespace_key = config['QUEUE']['ACCOUNT_KEY']
    SASName = config['QUEUE']['SASNAME']
    queue_name = config['QUEUE']['QUEUE_NAME']
    bus_service = ServiceBusService(service_namespace=queue_namespace,
                                    shared_access_key_value=namespace_key,
                                    shared_access_key_name=SASName)
    try:
        queue_options = Queue()
        queue_options.max_size_in_megabytes = '5120'
        queue_options.default_message_time_to_live = 'PT8H'
        queue_options.requires_duplicate_detection = True
        #queue_options.requires_session = True
        bus_service.create_queue('email_notification', queue_options)
    except Exception as e:
        f = open("exceptions.txt", "a")
        f.write(e)
        print("Exception occured in Service Bus Handler, check log")
    return (bus_service)
Esempio n. 22
0
 def __init__(self):
     # 初始连接listen
     self.bus_service = ServiceBusService(
         service_namespace='sb-test',
         shared_access_key_name='WifiSniffListen',
         shared_access_key_value=
         'VzWU2ayXdk/Q8cqJGcaXbTnUkqYvbsah79FbqGhiR3I=',
         host_base=".servicebus.chinacloudapi.cn")
Esempio n. 23
0
class Diagnostics:
  def __init__(self,telemetry,case):
    self.sbkey = '5QcCwDvDXEcrvJrQs/upAi+amTRMXSlGNtIztknUnAA='
    self.sbs = ServiceBusService('linuxdiagnostics-ns', shared_access_key_name='PublicProducer', shared_access_key_value=self.sbkey)
    self.telemetry = telemetry
    self.case = case
    self.report = dict()
    self.data = dict()
    try:
      self.data['fstab'] = fstab()
      self.data['vmstat'] = vmstat()
      self.data['diskstats'] = diskstats()
      self.data['meminfo'] = meminfo()
      self.data['loadavg'] = loadavg()
      self.data['proc.sys.fs'] = procsysfs()
      self.data['inodes_by_mount'] = inodes_by_mp()
      self.data['waagent'] = waagent()
      self.check_fstab_uuid()
      self.check_resourcedisk_inodes()
      self.success_telemetry()
    except:
      self.error_telemetry(str(sys.last_value))
      exit()

  def print_report(self,human):
    if(human):
      for item in map(lambda x: human_readable(self.report[x]),self.report):
        print item
    else:
      print json.dumps(self.report)

# == Telemetry ==
  def success_telemetry(self):
    if(self.telemetry):
      event = {'event': 'success', 'report': self.report, 'data': self.data}
      if(self.case):
        event['case'] = self.case
      self.sbs.send_event('linuxdiagnostics',json.dumps(event))

  def error_telemetry(self,e):
    if(self.telemetry):
      event = {'event': 'error', 'error': e, 'report': self.report, 'data':self.data}
      if(self.case):
        event['case'] = self.case
      sbs.send_event('linuxdiagnostics',json.dumps(event))

# == Diagnostics ==
  def check_fstab_uuid(self):
    uuids = map (lambda r:bool(re.match('UUID',r['fs_spec'],re.I)), self.data['fstab'])
    uuids = filter(lambda x: not x, uuids)
    if(len(uuids) > 0):
      self.report['linux.uuid']= report("fstab doesn't use UUIDs", 'red', 'best_practices', "/etc/fstab isn't using UUIDs to reference its volumes.", '')

  def check_resourcedisk_inodes(self):
    mountpoint = self.data['waagent']['ResourceDisk.MountPoint']
    x=filter(lambda x: x['mountpoint']==mountpoint ,self.data['inodes_by_mount'])[0]
    if(x['inodes_used'] > 20):
      self.report['azure.resource_disk_usage'] = report('Resource Disk Usage', 'yellow', 'data_loss',"This instance appears to be using the resource disk. Data stored on this mountpoint may be lost.", '')
def send_to_queue(data, queue):

    svcbus = ServiceBusService(
        service_namespace='botista',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='0/O8pigV09yoxSrSpEjKK+uoMWS1JYYJQEbnbzVxnpA=')

    event = Message(json.dumps(data))
    svcbus.send_queue_message(queue, event)
Esempio n. 25
0
 def __init__(self):
     self.service_bus = ServiceBusService(
         ServiceBusTaskSource.service_bus_namespace(),
         shared_access_key_name=os.environ.get(
             'AZURE_SERVICE_BUS_ACCESS_KEY_NAME'),
         shared_access_key_value=os.environ.get(
             'AZURE_SERVICE_BUS_ACCESS_KEY_VALUE'))
     self.queue_name = os.environ.get('AZURE_SERVICE_BUS_QUEUE_NAME',
                                      'training')
Esempio n. 26
0
 def __init__(self, service_name, key_name, key_value):
     self.key_name = key_name
     self.key_value = key_value
     self.service_name = service_name
     self.records = []
     self.limit = 256 * 1024  # Message limit for eventHub
     self.sbs = ServiceBusService(service_name,
                                  shared_access_key_name=key_name,
                                  shared_access_key_value=key_value)
Esempio n. 27
0
    def _initialize_event_subscriber(self):
        self.bus_service = ServiceBusService(
            service_namespace=self.service_bus_namespace,
            shared_access_key_name=self.service_bus_sas_key_name,
            shared_access_key_value=self.service_bus_sas_key_value)

        self._remove_all_event_subscriptions()
        self._add_event_subscription(CO2_SENSOR_ID)
        self._remove_all_event_messages()
Esempio n. 28
0
def send_msg_bus(namespace, hub, user, password):
    from azure.servicebus import ServiceBusService, Message, Queue
    from datetime import datetime
    import subprocess
    bus_service = ServiceBusService(service_namespace=namespace,
                                    shared_access_key_name=user,
                                    shared_access_key_value=password)
    message = subprocess.getoutput('az resource list -o json')
    #  message = 'mon script,"{}"'
    bus_service.send_event(hub, message)
class EventHubSender(BaseSender):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._sender = ServiceBusService(self._config.service_namespace,
                                         shared_access_key_name=self._config.key_name,
                                         shared_access_key_value=self._config.key_value)

    def send(self, label, data):
        assert isinstance(label, str), 'label must be a string'
        self._sender.send_event(label, data)
    def setUp(self):
        self.sbs = ServiceBusService(
            credentials.getEventHubNamespace(),
            shared_access_key_name=credentials.getEventHubSasKeyName(),
            shared_access_key_value=credentials.getEventHubSasKeyValue(),
            )

        set_service_options(self.sbs)

        self.event_hub_name = getUniqueName('uthub')
Esempio n. 31
0
    def __init__(self,log):


        #Init variables
        self.isrunning = False
        self.logger = log
        self.events = cloudevents()
        self.device_method_return_value = DeviceMethodReturnValue()
        self.sbs = ServiceBusService('dev-sw-evhb',shared_access_key_name='sender',shared_access_key_value='3B+Q02HNLYHGtBE9QtET2f3temGWtkL/CmDT5Fy/ldU=')
        self.block_blob_service = BlockBlobService(account_name='devschoolwayzdatastorage', account_key='4+z+7pEC7FQJNEe+UIX+4PRDrqaHWTTDeTXaGOPPqnZGKJRTpi0IDaHhgZLKviC+5HQZKo1H5ZQzMmllIRZjdg==')
Esempio n. 32
0
class AzureWorker(Thread):
    def __init__(self):
        Thread.__init__(self)
        self._quit = Event()

        settings = Settings()
        settings.configure_logging('../logs/task_manager.log', 'TaskManagerAzureWorker')

        self.job_manager_url = 'http://' + settings.job_manager_api_connect + ':5000'

        self.unfinished = []
        self.finished = []
        self.executor = Executor(self.unfinished, self.finished)

        self.outgoing_topic = 'finished_jobs'
        self.incoming_topic = 'pending_jobs'

        self.bus_service = ServiceBusService(
            service_namespace=settings.azure_topic_namespace,
            shared_access_key_name=settings.azure_topic_keyname,
            shared_access_key_value=settings.azure_topic_key
        )
        self.bus_service.create_topic(self.incoming_topic)
        self.bus_service.create_topic(self.outgoing_topic)
        self.incoming_topic_subscription = 'AllMessages'
        self.bus_service.create_subscription(self.incoming_topic, self.incoming_topic_subscription)

    def quit(self):
        self._quit.set()

    def run(self):
        self.executor.start()

        # dislike of unstoppable threads
        while not self._quit.is_set():
            msg = self.bus_service.receive_subscription_message(self.incoming_topic, self.incoming_topic_subscription,
                                                                peek_lock=True)
            if msg.body is not None:
                # New job for us!
                job_id = msg.custom_properties['job_id']
                logging.info('getting job with id: ' + job_id + ' from the API')

                r = requests.get(self.job_manager_url + '/jobs/' + job_id)
                job = r.json()

                msg.delete()

                logging.info('appending tasks from job with id: ' + job['id'] + ' and name: ' + job['name'])
                self.unfinished.append(job['name'])

            sleep(3)

        # stop executor
        self.executor.quit()
        self.executor.join()
Esempio n. 33
0
class AzureConnection():
    def __init__(self):
        self.bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value=
            'Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
        queue_options = Queue()
        queue_options.max_size_in_megabytes = '5120'
        queue_options.default_message_time_to_live = 'PT96H'

        self.bus_service.create_queue('process_incoming', queue_options)
        self.bus_service.create_queue('whatsapp_sender', queue_options)
        self.bus_service.create_queue('myapp_sender', queue_options)

    def receive(self):
        msg = self.bus_service.receive_queue_message('process_incoming',
                                                     peek_lock=False)

        if msg != None and msg.body:
            logger.info(msg.body)
            return json.loads(msg.body)
        else:
            return None

    def send(self, jsondict):
        t = json.dumps(jsondict)
        msg = Message(t)
        logger.info(t)
        Q = jsondict['medium'] + '_sender'

        self.bus_service.send_queue_message(Q, msg)
Esempio n. 34
0
class AzureConnection():
    def __init__(self):
        self.bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value='Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
        queue_options = Queue()
        queue_options.max_size_in_megabytes = '5120'
        queue_options.default_message_time_to_live = 'PT96H'

        self.bus_service.create_queue('process_incoming', queue_options)
        self.bus_service.create_queue('whatsapp_sender', queue_options)
        self.bus_service.create_queue('myapp_sender', queue_options)
 

    def receive(self):
        msg = self.bus_service.receive_queue_message('process_incoming', peek_lock=False)
        
        if msg != None and msg.body:
                logger.info(  msg.body)
                return json.loads(msg.body)
        else:
                return None

    def send(self, jsondict):
        t = json.dumps(jsondict)
        msg = Message(t)
        logger.info(  t)
        Q = jsondict['medium'] + '_sender'
            
        self.bus_service.send_queue_message(Q, msg)
Esempio n. 35
0
    def __init__(self):
        super(EchoLayer, self).__init__()
        YowInterfaceLayer.__init__(self)
        self.connected = False
        self.serve = Serve()

        self.bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value=
            'Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
def notification_publisher():

    bus_service_plate_publisher = ServiceBusService(
        service_namespace="licenseplatepublisher",
        shared_access_key_name='listeneronly',
        shared_access_key_value='w+ifeMSBq1AQkedLCpMa8ut5c6bJzJxqHuX9Jx2XGOk=')

    msg = bus_service_plate_publisher.receive_subscription_message(
        'wantedplatelistupdate', 'JWWtexQpcUeCjnd9', peek_lock=False)

    return msg.body
Esempio n. 37
0
 def __init__(self, namespace, key, issuer, shared_access_key_name,
              shared_access_key_value, name):
     self.service = ServiceBusService(
         service_namespace=namespace,
         account_key=key,
         issuer=issuer,
         shared_access_key_name=shared_access_key_name,
         shared_access_key_value=shared_access_key_value)
     self.name = name
     self.max_retries = 3
     self.wait = lambda count: 1.0 * (2**count)
def workerthread():
    # outer loop to run while waiting
    from azure.servicebus import ServiceBusService, Message, Queue
    bus_read_service = ServiceBusService(
        service_namespace='vmsseight',
        shared_access_key_name='ListenOneTime',
        shared_access_key_value='OhFgmG5Cr/K9aOrE29YL7eXERzmUb3Fpf7J+FoBhiMw=')
    while (True):
        # main loop to thrash the CPI
        while (keepworking == True):
            msg = bus_read_service.receive_queue_message('vmsseightqueue', peek_lock=False)
        time.sleep(3)
Esempio n. 39
0
 def _ensureServiceBusQueuesExist(self):
     """
     Creates Azure service bus queues required by the service.
     """
     logger.info("Checking for existence of Service Bus Queues.")
     namespace = self.sbms.get_namespace(self.config.getServiceBusNamespace())
     sbs = ServiceBusService(namespace.name, namespace.default_key, issuer='owner')
     queue_names = ['jobresponsequeue', 'windowscomputequeue', 'linuxcomputequeue']
     for name in queue_names:
         logger.info("Checking for existence of Queue %s.", name)
         sbs.create_queue(name, fail_on_exist=False)
         logger.info("Queue %s is ready.", name)
Esempio n. 40
0
class azure_interface:
  def __init__(self, name_space, key_name, key_value, proxy = None):
    self.sbs = ServiceBusService(service_namespace=name_space,shared_access_key_name=key_name, shared_access_key_value=key_value)
    if proxy:
      self.sbs.set_proxy('proxy.iisc.ernet.in',3128)

  def event_send(self, hubname ,msg):
    try:
      hubStatus = self.sbs.send_event(hubname, str(msg))
      print "Send Status:", repr(hubStatus)
    except:
      print sys.exc_info()               
Esempio n. 41
0
class AzureServiceBusQueue(Queue):
    """
    Implements a Queue backed by a Windows Azure Service Bus Queue.
    """

    # Timeout in seconds. receive_message is blocking and returns as soon as one of two
    # conditions occurs: a message is received or the timeout period has elapsed.
    polling_timeout = 60

    def __init__(self, namespace, key, issuer, shared_access_key_name,
                 shared_access_key_value, name):
        self.service = ServiceBusService(
            service_namespace=namespace,
            account_key=key,
            issuer=issuer,
            shared_access_key_name=shared_access_key_name,
            shared_access_key_value=shared_access_key_value)
        self.name = name
        self.max_retries = 3
        self.wait = lambda count: 1.0 * (2**count)

    def _try_request(self, fn, retry_count=0, fail=None):
        '''Helper to retry request for sending and receiving messages.'''
        try:
            return fn()
        except (WindowsAzureError) as e:
            if retry_count < self.max_retries:
                logger.error(
                    "Retrying request after error occurred. Attempt %s of %s.",
                    retry_count + 1, self.max_retries)
                wait_interval = self.wait(retry_count)
                if wait_interval > 0.0:
                    sleep(wait_interval)
                return self._try_request(fn,
                                         retry_count=retry_count + 1,
                                         fail=fail)
            else:
                if fail is not None:
                    fail()
                raise e

    def receive_message(self):
        op = lambda: self.service.receive_queue_message(
            self.name, peek_lock=False, timeout=self.polling_timeout)
        msg = self._try_request(op)
        return None if msg.body is None else AzureServiceBusQueueMessage(
            self, msg)

    def send_message(self, body):
        op = lambda: self.service.send_queue_message(self.name, Message(body))
        fail = lambda: logger.error(
            "Failed to send message. Message body is:\n%s", body)
        self._try_request(op, fail=fail)
Esempio n. 42
0
def azure_autheticate():
    key_name = 'beardcavekey' # SharedAccessKeyName from Azure portal
    key_value = 'qc4xkJJlpEiRpcwCiD56dh/oxJBlhmM0gCwUcYw10GM=' # SharedAccessKey from Azure portal
    service_namespace = 'beardcave'
    sbs = ServiceBusService(service_namespace,
                            shared_access_key_name=key_name,
                            shared_access_key_value=key_value)

    print "authenticated"
    msg = Message('g3 off')
    sbs.send_topic_message('lighttopic', msg)
    # msg = sbs.receive_subscription_message('lighttopic', 'lightsubscription', peek_lock=True)
    print(msg.body)
    print "sent"
    def __init__(self, azure_settings):
        self.bus_service = ServiceBusService(
            service_namespace= azure_settings['name_space'],
            shared_access_key_name = azure_settings['key_name'],
            shared_access_key_value = azure_settings['key_value'])
		
        self.queue_name = azure_settings['queue_name']
	def __init__(self, azure_settings):
		self.bus_service = ServiceBusService(
			service_namespace= azure_settings.name_space,
			shared_access_key_name = azure_settings.key_name,
			shared_access_key_value = azure_settings.key_value)
		
		self.queue_name = azure_settings(queue_name)
Esempio n. 45
0
    def __init__(self):
        Thread.__init__(self)
        self._quit = Event()

        settings = Settings()
        settings.configure_logging('../logs/task_manager.log', 'TaskManagerAzureWorker')

        self.job_manager_url = 'http://' + settings.job_manager_api_connect + ':5000'

        self.unfinished = []
        self.finished = []
        self.executor = Executor(self.unfinished, self.finished)

        self.outgoing_topic = 'finished_jobs'
        self.incoming_topic = 'pending_jobs'

        self.bus_service = ServiceBusService(
            service_namespace=settings.azure_topic_namespace,
            shared_access_key_name=settings.azure_topic_keyname,
            shared_access_key_value=settings.azure_topic_key
        )
        self.bus_service.create_topic(self.incoming_topic)
        self.bus_service.create_topic(self.outgoing_topic)
        self.incoming_topic_subscription = 'AllMessages'
        self.bus_service.create_subscription(self.incoming_topic, self.incoming_topic_subscription)
    def on_data(self, data):

        # Twitter returns data in JSON format - we need to decode it first
		decoded = json.loads(data)

        # # Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
        # print '@%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
        # print ''
        # return True

		sbs = ServiceBusService(service_namespace=config.servns, shared_access_key_name=config.key_name, shared_access_key_value=config.key_value)
		searched_tweets = "{ 'source': '" + unicode(decoded['user']['screen_name']) + "' , 'text': '" + unicode(decoded['text'].encode('ascii', 'ignore')) + "' }"
		print(unicode(searched_tweets))
		sbs.send_event('iot', unicode(searched_tweets))

		return True
 def __init__(self, namespace, key, issuer, shared_access_key_name, shared_access_key_value, name):
     self.service = ServiceBusService(service_namespace=namespace, account_key=key,
                                      issuer=issuer, shared_access_key_name=shared_access_key_name,
                                      shared_access_key_value=shared_access_key_value)
     self.name = name
     self.max_retries = 3
     self.wait = lambda count: 1.0*(2**count)
class AzureServiceBusQueue(Queue):
    """
    Implements a Queue backed by a Windows Azure Service Bus Queue.
    """

    # Timeout in seconds. receive_message is blocking and returns as soon as one of two
    # conditions occurs: a message is received or the timeout period has elapsed.
    polling_timeout = 60

    def __init__(self, namespace, key, issuer, shared_access_key_name, shared_access_key_value, name):
        self.service = ServiceBusService(service_namespace=namespace, account_key=key,
                                         issuer=issuer, shared_access_key_name=shared_access_key_name,
                                         shared_access_key_value=shared_access_key_value)
        self.name = name
        self.max_retries = 3
        self.wait = lambda count: 1.0*(2**count)

    def _try_request(self, fn, retry_count=0, fail=None):
        '''Helper to retry request for sending and receiving messages.'''
        try:
            return fn()
        except (WindowsAzureError) as e:
            if retry_count < self.max_retries:
                logger.error("Retrying request after error occurred. Attempt %s of %s.",
                             retry_count+1, self.max_retries)
                wait_interval = self.wait(retry_count)
                if wait_interval > 0.0:
                    sleep(wait_interval)
                return self._try_request(fn, retry_count=retry_count+1, fail=fail)
            else:
                if fail is not None:
                    fail()
                raise e

    def receive_message(self):
        op = lambda: self.service.receive_queue_message(self.name,
                                                        peek_lock=False,
                                                        timeout=self.polling_timeout)
        msg = self._try_request(op)
        return None if msg.body is None else AzureServiceBusQueueMessage(self, msg)

    def send_message(self, body):
        op = lambda: self.service.send_queue_message(self.name, Message(body))
        fail = lambda: logger.error("Failed to send message. Message body is:\n%s", body)
        self._try_request(op, fail=fail)
Esempio n. 49
0
class AzureServiceBusQueue(Queue):
    """
    Implements a Queue backed by a Windows Azure Service Bus Queue.
    """

    # Timeout in seconds. receive_message is blocking and returns as soon as one of two
    # conditions occurs: a message is received or the timeout period has elapsed.
    polling_timeout = 60

    def __init__(self, namespace, key, issuer, name):
        self.service = ServiceBusService(service_namespace=namespace, account_key=key, issuer=issuer)
        self.name = name

    def receive_message(self):
        msg = self.service.receive_queue_message(self.name, peek_lock=False, timeout=self.polling_timeout)
        return None if msg.body is None else AzureServiceBusQueueMessage(self, msg)

    def send_message(self, body):
        self.service.send_queue_message(self.name, Message(body))
Esempio n. 50
0
    def __init__(self):
        super(EchoLayer, self).__init__()
        YowInterfaceLayer.__init__(self)
        self.connected = False
        self.serve = Serve()

        self.bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value='Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
class azure_service_bus_listener(object):
	
	def __init__(self, azure_settings):
		self.bus_service = ServiceBusService(
			service_namespace= azure_settings.name_space,
			shared_access_key_name = azure_settings.key_name,
			shared_access_key_value = azure_settings.key_value)
		
		self.queue_name = azure_settings(queue_name)
	
	def wait_for_message(self, on_receive_target):
		message = self.bus_service.receive_queue_message(self.queue_name, peek_lock=False)
		message_string = message.body.decode('utf-8')
		on_receive_target(message_string)
		
	def create_queue(self):
		q_opt = Queue()
		q_opt.max_size_in_megabytes = '1024'
		q_opt.default_message_time_to_live = 'PT1M'
		self.bus_service.create_queue(self.queue_name, q_opt)
Esempio n. 52
0
    def __init__(self, config):
        account_key = getattr(config, 'sb_account_key', None)
        issuer = getattr(config, 'sb_issuer', None)
        if issuer is None:
            issuer = 'owner'
        if account_key is None:
            return

        self.bus_service = ServiceBusService(service_namespace='jabber-fimsquad',\
                account_key=account_key, issuer=issuer)
        self.topic = 'chat-general'
Esempio n. 53
0
 def __init__(self, args):
     if args.service_bus_namespace:
         if args.verbose:
             print 'Notifying via Azure Service Bus'
         self.topic = args.service_bus_topic
         self.service_bus = ServiceBusService(
             service_namespace=args.service_bus_namespace,
             account_key=args.service_bus_account_key,
             issuer=args.service_bus_issuer)
     else:
         self.service_bus = None
    def setUp(self):
        self.sbs = ServiceBusService(credentials.getServiceBusNamespace(), 
                                     credentials.getServiceBusKey(), 
                                     'owner')
        set_service_options(self.sbs)

        self.queue_name = getUniqueName('utqueue')
        self.topic_name = getUniqueName('uttopic')

        self.additional_queue_names = []
        self.additional_topic_names = []
Esempio n. 55
0
class AzureListener(Thread):
    def __init__(self, machine_manager_logic):
        Thread.__init__(self)
        self._quit = Event()
        self.daemon = True
        self.log = logging.getLogger(__name__)
        self.notification_topic = 'jobs_changed'
        self.subscription = 'AllMessages'
        self.machine_manager_logic = machine_manager_logic

        settings = Settings()
        self.bus_service = ServiceBusService(
            service_namespace=settings.azure_topic_namespace,
            shared_access_key_name=settings.azure_topic_keyname,
            shared_access_key_value=settings.azure_topic_key
        )
        self.bus_service.create_topic(self.notification_topic)
        self.bus_service.create_subscription(self.notification_topic, self.subscription)

    def quit(self):
        self._quit.set()

    def run(self):
        # dislike of unstoppable threads
        while not self._quit.is_set():
            msg = self.bus_service.receive_subscription_message(self.notification_topic, self.subscription,
                                                                peek_lock=False, timeout=0.1)
            if msg.body is not None:
                self.log.info(msg.body + ":" + json.dumps(msg.custom_properties))

                if "Created" in msg.body:
                    self.machine_manager_logic.job_added(msg.custom_properties['job_id'])

                if "Assigned" in msg.body:
                    self.machine_manager_logic.job_assigned(msg.custom_properties['job_id'], msg.custom_properties['machine_id'])

                if "Finished" in msg.body:
                    self.machine_manager_logic.job_removed(msg.custom_properties['job_id'])

            self.machine_manager_logic.check_machines()
            sleep(10)
Esempio n. 56
0
    def __init__(self):
        self.bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value='Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
        queue_options = Queue()
        queue_options.max_size_in_megabytes = '5120'
        queue_options.default_message_time_to_live = 'PT96H'

        self.bus_service.create_queue('process_incoming', queue_options)
        self.bus_service.create_queue('whatsapp_sender', queue_options)
        self.bus_service.create_queue('myapp_sender', queue_options)
Esempio n. 57
0
def main(argv):
    print 'Starting program...'

    internalDeviceId = argv[0]
    json_data=open('config.json')
    config_data = json.load(json_data)
    json_data.close()

    externalDeviceId = config_data["Devices"][internalDeviceId]
    queue_name = 'custom_' + config_data["Server"]["id"] + '_' + config_data["Server"]["Deviceid"] + '_' + externalDeviceId
    print (queue_name)
    
    bus_service = ServiceBusService( service_namespace='rdciot', shared_access_key_name='RootManageSharedAccessKey', shared_access_key_value='EXeZe7r49jCoDz79fESxtMdXwYU6iQwG1Gbo8J4HXyY=')
    print bus_service
    
    while True:
        msg = bus_service.receive_queue_message(queue_name, peek_lock=False)
        print(msg.body)
    
    
    print 'Program finished.'
Esempio n. 58
0
    def __init__(self, namespace, access_key_name, access_key_value, q_name, q_max_size="5120", msg_ttl="PT1M"):
        self.bus_svc = ServiceBusService(
            service_namespace=namespace,
            shared_access_key_name=access_key_name,
            shared_access_key_value=access_key_value,
        )
        self.queue_options = Queue()
        self.queue_options.max_size_in_megabytes = q_max_size
        self.queue_options.default_message_time_to_live = msg_ttl

        self.queue_name = q_name
        self.bus_svc.create_queue(self.queue_name, self.queue_options)
    def setUp(self):
        super(ServiceBusEventHubTest, self).setUp()

        self.sbs = ServiceBusService(
            self.settings.EVENTHUB_NAME,
            shared_access_key_name=self.settings.EVENTHUB_SAS_KEY_NAME,
            shared_access_key_value=self.settings.EVENTHUB_SAS_KEY_VALUE,
            request_session=Session(),
        )

        self._set_service_options(self.sbs, self.settings)

        self.event_hub_name = self.get_resource_name('uthub')