2019-07-20 21:33:17 +00:00
# include <cstdio>
# include <cstring>
# include <trantor/utils/Logger.h>
# include <helpers.h>
# include "relay_dbo.h"
# include "globals.h"
2019-07-22 20:06:13 +00:00
# include "controller_dbo.h"
2019-07-29 20:02:38 +00:00
# include "schedule_dbo.h"
2019-07-20 21:33:17 +00:00
static bool relay_db_update_insert ( relay_dbo * relay , sqlite3_stmt * stmt )
{
int rc ;
2020-04-23 15:00:12 +00:00
uint32_t schedules_ids [ 7 ] ;
for ( int i = 0 ; i < 7 ; + + i )
{
schedules_ids [ i ] = relay - > schedules [ i ] - > id ;
}
2019-07-20 21:33:17 +00:00
sqlite3_bind_int ( stmt , 1 , relay - > id ) ;
sqlite3_bind_int ( stmt , 2 , relay - > number ) ;
sqlite3_bind_text ( stmt , 3 , relay - > name , - 1 , SQLITE_STATIC ) ;
2020-04-23 15:00:12 +00:00
sqlite3_bind_blob ( stmt , 4 , schedules_ids , sizeof ( uint32_t ) * 7 , SQLITE_STATIC ) ;
2020-02-23 19:06:14 +00:00
sqlite3_bind_blob ( stmt , 5 , relay - > controller_id , sizeof ( uuid_t ) , SQLITE_STATIC ) ;
2019-09-24 21:33:35 +00:00
sqlite3_bind_text ( stmt , 6 , relay - > tag , - 1 , SQLITE_STATIC ) ;
2019-07-20 21:33:17 +00:00
rc = sqlite3_step ( stmt ) ;
if ( rc ! = SQLITE_DONE )
{
printf ( " ERROR inserting data: %s \n " , sqlite3_errmsg ( globals : : db ) ) ;
return false ;
}
sqlite3_finalize ( stmt ) ;
return true ;
}
static relay_dbo *
relay_db_select_mapper ( sqlite3_stmt * stmt )
{
2019-07-21 22:00:07 +00:00
auto * new_relay = new relay_dbo ( ) ;
2020-04-23 15:00:12 +00:00
const char * new_tag ;
const uint32_t * new_relays_ids ;
2019-07-20 21:33:17 +00:00
for ( int i = 0 ; i < sqlite3_column_count ( stmt ) ; i + + )
{
const char * name = sqlite3_column_name ( stmt , i ) ;
switch ( name [ 0 ] )
{
2019-07-29 20:02:38 +00:00
case ' c ' : // controller_id
2020-02-23 19:06:14 +00:00
uuid_copy ( new_relay - > controller_id , ( const unsigned char * ) sqlite3_column_blob ( stmt , i ) ) ;
2019-07-20 21:33:17 +00:00
break ;
case ' i ' :
new_relay - > id = sqlite3_column_int ( stmt , i ) ;
break ;
case ' n ' :
2019-07-20 22:29:05 +00:00
switch ( name [ 1 ] )
2019-07-20 21:33:17 +00:00
{
case ' a ' : // name
strncpy ( new_relay - > name , ( const char * ) sqlite3_column_text ( stmt , i ) , 127 ) ;
break ;
case ' u ' : // number
new_relay - > number = sqlite3_column_int ( stmt , i ) ;
break ;
default :
break ;
}
2020-01-02 20:55:42 +00:00
break ;
2020-04-23 15:00:12 +00:00
case ' s ' : // schedules_ids
new_relays_ids = ( const uint32_t * ) sqlite3_column_blob ( stmt , i ) ;
for ( int i = 0 ; i < 7 ; + + i )
{
new_relay - > schedules [ i ] = schedule_dbo : : get_by_id_or_off ( new_relays_ids [ i ] ) ;
}
break ;
2019-09-24 21:33:35 +00:00
case ' t ' : // tag
2020-01-02 20:55:42 +00:00
new_tag = ( const char * ) sqlite3_column_text ( stmt , i ) ;
new_relay - > tag [ 0 ] = ' \0 ' ;
if ( new_tag )
{
strncpy ( new_relay - > tag , ( const char * ) sqlite3_column_text ( stmt , i ) , 63 ) ;
}
2019-09-24 21:33:35 +00:00
break ;
2019-07-20 21:33:17 +00:00
default : // ignore columns not implemented
break ;
}
}
return new_relay ;
}
static relay_dbo * *
relay_db_select ( sqlite3_stmt * stmt )
{
auto * * all_relays = ( relay_dbo * * ) malloc ( sizeof ( relay_dbo * ) ) ;
int row = 0 ;
while ( true )
{
int s ;
s = sqlite3_step ( stmt ) ;
if ( s = = SQLITE_ROW )
{
relay_dbo * new_relay = relay_db_select_mapper ( stmt ) ;
2019-07-29 20:02:38 +00:00
2019-07-20 21:33:17 +00:00
row + + ;
all_relays = ( relay_dbo * * ) realloc ( all_relays , sizeof ( relay_dbo * ) * ( row + 1 ) ) ;
all_relays [ row - 1 ] = new_relay ;
}
else
{
if ( s = = SQLITE_DONE )
{
break ;
}
else
{
2019-07-20 22:29:05 +00:00
LOG_ERROR < < " Error Selecting relays from database: " < < sqlite3_errstr ( s ) ;
break ;
2019-07-20 21:33:17 +00:00
}
}
}
sqlite3_finalize ( stmt ) ;
all_relays [ row ] = nullptr ;
return all_relays ;
}
bool
relay_dbo : : update ( )
{
sqlite3_stmt * stmt ;
2020-04-23 15:00:12 +00:00
sqlite3_prepare_v2 ( globals : : db , " UPDATE relays set number = ?2, name = ?3, schedules_ids = ?4, controller_id = ?5, tag = ?6 WHERE id = ?1; " , - 1 , & stmt , nullptr ) ;
2019-07-20 21:33:17 +00:00
return relay_db_update_insert ( this , stmt ) ;
}
bool
relay_dbo : : insert ( )
{
sqlite3_stmt * stmt ;
2020-04-23 15:00:12 +00:00
sqlite3_prepare_v2 ( globals : : db , " INSERT INTO relays(number, name, schedules_ids, controller_id, tag) values (?2, ?3, ?4, ?5, ?6); " , - 1 , & stmt , nullptr ) ;
2019-07-20 21:33:17 +00:00
return relay_db_update_insert ( this , stmt ) ;
}
bool
relay_dbo : : remove ( )
{
sqlite3_stmt * stmt ;
int rc ;
sqlite3_prepare_v2 ( globals : : db , " DELETE FROM relays WHERE id=?1; " , - 1 , & stmt , nullptr ) ;
sqlite3_bind_int ( stmt , 1 , this - > id ) ;
rc = sqlite3_step ( stmt ) ;
sqlite3_finalize ( stmt ) ;
return rc = = SQLITE_DONE ;
}
Json : : Value
relay_dbo : : to_json ( )
{
2020-04-23 15:00:12 +00:00
this - > active_schedule = this - > schedules [ helpers : : get_day_of_week ( ) ] ;
2020-02-23 19:06:14 +00:00
char controller_id_str [ 37 ] ;
uuid_unparse ( this - > controller_id , controller_id_str ) ;
2020-04-19 00:44:35 +00:00
char active_schedule_uid_str [ 37 ] ;
schedule_dbo : : unparse_uid ( this - > active_schedule - > uid , active_schedule_uid_str ) ;
2020-04-23 15:00:12 +00:00
Json : : Value schedules_json ( Json : : arrayValue ) ;
for ( int i = 0 ; i < 7 ; + + i )
{
schedules_json . append ( this - > schedules [ i ] - > to_json ( ) ) ;
}
2019-07-20 21:33:17 +00:00
Json : : Value relay_json ;
relay_json [ " name " ] = this - > name ;
2019-07-20 22:29:05 +00:00
relay_json [ " number " ] = this - > number ;
2020-04-19 00:44:35 +00:00
relay_json [ " active_schedule_id " ] = active_schedule_uid_str ;
2020-02-23 19:06:14 +00:00
relay_json [ " controller_id " ] = controller_id_str ;
2019-07-29 20:02:38 +00:00
relay_json [ " active_schedule " ] = this - > active_schedule - > to_json ( ) ;
2020-04-23 15:00:12 +00:00
relay_json [ " schedules " ] = schedules_json ;
2019-09-24 21:33:35 +00:00
relay_json [ " tag " ] = this - > tag ;
2019-07-20 21:33:17 +00:00
return relay_json ;
}
relay_dbo * *
relay_dbo : : get_all ( )
{
sqlite3_stmt * stmt ;
sqlite3_prepare_v2 ( globals : : db , " SELECT * FROM relays; " , - 1 , & stmt , nullptr ) ;
return relay_db_select ( stmt ) ;
}
relay_dbo * *
2020-02-23 19:06:14 +00:00
relay_dbo : : get_by_simple ( const char * key , const void * value , intptr_t bind_func , int bind_func_param )
2019-07-20 21:33:17 +00:00
{
helpers : : sql_filter_builder * filters [ 1 ] ;
helpers : : sql_filter_builder filter
{
key ,
value ,
bind_func ,
2020-02-23 19:06:14 +00:00
bind_func_param ,
2019-07-20 21:33:17 +00:00
" ; "
} ;
filters [ 0 ] = & filter ;
sqlite3_stmt * stmt = helpers : : create_sql_filtered_query ( " SELECT * FROM relays WHERE " , filters ) ;
return relay_db_select ( stmt ) ;
}
2019-07-20 22:29:05 +00:00
relay_dbo * *
relay_dbo : : get_by ( helpers : : sql_filter_builder * * filters )
{
sqlite3_stmt * stmt = helpers : : create_sql_filtered_query ( " SELECT * FROM relays WHERE " , filters ) ;
return relay_db_select ( stmt ) ;
}
2019-07-21 22:00:07 +00:00
relay_dbo *
2020-02-23 19:06:14 +00:00
relay_dbo : : get_relay_for_controller ( uuid_t controller_id , int relay_num )
2019-07-21 22:00:07 +00:00
{
helpers : : sql_filter_builder * filters [ 2 ] ;
helpers : : sql_filter_builder filter (
" number " ,
2020-04-19 00:44:35 +00:00
& relay_num ,
2019-07-21 22:00:07 +00:00
( intptr_t ) & sqlite3_bind_int ,
2020-02-23 19:06:14 +00:00
0 ,
2019-07-21 22:00:07 +00:00
" AND "
) ;
helpers : : sql_filter_builder filter2 (
2019-07-22 20:06:13 +00:00
" controller_id " ,
( void * ) controller_id ,
2020-02-23 19:06:14 +00:00
( intptr_t ) sqlite3_bind_blob ,
sizeof ( uuid_t ) ,
2019-07-21 22:00:07 +00:00
" ; "
) ;
filters [ 0 ] = & filter ;
filters [ 1 ] = & filter2 ;
auto relays = relay_dbo : : get_by ( filters ) ;
relay_dbo * relay = relays [ 0 ] ;
free ( relays ) ;
return relay ;
}
bool
2020-02-23 19:06:14 +00:00
relay_dbo : : valid_num_for_controller ( uuid_t search_controller_id , int relay_num )
2019-07-21 22:00:07 +00:00
{
2020-02-23 19:06:14 +00:00
controller_dbo * * controllers = controller_dbo : : get_by_simple ( " id " , search_controller_id , ( intptr_t ) & sqlite3_bind_blob , sizeof ( uuid_t ) ) ;
2019-07-21 22:00:07 +00:00
2019-07-22 20:06:13 +00:00
bool valid_id_and_num = controllers [ 0 ] & & controllers [ 0 ] - > relay_count > relay_num ;
controller_dbo : : free_list ( controllers ) ;
2019-07-21 22:00:07 +00:00
return valid_id_and_num ;
}
2019-07-20 21:33:17 +00:00
void
relay_dbo : : free_list ( relay_dbo * * relays_list )
{
for ( int i = 0 ; relays_list [ i ] ! = nullptr ; i + + )
{
2019-09-08 17:34:37 +00:00
delete relays_list [ i ] ;
2019-07-20 21:33:17 +00:00
}
free ( relays_list ) ;
2020-01-02 20:55:42 +00:00
}