-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcounter.py
More file actions
339 lines (292 loc) · 11.5 KB
/
Copy pathcounter.py
File metadata and controls
339 lines (292 loc) · 11.5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"""Energy counter."""
from __future__ import annotations
from datetime import datetime, timedelta
from enum import Enum, auto
import logging
from typing import Final
from ...api import EnergyStatistics
from ...constants import HOUR_IN_SECONDS, LOCAL_TIMEZONE, PULSES_PER_KW_SECOND
from ...exceptions import EnergyError
from ..helpers import EnergyCalibration
from .pulses import PulseCollection, PulseLogRecord
class EnergyType(Enum):
"""Energy collection types."""
CONSUMPTION_HOUR = auto()
PRODUCTION_HOUR = auto()
CONSUMPTION_DAY = auto()
PRODUCTION_DAY = auto()
ENERGY_COUNTERS: Final = (
EnergyType.CONSUMPTION_HOUR,
EnergyType.PRODUCTION_HOUR,
EnergyType.CONSUMPTION_DAY,
EnergyType.PRODUCTION_DAY,
)
ENERGY_HOUR_COUNTERS: Final = (
EnergyType.CONSUMPTION_HOUR,
EnergyType.PRODUCTION_HOUR,
)
ENERGY_DAY_COUNTERS: Final = (
EnergyType.CONSUMPTION_DAY,
EnergyType.PRODUCTION_DAY,
)
ENERGY_CONSUMPTION_COUNTERS: Final = (
EnergyType.CONSUMPTION_HOUR,
EnergyType.CONSUMPTION_DAY,
)
ENERGY_PRODUCTION_COUNTERS: Final = (
EnergyType.PRODUCTION_HOUR,
EnergyType.PRODUCTION_DAY,
)
_LOGGER = logging.getLogger(__name__)
class EnergyCounters:
"""Hold all energy counters."""
def __init__(self, mac: str) -> None:
"""Initialize EnergyCounter class."""
self._mac = mac
self._calibration: EnergyCalibration | None = None
self._counters: dict[EnergyType, EnergyCounter] = {}
self._current_logaddress: int | None = None
for energy_type in ENERGY_COUNTERS:
self._counters[energy_type] = EnergyCounter(energy_type, mac)
self._pulse_collection = PulseCollection(mac)
self._energy_statistics = EnergyStatistics()
@property
def collected_logs(self) -> int:
"""Total collected logs."""
return self._pulse_collection.collected_logs
def set_current_logaddres(self, address: int) -> None:
"""Update current logaddress value."""
self._current_logaddress = address
def add_empty_log(self, address: int, slot: int) -> None:
"""Add empty energy log record to mark any start of beginning of energy log collection."""
self._pulse_collection.add_empty_log(address, slot)
def add_pulse_log( # pylint: disable=too-many-arguments
self,
address: int,
slot: int,
timestamp: datetime,
pulses: int,
import_only: bool = False,
) -> None:
"""Add pulse log."""
if (
self._pulse_collection.add_log(
address, slot, timestamp, pulses, import_only
)
and not import_only
):
self.update()
def get_pulse_logs(self) -> dict[int, dict[int, PulseLogRecord]]:
"""Return currently collected pulse logs."""
return self._pulse_collection.logs
def add_pulse_stats(
self, pulses_consumed: int, pulses_produced: int, timestamp: datetime
) -> None:
"""Add pulse statistics."""
_LOGGER.debug("add_pulse_stats for %s with timestamp=%s", self._mac, timestamp)
_LOGGER.debug("consumed=%s | produced=%s", pulses_consumed, pulses_produced)
self._pulse_collection.update_pulse_counter(
pulses_consumed, pulses_produced, timestamp
)
self.update()
def reset_pulse_collection(self) -> None:
"""Reset the related pulse collection."""
self._pulse_collection.reset()
@property
def energy_statistics(self) -> EnergyStatistics:
"""Return collection with energy statistics."""
return self._energy_statistics
@property
def consumption_interval(self) -> int | None:
"""Measurement interval for energy consumption."""
return self._pulse_collection.log_interval_consumption
@property
def production_interval(self) -> int | None:
"""Measurement interval for energy production."""
return self._pulse_collection.log_interval_production
@property
def log_addresses_missing(self) -> list[int] | None:
"""Return list of addresses of energy logs."""
return self._pulse_collection.log_addresses_missing
@property
def current_logaddress(self) -> int | None:
"""Return current registered logaddress value."""
return self._current_logaddress
@property
def log_rollover(self) -> bool:
"""Indicate if new log is required due to rollover."""
return self._pulse_collection.log_rollover
@property
def calibration(self) -> EnergyCalibration | None:
"""Energy calibration configuration."""
return self._calibration
@calibration.setter
def calibration(self, calibration: EnergyCalibration) -> None:
"""Energy calibration configuration."""
for node_event in ENERGY_COUNTERS:
self._counters[node_event].calibration = calibration
self._calibration = calibration
def update(self) -> None:
"""Update counter collection."""
self._pulse_collection.recalculate_missing_log_addresses()
if self._calibration is None:
return
self._energy_statistics.current_logaddress = self._current_logaddress
self._energy_statistics.log_interval_consumption = (
self._pulse_collection.log_interval_consumption
)
(
self._energy_statistics.hour_consumption,
self._energy_statistics.hour_consumption_reset,
) = self._counters[EnergyType.CONSUMPTION_HOUR].update(self._pulse_collection)
(
self._energy_statistics.day_consumption,
self._energy_statistics.day_consumption_reset,
) = self._counters[EnergyType.CONSUMPTION_DAY].update(self._pulse_collection)
if self._pulse_collection.production_logging:
self._energy_statistics.log_interval_production = (
self._pulse_collection.log_interval_production
)
(
self._energy_statistics.hour_production,
self._energy_statistics.hour_production_reset,
) = self._counters[EnergyType.PRODUCTION_HOUR].update(
self._pulse_collection
)
(
self._energy_statistics.day_production,
self._energy_statistics.day_production_reset,
) = self._counters[EnergyType.PRODUCTION_DAY].update(self._pulse_collection)
@property
def timestamp(self) -> datetime | None:
"""Return the last valid timestamp or None."""
if self._calibration is None:
return None
if self._pulse_collection.log_addresses_missing is None:
return None
if len(self._pulse_collection.log_addresses_missing) > 0:
return None
return self._pulse_collection.last_update
class EnergyCounter:
"""Energy counter to convert pulses into energy."""
def __init__(
self,
energy_id: EnergyType,
mac: str,
) -> None:
"""Initialize energy counter based on energy id."""
self._mac = mac
self._midnight_reset_passed = False
if energy_id not in ENERGY_COUNTERS:
raise EnergyError(f"Invalid energy id '{energy_id}' for Energy counter")
self._calibration: EnergyCalibration | None = None
self._duration = "hour"
if energy_id in ENERGY_DAY_COUNTERS:
self._duration = "day"
self._energy_id: EnergyType = energy_id
self._is_consumption = True
self._direction = "consumption"
if self._energy_id in ENERGY_PRODUCTION_COUNTERS:
self._direction = "production"
self._is_consumption = False
self._last_reset: datetime | None = None
self._last_update: datetime | None = None
self._pulses: int | None = None
@property
def direction(self) -> str:
"""Energy direction (consumption or production)."""
return self._direction
@property
def duration(self) -> str:
"""Energy time span."""
return self._duration
@property
def calibration(self) -> EnergyCalibration | None:
"""Energy calibration configuration."""
return self._calibration
@calibration.setter
def calibration(self, calibration: EnergyCalibration) -> None:
"""Energy calibration configuration."""
self._calibration = calibration
@property
def is_consumption(self) -> bool:
"""Indicate the energy direction."""
return self._is_consumption
@property
def energy(self) -> float | None:
"""Total energy (in kWh) since last reset."""
if self._pulses is None or self._calibration is None:
return None
if self._pulses == 0:
return 0.0
# Handle both positive and negative pulses values
negative = False
if self._pulses < 0:
negative = True
pulses_per_s = abs(self._pulses) / float(HOUR_IN_SECONDS)
corrected_pulses = HOUR_IN_SECONDS * (
(
(
((pulses_per_s + self._calibration.off_noise) ** 2)
* self._calibration.gain_b
)
+ (
(pulses_per_s + self._calibration.off_noise)
* self._calibration.gain_a
)
)
+ self._calibration.off_tot
)
calc_value = corrected_pulses / PULSES_PER_KW_SECOND / HOUR_IN_SECONDS
if negative:
calc_value = -calc_value
return calc_value
@property
def last_reset(self) -> datetime | None:
"""Last reset of energy counter."""
return self._last_reset
@property
def last_update(self) -> datetime | None:
"""Last update of energy counter."""
return self._last_update
def update(
self, pulse_collection: PulseCollection
) -> tuple[float | None, datetime | None]:
"""Get pulse update."""
last_reset = datetime.now(tz=LOCAL_TIMEZONE)
if self._energy_id in ENERGY_HOUR_COUNTERS:
last_reset = last_reset.replace(minute=0, second=0, microsecond=0)
if self._energy_id in ENERGY_DAY_COUNTERS:
# Postpone the last_reset time-changes at day-end until a device pulsecounter resets
if last_reset.hour == 0 and (
not pulse_collection.pulse_counter_reset
and not self._midnight_reset_passed
):
last_reset = (last_reset - timedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=0
)
else:
if last_reset.hour == 0 and pulse_collection.pulse_counter_reset:
self._midnight_reset_passed = True
if last_reset.hour == 1 and self._midnight_reset_passed:
self._midnight_reset_passed = False
last_reset = last_reset.replace(
hour=0, minute=0, second=0, microsecond=0
)
pulses, last_update = pulse_collection.collected_pulses(
last_reset, self._is_consumption
)
_LOGGER.debug(
"Counter Update | %s | pulses=%s | last_update=%s",
self._mac,
pulses,
last_update,
)
if pulses is None or last_update is None:
return (None, None)
self._last_update = last_update
self._last_reset = last_reset
self._pulses = pulses
energy = self.energy
_LOGGER.debug("energy=%s on last_update=%s", energy, last_update)
return (energy, last_reset)