Skip to content

Commit abaecb7

Browse files
authored
Merge pull request #7 from iot-for-all/develop
Added helpers for storage
2 parents 30ed120 + 37cdabb commit abaecb7

14 files changed

+199
-87
lines changed

CHANGELOG

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
1.1.2 (2021-11-8)
2+
-----------------
3+
- added credentials cache serialize/deserialize methods
4+
- added "Property" model
5+
- added storage tests
6+
7+
1.1.1 (2021-06-25)
8+
-----------------
9+
- minor fixes for value wrapping
10+
11+
112
1.1.0 (2021-02-05)
213
-----------------
314
- dropped Python 2.7 support (client should continue to work without tests)

samples/async_device_key.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from random import randint
77

8+
from iotc.models import Property,Command
9+
810
config = configparser.ConfigParser()
911
config.read(os.path.join(os.path.dirname(__file__), "samples.ini"))
1012

@@ -29,11 +31,6 @@
2931

3032
class MemStorage(Storage):
3133
def retrieve(self):
32-
# return CredentialsCache(
33-
# hub_name,
34-
# device_id,
35-
# key,
36-
# )
3734
return None
3835

3936
def persist(self, credentials):
@@ -48,8 +45,8 @@ def persist(self, credentials):
4845
model_id = None
4946

5047

51-
async def on_props(property_name, property_value, component_name):
52-
print("Received {}:{}".format(property_name, property_value))
48+
async def on_props(prop:Property):
49+
print(f"Received {prop.name}:{prop.value}")
5350
return True
5451

5552

samples/async_eventhub_logger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
IOTCConnectType,
55
IOTCLogLevel,
66
IOTCEvents,
7-
Command,
87
CredentialsCache,
98
Storage,
109
)
10+
from iotc.models import Command, Property
1111
import os
1212
import asyncio
1313
import configparser
@@ -76,8 +76,8 @@ def persist(self, credentials):
7676
model_id = None
7777

7878

79-
async def on_props(property_name, property_value, component_name):
80-
print("Received {}:{}".format(property_name, property_value))
79+
async def on_props(prop: Property):
80+
print(f"Received {prop.name}:{prop.value}")
8181
return True
8282

8383

samples/async_file_logger.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
from iotc.models import Command, Property, CredentialsCache, Storage
2+
from iotc.aio import IoTCClient
3+
from iotc import (
4+
IOTCConnectType,
5+
IOTCLogLevel,
6+
IOTCEvents,
7+
)
18
import os
29
import asyncio
310
import configparser
@@ -12,22 +19,13 @@
1219
if config["DEFAULT"].getboolean("Local"):
1320
sys.path.insert(0, "src")
1421

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

2523
class FileLogger:
26-
def __init__(self,logpath,logname="iotc_py_log"):
27-
self._logger=logging.getLogger(logname)
24+
def __init__(self, logpath, logname="iotc_py_log"):
25+
self._logger = logging.getLogger(logname)
2826
self._logger.setLevel(logging.DEBUG)
29-
handler= logging.handlers.RotatingFileHandler(
30-
os.path.join(logpath,logname), maxBytes=20, backupCount=5)
27+
handler = logging.handlers.RotatingFileHandler(
28+
os.path.join(logpath, logname), maxBytes=20, backupCount=5)
3129
self._logger.addHandler(handler)
3230

3331
async def _log(self, message):
@@ -46,15 +44,13 @@ def set_log_level(self, log_level):
4644
self._log_level = log_level
4745

4846

49-
5047
device_id = config["DEVICE_M3"]["DeviceId"]
5148
scope_id = config["DEVICE_M3"]["ScopeId"]
5249
key = config["DEVICE_M3"]["DeviceKey"]
5350
hub_name = config["DEVICE_M3"]["HubName"]
5451
log_path = config['FileLog']['LogsPath']
5552

5653

