forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestStore.h
More file actions
70 lines (49 loc) · 1.89 KB
/
Copy pathRequestStore.h
File metadata and controls
70 lines (49 loc) · 1.89 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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2023
// MIT License
#ifndef MO_REQUESTSTORE_H
#define MO_REQUESTSTORE_H
#include <memory>
#include <deque>
#include <ArduinoJson.h>
#define MO_MAX_OPNR 10000
namespace MicroOcpp {
class RequestStore;
class FilesystemAdapter;
class Configuration;
class StoredOperationHandler {
private:
RequestStore& context;
int opNr = -1;
std::shared_ptr<FilesystemAdapter> filesystem;
std::unique_ptr<DynamicJsonDocument> rpc;
std::unique_ptr<DynamicJsonDocument> payload;
bool isPersistent = false;
public:
StoredOperationHandler(RequestStore& context, std::shared_ptr<FilesystemAdapter> filesystem) : context(context), filesystem(filesystem) {}
void setRpcData(std::unique_ptr<DynamicJsonDocument> rpc) {this->rpc = std::move(rpc);}
void setPayload(std::unique_ptr<DynamicJsonDocument> payload) {this->payload = std::move(payload);}
std::unique_ptr<DynamicJsonDocument> getRpcData() {return std::move(rpc);}
std::unique_ptr<DynamicJsonDocument> getPayload() {return std::move(payload);}
bool commit();
void clearBuffer() {rpc.reset(); payload.reset();}
bool restore(unsigned int opNr);
int getOpNr() {return isPersistent ? opNr : -1;}
};
class RequestStore {
private:
std::shared_ptr<FilesystemAdapter> filesystem;
std::shared_ptr<Configuration> opBeginInt; //Tx-related operations are stored; index of the first pending operation
unsigned int opEnd = 0; //one place after last number
public:
RequestStore() = delete;
RequestStore(std::shared_ptr<FilesystemAdapter> filesystem);
std::unique_ptr<StoredOperationHandler> makeOpHandler();
std::unique_ptr<StoredOperationHandler> fetchOpHandler(unsigned int opNr);
unsigned int reserveOpNr();
void advanceOpNr(unsigned int oldOpNr);
unsigned int getOpBegin();
unsigned int getOpEnd() {return opEnd;}
};
}
#endif