Skip to content

Commit 94eb0eb

Browse files
authored
update / fix MeterValues format (#301)
* fix whitespaces in MeterValues * change EnergyMeterInput data type to int
1 parent 8eb9e2e commit 94eb0eb

File tree

8 files changed

+23
-27
lines changed

8 files changed

+23
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Rename master branch into main
99
- Tx logic directly checks if WebSocket is offline ([#282](https://github.com/matth-x/MicroOcpp/pull/282))
1010
- `ocppPermitsCharge` ignores Faulted state ([#279](https://github.com/matth-x/MicroOcpp/pull/279))
11+
- `setEnergyMeterInput` expects `int` input ([#301](https://github.com/matth-x/MicroOcpp/pull/301))
1112

1213
### Added
1314

@@ -40,6 +41,7 @@
4041
- Fix compilation error caused by `PRId32` ([#279](https://github.com/matth-x/MicroOcpp/pull/279))
4142
- Don't load FW-mngt. module when no handlers set ([#271](https://github.com/matth-x/MicroOcpp/pull/271))
4243
- Avoid creating conf when operation fails ([#290](https://github.com/matth-x/MicroOcpp/pull/290))
44+
- Fix whitespaces in MeterValues ([#301](https://github.com/matth-x/MicroOcpp/pull/301))
4345

4446
## [1.0.3] - 2024-04-06
4547

src/MicroOcpp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ void setConnectorPluggedInput(std::function<bool()> pluggedInput, unsigned int c
563563
connector->setConnectorPluggedInput(pluggedInput);
564564
}
565565

566-
void setEnergyMeterInput(std::function<float()> energyInput, unsigned int connectorId) {
566+
void setEnergyMeterInput(std::function<int()> energyInput, unsigned int connectorId) {
567567
if (!context) {
568568
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
569569
return;
@@ -576,8 +576,8 @@ void setEnergyMeterInput(std::function<float()> energyInput, unsigned int connec
576576
SampledValueProperties meterProperties;
577577
meterProperties.setMeasurand("Energy.Active.Import.Register");
578578
meterProperties.setUnit("Wh");
579-
auto mvs = std::unique_ptr<SampledValueSamplerConcrete<float, SampledValueDeSerializer<float>>>(
580-
new SampledValueSamplerConcrete<float, SampledValueDeSerializer<float>>(
579+
auto mvs = std::unique_ptr<SampledValueSamplerConcrete<int, SampledValueDeSerializer<int>>>(
580+
new SampledValueSamplerConcrete<int, SampledValueDeSerializer<int>>(
581581
meterProperties,
582582
[energyInput] (ReadingContext) {return energyInput();}
583583
));

src/MicroOcpp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ bool ocppPermitsCharge(unsigned int connectorId = 1);
266266

267267
void setConnectorPluggedInput(std::function<bool()> pluggedInput, unsigned int connectorId = 1); //Input about if an EV is plugged to this EVSE
268268

269-
void setEnergyMeterInput(std::function<float()> energyInput, unsigned int connectorId = 1); //Input of the electricity meter register
269+
void setEnergyMeterInput(std::function<int()> energyInput, unsigned int connectorId = 1); //Input of the electricity meter register in Wh
270270

271-
void setPowerMeterInput(std::function<float()> powerInput, unsigned int connectorId = 1); //Input of the power meter reading
271+
void setPowerMeterInput(std::function<float()> powerInput, unsigned int connectorId = 1); //Input of the power meter reading in W
272272

273273
//Smart Charging Output, alternative for Watts only, Current only, or Watts x Current x numberPhases. Only one
274274
//of them can be set at a time

src/MicroOcpp/Model/Metering/SampledValue.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// matth-x/MicroOcpp
2-
// Copyright Matthias Akstaller 2019 - 2023
2+
// Copyright Matthias Akstaller 2019 - 2024
33
// MIT License
44

55
#include <MicroOcpp/Model/Metering/SampledValue.h>
66
#include <MicroOcpp/Debug.h>
77
#include <cinttypes>
88

9+
#ifndef MO_SAMPLEDVALUE_FLOAT_FORMAT
10+
#define MO_SAMPLEDVALUE_FLOAT_FORMAT "%.2f"
11+
#endif
12+
913
using namespace MicroOcpp;
1014

1115
int32_t SampledValueDeSerializer<int32_t>::deserialize(const char *str) {
@@ -18,6 +22,13 @@ std::string SampledValueDeSerializer<int32_t>::serialize(int32_t& val) {
1822
return std::string(str);
1923
}
2024

25+
std::string SampledValueDeSerializer<float>::serialize(float& val) {
26+
char str [20];
27+
str[0] = '\0';
28+
snprintf(str, 20, MO_SAMPLEDVALUE_FLOAT_FORMAT, val);
29+
return std::string(str);
30+
}
31+
2132
//helper function
2233
namespace MicroOcpp {
2334
namespace Ocpp16 {

src/MicroOcpp/Model/Metering/SampledValue.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// matth-x/MicroOcpp
2-
// Copyright Matthias Akstaller 2019 - 2023
2+
// Copyright Matthias Akstaller 2019 - 2024
33
// MIT License
44

55
#ifndef SAMPLEDVALUE_H
@@ -36,11 +36,7 @@ class SampledValueDeSerializer<float> { // Used in meterValues
3636
public:
3737
static float deserialize(const char *str) {return atof(str);}
3838
static bool ready(float& val) {return true;} //float is always valid
39-
static std::string serialize(float& val) {
40-
char str[20];
41-
dtostrf(val,4,1,str);
42-
return std::string(str);
43-
}
39+
static std::string serialize(float& val);
4440
static int32_t toInteger(float& val) {return (int32_t) val;}
4541
};
4642

src/MicroOcpp/Platform.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// matth-x/MicroOcpp
2-
// Copyright Matthias Akstaller 2019 - 2023
2+
// Copyright Matthias Akstaller 2019 - 2024
33
// MIT License
44

55
#include <MicroOcpp/Platform.h>
@@ -80,11 +80,3 @@ unsigned long mocpp_tick_ms_unix() {
8080
}
8181
#endif
8282
#endif
83-
84-
#if MO_PLATFORM != MO_PLATFORM_ARDUINO
85-
void dtostrf(float value, int min_width, int num_digits_after_decimal, char *target){
86-
char fmt[20];
87-
sprintf(fmt, "%%%d.%df", min_width, num_digits_after_decimal);
88-
sprintf(target, fmt, value);
89-
}
90-
#endif

src/MicroOcpp/Platform.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ unsigned long mocpp_tick_ms_unix();
102102
#endif
103103
#endif
104104

105-
#if MO_PLATFORM != MO_PLATFORM_ARDUINO
106-
void dtostrf(float value, int min_width, int num_digits_after_decimal, char *target);
107-
#endif
108-
109105
#ifndef MO_ENABLE_MBEDTLS
110106
#define MO_ENABLE_MBEDTLS 0
111107
#endif

tests/Metering.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ TEST_CASE("Metering") {
289289

290290
setOnReceiveRequest("MeterValues", [base, &checkProcessed] (JsonObject payload) {
291291
checkProcessed = true;
292-
REQUIRE((!strcmp(payload["meterValue"][0]["sampledValue"][0]["value"] | "", "3600") ||
293-
!strcmp(payload["meterValue"][0]["sampledValue"][0]["value"] | "", "3600.0")));
292+
REQUIRE( !strncmp(payload["meterValue"][0]["sampledValue"][0]["value"] | "", "3600", strlen("3600")) );
294293
});
295294

296295
loop();

0 commit comments

Comments
 (0)