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.
1683 lines
59 KiB
1683 lines
59 KiB
#include "SQLite_dataBinder.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_nServerID = 1;
|
|
};
|
|
|
|
sqliteDBC::~sqliteDBC(){
|
|
if ( m_pDataStorage ) delete( m_pDataStorage );
|
|
if ( m_pDataMemory ) delete( m_pDataMemory );
|
|
_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, "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 );
|
|
|
|
#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 );
|
|
//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_bIsConnected = true;
|
|
return m_bIsConnected;
|
|
};
|
|
|
|
void sqliteDBC::initMemDB( uint32_t sid ){
|
|
m_pDataMemory->Query("DROP TABLE \"nodes_qcq\"");
|
|
m_pDataMemory->Query("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->Query("DROP TABLE \"servers\"");
|
|
m_pDataMemory->Query("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 0.0,\
|
|
\"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->Query( 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 );
|
|
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;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
m_pDataMemory->deleteResult( result );
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
|
|
if(m_bIsConnected) {
|
|
char tQBuf[MAX_PATH_LEN];
|
|
char tIpBuf[CTEXT];
|
|
char *tPos;
|
|
char *tPos2;
|
|
|
|
tPos = strstr(Addr,"//");
|
|
if( tPos != NULL ) {
|
|
tPos2 = strstr( tPos + 2,"/");
|
|
if( tPos2 != NULL ) {
|
|
int len;
|
|
len = tPos2 - tPos - 2;
|
|
strncpy(tIpBuf,tPos + 2,len);
|
|
tIpBuf[len] = (char)0;
|
|
} else {
|
|
LOG_ERROR("Pos 2 - err");
|
|
tIpBuf[0] = (char)0;
|
|
};
|
|
} else {
|
|
LOG_ERROR("Pos 0 - err");
|
|
tIpBuf[0] = (char)0;
|
|
};
|
|
sprintf(tQBuf,"UPDATE cameras SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
|
|
Addr,
|
|
tIpBuf,
|
|
CamID
|
|
);
|
|
#ifdef DEBUG_ALL
|
|
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::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::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;
|
|
};
|
|
|
|
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 ){
|
|
return false;
|
|
};
|
|
|
|
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;
|
|
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 ) {
|
|
int len,len2,len3;
|
|
len = tPos2 - tPos - 2;
|
|
strncpy( tIpBuf, tPos + 2, len);
|
|
tIpBuf[len] = (char)0;
|
|
strncpy( tPath, tPos + 2 + len, CTEXT );
|
|
} else {
|
|
LOG_ERROR("Pos 2 - err");
|
|
tIpBuf[0] = (char)0;
|
|
};
|
|
} else {
|
|
LOG_ERROR("Pos 0 - err");
|
|
tIpBuf[0] = (char)0;
|
|
};
|
|
sprintf(tQBuf,"INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',2,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_ALL
|
|
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 ){
|
|
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',0,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`) VALUES(%d,'A',1,'%s','%s',%d,%d,'v4l2','%s','v4l2') ON CONFLICT(`source_id`,`token`) DO UPDATE SET `valid`=1, img_width=%d, img_height=%d",
|
|
camID, name, tM5, width, height, path, width, height );
|
|
#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::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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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_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;
|
|
}
|
|
|
|
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::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 `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" );
|
|
};
|
|
#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" );
|
|
#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 );
|
|
|
|
// 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::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_ALL
|
|
LOG(tQBuf);
|
|
#endif
|
|
m_pDataStorage->SilentQuery( tQBuf );
|
|
sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
|
|
#ifdef DEBUG_ALL
|
|
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 ){
|
|
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;
|
|
};
|
|
|
|
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("site_id");
|
|
if( it != cArr->end() ){
|
|
tVal->site_id = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("node_id");
|
|
if( it != cArr->end() ){
|
|
tVal->node_id = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("online");
|
|
if( it != cArr->end() ){
|
|
tVal->online = (bool) atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("type");
|
|
if( it != cArr->end() ){
|
|
tVal->type = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("name");
|
|
if( it != cArr->end() ){
|
|
strncpy( tVal->name, it->second.c_str(), 64 );
|
|
};
|
|
it = cArr->find("hardware");
|
|
if( it != cArr->end() ){
|
|
strncpy( tVal->hardware, it->second.c_str(), 32 );
|
|
};
|
|
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() );
|
|
};
|
|
};
|
|
};
|
|
};
|
|
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;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|