Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
azure-iot-edge-devices
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sireesha.k
azure-iot-edge-devices
Commits
dab06d97
Commit
dab06d97
authored
May 28, 2021
by
sireesha.k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
iot edge devices creation, deletion and listing
parent
c1922a3a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
85 additions
and
0 deletions
+85
-0
main.py
main.py
+12
-0
scripts/__init__.py
scripts/__init__.py
+0
-0
scripts/handler/__init__.py
scripts/handler/__init__.py
+0
-0
scripts/handler/iotdevices.py
scripts/handler/iotdevices.py
+73
-0
No files found.
main.py
0 → 100644
View file @
dab06d97
from
scripts.handler.iotdevices
import
DeviceManager
if
__name__
==
'__main__'
:
connectionString
=
'HostName=sireesha-iothub-1.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=FUirg79qdXCCkLJnaDz008PAszEPd3Ks9a6xelokEtM='
dm
=
DeviceManager
(
connectionString
)
deviceId
=
'edgedevice1'
# print(dm.createDeviceId(deviceId))
# print(dm.retrieveDeviceId(deviceId))
# # print(dm.deleteDeviceId(deviceId))
# print(dm.listDeviceIds())
message
=
'Hello, IoT Hub'
print
(
dm
.
sendD2CMsg
(
deviceId
,
message
))
scripts/__init__.py
0 → 100644
View file @
dab06d97
scripts/handler/__init__.py
0 → 100644
View file @
dab06d97
scripts/handler/iotdevices.py
0 → 100644
View file @
dab06d97
import
base64
import
hmac
import
hashlib
import
time
import
requests
import
urllib
import
urllib.parse
class
DeviceManager
:
API_VERSION
=
'2018-06-30'
TOKEN_VALID_SECS
=
365
*
24
*
60
*
60
TOKEN_FORMAT
=
'SharedAccessSignature sr=
%
s&sig=
%
s&se=
%
s&skn=
%
s'
def
__init__
(
self
,
connectionString
=
None
):
if
connectionString
!=
None
:
iotHost
,
keyName
,
keyValue
=
[
sub
[
sub
.
index
(
'='
)
+
1
:]
for
sub
in
connectionString
.
split
(
";"
)]
self
.
iotHost
=
iotHost
self
.
keyName
=
keyName
self
.
keyValue
=
keyValue
def
_buildExpiryOn
(
self
):
return
'
%
d'
%
(
time
.
time
()
+
self
.
TOKEN_VALID_SECS
)
def
_buildSasToken
(
self
):
targetUri
=
self
.
iotHost
.
lower
()
expiryTime
=
self
.
_buildExpiryOn
()
toSign
=
'
%
s
\n
%
s'
%
(
targetUri
,
expiryTime
)
key
=
base64
.
b64decode
(
self
.
keyValue
.
encode
(
'utf-8'
))
signature
=
urllib
.
parse
.
quote
(
base64
.
b64encode
(
hmac
.
HMAC
(
key
,
toSign
.
encode
(
'utf-8'
),
hashlib
.
sha256
)
.
digest
()
)
)
.
replace
(
'/'
,
'
%2
F'
)
return
self
.
TOKEN_FORMAT
%
(
targetUri
,
signature
,
expiryTime
,
self
.
keyName
)
def
createDeviceId
(
self
,
deviceId
):
sasToken
=
self
.
_buildSasToken
()
print
(
sasToken
)
url
=
'https://
%
s/devices/
%
s?api-version=
%
s'
%
(
self
.
iotHost
,
deviceId
,
self
.
API_VERSION
)
print
(
url
)
body
=
"{
\r\n\t\"
deviceId
\"
:
\"
%
s
\"
,
\r\n\t\"
capabilities
\"
: {
\r\n\t\t\"
iotEdge
\"
: true
\r\n\t
}
\r\n
}"
%
(
deviceId
)
r
=
requests
.
put
(
url
,
headers
=
{
'Content-Type'
:
'application/json'
,
'Authorization'
:
sasToken
},
data
=
body
)
return
r
.
text
,
r
.
status_code
def
deleteDeviceId
(
self
,
deviceId
):
sasToken
=
self
.
_buildSasToken
()
url
=
'https://
%
s/devices/
%
s?api-version=
%
s'
%
(
self
.
iotHost
,
deviceId
,
self
.
API_VERSION
)
r
=
requests
.
delete
(
url
,
headers
=
{
'Content-Type'
:
'application/json'
,
'Authorization'
:
sasToken
,
'If-Match'
:
'*'
})
# If-Match Etag, but if * is used, no need to precise the Etag of the device. The Etag of the device can be seen in the header requests.text response
return
r
.
text
,
r
.
status_code
def
listDeviceIds
(
self
,
top
=
None
):
if
top
==
None
:
top
=
1000
sasToken
=
self
.
_buildSasToken
()
url
=
'https://
%
s/devices?top=
%
d&api-version=
%
s'
%
(
self
.
iotHost
,
top
,
self
.
API_VERSION
)
r
=
requests
.
get
(
url
,
headers
=
{
'Content-Type'
:
'application/json'
,
'Authorization'
:
sasToken
})
return
r
.
text
,
r
.
status_code
def
retrieveDeviceId
(
self
,
deviceId
):
sasToken
=
self
.
_buildSasToken
()
url
=
'https://
%
s/devices/
%
s?api-version=
%
s'
%
(
self
.
iotHost
,
deviceId
,
self
.
API_VERSION
)
r
=
requests
.
get
(
url
,
headers
=
{
'Content-Type'
:
'application/json'
,
'Authorization'
:
sasToken
})
return
r
.
text
,
r
.
status_code
def
sendD2CMsg
(
self
,
deviceId
,
message
):
sasToken
=
self
.
_buildSasToken
()
url
=
'https://
%
s/devices/
%
s/messages/events?api-version=
%
s'
%
(
self
.
iotHost
,
deviceId
,
self
.
API_VERSION
)
r
=
requests
.
post
(
url
,
headers
=
{
'Authorization'
:
sasToken
},
data
=
message
)
return
r
.
text
,
r
.
status_code
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment