-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_script.py
66 lines (50 loc) · 1.68 KB
/
auto_script.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
import pandas as pd
from Adafruit_IO import Client
from datetime import datetime
now = datetime.now()
time_str = now.strftime("<%d/%m/%Y %H:%M:%S>")
aio = Client('Archit149', '2619ba57dee340489754ca6dac5de74b')
#Data File
file_path = "H:\Project\Data_archive2.csv"
file_data = pd.read_csv(file_path)
#Log File
log_path = "H:\Project\Log.txt"
log_stream = open(log_path, mode = "a")
timestamp = list(file_data['timestamp'])
if timestamp != []:
t = int(timestamp[-1])
else:
t = 0 # Accounting for a File that does not have anything Written
timestamp = []
power = []
data = aio.data('energymonitor.tv-po-ts')
data = data[::-1]
d = {}
for i in data:
m = str(i)
ind = m.index("value")
m = m[ind+7:ind+27]
ts, po = m.split(',')
po = float(po)
ts = int(ts)
if t < ts:
power.append(po)
timestamp.append(ts)
d['timestamp'] = timestamp
d['power'] = power
write_data = pd.DataFrame(d)
print(write_data)
pd.DataFrame.to_csv(write_data, file_path, mode = 'a', header = False, index = False, chunksize = 10000)
file_data = pd.read_csv(file_path)
new_t = list(file_data['timestamp'])
new_t = new_t[-1]
if new_t != t:
log_stream.write(f"\n{time_str} {len(write_data)} rows to file: \"{file_path}\" latest Timestamp: {timestamp[-1]}")
# print(f"\nSucessfully Wrote {len(write_data)} rows to file upto Timestamp: {timestamp[-1]}")
elif timestamp == []:
log_stream.write(f"\n{time_str} Nothing to Write to file")
# print("\nNothing to Write to file")
else:
log_stream.write(f"\n {time_str} Failed to Write Data to file")
# print("Failed to Write Data to File")
log_stream.close()