forked from vbocan/Voltcraft-Data-Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoltcraft.py
69 lines (59 loc) · 2.93 KB
/
Voltcraft.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
#!python3
"""
Project: Voltcraft Data Analyzer
Author: Valer Bocan, PhD <[email protected]>
Last updated: April 11th, 2020 (while locked at home, during the COVID-19 crisis)
Module
description: The VoltcraftDataFile module processes data files containing history of voltage, current and power factor,
as generated by the Voltcraft Energy-Logger 4000.
License: This project is placed in the public domain, hoping that it will be useful to people tinkering with Voltcraft products.
Reference: Voltcraft File Format: http://www2.produktinfo.conrad.com/datenblaetter/125000-149999/125323-da-01-en-Datenprotokoll_SD_card_file_Formatv1_2.pdf
"""
from sys import argv
from os import listdir, getcwd
from os.path import isfile, join, getsize
from datetime import timedelta
from datetime import datetime
from DataExport import WriteInfoData, WriteHistoricData, WriteBlackoutData
import VoltcraftInformationFile
import VoltcraftDataFile
PowerData = []
ErrorOccured = False
if __name__ == "__main__":
print("Voltcraft Data Analyzer v1.21")
print("Valer Bocan, PhD <[email protected]>")
TargetFolder = getcwd()
if len(argv) > 1:
TargetFolder = argv[1]
print("Processing Voltcraft files from folder {0}:".format(TargetFolder))
# Get all files in the target folder
TargetFiles = [ join(TargetFolder, f) for f in listdir(TargetFolder) if f.upper().endswith("BIN")]
for filename in TargetFiles:
try:
print("Processing file {0}.".format(filename))
# Check file size (if it's 102, we have an information file, else it's a data file)
isInfoFile = (getsize(filename) == 102);
if isInfoFile:
PowerInfo = VoltcraftInformationFile.Process(filename)
else:
data = list(VoltcraftDataFile.Process(filename))
PowerData += data
except Exception as e:
print("Error processing current file: {0}.".format(e))
ErrorOccured = True
break
if not ErrorOccured:
# Save general power information and parameter history
print("Saving general power information to info.txt.")
SortedPowerData = sorted(PowerData, key=lambda x: x["Timestamp"], reverse=False)
BlackoutData = list(VoltcraftDataFile.DetectBlackouts(SortedPowerData))
WriteInfoData("info.txt", PowerInfo, SortedPowerData, BlackoutData)
#Save raw power history
print("Saving raw power history ({0} items) to power_history.csv.".format(len(SortedPowerData)))
WriteHistoricData("power_history.csv", SortedPowerData)
#Save raw blackout data
print("Saving raw blackout history ({0} items) to blackout_history.csv.".format(len(BlackoutData)))
WriteBlackoutData("blackout_history.csv", BlackoutData)
print("Done.")
#Wait for key press
input('Press Enter to exit')