Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ It hides some of the complexities of the official Azure IoT SDK and uses IoT Cen

### Disclaimer

> **This library is experimental and has the purpose of providing an easy to use solution for prototyping and small projects. Although stable and actively maintained, its use in production is discouraged.
> Please refer to official [Azure IoT Python SDK](https://github.com/Azure/azure-iot-sdk-python) when building production products.**
> **This library is experimental and has the purpose of providing an easy to use solution for prototyping and small projects. Its use in production is discouraged.
The library is going to be archived soon so we suggest new developments to start using official Azure IoT SDK.**

> Please refer to [Azure IoT Python SDK](https://github.com/Azure/azure-iot-sdk-python) when building production products.**

_If you're looking for the v0.x.x client library, it is now preserved [here](https://github.com/obastemur/iot_client/tree/master/python).
Latest version on pypi is 0.3.9_
Expand Down
33 changes: 16 additions & 17 deletions samples/async_device_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
device_id = config["DEVICE_M3"]["DeviceId"]
scope_id = config["DEVICE_M3"]["ScopeId"]
key = config["DEVICE_M3"]["DeviceKey"]
hub_name = config["DEVICE_M3"]["HubName"]
# hub_name = config["SMARTPHONE"]["HubName"]


class MemStorage(Storage):
def retrieve(self):
return CredentialsCache(
hub_name,
device_id,
key,
)
# return CredentialsCache(
# hub_name,
# device_id,
# key,
# )
return None

def persist(self, credentials):
# a further option would be updating config file with latest hub name
Expand All @@ -42,7 +43,7 @@ def persist(self, credentials):

# optional model Id for auto-provisioning
try:
model_id = config["DEVICE_M3"]["ModelId"]
model_id = config["SMARTPHONE"]["ModelId"]
except:
model_id = None

Expand Down Expand Up @@ -89,17 +90,15 @@ async def main():
await client.connect()
await client.send_property({"writeableProp": 50})

while client.is_connected():
print("client connected {}".format(client._device_client.connected))
await client.send_telemetry(
{
"acceleration": {
"x": str(randint(20, 45)),
"y": str(randint(20, 45)),
"z": str(randint(20, 45)),
while not client.terminated():
if client.is_connected():
await client.send_telemetry(
{
"temperature": randint(20, 45)
},{
"$.sub": "firstcomponent"
}
}
)
)
await asyncio.sleep(3)

asyncio.run(main())
52 changes: 26 additions & 26 deletions samples/async_eventhub_logger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
from azure.eventhub import EventHubProducerClient, EventData
from iotc.aio import IoTCClient
from iotc import (
IOTCConnectType,
IOTCLogLevel,
IOTCEvents,
Command,
CredentialsCache,
Storage,
)
import os
import asyncio
import configparser
Expand All @@ -11,19 +21,11 @@
if config["DEFAULT"].getboolean("Local"):
sys.path.insert(0, "src")

from iotc import (
IOTCConnectType,
IOTCLogLevel,
IOTCEvents,
Command,
CredentialsCache,
Storage,
)
from iotc.aio import IoTCClient

class EventHubLogger:
def __init__(self, conn_str, eventhub_name):
self._producer = EventHubProducerClient.from_connection_string(conn_str, eventhub_name=eventhub_name)
self._producer = EventHubProducerClient.from_connection_string(
conn_str, eventhub_name=eventhub_name)

async def _create_batch(self):
self._event_data_batch = await self._producer.create_batch()
Expand All @@ -45,7 +47,6 @@ def set_log_level(self, log_level):
self._log_level = log_level



device_id = config["DEVICE_M3"]["DeviceId"]
scope_id = config["DEVICE_M3"]["ScopeId"]
key = config["DEVICE_M3"]["DeviceKey"]
Expand All @@ -55,7 +56,6 @@ def set_log_level(self, log_level):
event_hub_name = config['EventHub']['EventHubName']



class MemStorage(Storage):
def retrieve(self):
return CredentialsCache(
Expand Down Expand Up @@ -86,8 +86,9 @@ async def on_commands(command: Command):
await command.reply()


async def on_enqueued_commands(command:Command):
print("Received offline command {} with value {}".format(command.name, command.value))
async def on_enqueued_commands(command: Command):
print("Received offline command {} with value {}".format(
command.name, command.value))


# change connect type to reflect the used key (device or group)
Expand All @@ -96,7 +97,7 @@ async def on_enqueued_commands(command:Command):
scope_id,
IOTCConnectType.IOTC_CONNECT_DEVICE_KEY,
key,
logger=EventHubLogger(event_hub_conn_str,event_hub_name)
logger=EventHubLogger(event_hub_conn_str, event_hub_name),
storage=MemStorage(),
)
if model_id != None:
Expand All @@ -107,21 +108,20 @@ async def on_enqueued_commands(command:Command):
client.on(IOTCEvents.IOTC_COMMAND, on_commands)
client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands)


async def main():
await client.connect()
await client.send_property({"writeableProp": 50})

while client.is_connected():
print("client connected {}".format(client._device_client.connected))
await client.send_telemetry(
{
"acceleration": {
"x": str(randint(20, 45)),
"y": str(randint(20, 45)),
"z": str(randint(20, 45)),

while not client.terminated():
if client.is_connected():
await client.send_telemetry(
{
"temperature": randint(20, 45)
}, {
"$.sub": "firstcomponent"
}
}
)
)
await asyncio.sleep(3)

asyncio.run(main())
21 changes: 10 additions & 11 deletions samples/async_file_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import configparser
import sys
import logging

from random import randint

Expand Down Expand Up @@ -94,7 +95,7 @@ async def on_enqueued_commands(command:Command):
scope_id,
IOTCConnectType.IOTC_CONNECT_DEVICE_KEY,
key,
logger=FileLogger(log_path)
logger=FileLogger(log_path),
storage=MemStorage(),
)
if model_id != None:
Expand All @@ -109,17 +110,15 @@ async def main():
await client.connect()
await client.send_property({"writeableProp": 50})

while client.is_connected():
print("client connected {}".format(client._device_client.connected))
await client.send_telemetry(
{
"acceleration": {
"x": str(randint(20, 45)),
"y": str(randint(20, 45)),
"z": str(randint(20, 45)),
while not client.terminated():
if client.is_connected():
await client.send_telemetry(
{
"temperature": randint(20, 45)
},{
"$.sub": "firstcomponent"
}
}
)
)
await asyncio.sleep(3)

asyncio.run(main())
18 changes: 8 additions & 10 deletions samples/async_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,15 @@ async def main():
await client.connect()
await client.send_property({"writeableProp": 50})

while client.is_connected():
print("client connected {}".format(client._device_client.connected))
await client.send_telemetry(
{
"acceleration": {
"x": str(randint(20, 45)),
"y": str(randint(20, 45)),
"z": str(randint(20, 45)),
while not client.terminated():
if client.is_connected():
await client.send_telemetry(
{
"temperature": randint(20, 45)
},{
"$.sub": "firstcomponent"
}
}
)
)
await asyncio.sleep(3)

asyncio.run(main())
18 changes: 8 additions & 10 deletions samples/sync_device_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,15 @@ def main():
client.connect()
client.send_property({"writeableProp": 50})

while client.is_connected():
print("client connected {}".format(client._device_client.connected))
client.send_telemetry(
{
"acceleration": {
"x": str(randint(20, 45)),
"y": str(randint(20, 45)),
"z": str(randint(20, 45)),
while not client.terminated():
if client.is_connected():
client.send_telemetry(
{
"temperature": randint(20, 45)
}, {
"$.sub": "firstcomponent"
}
}
)
)
time.sleep(3)

main()
19 changes: 9 additions & 10 deletions samples/sync_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import configparser
import sys
from random import randint
import time

config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__),'samples.ini'))
Expand Down Expand Up @@ -73,17 +74,15 @@ def main():
client.connect()
client.send_property({"writeableProp": 50})

while client.is_connected():
print("client connected {}".format(client._device_client.connected))
client.send_telemetry(
{
"acceleration": {
"x": str(randint(20, 45)),
"y": str(randint(20, 45)),
"z": str(randint(20, 45)),
while not client.terminated():
if client.is_connected():
client.send_telemetry(
{
"temperature": randint(20, 45)
}, {
"$.sub": "firstcomponent"
}
}
)
)
time.sleep(3)

main()
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
with open("README.md", "r") as fh:
long_description = fh.read()

version = "1.1.0"
version = "1.1.1"

setuptools.setup(
name='iotc',
Expand All @@ -24,10 +24,8 @@
classifiers=[
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.7+'
],
include_package_data=True,
install_requires=["azure-iot-device"]
Expand Down
Loading