fix: myself. need smaller commits
This commit is contained in:
		
							parent
							
								
									0b8c755a6b
								
							
						
					
					
						commit
						0247031a3d
					
				
					 11 changed files with 164 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue