|
| 1 | +import os |
| 2 | +import asyncio |
| 3 | +import configparser |
| 4 | +import sys |
| 5 | + |
| 6 | +from random import randint |
| 7 | + |
| 8 | +config = configparser.ConfigParser() |
| 9 | +config.read(os.path.join(os.path.dirname(__file__), "samples.ini")) |
| 10 | + |
| 11 | +if config["DEFAULT"].getboolean("Local"): |
| 12 | + sys.path.insert(0, "src") |
| 13 | + |
| 14 | +from iotc import ( |
| 15 | + IOTCConnectType, |
| 16 | + IOTCLogLevel, |
| 17 | + IOTCEvents, |
| 18 | + Command, |
| 19 | + CredentialsCache, |
| 20 | + Storage, |
| 21 | +) |
| 22 | +from iotc.aio import IoTCClient |
| 23 | + |
| 24 | +class EventHubLogger: |
| 25 | + def __init__(self, conn_str, eventhub_name): |
| 26 | + self._producer = EventHubProducerClient.from_connection_string(conn_str, eventhub_name=eventhub_name) |
| 27 | + |
| 28 | + async def _create_batch(self): |
| 29 | + self._event_data_batch = await self._producer.create_batch() |
| 30 | + |
| 31 | + async def _log(self, message): |
| 32 | + event_data_batch = await self._producer.create_batch() |
| 33 | + event_data_batch.add(EventData(message)) |
| 34 | + await self._producer.send_batch(event_data_batch) |
| 35 | + |
| 36 | + async def info(self, message): |
| 37 | + if self._log_level != IOTCLogLevel.IOTC_LOGGING_DISABLED: |
| 38 | + await self._log(message) |
| 39 | + |
| 40 | + async def debug(self, message): |
| 41 | + if self._log_level == IOTCLogLevel.IOTC_LOGGING_ALL: |
| 42 | + await self._log(message) |
| 43 | + |
| 44 | + def set_log_level(self, log_level): |
| 45 | + self._log_level = log_level |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +device_id = config["DEVICE_M3"]["DeviceId"] |
| 50 | +scope_id = config["DEVICE_M3"]["ScopeId"] |
| 51 | +key = config["DEVICE_M3"]["DeviceKey"] |
| 52 | +hub_name = config["DEVICE_M3"]["HubName"] |
| 53 | + |
| 54 | +event_hub_conn_str = config['EventHub']['ConnectionString'] |
| 55 | +event_hub_name = config['EventHub']['EventHubName'] |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +class MemStorage(Storage): |
| 60 | + def retrieve(self): |
| 61 | + return CredentialsCache( |
| 62 | + hub_name, |
| 63 | + device_id, |
| 64 | + key, |
| 65 | + ) |
| 66 | + |
| 67 | + def persist(self, credentials): |
| 68 | + # a further option would be updating config file with latest hub name |
| 69 | + return None |
| 70 | + |
| 71 | + |
| 72 | +# optional model Id for auto-provisioning |
| 73 | +try: |
| 74 | + model_id = config["DEVICE_M3"]["ModelId"] |
| 75 | +except: |
| 76 | + model_id = None |
| 77 | + |
| 78 | + |
| 79 | +async def on_props(property_name, property_value, component_name): |
| 80 | + print("Received {}:{}".format(property_name, property_value)) |
| 81 | + return True |
| 82 | + |
| 83 | + |
| 84 | +async def on_commands(command: Command): |
| 85 | + print("Received command {} with value {}".format(command.name, command.value)) |
| 86 | + await command.reply() |
| 87 | + |
| 88 | + |
| 89 | +async def on_enqueued_commands(command:Command): |
| 90 | + print("Received offline command {} with value {}".format(command.name, command.value)) |
| 91 | + |
| 92 | + |
| 93 | +# change connect type to reflect the used key (device or group) |
| 94 | +client = IoTCClient( |
| 95 | + device_id, |
| 96 | + scope_id, |
| 97 | + IOTCConnectType.IOTC_CONNECT_DEVICE_KEY, |
| 98 | + key, |
| 99 | + logger=EventHubLogger(event_hub_conn_str,event_hub_name) |
| 100 | + storage=MemStorage(), |
| 101 | +) |
| 102 | +if model_id != None: |
| 103 | + client.set_model_id(model_id) |
| 104 | + |
| 105 | +client.set_log_level(IOTCLogLevel.IOTC_LOGGING_ALL) |
| 106 | +client.on(IOTCEvents.IOTC_PROPERTIES, on_props) |
| 107 | +client.on(IOTCEvents.IOTC_COMMAND, on_commands) |
| 108 | +client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands) |
| 109 | + |
| 110 | +async def main(): |
| 111 | + await client.connect() |
| 112 | + await client.send_property({"writeableProp": 50}) |
| 113 | + |
| 114 | + while client.is_connected(): |
| 115 | + print("client connected {}".format(client._device_client.connected)) |
| 116 | + await client.send_telemetry( |
| 117 | + { |
| 118 | + "acceleration": { |
| 119 | + "x": str(randint(20, 45)), |
| 120 | + "y": str(randint(20, 45)), |
| 121 | + "z": str(randint(20, 45)), |
| 122 | + } |
| 123 | + } |
| 124 | + ) |
| 125 | + await asyncio.sleep(3) |
| 126 | + |
| 127 | +asyncio.run(main()) |
0 commit comments