controller-legacy/models/controller_save.c
2020-04-24 15:08:26 +02:00

90 lines
2.3 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>
#include <models/controller.h>
#include <macros.h>
int
controller_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_controller_e key_controller, MDB_val value)
{
int err;
MDB_val key;
key.mv_size = sizeof(db_key_controller_e);
key.mv_data = &key_controller;
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
{
LOG_ERROR("mdb_put error %s\n", mdb_strerror(err));
mdb_txn_abort(mdb_txn);
return 1;
}
return 0;
}
int
controller_save(controller_t *controller, MDB_env *mdb_env)
{
int err;
MDB_txn *mdb_txn;
MDB_dbi mdb_dbi;
MDB_val value;
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
{
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
exit(1);
}
if((err = mdb_dbi_open(mdb_txn, "controller", MDB_CREATE, &mdb_dbi)) != 0)
{
LOG_ERROR("mdb_dbi_open error %s\n", mdb_strerror(err));
exit(1);
}
value.mv_size = sizeof(uuid_t);
value.mv_data = controller->id;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_ID, value))
{
LOG_ERROR("failed to save ID\n");
return 1;
}
value.mv_size = sizeof(char) * (strlen(controller->name) + 1);
value.mv_data = controller->name;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_NAME, value))
{
LOG_ERROR("failed to save name\n");
return 1;
}
value.mv_size = sizeof(controller->command_port);
value.mv_data = &controller->command_port;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_COMMAND_PORT, value))
{
LOG_ERROR("failed to save command port\n");
return 1;
}
value.mv_size = sizeof(controller->discovery_port);
value.mv_data = &controller->discovery_port;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_DISCOVERY_PORT, value))
{
LOG_ERROR("failed to save discovery port\n");
return 1;
}
mdb_txn_commit(mdb_txn);
for(uint8_t i = 0; i < controller->relay_count; ++i)
{
LOG_TRACE("saving relays[%d/%d]\n", i, controller->relay_count);
relay_save(controller->relays[i], mdb_env);
}
return 0;
}