-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvent.py
More file actions
160 lines (122 loc) · 4.84 KB
/
Copy pathvent.py
File metadata and controls
160 lines (122 loc) · 4.84 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
155
156
157
158
159
160
#
# vent.py
# python 3
#
# Michael Wood
# 2018.4.24
#
import sys
import requests
import csv
import json
import os
payload = {'day': '2018-01-01'}
baseUri = 'https://solaflect-uplink-east.azurewebsites.net/api/v2/'
headers = {'Authorization' : 'Bearer 4615dca0-ba2e-45ff-afc9-e8e2b3cf401b'}
def represents_int(s):
try:
int(s)
return True
except ValueError:
return False
def pull(whatToPull): #'machines', 'gateways', or remaining uri for api call
uri = baseUri + whatToPull +'/'
r = requests.get(uri, headers=headers)
j = r.json()
#print(r.status_code)
#print(r.headers['content-type'])
#print(json.dumps(j, indent=4, sort_keys=True))
file = open('pull.csv', 'w')
if whatToPull == 'machines':
for row in j:
sn = row['serialNumber']
type = row['coordinatorType']
#name = row['friendlyName']
url = row['url']
file.write(sn + ',' + type + ',' + url + '\n')
elif whatToPull == 'gateways' :
for row in j:
netID = row['netid']
name = row['friendlyName']
file.write(str(netID) + ',' + str(name) + '\n')
else :
for row in j:
file.write(row['CsvLine'] + '\n')
file.close()
print('Pull ' + whatToPull + ' complete')
def analyze_all_machines(date):
machine_csv = csv.reader(open('machine_list.csv'), delimiter=',')
for row in machine_csv:
if represents_int(row[0][0]) and represents_int(row[2]) :
analyze_one_machine(row[0], date, row[1], row[2]) #pass: sn, date, type, stowPos
print(date + ' ' + row[0] + ' complete') #pass: date, sn
else :
print('ERROR: ' + date + ' ' + row[0])
def analyze_one_machine(sn, date, type, stowPosStr):
try:
gen = int(sn[0])
stowPos = int(stowPosStr)
uri = baseUri + 'machines/' + sn + '/logs'
payload['day'] = date
r = requests.get(uri, headers=headers, params=payload)
#print(r.status_code)
#print(r.headers['content-type'])
j = r.json()
file = open('log.csv', 'w')
for row in j:
file.write(row['CsvLine'] + '\n')
file.close()
log_csv = csv.reader(open('log.csv'), delimiter=',')
output = open('output.csv', 'a')
stow = True
if type == 'Erasmo' or gen != 6:
for row in log_csv:
if ((len(row) > 30) and represents_int(row[6])) :
elpos = int(row[6])
if ((elpos >= (stowPos-1000)) and (not stow)) :
stow = True
output.write(sn + ',' + row[3] + ',' + row[6] + ',' + row[21] + ',' + row[30] + '\n')
if ((elpos < (stowPos-1000)) and stow) :
stow = False
elif type == 'SecondGen' and gen == 6:
for row in log_csv:
if ((len(row) > 23) and represents_int(row[3])) :
elpos = int(row[3])
if ((elpos >= (stowPos-100)) and (not stow)) :
stow = True
output.write(sn + ',' + row[1][7:15] + ',' + row[3] + ',' + row[23] + ',' + row[12] + '\n')
if ((elpos < (stowPos-100)) and stow) :
stow = False
output.close()
except requests.HTTPError as e:
print("HTTP Error: {}".format(e))
except requests.ConnectTimeout as e:
print("The request timed out while trying to connect to the remote server: {}".format(e))
except requests.ConnectionError as e:
print("Connection Error: {}".format(e))
except requests.RequestException as e:
print("Unhandled exception: {}".format(e))
#
# Main execution
#
try:
os.remove('output.csv')
os.remove('pull.csv')
except OSError:
pass
if len(sys.argv) > 1:
if sys.argv[1] == '-pm':
pull('machines')
elif sys.argv[1] == '-pg':
pull('gateways')
elif sys.argv[1] == '-pl' :
pull(sys.argv[2])
elif sys.argv[1] == '-aam':
analyze_all_machines(str(sys.argv[2])) #pass: date
elif sys.argv[1] == '-aom':
#pass: sn, date, type, stowPos
analyze_one_machine(str(sys.argv[2]), str(sys.argv[3]), str(sys.argv[4]), str(sys.argv[5]))
else:
analyze_all_machines('2018-04-04')
analyze_all_machines('2018-04-05')
print("Hey it worked! (stop celebrating and get to work)")