add: schedules
This commit is contained in:
		
							parent
							
								
									2d24339421
								
							
						
					
					
						commit
						cac5781222
					
				
					 21 changed files with 1030 additions and 39 deletions
				
			
		| 
						 | 
				
			
			@ -5,4 +5,6 @@ namespace config
 | 
			
		|||
    int discover_max_client_backlog = 20;
 | 
			
		||||
    int discover_port_dev = 4420;
 | 
			
		||||
    int discover_port = 4419;
 | 
			
		||||
    int discover_timeout_ms = 2000;
 | 
			
		||||
    int discover_code_accept = 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								config.h
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								config.h
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -6,6 +6,8 @@ namespace config
 | 
			
		|||
    extern int discover_max_client_backlog;
 | 
			
		||||
    extern int discover_port_dev;
 | 
			
		||||
    extern int discover_port;
 | 
			
		||||
    extern int discover_timeout_ms;
 | 
			
		||||
    extern int discover_code_accept;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //EMGAUWA_CORE_CONFIG_H
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,70 @@
 | 
			
		|||
#include <netdb.h>
 | 
			
		||||
#include <models/device_dbo.h>
 | 
			
		||||
#include "api_v1_devices.h"
 | 
			
		||||
using namespace api::v1;
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
devices::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
 | 
			
		||||
{
 | 
			
		||||
    device_dbo **all_devices = device_dbo::get_all();
 | 
			
		||||
    Json::Value all_devices_json(Json::arrayValue);
 | 
			
		||||
 | 
			
		||||
    for(int i = 0; all_devices[i] != nullptr; i++)
 | 
			
		||||
    {
 | 
			
		||||
        all_devices_json.append(all_devices[i]->to_json());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto resp = HttpResponse::newHttpJsonResponse(all_devices_json);
 | 
			
		||||
 | 
			
		||||
    callback(resp);
 | 
			
		||||
 | 
			
		||||
    device_dbo::free_list(all_devices);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
devices::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, std::string device_id)
 | 
			
		||||
{
 | 
			
		||||
    device_dbo *device = device_dbo::get_one_by("id", device_id.c_str());
 | 
			
		||||
 | 
			
		||||
    if(device)
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpJsonResponse(device->to_json());
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
 | 
			
		||||
        free(device);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        resp->setStatusCode(k404NotFound);
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
devices::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, std::string device_id)
 | 
			
		||||
{
 | 
			
		||||
    device_dbo *device = device_dbo::get_one_by("id", device_id.c_str());
 | 
			
		||||
 | 
			
		||||
    if(device)
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        if(!device->remove())
 | 
			
		||||
        {
 | 
			
		||||
            resp->setStatusCode(k500InternalServerError);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
 | 
			
		||||
        free(device);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        resp->setStatusCode(k404NotFound);
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,15 +10,17 @@ namespace api
 | 
			
		|||
        public:
 | 
			
		||||
            METHOD_LIST_BEGIN
 | 
			
		||||
            METHOD_ADD(devices::post_discover, "/discover", Post);
 | 
			
		||||
            //METHOD_ADD(Devices::get_all,"/",Get);
 | 
			
		||||
            //METHOD_ADD(Devices::get_one,"/{1}",Get);
 | 
			
		||||
            METHOD_ADD(devices::get_all,"/",Get);
 | 
			
		||||
            METHOD_ADD(devices::get_one_by_id,"/{1}",Get);
 | 
			
		||||
            METHOD_ADD(devices::delete_one_by_id,"/{1}",Delete);
 | 
			
		||||
            //METHOD_ADD(Devices::get_relays_all,"/{1}/relays",Get);
 | 
			
		||||
            //METHOD_ADD(Devices::get_relays_one,"/{1}/relays/{2}",Get);
 | 
			
		||||
            METHOD_LIST_END
 | 
			
		||||
 | 
			
		||||
            void post_discover(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback);
 | 
			
		||||
            //void get_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback);
 | 
			
		||||
            //void get_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string device_id);
 | 
			
		||||
            void get_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback);
 | 
			
		||||
            void get_one_by_id(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string device_id);
 | 
			
		||||
            void delete_one_by_id(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string device_id);
 | 
			
		||||
            //void get_relays_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string device_id);
 | 
			
		||||
            //void get_relays_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string device_id,std::string relay_id);
 | 
			
		||||
        };
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,16 +2,17 @@
 | 
			
		|||
#include <unistd.h>
 | 
			
		||||
#include <config.h>
 | 
			
		||||
#include <helpers.h>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <models/device_dbo.h>
 | 
			
		||||
#include "api_v1_devices.h"
 | 
			
		||||
using namespace api::v1;
 | 
			
		||||
 | 
			
		||||
void devices::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
 | 
			
		||||
{
 | 
			
		||||
    LOG_DEBUG << "Discovering";
 | 
			
		||||
    auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
 | 
			
		||||
    int discovery_server_socket = helpers::bind_tcp_server("0.0.0.0", "0", config::discover_max_client_backlog);
 | 
			
		||||
    int discover_server_port = helpers::get_server_port(discovery_server_socket);
 | 
			
		||||
    int discover_server_socket = helpers::bind_tcp_server("0.0.0.0", "0", config::discover_max_client_backlog);
 | 
			
		||||
    int discover_server_port = helpers::get_server_port(discover_server_socket);
 | 
			
		||||
 | 
			
		||||
    if(discover_server_port == -1)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -23,11 +24,129 @@ void devices::post_discover(const HttpRequestPtr &req, std::function<void(const
 | 
			
		|||
    Json::Value payload;
 | 
			
		||||
    payload["port"] = discover_server_port;
 | 
			
		||||
 | 
			
		||||
    Json::StreamWriterBuilder wbuilder;
 | 
			
		||||
    Json::StreamWriterBuilder json_writer;
 | 
			
		||||
 | 
			
		||||
    helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, Json::writeString(wbuilder, payload).c_str());
 | 
			
		||||
    if(helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, Json::writeString(json_writer, payload).c_str()) < 0)
 | 
			
		||||
    {
 | 
			
		||||
        resp->setStatusCode(k500InternalServerError);
 | 
			
		||||
        callback(resp);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    close(discovery_server_socket);
 | 
			
		||||
    struct sockaddr_storage their_addr;
 | 
			
		||||
    socklen_t addr_size;
 | 
			
		||||
    int client_fd, s_ret;
 | 
			
		||||
    fd_set accept_fds;
 | 
			
		||||
    struct timeval timeout;
 | 
			
		||||
 | 
			
		||||
    uint8_t discover_answer_buf[1];
 | 
			
		||||
    uint8_t discover_header_buf[1];
 | 
			
		||||
 | 
			
		||||
    device_dbo **all_devices = device_dbo::get_all();
 | 
			
		||||
 | 
			
		||||
    while(true)
 | 
			
		||||
    {
 | 
			
		||||
        addr_size = sizeof(their_addr);
 | 
			
		||||
 | 
			
		||||
        FD_ZERO(&accept_fds);
 | 
			
		||||
        FD_SET(discover_server_socket, &accept_fds);
 | 
			
		||||
 | 
			
		||||
        timeout.tv_sec = floor(config::discover_timeout_ms / 1000);
 | 
			
		||||
        timeout.tv_usec = (config::discover_timeout_ms % 1000) * 1000;
 | 
			
		||||
 | 
			
		||||
        s_ret = select(discover_server_socket + 1, &accept_fds, NULL, NULL, &timeout);
 | 
			
		||||
        if(s_ret == 0)
 | 
			
		||||
        {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            if((client_fd = accept(discover_server_socket, (struct sockaddr *) &their_addr, &addr_size)) < 0)
 | 
			
		||||
            {
 | 
			
		||||
                LOG_ERROR << "Error Accepting client " << strerror(errno);
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if(recv(client_fd, discover_header_buf, 1, 0) < 0)
 | 
			
		||||
            {
 | 
			
		||||
                LOG_ERROR << "Error Receiving header from client";
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            int payload_length = discover_header_buf[0];
 | 
			
		||||
            char *answer_payload = (char*)malloc((payload_length + 1) * sizeof(*answer_payload));
 | 
			
		||||
 | 
			
		||||
            if(recv(client_fd, answer_payload, payload_length, 0) < 0)
 | 
			
		||||
            {
 | 
			
		||||
                LOG_ERROR << "Error Receiving payload from client";
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            struct sockaddr_in addr;
 | 
			
		||||
            socklen_t addr_size = sizeof(struct sockaddr_in);
 | 
			
		||||
            if(getpeername(client_fd, (struct sockaddr *)&addr, &addr_size) != 0)
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
                LOG_ERROR << "Error Receiving payload from client";
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            answer_payload[payload_length] = '\0';
 | 
			
		||||
 | 
			
		||||
            Json::Reader json_reader;
 | 
			
		||||
            Json::Value client_info;
 | 
			
		||||
 | 
			
		||||
            json_reader.parse(answer_payload, &answer_payload[payload_length], client_info, false);
 | 
			
		||||
 | 
			
		||||
            const char *discovered_id = client_info["id"].asCString();
 | 
			
		||||
 | 
			
		||||
            bool found_discovered_in_list = false;
 | 
			
		||||
 | 
			
		||||
            for(int i = 0; all_devices[i] != nullptr; i++)
 | 
			
		||||
            {
 | 
			
		||||
                if(!found_discovered_in_list)
 | 
			
		||||
                {
 | 
			
		||||
                    if(strcmp(all_devices[i]->id, discovered_id) == 0)
 | 
			
		||||
                    {
 | 
			
		||||
                        all_devices[i]->active = true;
 | 
			
		||||
                        all_devices[i]->update();
 | 
			
		||||
                        free(all_devices[i]);
 | 
			
		||||
                        found_discovered_in_list = true;
 | 
			
		||||
                        all_devices[i] = all_devices[i + 1];
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    all_devices[i] = all_devices[i + 1];
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if(!found_discovered_in_list)
 | 
			
		||||
            {
 | 
			
		||||
                device_dbo discovered_device;
 | 
			
		||||
                strcpy(discovered_device.ip, inet_ntoa(addr.sin_addr));
 | 
			
		||||
                strcpy(discovered_device.id, discovered_id);
 | 
			
		||||
                strcpy(discovered_device.name, client_info["name"].asCString());
 | 
			
		||||
                discovered_device.relay_count = client_info["relay_count"].asInt();
 | 
			
		||||
                discovered_device.port = client_info["port"].asInt();
 | 
			
		||||
                discovered_device.active = true;
 | 
			
		||||
 | 
			
		||||
                discovered_device.insert();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            discover_answer_buf[0] = config::discover_code_accept;
 | 
			
		||||
            send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
 | 
			
		||||
            close(client_fd);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    for(int i = 0; all_devices[i] != nullptr; i++)
 | 
			
		||||
    {
 | 
			
		||||
        all_devices[i]->active = false;
 | 
			
		||||
        all_devices[i]->update();
 | 
			
		||||
        LOG_DEBUG << "Lost: " << all_devices[i]->name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    close(discover_server_socket);
 | 
			
		||||
 | 
			
		||||
    callback(resp);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										98
									
								
								controllers/api_v1_schedules.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								controllers/api_v1_schedules.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,98 @@
 | 
			
		|||
#include <netdb.h>
 | 
			
		||||
#include <models/schedule_dbo.h>
 | 
			
		||||
#include <helpers.h>
 | 
			
		||||
#include "api_v1_schedules.h"
 | 
			
		||||
using namespace api::v1;
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
schedules::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
 | 
			
		||||
{
 | 
			
		||||
    schedule_dbo **all_schedules = schedule_dbo::get_all();
 | 
			
		||||
    Json::Value all_schedules_json(Json::arrayValue);
 | 
			
		||||
 | 
			
		||||
    for(int i = 0; all_schedules[i] != nullptr; i++)
 | 
			
		||||
    {
 | 
			
		||||
        all_schedules_json.append(all_schedules[i]->to_json());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Json::StreamWriterBuilder jw;
 | 
			
		||||
 | 
			
		||||
    auto resp = HttpResponse::newHttpJsonResponse(all_schedules_json);
 | 
			
		||||
 | 
			
		||||
    callback(resp);
 | 
			
		||||
 | 
			
		||||
    schedule_dbo::free_list(all_schedules);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, std::string schedule_id)
 | 
			
		||||
{
 | 
			
		||||
    schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
 | 
			
		||||
 | 
			
		||||
    if(schedule)
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpJsonResponse(schedule->to_json());
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
 | 
			
		||||
        free(schedule);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        resp->setStatusCode(k404NotFound);
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, std::string schedule_id)
 | 
			
		||||
{
 | 
			
		||||
    schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
 | 
			
		||||
 | 
			
		||||
    if(schedule)
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        if(!schedule->remove())
 | 
			
		||||
        {
 | 
			
		||||
            resp->setStatusCode(k500InternalServerError);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
 | 
			
		||||
        free(schedule);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
        resp->setStatusCode(k404NotFound);
 | 
			
		||||
 | 
			
		||||
        callback(resp);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
 | 
			
		||||
{
 | 
			
		||||
    Json::Value body = *req->jsonObject();
 | 
			
		||||
 | 
			
		||||
    const char *name = body["name"].asCString();
 | 
			
		||||
    period_list *periods = helpers::parse_periods(body["periods"]);
 | 
			
		||||
 | 
			
		||||
    schedule_dbo new_schedule;
 | 
			
		||||
 | 
			
		||||
    strncpy(new_schedule.name, name, 127);
 | 
			
		||||
    new_schedule.name[127] = '\0';
 | 
			
		||||
    strncpy(new_schedule.id, drogon::utils::getUuid().c_str(), 32);
 | 
			
		||||
    new_schedule.id[32] = '\0';
 | 
			
		||||
    new_schedule.periods = periods;
 | 
			
		||||
 | 
			
		||||
    new_schedule.insert();
 | 
			
		||||
 | 
			
		||||
    auto resp = HttpResponse::newHttpResponse();
 | 
			
		||||
    callback(resp);
 | 
			
		||||
 | 
			
		||||
    delete periods;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										28
									
								
								controllers/api_v1_schedules.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								controllers/api_v1_schedules.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
#include <drogon/HttpController.h>
 | 
			
		||||
using namespace drogon;
 | 
			
		||||
namespace api
 | 
			
		||||
{
 | 
			
		||||
    namespace v1
 | 
			
		||||
    {
 | 
			
		||||
        class schedules:public drogon::HttpController<schedules>
 | 
			
		||||
        {
 | 
			
		||||
        public:
 | 
			
		||||
            METHOD_LIST_BEGIN
 | 
			
		||||
            METHOD_ADD(schedules::post_new,"/",Post, "filters::json_required");
 | 
			
		||||
            METHOD_ADD(schedules::get_all,"/",Get);
 | 
			
		||||
            METHOD_ADD(schedules::get_one_by_id,"/{1}",Get);
 | 
			
		||||
            METHOD_ADD(schedules::delete_one_by_id,"/{1}",Delete);
 | 
			
		||||
            //METHOD_ADD(Devices::get_relays_all,"/{1}/relays",Get);
 | 
			
		||||
            //METHOD_ADD(Devices::get_relays_one,"/{1}/relays/{2}",Get);
 | 
			
		||||
            METHOD_LIST_END
 | 
			
		||||
 | 
			
		||||
            void post_new(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback);
 | 
			
		||||
            void get_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback);
 | 
			
		||||
            void get_one_by_id(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id);
 | 
			
		||||
            void delete_one_by_id(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id);
 | 
			
		||||
            //void get_relays_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id);
 | 
			
		||||
            //void get_relays_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id,std::string relay_id);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								filters/json_required.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								filters/json_required.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
#include "json_required.h"
 | 
			
		||||
 | 
			
		||||
using namespace drogon;
 | 
			
		||||
using namespace filters;
 | 
			
		||||
 | 
			
		||||
void json_required::doFilter(const HttpRequestPtr &req,
 | 
			
		||||
                         FilterCallback &&fcb,
 | 
			
		||||
                         FilterChainCallback &&fccb)
 | 
			
		||||
{
 | 
			
		||||
    if (req->getJsonObject())
 | 
			
		||||
    {
 | 
			
		||||
        fccb();
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    auto res = drogon::HttpResponse::newHttpResponse();
 | 
			
		||||
    res->setStatusCode(k400BadRequest);
 | 
			
		||||
    fcb(res);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								filters/json_required.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								filters/json_required.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,17 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <drogon/HttpFilter.h>
 | 
			
		||||
using namespace drogon;
 | 
			
		||||
namespace filters
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
class json_required : public HttpFilter<json_required>
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    json_required() {}
 | 
			
		||||
    virtual void doFilter(const HttpRequestPtr &req,
 | 
			
		||||
                          FilterCallback &&fcb,
 | 
			
		||||
                          FilterChainCallback &&fccb) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,6 +1,10 @@
 | 
			
		|||
#ifndef EMGAUWA_CORE_HELPERS_H
 | 
			
		||||
#define EMGAUWA_CORE_HELPERS_H
 | 
			
		||||
 | 
			
		||||
#include <json/value.h>
 | 
			
		||||
#include <models/period.h>
 | 
			
		||||
#include <models/period_list.h>
 | 
			
		||||
 | 
			
		||||
namespace helpers
 | 
			
		||||
{
 | 
			
		||||
    int
 | 
			
		||||
| 
						 | 
				
			
			@ -11,6 +15,9 @@ namespace helpers
 | 
			
		|||
 | 
			
		||||
    int
 | 
			
		||||
    send_udp_broadcast(const char *addr, int port, const char* message);
 | 
			
		||||
 | 
			
		||||
    period_list*
 | 
			
		||||
    parse_periods(Json::Value periods_json);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //EMGAUWA_CORE_HELPERS_H
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,7 +12,7 @@ helpers::get_server_port(int fd)
 | 
			
		|||
    socklen_t addrlen = sizeof(sin);
 | 
			
		||||
    if(getsockname(fd, (struct sockaddr *)&sin, &addrlen) == 0)
 | 
			
		||||
    {
 | 
			
		||||
        return sin.sin_port;
 | 
			
		||||
        return ntohs(sin.sin_port);
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										81
									
								
								helpers/parse_periods.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								helpers/parse_periods.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,81 @@
 | 
			
		|||
#include <helpers.h>
 | 
			
		||||
#include <drogon/drogon.h>
 | 
			
		||||
#include <models/period_list.h>
 | 
			
		||||
 | 
			
		||||
static int
 | 
			
		||||
parse_HHMM(const char *begin, uint16_t *h, uint16_t *m)
 | 
			
		||||
{
 | 
			
		||||
    uint16_t tmp_h, tmp_m;
 | 
			
		||||
    char *check = nullptr;
 | 
			
		||||
 | 
			
		||||
    tmp_h = strtol(begin, &check, 10);
 | 
			
		||||
    if(begin == check)
 | 
			
		||||
    {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    begin = check + 1;
 | 
			
		||||
    tmp_m = strtol(begin, &check, 10);
 | 
			
		||||
    if(begin == check)
 | 
			
		||||
    {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    *h = tmp_h;
 | 
			
		||||
    *m = tmp_m;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
period_list*
 | 
			
		||||
helpers::parse_periods(Json::Value periods_json)
 | 
			
		||||
{
 | 
			
		||||
    auto result = new period_list();
 | 
			
		||||
 | 
			
		||||
    for (Json::Value::ArrayIndex i = 0; i != periods_json.size(); i++)
 | 
			
		||||
    {
 | 
			
		||||
        Json::Value p = periods_json[i];
 | 
			
		||||
        if(!(p.isMember("start") && p.isMember("end")))
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        const char *start_str = p["start"].asCString();
 | 
			
		||||
        const char *end_str = p["end"].asCString();
 | 
			
		||||
 | 
			
		||||
        uint16_t h, m, start, end;
 | 
			
		||||
 | 
			
		||||
        if(parse_HHMM(start_str, &h, &m))
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        start = (h * 60) + m;
 | 
			
		||||
 | 
			
		||||
        if(parse_HHMM(end_str, &h, &m))
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        end = (h * 60) + m;
 | 
			
		||||
 | 
			
		||||
        if(start < 0 || start > 24 * 60 || end < 0 || end > 24 * 60)
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        bool period_overlap = false;
 | 
			
		||||
        for(int j = 0; j < result->length; j++)
 | 
			
		||||
        {
 | 
			
		||||
            if(result->periods[j]->is_in_period(start) || result->periods[j]->is_in_period(end))
 | 
			
		||||
            {
 | 
			
		||||
                period_overlap = true;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if(period_overlap)
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        result->add_period(start, end);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								main.cc
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								main.cc
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -2,11 +2,17 @@
 | 
			
		|||
#include <sqlite3.h>
 | 
			
		||||
 | 
			
		||||
#include <models/device_dbo.h>
 | 
			
		||||
#include <csignal>
 | 
			
		||||
 | 
			
		||||
#include "globals.h"
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
/*static void test()
 | 
			
		||||
{
 | 
			
		||||
    LOG_DEBUG << "LOOP";
 | 
			
		||||
}*/
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    int rc;
 | 
			
		||||
 | 
			
		||||
    /* Open database */
 | 
			
		||||
| 
						 | 
				
			
			@ -17,17 +23,11 @@ int main() {
 | 
			
		|||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    device_dbo test;
 | 
			
		||||
    strcpy(test.id, "meine-tolle_id-123");
 | 
			
		||||
    strcpy(test.name, "test_device latest name");
 | 
			
		||||
    strcpy(test.ip, "192.168.1.68");
 | 
			
		||||
    test.active = true;
 | 
			
		||||
 | 
			
		||||
    //test.save();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //Load config file
 | 
			
		||||
    drogon::app().loadConfigFile("../config.json");
 | 
			
		||||
 | 
			
		||||
    //drogon::app().getLoop()->runEvery(1, &test);
 | 
			
		||||
 | 
			
		||||
    //Run HTTP framework,the method will block in the internal event loop
 | 
			
		||||
    drogon::app().run();
 | 
			
		||||
    return 0;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,25 +3,27 @@
 | 
			
		|||
//
 | 
			
		||||
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <trantor/utils/Logger.h>
 | 
			
		||||
#include "device_dbo.h"
 | 
			
		||||
#include "globals.h"
 | 
			
		||||
#include <macros.h>
 | 
			
		||||
 | 
			
		||||
bool device_dbo::save()
 | 
			
		||||
static bool device_db_update_insert(device_dbo *device, sqlite3_stmt *stmt)
 | 
			
		||||
{
 | 
			
		||||
    int rc;
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "INSERT INTO devices(id, name, ip, active) values (?1, ?2, ?3, ?4);", -1, &stmt, nullptr);
 | 
			
		||||
    //sqlite3_prepare_v2(globals::db, "UPDATE devices set name = ?2, ip = ?3, active = ?4 WHERE id = ?1;", -1, &stmt, nullptr);
 | 
			
		||||
 | 
			
		||||
    sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_text(stmt, 2, this->name, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_text(stmt, 3, this->ip, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_int(stmt, 4, this->active);
 | 
			
		||||
    //sqlite3_bind_int(stmt, 2, 21);
 | 
			
		||||
    sqlite3_bind_text(stmt, 1, device->id, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_text(stmt, 2, device->name, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_text(stmt, 3, device->ip, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_int(stmt, 4, device->active);
 | 
			
		||||
    sqlite3_bind_int(stmt, 5, device->port);
 | 
			
		||||
    sqlite3_bind_int(stmt, 6, device->relay_count);
 | 
			
		||||
 | 
			
		||||
    rc = sqlite3_step(stmt);
 | 
			
		||||
    if (rc != SQLITE_DONE) {
 | 
			
		||||
    if (rc != SQLITE_DONE)
 | 
			
		||||
    {
 | 
			
		||||
        printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -30,3 +32,162 @@ bool device_dbo::save()
 | 
			
		|||
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static device_dbo*
 | 
			
		||||
device_db_select_mapper(sqlite3_stmt *stmt)
 | 
			
		||||
{
 | 
			
		||||
    auto *new_device = (device_dbo*)malloc(sizeof(device_dbo));
 | 
			
		||||
    for(int i = 0; i < sqlite3_column_count(stmt); i++)
 | 
			
		||||
    {
 | 
			
		||||
        const char *name = sqlite3_column_name(stmt, i);
 | 
			
		||||
        switch(name[0])
 | 
			
		||||
        {
 | 
			
		||||
            case 'a': // active
 | 
			
		||||
                new_device->active = sqlite3_column_int(stmt, i);
 | 
			
		||||
                break;
 | 
			
		||||
            case 'i':
 | 
			
		||||
                switch(name[1])
 | 
			
		||||
                {
 | 
			
		||||
                    case 'd': // id
 | 
			
		||||
                        strncpy(new_device->id, (const char*)sqlite3_column_text(stmt, i), 32);
 | 
			
		||||
                        break;
 | 
			
		||||
                    case 'p': // ip
 | 
			
		||||
                        strncpy(new_device->ip, (const char*)sqlite3_column_text(stmt, i), 16);
 | 
			
		||||
                        break;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case 'n': // name
 | 
			
		||||
                strncpy(new_device->name, (const char*)sqlite3_column_text(stmt, i), 127);
 | 
			
		||||
                break;
 | 
			
		||||
            case 'p': // port
 | 
			
		||||
                new_device->port = sqlite3_column_int(stmt, i);
 | 
			
		||||
                break;
 | 
			
		||||
            case 'r': // relay_count
 | 
			
		||||
                new_device->relay_count = sqlite3_column_int(stmt, i);
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return new_device;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static device_dbo**
 | 
			
		||||
device_db_select(sqlite3_stmt *stmt)
 | 
			
		||||
{
 | 
			
		||||
    auto **all_devices = (device_dbo**)malloc(sizeof(device_dbo*));
 | 
			
		||||
 | 
			
		||||
    int row = 0;
 | 
			
		||||
 | 
			
		||||
    while(true)
 | 
			
		||||
    {
 | 
			
		||||
        int s;
 | 
			
		||||
 | 
			
		||||
        s = sqlite3_step(stmt);
 | 
			
		||||
        if (s == SQLITE_ROW)
 | 
			
		||||
        {
 | 
			
		||||
            device_dbo *new_device = device_db_select_mapper(stmt);
 | 
			
		||||
            row++;
 | 
			
		||||
 | 
			
		||||
            all_devices = (device_dbo**)realloc(all_devices, sizeof(device_dbo*) * (row + 1));
 | 
			
		||||
            all_devices[row - 1] = new_device;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            if (s == SQLITE_DONE)
 | 
			
		||||
            {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                LOG_ERROR << "Error Selecting devices from database";
 | 
			
		||||
                return nullptr;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    all_devices[row] = nullptr;
 | 
			
		||||
 | 
			
		||||
    return all_devices;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool
 | 
			
		||||
device_dbo::update()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "UPDATE devices set name = ?2, ip = ?3, active = ?4, port = ?5, relay_count = ?6 WHERE id = ?1;", -1, &stmt, nullptr);
 | 
			
		||||
 | 
			
		||||
    return device_db_update_insert(this, stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool
 | 
			
		||||
device_dbo::insert()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "INSERT INTO devices(id, name, ip, active, port, relay_count) values (?1, ?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr);
 | 
			
		||||
 | 
			
		||||
    return device_db_update_insert(this, stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool
 | 
			
		||||
device_dbo::remove()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
    int rc;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "DELETE FROM devices WHERE id=?1;", -1, &stmt, nullptr);
 | 
			
		||||
    sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
 | 
			
		||||
    rc = sqlite3_step(stmt);
 | 
			
		||||
 | 
			
		||||
    return rc == SQLITE_DONE;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Json::Value
 | 
			
		||||
device_dbo::to_json()
 | 
			
		||||
{
 | 
			
		||||
    Json::Value device_json;
 | 
			
		||||
    device_json["name"] = this->name;
 | 
			
		||||
    device_json["id"] = this->id;
 | 
			
		||||
    device_json["ip"] = this->ip;
 | 
			
		||||
    device_json["port"] = this->port;
 | 
			
		||||
    device_json["relay_count"] = this->relay_count;
 | 
			
		||||
    device_json["active"] = this->active;
 | 
			
		||||
 | 
			
		||||
    return device_json;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
device_dbo**
 | 
			
		||||
device_dbo::get_all()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "SELECT * FROM devices;", -1, &stmt, nullptr);
 | 
			
		||||
 | 
			
		||||
    return device_db_select(stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
device_dbo*
 | 
			
		||||
device_dbo::get_one_by(const char *key, const char *value)
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
    char* sql;
 | 
			
		||||
 | 
			
		||||
    asprintf(&sql, "SELECT * FROM devices WHERE %s=?1;", key);
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, sql, -1, &stmt, nullptr);
 | 
			
		||||
    sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
 | 
			
		||||
 | 
			
		||||
    return device_db_select(stmt)[0];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
device_dbo::free_list(device_dbo **devices_list)
 | 
			
		||||
{
 | 
			
		||||
    for(int i = 0; devices_list[i] != nullptr; i++)
 | 
			
		||||
    {
 | 
			
		||||
        free(devices_list[i]);
 | 
			
		||||
    }
 | 
			
		||||
    free(devices_list);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,18 +3,39 @@
 | 
			
		|||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <sqlite3.h>
 | 
			
		||||
#include <json/value.h>
 | 
			
		||||
 | 
			
		||||
class device_dbo
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    char id[23];
 | 
			
		||||
 | 
			
		||||
    char id[33];
 | 
			
		||||
    char name[128];
 | 
			
		||||
    char ip[17];
 | 
			
		||||
    bool active;
 | 
			
		||||
    int port;
 | 
			
		||||
    int relay_count;
 | 
			
		||||
 | 
			
		||||
    bool save();
 | 
			
		||||
    bool
 | 
			
		||||
    update();
 | 
			
		||||
 | 
			
		||||
    bool
 | 
			
		||||
    insert();
 | 
			
		||||
 | 
			
		||||
    bool
 | 
			
		||||
    remove();
 | 
			
		||||
 | 
			
		||||
    Json::Value
 | 
			
		||||
    to_json();
 | 
			
		||||
 | 
			
		||||
    static void
 | 
			
		||||
    free_list(device_dbo **devices_list);
 | 
			
		||||
 | 
			
		||||
    static device_dbo*
 | 
			
		||||
    get_one_by(const char *key, const char *value);
 | 
			
		||||
 | 
			
		||||
    static device_dbo**
 | 
			
		||||
    get_all();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										41
									
								
								models/period.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								models/period.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include "period.h"
 | 
			
		||||
 | 
			
		||||
period::period(uint16_t start, uint16_t end)
 | 
			
		||||
{
 | 
			
		||||
    this->start = start;
 | 
			
		||||
    this->end = end;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Json::Value
 | 
			
		||||
period::to_json()
 | 
			
		||||
{
 | 
			
		||||
    Json::Value result;
 | 
			
		||||
 | 
			
		||||
    char start[6], end[6];
 | 
			
		||||
 | 
			
		||||
    sprintf(start, "%02d:%02d", (int)(this->start / 60), this->start % 60);
 | 
			
		||||
    sprintf(end, "%02d:%02d", (int)(this->end / 60), this->end % 60);
 | 
			
		||||
 | 
			
		||||
    result["start"] = std::string(start);
 | 
			
		||||
    result["end"] = std::string(end);
 | 
			
		||||
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool
 | 
			
		||||
period::is_in_period(uint16_t timestamp)
 | 
			
		||||
{
 | 
			
		||||
    if(this->start < this->end)
 | 
			
		||||
    {
 | 
			
		||||
        return this->start < timestamp and timestamp < this->end;
 | 
			
		||||
    }
 | 
			
		||||
    if(this->start > this->end)
 | 
			
		||||
    {
 | 
			
		||||
        return this->end < timestamp and timestamp < this->start;
 | 
			
		||||
    }
 | 
			
		||||
    return this->start == timestamp;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								models/period.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								models/period.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
#ifndef EMGAUWA_CORE_PERIOD_H
 | 
			
		||||
#define EMGAUWA_CORE_PERIOD_H
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#include <json/json.h>
 | 
			
		||||
 | 
			
		||||
class period
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    uint16_t start;
 | 
			
		||||
    uint16_t end;
 | 
			
		||||
 | 
			
		||||
    period(uint16_t start, uint16_t end);
 | 
			
		||||
 | 
			
		||||
    Json::Value
 | 
			
		||||
    to_json();
 | 
			
		||||
 | 
			
		||||
    bool
 | 
			
		||||
    is_in_period(uint16_t timestamp);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif //EMGAUWA_CORE_PERIOD_H
 | 
			
		||||
							
								
								
									
										73
									
								
								models/period_list.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								models/period_list.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,73 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <drogon/drogon.h>
 | 
			
		||||
#include "period_list.h"
 | 
			
		||||
 | 
			
		||||
period_list::period_list()
 | 
			
		||||
{
 | 
			
		||||
    this->periods = (period**)malloc(0);
 | 
			
		||||
    this->length = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
period_list::period_list(const uint16_t* periods_blob)
 | 
			
		||||
{
 | 
			
		||||
    this->length = periods_blob[0];
 | 
			
		||||
    this->periods = (period**)malloc(sizeof(period*) * this->length);
 | 
			
		||||
 | 
			
		||||
    for(int i = 0; i < length; i++)
 | 
			
		||||
    {
 | 
			
		||||
        auto new_period = new period(periods_blob[(i * 2) + 1], periods_blob[(i * 2) + 2]);
 | 
			
		||||
        periods[i] = new_period;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
period_list::period_list(period **periods, int length)
 | 
			
		||||
{
 | 
			
		||||
    this->periods = periods;
 | 
			
		||||
    this->length = length;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
period_list::~period_list()
 | 
			
		||||
{
 | 
			
		||||
    for(int i = 0; i < length; i++)
 | 
			
		||||
    {
 | 
			
		||||
        delete this->periods[i];
 | 
			
		||||
    }
 | 
			
		||||
    free(this->periods);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
period_list::add_period(uint16_t start, uint16_t end)
 | 
			
		||||
{
 | 
			
		||||
    this->length++;
 | 
			
		||||
    this->periods = (period**)realloc(this->periods, sizeof(period*) * this->length);
 | 
			
		||||
    this->periods[this->length - 1] = new period(start, end);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Json::Value
 | 
			
		||||
period_list::to_json()
 | 
			
		||||
{
 | 
			
		||||
    Json::Value result(Json::arrayValue);
 | 
			
		||||
    for(int i = 0; i < this->length; i++)
 | 
			
		||||
    {
 | 
			
		||||
        result.append(this->periods[i]->to_json());
 | 
			
		||||
    }
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t*
 | 
			
		||||
period_list::to_db_blob()
 | 
			
		||||
{
 | 
			
		||||
    auto result = (uint16_t*)malloc(sizeof(uint16_t) * ((this->length * 2) + 1));
 | 
			
		||||
 | 
			
		||||
    result[0] = this->length;
 | 
			
		||||
 | 
			
		||||
    for(int i = 0; i < this->length; i++)
 | 
			
		||||
    {
 | 
			
		||||
        result[(i * 2) + 1] = this->periods[i]->start;
 | 
			
		||||
        result[(i * 2) + 2] = this->periods[i]->end;
 | 
			
		||||
    }
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								models/period_list.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								models/period_list.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
#ifndef EMGAUWA_CORE_PERIOD_LIST_H
 | 
			
		||||
#define EMGAUWA_CORE_PERIOD_LIST_H
 | 
			
		||||
 | 
			
		||||
#include <json/json.h>
 | 
			
		||||
#include "period.h"
 | 
			
		||||
 | 
			
		||||
class period_list
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    period **periods;
 | 
			
		||||
    int length;
 | 
			
		||||
 | 
			
		||||
    period_list();
 | 
			
		||||
    period_list(const uint16_t *periods_blob);
 | 
			
		||||
    period_list(period **periods, int length);
 | 
			
		||||
    ~period_list();
 | 
			
		||||
 | 
			
		||||
    void
 | 
			
		||||
    add_period(uint16_t start, uint16_t end);
 | 
			
		||||
 | 
			
		||||
    Json::Value
 | 
			
		||||
    to_json();
 | 
			
		||||
 | 
			
		||||
    uint16_t*
 | 
			
		||||
    to_db_blob();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif //EMGAUWA_CORE_PERIOD_LIST_H
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,182 @@
 | 
			
		|||
//
 | 
			
		||||
// Created by tobias on 09/07/19.
 | 
			
		||||
// Created by tobias on 08/07/19.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <trantor/utils/Logger.h>
 | 
			
		||||
#include "schedule_dbo.h"
 | 
			
		||||
#include "globals.h"
 | 
			
		||||
#include "period.h"
 | 
			
		||||
#include <macros.h>
 | 
			
		||||
 | 
			
		||||
static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt)
 | 
			
		||||
{
 | 
			
		||||
    int rc;
 | 
			
		||||
    uint16_t *periods_blob = schedule->periods->to_db_blob();
 | 
			
		||||
 | 
			
		||||
    sqlite3_bind_text(stmt, 1, schedule->id, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC);
 | 
			
		||||
    sqlite3_bind_blob(stmt, 3, periods_blob, sizeof(uint16_t) * ((periods_blob[0] * 2) + 1), SQLITE_STATIC);
 | 
			
		||||
 | 
			
		||||
    rc = sqlite3_step(stmt);
 | 
			
		||||
    if (rc != SQLITE_DONE)
 | 
			
		||||
    {
 | 
			
		||||
        printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
 | 
			
		||||
        free(periods_blob);
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sqlite3_finalize(stmt);
 | 
			
		||||
 | 
			
		||||
    free(periods_blob);
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static schedule_dbo*
 | 
			
		||||
schedule_db_select_mapper(sqlite3_stmt *stmt)
 | 
			
		||||
{
 | 
			
		||||
    auto new_schedule = new schedule_dbo();
 | 
			
		||||
    for(int i = 0; i < sqlite3_column_count(stmt); i++)
 | 
			
		||||
    {
 | 
			
		||||
        const char *name = sqlite3_column_name(stmt, i);
 | 
			
		||||
        switch(name[0])
 | 
			
		||||
        {
 | 
			
		||||
            case 'i': // id
 | 
			
		||||
                strncpy(new_schedule->id, (const char*)sqlite3_column_text(stmt, i), 32);
 | 
			
		||||
                new_schedule->id[32] = '\0';
 | 
			
		||||
                break;
 | 
			
		||||
            case 'n': // name
 | 
			
		||||
                strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
 | 
			
		||||
                new_schedule->name[127] = '\0';
 | 
			
		||||
                break;
 | 
			
		||||
            case 'p': // periods
 | 
			
		||||
                auto periods_blob = (const uint16_t*)sqlite3_column_blob(stmt, i);
 | 
			
		||||
                new_schedule->periods = new period_list(periods_blob);
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return new_schedule;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static schedule_dbo**
 | 
			
		||||
schedule_db_select(sqlite3_stmt *stmt)
 | 
			
		||||
{
 | 
			
		||||
    auto **all_schedules = (schedule_dbo**)malloc(sizeof(schedule_dbo*));
 | 
			
		||||
 | 
			
		||||
    int row = 0;
 | 
			
		||||
 | 
			
		||||
    while(true)
 | 
			
		||||
    {
 | 
			
		||||
        int s;
 | 
			
		||||
 | 
			
		||||
        s = sqlite3_step(stmt);
 | 
			
		||||
        if (s == SQLITE_ROW)
 | 
			
		||||
        {
 | 
			
		||||
            schedule_dbo *new_schedule = schedule_db_select_mapper(stmt);
 | 
			
		||||
            row++;
 | 
			
		||||
 | 
			
		||||
            all_schedules = (schedule_dbo**)realloc(all_schedules, sizeof(schedule_dbo*) * (row + 1));
 | 
			
		||||
            all_schedules[row - 1] = new_schedule;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            if (s == SQLITE_DONE)
 | 
			
		||||
            {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                LOG_ERROR << "Error Selecting schedules from database";
 | 
			
		||||
                return nullptr;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    all_schedules[row] = nullptr;
 | 
			
		||||
 | 
			
		||||
    return all_schedules;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool
 | 
			
		||||
schedule_dbo::update()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "UPDATE schedules set name = ?2, periods = ?3 WHERE id = ?1;", -1, &stmt, nullptr);
 | 
			
		||||
 | 
			
		||||
    return schedule_db_update_insert(this, stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool
 | 
			
		||||
schedule_dbo::insert()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(id, name, periods) values (?1, ?2, ?3);", -1, &stmt, nullptr);
 | 
			
		||||
 | 
			
		||||
    return schedule_db_update_insert(this, stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool
 | 
			
		||||
schedule_dbo::remove()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
    int rc;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr);
 | 
			
		||||
    sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
 | 
			
		||||
    rc = sqlite3_step(stmt);
 | 
			
		||||
 | 
			
		||||
    return rc == SQLITE_DONE;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Json::Value
 | 
			
		||||
schedule_dbo::to_json()
 | 
			
		||||
{
 | 
			
		||||
    Json::Value schedule_json;
 | 
			
		||||
    schedule_json["name"] = this->name;
 | 
			
		||||
    schedule_json["id"] = this->id;
 | 
			
		||||
    schedule_json["periods"] = this->periods->to_json();
 | 
			
		||||
 | 
			
		||||
    return schedule_json;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
schedule_dbo**
 | 
			
		||||
schedule_dbo::get_all()
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, "SELECT * FROM schedules;", -1, &stmt, nullptr);
 | 
			
		||||
 | 
			
		||||
    return schedule_db_select(stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
schedule_dbo*
 | 
			
		||||
schedule_dbo::get_one_by(const char *key, const char *value)
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
    char* sql;
 | 
			
		||||
 | 
			
		||||
    asprintf(&sql, "SELECT * FROM schedules WHERE %s=?1;", key);
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(globals::db, sql, -1, &stmt, nullptr);
 | 
			
		||||
    sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
 | 
			
		||||
 | 
			
		||||
    return schedule_db_select(stmt)[0];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
schedule_dbo::free_list(schedule_dbo **schedules_list)
 | 
			
		||||
{
 | 
			
		||||
    for(int i = 0; schedules_list[i] != nullptr; i++)
 | 
			
		||||
    {
 | 
			
		||||
        delete schedules_list[i]->periods;
 | 
			
		||||
        delete schedules_list[i];
 | 
			
		||||
    }
 | 
			
		||||
    free(schedules_list);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,40 @@
 | 
			
		|||
//
 | 
			
		||||
// Created by tobias on 09/07/19.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef EMGAUWA_CORE_SCHEDULE_DBO_H
 | 
			
		||||
#define EMGAUWA_CORE_SCHEDULE_DBO_H
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <sqlite3.h>
 | 
			
		||||
#include <json/value.h>
 | 
			
		||||
#include "period.h"
 | 
			
		||||
#include "period_list.h"
 | 
			
		||||
 | 
			
		||||
class schedule_dbo
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    char id[33];
 | 
			
		||||
    char name[128];
 | 
			
		||||
    period_list *periods;
 | 
			
		||||
 | 
			
		||||
    bool
 | 
			
		||||
    update();
 | 
			
		||||
 | 
			
		||||
    bool
 | 
			
		||||
    insert();
 | 
			
		||||
 | 
			
		||||
    bool
 | 
			
		||||
    remove();
 | 
			
		||||
 | 
			
		||||
    Json::Value
 | 
			
		||||
    to_json();
 | 
			
		||||
 | 
			
		||||
    static void
 | 
			
		||||
    free_list(schedule_dbo **schedules_list);
 | 
			
		||||
 | 
			
		||||
    static schedule_dbo*
 | 
			
		||||
    get_one_by(const char *key, const char *value);
 | 
			
		||||
 | 
			
		||||
    static schedule_dbo**
 | 
			
		||||
    get_all();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue