Skip to content

Commit

Permalink
moved limit to own API endpoint
Browse files Browse the repository at this point in the history
todo: remove limit from EvseManager
  • Loading branch information
KipK committed Jan 31, 2023
1 parent 6700e9d commit ccf959c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 43 deletions.
86 changes: 85 additions & 1 deletion src/limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,89 @@

#include "limit.h"
#include "debug.h"
#include <Arduino.h>
#include <ArduinoJson.h>
#include <MicroTasks.h>
#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;
Expand Down Expand Up @@ -85,4 +168,5 @@ bool Limit::limitEnergy(uint32_t val) {
return false;
}
else return true;
}
}

56 changes: 24 additions & 32 deletions src/limit.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef _OPENEVSE_LIMIT_H
#define _OPENEVSE_LIMIT_H

#include <MicroTasks.h>
#include "evse_man.h"


#ifndef EVSE_LIMIT_LOOP_TIME
#define EVSE_LIMIT_LOOP_TIME 1000
#endif

#include <Arduino.h>
#include <ArduinoJson.h>
#include <MicroTasks.h>
#include "evse_man.h"

class LimitType {

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
14 changes: 4 additions & 10 deletions src/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}");
Expand All @@ -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)
Expand Down

0 comments on commit ccf959c

Please sign in to comment.