|
1 |
| -import sys |
2 | 1 | import os
|
3 | 2 | import configparser
|
| 3 | +import sys |
4 | 4 | import time
|
5 |
| -from random import randint |
6 | 5 |
|
| 6 | +from random import randint |
7 | 7 |
|
8 | 8 | 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")) |
10 | 10 |
|
| 11 | +if config["DEFAULT"].getboolean("Local"): |
| 12 | + sys.path.insert(0, "src") |
11 | 13 |
|
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 |
14 | 23 |
|
15 |
| -from iotc import IoTCClient, IOTCConnectType, IOTCLogLevel, IOTCEvents,Storage,CredentialsCache |
16 |
| - |
17 |
| - |
18 |
| -# Change config section name to reflect sample.ini |
19 | 24 | device_id = config["DEVICE_M3"]["DeviceId"]
|
20 | 25 | scope_id = config["DEVICE_M3"]["ScopeId"]
|
21 | 26 | key = config["DEVICE_M3"]["DeviceKey"]
|
22 | 27 |
|
| 28 | + |
23 | 29 | class MemStorage(Storage):
|
24 | 30 | 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 | + |
26 | 37 | def persist(self, credentials):
|
27 | 38 | return None
|
28 | 39 |
|
| 40 | + |
29 | 41 | # optional model Id for auto-provisioning
|
30 | 42 | try:
|
31 | 43 | model_id = config["DEVICE_M3"]["ModelId"]
|
32 | 44 | except:
|
33 | 45 | model_id = None
|
34 | 46 |
|
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)) |
37 | 50 | return True
|
38 | 51 |
|
39 | 52 |
|
40 | 53 | 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)) |
42 | 55 | command.reply()
|
43 | 56 |
|
44 | 57 |
|
| 58 | +def on_enqueued_commands(command): |
| 59 | + print("Received offline command {} with value {}".format(command.name, command.value)) |
45 | 60 |
|
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 | +) |
49 | 70 | if model_id != None:
|
50 | 71 | client.set_model_id(model_id)
|
51 | 72 |
|
52 | 73 | client.set_log_level(IOTCLogLevel.IOTC_LOGGING_ALL)
|
53 | 74 | client.on(IOTCEvents.IOTC_PROPERTIES, on_props)
|
54 | 75 | client.on(IOTCEvents.IOTC_COMMAND, on_commands)
|
55 |
| - |
| 76 | +client.on(IOTCEvents.IOTC_ENQUEUED_COMMAND, on_enqueued_commands) |
56 | 77 |
|
57 | 78 |
|
58 | 79 | def main():
|
59 | 80 | client.connect()
|
| 81 | + client.send_property({"writeableProp": 50}) |
| 82 | + |
60 | 83 | 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 | + ) |
66 | 94 | time.sleep(3)
|
67 | 95 |
|
68 |
| - |
69 | 96 | main()
|
0 commit comments