add: schedules
This commit is contained in:
parent
2d24339421
commit
cac5781222
21 changed files with 1030 additions and 39 deletions
|
@ -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);
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue