Skip to content

Commit 93bf13d

Browse files
committed
add modelid
1 parent f1df257 commit 93bf13d

File tree

6 files changed

+141
-64
lines changed

6 files changed

+141
-64
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ build
1616
dist
1717

1818
# virtualenv
19-
.env2
20-
.env3
19+
.pyenv
2120
Include/
2221
Lib/
2322
Scripts/

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.1.3 (2022-10-20)
2+
-----------------
3+
- added model_id to iothub connection string
4+
15
1.1.2 (2021-11-8)
26
-----------------
37
- added credentials cache serialize/deserialize methods

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.2"
8+
version = "1.1.3"
99

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

src/iotc/__init__.py

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,15 @@ def set_log_level(self, log_level):
109109

110110

111111
class AbstractClient:
112-
def __init__(self, device_id, scope_id, cred_type, key_or_cert, storage=None, max_connection_attempts=5):
112+
def __init__(
113+
self,
114+
device_id,
115+
scope_id,
116+
cred_type,
117+
key_or_cert,
118+
storage=None,
119+
max_connection_attempts=5,
120+
):
113121
self._device_id = device_id
114122
self._scope_id = scope_id
115123
self._cred_type = cred_type
@@ -168,8 +176,7 @@ def set_content_encoding(self, content_encoding):
168176
self._content_encoding = content_encoding
169177

170178
def _prepare_message(self, payload, properties):
171-
msg = Message(payload, uuid.uuid4(),
172-
self._content_encoding, self._content_type)
179+
msg = Message(payload, uuid.uuid4(), self._content_encoding, self._content_type)
173180
if bool(properties):
174181
for prop in properties:
175182
msg.custom_properties[prop] = properties[prop]
@@ -186,34 +193,43 @@ def on(self, eventname, callback):
186193

187194
def _sync_twin(self):
188195
try:
189-
desired = self._twin['desired']
190-
desired_version = self._twin['desired']['$version']
196+
desired = self._twin["desired"]
197+
desired_version = self._twin["desired"]["$version"]
191198
except KeyError:
192199
return
193200
try:
194-
reported = self._twin['reported']
201+
reported = self._twin["reported"]
195202
except KeyError:
196203
return
197204
patch = {}
198205
for desired_prop in desired:
199206
print("Syncing property '{}'".format(desired_prop))
200-
if desired_prop == '$version':
207+
if desired_prop == "$version":
201208
continue
202209
# is a component
203-
if str(type(desired[desired_prop])) == "<class 'dict'>" and '__t' in desired[desired_prop]:
210+
if (
211+
str(type(desired[desired_prop])) == "<class 'dict'>"
212+
and "__t" in desired[desired_prop]
213+
):
204214
desired_prop_component = desired_prop
205215
for desired_prop_name in desired[desired_prop_component]:
206216
if desired_prop_name == "__t":
207217
continue
208218
has_reported = False
209219
try:
210-
has_reported = reported[desired_prop_component][desired_prop_name]
220+
has_reported = reported[desired_prop_component][
221+
desired_prop_name
222+
]
211223
except KeyError:
212224
pass
213225
if not has_reported: # no reported yet. send desired
214226
patch[desired_prop_component] = desired[desired_prop_component]
215227
# desired is more recent
216-
if has_reported and 'av' in has_reported and has_reported['av'] < desired_version:
228+
if (
229+
has_reported
230+
and "av" in has_reported
231+
and has_reported["av"] < desired_version
232+
):
217233
patch[desired_prop_component] = desired[desired_prop_component]
218234
else: # default component
219235
has_reported = False
@@ -224,22 +240,39 @@ def _sync_twin(self):
224240
if not has_reported: # no reported yet. send desired
225241
patch[desired_prop] = desired[desired_prop]
226242
# desired is more recent
227-
if has_reported and 'av' in has_reported and has_reported['av'] < desired_version:
243+
if (
244+
has_reported
245+
and "av" in has_reported
246+
and has_reported["av"] < desired_version
247+
):
228248
patch[desired_prop] = desired[desired_prop]
229249

230250
if patch: # there are desired to ack
231-
patch['$version'] = desired_version
251+
patch["$version"] = desired_version
232252
return patch
233253
else:
234254
return None
235255

236256

237257
class IoTCClient(AbstractClient):
238258
def __init__(
239-
self, device_id, scope_id, cred_type, key_or_cert, logger=None, storage=None, max_connection_attempts=5
259+
self,
260+
device_id,
261+
scope_id,
262+
cred_type,
263+
key_or_cert,
264+
logger=None,
265+
storage=None,
266+
max_connection_attempts=5,
240267
):
241268
AbstractClient.__init__(
242-
self, device_id, scope_id, cred_type, key_or_cert, storage, max_connection_attempts
269+
self,
270+
device_id,
271+
scope_id,
272+
cred_type,
273+
key_or_cert,
274+
storage,
275+
max_connection_attempts,
243276
)
244277
if logger is None:
245278
self._logger = ConsoleLogger(IOTCLogLevel.IOTC_LOGGING_API_ONLY)
@@ -281,8 +314,8 @@ def _handle_property_ack(
281314
"value": property_value,
282315
"ac": 200,
283316
"ad": "Completed",
284-
"av": property_version
285-
}
317+
"av": property_version,
318+
},
286319
}
287320
}
288321
)
@@ -311,8 +344,9 @@ def _update_properties(self, patch, prop_cb):
311344

312345
# check if component
313346
try:
314-
is_component = str(
315-
type(patch[prop])) == "<class 'dict'>" and patch[prop]["__t"]
347+
is_component = (
348+
str(type(patch[prop])) == "<class 'dict'>" and patch[prop]["__t"]
349+
)
316350
except KeyError:
317351
pass
318352
if is_component:
@@ -332,9 +366,7 @@ def _update_properties(self, patch, prop_cb):
332366
prop,
333367
)
334368
else:
335-
self._handle_property_ack(
336-
prop_cb, prop, patch[prop], patch["$version"]
337-
)
369+
self._handle_property_ack(prop_cb, prop, patch[prop], patch["$version"])
338370

339371
def _on_properties(self, patch):
340372
self._logger.debug("Setup properties listener")
@@ -435,7 +467,9 @@ def connect(self, force_dps=False):
435467
Connects the device.
436468
:raises exception: If connection fails
437469
"""
438-
if self._connection_attempts_count > self._max_connection_attempts: # max number of retries. exit
470+
if (
471+
self._connection_attempts_count > self._max_connection_attempts
472+
): # max number of retries. exit
439473
self._terminate = True
440474
self._connecting = False
441475
return
@@ -459,8 +493,7 @@ def connect(self, force_dps=False):
459493
self._key_or_cert = self._compute_derived_symmetric_key(
460494
self._key_or_cert, self._device_id
461495
)
462-
self._logger.debug(
463-
"Device key: {}".format(self._key_or_cert))
496+
self._logger.debug("Device key: {}".format(self._key_or_cert))
464497

465498
self._provisioning_client = (
466499
ProvisioningDeviceClient.create_from_symmetric_key(
@@ -475,8 +508,7 @@ def connect(self, force_dps=False):
475508
self._cert_file = self._key_or_cert["cert_file"]
476509
try:
477510
self._cert_phrase = self._key_or_cert["cert_phrase"]
478-
x509 = X509(self._cert_file, self._key_file,
479-
self._cert_phrase)
511+
x509 = X509(self._cert_file, self._key_file, self._cert_phrase)
480512
except:
481513
self._logger.debug(
482514
"No passphrase available for certificate. Trying without it"
@@ -494,7 +526,8 @@ def connect(self, force_dps=False):
494526

495527
if self._model_id:
496528
self._provisioning_client.provisioning_payload = {
497-
"iotcModelId": self._model_id
529+
"iotcModelId": self._model_id,
530+
"modelId": self._model_id,
498531
}
499532
try:
500533
registration_result = self._provisioning_client.register()
@@ -535,19 +568,25 @@ def connect(self, force_dps=False):
535568
IOTCConnectType.IOTC_CONNECT_SYMM_KEY,
536569
):
537570
self._device_client = IoTHubDeviceClient.create_from_connection_string(
538-
_credentials.connection_string
571+
_credentials.connection_string, product_info=self._model_id
539572
)
540573
else:
541-
if 'cert_phrase' in _credentials.certificate:
574+
if "cert_phrase" in _credentials.certificate:
542575
x509 = X509(
543-
_credentials.certificate['cert_file'], _credentials.certificate['key_file'], _credentials.certificate['cert_phrase'])
576+
_credentials.certificate["cert_file"],
577+
_credentials.certificate["key_file"],
578+
_credentials.certificate["cert_phrase"],
579+
)
544580
else:
545581
x509 = X509(
546-
_credentials.certificate['cert_file'], _credentials.certificate['key_file'])
582+
_credentials.certificate["cert_file"],
583+
_credentials.certificate["key_file"],
584+
)
547585
self._device_client = IoTHubDeviceClient.create_from_x509_certificate(
548586
x509=x509,
549587
hostname=_credentials.hub_name,
550588
device_id=_credentials.device_id,
589+
product_info=self._model_id,
551590
)
552591
self._device_client.connect()
553592
self._logger.debug("Device connected")
@@ -561,14 +600,18 @@ def connect(self, force_dps=False):
561600
except: # connection to hub failed. hub can be down or connection string expired. fallback to dps
562601
t, v, tb = sys.exc_info()
563602
self._logger.info("ERROR: Failed to connect to Hub")
564-
if force_dps is True: # don't fallback to dps as we already using it for connecting
603+
if (
604+
force_dps is True
605+
): # don't fallback to dps as we already using it for connecting
565606
sys.exit(1)
566607
self._connection_attempts_count += 1
567608
self.connect(True)
568609

569610
# setup listeners
570611

571-
self._device_client.on_twin_desired_properties_patch_received = self._on_properties
612+
self._device_client.on_twin_desired_properties_patch_received = (
613+
self._on_properties
614+
)
572615
self._device_client.on_method_request_received = self._on_commands
573616
self._device_client.on_message_received = self._on_enqueued_commands
574617

@@ -602,8 +645,7 @@ def _compute_derived_symmetric_key(self, secret, reg_id):
602645
try:
603646
secret = base64.b64decode(secret)
604647
except:
605-
self._logger.debug(
606-
"ERROR: broken base64 secret => `" + secret + "`")
648+
self._logger.debug("ERROR: broken base64 secret => `" + secret + "`")
607649
sys.exit()
608650

609651
return base64.b64encode(

0 commit comments

Comments
 (0)