Skip to content

Commit cf4e832

Browse files
author
lucadruda
committed
restructure
1 parent 995cac3 commit cf4e832

File tree

11 files changed

+331
-132
lines changed

11 files changed

+331
-132
lines changed

samples/py2.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,96 @@
1-
import sys
21
import os
32
import configparser
3+
import sys
44
import time
5-
from random import randint
65

6+
from random import randint
77

88
config = configparser.ConfigParser()
9-
config.read(os.path.join(os.path.dirname(__file__),'samples.ini'))
9+
config.read(os.path.join(os.path.dirname(__file__), "samples.ini"))
1010

11+
if config["DEFAULT"].getboolean("Local"):
12+
sys.path.insert(0, "src")
1113

12-
if config['DEFAULT'].getboolean('Local'):
13-
sys.path.insert(0, 'src')
14+
from iotc import (
15+
IOTCConnectType,
16+
IOTCLogLevel,
17+
IOTCEvents,
18+
Command,
19+
CredentialsCache,
20+
Storage,
21+
)
22+
from iotc import IoTCClient
1423

15-
from iotc import IoTCClient, IOTCConnectType, IOTCLogLevel, IOTCEvents,Storage,CredentialsCache
16-
17-
18-
# Change config section name to reflect sample.ini
1924
device_id = config["DEVICE_M3"]["DeviceId"]
2025
scope_id = config["DEVICE_M3"]["ScopeId"]
2126
key = config["DEVICE_M3"]["DeviceKey"]
2227

28+
2329
class MemStorage(Storage):
2430
def retrieve(self):
25-
return CredentialsCache('iotc-1f9e162c-eacc-408d-9fb2-c9926a071037.azure-devices.net','javasdkcomponents2',key)
31+
return CredentialsCache(
32+
"iotc-1f9e162c-eacc-408d-9fb2-c9926a071037.azure-devices.net",
33+
"javasdkcomponents2",
34+
key,
35+
)
36+
2637
def persist(self, credentials):
2738
return None
2839

40+
2941
# optional model Id for auto-provisioning
3042
try:
3143
model_id = config["DEVICE_M3"]["ModelId"]
3244
except:
3345
model_id = None
3446

35-
def on_props(propName, propValue):
36-
print(propValue)
47+
48+
def on_props(property_name, property_value, component_name):
49+
print("Received {}:{}".format(property_name, property_value))
3750
return True
3851

3952

4053
def on_commands(command):
41-
print('Received command {} with value {}'.format(command.name,command.value))
54+
print("Received command {} with value {}".format(command.name, command.value))
4255
command.reply()
4356

4457

58+
def on_enqueued_commands(command):
59+
print("Received offline command {} with value {}".format(command.name, command.value))
4560

46-
# see client.Device documentation above for x509 argument sample
47-
client = IoTCClient(device_id, scope_id,
48-
IOTCConnectType.IOTC_CONNECT_DEVICE_KEY, key,storage=MemStorage())
61+
62+
# change connect type to reflect the used key (device or group)
63+
client = IoTCClient(
64+
device_id,
65+
scope_id,
66+
IOTCConnectType.IOTC_CONNECT_DEVICE_KEY,
67+
key,
68+
storage=MemStorage(),
69+
)
4970
if model_id != None:
5071
client.set_model_id(model_id)
5172

5273
client.set_log_level(IOTCLogLevel.IOTC_LOGGING_ALL)
5374
client.on(IOTCEvents.IOTC_PROPERTIES, on_props)
5475
client.on(IOTCEvents.IOTC_COMMAND, on_commands)
55-
76+
client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands)
5677

5778

5879
def main():
5980
client.connect()
81+
client.send_property({"writeableProp": 50})
82+
6083
while client.is_connected():
61-
client.send_telemetry({
62-
'accelerometerX': str(randint(20, 45)),
63-
'accelerometerY': str(randint(20, 45)),
64-
"accelerometerZ": str(randint(20, 45))
65-
})
84+
print("client connected {}".format(client._device_client.connected))
85+
client.send_telemetry(
86+
{
87+
"acceleration": {
88+
"x": str(randint(20, 45)),
89+
"y": str(randint(20, 45)),
90+
"z": str(randint(20, 45)),
91+
}
92+
}
93+
)
6694
time.sleep(3)
6795

68-
6996
main()

samples/py3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ async def main():
9696
# await client.run_telemetry_loop(telemetry_loop)
9797

9898
while client.is_connected():
99+
print("client connected {}".format(client._device_client.connected))
99100
await client.send_telemetry(
100101
{
101102
"acceleration": {
@@ -106,6 +107,6 @@ async def main():
106107
}
107108
)
108109
await asyncio.sleep(3)
109-
print("Exiting")
110+
# await client.disconnect()
110111

111112
asyncio.run(main())

0 commit comments

Comments
 (0)