-
Notifications
You must be signed in to change notification settings - Fork 3
/
telemetry.py
75 lines (70 loc) · 3.2 KB
/
telemetry.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
# coding: utf-8
from applicationinsights import TelemetryClient
import sys,hashlib,os.path,re, uuid
import platform
IKEY = "0823bae8-a3b8-4fd5-80e5-f7272a2377a9"
LANGUAGE = "Python"
DEVICE = "RaspberryPi"
PROMPT_TEXT = "\nMicrosoft would like to collect data about how users use Azure IoT " \
"samples and some problems they encounter. Microsoft uses this information to improve "\
"our tooling experience. Participation is voluntary and when you choose to participate " \
"your device automatically sends information to Microsoft about how you use Azure IoT "\
"samples. If you want to change this setting after first time, please delete the "\
"telemetry.config file and restart the program. "\
"\n\nSelect y to enable data collection (y/n, default is y). "
class Telemetry:
def __init__(self):
try:
self.telemetry = TelemetryClient(IKEY)
if os.path.exists("telemetry.config"):
config_file = open("telemetry.config", "r")
if config_file.read() == "1":
self.enable_telemetry = True
else:
self.enable_telemetry = False
else:
self.enable_telemetry = self._query_yes_no(PROMPT_TEXT)
config_file = open("telemetry.config", "w")
if self.enable_telemetry:
config_file.write("1")
self.telemetry.track_event("yes", {"device": DEVICE, "language": LANGUAGE})
else:
config_file.write("0")
self.telemetry.context.location.ip = "0.0.0.0"
self.telemetry.track_event("no", {"device": DEVICE, "language": LANGUAGE})
self.telemetry.flush()
except:
pass
def send_telemetry_data(self, iot_hub_name, event, message):
try:
if self.enable_telemetry:
hash_mac = self._get_mac_hash()
hash_iot_hub_name = hashlib.sha256(iot_hub_name.encode("utf-8")).hexdigest()
self.telemetry.track_event(event, {"iothub": hash_iot_hub_name, "message": message,
"language": LANGUAGE, "device": DEVICE, "mac": hash_mac,
"osType": platform.system(), "osPlatform": platform.dist()[0],
"osRelease": platform.dist()[1]})
self.telemetry.flush()
except:
pass
def _get_mac_hash(self):
mac = ":".join(re.findall("..", "%012x" % uuid.getnode()))
return hashlib.sha256(mac.encode("utf-8")).hexdigest()
def _query_yes_no(self, question):
global input
default = "y"
valid = {"y": True, "n": False}
prompt = " [Y/n] "
while True:
sys.stdout.write(question + prompt)
try:
input = raw_input
except NameError:
pass
choice = input().lower()
if default is not None and choice == "":
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'y' or 'n' ")