Skip to content

Commit d5ff84e

Browse files
committed
distribute OCPP time to modules
1 parent 86dec10 commit d5ff84e

8 files changed

Lines changed: 52 additions & 17 deletions

File tree

src/ArduinoOcpp/Core/OcppTime.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1-
// matth-x/ESP8266-OCPP
1+
// matth-x/ArduinoOcpp
22
// Copyright Matthias Akstaller 2019 - 2021
33
// MIT License
44

55
#include <ArduinoOcpp/Core/OcppTime.h>
66

77
namespace ArduinoOcpp {
88

9+
namespace Clocks {
10+
11+
ulong lastClockReading = 0;
12+
otime_t lastClockValue = 0;
13+
14+
/*
15+
* Basic clock implementation. Works if millis() is exact enough for you and if device doesn't go in sleep mode.
16+
*/
17+
OcppClock DEFAULT_CLOCK = [] () {
18+
ulong tReading = (millis() - lastClockReading) / 1000UL;
19+
if (tReading > 0) {
20+
lastClockValue += tReading;
21+
lastClockReading += tReading * 1000UL;
22+
}
23+
return lastClockValue;};
24+
} //end namespace Clocks
25+
26+
927
OcppTimestamp::OcppTimestamp() {
1028

1129
}
@@ -170,6 +188,10 @@ OcppTimestamp OcppTimestamp::operator-(int secs) {
170188
return this->operator+(-secs);
171189
}
172190

191+
OcppTime::OcppTime(OcppClock system_clock) : system_clock(system_clock) {
192+
193+
}
194+
173195
bool OcppTime::setOcppTime(const char* jsonDateString) {
174196

175197
OcppTimestamp timestamp = OcppTimestamp();

src/ArduinoOcpp/Core/OcppTime.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// matth-x/ESP8266-OCPP
1+
// matth-x/ArduinoOcpp
22
// Copyright Matthias Akstaller 2019 - 2021
33
// MIT License
44

@@ -14,6 +14,15 @@
1414
namespace ArduinoOcpp {
1515

1616
typedef int32_t otime_t; //requires 32bit signed integer or bigger
17+
typedef std::function<otime_t()> OcppClock;
18+
19+
namespace Clocks {
20+
21+
/*
22+
* Basic clock implementation. Works if millis() is exact enough for you and if device doesn't go in sleep mode.
23+
*/
24+
extern OcppClock DEFAULT_CLOCK;
25+
} //end namespace Clocks
1726

1827
class OcppTimestamp {
1928
private:
@@ -61,11 +70,11 @@ class OcppTime {
6170
OcppTimestamp ocpp_basetime;
6271
otime_t system_basetime = 0; //your system clock's time at the moment when the OCPP server's time was taken
6372

64-
std::function<otime_t()> system_clock = [] () {return (otime_t) 0;};
73+
OcppClock system_clock = [] () {return (otime_t) 0;};
6574

6675
public:
6776

68-
OcppTime(std::function<otime_t()> system_clock);
77+
OcppTime(OcppClock system_clock);
6978

7079
otime_t getOcppTimeScalar(); //returns current time of the OCPP server in non-UNIX but signed integer format. t2 - t1 is the time difference in seconds.
7180
OcppTimestamp createTimestamp(otime_t scalar); //creates a timestamp in a JSON-serializable format. createTimestamp(getOcppTimeScalar()) will return the current OCPP time

src/ArduinoOcpp/Tasks/Metering/ConnectorMeterValuesRecorder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// matth-x/ESP8266-OCPP
1+
// matth-x/ArduinoOcpp
22
// Copyright Matthias Akstaller 2019 - 2021
33
// MIT License
44

@@ -8,8 +8,8 @@
88
using namespace ArduinoOcpp;
99
using namespace ArduinoOcpp::Ocpp16;
1010

11-
ConnectorMeterValuesRecorder::ConnectorMeterValuesRecorder(int connectorId)
12-
: connectorId(connectorId) {
11+
ConnectorMeterValuesRecorder::ConnectorMeterValuesRecorder(int connectorId, OcppTime *ocppTime)
12+
: connectorId(connectorId), ocppTime(ocppTime) {
1313
sampleTimestamp = LinkedList<time_t>();
1414
energy = LinkedList<float>();
1515
power = LinkedList<float>();

src/ArduinoOcpp/Tasks/Metering/ConnectorMeterValuesRecorder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// matth-x/ESP8266-OCPP
1+
// matth-x/ArduinoOcpp
22
// Copyright Matthias Akstaller 2019 - 2021
33
// MIT License
44

@@ -15,6 +15,7 @@
1515
#include <ArduinoOcpp/TimeHelper.h>
1616
#include <ArduinoOcpp/MessagesV16/MeterValues.h>
1717
#include <ArduinoOcpp/Core/Configuration.h>
18+
#include <ArduinoOcpp/Core/OcppTime.h>
1819

1920
namespace ArduinoOcpp {
2021

@@ -26,6 +27,7 @@ typedef std::function<float()> EnergySampler;
2627
class ConnectorMeterValuesRecorder {
2728
private:
2829
const int connectorId;
30+
OcppTime *ocppTime;
2931

3032
LinkedList<time_t> sampleTimestamp;
3133
LinkedList<float> energy;
@@ -46,7 +48,7 @@ class ConnectorMeterValuesRecorder {
4648
Ocpp16::MeterValues *toMeterValues();
4749
void clear();
4850
public:
49-
ConnectorMeterValuesRecorder(int connectorId);
51+
ConnectorMeterValuesRecorder(int connectorId, OcppTime *ocppTime);
5052

5153
Ocpp16::MeterValues *loop();
5254

src/ArduinoOcpp/Tasks/Metering/MeteringService.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// matth-x/ESP8266-OCPP
1+
// matth-x/ArduinoOcpp
22
// Copyright Matthias Akstaller 2019 - 2021
33
// MIT License
44

@@ -13,12 +13,12 @@
1313
using namespace ArduinoOcpp;
1414
using namespace ArduinoOcpp::Ocpp16;
1515

16-
MeteringService::MeteringService(WebSocketsClient *webSocket, int numConn)
16+
MeteringService::MeteringService(WebSocketsClient *webSocket, int numConn, OcppTime *ocppTime)
1717
: webSocket(webSocket), numConnectors(numConn) {
1818

1919
connectors = (ConnectorMeterValuesRecorder**) malloc(numConn * sizeof(ConnectorMeterValuesRecorder*));
2020
for (int i = 0; i < numConn; i++) {
21-
connectors[i] = new ConnectorMeterValuesRecorder(i);
21+
connectors[i] = new ConnectorMeterValuesRecorder(i, ocppTime);
2222
}
2323

2424
setMeteringSerivce(this); //make MeteringService available through Ocpp Engine

src/ArduinoOcpp/Tasks/Metering/MeteringService.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// matth-x/ESP8266-OCPP
1+
// matth-x/ArduinoOcpp
22
// Copyright Matthias Akstaller 2019 - 2021
33
// MIT License
44

@@ -34,7 +34,7 @@ class MeteringService {
3434
const int numConnectors;
3535
ConnectorMeterValuesRecorder **connectors;
3636
public:
37-
MeteringService(WebSocketsClient *webSocket, int numConnectors);
37+
MeteringService(WebSocketsClient *webSocket, int numConnectors, OcppTime *ocppTime);
3838

3939
~MeteringService();
4040

src/ArduinoOcpp/Tasks/SmartCharging/SmartChargingService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
using namespace::ArduinoOcpp;
2828

29-
SmartChargingService::SmartChargingService(float chargeLimit, int numConnectors, FilesystemOpt filesystemOpt)
30-
: DEFAULT_CHARGE_LIMIT(chargeLimit), filesystemOpt(filesystemOpt) {
29+
SmartChargingService::SmartChargingService(float chargeLimit, int numConnectors, OcppTime *ocppTime, FilesystemOpt filesystemOpt)
30+
: DEFAULT_CHARGE_LIMIT(chargeLimit), ocppTime(ocppTime), filesystemOpt(filesystemOpt) {
3131

3232
if (numConnectors > 2) {
3333
Serial.print(F("[SmartChargingService] Error: Unfortunately, multiple connectors are not implemented in SmartChargingService yet. Only connector 1 will receive charging limits\n"));

src/ArduinoOcpp/Tasks/SmartCharging/SmartChargingService.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <ArduinoOcpp/Tasks/SmartCharging/SmartChargingModel.h>
1515
#include <ArduinoOcpp/Core/ConfigurationOptions.h>
16+
#include <ArduinoOcpp/Core/OcppTime.h>
1617

1718
namespace ArduinoOcpp {
1819

@@ -21,6 +22,7 @@ typedef std::function<void(float)> OnLimitChange;
2122

2223
class SmartChargingService {
2324
private:
25+
OcppTime *ocppTime;
2426
const float DEFAULT_CHARGE_LIMIT;
2527
ChargingProfile *ChargePointMaxProfile[CHARGEPROFILEMAXSTACKLEVEL];
2628
ChargingProfile *TxDefaultProfile[CHARGEPROFILEMAXSTACKLEVEL];
@@ -38,7 +40,7 @@ class SmartChargingService {
3840
bool loadProfiles();
3941

4042
public:
41-
SmartChargingService(float chargeLimit, int numConnectors, FilesystemOpt filesystemOpt = FilesystemOpt::Use_Mount_FormatOnFail);
43+
SmartChargingService(float chargeLimit, int numConnectors, OcppTime *ocppTime, FilesystemOpt filesystemOpt = FilesystemOpt::Use_Mount_FormatOnFail);
4244
void beginCharging(time_t t, int transactionID);
4345
void beginChargingNow();
4446
void endChargingNow();

0 commit comments

Comments
 (0)