-
Notifications
You must be signed in to change notification settings - Fork 1
/
VoltcraftInformationFile.py
105 lines (90 loc) · 6.07 KB
/
VoltcraftInformationFile.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
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
#!python3
"""
Project: Voltcraft Data Analyzer
Author: Valer Bocan, PhD <[email protected]>
Last updated: June 26th, 2014
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
"""
import struct
from datetime import datetime
from ByteDecoders import DecodeHex
from ByteDecoders import DecodeDecimal
def Process(filename):
"""
Description: Process a Voltcraft information file. Such files contain the information displayed by the Voltcraft device on its screen, when operated manually.
Input: File name.
Output: Dictionary containing values for:
"TotalPowerConsumed" - kWh
"TotalRecordedTime" - decimal minutes
"TotalOnTime" - decimal minutes
"ConsumptionHistory" - consumption history - tuple containing 10 values (1st meaning today, last meaning nine days ago)
"RecordedTimeHistory" - recorded time history - tuple containing 10 values (1st meaning today, last meaning nine days ago)
"OnTimeHistory" - total time history - tuple containing 10 values (1st meaning today, last meaning nine days ago)
"UnitNumber" - device profile number (Voltcraft Energy-Logger 4000 has several profiles with separate statistics)
"Tariff1"
"Tariff2"
"InitialDateTime" - date and time when when the device was initially set
"""
try:
with open(filename, "rb") as fin:
x = struct.unpack('5s3B3B3B3B3B3B3B3B3B3B3B3B3B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B1B4B4B1B1B1B1B1B4B', fin.read(102))
except IOError:
raise Exception('The specified file does not exist.')
except struct.error:
raise Exception('The specified file must be at least 102 bytes long.')
VoltcraftInfo = {}
# Check file validity
StartMagicString = x[0] # Should be INFO:
EndMagicString = DecodeHex(x[94:98]) # Should be #FFFFFFFF
if StartMagicString != b"INFO:" or EndMagicString != 4294967295:
return VoltcraftInfo
VoltcraftInfo["TotalPowerConsumed"] = DecodeHex(x[1:4]) / 1000 # kWh
VoltcraftInfo["TotalRecordedTime"] = DecodeHex(x[4:7]) / 100 # Minutes in decimal format
VoltcraftInfo["TotalOnTime"] = DecodeHex(x[7:10]) / 100 # Minutes in decimal format
ConsumptionToday = DecodeHex(x[10:13]) / 1000 # kWh
ConsumptionYesterday = DecodeHex(x[13:16]) / 1000 # kWh
Consumption2DaysAgo = DecodeHex(x[16:19]) / 1000 # kWh
Consumption3DaysAgo = DecodeHex(x[19:22]) / 1000 # kWh
Consumption4DaysAgo = DecodeHex(x[22:25]) / 1000 # kWh
Consumption5DaysAgo = DecodeHex(x[25:28]) / 1000 # kWh
Consumption6DaysAgo = DecodeHex(x[28:31]) / 1000 # kWh
Consumption7DaysAgo = DecodeHex(x[31:34]) / 1000 # kWh
Consumption8DaysAgo = DecodeHex(x[34:37]) / 1000 # kWh
Consumption9DaysAgo = DecodeHex(x[37:40]) / 1000 # kWh
VoltcraftInfo["ConsumptionHistory"] = (ConsumptionToday, ConsumptionYesterday, Consumption2DaysAgo, Consumption3DaysAgo, Consumption4DaysAgo, Consumption5DaysAgo, Consumption6DaysAgo, Consumption7DaysAgo, Consumption8DaysAgo, Consumption9DaysAgo)
RecordedTimeToday = DecodeHex(x[40:42]) / 100 # Hours in decimal format
RecordedTimeYesterday = DecodeHex(x[42:44]) / 100 # Hours in decimal format
RecordedTime2DaysAgo = DecodeHex(x[44:46]) / 100 # Hours in decimal format
RecordedTime3DaysAgo = DecodeHex(x[46:48]) / 100 # Hours in decimal format
RecordedTime4DaysAgo = DecodeHex(x[48:50]) / 100 # Hours in decimal format
RecordedTime5DaysAgo = DecodeHex(x[50:52]) / 100 # Hours in decimal format
RecordedTime6DaysAgo = DecodeHex(x[52:54]) / 100 # Hours in decimal format
RecordedTime7DaysAgo = DecodeHex(x[54:56]) / 100 # Hours in decimal format
RecordedTime8DaysAgo = DecodeHex(x[56:58]) / 100 # Hours in decimal format
RecordedTime9DaysAgo = DecodeHex(x[58:60]) / 100 # Hours in decimal format
VoltcraftInfo["RecordedTimeHistory"] = (RecordedTimeToday, RecordedTimeYesterday, RecordedTime2DaysAgo, RecordedTime3DaysAgo, RecordedTime4DaysAgo, RecordedTime5DaysAgo, RecordedTime6DaysAgo, RecordedTime7DaysAgo, RecordedTime8DaysAgo, RecordedTime9DaysAgo)
TotalTimeToday = DecodeHex(x[60:62]) / 100 # Hours in decimal format
TotalTimeYesterday = DecodeHex(x[62:64]) / 100 # Hours in decimal format
TotalTime2DaysAgo = DecodeHex(x[64:66]) / 100 # Hours in decimal format
TotalTime3DaysAgo = DecodeHex(x[66:68]) / 100 # Hours in decimal format
TotalTime4DaysAgo = DecodeHex(x[68:70]) / 100 # Hours in decimal format
TotalTime5DaysAgo = DecodeHex(x[70:72]) / 100 # Hours in decimal format
TotalTime6DaysAgo = DecodeHex(x[72:74]) / 100 # Hours in decimal format
TotalTime7DaysAgo = DecodeHex(x[74:76]) / 100 # Hours in decimal format
TotalTime8DaysAgo = DecodeHex(x[76:78]) / 100 # Hours in decimal format
TotalTime9DaysAgo = DecodeHex(x[78:80]) / 100 # Hours in decimal format
VoltcraftInfo["OnTimeHistory"] = (TotalTimeToday, TotalTimeYesterday, TotalTime2DaysAgo, TotalTime3DaysAgo, TotalTime4DaysAgo, TotalTime5DaysAgo, TotalTime6DaysAgo, TotalTime7DaysAgo, TotalTime8DaysAgo, TotalTime9DaysAgo)
VoltcraftInfo["UnitNumber"] = DecodeHex(x[80:81])
VoltcraftInfo["Tariff1"] = DecodeDecimal(x[81:85]) / 1000
VoltcraftInfo["Tariff2"] = DecodeDecimal(x[85:89]) / 1000
Hour = DecodeHex(x[89:90])
Minute = DecodeHex(x[90:91])
Month = DecodeHex(x[91:92])
Day = DecodeHex(x[92:93])
Year = DecodeHex(x[93:94]) + 2000
VoltcraftInfo["InitialDateTime"] = datetime(Year, Month, Day, Hour, Minute)
return VoltcraftInfo