diff --git a/src/limit.cpp b/src/limit.cpp index e547eb9a..a1e7af40 100644 --- a/src/limit.cpp +++ b/src/limit.cpp @@ -4,6 +4,89 @@ #include "limit.h" #include "debug.h" +#include +#include +#include +#include "evse_man.h" + +// --------------------------------------------- +// +// LimitProperties Class +// +//---------------------------------------------- +LimitProperties::LimitProperties() +{ + _type = LimitType::None; + _value = 0; + _auto_release = true; +} + +LimitProperties::~LimitProperties() +{ + DBUGLN("LimitProperties Destructor"); + +} + +void LimitProperties::init() +{ + _type = LimitType::None; + _value = 0; + _auto_release = true; +} + +LimitType LimitProperties::getType() { + return _type; +}; + +bool LimitProperties::setType(LimitType type) +{ + _type = type; + return true; +}; + +uint32_t LimitProperties::getValue() { + return _value; + }; + +bool LimitProperties::setValue(uint32_t value) +{ + _value = value; + return true; +}; + +bool LimitProperties::getAutoRelease() { + return _auto_release; +} + +bool LimitProperties::deserialize(JsonObject &obj) +{ + if(obj.containsKey("type")) { + _type.fromString(obj["type"]); + } + if(obj.containsKey("value")) { + _value = obj["value"]; + } + if(obj.containsKey("auto_release")) { + _auto_release = obj["auto_release"]; + } + return _type > 0 && _value > 0; + +} + +bool LimitProperties::serialize(JsonObject &obj) +{ + + obj["type"] = _type.toString(); + obj["value"] = _value; + obj["auto_release"] = _auto_release; + return true; +} + +// --------------------------------------------- +// +// Limit Class +// +//---------------------------------------------- //global instance Limit limit; @@ -85,4 +168,5 @@ bool Limit::limitEnergy(uint32_t val) { return false; } else return true; -} \ No newline at end of file +} + diff --git a/src/limit.h b/src/limit.h index 9aa577de..65fdd964 100644 --- a/src/limit.h +++ b/src/limit.h @@ -1,14 +1,14 @@ #ifndef _OPENEVSE_LIMIT_H #define _OPENEVSE_LIMIT_H -#include -#include "evse_man.h" - #ifndef EVSE_LIMIT_LOOP_TIME #define EVSE_LIMIT_LOOP_TIME 1000 #endif - +#include +#include +#include +#include "evse_man.h" class LimitType { @@ -79,35 +79,19 @@ class LimitProperties : virtual public JsonSerialize<512> { bool _auto_release; public: LimitProperties(); + ~LimitProperties(); + void init(); + bool setType(LimitType type); + bool setValue(uint32_t value); + LimitType getType(); + uint32_t getValue(); + bool getAutoRelease(); + + using JsonSerialize::deserialize; + virtual bool deserialize(JsonObject &obj); + using JsonSerialize::serialize; + virtual bool serialize(JsonObject &obj); - void init() { - _type = LimitType::None; - _value = 0; - _auto_release = true; - }; - - bool setType(LimitType type) { - _type = type; - return true; - }; - bool setValue(uint32_t value) { - _value = value; - return true; - }; - LimitType getType() { - return _type; - }; - uint32_t getValue() { - return _value; - }; - bool getAutoRelease() { - return _auto_release; - } - - using JsonSerialize::deserialize; - virtual bool deserialize(JsonObject &obj); - using JsonSerialize::serialize; - virtual bool serialize(JsonObject &obj); }; class Limit: public MicroTasks::Task @@ -133,6 +117,14 @@ class Limit: public MicroTasks::Task bool hasLimit() { return _limit_properties.getType() != LimitType::None; } + bool set(String json) { + LimitProperties props; + if (props.deserialize(json)) { + set(props); + return true; + } + else return false; + } bool set(LimitProperties props) { _limit_properties = props; return true; diff --git a/src/web_server.cpp b/src/web_server.cpp index 40bcad9e..0064a39b 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -898,8 +898,7 @@ void handleLimitGet(MongooseHttpServerRequest *request, MongooseHttpServerRespon { if(limit.hasLimit()) { - LimitProperties props = limit.getLimitProperties(); - props.serialize(response); + limit.getLimitProperties().serialize(response); } else { response->setCode(404); response->print("{\"msg\":\"No limit\"}"); @@ -909,21 +908,16 @@ void handleLimitGet(MongooseHttpServerRequest *request, MongooseHttpServerRespon void handleLimitPost(MongooseHttpServerRequest *request, MongooseHttpServerResponseStream *response) { String body = request->body().toString(); - LimitProperties props; - if (props.deserialize(body)) { - if (limit.set(props)) { + + if (limit.set(body)) { response->setCode(201); response->print("{\"msg\":\"Created\"}"); // todo: mqtt_publish_limit(); // update limit props to mqtt } else { // unused for now response->setCode(500); - response->print("{\"msg\":\"Failed to set limit\"}"); + response->print("{\"msg\":\"Failed to parse JSON\"}"); } - } else { - response->setCode(500); - response->print("{\"msg\":\"Failed to parse JSON\"}"); - } } void handleLimitDelete(MongooseHttpServerRequest *request, MongooseHttpServerResponseStream *response)