You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3280 lines
120 KiB

#include "SQLite_dataBinder.h"
#include "../../components/base/basicEvent.h"
#define LOG( X ) m_pLog->Log( "SDBC", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG2w34[256];\
sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
m_pLog->Log( "SDBC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
}
#else
#define LOG_ERROR( X ) m_pLog->Log( "SDBC", this, _LOG_ERROR, X );
#endif
sqliteDBC *sqliteDBC::_instance = NULL;
/**
* Returns the single instance of the object.
*/
sqliteDBC *sqliteDBC::getInstance( config_setting_t * Settings ) {
// check if the instance has been created yet
if( 0 == _instance ){
// if not, then create it
_instance = new sqliteDBC( Settings );
};
// return the single instance
return _instance;
};
sqliteDBC::sqliteDBC( config_setting_t * Settings ):dataBinder(){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DATABASE" );
m_bIsConnected = false;
m_pDataStorage = NULL;
m_pDataMemory = NULL;
m_pDataReports = NULL;
m_nServerID = 1;
};
sqliteDBC::~sqliteDBC(){
if ( m_pDataStorage ) delete( m_pDataStorage );
if ( m_pDataMemory ) delete( m_pDataMemory );
if ( m_pDataReports ) delete( m_pDataReports );
_instance = NULL;
};
bool sqliteDBC::connect( void ){
if ( m_pSettings == NULL) return false;
bool bRecreateMemStructures = false;
config_setting_t * pSetting;
const char * tBuff;
//Disconnect if Connected
m_bIsConnected = false;
char web_access_user[64];
char web_access_group[64];
//Apply New Config
#ifdef DEBUG
LOG( "Reading DB Settings ... " );
#endif
if(!config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char**)&tBuff )){
pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
sprintf( m_pDbFile, "data/mysqlitedb.db" );
config_setting_set_string( pSetting, m_pDbFile );
basicSettingsProvider::getInstance()->compromise();
LOG("Data Storage not set - using default");
} else {
sprintf( m_pDbFile, tBuff );
};
if(!config_setting_lookup_string(m_pSettings, "REPORT_FILE_NAME", (const char**)&tBuff )){
pSetting = config_setting_add(m_pSettings, "REPORT_FILE_NAME", CONFIG_TYPE_STRING);
sprintf( m_pDbReport, "data/reports.db" );
config_setting_set_string( pSetting, m_pDbReport );
basicSettingsProvider::getInstance()->compromise();
LOG("Reports Storage not set - using default");
} else {
sprintf( m_pDbReport, tBuff );
};
if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char**)&tBuff )){
pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
sprintf( web_access_user, "www-data" );
config_setting_set_string( pSetting, web_access_user );
basicSettingsProvider::getInstance()->compromise();
LOG("Web Access User not set - using default");
} else {
sprintf( web_access_user, tBuff );
};
if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char**)&tBuff )){
pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
sprintf( web_access_group, "www-data" );
config_setting_set_string( pSetting, web_access_group );
basicSettingsProvider::getInstance()->compromise();
LOG("Web Access Group not set - using default");
} else {
sprintf( web_access_group, tBuff );
};
if( !fileExist( DB_SHMEM_POINT )) {
bRecreateMemStructures = true;
mkdir( DB_SHMEM_POINT, 0775 );// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
chown( DB_SHMEM_POINT, web_access_user, web_access_group ); //To make Accessed from PHP
chmod( DB_SHMEM_POINT, 0775 );
};
char tB[128];
if (!config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **)&tBuff)) {
pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
tBuff = tB;
sprintf( tB, "mem_db.db" );
config_setting_set_string( pSetting, tB );
basicSettingsProvider::getInstance()->compromise();
LOG("Memory Storage not set - using default");
};
sprintf( m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff );
basicSettingsProvider::getInstance()->validate();
// Kill Connection if any
if ( m_pDataStorage ) delete( m_pDataStorage );
if ( m_pDataMemory ) delete( m_pDataMemory );
if ( m_pDataReports ) delete( m_pDataReports );
#ifdef DEBUG
LOG( m_pDbFile );
#endif
// Create New Connections
m_pDataStorage = new SQLite_Connector();
if ( m_pDataStorage ) {
if( m_pDataStorage->Connect( m_pDbFile, 0, true ) ){
#ifdef DEBUG
LOG("DB File Opened");
#endif
chown( m_pDbFile, web_access_user, web_access_group );
chmod( m_pDbFile, 0664 );
} else{
LOG_ERROR("Cannot Connect to DB File");
delete( m_pDataStorage );
m_pDataStorage = NULL;
LOG("Storage Deleted!");
return false;
};
};
#ifdef DEBUG
LOG( m_pDbMemory );
#endif
if( !fileExist( m_pDbMemory )) {
bRecreateMemStructures = true;
//Always recreate
unlink( m_pDbMemory );
};
m_pDataMemory = new SQLite_Connector();
if ( m_pDataMemory ) {
if( m_pDataMemory->Connect( m_pDbMemory, 0, true ) ){
#ifdef DEBUG
LOG("DB Memory Opened");
#endif
chown( m_pDbMemory, web_access_user, web_access_group );
chmod( m_pDbMemory, 0664 );
m_pDataMemory->SilentQuery("PRAGMA journal_mode=WAL");
//Recreate at Register Server
//if ( bRecreateMemStructures ) initMemDB();
} else{
LOG_ERROR("Cannot Connect to DB Memory");
delete( m_pDataStorage );
delete( m_pDataMemory );
m_pDataStorage = NULL;
m_pDataMemory = NULL;
return false;
};
};
m_pDataReports = new SQLite_Connector();
if ( m_pDataReports ) {
if( m_pDataReports->Connect( m_pDbReport, 0, true ) ){
#ifdef DEBUG
LOG("Report File Opened");
#endif
chown( m_pDbReport, web_access_user, web_access_group );
chmod( m_pDbReport, 0664 );
} else{
LOG_ERROR("Cannot Connect to DB File");
delete( m_pDataReports );
m_pDataReports = NULL;
LOG("Storage Deleted!");
return false;
};
};
m_bIsConnected = true;
return m_bIsConnected;
};
void sqliteDBC::initMemDB( uint32_t sid ){
m_pDataMemory->SilentQuery("DROP TABLE \"nodes_qcq\"");
m_pDataMemory->SilentQuery("CREATE TABLE \"nodes_qcq\" ( \
\"id\" INTEGER NOT NULL,\
\"node_id\" INTEGER NOT NULL,\
\"cmd\" INTEGER NOT NULL,\
\"status\" INTEGER DEFAULT 0,\
\"parameter\" TEXT,\
PRIMARY KEY(\"id\" AUTOINCREMENT)\
);");
m_pDataMemory->SilentQuery("DROP TABLE \"servers\"");
m_pDataMemory->SilentQuery("CREATE TABLE \"servers\" (\
\"id\" INTEGER NOT NULL,\
\"ts\" INTEGER DEFAULT 0,\
\"pid\" INTEGER DEFAULT 0,\
\"hw_info\" TEXT,\
\"os_info\" TEXT,\
\"cpu_cores\" INTEGER DEFAULT 1,\
\"stat_cpu1\" REAL DEFAULT 0.0,\
\"stat_cpu5\" REAL DEFAULT 0.0,\
\"stat_cpu15\" REAL DEFAULT 0.0,\
\"stat_mem\" REAL DEFAULT 0.0,\
\"stat_disk\" REAL DEFAULT 0.0,\
\"val_mem_tottal\" INTEGER DEFAULT 0,\
\"val_mem_current\" INTEGER DEFAULT 0,\
\"val_disk_tottal\" INTEGER DEFAULT 0,\
\"val_disk_current\" INTEGER DEFAULT 0,\
\"temp0\" REAL DEFAULT 0.0,\
PRIMARY KEY(\"id\" AUTOINCREMENT)\
);");
char tB[128];
sprintf( tB, "INSERT INTO `servers` (`id`,`ts`) VALUES (%d, strftime('%%s') )", sid );
m_pDataMemory->SilentQuery( tB );
};
DBC_PUSHED_CMD *sqliteDBC::getNodePushedCMD( uint32_t NodeID ){
DBC_PUSHED_CMD * RetVal = NULL;
if ( m_bIsConnected ) {
char QS[MAX_PATH];
AAssoc * result;
sprintf( QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID );
#ifdef DEBUG_ALL
LOG( QS );
#endif
result = m_pDataMemory->Query( QS );
if ( result ) {
if( result->size() > 0 ) {
RetVal = (DBC_PUSHED_CMD*)malloc(sizeof(DBC_PUSHED_CMD));
if( RetVal ) {
Assoc * cArr;
Assoc::iterator it;
memset( RetVal, 0, sizeof(DBC_PUSHED_CMD));
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal->id = atoi( it->second.c_str() );
};
it = cArr->find("cmd");
if( it != cArr->end() ){
RetVal->CMD = atoi( it->second.c_str() );
};
it = cArr->find("parameter");
if( it != cArr->end() ){
strncpy( RetVal->PARAM, it->second.c_str(), 256 );
RetVal->PARAM[255] = (char)0;
};
};
#ifdef DEBUG_ALL
} else {
LOG("No Rows Fetched!");
#endif
};
};
m_pDataMemory->deleteResult( result );
};
return RetVal;
};
bool sqliteDBC::updateNodePushedCMD( uint32_t CmdID, uint8_t status ){
if ( m_bIsConnected ) {
char QS[MAX_PATH];
sprintf( QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID );
return m_pDataMemory->SilentQuery( QS );
};
return false;
};
bool sqliteDBC::cleanNodePushedCMD( void ){
if ( m_bIsConnected ) {
return m_pDataMemory->SilentQuery( "DELETE FROM `nodes_qcq` WHERE `status` > 200" );
};
return false;
};
void sqliteDBC::deleteNIF( uint32_t NifID ){
if ( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "", NifID );
#ifdef DEBUG_ALL
LOG( tQBuf );
#endif
m_pDataStorage->SilentQuery( tQBuf );
};
};
void sqliteDBC::deleteNIS( uint32_t NisID ){
};
void sqliteDBC::deleteNetwork( uint32_t NetID ){
};
void sqliteDBC::deleteResource( uint32_t ResourceID ){
};
void sqliteDBC::deleteSensor( uint32_t SensorID ){
};
void sqliteDBC::deleteUART( uint32_t UARTID ){
};
bool sqliteDBC::updateServerUtilisation( DBC_SYS_LOAD_INFO *ptr ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf(tQBuf, "UPDATE `servers` SET `ts`=STRFTIME('%%s'), `cpu_cores`=%d, `stat_cpu1`=%0.1f, `stat_cpu5`=%0.1f, `stat_cpu15`=%0.1f, `stat_mem`=%0.1f, `stat_disk`=%0.1f, `temp0`=%0.1f, `val_mem_tottal`=%" PRIu64 ", `val_mem_current`=%" PRIu64 ", `val_disk_tottal`=%" PRIu64 ", `val_disk_current`=%" PRIu64 " WHERE `id`=%d",
ptr->CPU_CORES,
ptr->cpu1,
ptr->cpu5,
ptr->cpu15,
ptr->mem,
ptr->disk,
ptr->temperature,
ptr->T_DISK,
ptr->C_DISK,
ptr->T_MEM,
ptr->C_MEM,
m_nServerID
);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataMemory->SilentQuery( tQBuf );
return true;
};
return false;
};
/********************************************************************
* *
* Slow DB Functions *
* *
********************************************************************/
bool sqliteDBC::addDeviceDraw( uint32_t DBID, uint8_t type, uint8_t lrf, void *th_data, uint32_t th_size ){
char tQBuf[1024];
if ( type == 0 ) {
CSTR uid = getUniqID_MD5();
if( uid == NULL ) return false;
sprintf( tQBuf, "data/device_draws/%s.raw", uid );
FILE *fd = fopen( tQBuf, "wb+" );
if( fd!= NULL ) {
uint32_t sisi = th_size >> 2;
fwrite( &sisi, 4, 1, fd );
fwrite( th_data, 4, sisi, fd );
fclose( fd );
sprintf( tQBuf, "INSERT INTO `lrf_draw`(`node_id`,`lrf_id`,`type`,`ts`,`data`) VALUES(%d,%d,%d,STRFTIME('%%s'),'%s')",
DBID, lrf, type, uid );
free ( uid );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
free( uid );
} else {
LOG("No RAW DATA!");
};
return false;
};
uint32_t sqliteDBC::addEvent( DBC_EVT_RECORD *ptr ){
if ( ptr == NULL ) return 0;
if (m_bIsConnected) {
char tQBuf[1024];
switch ( ptr->type ) {
case EVT_FLT_LATF_CHK:
case EVT_FLT_LATF_UP_DETEC:
case EVT_FLT_LATF_BT_DETEC:
case EVT_FLT_LATF_TRP_DET:
case EVT_FLT_LATF_TRP_UPDT:
case EVT_FLT_LATF_SUSP_START:
case EVT_FLT_LATF_SUSP_NAK:
case EVT_FLT_LATF_REGULAR:
case EVT_FLT_LATF_DISABLED:
case EVT_FLT_LATF_SUSP_DET:
case EVT_FLT_LATF_DIFF_DET:
case EVT_LRF_START_SESSION:
case EVT_LRF_END_SESSION_TO:
case EVT_LRF_END_SESSION_MA:
case EVT_STUDY:
case EVT_BUSY:
case EVT_FLIP:
case EVT_READY:{
sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)",
ptr->node_id,
ptr->uuid,
ptr->ts_s,
ptr->ts_ms,
ptr->type
);
break;
};
case EVT_ROI_ACTIVE:
case EVT_TARGET_3D_WHITE:
case EVT_TARGET_3D_BLACK:{
sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`range_digital`,`azimuth`,`elevation`,`range`,`area`,`lat`,`lon`,`alt`)" \
" VALUES(%d,'%s',%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
ptr->node_id,
ptr->uuid,
ptr->ts_s,
ptr->ts_ms,
ptr->type,
ptr->range_digital,
ptr->az,
ptr->el,
ptr->ra,
ptr->area,
ptr->lat,
ptr->lon,
ptr->alt
);
break;
};
case EVT_TARGET_2D_WHITE:
case EVT_TARGET_2D_BLACK:{
sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)" \
" VALUES(%d,'%s',%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
ptr->node_id,
ptr->uuid,
ptr->ts_s,
ptr->ts_ms,
ptr->type,
ptr->range_digital,
ptr->az,
ptr->ra,
ptr->area,
ptr->lat,
ptr->lon,
ptr->alt
);
break;
};
default: return 0;
};
LOG( tQBuf );
return ptr->id = m_pDataReports->QueryID( tQBuf );
};
return 0;
};
bool sqliteDBC::addIPCam( OBSERVER_RESULT *tPtr ){
if(m_bIsConnected) {
char tQBuf[1024];
char tIpBuf[32];
char tProto[32];
char tPath[CTEXT];
char *tPos;
char *tPos2;
uint16_t tPort = 0;
memcpy( tProto, tPtr->XAddrs, 5 );
tProto[5] = (char)0;
if( tProto[4] == ':') tProto[4] = (char)0;
memset( tPath, 0, CTEXT );
tPos = strstr(tPtr->XAddrs,"//");
if( tPos != NULL ) {
tPos2 = strstr( tPos + 2,":");
if( tPos2 != NULL ) {
LOG("IP Port Defined!");
int len, len2, len3;
len = tPos2 - tPos - 2;
strncpy( tIpBuf, tPos + 2, len);
tIpBuf[len] = (char)0;
tPort = atoi( tPos2 + 1 );
strncpy( tPath, tPos + 2 + len, CTEXT );
} else {
tPos2 = strstr( tPos + 2,"/");
if( tPos2 != NULL ) {
int len, len2, len3;
len = tPos2 - tPos - 2;
strncpy( tIpBuf, tPos + 2, len);
tIpBuf[len] = (char)0;
strncpy( tPath, tPos + 2 + len, CTEXT );
} else {
tPos2 = strstr( tPos + 2,"/");
LOG_ERROR("Pos 2 - err");
tIpBuf[0] = (char)0;
};
};
} else {
LOG_ERROR("Pos 0 - err");
tIpBuf[0] = (char)0;
};
if ( tPort ) {
sprintf(tQBuf, "INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`port`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',%d,12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s', `port`=%d",
tPtr->RefID,
//tPtr->XAddrs,
tPath,
tIpBuf,
tPort,
tPtr->MAC,
tPtr->Hardware,
tPtr->Name,
tProto,
//tPtr->XAddrs,
tIpBuf,
tPort
);
} else {
sprintf(tQBuf, "INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s'",
tPtr->RefID,
//tPtr->XAddrs,
tPath,
tIpBuf,
tPtr->MAC,
tPtr->Hardware,
tPtr->Name,
tProto,
//tPtr->XAddrs,
tIpBuf
);
}
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
uint32_t sqliteDBC::addLocalCam( CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[1024];
char tM5[33];
CSTR tMD5 = MD5String( path );
if( tMD5 != NULL ) {
memcpy( tM5, tMD5, 33 );
tM5[32]=(char)0;
free( tMD5 );
sprintf(tQBuf,"INSERT INTO cameras (`online`,`uuid`,`type`,`status`,`hardware`,`name`,`ip`) VALUES (1,'%s',10,0,'%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1",
tM5,
hardware,
name,
path
);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
uint32_t camID = (uint32_t) m_pDataStorage->QueryID( tQBuf );
if ( camID == 0 ) {
camID = getCamId( tM5 );
};
sprintf(tQBuf, "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`mrl_proto`,`mrl`,`picture_proto`,`fps`) VALUES(%d,'A',1,'%s','%s',%d,%d,'v4l2','%s','v4l2',%d) ON CONFLICT(`source_id`,`token`) DO UPDATE SET `valid`=1, img_width=%d, img_height=%d, `fps`=%d",
camID, name, tM5, width, height, path, fps, width, height, fps );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
if( RetVal == 0 ){
RetVal = getMediaStreamID( camID, tM5 );
};
};
};
return RetVal;
};
uint32_t sqliteDBC::addLocalNetwork( uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "INSERT INTO `networks`(`nif_id`,`type`,`address`,`gateway`,`mask`,`broadcast`) VALUES (%d,%d,'%s','%s','%s','%s') ON CONFLICT(`nif_id`,`type`) DO UPDATE SET `address`='%s', `gateway`='%s', `mask`='%s', `broadcast`='%s'",
NIF_ID,
net_type,
address,
gateway,
mask,
broadcast,
address,
gateway,
mask,
broadcast
);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
RetVal = m_pDataStorage->QueryID( tQBuf );
// On Update IF data no need to return ID
//if( RetVal == 0 ) {
// RetVal = getNetworkInterfaceID( tKey );
//};
};
return RetVal;
};
uint32_t sqliteDBC::addLocalNetworkInfrastructure( uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
bool ena_routing = true;
if( OUTBOUND_NIF == 0 ){
ena_routing = false;
};
if( ena_dhcp ){
if ( BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL ) {
LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
return 0;
};
sprintf(tQBuf, "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`,`enable_dhcp`,`dhcp_range_begin`,`dhcp_range_end`,`dhcp_range_mask`,`dhcp_lease_time`) VALUES (%d,%d,%d,1,'%s','%s','%s','%s') ON CONFLICT(`nif_id`) DO UPDATE SET `enable_routing`=%d, `outbond_nif`=%d, `enable_dhcp`=1, `dhcp_range_begin`='%s', `dhcp_range_end`='%s', `dhcp_range_mask`='%s', `dhcp_lease_time`='%s'",
NIF_ID,
ena_routing,
OUTBOUND_NIF,
BRANG,
ERANG,
MASK,
LEASE_TIME,
ena_routing,
OUTBOUND_NIF,
BRANG,
ERANG,
MASK,
LEASE_TIME
);
} else {
if( ena_routing ){
sprintf(tQBuf, "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`) VALUES(%d,1,%d) ON CONFLICT(`nif_id`) DO UPDATE SET `enable_routing`=1, `outbond_nif`=%d",
NIF_ID,
OUTBOUND_NIF,
OUTBOUND_NIF
);
} else {
LOG_ERROR("No Special Service Defined To Network Infrastructure!");
return 0;
};
};
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
RetVal = m_pDataStorage->QueryID( tQBuf );
// On Update IF data no need to return ID
if( RetVal == 0 ) {
RetVal = getNetworkInfrastructureID( NIF_ID );
};
};
return RetVal;
};
uint32_t sqliteDBC::addLocalNIF( CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
char * tKey = MD5String( device );
if( tKey != NULL ) {
sprintf( tQBuf, "INSERT INTO `network_interfaces` (`type`,`key`,`device_name`,`mac`,`mode`,`aux_id`) VALUES (%d,'%s','%s','%s',%d,%d) ON CONFLICT(`key`) DO UPDATE SET `mode`=%d,`aux_id`=%d,`mac`='%s'",
dtype,
tKey,
device,
mac,
dmode,
aux_id,
dmode,
aux_id,
mac
);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
RetVal = m_pDataStorage->QueryID( tQBuf );
if( RetVal == 0 ) {
RetVal = getNIF_ID( tKey );
};
free( tKey );
};
};
return RetVal;
};
uint32_t sqliteDBC::addLocalUART( uint8_t utype, uint32_t speed, uint8_t bit, char parity, float stop_bit, CSTR PATH, uint8_t mode, uint16_t port ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "INSERT INTO `uart_settings`(`node_id`,`type`,`speed`,`bit`,`parity`,`stop_bit`,`mode`,`port`,`path`) VALUES (%d,%d,%d,%d,'%c',%.1f,%d,%d,'%s') ON CONFLICT(`node_id`,`type`,`path`) DO UPDATE SET `id`=`id`",
m_nServerNodeID,
utype,
speed,
bit,
parity,
stop_bit,
mode,
port,
PATH
);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
RetVal = m_pDataStorage->QueryID( tQBuf );
if( RetVal == 0 ) {
RetVal = getUartID( m_nServerNodeID, utype, PATH );
};
};
return RetVal;
};
uint32_t sqliteDBC::addLocalWiFi( uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "INSERT INTO `wifis` (`nif_id`,`mode`,`aux_id`,`SSID`,`password`,`encryption_type`) VALUES (%d,%d,%d,'%s','%s',%d) ON CONFLICT (`nif_id`) DO UPDATE SET `mode`=%d, `aux_id`=%d, `SSID`='%s', `password`='%s', `encryption_type`=%d",
NIF_ID,
mode,
router_id,
SSID,
password,
encryption,
mode,
router_id,
SSID,
password,
encryption
);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
RetVal = m_pDataStorage->QueryID( tQBuf );
if( RetVal == 0 ) {
RetVal = getWiFi_ID( NIF_ID );
};
};
return RetVal;
};
bool sqliteDBC::cleanLRFMask( uint32_t maskID, bool deleteMask ){
if(m_bIsConnected) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "DELETE FROM `lrf_mask_trace_zones` WHERE `zone_id` IN ( SELECT `id` FROM `lrf_mask_traces` WHERE `mask_id`=%d )", maskID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf( tQBuf, "DELETE FROM `lrf_mask_traces` WHERE `mask_id`=%d", maskID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf( tQBuf, "DELETE FROM `lrf_mask_vertex_points` WHERE `vertex_id` IN ( SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d )", maskID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf( tQBuf, "DELETE FROM `lrf_mask_vertexes` WHERE `mask_id`=%d", maskID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
//Delete Mask Itself!
if ( deleteMask ) {
sprintf( tQBuf, "DELETE FROM `lrf_masks` WHERE `id`=%d", maskID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
};
return true;
};
return false;
};
bool sqliteDBC::cleanResources( bool all ){
if(m_bIsConnected) {
char tQBuf[MAX_PATH_LEN];
if( all ) {
sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID );
} else {
sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE ( `node_id`=%d ) AND ( `type`=100 OR `type`=101 OR `type`=103 OR `type`=120 )", m_nServerNodeID );
};
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::wipeServerResources(){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "DELETE FROM `resources` WHERE `node_id`=%d", m_nServerNodeID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
uint32_t sqliteDBC::addLRFMask( uint32_t devID, uint8_t lrfID, uint8_t layerID ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "INSERT INTO `lrf_masks`(`sensor_id`,`lrf_id`,`layer`) VALUES(%d,%d,%d) ON CONFLICT(`sensor_id`,`lrf_id`,`layer`) DO UPDATE SET `layer`=%d",
devID,
lrfID,
layerID,
layerID
);
#ifdef DEBUG
LOG(tQBuf);
#endif
RetVal = m_pDataStorage->QueryID( tQBuf );
if( RetVal == 0 ) {
RetVal = getLRFMaskID( devID, lrfID, layerID );
};
};
return RetVal;
};
uint32_t sqliteDBC::addLRFMaskPolygone( uint32_t maskID, uint8_t r, uint8_t g, uint8_t b ){
uint32_t RetVal = 0;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "INSERT INTO `lrf_mask_vertexes`(`mask_id`,`r`,`g`,`b`) VALUES(%d,%d,%d,%d)",
maskID,
r,
g,
b
);
#ifdef DEBUG
LOG(tQBuf);
#endif
RetVal = m_pDataStorage->QueryID( tQBuf );
};
return RetVal;
};
bool sqliteDBC::addLRFMaskPolygonePoint( uint32_t polyID, int16_t x, int16_t y ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "INSERT INTO `lrf_mask_vertex_points`(`vertex_id`,`x`,`y`) VALUES(%d,%d,%d)",
polyID,
x,
y
);
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){
uint32_t RetVal = 0;
if( m_bIsConnected ){
char tQBuf[2048];
sprintf( tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName );
CSTR tMD5 = MD5String( tQBuf );
if( tMD5 ) {
sprintf( tQBuf, "INSERT INTO `resources`(`node_id`,`type`,`md5`,`aux_id`,`device`,`active`) VALUES (%d,%d,'%s',%d,'%s',1) ON CONFLICT(`md5`) DO UPDATE SET `active`=1, `aux_id`=%d",
m_nServerNodeID,
type,
tMD5,
auxID,
devName,
auxID
);
free( tMD5 );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
if( RetVal == 0 ) {
RetVal = getResourceID( m_nServerNodeID, type, auxID );
};
};
};
return RetVal;
};
bool sqliteDBC::addVideoEvent( uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms ){
if( m_bIsConnected ){
char tQBuf[2048];
sprintf( tQBuf, "INSERT INTO `events_2_videos`(`evt_id`,`video_id`,`frame`,`tss`,`tms`) VALUES(%d,%d,%d,%d,%d)",
evtID,
videoID,
frame,
tss,
tms
);
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataReports->SilentQuery( tQBuf );
};
return false;
};
uint32_t sqliteDBC::addVideoRecord( uint32_t streamID, CSTR md5, CSTR ext, uint32_t tss, uint32_t tms, uint32_t fps ){
uint32_t RetVal = 0;
if( m_bIsConnected ){
char tQBuf[2048];
sprintf( tQBuf, "INSERT INTO `videos`(`media_stream_id`,`md5`,`uts`,`uts_ms`,`ext`,`fps`) VALUES(%d,'%s',%d,%d,'%s',%d)",
streamID,
md5,
tss,
tms,
ext,
fps
);
#ifdef DEBUG
LOG(tQBuf);
#endif
RetVal = (uint32_t) m_pDataReports->QueryID( tQBuf );
};
return RetVal;
};
DBC_MODULE_EMBED_VIDEO_DATA *sqliteDBC::getEmbedVideoData( uint32_t ID ){
DBC_MODULE_EMBED_VIDEO_DATA *tPtr = NULL;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
AAssoc *result;
sprintf( tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tPtr = (DBC_MODULE_EMBED_VIDEO_DATA*)malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
if( tPtr!= NULL ) {
Assoc::iterator it;
memset(tPtr,0,sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
cArr = result->at(0);
it = cArr->find("output_mode");
if( it != cArr->end() ){
tPtr->output_mode = atoi( it->second.c_str() );
};
it = cArr->find("web_port");
if( it != cArr->end() ){
tPtr->port = atoi( it->second.c_str() );
};
it = cArr->find("width");
if( it != cArr->end() ){
tPtr->width = atoi( it->second.c_str() );
};
it = cArr->find("height");
if( it != cArr->end() ){
tPtr->height = atoi( it->second.c_str() );
};
it = cArr->find("compression_quality");
if( it != cArr->end() ){
tPtr->compression_quality = atoi( it->second.c_str() );
};
it = cArr->find("show_ts");
if( it != cArr->end() ){
tPtr->show_ts = (bool)atoi( it->second.c_str() );
};
it = cArr->find("ts_x");
if( it != cArr->end() ){
tPtr->ts_x = atoi( it->second.c_str() );
};
it = cArr->find("ts_y");
if( it != cArr->end() ){
tPtr->ts_y = atoi( it->second.c_str() );
};
it = cArr->find("ts_font_face");
if( it != cArr->end() ){
tPtr->ts_font_face = atoi( it->second.c_str() );
};
it = cArr->find("ts_font_scale");
if( it != cArr->end() ){
tPtr->ts_font_scale = atof( it->second.c_str() );
};
it = cArr->find("ts_font_thickness");
if( it != cArr->end() ){
tPtr->ts_font_thickness = atoi( it->second.c_str() );
};
it = cArr->find("ts_line_type");
if( it != cArr->end() ){
tPtr->ts_line_type = atoi( it->second.c_str() );
};
it = cArr->find("ts_transperant");
if( it != cArr->end() ){
tPtr->ts_transperant = (bool)atoi( it->second.c_str() );
};
it = cArr->find("show_label");
if( it != cArr->end() ){
tPtr->show_label = (bool)atoi( it->second.c_str() );
};
it = cArr->find("label_x");
if( it != cArr->end() ){
tPtr->label_x = atoi( it->second.c_str() );
};
it = cArr->find("label_y");
if( it != cArr->end() ){
tPtr->label_y = atoi( it->second.c_str() );
};
it = cArr->find("label_font_face");
if( it != cArr->end() ){
tPtr->label_font_face = atoi( it->second.c_str() );
};
it = cArr->find("label_font_scale");
if( it != cArr->end() ){
tPtr->label_font_scale = atof( it->second.c_str() );
};
it = cArr->find("label_font_thickness");
if( it != cArr->end() ){
tPtr->label_font_thickness = atoi( it->second.c_str() );
};
it = cArr->find("label_line_type");
if( it != cArr->end() ){
tPtr->label_line_type = atoi( it->second.c_str() );
};
it = cArr->find("label_transperant");
if( it != cArr->end() ){
tPtr->label_transperant = (bool) atoi( it->second.c_str() );
};
it = cArr->find("show_logo");
if( it != cArr->end() ){
tPtr->show_logo = (bool) atoi( it->second.c_str() );
};
it = cArr->find("logo_w");
if( it != cArr->end() ){
tPtr->logo_w = atoi( it->second.c_str() );
};
it = cArr->find("logo_h");
if( it != cArr->end() ){
tPtr->logo_h = atoi( it->second.c_str() );
};
it = cArr->find("logo_x");
if( it != cArr->end() ){
tPtr->logo_x = atoi( it->second.c_str() );
};
it = cArr->find("logo_y");
if( it != cArr->end() ){
tPtr->logo_y = atoi( it->second.c_str() );
};
it = cArr->find("logo_md5");
if( it != cArr->end() ){
strncpy( tPtr->logo_md5, it->second.c_str(),32);
tPtr->logo_md5[32]=(char)0;
};
};
};
};
deleteResult( result );
};
return tPtr;
};
DBC_MEDIA_STREAM_DATA *sqliteDBC::getMediaStreamData(uint32_t ID){
DBC_MEDIA_STREAM_DATA *tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
if( tVal != NULL ) {
memset( tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA) );
Assoc::iterator it;
cArr = result->at(0);
tVal->id = ID;
it = cArr->find("source_id");
if( it != cArr->end() ){
tVal->source_id = atoi( it->second.c_str() );
};
it = cArr->find("source_type");
if( it != cArr->end() ){
sprintf( tVal->source_type, it->second.c_str() );
};
it = cArr->find("valid");
if( it != cArr->end() ){
tVal->valid = (bool) atoi( it->second.c_str() );
};
it = cArr->find("token");
if( it != cArr->end() ){
sprintf( tVal->token, it->second.c_str() );
};
it = cArr->find("img_width");
if( it != cArr->end() ){
tVal->width = atoi( it->second.c_str() );
};
it = cArr->find("img_height");
if( it != cArr->end() ){
tVal->height = atoi( it->second.c_str() );
};
it = cArr->find("fps");
if( it != cArr->end() ){
tVal->fps = atoi( it->second.c_str() );
};
it = cArr->find("compression_type");
if( it != cArr->end() ){
sprintf(tVal->c_type, it->second.c_str());
};
it = cArr->find("mrl_proto");
if( it != cArr->end() ){
sprintf( tVal->mrl_proto, it->second.c_str() );
};
it = cArr->find("mrl_port");
if( it != cArr->end() ){
tVal->mrl_port = atoi( it->second.c_str() );
};
it = cArr->find("mrl");
if( it != cArr->end() ){
sprintf( tVal->mrl, it->second.c_str() );
};
};
};
};
deleteResult( result );
};
return tVal;
};
uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
uint32_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
DBC_MODULE_DATA *tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
if( tVal != NULL ) {
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->module_id = atoi( it->second.c_str() );
};
it = cArr->find("aux_id");
if( it != cArr->end() ){
tVal->aux_id = atoi( it->second.c_str() );
};
it = cArr->find("type");
if( it != cArr->end() ){
tVal->type = atoi( it->second.c_str() );
};
it = cArr->find("enabled");
if( it != cArr->end() ){
tVal->enabled = (bool)atoi( it->second.c_str() );
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_LRF_INI *sqliteDBC::getINIData( uint32_t lrfID ){
DBC_LRF_INI * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM `lrf_ini_settings` WHERE `sensor_id`=%d", lrfID );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_LRF_INI*) malloc(sizeof(DBC_LRF_INI));
if( tVal != NULL ) {
memset( tVal, 0 , sizeof(DBC_LRF_INI));
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("AUTORESET_ENABLE");
if( it != cArr->end() ){
tVal->_AUTO_RESET_ENABLE = (bool) atoi( it->second.c_str() );
};
it = cArr->find("AUTORESET_INTERLEAVE");
if( it != cArr->end() ){
tVal->_AUTO_RESET_INTERLEAVE = atoi( it->second.c_str() );
};
it = cArr->find("AUTORESET_TIMEOUT");
if( it != cArr->end() ){
tVal->_AUTO_RESET_TIMEOUT = atoi( it->second.c_str() );
};
it = cArr->find("BOARD_TEMPERATURE");
if( it != cArr->end() ){
tVal->_BOARD_TEMPERATURE = atoi( it->second.c_str() );
};
it = cArr->find("BOARD_ZEROSHIFT");
if( it != cArr->end() ){
tVal->_BOARD_ZEROSHIFT = atoi( it->second.c_str() );
};
it = cArr->find("BOARD_SPEED");
if( it != cArr->end() ){
tVal->_BOARD_SPEED = atoi( it->second.c_str() );
};
it = cArr->find("RUN_DIR");
if( it != cArr->end() ){
tVal->_RUN_DIR = atoi( it->second.c_str() );
};
it = cArr->find("JUST_MOVING_TARGETS");
if( it != cArr->end() ){
tVal->_JUST_MOVING_TARGETS = (bool) atoi( it->second.c_str() );
};
it = cArr->find("JUST_MOVING_RADIAL");
if( it != cArr->end() ){
tVal->_JUST_MOVING_RADIAL = (bool) atoi( it->second.c_str() );
};
it = cArr->find("JUST_MOVING_DISTANCE");
if( it != cArr->end() ){
tVal->_JUST_MOVING_DISTANCE = (bool) atoi( it->second.c_str() );
};
it = cArr->find("FREEZ_FIRST_TH");
if( it != cArr->end() ){
tVal->_FREEZ_FIRST_TH = (bool) atoi( it->second.c_str() );
};
it = cArr->find("SAVE_FIRST_TH");
if( it != cArr->end() ){
tVal->_SAVE_FIRST_TH = (bool) atoi( it->second.c_str() );
};
};
};
};
deleteResult( result );
};
return tVal;
};
bool sqliteDBC::getLRFEventSessionStatus( uint32_t nodeID ){
bool rv = false;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf( tQBuf, "SELECT `evt_type` FROM `events` WHERE `node_id`=%d AND ( `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ) ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", nodeID, EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataReports->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("evt_type");
if( it != cArr->end() ){
if( atoi( it->second.c_str() ) == EVT_LRF_START_SESSION ) rv = true;
};
};
};
deleteResult( result );
};
return rv;
};
DBC_LRF_DATA *sqliteDBC::getLRFData( uint32_t ID ){
DBC_LRF_DATA * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_LRF_DATA*) malloc(sizeof(DBC_LRF_DATA));
if( tVal != NULL ) {
memset( tVal, 0 , sizeof(DBC_LRF_DATA));
Assoc::iterator it;
tVal->id = ID;
cArr = result->at(0);
it = cArr->find("data_status");
if( it != cArr->end() ){
tVal->data_status = atoi( it->second.c_str() );
};
it = cArr->find("serial");
if( it != cArr->end() ){
strncpy( tVal->serial, it->second.c_str(), sizeof(tVal->serial)-1 );
tVal->serial[sizeof(tVal->serial)-1]=(char)0;
};
it = cArr->find("version");
if( it != cArr->end() ){
strncpy( tVal->version, it->second.c_str(), sizeof(tVal->version)-1 );
tVal->version[sizeof(tVal->version)-1]=(char)0;
};
it = cArr->find("x");
if( it != cArr->end() ){
tVal->x = atof( it->second.c_str() );
};
it = cArr->find("y");
if( it != cArr->end() ){
tVal->y = atof( it->second.c_str() );
};
it = cArr->find("z");
if( it != cArr->end() ){
tVal->z = atof( it->second.c_str() );
};
it = cArr->find("x_rot");
if( it != cArr->end() ){
tVal->x_rot = atof( it->second.c_str() );
};
it = cArr->find("y_rot");
if( it != cArr->end() ){
tVal->y_rot = atof( it->second.c_str() );
};
it = cArr->find("z_rot");
if( it != cArr->end() ){
tVal->z_rot = atof( it->second.c_str() );
};
it = cArr->find("uart_id");
if( it != cArr->end() ){
tVal->uart_id = atoi( it->second.c_str() );
};
it = cArr->find("device_id");
if( it != cArr->end() ){
tVal->device_id = atoi( it->second.c_str() );
};
it = cArr->find("ip_address");
if( it != cArr->end() ){
sprintf( tVal->master_host, it->second.c_str() );
};
it = cArr->find("aux_ip_address");
if( it != cArr->end() ){
sprintf( tVal->slave_host, it->second.c_str() );
};
it = cArr->find("port");
if( it != cArr->end() ){
tVal->master_port = atoi( it->second.c_str() );
};
it = cArr->find("aux_port");
if( it != cArr->end() ){
tVal->slave_port = atoi( it->second.c_str() );
};
it = cArr->find("auto_draw");
if( it != cArr->end() ){
tVal->auto_draw = atoi( it->second.c_str() );
};
it = cArr->find("filter_id");
if( it != cArr->end() ){
tVal->filter_id = atoi( it->second.c_str() );
};
//it = cArr->find("enabled_lrf");
//if( it != cArr->end() ){
// tVal->enabled_lrf = atoi( it->second.c_str() );
//};
it = cArr->find("max_range");
if( it != cArr->end() ){
tVal->max_range = atof( it->second.c_str() );
};
it = cArr->find("base_shift");
if( it != cArr->end() ){
tVal->base_shift = atoi( it->second.c_str() );
};
it = cArr->find("mirror_shift");
if( it != cArr->end() ){
tVal->mirror_shift = atoi( it->second.c_str() );
};
it = cArr->find("detection_profile_id");
if( it != cArr->end() ){
tVal->detection_profile_id = atoi( it->second.c_str() );
};
it = cArr->find("draw_lrf_id");
if( it != cArr->end() ){
tVal->draw_lrf_id = atoi( it->second.c_str() );
};
it = cArr->find("width_table_id");
if( it != cArr->end() ){
tVal->width_table_id = atoi( it->second.c_str() );
};
it = cArr->find("zoom_table_id");
if( it != cArr->end() ){
tVal->zoom_table_id = atoi( it->second.c_str() );
};
it = cArr->find("embd_cam");
if( it != cArr->end() ){
tVal->embd_cam = atoi( it->second.c_str() );
};
it = cArr->find("embd_cam_auto");
if( it != cArr->end() ){
tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
};
it = cArr->find("embd_dc");
if( it != cArr->end() ){
tVal->embd_dc = atoi( it->second.c_str() );
};
it = cArr->find("autoreset");
if( it != cArr->end() ){
tVal->autoreset = (bool)atoi( it->second.c_str() );
};
it = cArr->find("force_settings");
if( it != cArr->end() ){
tVal->force_settings = (bool)atoi( it->second.c_str() );
};
};
};
};
deleteResult( result );
};
return tVal;
};
PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master ){
PLGS_2D_PARAMETERS retVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID,((master)?1:0) );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
memset( retVal, 0, sizeof(LGS_2D_PARAMETERS));
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("order");
if( it != cArr->end() ){
retVal->nOrder = atoi(it->second.c_str());
};
it = cArr->find("offset");
if( it != cArr->end() ){
retVal->nOffset = atoi(it->second.c_str());
};
it = cArr->find("delta");
if( it != cArr->end() ){
retVal->nDelta = atoi(it->second.c_str());
};
it = cArr->find("flip");
if( it != cArr->end() ){
retVal->nFlip = atoi(it->second.c_str());
};
it = cArr->find("width");
if( it != cArr->end() ){
retVal->nWidth = atoi(it->second.c_str());
};
it = cArr->find("sst");
if( it != cArr->end() ){
retVal->bSST = atoi(it->second.c_str());
};
it = cArr->find("sun");
if( it != cArr->end() ){
retVal->nSun = atoi(it->second.c_str());
};
};
};
deleteResult( result );
};
return retVal;
};
uint8_t sqliteDBC::getLRFDataStatus( uint32_t ID ){
uint8_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("data_status");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
uint32_t sqliteDBC::getLRFMaskID( uint32_t deviceID, uint8_t lrfID, uint8_t layer ){
uint32_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
uint8_t sqliteDBC::getLRFMasksStatus( uint32_t deviceID ){
uint8_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("layer");
if( it != cArr->end() ){
RetVal |= ( 1<< atoi( it->second.c_str() ));
};
};
};
};
deleteResult( result );
};
return RetVal;
};
DBC_LRF_UPLOAD_DATA *sqliteDBC::getLRFUploadData( uint32_t upID ){
DBC_LRF_UPLOAD_DATA *RetVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result->size() > 0 ) {
Assoc *cArr;
RetVal = (DBC_LRF_UPLOAD_DATA*) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
if( RetVal != NULL ) {
memset( RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
Assoc::iterator it;
RetVal->id = upID;
cArr = result->at(0);
it = cArr->find("md5");
if( it != cArr->end() ){
strncpy( RetVal->md5, it->second.c_str(), 32 );
};
it = cArr->find("file");
if( it != cArr->end() ){
strncpy( RetVal->fname, it->second.c_str(), 16 );
};
it = cArr->find("issue_reset");
if( it != cArr->end() ){
RetVal->transfer_mode = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
uint32_t sqliteDBC::getNodeAuxParam( uint32_t NodeID ){
uint32_t retVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("aux_parameter");
if( it != cArr->end() ){
retVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return retVal;
};
DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
DBC_NODE_DATA * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
if( tVal != NULL ) {
memset( tVal, 0, sizeof(DBC_NODE_DATA));
Assoc::iterator it;
tVal->node_id = ID;
cArr = result->at(0);
it = cArr->find("type");
if( it != cArr->end() ){
tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
};
it = cArr->find("aux_id");
if( it != cArr->end() ){
tVal->aux_id = atoi( it->second.c_str() );
};
it = cArr->find("aux_parameter");
if( it != cArr->end() ){
tVal->aux_parameter = atoi( it->second.c_str() );
};
it = cArr->find("online");
if( it != cArr->end() ){
tVal->online = (bool)atoi( it->second.c_str() );
};
it = cArr->find("enable");
if( it != cArr->end() ){
tVal->enabled = (bool)atoi( it->second.c_str() );
};
it = cArr->find("name");
if( it != cArr->end() ){
sprintf( tVal->name, it->second.c_str() );
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
DBC_NET_IF_DATA * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
if( tVal != NULL ) {
Assoc::iterator it;
tVal->id = nif_id;
cArr = result->at(0);
it = cArr->find("type");
if( it != cArr->end() ){
tVal->type = atoi(it->second.c_str());
};
it = cArr->find("mode");
if( it != cArr->end() ){
tVal->mode = atoi(it->second.c_str());
};
it = cArr->find("device_name");
if( it != cArr->end() ){
strncpy( tVal->device_name, it->second.c_str(),11);
tVal->device_name[10]=(char)0;
};
it = cArr->find("mac");
if( it != cArr->end() ){
strncpy( tVal->mac, it->second.c_str(), 33 );
tVal->mac[32]=(char)0;
};
it = cArr->find("aux_id");
if( it != cArr->end() ){
tVal->aux_id = atoi(it->second.c_str());
};
};
};
};
deleteResult( result );
};
return tVal;
};
uint32_t sqliteDBC::getNIF_ID( CSTR key ){
uint32_t tVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
tVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return tVal;
};
uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
uint32_t tVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
tVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return tVal;
};
uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
uint32_t tVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
tVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
DBC_RESOURCE_DATA *tVal = NULL;
if( m_bIsConnected ){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
if( tVal != NULL ) {
Assoc::iterator it;
cArr = result->at(0);
tVal->id = ResourceID;
it = cArr->find("node_id");
if( it != cArr->end() ){
tVal->node_id = atoi( it->second.c_str() );
};
it = cArr->find("type");
if( it != cArr->end() ){
tVal->type = atoi( it->second.c_str() );
};
it = cArr->find("aux_id");
if( it != cArr->end() ){
tVal->aux_id = atoi( it->second.c_str() );
};
it = cArr->find("device");
if( it != cArr->end() ){
strncpy( tVal->device, it->second.c_str(), 64 );
tVal->device[63] = (char)0;
};
};
};
};
deleteResult( result );
};
return tVal;
};
uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
uint32_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
AAssoc *result;
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc * cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
uint32_t sqliteDBC::getServerID( CSTR identity ){
uint32_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
AAssoc *result;
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc * cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
uint32_t sqliteDBC::getServerNode( uint32_t SID ){
uint32_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
AAssoc *result;
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc * cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
}
DBC_TARGET_FLITER_DATA *sqliteDBC::getTargetFilterData( uint32_t ID ){
DBC_TARGET_FLITER_DATA * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_TARGET_FLITER_DATA*) malloc(sizeof(DBC_TARGET_FLITER_DATA));
if( tVal != NULL ) {
memset( tVal, 0 , sizeof(DBC_TARGET_FLITER_DATA));
Assoc::iterator it;
tVal->id = ID;
cArr = result->at(0);
it = cArr->find("type");
if( it != cArr->end() ){
tVal->type = (TARGET_FILTER_TYPE) atoi( it->second.c_str() );
};
it = cArr->find("aux_id");
if( it != cArr->end() ){
tVal->aux_id = atoi( it->second.c_str() );
};
it = cArr->find("enabled");
if( it != cArr->end() ){
tVal->enabled = (bool) atoi( it->second.c_str() );
};
};
};
};
deleteResult( result );
};
return tVal;
};
TRAIN_FILTER_DATA *sqliteDBC::getTrainFilterData( uint32_t ID ){
TRAIN_FILTER_DATA * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (TRAIN_FILTER_DATA*) malloc(sizeof(TRAIN_FILTER_DATA));
if( tVal != NULL ) {
memset( tVal, 0 , sizeof(TRAIN_FILTER_DATA));
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("UPPER_SENSOR");
if( it != cArr->end() ){
tVal->nUpperSensorID = atoi( it->second.c_str() );
};
it = cArr->find("LOWER_SENSOR");
if( it != cArr->end() ){
tVal->nBottomSensorID = atoi( it->second.c_str() );
};
it = cArr->find("TRAIN_DETECT");
if( it != cArr->end() ){
tVal->bDetectTrain = (bool)atoi( it->second.c_str() );
};
it = cArr->find("TRAIN_WIDTH");
if( it != cArr->end() ){
tVal->nTrainWidth = atoi( it->second.c_str() );
};
it = cArr->find("DECISION_TIME");
if( it != cArr->end() ){
tVal->nDecisionTime = 1000 * atoi( it->second.c_str() );
};
it = cArr->find("SUSPECT_TIME");
if( it != cArr->end() ){
tVal->nSuspectTime = 1000 * atoi( it->second.c_str() );
};
it = cArr->find("TRACK_TRAIN");
if( it != cArr->end() ){
tVal->bTrackTrain = (bool)atoi( it->second.c_str() );
};
it = cArr->find("DEBUG");
if( it != cArr->end() ){
tVal->bDebugMode = (bool)atoi( it->second.c_str() );
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_UART_DATA *sqliteDBC::getUartData( uint32_t UartID ){
DBC_UART_DATA *tVal = NULL;
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
AAssoc *result;
sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
if( tVal != NULL ) {
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("type");
if( it != cArr->end() ){
tVal->type = atoi( it->second.c_str() );
};
it = cArr->find("speed");
if( it != cArr->end() ){
tVal->speed = atoi( it->second.c_str() );
};
it = cArr->find("mode");
if( it != cArr->end() ){
tVal->mode = atoi( it->second.c_str() );
};
it = cArr->find("port");
if( it != cArr->end() ){
tVal->port = atoi( it->second.c_str() );
};
it = cArr->find("path");
if( it != cArr->end() ){
strncpy( tVal->path, it->second.c_str(), 128 );
};
};
};
};
deleteResult( result );
};
return tVal;
};
uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
uint32_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
AAssoc *result;
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc * cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
uint32_t tVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
tVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listFilters( uint32_t ServerID ){
DBC_INT_LIST * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1" );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listLRFUploads( uint32_t deviceID ){
DBC_INT_LIST * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
DBC_INT_LIST * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
switch( listMode ){
case LIST_VALID_RECORDS:
sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1 AND `nd`.`online`=1" );
break;
case LIST_INVALID_RECORDS:
sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1 AND `nd`.`online`=0" );
break;
default:
sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1" );
};
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listModules(){
DBC_INT_LIST * tVal = NULL;
if(m_bIsConnected){
AAssoc *result;
result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
DBC_INT_LIST * tVal = NULL;
char tParam[CTEXT];
//memset( tParam, 0, CTEXT );
sprintf(tParam,"0");
if( old_ids != NULL ) {
char tBuffer[CTEXT];
for(int i = 0; i< old_ids->size(); i++ ) {
sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
strncpy( tParam, tBuffer, CTEXT );
};
};
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
DBC_INT_LIST * tVal = NULL;
char tParam[CTEXT];
//memset( tParam, 0, CTEXT );
sprintf(tParam,"0");
if( old_ids != NULL ) {
char tBuffer[CTEXT];
for(int i = 0; i< old_ids->size(); i++ ) {
sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
strncpy( tParam, tBuffer, CTEXT );
};
};
if( m_bIsConnected ){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam );
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
DBC_INT_LIST * tVal = NULL;
if( m_bIsConnected ){
char tQBuf[1024];
AAssoc *result;
if( all ) {
sprintf(tQBuf, "SELECT `id` FROM `nodes`");
} else {
sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1" );
};
#ifdef DEBUG_EXTRA
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0; i <result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
DBC_INT_LIST * tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
if ( NodeID == 0 ) NodeID = m_nServerNodeID;
sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
#ifdef DEBUG
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = new DBC_INT_LIST();
if( tVal != NULL ) {
Assoc::iterator it;
for(int i=0;i<result->size();i++) {
cArr = result->at(i);
it = cArr->find("id");
if( it != cArr->end() ){
tVal->push_back( atoi( it->second.c_str() ));
};
};
};
};
};
deleteResult( result );
};
return tVal;
};
void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
if( m_bIsConnected ){
m_nSiteID = SiteID;
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
m_nServerID = getServerID( identity );
*ServID = m_nServerID;
if( m_nServerID ) {
//if ( bRecreateMemStructures )
initMemDB( m_nServerID );
sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataMemory->SilentQuery( tQBuf );
sprintf(tQBuf,"INSERT INTO `nodes`(`aux_id`,`type`,`online`,`name`) VALUES (%d, 0,1,'Controller') ON CONFLICT(`type`,`aux_id`) DO UPDATE SET `online`=1", *ServID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
if( m_nServerNodeID== 0 ){
m_nServerNodeID = getServerNode( m_nServerID );
};
*ServNode = m_nServerNodeID;
// Clenup Nodes and services ...
sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf(tQBuf,"UPDATE `cameras` SET `online`=0 WHERE `manual`=0" );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )" );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
//sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
#ifdef DEBUG_ALL
//LOG(tQBuf);
#endif
//m_pDataStorage->SilentQuery( tQBuf );
//sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
#ifdef DEBUG_ALL
//LOG(tQBuf);
#endif
//m_pDataStorage->SilentQuery( tQBuf );
// sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
#ifdef DEBUG_ALL
// LOG(tQBuf);
#endif
// m_pDataStorage->SilentQuery( tQBuf );
//cleanResources( tServerNodeID );
setNodeOnline( m_nServerNodeID );
};
};
};
bool sqliteDBC::setLRFDataStatus( uint32_t lrfID, uint8_t ST ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::setLRFDetectionProfile( uint32_t lrfID, uint8_t profileID ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::setLRFMaskStatus( uint32_t maskID, bool status ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
char S[2];
if (status ) {
S[0] = 'E';
S[1] = (char)0;
} else {
S[0] = 'D';
S[1] = (char)0;
};
sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='%s' WHERE `id`=%d", S, maskID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::setLRFAltSerial( uint32_t lrfID, CSTR Serial ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::setLRFAltVerison( uint32_t lrfID, CSTR version ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
if(m_bIsConnected){
char tQBuf[CTEXT];
if( NodeID ) {
sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
};
return false;
};
bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
if(m_bIsConnected){
char tQBuf[CTEXT];
if( NodeID ) {
sprintf(tQBuf,"UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
};
return false;
};
bool sqliteDBC::setRessourceActive( uint32_t RessourceID, bool Status ){
if(m_bIsConnected){
char tQBuf[CTEXT];
if( RessourceID ) {
sprintf(tQBuf,"UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
};
return false;
};
bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
if ( status ) {
sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
} else {
sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
};
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
void sqliteDBC::unregisterServer( void ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
};
setNodeOffline( m_nServerNodeID );
};
bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
ptr->OS_INFO,
ptr->HW_INFO,
m_nSiteID
);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::fillShadow2D( uint32_t ID, SHADOW & SH ){
if( m_bIsConnected ){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `lv`.`id`, `lm`.`layer`, `lm`.`status` FROM `lrf_mask_vertexes` `lv` LEFT JOIN `lrf_masks` `lm` ON (`lm`.`id` = `lv`.`mask_id`) WHERE `lm`.`sensor_id`=%d ORDER BY `lv`.`id`", ID );
LOG(tQBuf);
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
SH.m_nEnabled = 0xFFFFFFFF;
SH.m_nPolygonCount = result->size();
SH.pPolygones = (SHADOW_POLY *) calloc( SH.m_nPolygonCount, sizeof( SHADOW_POLY ) );
if ( SH.pPolygones ) {
Assoc * cArr;
for ( uint8_t idx = 0; idx < result->size(); idx++ ) {
Assoc::iterator it;
cArr = result->at(idx);
it = cArr->find("id");
if( it != cArr->end() ){
SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
};
it = cArr->find("layer");
if( it != cArr->end() ){
SH.pPolygones[idx].bType = atoi(it->second.c_str());
};
it = cArr->find("status");
if( it != cArr->end() ){
SH.m_nEnabled &= ( it->second.c_str()[0] == 'E' )?0xFFFFFFFF:~(1<<SH.pPolygones[idx].bType);
};
sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID );
LOG(tQBuf);
AAssoc *v_result = m_pDataStorage->Query( tQBuf );
if( v_result!= NULL ) {
if( v_result->size() > 0 ) {
SH.pPolygones[idx].wCount = v_result->size();
SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc( SH.pPolygones[idx].wCount, sizeof( HZ_POINT ) );
if(SH.pPolygones[idx].pPoint ) {
for ( uint32_t zdx =0; zdx < SH.pPolygones[idx].wCount; zdx++ ) {
cArr = v_result->at(zdx);
it = cArr->find("x");
if( it != cArr->end() ){
SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
};
it = cArr->find("y");
if( it != cArr->end() ){
SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
};
LOG("Point Added!");
};
} else {
LOG("No mem for Points!");
};
} else {
LOG("No Points!");
SH.pPolygones[idx].wCount = 0;
SH.pPolygones[idx].pPoint = NULL;
};
deleteResult( v_result );
};
};
};
};
deleteResult( result );
};
return true;
};
return false;
};
bool sqliteDBC::getAppDebugMode(){
bool rVal = false;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1" );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("work_mode");
if( it != cArr->end() ){
rVal = (bool) atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return rVal;
};
DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
DBC_CAM_DATA *tVal = NULL;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
if( tVal != NULL ) {
memset( tVal, 0 , sizeof(DBC_CAM_DATA));
tVal->id = ID;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("type");
if( it != cArr->end() ){
tVal->type = atoi( it->second.c_str() );
};
it = cArr->find("online");
if( it != cArr->end() ){
tVal->online = (bool) atoi( it->second.c_str() );
};
it = cArr->find("stream_id");
if( it != cArr->end() ){
tVal->stream_id = atoi( it->second.c_str() );
};
it = cArr->find("hardware");
if( it != cArr->end() ){
strncpy( tVal->hardware, it->second.c_str(), 32 );
};
it = cArr->find("name");
if( it != cArr->end() ){
strncpy( tVal->name, it->second.c_str(), 64 );
};
it = cArr->find("status");
if( it != cArr->end() ){
tVal->status = atoi( it->second.c_str() );
};
it = cArr->find("session");
if( it != cArr->end() ){
strncpy( tVal->session, it->second.c_str(), 32 );
};
it = cArr->find("protocol");
if( it != cArr->end() ){
tVal->protocol = atoi( it->second.c_str() );
};
it = cArr->find("protocol_aux");
if( it != cArr->end() ){
tVal->protocol_aux = atoi( it->second.c_str() );
};
it = cArr->find("uart_id");
if( it != cArr->end() ){
tVal->uart_id = atoi( it->second.c_str() );
};
it = cArr->find("x");
if( it != cArr->end() ){
tVal->x = atof( it->second.c_str() );
};
it = cArr->find("y");
if( it != cArr->end() ){
tVal->y = atof( it->second.c_str() );
};
it = cArr->find("z");
if( it != cArr->end() ){
tVal->z = atof( it->second.c_str() );
};
it = cArr->find("x_rot");
if( it != cArr->end() ){
tVal->x_rot = atof( it->second.c_str() );
};
it = cArr->find("y_rot");
if( it != cArr->end() ){
tVal->y_rot = atof( it->second.c_str() );
};
it = cArr->find("z_rot");
if( it != cArr->end() ){
tVal->z_rot = atof( it->second.c_str() );
};
it = cArr->find("imager_type");
if( it != cArr->end() ){
tVal->imager_type = atoi( it->second.c_str() );
};
it = cArr->find("lens_f");
if( it != cArr->end() ){
tVal->lens_f = atoi( it->second.c_str() );
};
it = cArr->find("ptz_speed_step");
if( it != cArr->end() ){
tVal->ptz_speed_step = atof( it->second.c_str() );
};
it = cArr->find("pan_model");
if( it != cArr->end() ){
tVal->pan_model = atoi( it->second.c_str() );
};
it = cArr->find("pan_angles");
if( it != cArr->end() ){
tVal->pan_angles = atoi( it->second.c_str() );
};
it = cArr->find("tilt_model");
if( it != cArr->end() ){
tVal->tilt_model = atoi( it->second.c_str() );
};
it = cArr->find("tilt_angles");
if( it != cArr->end() ){
tVal->tilt_angles = atoi( it->second.c_str() );
};
it = cArr->find("zoom_model");
if( it != cArr->end() ){
tVal->zoom_model = atoi( it->second.c_str() );
};
it = cArr->find("zoom");
if( it != cArr->end() ){
tVal->zoom = atof( it->second.c_str() );
};
//it = cArr->find("max_range");
//if( it != cArr->end() ){
// tVal->max_range = atof( it->second.c_str() );
//};
it = cArr->find("access_login");
if( it != cArr->end() ){
strncpy( tVal->username, it->second.c_str(), 16 );
};
it = cArr->find("access_pass");
if( it != cArr->end() ){
strncpy( tVal->password, it->second.c_str(), 16 );
};
it = cArr->find("uuid");
if( it != cArr->end() ){
strncpy( tVal->uuid, it->second.c_str(), 64 );
};
it = cArr->find("mac");
if( it != cArr->end() ){
strncpy( tVal->mac, it->second.c_str(), 16 );
};
it = cArr->find("ip");
if( it != cArr->end() ){
strncpy( tVal->ip, it->second.c_str(), 16 );
};
it = cArr->find("port");
if( it != cArr->end() ){
tVal->port = atoi( it->second.c_str() );
};
it = cArr->find("onvif_access");
if( it != cArr->end() ){
strncpy( tVal->onvif_access, it->second.c_str(), 5);
};
it = cArr->find("onvif_ctrl");
if( it != cArr->end() ){
strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
};
it = cArr->find("ptz_token");
if( it != cArr->end() ){
strncpy( tVal->ptz_token, it->second.c_str(), 64);
};
it = cArr->find("trigger_mode");
if( it != cArr->end() ){
tVal->triggering_mode = atoi( it->second.c_str() );
};
it = cArr->find("auth_mode");
if( it != cArr->end() ){
tVal->auth_mode = atoi( it->second.c_str() );
};
it = cArr->find("ptz_axel");
if( it != cArr->end() ){
tVal->ptz_axel = atoi( it->second.c_str() );
};
it = cArr->find("manual");
if( it != cArr->end() ){
tVal->manual = (bool) atoi( it->second.c_str() );
};
it = cArr->find("homing_enable");
if( it != cArr->end() ){
tVal->homing_enable = (bool) atoi( it->second.c_str() );
};
it = cArr->find("homing_to");
if( it != cArr->end() ){
tVal->homing_to = atoi( it->second.c_str() );
};
it = cArr->find("home_token");
if( it != cArr->end() ){
strncpy( tVal->home_token, it->second.c_str(), 32 );
};
};
};
};
deleteResult( result );
};
return tVal;
};
uint32_t sqliteDBC::getCamId( CSTR UUID ){
uint32_t RetVal = 0;
if(m_bIsConnected){
char tQBuf[CTEXT];
AAssoc *result;
sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
result = m_pDataStorage->Query( tQBuf );
if( result!= NULL ) {
if( result->size() > 0 ) {
Assoc *cArr;
Assoc::iterator it;
cArr = result->at(0);
it = cArr->find("id");
if( it != cArr->end() ){
RetVal = atoi( it->second.c_str() );
};
};
};
deleteResult( result );
};
return RetVal;
};
CSTR sqliteDBC::getDataParam( uint8_t pid ){
switch ( pid ) {
case 0: return strdup( m_pDbFile );
};
return NULL;
};
bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
if(m_bIsConnected) {
char tQBuf[MAX_PATH_LEN];
char tIpBuf[CTEXT];
char *tPos;
char *tPos2;
uint16_t tPort = 0;
tPos = strstr(Addr,"//");
if( tPos != NULL ) {
tPos2 = strstr( tPos + 2,":");
if( tPos2 != NULL ) {
LOG("IP Port Defined!");
int len, len2, len3;
len = tPos2 - tPos - 2;
strncpy( tIpBuf, tPos + 2, len);
tIpBuf[len] = (char)0;
tPort = atoi( tPos2 + 1 );
//strncpy( tPath, tPos + 2 + len, CTEXT );
} else {
tPos2 = strstr( tPos + 2,"/");
if( tPos2 != NULL ) {
int len, len2, len3;
len = tPos2 - tPos - 2;
strncpy( tIpBuf, tPos + 2, len);
tIpBuf[len] = (char)0;
//strncpy( tPath, tPos + 2 + len, CTEXT );
} else {
tPos2 = strstr( tPos + 2,"/");
LOG_ERROR("Pos 2 - err");
tIpBuf[0] = (char)0;
};
};
} else {
LOG_ERROR("Pos 0 - err");
tIpBuf[0] = (char)0;
};
if ( tPort ) {
sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d",
Addr,
tIpBuf,
tPort,
CamID
);
} else {
sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
Addr,
tIpBuf,
CamID
);
}
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::updateCamOnlineStatus( uint32_t camID, bool online ){
if(m_bIsConnected) {
char tQBuf[MAX_PATH_LEN];
if ( online ) {
sprintf( tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID );
} else {
sprintf( tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID );
};
m_pDataStorage->SilentQuery(tQBuf);
if ( online ) {
sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
} else {
sprintf( tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID );
};
m_pDataStorage->SilentQuery(tQBuf);
return true;
};
return false;
};
bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
if(m_bIsConnected) {
uint32_t cID = getCamId( uuid );
if ( cID ) {
return updateIPCam( cID, Addr );
};
};
return false;
};
bool sqliteDBC::updateLocalCam( CSTR uuid ){
if(m_bIsConnected) {
uint32_t cID = getCamId( uuid );
if ( cID ) {
return updateLocalCam( cID );
};
};
return false;
};
bool sqliteDBC::updateLocalCam( uint32_t CamID ){
if(m_bIsConnected) {
char tQBuf[MAX_PATH_LEN];
sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::updateLRFComStat( uint32_t lrfID, uint32_t ErrorPerMinute ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::updateLRFFrozenStatus( uint32_t lrfID, bool FFT, bool SFT ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID );
#ifdef DEBUG
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::updateLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master, uint16_t nOrder, uint16_t nOffset, uint16_t nDelta, uint16_t nWidth, uint16_t nFlip, uint16_t nSun, uint8_t bSST, uint32_t GP, uint32_t spikes ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
if( master ) {
sprintf( tQBuf, "UPDATE INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,1,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
lrfID,
profileID,
nOrder,
nOffset,
nDelta,
nFlip,
nWidth,
bSST,
nSun,
GP,
spikes,
nOrder,
nOffset,
nDelta,
nFlip,
nWidth,
bSST,
nSun,
GP,
spikes );
} else {
sprintf( tQBuf, "INSERT INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,0,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
lrfID,
profileID,
nOrder,
nOffset,
nDelta,
nFlip,
nWidth,
bSST,
nSun,
GP,
spikes,
nOrder,
nOffset,
nDelta,
nFlip,
nWidth,
bSST,
nSun,
GP,
spikes );
};
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
CSTR tES = m_pDataStorage->escapeString( Serial );
if( tES ) {
sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID );
free( tES );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
};
return false;
};
bool sqliteDBC::updateLRFUpload( uint32_t id, uint8_t status ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::updateLRFUnfinishedUploads( uint32_t deviceID ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
return false;
};
bool sqliteDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
CSTR tES = m_pDataStorage->escapeString( version );
if( tES ) {
sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID );
free( tES );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
};
return false;
};
bool sqliteDBC::updateServerIdentity( uint32_t m_nServerID, CSTR m_sIdentity ){
if( m_bIsConnected ) {
char tQBuf[MAX_PATH_LEN];
CSTR tES = m_pDataStorage->escapeString( m_sIdentity );
if( tES ) {
sprintf( tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID );
free( tES );
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
m_pDataStorage->SilentQuery( tQBuf );
return true;
};
};
return false;
};
#undef LOG
#undef LOG_ERROR