Skip to content

Commit bc54053

Browse files
authored
Merge pull request #5 from iot-for-all/develop
Support for IoT Central M3 apps and hub failover
2 parents d828454 + 8ad8d95 commit bc54053

File tree

12 files changed

+364
-433
lines changed

12 files changed

+364
-433
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ It hides some of the complexities of the official Azure IoT SDK and uses IoT Cen
1111

1212
### Disclaimer
1313

14-
> **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.
15-
> Please refer to official [Azure IoT Python SDK](https://github.com/Azure/azure-iot-sdk-python) when building production products.**
14+
> **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.
15+
The library is going to be archived soon so we suggest new developments to start using official Azure IoT SDK.**
16+
17+
> Please refer to [Azure IoT Python SDK](https://github.com/Azure/azure-iot-sdk-python) when building production products.**
1618
1719
_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).
1820
Latest version on pypi is 0.3.9_

samples/async_device_key.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424
device_id = config["DEVICE_M3"]["DeviceId"]
2525
scope_id = config["DEVICE_M3"]["ScopeId"]
2626
key = config["DEVICE_M3"]["DeviceKey"]
27-
hub_name = config["DEVICE_M3"]["HubName"]
27+
# hub_name = config["SMARTPHONE"]["HubName"]
2828

2929

3030
class MemStorage(Storage):
3131
def retrieve(self):
32-
return CredentialsCache(
33-
hub_name,
34-
device_id,
35-
key,
36-
)
32+
# return CredentialsCache(
33+
# hub_name,
34+
# device_id,
35+
# key,
36+
# )
37+
return None
3738

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

4344
# optional model Id for auto-provisioning
4445
try:
45-
model_id = config["DEVICE_M3"]["ModelId"]
46+
model_id = config["SMARTPHONE"]["ModelId"]
4647
except:
4748
model_id = None
4849

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

92-
while client.is_connected():
93-
print("client connected {}".format(client._device_client.connected))
94-
await client.send_telemetry(
95-
{
96-
"acceleration": {
97-
"x": str(randint(20, 45)),
98-
"y": str(randint(20, 45)),
99-
"z": str(randint(20, 45)),
93+
while not client.terminated():
94+
if client.is_connected():
95+
await client.send_telemetry(
96+
{
97+
"temperature": randint(20, 45)
98+
},{
99+
"$.sub": "firstcomponent"
100100
}
101-
}
102-
)
101+
)
103102
await asyncio.sleep(3)
104103

105104
asyncio.run(main())

samples/async_eventhub_logger.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
from azure.eventhub import EventHubProducerClient, EventData
2+
from iotc.aio import IoTCClient
3+
from iotc import (
4+
IOTCConnectType,
5+
IOTCLogLevel,
6+
IOTCEvents,
7+
Command,
8+
CredentialsCache,
9+
Storage,
10+
)
111
import os
212
import asyncio
313
import configparser
@@ -11,19 +21,11 @@
1121
if config["DEFAULT"].getboolean("Local"):
1222
sys.path.insert(0, "src")
1323

14-
from iotc import (
15-
IOTCConnectType,
16-
IOTCLogLevel,
17-
IOTCEvents,
18-
Command,
19-
CredentialsCache,
20-
Storage,
21-
)
22-
from iotc.aio import IoTCClient
2324

2425
class EventHubLogger:
2526
def __init__(self, conn_str, eventhub_name):
26-
self._producer = EventHubProducerClient.from_connection_string(conn_str, eventhub_name=eventhub_name)
27+
self._producer = EventHubProducerClient.from_connection_string(
28+
conn_str, eventhub_name=eventhub_name)
2729

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

4749

48-
4950
device_id = config["DEVICE_M3"]["DeviceId"]
5051
scope_id = config["DEVICE_M3"]["ScopeId"]
5152
key = config["DEVICE_M3"]["DeviceKey"]
@@ -55,7 +56,6 @@ def set_log_level(self, log_level):
5556
event_hub_name = config['EventHub']['EventHubName']
5657

5758

58-
5959
class MemStorage(Storage):
6060
def retrieve(self):
6161
return CredentialsCache(
@@ -86,8 +86,9 @@ async def on_commands(command: Command):
8686
await command.reply()
8787

8888

89-
async def on_enqueued_commands(command:Command):
90-
print("Received offline command {} with value {}".format(command.name, command.value))
89+
async def on_enqueued_commands(command: Command):
90+
print("Received offline command {} with value {}".format(
91+
command.name, command.value))
9192

9293

9394
# change connect type to reflect the used key (device or group)
@@ -96,7 +97,7 @@ async def on_enqueued_commands(command:Command):
9697
scope_id,
9798
IOTCConnectType.IOTC_CONNECT_DEVICE_KEY,
9899
key,
99-
logger=EventHubLogger(event_hub_conn_str,event_hub_name)
100+
logger=EventHubLogger(event_hub_conn_str, event_hub_name),
100101
storage=MemStorage(),
101102
)
102103
if model_id != None:
@@ -107,21 +108,20 @@ async def on_enqueued_commands(command:Command):
107108
client.on(IOTCEvents.IOTC_COMMAND, on_commands)
108109
client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands)
109110

111+
110112
async def main():
111113
await client.connect()
112114
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)),
115+
116+
while not client.terminated():
117+
if client.is_connected():
118+
await client.send_telemetry(
119+
{
120+
"temperature": randint(20, 45)
121+
}, {
122+
"$.sub": "firstcomponent"
122123
}
123-
}
124-
)
124+
)
125125
await asyncio.sleep(3)
126126

