Beispiel #1
0
def run():
    logger.info('starting')
    iot_conf, influx_conf = get_config('conf/config.ini')
    influxdb = InfluxAdapter(influx_conf['HOSTNAME'], influx_conf['PORT'],
                             influx_conf['USER'], influx_conf['PASSWORD'],
                             influx_conf['DATABASE'])
    client = EventHubClient.from_iothub_connection_string(
        iot_conf['IOTHUB_CONNSTR'], debug=False)

    partitions = iot_conf.getint('PARTITION_COUNT')
    new_receiver = lambda x: client.add_receiver("$default",
                                                 str(x),
                                                 offset=Offset("@latest"),
                                                 operation='/messages/events')
    receivers = [new_receiver(pid) for pid in range(partitions)]

    try:
        client.run()
        p = multiprocessing.dummy.Pool(partitions)
        receiverfunc = functools.partial(receivefunc, influxdb=influxdb)
        p.map(receiverfunc, receivers)
    except KeyboardInterrupt:
        logger.info('stopping')
        stop_event.set()
    finally:
        client.stop()
def create_eventhub_client_from_iothub_connection_string(live_eventhub_config):
    # [START create_eventhub_client_iot_connstr]
    import os
    from azure.eventhub import EventHubClient

    iot_connection_str = os.environ['IOTHUB_CONNECTION_STR']
    client = EventHubClient.from_iothub_connection_string(iot_connection_str)
Beispiel #3
0
def create_eventhub_client_from_iothub_connection_string(live_eventhub_config):
    # [START create_eventhub_client_iot_connstr]
    import os
    from azure.eventhub import EventHubClient

    iot_connection_str = os.environ['IOTHUB_CONNECTION_STR']
    client = EventHubClient.from_iothub_connection_string(iot_connection_str)
def test_iothub_receive_sync(iot_connection_str, device_id):
    client = EventHubClient.from_iothub_connection_string(iot_connection_str, debug=True)
    receiver = client.add_receiver("$default", "0", operation='/messages/events')
    try:
        client.run()
        partitions = client.get_eventhub_info()
        assert partitions["partition_ids"] == ["0", "1", "2", "3"]
        received = receiver.receive(timeout=5)
        assert len(received) == 0
    finally:
        client.stop()
def test_iothub_send_single_event(iot_connection_str, device_id):
    client = EventHubClient.from_iothub_connection_string(iot_connection_str, debug=True)
    sender = client.add_sender(operation='/messages/devicebound')
    try:
        client.run()
        outcome = sender.send(EventData(b"A single event", to_device=device_id))
        assert outcome.value == 0
    except:
        raise
    finally:
        client.stop()
def test_iothub_send_single_event(iot_connection_str, device_id):
    client = EventHubClient.from_iothub_connection_string(iot_connection_str, debug=True)
    sender = client.add_sender(operation='/messages/devicebound')
    try:
        client.run()
        outcome = sender.send(EventData(b"A single event", to_device=device_id))
        assert outcome.value == 0
    except:
        raise
    finally:
        client.stop()
Beispiel #7
0
def test_iothub_receive(iot_connection_str, device_id):
    client = EventHubClient.from_iothub_connection_string(iot_connection_str,
                                                          debug=True)
    receiver = client.add_receiver("$default",
                                   "0",
                                   operation='/messages/events')
    try:
        client.run()
        received = receiver.receive(timeout=5)
        assert len(received) == 0
    finally:
        client.stop()
