100 lines
3 KiB
C
100 lines
3 KiB
C
#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
|
|
relay_load_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_relay_e key_relay, uint8_t num, MDB_val *value)
|
|
{
|
|
int err;
|
|
|
|
size_t key_size = sizeof(db_key_relay_e) + sizeof(uint8_t);
|
|
void *key_data = malloc(key_size);
|
|
memmove((uint8_t*)key_data, &key_relay, sizeof(db_key_relay_e));
|
|
memmove((uint8_t*)key_data + sizeof(db_key_relay_e), &num, sizeof(uint8_t));
|
|
|
|
MDB_val key;
|
|
key.mv_size = key_size;
|
|
key.mv_data = key_data;
|
|
|
|
if((err = mdb_get(mdb_txn, mdb_dbi, &key, value)) != 0)
|
|
{
|
|
LOG_ERROR("mdb_get error %s", mdb_strerror(err));
|
|
mdb_txn_abort(mdb_txn);
|
|
free(key_data);
|
|
return 1;
|
|
}
|
|
free(key_data);
|
|
return 0;
|
|
}
|
|
|
|
relay_t*
|
|
relay_load(MDB_env *mdb_env, uint8_t num)
|
|
{
|
|
int err;
|
|
MDB_txn *mdb_txn;
|
|
MDB_dbi mdb_dbi;
|
|
|
|
relay_t *new_relay;
|
|
|
|
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
|
|
{
|
|
LOG_ERROR("mdb_txn_begin error %s", mdb_strerror(err));
|
|
return relay_create(num);
|
|
}
|
|
|
|
if((err = mdb_dbi_open(mdb_txn, "relays", 0, &mdb_dbi)) != 0)
|
|
{
|
|
switch(err)
|
|
{
|
|
case MDB_NOTFOUND:
|
|
LOG_INFO("no relay for num %d found in db. returning new one (no relays db)", num);
|
|
mdb_txn_abort(mdb_txn);
|
|
return relay_create(num);
|
|
default:
|
|
LOG_ERROR("mdb_txn_begin error %s", mdb_strerror(err));
|
|
return relay_create(num);
|
|
}
|
|
}
|
|
|
|
new_relay = malloc(sizeof(relay_t));
|
|
new_relay->number = num;
|
|
|
|
MDB_val value;
|
|
|
|
if((err = relay_load_single(mdb_txn, mdb_dbi, DB_KEY_RELAY_NAME, num, &value)) != 0)
|
|
{
|
|
LOG_INFO("no relay for num %d found in db. returning new one", num);
|
|
mdb_txn_abort(mdb_txn); // transaction is read only
|
|
return relay_create(num);
|
|
}
|
|
strncpy(new_relay->name, (char*)value.mv_data, MAX_NAME_LENGTH);
|
|
new_relay->name[MAX_NAME_LENGTH] = '\0';
|
|
|
|
if((err = relay_load_single(mdb_txn, mdb_dbi, DB_KEY_RELAY_SCHEDULE_ID, num, &value)) != 0)
|
|
{
|
|
LOG_INFO("no relay for num %d found in db. returning new one", num);
|
|
mdb_txn_abort(mdb_txn); // transaction is read only
|
|
return relay_create(num);
|
|
}
|
|
uuid_t *schedule_id = (uuid_t*)value.mv_data;
|
|
|
|
if((err = relay_load_single(mdb_txn, mdb_dbi, DB_KEY_RELAY_SCHEDULE_PERIODS, num, &value)) != 0)
|
|
{
|
|
LOG_INFO("no relay for num %d found in db. returning new one", num);
|
|
mdb_txn_abort(mdb_txn); // transaction is read only
|
|
return relay_create(num);
|
|
}
|
|
uint16_t schedule_periods_length = ((uint16_t*)value.mv_data)[0];
|
|
uint16_t *schedule_periods = ((uint16_t*)value.mv_data) + 1;
|
|
|
|
new_relay->schedule = schedule_create(*schedule_id, schedule_periods_length, schedule_periods);
|
|
|
|
mdb_txn_abort(mdb_txn); // transaction is read only
|
|
|
|
return new_relay;
|
|
}
|