127127
asyncio.run(main())

samples/async_file_logger.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import asyncio
33
import configparser
44
import sys
5+
import logging
56

67
from random import randint
78

@@ -94,7 +95,7 @@ async def on_enqueued_commands(command:Command):
9495
scope_id,
9596
IOTCConnectType.IOTC_CONNECT_DEVICE_KEY,
9697
key,
97-
logger=FileLogger(log_path)
98+
logger=FileLogger(log_path),
9899
storage=MemStorage(),
99100
)
100101
if model_id != None:
@@ -109,17 +110,15 @@ async def main():
109110
await client.connect()
110111
await client.send_property({"writeableProp": 50})
111112

112-
while client.is_connected():
113-
print("client connected {}".format(client._device_client.connected))
114-
await client.send_telemetry(
115-
{
116-
"acceleration": {
117-
"x": str(randint(20, 45)),
118-
"y": str(randint(20, 45)),
119-
"z": str(randint(20, 45)),
113+
while not client.terminated():
114+
if client.is_connected():
115+
await client.send_telemetry(
116+
{
117+
"temperature": randint(20, 45)
118+
},{
119+
"$.sub": "firstcomponent"
120120
}
121-
}
122-
)
121+
)
123122
await asyncio.sleep(3)
124123

125124
asyncio.run(main())

samples/async_x509.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,15 @@ async def main():
7474
await client.connect()
7575
await client.send_property({"writeableProp": 50})
7676

77-
while client.is_connected():
78-
print("client connected {}".format(client._device_client.connected))
79-
await client.send_telemetry(
80-
{
81-
"acceleration": {
82-
"x": str(randint(20, 45)),
83-
"y": str(randint(20, 45)),
84-
"z": str(randint(20, 45)),
77+
while not client.terminated():
78+
if client.is_connected():
79+
await client.send_telemetry(
80+
{
81+
"temperature": randint(20, 45)
82+
},{
83+
"$.sub": "firstcomponent"
8584
}
86-
}
87-
)
85+
)
8886
await asyncio.sleep(3)
8987

9088
asyncio.run(main())

samples/sync_device_key.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,15 @@ def main():
8282
client.connect()
8383
client.send_property({"writeableProp": 50})
8484

85-
while client.is_connected():
86-
print("client connected {}".format(client._device_client.connected))
87-
client.send_telemetry(
88-
{
89-
"acceleration": {
90-
"x": str(randint(20, 45)),
91-
"y": str(randint(20, 45)),
92-
"z": str(randint(20, 45)),
85+
while not client.terminated():
86+
if client.is_connected():
87+
client.send_telemetry(
88+
{
89+
"temperature": randint(20, 45)
90+
}, {
91+
"$.sub": "firstcomponent"
9392
}
94-
}
95-
)
93+
)
9694
time.sleep(3)
9795

9896
main()

samples/sync_x509.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import configparser
33
import sys
44
from random import randint
5+
import time
56

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

76-
while client.is_connected():
77-
print("client connected {}".format(client._device_client.connected))
78-
client.send_telemetry(
79-
{
80-
"acceleration": {
81-
"x": str(randint(20, 45)),
82-
"y": str(randint(20, 45)),
83-
"z": str(randint(20, 45)),
77+
while not client.terminated():
78+
if client.is_connected():
79+
client.send_telemetry(
80+
{
81+
"temperature": randint(20, 45)
82+
}, {
83+
"$.sub": "firstcomponent"
8484
}
85-
}
86-
)
85+
)
8786
time.sleep(3)
8887

8988
main()

setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
with open("README.md", "r") as fh:
66
long_description = fh.read()
77

8-
version = "1.1.0"
8+
version = "1.1.1"
99

1010
setuptools.setup(
1111
name='iotc',
@@ -24,10 +24,8 @@
2424
classifiers=[
2525
'License :: OSI Approved :: MIT License',
2626
'Programming Language :: Python',
27-
'Programming Language :: Python :: 2',
28-
'Programming Language :: Python :: 2.7',
2927
'Programming Language :: Python :: 3',
30-
'Programming Language :: Python :: 3.8',
28+
'Programming Language :: Python :: 3.7+'
3129
],
3230
include_package_data=True,
3331
install_requires=["azure-iot-device"]

0 commit comments

Comments
 (0)