-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
91 lines (84 loc) · 3.54 KB
/
demo.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
import sys
import os.path
from iotconnect import IoTConnectSDK
import json
import time
import raspi
from datetime import datetime
def callbackMessage(msg):
if msg:
print("\n--- Command Message Received ---")
print(str(msg['ack']))
print(str(msg['ackId']))
print(str(msg['command']))
print(str(msg['uniqueId']))
def callbackTwinMessage(msg):
if msg:
print("\n--- Twin Message Received ---")
print(json.dumps(msg))
if msg.get("desired"):
if msg["desired"]["light"] == "ON":
turn_on()
if msg["desired"]["light"] == "OFF":
raspi.turn_off()
if msg.get("light"):
if msg["light"] == "ON":
raspi.turn_on()
if msg["light"] == "OFF":
raspi.turn_off()
def main(argv):
try:
env = "PROD"
if(argv[1] is not None):
env = argv[1]
uniqueId = raw_input("Enter device serial number : ").rstrip()
cpId = raw_input("Enter CPID : ").rstrip()
with IoTConnectSDK(cpId, uniqueId, callbackMessage, callbackTwinMessage, env) as sdk:
try:
raspi.setup()
input = 'y'
while input == 'y':
devices = sdk.GetAttributes()
if len(devices) > 0:
dataArray = []
for device in devices:
if device["tg"] == "":
print("\nEnter Device Id : %s" % device["id"])
else:
print("\n## TAG :: %s [Device Id :: %s]" % (device["tg"], device["id"]))
aObj = {}
for attribute in device["attr"]:
if attribute["p"] == "":
for prop in attribute["d"]:
val = raw_input("Enter " + prop["ln"] + " : ").rstrip()
if val != "":
aObj[prop["ln"]] = val
else:
print("Enter " + attribute["p"] + " :")
aObj[attribute["p"]] = {}
for prop in attribute["d"]:
val = raw_input(" Enter " + prop["ln"] + " : ").rstrip()
if val != "":
aObj[attribute["p"]][prop["ln"]] = val
if len(aObj.items()) > 0:
dObj = {
"uniqueId": device["id"],
"time": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z"),
"data" : aObj
}
dataArray.append(dObj)
if len(dataArray) > 0:
sdk.SendData(dataArray)
input = raw_input("\nWould you like to send data again ? (Y/N) : ")
input = input.lower().rstrip()
else:
input = "exit"
except KeyboardInterrupt:
raspi.destroy()
sys.exit(0)
except Exception as ex:
raspi.destroy()
print(ex.message)
sys.exit(0)
if __name__ == "__main__":
main(sys.argv)