fix: all the bugs with the new database and commanding
This commit is contained in:
parent
dd9e533f22
commit
0eef646fe0
5 changed files with 45 additions and 17 deletions
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(core
|
project(core
|
||||||
VERSION 0.3.0
|
VERSION 0.3.1
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(core src/main.c)
|
add_executable(core src/main.c)
|
||||||
|
|
|
@ -29,6 +29,9 @@ relay_save();
|
||||||
void
|
void
|
||||||
relay_reload_active_schedule(relay_t *relay);
|
relay_reload_active_schedule(relay_t *relay);
|
||||||
|
|
||||||
|
void
|
||||||
|
relay_reload_schedules(relay_t *relay);
|
||||||
|
|
||||||
cJSON*
|
cJSON*
|
||||||
relay_to_json(relay_t *relay);
|
relay_to_json(relay_t *relay);
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,8 @@ command_schedule_update(schedule_t *schedule)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
controller_free(controller);
|
controller_free(controller);
|
||||||
|
|
||||||
|
LOGGER_DEBUG("sending command to controller %s\n", controller->name);
|
||||||
result |= command_send(controller, payload, payload_size);
|
result |= command_send(controller, payload, payload_size);
|
||||||
}
|
}
|
||||||
relay_free_list(relays);
|
relay_free_list(relays);
|
||||||
|
@ -117,7 +119,7 @@ command_relay_schedules_set(relay_t *relay)
|
||||||
mpack_start_map(&writer, 3);
|
mpack_start_map(&writer, 3);
|
||||||
|
|
||||||
mpack_write_uint(&writer, COMMAND_MAPPING_CODE);
|
mpack_write_uint(&writer, COMMAND_MAPPING_CODE);
|
||||||
mpack_write_u16(&writer, COMMAND_CODE_RELAY_SCHEDULES_GET);
|
mpack_write_u16(&writer, COMMAND_CODE_RELAY_SCHEDULES_SET);
|
||||||
|
|
||||||
mpack_write_uint(&writer, COMMAND_MAPPING_RELAY_NUM);
|
mpack_write_uint(&writer, COMMAND_MAPPING_RELAY_NUM);
|
||||||
mpack_write_u8(&writer, relay->number);
|
mpack_write_u8(&writer, relay->number);
|
||||||
|
@ -161,6 +163,7 @@ command_relay_schedules_set(relay_t *relay)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGGER_DEBUG("sending command to controller %s\n", controller->name);
|
||||||
int result = command_send(controller, payload, payload_size);
|
int result = command_send(controller, payload, payload_size);
|
||||||
|
|
||||||
controller_free(controller);
|
controller_free(controller);
|
||||||
|
@ -194,6 +197,7 @@ command_controller_name_set(controller_t *controller)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGGER_DEBUG("sending command to controller %s\n", controller->name);
|
||||||
int result = command_send(controller, payload, payload_size);
|
int result = command_send(controller, payload, payload_size);
|
||||||
|
|
||||||
free(payload);
|
free(payload);
|
||||||
|
@ -260,11 +264,12 @@ command_relay_pulse(relay_t *relay, uint8_t duration)
|
||||||
// finish writing
|
// finish writing
|
||||||
if (mpack_writer_destroy(&writer) != mpack_ok)
|
if (mpack_writer_destroy(&writer) != mpack_ok)
|
||||||
{
|
{
|
||||||
LOGGER_ERR("an error occurred encoding the data");
|
LOGGER_ERR("an error occurred encoding the data\n");
|
||||||
controller_free(controller);
|
controller_free(controller);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGGER_DEBUG("sending command to controller %s\n", controller->name);
|
||||||
int result = command_send(controller, payload, payload_size);
|
int result = command_send(controller, payload, payload_size);
|
||||||
|
|
||||||
controller_free(controller);
|
controller_free(controller);
|
||||||
|
|
|
@ -270,7 +270,6 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
json = relay_to_json(relay);
|
json = relay_to_json(relay);
|
||||||
|
|
||||||
LOGGER_DEBUG("commanding schedules");
|
|
||||||
command_relay_schedules_set(relay);
|
command_relay_schedules_set(relay);
|
||||||
|
|
||||||
endpoint_response_json(response, 200, json);
|
endpoint_response_json(response, 200, json);
|
||||||
|
|
|
@ -66,19 +66,8 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
schedule_t **schedules = schedule_get_relay_weekdays(new_relay->id);
|
memset(new_relay->schedules, 0, sizeof(schedule_t*) * 7);
|
||||||
for(int i = 0; i < 7; ++i)
|
relay_reload_schedules(new_relay);
|
||||||
{
|
|
||||||
if(schedules[i] == NULL)
|
|
||||||
{
|
|
||||||
LOGGER_ERR("got only %d/7 schedules for relay_id %d\n", i, new_relay->id);
|
|
||||||
relay_free(new_relay);
|
|
||||||
free(schedules);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
new_relay->schedules[i] = schedules[i];
|
|
||||||
}
|
|
||||||
free(schedules); // don't free list, because contents are kept in relay->schedules
|
|
||||||
|
|
||||||
relay_reload_active_schedule(new_relay);
|
relay_reload_active_schedule(new_relay);
|
||||||
|
|
||||||
|
@ -194,6 +183,38 @@ relay_reload_active_schedule(relay_t *relay)
|
||||||
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
|
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
relay_reload_schedules(relay_t *relay)
|
||||||
|
{
|
||||||
|
schedule_t **schedules = schedule_get_relay_weekdays(relay->id);
|
||||||
|
|
||||||
|
uuid_t off_id;
|
||||||
|
memset(off_id, 0, sizeof(uuid_t));
|
||||||
|
memcpy(off_id, "off", 3);
|
||||||
|
|
||||||
|
int fill_with_off = 0;
|
||||||
|
for(int i = 0; i < 7; ++i)
|
||||||
|
{
|
||||||
|
if(schedules[i] == NULL || fill_with_off)
|
||||||
|
{
|
||||||
|
LOGGER_WARNING("got only %d/7 schedules for relay_id %d\n", i, relay->id);
|
||||||
|
relay->schedules[i] = schedule_get_by_uid(off_id);
|
||||||
|
|
||||||
|
fill_with_off = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(relay->schedules[i])
|
||||||
|
{
|
||||||
|
schedule_free(relay->schedules[i]);
|
||||||
|
}
|
||||||
|
relay->schedules[i] = schedules[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(schedules); // don't free list, because contents are kept in relay->schedules
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
relay_free(relay_t *relay)
|
relay_free(relay_t *relay)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue