add: init relays after discover

This commit is contained in:
Tobias Reisinger 2020-05-11 22:36:47 +02:00
parent 606f2483a5
commit dab106c1f6
3 changed files with 35 additions and 5 deletions

View file

@ -1,5 +1,7 @@
[core]
server-port = 5000
: 4421 for dev-env; 4420 for testing-env; 4419 for prod-env; 4422 for testing
discovery-port = 4421
database = core.sqlite
log-level = debug

View file

@ -243,12 +243,37 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
discovered_controller->port = discovered_command_port;
discovered_controller->active = 1;
// TODO get relays during discovery
relay_t **discovered_relays = malloc(sizeof(relay_t*));
discovered_relays[0] = NULL;
controller_save(discovered_controller);
// TODO get relays during discovery and don't create empty ones
relay_t **discovered_relays = malloc(sizeof(relay_t*) * (discovered_controller->relay_count + 1));
for(int i = 0; i < discovered_controller->relay_count; ++i)
{
relay_t *new_relay = malloc(sizeof(relay_t));
new_relay->id = 0;
sprintf(new_relay->name, "Relay %d", i + 1);
new_relay->number = i;
new_relay->controller_id = discovered_controller->id;
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
for(int i = 0; i < 7; ++i)
{
new_relay->schedules[i] = schedule_get_by_uid(tmp_uuid);
}
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
new_relay->active_schedule = new_relay->schedules[helper_get_weekday(time_struct)];
relay_save(new_relay);
discovered_relays[i] = new_relay;
}
discovered_relays[discovered_controller->relay_count] = NULL;
discovered_controller->relays = discovered_relays;
controller_save(discovered_controller);
controller_free(discovered_controller);
}
mpack_tree_destroy(&tree);

View file

@ -139,7 +139,10 @@ controller_save(controller_t *controller)
}
else
{
controller->id = sqlite3_last_insert_rowid(global_database);
if(!controller->id)
{
controller->id = sqlite3_last_insert_rowid(global_database);
}
}
return result;
}