2020-04-13 22:50:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
|
|
|
|
#include <models/relay.h>
|
|
|
|
#include <macros.h>
|
|
|
|
|
|
|
|
static int
|
2020-04-24 22:48:59 +00:00
|
|
|
relay_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_relay_e key_relay, uint8_t relay_num, MDB_val value)
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
size_t key_size = sizeof(db_key_relay_e) + sizeof(uint8_t);
|
2020-04-24 22:48:59 +00:00
|
|
|
uint8_t *key_data = malloc(key_size);
|
|
|
|
uint8_t *key_data_writer = key_data;
|
|
|
|
memmove(key_data_writer, &relay_num, sizeof(uint8_t));
|
|
|
|
key_data_writer += sizeof(uint8_t);
|
|
|
|
memmove(key_data_writer, &key_relay, sizeof(db_key_relay_e));
|
2020-04-13 22:50:55 +00:00
|
|
|
|
|
|
|
MDB_val key;
|
|
|
|
key.mv_size = key_size;
|
|
|
|
key.mv_data = key_data;
|
|
|
|
|
|
|
|
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_ERROR("mdb_put error %s\n", mdb_strerror(err));
|
2020-04-13 22:50:55 +00:00
|
|
|
mdb_txn_abort(mdb_txn);
|
|
|
|
free(key_data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
free(key_data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
relay_save(relay_t *relay, MDB_env *mdb_env)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_TRACE("saving relay %d @ %p\n", relay->number, relay);
|
2020-04-13 22:50:55 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
2020-04-13 22:50:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((err = mdb_dbi_open(mdb_txn, "relays", MDB_CREATE, &mdb_dbi)) != 0)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_ERROR("mdb_dbi_open error %s\n", mdb_strerror(err));
|
2020-04-13 22:50:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
value.mv_size = sizeof(char) * (strlen(relay->name) + 1);
|
|
|
|
value.mv_data = relay->name;
|
|
|
|
if(relay_save_single(mdb_txn, mdb_dbi, DB_KEY_RELAY_NAME, relay->number, value))
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_ERROR("failed to save name\n");
|
2020-04-13 22:50:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:48:59 +00:00
|
|
|
mdb_txn_commit(mdb_txn);
|
2020-04-13 22:50:55 +00:00
|
|
|
|
2020-04-24 22:48:59 +00:00
|
|
|
for(int i = 0; i < 7; ++i)
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
2020-04-24 22:48:59 +00:00
|
|
|
schedule_save(relay->schedules[i], relay->number, mdb_env);
|
2020-04-13 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|