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

@ -7,4 +7,14 @@ namespace config
uint16_t discover_port = 4419;
int discover_timeout_ms = 2000;
uint8_t discover_code_accept = 0;
uint8_t discover_code_reject = 100;
uint8_t command_code_get_time = 1;
uint8_t command_code_get_id = 2;
uint8_t command_code_set_name = 100;
uint8_t command_code_get_name = 101;
uint8_t command_code_set_schedule = 102;
uint8_t command_code_get_schedule = 103;
uint8_t command_code_set_relay_name = 104;
uint8_t command_code_get_relay_name = 105;
}

View file

@ -10,6 +10,16 @@ namespace config
extern uint16_t discover_port;
extern int discover_timeout_ms;
extern uint8_t discover_code_accept;
extern uint8_t discover_code_reject;
extern uint8_t command_code_get_time;
extern uint8_t command_code_get_id;
extern uint8_t command_code_set_name;
extern uint8_t command_code_get_name;
extern uint8_t command_code_set_schedule;
extern uint8_t command_code_get_schedule;
extern uint8_t command_code_set_relay_name;
extern uint8_t command_code_get_relay_name;
}
#endif //EMGAUWA_CORE_CONFIG_H

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);

View file

@ -32,6 +32,9 @@ namespace helpers
sqlite3_stmt*
create_sql_filtered_query(const char *sql, sql_filter_builder **filters);
int
open_tcp_connection(char* host, char* port);
}
#endif //EMGAUWA_CORE_HELPERS_H

View file

@ -0,0 +1,33 @@
#include <helpers.h>
#include <netdb.h>
#include <trantor/utils/Logger.h>
int
helpers::open_tcp_connection(char *host, char *port)
{
int s, status;
struct addrinfo hints{}, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(host, port, &hints, &res)) != 0)
{
LOG_ERROR << "Error getting addressinfo: " << gai_strerror(status);
freeaddrinfo(res);
return 0;
}
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket
if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0)
{
LOG_ERROR << "Error opening connection";
freeaddrinfo(res);
return 0;
}
freeaddrinfo(res);
return s;
}

View file

@ -2,10 +2,17 @@
#include <cstring>
#include <cstring>
#include <trantor/utils/Logger.h>
#include <sys/socket.h>
#include <unistd.h>
#include <helpers.h>
#include "controller_dbo.h"
#include "globals.h"
controller_dbo::~controller_dbo()
{
relay_dbo::free_list(this->relays);
}
static bool controller_db_update_insert(controller_dbo *controller, sqlite3_stmt *stmt)
{
int rc;
@ -85,6 +92,7 @@ controller_db_select(sqlite3_stmt *stmt)
if (s == SQLITE_ROW)
{
controller_dbo *new_controller = controller_db_select_mapper(stmt);
new_controller->relays = relay_dbo::get_by_simple("controller_id", new_controller->id, (intptr_t)&sqlite3_bind_text);
row++;
all_controllers = (controller_dbo**)realloc(all_controllers, sizeof(controller_dbo*) * (row + 1));
@ -153,10 +161,18 @@ controller_dbo::to_json()
controller_json["name"] = this->name;
controller_json["id"] = this->id;
controller_json["ip"] = this->ip;
controller_json["port"] = this->port;
//controller_json["port"] = this->port;
controller_json["relay_count"] = this->relay_count;
controller_json["active"] = this->active;
controller_json["relays"] = Json::arrayValue;
for(int i = 0; this->relays[i] != nullptr; i++)
{
controller_json["relays"].append(this->relays[i]->to_json());
}
return controller_json;
}
@ -195,13 +211,32 @@ controller_dbo::get_by(helpers::sql_filter_builder **filters)
return controller_db_select(stmt);
}
bool
controller_dbo::command(int command_code, const char *payload)
{
char port[6];
sprintf(port, "%d", this->port);
int controller_socket = helpers::open_tcp_connection(this->ip, port);
if(!controller_socket)
{
return false;
}
send(controller_socket, &command_code, 1, 0);
send(controller_socket, payload, strlen(payload), 0);
close(controller_socket);
return true;
}
void
controller_dbo::free_list(controller_dbo **controllers_list)
{
for(int i = 0; controllers_list[i] != nullptr; i++)
{
free(controllers_list[i]);
delete controllers_list[i];
}
free(controllers_list);
}

View file

@ -5,6 +5,7 @@
#include <sqlite3.h>
#include <json/value.h>
#include <helpers.h>
#include "relay_dbo.h"
class controller_dbo
{
@ -16,6 +17,9 @@ public:
bool active;
int port;
int relay_count;
relay_dbo **relays;
~controller_dbo();
bool
update();
@ -29,9 +33,6 @@ public:
Json::Value
to_json();
static void
free_list(controller_dbo **controllers_list);
static controller_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func);
@ -40,6 +41,12 @@ public:
static controller_dbo**
get_all();
bool
command(int command_code, const char *payload);
static void
free_list(controller_dbo **controllers_list);
};

View file

@ -6,6 +6,7 @@
#include "relay_dbo.h"
#include "globals.h"
#include "controller_dbo.h"
#include "schedule_dbo.h"
static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
{
@ -41,7 +42,7 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
case 'a': // active_schedule_id
strncpy(new_relay->active_schedule_id, (const char*)sqlite3_column_text(stmt, i), 33);
break;
case 'd': // controller_id
case 'c': // controller_id
strncpy(new_relay->controller_id, (const char*)sqlite3_column_text(stmt, i), 33);
break;
case 'i':
@ -81,6 +82,18 @@ relay_db_select(sqlite3_stmt *stmt)
if (s == SQLITE_ROW)
{
relay_dbo *new_relay = relay_db_select_mapper(stmt);
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", new_relay->active_schedule_id, (intptr_t)&sqlite3_bind_text);
if(!schedules[0])
{
free(schedules);
schedules = schedule_dbo::get_by_simple("id", "off", (intptr_t)&sqlite3_bind_text);
strcpy(new_relay->active_schedule_id, "off");
}
new_relay->active_schedule = schedules[0];
free(schedules);
row++;
all_relays = (relay_dbo**)realloc(all_relays, sizeof(relay_dbo*) * (row + 1));
@ -150,6 +163,7 @@ relay_dbo::to_json()
relay_json["number"] = this->number;
relay_json["active_schedule_id"] = this->active_schedule_id;
relay_json["controller_id"] = this->controller_id;
relay_json["active_schedule"] = this->active_schedule->to_json();
return relay_json;
}

View file

@ -5,6 +5,7 @@
#include <sqlite3.h>
#include <json/value.h>
#include <helpers.h>
#include "schedule_dbo.h"
class relay_dbo
{
@ -15,6 +16,7 @@ public:
int number;
char controller_id[33];
char active_schedule_id[33];
schedule_dbo *active_schedule;
bool
update();