forked from henry1758f/Azure-IoTHub-general-device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
420 lines (376 loc) · 16.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
################################################
# SYNNEX International Technology Corp.
# General Device Monitor with Azure IoT Hub
################################################
import psutil
import asyncio
import platform
import json
import os
import sys
#for IP Address obtain
import socket
import requests
from azure.iot.device.aio import IoTHubDeviceClient
from azure.iot.device.aio import ProvisioningDeviceClient
from azure.iot.device import constant, Message, MethodResponse
## for DPS Testing
model_id = os.getenv("IOTHUB_DEVICE_MODELID")
# For Multiple components
import pnp_helper
windows_device_info_component_name = "WindowsDeviceInfo1"
linux_device_info_component_name = "LinuxDeviceInfo1"
#================#
global period
period = 2
def end_listener():
if os.getenv("KEYPAD_INTERRUPT") == "ENABLE":
while True:
selection = input("Press Q to quit\n")
if selection == "Q" or selection == "q":
print("Quitting...")
break
else :
print('[DEBUG] Telemetry will send forever.')
import time
while True:
time.sleep(1)
sys.stdout.flush()
async def property_update(device_client,os_type,machine):
print("[DEBUG] Update System Message")
# get IP Address
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ipLocal = s.getsockname()[0]
s.close()
ipPublic = requests.get('http://ifconfig.me/ip', timeout=1).text.strip()
#
global root_path
if os_type == "Windows":
root_path = 'C:/'
hostname_str = platform.node()
cpuInfo = ' '.join(os.popen('wmic cpu get name').read().splitlines()[2].split() )
cpuCores = psutil.cpu_count()
biosManufacturer = ' '.join(os.popen('wmic bios get Manufacturer').read().splitlines()[2].split() )
biosVersion = ''.join(os.popen('wmic bios get Version').read().splitlines()[2].split() )
baseboardManufacturer = ''.join(os.popen('wmic baseboard get Manufacturer').read().splitlines()[2].split() )
baseboardSerialNumber = ''.join(os.popen('wmic baseboard get SerialNumber').read().splitlines()[2].split() )
baseboardProduct = ''.join(os.popen('wmic baseboard get Product').read().splitlines()[2].split() )
osVersion = ''.join(os.popen('wmic os get Version').read().splitlines()[2].split() )
osBuildNumber = ''.join(os.popen('wmic os get BuildNumber').read().splitlines()[2].split() )
elif os_type == "Linux":
root_path = '/'
hostname_str = platform.node()
osVersion = ' '.join(os.popen('hostnamectl |grep "Operating System"').read().split(':')[1].split() )
osBuildNumber = ' '.join(os.popen('hostnamectl |grep "Kernel"').read().split(':')[1].split() )
if "x86" in machine:
cpuInfo = ' '.join(os.popen('lscpu |grep "Model name"').read().split(':')[1].split() )
biosManufacturer = ' '.join(os.popen('cat /sys/class/dmi/id/bios_vendor').read().split() )
biosVersion = ' '.join(os.popen('cat /sys/class/dmi/id/bios_version').read().split() )
baseboardManufacturer = ' '.join(os.popen('cat /sys/class/dmi/id/board_vendor').read().split() )
baseboardSerialNumber = ' '.join(os.popen('sudo cat /sys/class/dmi/id/board_serial').read().split() )
baseboardProduct = ' '.join(os.popen('cat /sys/class/dmi/id/board_name').read().split() )
# Linux Only
highTemp = psutil.sensors_temperatures()['coretemp'][0][2]
criticalTemp = psutil.sensors_temperatures()['coretemp'][0][3]
else :
biosManufacturer = 'N/A'
biosVersion = 'N/A'
baseboardManufacturer = 'N/A'
baseboardSerialNumber = 'N/A'
baseboardProduct = 'N/A'
try:
cpuInfo = ' '.join(os.popen('lscpu |grep "Model name"').read().split(':')[1].split() )
except:
cpuInfo = machine
try:
highTemp = psutil.sensors_temperatures()['soc-thermal'][0][2]
criticalTemp = psutil.sensors_temperatures()['soc-thermal'][0][3]
except:
highTemp = 0
criticalTemp = 0
cpuCores = psutil.cpu_count()
cpuMaxfreq = psutil.cpu_freq().max
logicalDISKtotal = psutil.disk_usage(root_path).total
memTotal = psutil.virtual_memory().total
# Print Property result
print('============================')
print('Property List Upodate >>>>>>')
print("OS type : {os}".format(os=os_type))
print("OS Version : {osV}".format(osV=osVersion))
print("OS Build/Kernel : {osK}".format(osK=osBuildNumber))
print("Hostname : {host}".format(host=hostname_str))
print("CPU Info : {cpu}".format(cpu=cpuInfo))
print("CPU Core Count : {cpus}".format(cpus=cpuCores))
print("CPU Max Frequency : {cpuMF}".format(cpuMF=cpuMaxfreq))
if os_type == "Linux":
print("> CPU High Temp : {cpu_ht} Ce".format(cpu_ht=highTemp))
print("> CPU Critical : {cpu_ct} Ce".format(cpu_ct=criticalTemp))
print("BIOS Manufature : {biosM}".format(biosM=biosManufacturer))
print("BIOS Version : {biosV}".format(biosV=biosVersion))
print("Board Manufature : {boardM}".format(boardM=baseboardManufacturer))
print("Board Product : {boardP}".format(boardP=baseboardProduct))
print("Board SerialNumber : {boardSN}".format(boardSN=baseboardSerialNumber))
print("System DISK size : {diskSZ}".format(diskSZ=logicalDISKtotal))
print("Memory size : {memSZ}".format(memSZ=memTotal))
print("Local IP Address : {ip}".format(ip=ipLocal))
print("Public IP Address : {ip}".format(ip=ipPublic))
# For Multiple components
if os_type == "Windows":
properties_device_info = pnp_helper.create_reported_properties(
windows_device_info_component_name,
hostname=hostname_str,
cpuInfo=cpuInfo,
cpuCores=cpuCores,
cpuMaxfreq=cpuMaxfreq,
biosManufacturer=biosManufacturer,
biosVersion=biosVersion,
baseboardManufacturer=baseboardManufacturer,
baseboardSerialNumber=baseboardSerialNumber,
baseboardProduct=baseboardProduct,
osVersion=osVersion,
osBuildNumber=osBuildNumber,
memTotal=memTotal,
logicalDISKtotal=logicalDISKtotal,
ipLocal=ipLocal,
ipPublic=ipPublic,
)
elif os_type == "Linux":
properties_device_info = pnp_helper.create_reported_properties(
linux_device_info_component_name,
hostname=hostname_str,
cpuInfo=cpuInfo,
cpuCores=cpuCores,
cpuMaxfreq=cpuMaxfreq,
biosManufacturer=biosManufacturer,
biosVersion=biosVersion,
baseboardManufacturer=baseboardManufacturer,
baseboardSerialNumber=baseboardSerialNumber,
baseboardProduct=baseboardProduct,
osVersion=osVersion,
osBuildNumber=osBuildNumber,
memTotal=memTotal,
logicalDISKtotal=logicalDISKtotal,
ipLocal=ipLocal,
ipPublic=ipPublic,
highTemp=highTemp,
criticalTemp=criticalTemp,
)
global property_updates
property_updates = asyncio.gather(
device_client.patch_twin_reported_properties(properties_device_info),
)
async def telemetery_update(device_client,os_type,machine):
print('[DEBUG] Start sending telemetry every {sec} Second(s).'.format(sec=period))
while True:
cpuLoading = psutil.cpu_percent()
cpuClock = psutil.cpu_freq().current
mem_free = psutil.virtual_memory().free
mem_usg = psutil.virtual_memory().percent
logicalDISKfree = psutil.disk_usage(root_path).free
logicalDISKpercent = psutil.disk_usage(root_path).percent
if os_type == "Linux":
if 'x86' in machine:
currentTemp = psutil.sensors_temperatures()['coretemp'][0][1]
else:
currentTemp = psutil.sensors_temperatures()['soc-thermal'][0][1]
currentTempGPU = psutil.sensors_temperatures()['gpu-thermal'][0][1]
json_msg = {}
json_msg["cpuLoading"]=cpuLoading
json_msg["cpuClock"]=cpuClock
json_msg["memFree"]=mem_free
json_msg["memUsage"]=mem_usg
json_msg["logicalDISKfree"]=logicalDISKfree
json_msg["logicalDISKusage"]=logicalDISKpercent
if os_type == "Linux":
json_msg["currentTemp"]=currentTemp
print('[DEBUG] Sending Telemetry :{m}'.format(m=json_msg))
# For Multiple components
if os_type == "Windows":
await send_telemetry_with_component_name(device_client, json_msg, windows_device_info_component_name)
elif os_type == "Linux":
await send_telemetry_with_component_name(device_client, json_msg, linux_device_info_component_name)
if not 'x86' in machine:
json_msg_gpu = {}
msg = json_msg_gpu["currentTempGPU"]=currentTempGPU
print('[DEBUG] Sending Telemetry :{m}'.format(m=msg))
await send_telemetry_with_component_name(device_client,msg)
await asyncio.sleep(period)
async def reboot_handler(values):
if values and type(values) == int:
print("Rebooting after delay of {delay} secs".format(delay=values))
asyncio.sleep(values)
print("Done rebooting")
def create_reboot_response(values):
response = {"result": True, "data": "reboot succeeded"}
return response
async def setperiod_handler(values):
global period
if values and type(values) == int:
print("Reset telemetry sending period from {delay_old} to {delay} secs".format(delay_old=period,delay=values))
period = values
print("Finished period setting!")
def create_setperiod_response(values):
response = {"result": True, "data": "Reset telemetry sending period succeeded"}
return response
async def provision_device(provisioning_host, id_scope, registration_id, symmetric_key, model_id):
provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key(
provisioning_host=provisioning_host,
registration_id=registration_id,
id_scope=id_scope,
symmetric_key=symmetric_key,
)
provisioning_device_client.provisioning_payload = {"modelId": model_id}
return await provisioning_device_client.register()
# For Multiple components
async def send_telemetry_with_component_name(device_client, telemetry_msg, component_name=None):
msg = pnp_helper.create_telemetry(telemetry_msg, component_name)
await device_client.send_message(msg)
print("Sent message")
async def execute_command_listener(
device_client,
component_name=None,
method_name=None,
user_command_handler=None,
create_user_response_handler=None,
):
"""
Coroutine for executing listeners. These will listen for command requests.
They will take in a user provided handler and call the user provided handler
according to the command request received.
:param device_client: The device client
:param component_name: The name of the device like "sensor"
:param method_name: (optional) The specific method name to listen for. Eg could be "blink", "turnon" etc.
If not provided the listener will listen for all methods.
:param user_command_handler: (optional) The user provided handler that needs to be executed after receiving "command requests".
If not provided nothing will be executed on receiving command.
:param create_user_response_handler: (optional) The user provided handler that will create a response.
If not provided a generic response will be created.
:return:
"""
while True:
if component_name and method_name:
command_name = component_name + "*" + method_name
elif method_name:
command_name = method_name
else:
command_name = None
command_request = await device_client.receive_method_request(command_name)
print("Command request received with payload")
values = command_request.payload
print(values)
if user_command_handler:
await user_command_handler(values)
else:
print("No handler provided to execute")
(response_status, response_payload) = pnp_helper.create_response_payload_with_status(
command_request, method_name, create_user_response=create_user_response_handler
)
command_response = MethodResponse.create_from_method_request(
command_request, response_status, response_payload
)
try:
await device_client.send_method_response(command_response)
except Exception:
print("responding to the {command} command failed".format(command=method_name))
async def main():
print("SYNNEX Technology International Corp. Azure-IoT General Device")
switch = os.getenv("IOTHUB_DEVICE_SECURITY_TYPE")
switch = "DPS"
if switch == "DPS":
provisioning_host = (
os.getenv("IOTHUB_DEVICE_DPS_ENDPOINT")
if os.getenv("IOTHUB_DEVICE_DPS_ENDPOINT")
else "global.azure-devices-provisioning.net"
)
id_scope = os.getenv("IOTHUB_DEVICE_DPS_ID_SCOPE")
registration_id = os.getenv("IOTHUB_DEVICE_DPS_DEVICE_ID")
symmetric_key = os.getenv("IOTHUB_DEVICE_DPS_DEVICE_KEY")
print('[DEBUG] id_scope={id},\n > registration_id={rid}\n > symmetric_key={skey}'.format(id=id_scope,rid=registration_id,skey=symmetric_key))
registration_result = await provision_device(
provisioning_host, id_scope, registration_id, symmetric_key, model_id
)
if registration_result.status == "assigned":
print("Device was assigned")
print(registration_result.registration_state.assigned_hub)
print(registration_result.registration_state.device_id)
device_client = IoTHubDeviceClient.create_from_symmetric_key(
symmetric_key=symmetric_key,
hostname=registration_result.registration_state.assigned_hub,
device_id=registration_result.registration_state.device_id,
product_info=model_id,
)
else:
raise RuntimeError(
"Could not provision device. Aborting Plug and Play device connection."
)
elif switch == "connectionString":
conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
print("Connecting using Connection String " + conn_str)
device_client = IoTHubDeviceClient.create_from_connection_string(
conn_str, product_info=model_id
)
else:
raise RuntimeError(
"At least one choice needs to be made for complete functioning of this sample."
)
# Connect the client.
await device_client.connect()
OS_SYSTEM = platform.system()
MACHINE = platform.machine()
# Command Listener
# For Multiple components
if OS_SYSTEM == "Windows":
listeners = asyncio.gather(
execute_command_listener(
device_client,
windows_device_info_component_name,
method_name="reboot",
user_command_handler=reboot_handler,
create_user_response_handler=create_reboot_response,
),
execute_command_listener(
device_client,
windows_device_info_component_name,
method_name="setperiod",
user_command_handler=setperiod_handler,
create_user_response_handler=create_setperiod_response,
),
)
elif OS_SYSTEM == "Linux":
listeners = asyncio.gather(
execute_command_listener(
device_client,
linux_device_info_component_name,
method_name="reboot",
user_command_handler=reboot_handler,
create_user_response_handler=create_reboot_response,
),
execute_command_listener(
device_client,
linux_device_info_component_name,
method_name="setperiod",
user_command_handler=setperiod_handler,
create_user_response_handler=create_setperiod_response,
),
)
await property_update(device_client,OS_SYSTEM,MACHINE)
telemetery_update_task = asyncio.create_task(telemetery_update(device_client,OS_SYSTEM,MACHINE))
loop = asyncio.get_running_loop()
end = loop.run_in_executor(None, end_listener)
await end
if not listeners.done():
listeners.set_result("DONE")
# For Multiple components
if not property_updates.done():
property_updates.set_result("DONE")
listeners.cancel()
# For Multiple components
property_updates.cancel()
telemetery_update_task.cancel()
# finally, disconnect
await device_client.disconnect()
#================================#
if __name__ == "__main__":
asyncio.run(main())