Skip to content

Commit e81ef7a

Browse files
committed
Trying to fix tariff on wrong date. Also some code cleanup
1 parent 0b48846 commit e81ef7a

File tree

9 files changed

+79
-84
lines changed

9 files changed

+79
-84
lines changed

lib/AmsData/include/AmsData.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
#ifndef _AMSDATA_H
88
#define _AMSDATA_H
99

10-
#include "Arduino.h"
11-
#include <Timezone.h>
10+
#include <WString.h>
1211
#include "OBIScodes.h"
1312

1413
enum AmsType {
@@ -28,7 +27,7 @@ class AmsData {
2827
AmsData();
2928

3029
void apply(AmsData& other);
31-
void apply(const OBIS_code_t obis, double value);
30+
void apply(const OBIS_code_t obis, double value, uint64_t millis64);
3231

3332
uint64_t getLastUpdateMillis();
3433

lib/AmsData/src/AmsData.cpp

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "AmsData.h"
8+
#include <algorithm>
89

910
AmsData::AmsData() {}
1011

@@ -17,7 +18,6 @@ void AmsData::apply(AmsData& other) {
1718
uint32_t power = (activeImportPower + other.getActiveImportPower()) / 2;
1819
float add = power * (((float) ms) / 3600000.0);
1920
activeImportCounter += add / 1000.0;
20-
//Serial.printf("%dW, %dms, %.6fkWh added\n", other.getActiveImportPower(), ms, add);
2121
}
2222

2323
if(other.getListType() > 1) {
@@ -112,7 +112,7 @@ void AmsData::apply(AmsData& other) {
112112
this->activeExportPower = other.getActiveExportPower();
113113
}
114114

115-
void AmsData::apply(OBIS_code_t obis, double value) {
115+
void AmsData::apply(OBIS_code_t obis, double value, uint64_t millis64) {
116116
if(obis.sensor == 0 && obis.gr == 0 && obis.tariff == 0) {
117117
meterType = value;
118118
}
@@ -127,138 +127,137 @@ void AmsData::apply(OBIS_code_t obis, double value) {
127127
}
128128
}
129129
if(obis.tariff != 0) {
130-
Serial.println("Tariff not implemented");
131130
return;
132131
}
133132
if(obis.gr == 7) { // Instant values
134133
switch(obis.sensor) {
135134
case 1:
136135
activeImportPower = value;
137-
listType = max(listType, (uint8_t) 2);
136+
listType = std::max(listType, (uint8_t) 2);
138137
break;
139138
case 2:
140139
activeExportPower = value;
141-
listType = max(listType, (uint8_t) 2);
140+
listType = std::max(listType, (uint8_t) 2);
142141
break;
143142
case 3:
144143
reactiveImportPower = value;
145-
listType = max(listType, (uint8_t) 2);
144+
listType = std::max(listType, (uint8_t) 2);
146145
break;
147146
case 4:
148147
reactiveExportPower = value;
149-
listType = max(listType, (uint8_t) 2);
148+
listType = std::max(listType, (uint8_t) 2);
150149
break;
151150
case 13:
152151
powerFactor = value;
153-
listType = max(listType, (uint8_t) 4);
152+
listType = std::max(listType, (uint8_t) 4);
154153
break;
155154
case 21:
156155
l1activeImportPower = value;
157-
listType = max(listType, (uint8_t) 4);
156+
listType = std::max(listType, (uint8_t) 4);
158157
break;
159158
case 22:
160159
l1activeExportPower = value;
161-
listType = max(listType, (uint8_t) 4);
160+
listType = std::max(listType, (uint8_t) 4);
162161
break;
163162
case 31:
164163
l1current = value;
165-
listType = max(listType, (uint8_t) 2);
164+
listType = std::max(listType, (uint8_t) 2);
166165
break;
167166
case 32:
168167
l1voltage = value;
169-
listType = max(listType, (uint8_t) 2);
168+
listType = std::max(listType, (uint8_t) 2);
170169
break;
171170
case 33:
172171
l1PowerFactor = value;
173-
listType = max(listType, (uint8_t) 4);
172+
listType = std::max(listType, (uint8_t) 4);
174173
break;
175174
case 41:
176175
l2activeImportPower = value;
177-
listType = max(listType, (uint8_t) 4);
176+
listType = std::max(listType, (uint8_t) 4);
178177
break;
179178
case 42:
180179
l2activeExportPower = value;
181-
listType = max(listType, (uint8_t) 4);
180+
listType = std::max(listType, (uint8_t) 4);
182181
break;
183182
case 51:
184183
l2current = value;
185-
listType = max(listType, (uint8_t) 2);
184+
listType = std::max(listType, (uint8_t) 2);
186185
break;
187186
case 52:
188187
l2voltage = value;
189-
listType = max(listType, (uint8_t) 2);
188+
listType = std::max(listType, (uint8_t) 2);
190189
break;
191190
case 53:
192191
l2PowerFactor = value;
193-
listType = max(listType, (uint8_t) 4);
192+
listType = std::max(listType, (uint8_t) 4);
194193
break;
195194
case 61:
196195
l3activeImportPower = value;
197-
listType = max(listType, (uint8_t) 4);
196+
listType = std::max(listType, (uint8_t) 4);
198197
break;
199198
case 62:
200199
l3activeExportPower = value;
201-
listType = max(listType, (uint8_t) 4);
200+
listType = std::max(listType, (uint8_t) 4);
202201
break;
203202
case 71:
204203
l3current = value;
205-
listType = max(listType, (uint8_t) 2);
204+
listType = std::max(listType, (uint8_t) 2);
206205
break;
207206
case 72:
208207
l3voltage = value;
209-
listType = max(listType, (uint8_t) 2);
208+
listType = std::max(listType, (uint8_t) 2);
210209
break;
211210
case 73:
212211
l3PowerFactor = value;
213-
listType = max(listType, (uint8_t) 4);
212+
listType = std::max(listType, (uint8_t) 4);
214213
break;
215214
}
216215
} else if(obis.gr == 8) { // Accumulated values
217216
switch(obis.sensor) {
218217
case 1:
219218
activeImportCounter = value;
220-
listType = max(listType, (uint8_t) 3);
219+
listType = std::max(listType, (uint8_t) 3);
221220
break;
222221
case 2:
223222
activeExportCounter = value;
224-
listType = max(listType, (uint8_t) 3);
223+
listType = std::max(listType, (uint8_t) 3);
225224
break;
226225
case 3:
227226
reactiveImportCounter = value;
228-
listType = max(listType, (uint8_t) 3);
227+
listType = std::max(listType, (uint8_t) 3);
229228
break;
230229
case 4:
231230
reactiveExportCounter = value;
232-
listType = max(listType, (uint8_t) 3);
231+
listType = std::max(listType, (uint8_t) 3);
233232
break;
234233
case 21:
235234
l1activeImportCounter = value;
236-
listType = max(listType, (uint8_t) 4);
235+
listType = std::max(listType, (uint8_t) 4);
237236
break;
238237
case 22:
239238
l1activeExportCounter = value;
240-
listType = max(listType, (uint8_t) 4);
239+
listType = std::max(listType, (uint8_t) 4);
241240
break;
242241
case 41:
243242
l2activeImportCounter = value;
244-
listType = max(listType, (uint8_t) 4);
243+
listType = std::max(listType, (uint8_t) 4);
245244
break;
246245
case 42:
247246
l2activeExportCounter = value;
248-
listType = max(listType, (uint8_t) 4);
247+
listType = std::max(listType, (uint8_t) 4);
249248
break;
250249
case 61:
251250
l3activeImportCounter = value;
252-
listType = max(listType, (uint8_t) 4);
251+
listType = std::max(listType, (uint8_t) 4);
253252
break;
254253
case 62:
255254
l3activeExportCounter = value;
256-
listType = max(listType, (uint8_t) 4);
255+
listType = std::max(listType, (uint8_t) 4);
257256
break;
258257
}
259258
}
260259
if(listType > 0)
261-
lastUpdateMillis = millis();
260+
lastUpdateMillis = millis64;
262261

263262
threePhase = l1voltage > 0 && l2voltage > 0 && l3voltage > 0;
264263
if(!threePhase)

lib/EnergyAccounting/include/EnergyAccounting.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#ifndef _ENERGYACCOUNTING_H
88
#define _ENERGYACCOUNTING_H
99

10-
#include "Arduino.h"
11-
#include "AmsData.h"
1210
#include "AmsDataStorage.h"
1311
#include "PriceService.h"
1412

@@ -83,7 +81,7 @@ class EnergyAccounting {
8381
void setPriceService(PriceService *ps);
8482
void setTimezone(Timezone*);
8583
EnergyAccountingConfig* getConfig();
86-
bool update(AmsData* amsData);
84+
bool update(time_t now, uint64_t lastUpdatedMillis, uint8_t listType, uint32_t activeImportPower, uint32_t activeExportPower);
8785
bool load();
8886
bool save();
8987
bool isInitialized();

lib/EnergyAccounting/src/EnergyAccounting.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ bool EnergyAccounting::isInitialized() {
5454
return this->init;
5555
}
5656

57-
bool EnergyAccounting::update(AmsData* amsData) {
57+
bool EnergyAccounting::update(time_t now, uint64_t lastUpdatedMillis, uint8_t listType, uint32_t activeImportPower, uint32_t activeExportPower) {
5858
if(config == NULL) return false;
59-
time_t now = time(nullptr);
6059
if(now < FirmwareVersion::BuildEpoch) return false;
6160
if(tz == NULL) {
6261
return false;
@@ -90,7 +89,7 @@ bool EnergyAccounting::update(AmsData* amsData) {
9089
calcDayCost();
9190
}
9291

93-
if(local.Hour != realtimeData->currentHour && (amsData->getListType() >= 3 || local.Minute == 1)) {
92+
if(local.Hour != realtimeData->currentHour && (listType >= 3 || local.Minute == 1)) {
9493
tmElements_t oneHrAgo, oneHrAgoLocal;
9594
breakTime(now-3600, oneHrAgo);
9695
uint16_t val = round(ds->getHourImport(oneHrAgo.Hour) / 10.0);
@@ -156,9 +155,9 @@ bool EnergyAccounting::update(AmsData* amsData) {
156155
}
157156
}
158157

159-
if(realtimeData->lastImportUpdateMillis < amsData->getLastUpdateMillis()) {
160-
unsigned long ms = amsData->getLastUpdateMillis() - realtimeData->lastImportUpdateMillis;
161-
float kwhi = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0;
158+
if(realtimeData->lastImportUpdateMillis < lastUpdatedMillis) {
159+
unsigned long ms = lastUpdatedMillis - realtimeData->lastImportUpdateMillis;
160+
float kwhi = (activeImportPower * (((float) ms) / 3600000.0)) / 1000.0;
162161
if(kwhi > 0) {
163162
realtimeData->use += kwhi;
164163
float importPrice = ps == NULL ? PRICE_NO_VALUE : ps->getCurrentPrice(PRICE_DIRECTION_IMPORT);
@@ -168,12 +167,12 @@ bool EnergyAccounting::update(AmsData* amsData) {
168167
realtimeData->costDay += cost;
169168
}
170169
}
171-
realtimeData->lastImportUpdateMillis = amsData->getLastUpdateMillis();
170+
realtimeData->lastImportUpdateMillis = lastUpdatedMillis;
172171
}
173172

174-
if(amsData->getListType() > 1 && realtimeData->lastExportUpdateMillis < amsData->getLastUpdateMillis()) {
175-
unsigned long ms = amsData->getLastUpdateMillis() - realtimeData->lastExportUpdateMillis;
176-
float kwhe = (amsData->getActiveExportPower() * (((float) ms) / 3600000.0)) / 1000.0;
173+
if(listType > 1 && realtimeData->lastExportUpdateMillis < lastUpdatedMillis) {
174+
unsigned long ms = lastUpdatedMillis - realtimeData->lastExportUpdateMillis;
175+
float kwhe = (activeExportPower * (((float) ms) / 3600000.0)) / 1000.0;
177176
if(kwhe > 0) {
178177
realtimeData->produce += kwhe;
179178
float exportPrice = ps == NULL ? PRICE_NO_VALUE : ps->getCurrentPrice(PRICE_DIRECTION_EXPORT);
@@ -183,7 +182,7 @@ bool EnergyAccounting::update(AmsData* amsData) {
183182
realtimeData->incomeDay += income;
184183
}
185184
}
186-
realtimeData->lastExportUpdateMillis = amsData->getLastUpdateMillis();
185+
realtimeData->lastExportUpdateMillis = lastUpdatedMillis;
187186
}
188187

189188
if(config != NULL) {

lib/MeterCommunicators/include/IEC6205675.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "AmsConfiguration.h"
1212
#include "DataParser.h"
1313
#include "Cosem.h"
14+
#include "Timezone.h"
1415
#if defined(AMS_REMOTE_DEBUG)
1516
#include "RemoteDebug.h"
1617
#endif

lib/MeterCommunicators/src/KmpCommunicator.cpp

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ void KmpCommunicator::configure(MeterConfig& meterConfig) {
2424
}
2525

2626
bool KmpCommunicator::loop() {
27-
uint64_t now = millis64();
28-
2927
bool ret = talker->loop();
3028
int lastError = getLastError();
3129
if(ret) {
@@ -58,35 +56,36 @@ AmsData* KmpCommunicator::getData(AmsData& meterState) {
5856
if(talker == NULL) return NULL;
5957
KmpDataHolder kmpData;
6058
talker->getData(kmpData);
59+
uint64_t now = millis64();
6160
AmsData* data = new AmsData();
62-
data->apply(OBIS_ACTIVE_IMPORT_COUNT, kmpData.activeImportCounter);
63-
data->apply(OBIS_ACTIVE_EXPORT_COUNT, kmpData.activeExportCounter);
64-
data->apply(OBIS_REACTIVE_IMPORT_COUNT, kmpData.reactiveImportCounter);
65-
data->apply(OBIS_REACTIVE_EXPORT_COUNT, kmpData.reactiveExportCounter);
66-
data->apply(OBIS_ACTIVE_IMPORT, kmpData.activeImportPower);
67-
data->apply(OBIS_ACTIVE_EXPORT, kmpData.activeExportPower);
68-
data->apply(OBIS_REACTIVE_IMPORT, kmpData.reactiveImportPower);
69-
data->apply(OBIS_REACTIVE_EXPORT, kmpData.reactiveExportPower);
70-
data->apply(OBIS_VOLTAGE_L1, kmpData.l1voltage);
71-
data->apply(OBIS_VOLTAGE_L2, kmpData.l2voltage);
72-
data->apply(OBIS_VOLTAGE_L3, kmpData.l3voltage);
73-
data->apply(OBIS_CURRENT_L1, kmpData.l1current);
74-
data->apply(OBIS_CURRENT_L2, kmpData.l2current);
75-
data->apply(OBIS_CURRENT_L3, kmpData.l3current);
76-
data->apply(OBIS_POWER_FACTOR_L1, kmpData.l1PowerFactor);
77-
data->apply(OBIS_POWER_FACTOR_L2, kmpData.l2PowerFactor);
78-
data->apply(OBIS_POWER_FACTOR_L3, kmpData.l3PowerFactor);
79-
data->apply(OBIS_POWER_FACTOR, kmpData.powerFactor);
80-
data->apply(OBIS_ACTIVE_IMPORT_L1, kmpData.l1activeImportPower);
81-
data->apply(OBIS_ACTIVE_IMPORT_L2, kmpData.l2activeImportPower);
82-
data->apply(OBIS_ACTIVE_IMPORT_L3, kmpData.l3activeImportPower);
83-
data->apply(OBIS_ACTIVE_EXPORT_L1, kmpData.l1activeExportPower);
84-
data->apply(OBIS_ACTIVE_EXPORT_L2, kmpData.l2activeExportPower);
85-
data->apply(OBIS_ACTIVE_EXPORT_L3, kmpData.l3activeExportPower);
86-
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L1, kmpData.l1activeImportCounter);
87-
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L2, kmpData.l2activeImportCounter);
88-
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L3, kmpData.l3activeImportCounter);
89-
data->apply(OBIS_METER_ID, kmpData.meterId);
90-
data->apply(OBIS_NULL, AmsTypeKamstrup);
61+
data->apply(OBIS_ACTIVE_IMPORT_COUNT, kmpData.activeImportCounter, now);
62+
data->apply(OBIS_ACTIVE_EXPORT_COUNT, kmpData.activeExportCounter, now);
63+
data->apply(OBIS_REACTIVE_IMPORT_COUNT, kmpData.reactiveImportCounter, now);
64+
data->apply(OBIS_REACTIVE_EXPORT_COUNT, kmpData.reactiveExportCounter, now);
65+
data->apply(OBIS_ACTIVE_IMPORT, kmpData.activeImportPower, now);
66+
data->apply(OBIS_ACTIVE_EXPORT, kmpData.activeExportPower, now);
67+
data->apply(OBIS_REACTIVE_IMPORT, kmpData.reactiveImportPower, now);
68+
data->apply(OBIS_REACTIVE_EXPORT, kmpData.reactiveExportPower, now);
69+
data->apply(OBIS_VOLTAGE_L1, kmpData.l1voltage, now);
70+
data->apply(OBIS_VOLTAGE_L2, kmpData.l2voltage, now);
71+
data->apply(OBIS_VOLTAGE_L3, kmpData.l3voltage, now);
72+
data->apply(OBIS_CURRENT_L1, kmpData.l1current, now);
73+
data->apply(OBIS_CURRENT_L2, kmpData.l2current, now);
74+
data->apply(OBIS_CURRENT_L3, kmpData.l3current, now);
75+
data->apply(OBIS_POWER_FACTOR_L1, kmpData.l1PowerFactor, now);
76+
data->apply(OBIS_POWER_FACTOR_L2, kmpData.l2PowerFactor, now);
77+
data->apply(OBIS_POWER_FACTOR_L3, kmpData.l3PowerFactor, now);
78+
data->apply(OBIS_POWER_FACTOR, kmpData.powerFactor, now);
79+
data->apply(OBIS_ACTIVE_IMPORT_L1, kmpData.l1activeImportPower, now);
80+
data->apply(OBIS_ACTIVE_IMPORT_L2, kmpData.l2activeImportPower, now);
81+
data->apply(OBIS_ACTIVE_IMPORT_L3, kmpData.l3activeImportPower, now);
82+
data->apply(OBIS_ACTIVE_EXPORT_L1, kmpData.l1activeExportPower, now);
83+
data->apply(OBIS_ACTIVE_EXPORT_L2, kmpData.l2activeExportPower, now);
84+
data->apply(OBIS_ACTIVE_EXPORT_L3, kmpData.l3activeExportPower, now);
85+
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L1, kmpData.l1activeImportCounter, now);
86+
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L2, kmpData.l2activeImportCounter, now);
87+
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L3, kmpData.l3activeImportCounter, now);
88+
data->apply(OBIS_METER_ID, kmpData.meterId, now);
89+
data->apply(OBIS_NULL, AmsTypeKamstrup, now);
9190
return data;
9291
}

lib/RealtimePlot/include/RealtimePlot.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#ifndef _REALTIMEPLOT_H
88
#define _REALTIMEPLOT_H
99

10-
#include <stdint.h>
1110
#include "AmsData.h"
1211

1312
#define REALTIME_SAMPLE 10000

lib/RealtimePlot/src/RealtimePlot.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
*/
66

7+
#include "Arduino.h"
78
#include "RealtimePlot.h"
89
#include <stdlib.h>
910

src/AmsToMqttBridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,7 @@ void handleDataSuccess(AmsData* data) {
16071607
debugD_P(PSTR("NOT Ready to update (internal clock %02d:%02d:%02d UTC, meter clock: %02d:%02d:%02d, list type %d, est: %d)"), tm.Hour, tm.Minute, tm.Second, mtm.Hour, mtm.Minute, mtm.Second, data->getListType(), wasCounterEstimated);
16081608
}
16091609

1610-
if(ea.update(data)) {
1610+
if(ea.update(dataUpdateTime, data->getLastUpdateMillis(), data->getListType(), data->getActiveImportPower(), data->getActiveExportPower())) {
16111611
debugI_P(PSTR("Saving energy accounting"));
16121612
if(!ea.save()) {
16131613
debugW_P(PSTR("Unable to save energy accounting"));

0 commit comments

Comments
 (0)