add: macro endpoints
add: basic macro support fix: database locking with lock-pointer fix: memory leaks
This commit is contained in:
parent
6a2b94ef1c
commit
9d2c48d645
30 changed files with 606 additions and 213 deletions
18
.drone.yml
18
.drone.yml
|
@ -29,12 +29,12 @@ steps:
|
||||||
image: serguzim/emgauwa-builder
|
image: serguzim/emgauwa-builder
|
||||||
pull: always
|
pull: always
|
||||||
commands:
|
commands:
|
||||||
- tar xzf emgauwa-controller.tar.gz
|
- tar xzf emgauwa-controller.tar.gz
|
||||||
- cd controller
|
- cd controller
|
||||||
- mkdir build
|
- mkdir build
|
||||||
- cd build
|
- cd build
|
||||||
- cmake -DWIRING_PI_DEBUG=on ..
|
- cmake -DWIRING_PI_DEBUG=on ..
|
||||||
- make
|
- make
|
||||||
|
|
||||||
- name: test
|
- name: test
|
||||||
image: serguzim/emgauwa-builder
|
image: serguzim/emgauwa-builder
|
||||||
|
@ -47,6 +47,12 @@ steps:
|
||||||
- cmake ..
|
- cmake ..
|
||||||
- make test
|
- make test
|
||||||
|
|
||||||
|
- name: show-log-valgrind
|
||||||
|
image: bash
|
||||||
|
pull: always
|
||||||
|
commands:
|
||||||
|
- cat tests/testing_latest/valgrind.log
|
||||||
|
|
||||||
- name: gitea_release
|
- name: gitea_release
|
||||||
image: plugins/gitea-release
|
image: plugins/gitea-release
|
||||||
settings:
|
settings:
|
||||||
|
|
|
@ -10,3 +10,7 @@ end_of_line = lf
|
||||||
insert_final_newline = true
|
insert_final_newline = true
|
||||||
indent_style = space
|
indent_style = space
|
||||||
indent_size = 4
|
indent_size = 4
|
||||||
|
|
||||||
|
[*.yml]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
|
|
||||||
|
typedef int database_transaction_lock;
|
||||||
|
|
||||||
extern sqlite3 *global_database;
|
extern sqlite3 *global_database;
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -15,14 +17,14 @@ void
|
||||||
database_migrate();
|
database_migrate();
|
||||||
|
|
||||||
|
|
||||||
int
|
void
|
||||||
database_transaction_begin();
|
database_transaction_begin(database_transaction_lock *lock);
|
||||||
|
|
||||||
void
|
void
|
||||||
database_transaction_commit();
|
database_transaction_commit(database_transaction_lock *lock);
|
||||||
|
|
||||||
void
|
void
|
||||||
database_transaction_rollback();
|
database_transaction_rollback(database_transaction_lock *lock);
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
18
include/endpoints/api_v1_macros.h
Normal file
18
include/endpoints/api_v1_macros.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef CORE_ENDPOINTS_API_V1_MACROS_H
|
||||||
|
#define CORE_ENDPOINTS_API_V1_MACROS_H
|
||||||
|
|
||||||
|
#include <router.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
api_v1_macros_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||||
|
|
||||||
|
void
|
||||||
|
api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||||
|
|
||||||
|
//void
|
||||||
|
//api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||||
|
|
||||||
|
//void
|
||||||
|
//api_v1_tags_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||||
|
|
||||||
|
#endif /* CORE_ENDPOINTS_API_V1_MACROS_H */
|
|
@ -1,7 +0,0 @@
|
||||||
#ifndef CORE_JUNCTION_MACRO_H
|
|
||||||
#define CORE_JUNCTION_MACRO_H
|
|
||||||
|
|
||||||
int
|
|
||||||
junction_macro_insert(int macro_id, int relay_id, int schedule_id, uint8_t weekday);
|
|
||||||
|
|
||||||
#endif /* CORE_JUNCTION_MACRO_H */
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <constants.h>
|
#include <constants.h>
|
||||||
#include <models/period.h>
|
#include <models/period.h>
|
||||||
|
#include <models/macro_action.h>
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
|
27
include/models/macro_action.h
Normal file
27
include/models/macro_action.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#ifndef CORE_MODELS_MACRO_ACTION_H
|
||||||
|
#define CORE_MODELS_MACRO_ACTION_H
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int macro_id;
|
||||||
|
int relay_id;
|
||||||
|
int schedule_id;
|
||||||
|
uint8_t weekday;
|
||||||
|
} macro_action_t;
|
||||||
|
|
||||||
|
int
|
||||||
|
macro_action_insert(macro_action_t *macro_action);
|
||||||
|
|
||||||
|
macro_action_t**
|
||||||
|
macro_action_get_for_macro(int macro_id);
|
||||||
|
|
||||||
|
void
|
||||||
|
macro_action_free_list(macro_action_t **macro_actions);
|
||||||
|
|
||||||
|
int*
|
||||||
|
macro_action_get_macro_ids_with_schedule(int schedule_id);
|
||||||
|
|
||||||
|
int*
|
||||||
|
macro_action_get_macro_ids_with_relay(int relay_id);
|
||||||
|
|
||||||
|
#endif /* CORE_MODELS_MACRO_ACTION_H */
|
|
@ -11,7 +11,7 @@ CREATE TABLE macros
|
||||||
name VARCHAR(128)
|
name VARCHAR(128)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE junction_macro
|
CREATE TABLE macro_actions
|
||||||
(
|
(
|
||||||
macro_id INTEGER
|
macro_id INTEGER
|
||||||
NOT NULL
|
NOT NULL
|
||||||
|
|
17
src/cache.c
17
src/cache.c
|
@ -1,6 +1,7 @@
|
||||||
#include <cache.h>
|
#include <cache.h>
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
#include <sql/cache.h>
|
#include <sql/cache.h>
|
||||||
|
#include <models/macro_action.h>
|
||||||
#include <models/junction_tag.h>
|
#include <models/junction_tag.h>
|
||||||
#include <models/junction_relay_schedule.h>
|
#include <models/junction_relay_schedule.h>
|
||||||
|
|
||||||
|
@ -136,13 +137,18 @@ cache_invalidate_schedule(int schedule_id)
|
||||||
cache_invalidate(key);
|
cache_invalidate(key);
|
||||||
|
|
||||||
int *relay_ids = junction_relay_schedule_get_relay_ids_with_schedule(schedule_id);
|
int *relay_ids = junction_relay_schedule_get_relay_ids_with_schedule(schedule_id);
|
||||||
|
|
||||||
for(int i = 0; relay_ids[i] != 0; ++i)
|
for(int i = 0; relay_ids[i] != 0; ++i)
|
||||||
{
|
{
|
||||||
cache_invalidate_relay(relay_ids[i], -1);
|
cache_invalidate_relay(relay_ids[i], -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(relay_ids);
|
free(relay_ids);
|
||||||
|
|
||||||
|
int *macro_ids = macro_action_get_macro_ids_with_schedule(schedule_id);
|
||||||
|
for(int i = 0; macro_ids[i] != 0; ++i)
|
||||||
|
{
|
||||||
|
cache_invalidate_macro(macro_ids[i]);
|
||||||
|
}
|
||||||
|
free(macro_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -181,6 +187,13 @@ cache_invalidate_relay(int relay_id, int status_relay)
|
||||||
{
|
{
|
||||||
cache_invalidate_controller(controller_id);
|
cache_invalidate_controller(controller_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int *macro_ids = macro_action_get_macro_ids_with_relay(relay_id);
|
||||||
|
for(int i = 0; macro_ids[i] != 0; ++i)
|
||||||
|
{
|
||||||
|
cache_invalidate_macro(macro_ids[i]);
|
||||||
|
}
|
||||||
|
free(macro_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -90,13 +90,17 @@ command_schedule_update(schedule_t *schedule)
|
||||||
LOGGER_ERR("couldn't find controller for relay %d\n", relays[i]->id);
|
LOGGER_ERR("couldn't find controller for relay %d\n", relays[i]->id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
controller_free(controller);
|
|
||||||
|
|
||||||
LOGGER_DEBUG("sending command to controller %s\n", controller->name);
|
LOGGER_DEBUG("sending command to controller %s\n", controller->name);
|
||||||
|
|
||||||
result |= command_send(controller, payload, payload_size);
|
result |= command_send(controller, payload, payload_size);
|
||||||
|
|
||||||
|
controller_free(controller);
|
||||||
}
|
}
|
||||||
relay_free_list(relays);
|
relay_free_list(relays);
|
||||||
|
|
||||||
|
free(payload);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include <sql/migration_1.h>
|
#include <sql/migration_1.h>
|
||||||
|
|
||||||
sqlite3 *global_database;
|
sqlite3 *global_database;
|
||||||
static int in_transaction;
|
static database_transaction_lock *transaction_lock;
|
||||||
|
|
||||||
void
|
void
|
||||||
database_init()
|
database_init()
|
||||||
|
@ -24,7 +24,7 @@ database_init()
|
||||||
database_migrate();
|
database_migrate();
|
||||||
|
|
||||||
sqlite3_exec(global_database, "PRAGMA foreign_keys = ON", 0, 0, 0);
|
sqlite3_exec(global_database, "PRAGMA foreign_keys = ON", 0, 0, 0);
|
||||||
in_transaction = 0;
|
transaction_lock = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -91,33 +91,37 @@ database_migrate()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
database_transaction_begin()
|
database_transaction_begin(database_transaction_lock *lock)
|
||||||
{
|
{
|
||||||
if(!in_transaction)
|
if(transaction_lock == NULL)
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("beginning transaction\n");
|
LOGGER_DEBUG("beginning transaction\n");
|
||||||
sqlite3_exec(global_database, "BEGIN TRANSACTION;", NULL, NULL, NULL);
|
sqlite3_exec(global_database, "BEGIN TRANSACTION;", NULL, NULL, NULL);
|
||||||
in_transaction = 1;
|
transaction_lock = lock;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
database_transaction_commit()
|
database_transaction_commit(database_transaction_lock *lock)
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("commiting transaction\n");
|
if(transaction_lock == lock)
|
||||||
sqlite3_exec(global_database, "COMMIT TRANSACTION;", NULL, NULL, NULL);
|
{
|
||||||
in_transaction = 0;
|
LOGGER_DEBUG("commiting transaction\n");
|
||||||
|
sqlite3_exec(global_database, "COMMIT TRANSACTION;", NULL, NULL, NULL);
|
||||||
|
transaction_lock = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
database_transaction_rollback()
|
database_transaction_rollback(database_transaction_lock *lock)
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("rolling back transaction\n");
|
if(transaction_lock == lock)
|
||||||
sqlite3_exec(global_database, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
|
{
|
||||||
in_transaction = 0;
|
LOGGER_DEBUG("rolling back transaction\n");
|
||||||
|
sqlite3_exec(global_database, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
|
||||||
|
transaction_lock = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -53,7 +53,7 @@ endpoint_response_text(endpoint_response_t *response, int status_code, const cha
|
||||||
|
|
||||||
response->status_code = status_code;
|
response->status_code = status_code;
|
||||||
response->content_type = "text/plain";
|
response->content_type = "text/plain";
|
||||||
if(content_length >= 0)
|
if(content_length)
|
||||||
{
|
{
|
||||||
response->content_length = content_length;
|
response->content_length = content_length;
|
||||||
response->alloced_content = false;
|
response->alloced_content = false;
|
||||||
|
|
|
@ -193,7 +193,8 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("deleted controller %s\n", args[0].value.v_str);
|
LOGGER_DEBUG("deleted controller %s\n", args[0].value.v_str);
|
||||||
endpoint_response_text(response, 200, "", 0);
|
static const char content[] = "delete controller";
|
||||||
|
endpoint_response_text(response, 200, content, STRLEN(content));
|
||||||
}
|
}
|
||||||
controller_free(controller);
|
controller_free(controller);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -36,7 +36,10 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int);
|
int controller_id = controller->id;
|
||||||
|
controller_free(controller);
|
||||||
|
|
||||||
|
relay_t* relay = relay_get_for_controller(controller_id, args[1].value.v_int);
|
||||||
|
|
||||||
if(!relay)
|
if(!relay)
|
||||||
{
|
{
|
||||||
|
@ -53,7 +56,6 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess
|
||||||
endpoint_response_json(response, 200, json);
|
endpoint_response_json(response, 200, json);
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
relay_free(relay);
|
relay_free(relay);
|
||||||
controller_free(controller);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -83,7 +85,11 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(args[1].value.v_int > controller->relay_count)
|
int controller_id = controller->id;
|
||||||
|
int controller_relay_count = controller->relay_count;
|
||||||
|
controller_free(controller);
|
||||||
|
|
||||||
|
if(args[1].value.v_int > controller_relay_count)
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("relay num %d is too high for %s\n", args[1].value.v_int, args[0].value.v_str);
|
LOGGER_DEBUG("relay num %d is too high for %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||||
|
|
||||||
|
@ -92,7 +98,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int);
|
relay_t* relay = relay_get_for_controller(controller_id, args[1].value.v_int);
|
||||||
|
|
||||||
if(!relay)
|
if(!relay)
|
||||||
{
|
{
|
||||||
|
@ -102,7 +108,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
relay->number = args[1].value.v_int;
|
relay->number = args[1].value.v_int;
|
||||||
snprintf(relay->name, MAX_NAME_LENGTH, "Relay %d", relay->number);
|
snprintf(relay->name, MAX_NAME_LENGTH, "Relay %d", relay->number);
|
||||||
relay->name[MAX_NAME_LENGTH] = '\0';
|
relay->name[MAX_NAME_LENGTH] = '\0';
|
||||||
relay->controller_id = controller->id;
|
relay->controller_id = controller_id;
|
||||||
|
|
||||||
uuid_t tmp_uuid;
|
uuid_t tmp_uuid;
|
||||||
memset(tmp_uuid, 0, sizeof(uuid_t));
|
memset(tmp_uuid, 0, sizeof(uuid_t));
|
||||||
|
@ -117,7 +123,6 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
|
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
|
||||||
}
|
}
|
||||||
|
|
||||||
controller_free(controller);
|
|
||||||
LOGGER_DEBUG("overwriting relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
LOGGER_DEBUG("overwriting relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||||
|
|
||||||
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||||
|
@ -202,16 +207,14 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int opened_transaction = database_transaction_begin();
|
database_transaction_lock lock;
|
||||||
|
database_transaction_begin(&lock);
|
||||||
|
|
||||||
if(relay_save(relay))
|
if(relay_save(relay))
|
||||||
{
|
{
|
||||||
LOGGER_ERR("failed to save relay\n");
|
LOGGER_ERR("failed to save relay\n");
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_rollback(&lock);
|
||||||
{
|
|
||||||
database_transaction_rollback();
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
@ -237,10 +240,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("invalid tag in tags\n");
|
LOGGER_DEBUG("invalid tag in tags\n");
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_rollback(&lock);
|
||||||
{
|
|
||||||
database_transaction_rollback();
|
|
||||||
}
|
|
||||||
|
|
||||||
relay_free(relay);
|
relay_free(relay);
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
@ -259,13 +259,12 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
}
|
}
|
||||||
tag_ids[i++] = tag_id;
|
tag_ids[i++] = tag_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
junction_tag_insert_list(tag_ids, relay->id, 0, json_tags_count);
|
junction_tag_insert_list(tag_ids, relay->id, 0, json_tags_count);
|
||||||
|
free(tag_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_commit(&lock);
|
||||||
{
|
|
||||||
database_transaction_commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
json = relay_to_json(relay, 0);
|
json = relay_to_json(relay, 0);
|
||||||
|
|
|
@ -36,7 +36,10 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int);
|
int controller_id = controller->id;
|
||||||
|
controller_free(controller);
|
||||||
|
|
||||||
|
relay_t* relay = relay_get_for_controller(controller_id, args[1].value.v_int);
|
||||||
|
|
||||||
if(!relay)
|
if(!relay)
|
||||||
{
|
{
|
||||||
|
@ -64,7 +67,7 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht
|
||||||
LOGGER_DEBUG("commanding pulse to relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
LOGGER_DEBUG("commanding pulse to relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||||
command_relay_pulse(relay, duration);
|
command_relay_pulse(relay, duration);
|
||||||
|
|
||||||
endpoint_response_text(response, 200, "", 0);
|
static const char content[] = "sent pulse";
|
||||||
|
endpoint_response_text(response, 200, content, STRLEN(content));
|
||||||
relay_free(relay);
|
relay_free(relay);
|
||||||
controller_free(controller);
|
|
||||||
}
|
}
|
||||||
|
|
257
src/endpoints/api_v1_macros.c
Normal file
257
src/endpoints/api_v1_macros.c
Normal file
|
@ -0,0 +1,257 @@
|
||||||
|
#include <cJSON.h>
|
||||||
|
#include <macros.h>
|
||||||
|
#include <constants.h>
|
||||||
|
#include <database.h>
|
||||||
|
#include <endpoints/api_v1_macros.h>
|
||||||
|
#include <logger.h>
|
||||||
|
#include <models/macro.h>
|
||||||
|
#include <models/macro_action.h>
|
||||||
|
#include <models/schedule.h>
|
||||||
|
#include <models/relay.h>
|
||||||
|
#include <models/controller.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
api_v1_macros_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||||
|
{
|
||||||
|
(void)args;
|
||||||
|
(void)hm;
|
||||||
|
(void)nc;
|
||||||
|
|
||||||
|
macro_t** all_macros = macro_get_all();
|
||||||
|
|
||||||
|
cJSON *json = cJSON_CreateArray();
|
||||||
|
|
||||||
|
for(int i = 0; all_macros[i] != NULL; ++i)
|
||||||
|
{
|
||||||
|
cJSON_AddItemToArray(json, macro_to_json(all_macros[i]));
|
||||||
|
free(all_macros[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
endpoint_response_json(response, 200, json);
|
||||||
|
cJSON_Delete(json);
|
||||||
|
free(all_macros);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||||
|
{
|
||||||
|
(void)args;
|
||||||
|
(void)nc;
|
||||||
|
|
||||||
|
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||||
|
|
||||||
|
if(json == NULL)
|
||||||
|
{
|
||||||
|
static const char content[] = "no valid json was supplied";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
|
||||||
|
if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("no name for macro provided\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "no name for macro provided";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *json_actions = cJSON_GetObjectItemCaseSensitive(json, "actions");
|
||||||
|
if(!cJSON_IsArray(json_actions))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("no actions for macro provided\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "no actions for macro provided";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
database_transaction_lock lock;
|
||||||
|
database_transaction_begin(&lock);
|
||||||
|
|
||||||
|
macro_t *new_macro = malloc(sizeof(macro_t));
|
||||||
|
|
||||||
|
new_macro->id = 0;
|
||||||
|
uuid_generate(new_macro->uid);
|
||||||
|
|
||||||
|
strncpy(new_macro->name, json_name->valuestring, MAX_NAME_LENGTH);
|
||||||
|
new_macro->name[MAX_NAME_LENGTH] = '\0';
|
||||||
|
|
||||||
|
if(macro_save(new_macro))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("macro could not be saved\n");
|
||||||
|
|
||||||
|
database_transaction_rollback(&lock);
|
||||||
|
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
|
||||||
|
static const char content[] = "macro could not be saved";
|
||||||
|
endpoint_response_text(response, 500, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *json_action;
|
||||||
|
cJSON_ArrayForEach(json_action, json_actions)
|
||||||
|
{
|
||||||
|
cJSON *json_action_weekday = cJSON_GetObjectItemCaseSensitive(json_action, "weekday");
|
||||||
|
if(!cJSON_IsNumber(json_action_weekday) || (json_action_weekday->valueint < 0) || (json_action_weekday->valueint > 6))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("one action is missing a weekday\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
|
||||||
|
static const char content[] = "one action is missing a weekday";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *json_action_schedule = cJSON_GetObjectItemCaseSensitive(json_action, "schedule");
|
||||||
|
if(!cJSON_IsObject(json_action_schedule))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action is missing schedule\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
|
||||||
|
static const char content[] = "one action is missing schedule";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cJSON *json_action_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_action_schedule, "id");
|
||||||
|
if(!cJSON_IsString(json_action_schedule_uid) || (json_action_schedule_uid->valuestring == NULL))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action is missing schedule id\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
|
||||||
|
static const char content[] = "one action is missing schedule id";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uuid_t action_schedule_uid;
|
||||||
|
if(schedule_uid_parse(json_action_schedule_uid->valuestring, action_schedule_uid))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action schedule has bad uid\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "action schedule has bad id";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
schedule_t *action_schedule = schedule_get_by_uid(action_schedule_uid);
|
||||||
|
if(action_schedule == NULL)
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action schedule was not found\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "action schedule was not found";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int action_schedule_id = action_schedule->id;
|
||||||
|
schedule_free(action_schedule);
|
||||||
|
|
||||||
|
|
||||||
|
cJSON *json_action_relay = cJSON_GetObjectItemCaseSensitive(json_action, "relay");
|
||||||
|
if(!cJSON_IsObject(json_action_relay))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action is missing relay\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
|
||||||
|
static const char content[] = "one action is missing relay";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cJSON *json_action_relay_number = cJSON_GetObjectItemCaseSensitive(json_action_relay, "number");
|
||||||
|
if(!cJSON_IsNumber(json_action_relay_number))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action is missing relay number\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
|
||||||
|
static const char content[] = "one action is missing relay number";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cJSON *json_action_relay_controller_uid = cJSON_GetObjectItemCaseSensitive(json_action_relay, "controller_id");
|
||||||
|
if(!cJSON_IsString(json_action_relay_controller_uid) || (json_action_relay_controller_uid->valuestring == NULL))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action is missing relay controller id\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
|
||||||
|
static const char content[] = "one action is missing relay controller id";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uuid_t action_controller_uid;
|
||||||
|
if(uuid_parse(json_action_relay_controller_uid->valuestring, action_controller_uid))
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action controller has bad uid\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "action controller has bad id";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
controller_t *action_controller = controller_get_by_uid(action_controller_uid);
|
||||||
|
if(action_controller == NULL)
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action controller was not found\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "action controller was not found";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int controller_id = action_controller->id;
|
||||||
|
int relay_num = json_action_relay_number->valueint;
|
||||||
|
|
||||||
|
controller_free(action_controller);
|
||||||
|
|
||||||
|
relay_t *action_relay = relay_get_for_controller(controller_id, relay_num);
|
||||||
|
if(action_relay == NULL)
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("action relay was not found\n");
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "action relay was not found";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int action_relay_id = action_relay->id;
|
||||||
|
relay_free(action_relay);
|
||||||
|
|
||||||
|
|
||||||
|
macro_action_t *new_action = malloc(sizeof(macro_action_t));
|
||||||
|
new_action->macro_id = new_macro->id;
|
||||||
|
new_action->relay_id = action_relay_id;
|
||||||
|
new_action->schedule_id = action_schedule_id;
|
||||||
|
new_action->weekday = json_action_weekday->valueint;
|
||||||
|
|
||||||
|
macro_action_insert(new_action);
|
||||||
|
free(new_action);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
database_transaction_commit(&lock);
|
||||||
|
|
||||||
|
cJSON_Delete(json);
|
||||||
|
json = macro_to_json(new_macro);
|
||||||
|
|
||||||
|
endpoint_response_json(response, 201, json);
|
||||||
|
|
||||||
|
cJSON_Delete(json);
|
||||||
|
macro_free(new_macro);
|
||||||
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
|
||||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
|
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
|
||||||
if(!cJSON_IsArray(json_periods))
|
if(!cJSON_IsArray(json_periods))
|
||||||
{
|
{
|
||||||
|
|
|
@ -226,7 +226,8 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
endpoint_response_text(response, 200, "", 0);
|
static const char content[] = "deleted schedule";
|
||||||
|
endpoint_response_text(response, 200, content, STRLEN(content));
|
||||||
}
|
}
|
||||||
schedule_free(schedule);
|
schedule_free(schedule);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -91,7 +91,10 @@ api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_arg
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("new tag saved\n");
|
LOGGER_DEBUG("new tag saved\n");
|
||||||
|
|
||||||
endpoint_response_text(response, 201, json_tag->valuestring, 0);
|
char *tag = malloc(sizeof(char) * (strlen(json_tag->valuestring) + 1));
|
||||||
|
strcpy(tag, json_tag->valuestring);
|
||||||
|
|
||||||
|
endpoint_response_text(response, 201, tag, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
|
|
@ -105,6 +105,7 @@ api_v1_tags_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoi
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
endpoint_response_text(response, 200, "", 0);
|
static const char content[] = "deleted tag";
|
||||||
|
endpoint_response_text(response, 200, content, STRLEN(content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,7 +117,8 @@ controller_db_select(sqlite3_stmt *stmt)
|
||||||
int
|
int
|
||||||
controller_save(controller_t *controller)
|
controller_save(controller_t *controller)
|
||||||
{
|
{
|
||||||
int opened_transaction = database_transaction_begin();
|
database_transaction_lock lock;
|
||||||
|
database_transaction_begin(&lock);
|
||||||
|
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
if(controller->id)
|
if(controller->id)
|
||||||
|
@ -142,10 +143,7 @@ controller_save(controller_t *controller)
|
||||||
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_rollback(&lock);
|
||||||
{
|
|
||||||
database_transaction_rollback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -154,10 +152,7 @@ controller_save(controller_t *controller)
|
||||||
controller->id = sqlite3_last_insert_rowid(global_database);
|
controller->id = sqlite3_last_insert_rowid(global_database);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_commit(&lock);
|
||||||
{
|
|
||||||
database_transaction_commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cache_invalidate_controller(controller->id);
|
cache_invalidate_controller(controller->id);
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include <models/junction_macro.h>
|
|
||||||
#include <logger.h>
|
|
||||||
#include <macros.h>
|
|
||||||
#include <database.h>
|
|
||||||
|
|
||||||
int
|
|
||||||
junction_macro_insert(int macro_id, int relay_id, int schedule_id, uint8_t weekday)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
sqlite3_stmt *stmt;
|
|
||||||
|
|
||||||
sqlite3_prepare_v2(global_database, "INSERT INTO junction_macro(macro_id, schedule_id, relay_id, weekday) values (?1, ?2, ?3);", -1, &stmt, NULL);
|
|
||||||
|
|
||||||
sqlite3_bind_int(stmt, 1, macro_id);
|
|
||||||
sqlite3_bind_int(stmt, 2, relay_id);
|
|
||||||
sqlite3_bind_int(stmt, 3, schedule_id);
|
|
||||||
sqlite3_bind_int(stmt, 4, weekday);
|
|
||||||
|
|
||||||
rc = sqlite3_step(stmt);
|
|
||||||
if (rc != SQLITE_DONE)
|
|
||||||
{
|
|
||||||
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -62,16 +62,18 @@ junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids)
|
||||||
sqlite3_bind_int(stmt, i * 3 + 3, relay_id);
|
sqlite3_bind_int(stmt, i * 3 + 3, relay_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(query);
|
||||||
|
|
||||||
rc = sqlite3_step(stmt);
|
rc = sqlite3_step(stmt);
|
||||||
if (rc != SQLITE_DONE)
|
if (rc != SQLITE_DONE)
|
||||||
{
|
{
|
||||||
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
|
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
|
||||||
return false;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -105,6 +105,8 @@ junction_tag_insert_list(int *tag_ids, int relay_id, int schedule_id, int count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(query);
|
||||||
|
|
||||||
rc = sqlite3_step(stmt);
|
rc = sqlite3_step(stmt);
|
||||||
if (rc != SQLITE_DONE)
|
if (rc != SQLITE_DONE)
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
#include <database.h>
|
#include <database.h>
|
||||||
#include <models/macro.h>
|
#include <models/macro.h>
|
||||||
#include <models/junction_macro.h>
|
#include <models/macro_action.h>
|
||||||
#include <models/schedule.h>
|
#include <models/schedule.h>
|
||||||
#include <models/tag.h>
|
#include <models/tag.h>
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ db_update_insert(macro_t *macro, sqlite3_stmt *stmt)
|
||||||
|
|
||||||
return rc != SQLITE_DONE;
|
return rc != SQLITE_DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static macro_t*
|
static macro_t*
|
||||||
macro_db_select_mapper(sqlite3_stmt *stmt)
|
macro_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
{
|
{
|
||||||
|
@ -95,16 +96,17 @@ macro_db_select(sqlite3_stmt *stmt)
|
||||||
int
|
int
|
||||||
macro_save(macro_t *macro)
|
macro_save(macro_t *macro)
|
||||||
{
|
{
|
||||||
int opened_transaction = database_transaction_begin();
|
database_transaction_lock lock;
|
||||||
|
database_transaction_begin(&lock);
|
||||||
|
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
if(macro->id)
|
if(macro->id)
|
||||||
{
|
{
|
||||||
sqlite3_prepare_v2(global_database, "UPDATE macros SET uid = ?2, name = ?3, periods = ?4 WHERE id=?1;", -1, &stmt, NULL);
|
sqlite3_prepare_v2(global_database, "UPDATE macros SET uid = ?2, name = ?3 WHERE id=?1;", -1, &stmt, NULL);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sqlite3_prepare_v2(global_database, "INSERT INTO macros(uid, name, periods) values (?2, ?3, ?4);", -1, &stmt, NULL);
|
sqlite3_prepare_v2(global_database, "INSERT INTO macros(uid, name) values (?2, ?3);", -1, &stmt, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = db_update_insert(macro, stmt);
|
int result = db_update_insert(macro, stmt);
|
||||||
|
@ -120,10 +122,7 @@ macro_save(macro_t *macro)
|
||||||
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_rollback(&lock);
|
||||||
{
|
|
||||||
database_transaction_rollback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -132,10 +131,7 @@ macro_save(macro_t *macro)
|
||||||
macro->id = sqlite3_last_insert_rowid(global_database);
|
macro->id = sqlite3_last_insert_rowid(global_database);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_commit(&lock);
|
||||||
{
|
|
||||||
database_transaction_commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cache_invalidate_macro(macro->id);
|
cache_invalidate_macro(macro->id);
|
||||||
|
@ -236,31 +232,42 @@ macro_to_json(macro_t *macro)
|
||||||
}
|
}
|
||||||
cJSON_AddItemToObject(json, "id", json_id);
|
cJSON_AddItemToObject(json, "id", json_id);
|
||||||
|
|
||||||
cJSON *json_tags = cJSON_CreateArray();
|
cJSON *json_actions = cJSON_CreateArray();
|
||||||
int *tags_ids = NULL; //junction_tag_get_tags_for_macro_id(macro->id);
|
macro_action_t **macro_actions = macro_action_get_for_macro(macro->id);
|
||||||
if(tags_ids != NULL)
|
for(int i = 0; macro_actions[i] != NULL; ++i)
|
||||||
{
|
{
|
||||||
for(int i = 0; tags_ids[i] != 0; ++i)
|
cJSON *json_action = cJSON_CreateObject();
|
||||||
{
|
|
||||||
char *tag = tag_get_tag(tags_ids[i]);
|
|
||||||
if(tag == NULL)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *json_tag = cJSON_CreateString(tag);
|
cJSON *json_action_weekday = cJSON_CreateNumber(macro_actions[i]->weekday);
|
||||||
if (json_tag == NULL)
|
if(json_action_weekday == NULL)
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("failed to add tag from string '%s'\n", tag);
|
LOGGER_DEBUG("failed to create weekday from number %d\n", macro_actions[i]->weekday);
|
||||||
free(tag);
|
continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
cJSON_AddItemToArray(json_tags, json_tag);
|
|
||||||
free(tag);
|
|
||||||
}
|
}
|
||||||
free(tags_ids);
|
|
||||||
|
relay_t *relay = relay_get_by_id(macro_actions[i]->relay_id);
|
||||||
|
if(!relay)
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("failed to get relay\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
schedule_t *schedule = schedule_get_by_id(macro_actions[i]->schedule_id);
|
||||||
|
if(!schedule)
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("failed to get schedule\n");
|
||||||
|
relay_free(relay);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_AddItemToObject(json_action, "weekday", json_action_weekday);
|
||||||
|
cJSON_AddItemToObject(json_action, "relay", relay_to_json(relay, 0));
|
||||||
|
cJSON_AddItemToObject(json_action, "schedule", schedule_to_json(schedule));
|
||||||
|
|
||||||
|
cJSON_AddItemToArray(json_actions, json_action);
|
||||||
}
|
}
|
||||||
cJSON_AddItemToObject(json, "tags", json_tags);
|
cJSON_AddItemToObject(json, "actions", json_actions);
|
||||||
|
|
||||||
|
macro_action_free_list(macro_actions);
|
||||||
|
|
||||||
char *json_str = cJSON_Print(json);
|
char *json_str = cJSON_Print(json);
|
||||||
cache_put_json_macro(macro->id, json_str);
|
cache_put_json_macro(macro->id, json_str);
|
||||||
|
@ -271,31 +278,6 @@ macro_to_json(macro_t *macro)
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_t*
|
|
||||||
macro_get_by_id_or_off(int id)
|
|
||||||
{
|
|
||||||
sqlite3_stmt *stmt;
|
|
||||||
|
|
||||||
sqlite3_prepare_v2(global_database, "SELECT * FROM macros WHERE id = ?1;", -1, &stmt, NULL);
|
|
||||||
sqlite3_bind_int(stmt, 1, id);
|
|
||||||
|
|
||||||
macro_t **sql_result = macro_db_select(stmt);
|
|
||||||
|
|
||||||
macro_t *result = sql_result[0];
|
|
||||||
free(sql_result);
|
|
||||||
|
|
||||||
if(result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
uuid_t tmp_uuid;
|
|
||||||
memset(tmp_uuid, 0, sizeof(uuid_t));
|
|
||||||
memcpy(tmp_uuid, "off", 3);
|
|
||||||
|
|
||||||
return macro_get_by_uid(tmp_uuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_t*
|
macro_t*
|
||||||
macro_get_by_id(int id)
|
macro_get_by_id(int id)
|
||||||
{
|
{
|
||||||
|
@ -313,34 +295,6 @@ macro_get_by_id(int id)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_t*
|
|
||||||
macro_get_by_uid_or_off(uuid_t uid)
|
|
||||||
{
|
|
||||||
char uuid_str[UUID_STR_LEN];
|
|
||||||
uuid_unparse(uid, uuid_str);
|
|
||||||
LOGGER_DEBUG("getting macro [uid=%s] or off from database\n", uuid_str);
|
|
||||||
sqlite3_stmt *stmt;
|
|
||||||
|
|
||||||
sqlite3_prepare_v2(global_database, "SELECT * FROM macros WHERE uid = ?1;", -1, &stmt, NULL);
|
|
||||||
sqlite3_bind_blob(stmt, 1, uid, sizeof(uuid_t), SQLITE_STATIC);
|
|
||||||
|
|
||||||
macro_t **sql_result = macro_db_select(stmt);
|
|
||||||
|
|
||||||
macro_t *result = sql_result[0];
|
|
||||||
free(sql_result);
|
|
||||||
|
|
||||||
if(result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
uuid_t tmp_uuid;
|
|
||||||
memset(tmp_uuid, 0, sizeof(uuid_t));
|
|
||||||
memcpy(tmp_uuid, "off", 3);
|
|
||||||
|
|
||||||
return macro_get_by_uid(tmp_uuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_t*
|
macro_t*
|
||||||
macro_get_by_uid(uuid_t uid)
|
macro_get_by_uid(uuid_t uid)
|
||||||
{
|
{
|
||||||
|
|
139
src/models/macro_action.c
Normal file
139
src/models/macro_action.c
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include <models/macro_action.h>
|
||||||
|
#include <logger.h>
|
||||||
|
#include <cache.h>
|
||||||
|
#include <database.h>
|
||||||
|
|
||||||
|
static macro_action_t*
|
||||||
|
macro_action_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
|
{
|
||||||
|
macro_action_t *new_macro_action = malloc(sizeof(macro_action_t));
|
||||||
|
for(int i = 0; i < sqlite3_column_count(stmt); i++)
|
||||||
|
{
|
||||||
|
const char *name = sqlite3_column_name(stmt, i);
|
||||||
|
switch(name[0])
|
||||||
|
{
|
||||||
|
case 'm': // macro_id
|
||||||
|
new_macro_action->macro_id = sqlite3_column_int(stmt, i);
|
||||||
|
break;
|
||||||
|
case 'r': // relay_id
|
||||||
|
new_macro_action->relay_id = sqlite3_column_int(stmt, i);
|
||||||
|
break;
|
||||||
|
case 's': // schedule_id
|
||||||
|
new_macro_action->schedule_id = sqlite3_column_int(stmt, i);
|
||||||
|
break;
|
||||||
|
case 'w': // weekday
|
||||||
|
new_macro_action->weekday = (uint8_t)sqlite3_column_int(stmt, i);
|
||||||
|
break;
|
||||||
|
default: // ignore columns not implemented
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new_macro_action;
|
||||||
|
}
|
||||||
|
|
||||||
|
static macro_action_t**
|
||||||
|
macro_action_db_select(sqlite3_stmt *stmt)
|
||||||
|
{
|
||||||
|
macro_action_t **all_macro_actions = malloc(sizeof(macro_action_t*));
|
||||||
|
|
||||||
|
int row = 0;
|
||||||
|
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
int s;
|
||||||
|
|
||||||
|
s = sqlite3_step(stmt);
|
||||||
|
if (s == SQLITE_ROW)
|
||||||
|
{
|
||||||
|
macro_action_t *new_macro_action = macro_action_db_select_mapper(stmt);
|
||||||
|
row++;
|
||||||
|
|
||||||
|
all_macro_actions = (macro_action_t**)realloc(all_macro_actions, sizeof(macro_action_t*) * (row + 1));
|
||||||
|
all_macro_actions[row - 1] = new_macro_action;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(s == SQLITE_DONE)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOGGER_ERR("error selecting macro_actions from database: %s\n", sqlite3_errstr(s));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
all_macro_actions[row] = NULL;
|
||||||
|
return all_macro_actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
macro_action_insert(macro_action_t *macro_action)
|
||||||
|
{
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(global_database, "INSERT INTO macro_actions(macro_id, relay_id, schedule_id, weekday) values (?1, ?2, ?3, ?4);", -1, &stmt, NULL);
|
||||||
|
|
||||||
|
sqlite3_bind_int(stmt, 1, macro_action->macro_id);
|
||||||
|
sqlite3_bind_int(stmt, 2, macro_action->relay_id);
|
||||||
|
sqlite3_bind_int(stmt, 3, macro_action->schedule_id);
|
||||||
|
sqlite3_bind_int(stmt, 4, macro_action->weekday);
|
||||||
|
|
||||||
|
int rc = sqlite3_step(stmt);
|
||||||
|
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
|
cache_invalidate_macro(macro_action->macro_id);
|
||||||
|
|
||||||
|
return rc != SQLITE_DONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_action_t**
|
||||||
|
macro_action_get_for_macro(int macro_id)
|
||||||
|
{
|
||||||
|
LOGGER_DEBUG("getting macro_actions [macro_id=%d] from database\n", macro_id);
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(global_database, "SELECT * FROM macro_actions WHERE macro_id=?", -1, &stmt, NULL);
|
||||||
|
sqlite3_bind_int(stmt, 1, macro_id);
|
||||||
|
|
||||||
|
return macro_action_db_select(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
macro_action_free_list(macro_action_t **macro_actions)
|
||||||
|
{
|
||||||
|
for(int i = 0; macro_actions[i] != NULL; ++i)
|
||||||
|
{
|
||||||
|
free(macro_actions[i]);
|
||||||
|
}
|
||||||
|
free(macro_actions);
|
||||||
|
}
|
||||||
|
|
||||||
|
int*
|
||||||
|
macro_action_get_macro_ids_with_schedule(int schedule_id)
|
||||||
|
{
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(global_database, "SELECT macro_id FROM macro_actions WHERE schedule_id=?1;", -1, &stmt, NULL);
|
||||||
|
sqlite3_bind_int(stmt, 1, schedule_id);
|
||||||
|
|
||||||
|
return database_helper_get_ids(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
int*
|
||||||
|
macro_action_get_macro_ids_with_relay(int relay_id)
|
||||||
|
{
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(global_database, "SELECT macro_id FROM macro_actions WHERE relay_id=?1;", -1, &stmt, NULL);
|
||||||
|
sqlite3_bind_int(stmt, 1, relay_id);
|
||||||
|
|
||||||
|
return database_helper_get_ids(stmt);
|
||||||
|
}
|
|
@ -115,7 +115,8 @@ relay_db_select(sqlite3_stmt *stmt)
|
||||||
int
|
int
|
||||||
relay_save(relay_t *relay)
|
relay_save(relay_t *relay)
|
||||||
{
|
{
|
||||||
int opened_transaction = database_transaction_begin();
|
database_transaction_lock lock;
|
||||||
|
database_transaction_begin(&lock);
|
||||||
|
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
if(relay->id)
|
if(relay->id)
|
||||||
|
@ -140,10 +141,7 @@ relay_save(relay_t *relay)
|
||||||
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_rollback(&lock);
|
||||||
{
|
|
||||||
database_transaction_rollback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -163,10 +161,7 @@ relay_save(relay_t *relay)
|
||||||
}
|
}
|
||||||
junction_relay_schedule_insert_weekdays(relay->id, schedule_ids);
|
junction_relay_schedule_insert_weekdays(relay->id, schedule_ids);
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_commit(&lock);
|
||||||
{
|
|
||||||
database_transaction_commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cache_invalidate_relay(relay->id, -1);
|
cache_invalidate_relay(relay->id, -1);
|
||||||
|
|
|
@ -112,7 +112,8 @@ schedule_db_select(sqlite3_stmt *stmt)
|
||||||
int
|
int
|
||||||
schedule_save(schedule_t *schedule)
|
schedule_save(schedule_t *schedule)
|
||||||
{
|
{
|
||||||
int opened_transaction = database_transaction_begin();
|
database_transaction_lock lock;
|
||||||
|
database_transaction_begin(&lock);
|
||||||
|
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
if(schedule->id)
|
if(schedule->id)
|
||||||
|
@ -137,10 +138,7 @@ schedule_save(schedule_t *schedule)
|
||||||
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_rollback(&lock);
|
||||||
{
|
|
||||||
database_transaction_rollback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -149,10 +147,7 @@ schedule_save(schedule_t *schedule)
|
||||||
schedule->id = sqlite3_last_insert_rowid(global_database);
|
schedule->id = sqlite3_last_insert_rowid(global_database);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(opened_transaction)
|
database_transaction_commit(&lock);
|
||||||
{
|
|
||||||
database_transaction_commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cache_invalidate_schedule(schedule->id);
|
cache_invalidate_schedule(schedule->id);
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <endpoints/api_v1_controllers.h>
|
#include <endpoints/api_v1_controllers.h>
|
||||||
#include <endpoints/api_v1_relays.h>
|
#include <endpoints/api_v1_relays.h>
|
||||||
#include <endpoints/api_v1_tags.h>
|
#include <endpoints/api_v1_tags.h>
|
||||||
|
#include <endpoints/api_v1_macros.h>
|
||||||
#include <endpoints/api_v1_ws.h>
|
#include <endpoints/api_v1_ws.h>
|
||||||
|
|
||||||
static endpoint_t endpoints[ROUTER_ENDPOINTS_MAX_COUNT];
|
static endpoint_t endpoints[ROUTER_ENDPOINTS_MAX_COUNT];
|
||||||
|
@ -90,6 +91,11 @@ router_init()
|
||||||
router_register_endpoint("/api/v1/tags/{str}", HTTP_METHOD_GET, api_v1_tags_STR_GET);
|
router_register_endpoint("/api/v1/tags/{str}", HTTP_METHOD_GET, api_v1_tags_STR_GET);
|
||||||
router_register_endpoint("/api/v1/tags/{str}", HTTP_METHOD_DELETE, api_v1_tags_STR_DELETE);
|
router_register_endpoint("/api/v1/tags/{str}", HTTP_METHOD_DELETE, api_v1_tags_STR_DELETE);
|
||||||
|
|
||||||
|
router_register_endpoint("/api/v1/macros/", HTTP_METHOD_GET, api_v1_macros_GET);
|
||||||
|
router_register_endpoint("/api/v1/macros/", HTTP_METHOD_POST, api_v1_macros_POST);
|
||||||
|
//router_register_endpoint("/api/v1/macros/{str}", HTTP_METHOD_GET, api_v1_macros_STR_GET);
|
||||||
|
//router_register_endpoint("/api/v1/macros/{str}", HTTP_METHOD_DELETE, api_v1_macros_STR_DELETE);
|
||||||
|
|
||||||
router_register_endpoint("/api/v1/ws/relays", HTTP_METHOD_WEBSOCKET, api_v1_ws_relays);
|
router_register_endpoint("/api/v1/ws/relays", HTTP_METHOD_WEBSOCKET, api_v1_ws_relays);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ source_dir=$PWD
|
||||||
working_dir=$PWD/testing_latest
|
working_dir=$PWD/testing_latest
|
||||||
working_bak=$PWD/testing_bak
|
working_bak=$PWD/testing_bak
|
||||||
|
|
||||||
alias valgrind_emgauwa="valgrind $2 --log-file=$working_dir/valgrind.log"
|
alias valgrind_emgauwa="valgrind -s $2 --log-file=$working_dir/valgrind.log"
|
||||||
|
|
||||||
|
|
||||||
rm -rf $working_bak
|
rm -rf $working_bak
|
||||||
|
|
Loading…
Reference in a new issue