fix: myself. need smaller commits

This commit is contained in:
Tobias Reisinger 2019-07-29 22:02:38 +02:00
parent 0b8c755a6b
commit 0247031a3d
11 changed files with 164 additions and 11 deletions

View file

@ -4,8 +4,8 @@
#include <helpers.h>
#include <cmath>
#include <models/controller_dbo.h>
#include <fstream>
#include "api_v1_controllers.h"
using namespace api::v1;
void controllers::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
@ -94,15 +94,20 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
}
answer_payload[payload_length] = '\0';
std::ifstream answer_payload_stream(answer_payload);
std::istringstream answer_payload_stream(answer_payload);
Json::CharReaderBuilder json_reader;
json_reader["strictRoot"] = true;
std::string errors;
Json::Value client_info;
if (!Json::parseFromStream(json_reader, answer_payload_stream, &client_info, &errors))
{
LOG_ERROR << "Failed to parse configuration: " << errors;
LOG_ERROR << "Failed to parse response: " << errors;
discover_answer_buf[0] = config::discover_code_reject;
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
close(client_fd);
continue;
}
@ -120,7 +125,7 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
{
known_controllers[i]->active = true;
known_controllers[i]->update();
free(known_controllers[i]);
delete known_controllers[i];
found_discovered_in_list = true;
known_controllers[i] = known_controllers[i + 1];
}

View file

@ -2,6 +2,8 @@
#include <models/relay_dbo.h>
#include <helpers.h>
#include <models/controller_dbo.h>
#include <models/schedule_dbo.h>
#include <config.h>
#include "api_v1_controllers.h"
using namespace api::v1;
@ -93,8 +95,22 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
}
else
{
auto schedules = schedule_dbo::get_by_simple("id", body["active_schedule"].asCString(), (intptr_t)&sqlite3_bind_text);
auto controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t)&sqlite3_bind_text);
Json::Value payload;
payload["target"] = relay_num;
payload["schedule"] = schedules[0]->to_json();
Json::StreamWriterBuilder json_writer;
controllers[0]->command(config::command_code_set_schedule, Json::writeString(json_writer, payload).c_str());
auto resp = HttpResponse::newHttpJsonResponse(relay->to_json());
callback(resp);
schedule_dbo::free_list(schedules);
controller_dbo::free_list(controllers);
}
delete relay;

View file

@ -48,6 +48,15 @@ schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
void
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
{
if(strcmp(schedule_id.c_str(), "off") == 0)
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k403Forbidden);
callback(resp);
return;
}
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
if(schedules[0])
@ -100,6 +109,15 @@ void
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &schedule_id)
{
if(strcmp(schedule_id.c_str(), "off") == 0)
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k403Forbidden);
callback(resp);
return;
}
Json::Value body = *req->jsonObject();
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);