Beispiel #8
0
def test_iothub_receive_sync(iot_connection_str, device_id):
    client = EventHubClient.from_iothub_connection_string(iot_connection_str,
                                                          debug=True)
    receiver = client.add_receiver("$default",
                                   "0",
                                   operation='/messages/events')
    try:
        client.run()
        partitions = client.get_eventhub_info()
        assert partitions["partition_ids"] == ["0", "1", "2", "3"]
        received = receiver.receive(timeout=5)
        assert len(received) == 0
    finally:
        client.stop()
 def connect(self, connection_string):
     print_message("EventHubApi: connecting EventHubClient")
     self.client = EventHubClient.from_iothub_connection_string(connection_string)
     print("EventHubApi: enabling EventHub telemetry")
     # partition_ids = self.client.get_eventhub_info()["partition_ids"]
     partition_ids = [0, 1, 2, 3]
     for id in partition_ids:
         print_message("EventHubApi: adding receiver for partition {}".format(id))
         receiver = self.client.add_receiver(
             "$default", id, operation="/messages/events", offset=Offset("@latest")
         )
         self.receivers.append(receiver)
     print_message("EventHubApi: starting client")
     self.client.run()
     print_message("EventHubApi: ready")
    def connect(self, offset="@latest"):
        global object_list
        if self not in object_list:
            object_list.append(self)

        started = False
        retry_iteration = 0
        while not started:
            adapter_config.logger("EventHubApi: connecting EventHubClient")
            self.client = EventHubClient.from_iothub_connection_string(
                self.connection_string)
            adapter_config.logger("EventHubApi: enabling EventHub telemetry")
            # partition_ids = self.client.get_eventhub_info()["partition_ids"]
            partition_ids = [0, 1, 2, 3]
            self.receivers = []
            for id in partition_ids:
                adapter_config.logger(
                    "EventHubApi: adding receiver for partition {}".format(id))
                receiver = self.client.add_receiver(
                    "$default",
                    id,
                    operation="/messages/events",
                    offset=Offset(offset))
                self.receivers.append(receiver)

            adapter_config.logger("EventHubApi: starting client")

            try:
                self.client.run()
                started = True
            except EventHubError as e:
                if e.message.startswith("ErrorCodes.ResourceLimitExceeded"):
                    retry_iteration += 1
                    retry_time = get_retry_time(retry_iteration)
                    adapter_config.logger(
                        "eventhub ResourceLimitExceeded.  Sleeping for {} seconds and trying again"
                        .format(retry_time))
                    self._close_eventhub_client()
                    time.sleep(retry_time)
                else:
                    raise e

            adapter_config.logger("EventHubApi: ready")
Beispiel #11
0
def get_iothub_data():
	list = ['0'] * 11
	
	client = EventHubClient.from_iothub_connection_string('<your_connection_string>', debug=True)
	receiver = client.add_receiver("$default", "3", operation='/messages/events', offset = Offset(datetime.datetime.utcnow()))
	
	try:
		client.run()
		eh_info = client.get_eventhub_info()
		print(eh_info)
		
		received = receiver.receive(timeout=5)
		print(received)
		
		for item in received: 
			message = json.loads(str(item.message))
			print(message)
			
			if 'data' in message:
				data = message['data']
				
				air_temp = str(int(data[0:2], 16))
				air_hum = str(int(data[2:4], 16))
				pressure = str(int((data[4:8]), 16))
				co2 = str(int(data[8:12], 16))
				dust = str(int(data[12:16], 16))
				illumination = str(int(data[16:20], 16))
				o2 = str(round(int(data[20:22], 16) / 10, 1))
				soil_temp = str(int(data[22:24], 16))
				soil_hum = str(int(data[24:26], 16))
				voltage = str(round(int(data[26:28], 16) / int('ff', 16) * 5, 1))
				error = str(int(data[28:], 16))
				
				list = [air_temp, air_hum, pressure, co2, dust, illumination, o2, soil_temp, soil_hum, voltage, error]
	
	finally:
		client.stop()
	
	return list
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
"""
An example to show receiving events from an IoT Hub partition.
"""
from azure import eventhub
from azure.eventhub import EventData, EventHubClient, Offset

import logging
logger = logging.getLogger('azure.eventhub')

CONNSTR = os.environ['IOTHUB_CONNECTION_STR']

client = EventHubClient.from_iothub_connection_string(iot_connection_str,
                                                      debug=True)
receiver = client.add_receiver("$default", "0", operation='/messages/events')
try:
    client.run()
    eh_info = client.get_eventhub_info()
    print(eh_info)

    received = receiver.receive(timeout=5)
    print(received)
finally:
    client.stop()
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

"""
An example to show receiving events from an IoT Hub partition.
"""
from azure import eventhub
from azure.eventhub import EventData, EventHubClient, Offset

import os
import logging
logger = logging.getLogger('azure.eventhub')

iot_connection_str = os.environ['IOTHUB_CONNECTION_STR']

client = EventHubClient.from_iothub_connection_string(iot_connection_str, debug=True)
receiver = client.add_receiver("$default", "0", operation='/messages/events')
try:
    client.run()
    eh_info = client.get_eventhub_info()
    print(eh_info)

    received = receiver.receive(timeout=5)
    print(received)
finally:
    client.stop()