-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicesTypes.py
More file actions
154 lines (126 loc) · 4.77 KB
/
devicesTypes.py
File metadata and controls
154 lines (126 loc) · 4.77 KB
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
import logging
import time
from datetime import datetime
from swagger_client.rest import ApiException
import schedule
from dataProcessing import getAPIAccessToken, readXlsx, createDB
# ------------------------------------------- Functions ------------------------------------------------------
# Function to call the API to get the devices types
def apiGetDevices(client, logger):
devicesInfo = []
api_instance = getAPIAccessToken(logger)
print("Calling Devices Types")
logger.info("Calling Devices Types")
for key in xlsxData.keys():
time.sleep(1)
api_instance = getAPIAccessToken(logger)
devicesInfo.append(key)
name = xlsxData.get(key).get("name")
devicesInfo.append(name)
devicesInfo.append(xlsxData.get(key).get("building"))
devicesResponse = api_instance.access_point_name_device_type_count_get(name)
dic = formatResponse(devicesResponse)
if dic != None:
devicesInfo.append(dic.get("android"))
devicesInfo.append(dic.get("ios"))
devicesInfo.append(dic.get("laptop"))
writeDevicesOnDB(client, devicesInfo)
devicesInfo.clear()
# Function to format the response getted from te API
def formatResponse(response):
android = 0
ios = 0
laptop = 0
if response.device_type_counts != None:
for device in response.device_type_counts:
# Getting the number of laptops
if "Unclassified" in device.device_type:
laptop += device.count
if "Intel-Device" in device.device_type:
laptop += device.count
if "Microsoft-Device" in device.device_type:
laptop += device.count
if "Unknown" in device.device_type:
laptop += device.count
# Getting the number of android devices
if "Samsung" in device.device_type:
android += device.count
if "Huawei" in device.device_type:
android += device.count
if "ZTE" in device.device_type:
android += device.count
if "ASUS" in device.device_type:
android += device.count
if "MotorolaMobile" in device.device_type:
android += device.count
if "LG" in device.device_type:
android += device.count
if "Sony" in device.device_type:
android += device.count
# Getting the number of ios devices
if "Apple" in device.device_type:
ios += device.count
if "iPhone" in device.device_type:
ios += device.count
if "iPad" in device.device_type:
ios += device.count
# print(type(laptop))
# print(type(ios))
# print(type(android))
return {
"laptop": int(laptop),
"ios": int(ios),
"android": int(android)
}
return None
# Function to write the devices types on the database
def writeDevicesOnDB(client, info):
# Data to send to the database
json_payload = []
#
# Note: tags -> metadata about the measurement
# fields -> a measurement that changes over time
#
data = {
"measurement" : "devicesTypes",
"time" : datetime.now(),
"tags" : {
"id" : info[0],
"ap_name" : info[1],
"building" : info[2]
},
"fields" : {
"android" : info[3],
"ios" : info[4],
"laptop" : info[5]
}
}
# Send data to the API
json_payload.append(data)
client.write_points(json_payload)
# ------------------------------------------ Main Function ---------------------------------------------------
if __name__ == "__main__":
# Creating the log file for the service
logging.basicConfig(filename='/var/log/devicesTypes.log', level=logging.INFO)
logger = logging.getLogger("DEVICES TYPES")
# To read the data given from the xlsx file to a var
xlsxData = readXlsx('.', 'PrimeCore.xlsx')
# Creating the database
client = createDB()
# Getting the initial information about the metrics
try:
apiGetDevices(client, logger)
except Exception as e:
print("Devices types Exception: " +str(e))
logger.error("Devices types Exception: " +str(e))
# Calling the API to get the metrics info every 8 minutes
try:
schedule.every(30).minutes.do(apiGetDevices, client, logger)
except Exception as e:
print("Devices types Exception: " +str(e))
logger.error("Devices types Exception: " +str(e))
while True:
try:
schedule.run_pending()
except ApiException as e:
logger.error("Exception: %s\n" % e)