This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsiotc_test.py
executable file
·74 lines (60 loc) · 2.45 KB
/
msiotc_test.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
#!/usr/bin/env python3
import io, iotc, json, logging, time
from iotc import IOTConnectType, IOTLogLevel
from datetime import datetime
from random import randint
import config as cfg
mslog = logging.getLogger('msiotc_test')
msfh = logging.FileHandler('msiotc_test.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
msfh.setFormatter(formatter)
mslog.addHandler(msfh)
mslog.setLevel(logging.DEBUG)
iotc = iotc.Device(cfg.scopeId, cfg.deviceKey, cfg.deviceId, IOTConnectType.IOTC_CONNECT_SYMM_KEY)
iotc.setLogLevel(IOTLogLevel.IOTC_LOGGING_API_ONLY)
mslog.info('Profile: ' + str(cfg.profile))
gCanSend = False
gCounter = 0
def onconnect(info):
global gCanSend
#print("- [onconnect] => status:" + str(info.getStatusCode()))
mslog.info('[onconnect] => status: ' + str(info.getStatusCode()))
if info.getStatusCode() == 0:
if iotc.isConnected():
mslog.info('Connected')
gCanSend = True
else:
mslog.info('Not connected?')
else:
print('Disconnected due to status code (' + str(info.getStatusCode()) + ')')
mslog.error('Disconnected due to status code (' + str(info.getStatusCode()) + ')')
#iotc.disconnect()
#print("Waiting to reconnect...")
#time.sleep(30)
#print("Trying to reconnect...")
#iotc.connect()
def onmessagesent(info):
#print("\t- [onmessagesent] => " + str(info.getPayload()))
mslog.info('[onmessagesent] => ' + str(info.getPayload()))
def oncommand(info):
#print("- [oncommand] => " + info.getTag() + " => " + str(info.getPayload()))
mslog.info('[oncommand] => ' + info.getTag() + ' => ' + + str(info.getPayload()))
def onsettingsupdated(info):
#print("- [onsettingsupdated] => " + info.getTag() + " => " + info.getPayload())
mslog.info('[onsettingsupdated] => ' + info.getTag() + ' => ' + info.getPayload())
iotc.on("ConnectionStatus", onconnect)
iotc.on("MessageSent", onmessagesent)
iotc.on("Command", oncommand)
iotc.on("SettingsUpdated", onsettingsupdated)
iotc.connect()
while iotc.isConnected():
iotc.doNext()
if gCanSend == True:
if gCounter % 20 == 0:
gCounter = 0
cur = time.time()
now = datetime.utcfromtimestamp(cur).strftime('%Y-%m-%d %H:%M:%S')
jsonStr = '{"temperature": ' + str(randint(20, 45)) + '}'
print("[" + now + "] Sending telemetry...", gCounter)
iotc.sendTelemetry(jsonStr)
gCounter += 1