57-
5854
class MemStorage(Storage):
5955
def retrieve(self):
6056
return CredentialsCache(
@@ -75,8 +71,8 @@ def persist(self, credentials):
7571
model_id = None
7672

7773

78-
async def on_props(property_name, property_value, component_name):
79-
print("Received {}:{}".format(property_name, property_value))
74+
async def on_props(prop: Property):
75+
print(f"Received {prop.name}:{prop.value}")
8076
return True
8177

8278

@@ -85,8 +81,9 @@ async def on_commands(command: Command):
8581
await command.reply()
8682

8783

88-
async def on_enqueued_commands(command:Command):
89-
print("Received offline command {} with value {}".format(command.name, command.value))
84+
async def on_enqueued_commands(command: Command):
85+
print("Received offline command {} with value {}".format(
86+
command.name, command.value))
9087

9188

9289
# change connect type to reflect the used key (device or group)
@@ -106,16 +103,17 @@ async def on_enqueued_commands(command:Command):
106103
client.on(IOTCEvents.IOTC_COMMAND, on_commands)
107104
client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands)
108105

106+
109107
async def main():
110108
await client.connect()
111109
await client.send_property({"writeableProp": 50})
112-
110+
113111
while not client.terminated():
114112
if client.is_connected():
115113
await client.send_telemetry(
116114
{
117115
"temperature": randint(20, 45)
118-
},{
116+
}, {
119117
"$.sub": "firstcomponent"
120118
}
121119
)

samples/async_x509.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
from iotc.models import Storage, CredentialsCache, Command, Property
2+
from iotc.aio import IoTCClient
3+
from iotc import IOTCConnectType, IOTCLogLevel, IOTCEvents
14
import os
25
import asyncio
36
import configparser
47
import sys
58
from random import randint
69

710
config = configparser.ConfigParser()
8-
config.read(os.path.join(os.path.dirname(__file__),'samples.ini'))
11+
config.read(os.path.join(os.path.dirname(__file__), 'samples.ini'))
912

1013
# Change config section name to reflect sample.ini
1114
device_id = config['DEVICE_A']['DeviceId']
1215
scope_id = config['DEVICE_A']['ScopeId']
1316
hub_name = config["DEVICE_A"]["HubName"]
14-
x509 = {'cert_file': config['DEVICE_A']['CertFilePath'],'key_file':config['DEVICE_A']['KeyFilePath'],'cert_phrase':config['DEVICE_A']['CertPassphrase']}
17+
x509 = {'cert_file': config['DEVICE_A']['CertFilePath'], 'key_file': config['DEVICE_A']
18+
['KeyFilePath'], 'cert_phrase': config['DEVICE_A']['CertPassphrase']}
1519

1620

1721
if config['DEFAULT'].getboolean('Local'):
1822
sys.path.insert(0, 'src')
1923

20-
from iotc import IOTCConnectType, IOTCLogLevel, IOTCEvents
21-
from iotc.aio import IoTCClient
2224

2325
class MemStorage(Storage):
2426
def retrieve(self):
@@ -40,8 +42,8 @@ def persist(self, credentials):
4042
model_id = None
4143

4244

43-
async def on_props(property_name, property_value, component_name):
44-
print("Received {}:{}".format(property_name, property_value))
45+
async def on_props(prop: Property):
46+
print(f"Received {prop.name}:{prop.value}")
4547
return True
4648

4749

@@ -50,8 +52,9 @@ async def on_commands(command: Command):
5052
await command.reply()
5153

5254

53-
async def on_enqueued_commands(command:Command):
54-
print("Received offline command {} with value {}".format(command.name, command.value))
55+
async def on_enqueued_commands(command: Command):
56+
print("Received offline command {} with value {}".format(
57+
command.name, command.value))
5558

5659

5760
# change connect type to reflect the used key (device or group)
@@ -70,16 +73,17 @@ async def on_enqueued_commands(command:Command):
7073
client.on(IOTCEvents.IOTC_COMMAND, on_commands)
7174
client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands)
7275

76+
7377
async def main():
7478
await client.connect()
7579
await client.send_property({"writeableProp": 50})
76-
80+
7781
while not client.terminated():
7882
if client.is_connected():
7983
await client.send_telemetry(
8084
{
8185
"temperature": randint(20, 45)
82-
},{
86+
}, {
8387
"$.sub": "firstcomponent"
8488
}
8589
)

samples/sync_device_key.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
from iotc.models import Command, Storage, CredentialsCache, Property
2+
from iotc import IoTCClient
3+
from iotc import (
4+
IOTCConnectType,
5+
IOTCLogLevel,
6+
IOTCEvents,
7+
)
18
import os
29
import configparser
10+
from re import M
311
import sys
412
import time
513

@@ -11,15 +19,6 @@
1119
if config["DEFAULT"].getboolean("Local"):
1220
sys.path.insert(0, "src")
1321

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

2423
device_id = config["DEVICE_M3"]["DeviceId"]
2524
scope_id = config["DEVICE_M3"]["ScopeId"]
@@ -47,8 +46,8 @@ def persist(self, credentials):
4746
model_id = None
4847

4948

50-
def on_props(property_name, property_value, component_name):
51-
print("Received {}:{}".format(property_name, property_value))
49+
def on_props(prop: Property):
50+
print(f"Received {prop.name}:{prop.value}")
5251
return True
5352

5453

@@ -58,7 +57,8 @@ def on_commands(command):
5857

5958

6059
def on_enqueued_commands(command):
61-
print("Received offline command {} with value {}".format(command.name, command.value))
60+
print("Received offline command {} with value {}".format(
61+
command.name, command.value))
6262

6363

6464
# change connect type to reflect the used key (device or group)
@@ -81,7 +81,7 @@ def on_enqueued_commands(command):
8181
def main():
8282
client.connect()
8383
client.send_property({"writeableProp": 50})
84-
84+
8585
while not client.terminated():
8686
if client.is_connected():
8787
client.send_telemetry(
@@ -93,4 +93,5 @@ def main():
9393
)
9494
time.sleep(3)
9595

96+
9697
main()

samples/sync_x509.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1+
from iotc.models import CredentialsCache, Storage, Property, Command
2+
from iotc import IOTCConnectType, IOTCLogLevel, IOTCEvents, IoTCClient
13
import os
24
import configparser
35
import sys
46
from random import randint
57
import time
68

79
config = configparser.ConfigParser()
8-
config.read(os.path.join(os.path.dirname(__file__),'samples.ini'))
10+
config.read(os.path.join(os.path.dirname(__file__), 'samples.ini'))
911

1012
# Change config section name to reflect sample.ini
1113
device_id = config['DEVICE_A']['DeviceId']
1214
scope_id = config['DEVICE_A']['ScopeId']
1315
hub_name = config["DEVICE_A"]["HubName"]
14-
x509 = {'cert_file': config['DEVICE_A']['CertFilePath'],'key_file':config['DEVICE_A']['KeyFilePath'],'cert_phrase':config['DEVICE_A']['CertPassphrase']}
16+
x509 = {'cert_file': config['DEVICE_A']['CertFilePath'], 'key_file': config['DEVICE_A']
17+
['KeyFilePath'], 'cert_phrase': config['DEVICE_A']['CertPassphrase']}
1518

1619

1720
if config['DEFAULT'].getboolean('Local'):
1821
sys.path.insert(0, 'src')
1922

20-
from iotc import IOTCConnectType, IOTCLogLevel, IOTCEvents,IoTCClient
21-
2223

2324
class MemStorage(Storage):
2425
def retrieve(self):
@@ -40,8 +41,8 @@ def persist(self, credentials):
4041
model_id = None
4142

4243

43-
def on_props(property_name, property_value, component_name):
44-
print("Received {}:{}".format(property_name, property_value))
44+
def on_props(prop: Property):
45+
print(f"Received {prop.name}:{prop.value}")
4546
return True
4647

4748

@@ -50,8 +51,9 @@ def on_commands(command: Command):
5051
command.reply()
5152

5253

53-
def on_enqueued_commands(command:Command):
54-
print("Received offline command {} with value {}".format(command.name, command.value))
54+
def on_enqueued_commands(command: Command):
55+
print("Received offline command {} with value {}".format(
56+
command.name, command.value))
5557

5658

5759
# change connect type to reflect the used key (device or group)
@@ -70,10 +72,11 @@ def on_enqueued_commands(command:Command):
7072
client.on(IOTCEvents.IOTC_COMMAND, on_commands)
7173
client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands)
7274

75+
7376
def main():
7477
client.connect()
7578
client.send_property({"writeableProp": 50})
76-
79+
7780
while not client.terminated():
7881
if client.is_connected():
7982
client.send_telemetry(
@@ -85,4 +88,5 @@ def main():
8588
)
8689
time.sleep(3)
8790

91+
8892
main()

setup.py

Lines changed: 1 addition & 1 deletion
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.1"
8+
version = "1.1.2"
99

1010
setuptools.setup(
1111
name='iotc',

0 commit comments

Comments
 (0)