#include "rgooseDBConnector.h" #define LOG( X ) m_pLog->Log( "ADBC", 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( "ADBC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ } #else #define LOG_ERROR( X ) m_pLog->Log( "ADBC", this, _LOG_ERROR, X ); #endif argusDBC *argusDBC::_instance = NULL; /** * Returns the single instance of the object. */ argusDBC* argusDBC::getInstance( config_setting_t * Settings ) { // check if the instance has been created yet if( 0 == _instance ){ // if not, then create it _instance = new argusDBC( Settings ); }; // return the single instance return _instance; }; argusDBC::argusDBC( config_setting_t * Settings ){ m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DBC" ); m_bIsConnected = false; m_nSiteID = 0; m_pDatabase = NULL; }; argusDBC::~argusDBC(){ m_bIsConnected = false; if( m_pDatabase != NULL ) delete( m_pDatabase ); m_pDatabase = NULL; _instance = NULL; }; bool argusDBC::connect( void ){ config_setting_t * tSetting; m_bIsConnected = false; if( m_pSettings!= NULL ) { tSetting = config_setting_get_member( m_pSettings, "DATABASE_HOST" ); if( tSetting == NULL ){ tSetting = config_setting_add(m_pSettings, "DATABASE_HOST", CONFIG_TYPE_STRING ); config_setting_set_string( tSetting, "localhost" ); basicSettingsProvider::getInstance()->compromise(); LOG("No DB Host Setting! Using default!"); }; sprintf(m_pDbHost, "%s", config_setting_get_string(tSetting)); tSetting = config_setting_get_member( m_pSettings, "DATABASE_NAME" ); if( tSetting == NULL ){ tSetting = config_setting_add(m_pSettings, "DATABASE_NAME", CONFIG_TYPE_STRING ); config_setting_set_string( tSetting, "dfsl_gunter" ); basicSettingsProvider::getInstance()->compromise(); LOG("No DB Database Setting! Using default!"); }; sprintf(m_pDbDatabase, "%s", config_setting_get_string(tSetting)); tSetting = config_setting_get_member( m_pSettings, "DATABASE_USER" ); if( tSetting == NULL ){ tSetting = config_setting_add(m_pSettings, "DATABASE_USER", CONFIG_TYPE_STRING ); config_setting_set_string( tSetting, "dbroot" ); basicSettingsProvider::getInstance()->compromise(); LOG("No DB User Setting! Using default!"); }; sprintf(m_pDbUsername, "%s", config_setting_get_string(tSetting)); tSetting = config_setting_get_member( m_pSettings, "DATABASE_PASSWORD" ); if( tSetting == NULL ){ tSetting = config_setting_add(m_pSettings, "DATABASE_PASSWORD", CONFIG_TYPE_STRING ); config_setting_set_string( tSetting, "123567" ); basicSettingsProvider::getInstance()->compromise(); LOG("No DB Password Setting! Using default!"); }; sprintf(m_pDbPassword, "%s", config_setting_get_string(tSetting)); basicSettingsProvider::getInstance()->validate(); m_pDatabase = new MySQL_Connector(); if( m_pDatabase == NULL )return false; m_bIsConnected = m_pDatabase->Connect( m_pDbDatabase, m_pDbHost, m_pDbUsername, m_pDbPassword ); } else { LOG_ERROR("No Settings Provided!"); }; return m_bIsConnected; }; bool argusDBC::cleanResources( uint32_t SiteID, uint32_t ServerNodeID, bool all ){ if(m_bIsConnected) { char tQBuf[MAX_PATH_LEN]; if( all ) { sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `site_id`=%d AND `node_id`=%d", SiteID, ServerNodeID ); } else { sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `site_id`=%d AND `node_id`=%d AND ( `type`=100 OR `type`=101 OR `type`=103 OR `type`=120 )", SiteID, ServerNodeID ); }; #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::updateIPCam( uint32_t SiteID, CSTR uuid, CSTR Addr ){ if(m_bIsConnected) { char tQBuf[MAX_PATH_LEN]; char tIpBuf[CTEXT]; char *tPos; char *tPos2; #ifdef DEBUG LOG("Cam Update"); #endif 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 `uuid`='%s' AND `site_id`=%d", Addr, tIpBuf, uuid, SiteID ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::updateIPCam( uint32_t SiteID, uint32_t CamID, CSTR Addr ){ if(m_bIsConnected) { char tQBuf[MAX_PATH_LEN]; char tIpBuf[CTEXT]; char *tPos; char *tPos2; #ifdef DEBUG LOG("Cam Update"); #endif 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 AND `site_id`=%d", Addr, tIpBuf, CamID, SiteID ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); sprintf(tQBuf,"UPDATE media_streams SET `valid`=1 WHERE source_id=%d AND source_type='D'", CamID ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::addIPCam( uint32_t SiteID, resultWoWo *tPtr ){ if(m_bIsConnected) { char tQBuf[1024]; char tIpBuf[32]; char tProto[32]; char tPath[CTEXT]; char *tPos; char *tPos2; #ifdef DEBUG LOG("Add New Camera Record"); #endif 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 SET `online`=1, `uuid`='%s', `onvif_ctrl`='%s', `ip`='%s', `type`=2, `status`=0, `site_id`=%d, `mac`='%s', `hardware`='%s', `name`='%s', `onvif_access`='%s' ON DUPLICATE KEY UPDATE `online`=1, `ip`='%s'", tPtr->RefID, //tPtr->XAddrs, tPath, tIpBuf, SiteID, tPtr->MAC, tPtr->Hardware, tPtr->Name, tProto, //tPtr->XAddrs, tIpBuf ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::addIPCam( uint32_t SiteID, OBSERVER_RESULT *tPtr ){ if(m_bIsConnected) { char tQBuf[1024]; char tIpBuf[32]; char tProto[32]; char tPath[CTEXT]; char *tPos; char *tPos2; #ifdef DEBUG LOG("Add New Camera Record"); #endif 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 SET `online`=1, `uuid`='%s', `onvif_ctrl`='%s', `ip`='%s', `type`=2, `status`=0, `site_id`=%d, `mac`='%s', `hardware`='%s', `name`='%s', `onvif_access`='%s' ON DUPLICATE KEY UPDATE `online`=1, `ip`='%s'", tPtr->RefID, //tPtr->XAddrs, tPath, tIpBuf, SiteID, tPtr->MAC, tPtr->Hardware, tPtr->Name, tProto, //tPtr->XAddrs, tIpBuf ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::transferEvtToArchive( uint32_t SECONDS ){ if(m_bIsConnected) { if( SECONDS > 0 ) { char tQBuf[CTEXT]; sprintf(tQBuf,"INSERT INTO events_archive ( SELECT * FROM events_cloud ec WHERE ec.ts < ( UNIX_TIMESTAMP(NOW()) - %d ) ); DELETE FROM events_cloud WHERE ts < ( UNIX_TIMESTAMP(NOW()) - %d );", SECONDS, SECONDS ); m_pDatabase->SilentQuery( tQBuf ); return true; }; }; return false; } bool argusDBC::getLastEvents( vectorEventRecords &VR, uint32_t tss, uint32_t tsms ){ if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM events_cloud WHERE `ts`>%d AND `ts_ms`>%d", tss, tsms ); result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { DBC_EVT_DATA *tPtr; Assoc *cArr; for(int i=0;isize();i++) { cArr = result->at(i); tPtr = (DBC_EVT_DATA*)malloc(sizeof(DBC_EVT_DATA)); if( tPtr!= NULL ) { Assoc::iterator it; memset(tPtr,0,sizeof(DBC_EVT_DATA)); it = cArr->find("id"); if( it != cArr->end() ){ tPtr->id = atoi( it->second.c_str() ); }; it = cArr->find("node_id"); if( it != cArr->end() ){ tPtr->node_id = atoi( it->second.c_str() ); }; it = cArr->find("uuid"); if( it != cArr->end() ){ strncpy( tPtr->uuid, it->second.c_str(),64 ); }; it = cArr->find("ts"); if( it != cArr->end() ){ tPtr->ts_s = atoi( it->second.c_str() ); }; it = cArr->find("ts_ms"); if( it != cArr->end() ){ tPtr->ts_ms = atoi( it->second.c_str() ); }; it = cArr->find("evt_type"); if( it != cArr->end() ){ tPtr->type = atoi( it->second.c_str() ); }; it = cArr->find("range_digital"); if( it != cArr->end() ){ tPtr->range_digital = atoi( it->second.c_str() ); }; it = cArr->find("azimuth"); if( it != cArr->end() ){ tPtr->az = atof( it->second.c_str() ); }; it = cArr->find("elevation"); if( it != cArr->end() ){ tPtr->el = atof( it->second.c_str() ); }; it = cArr->find("range"); if( it != cArr->end() ){ tPtr->ra = atof( it->second.c_str() ); }; it = cArr->find("area"); if( it != cArr->end() ){ tPtr->area = atof( it->second.c_str() ); }; it = cArr->find("lat"); if( it != cArr->end() ){ tPtr->lat = atof( it->second.c_str() ); }; it = cArr->find("lon"); if( it != cArr->end() ){ tPtr->lon = atof( it->second.c_str() ); }; it = cArr->find("alt"); if( it != cArr->end() ){ tPtr->alt = atof( it->second.c_str() ); }; it = cArr->find("ref_uuid"); if( it != cArr->end() ){ strncpy( tPtr->uuid, it->second.c_str(),64 ); }; char tBuf[CLOG]; sprintf(tBuf,"Evt: %d %d %d", tPtr->type, tPtr->ts_s, tPtr->ts_ms ); LOG(tBuf); VR.push_back(tPtr); }; }; }; deleteResult( result ); if( m_pDatabase->isConnected()) return true; }; return false; }; bool argusDBC::getAllCamRecords( uint32_t SiteID, vectorCamRecords &VR ){ if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM cameras WHERE `site_id`=%d AND `type`=2 AND `status`=2", SiteID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { DBC_CAM_DATA *tVal; Assoc *cArr; for(int i=0;isize();i++) { LOG("Cam_Record!"); cArr = result->at(i); tVal = (DBC_CAM_DATA*)malloc(sizeof(DBC_CAM_DATA)); if( tVal!= NULL ) { Assoc::iterator it; memset(tVal,0,sizeof(DBC_CAM_DATA)); it = cArr->find("id"); if( it != cArr->end() ){ tVal->id = atoi( it->second.c_str() ); }; 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() ){ strncpy( tVal->protocol_aux, it->second.c_str(), 64 ); }; it = cArr->find("lat"); if( it != cArr->end() ){ tVal->lat = atof( it->second.c_str() ); }; it = cArr->find("lon"); if( it != cArr->end() ){ tVal->lon = atof( it->second.c_str() ); }; it = cArr->find("alt"); if( it != cArr->end() ){ tVal->alt = atof( it->second.c_str() ); }; it = cArr->find("picth"); if( it != cArr->end() ){ tVal->pitch = atof( it->second.c_str() ); }; it = cArr->find("roll"); if( it != cArr->end() ){ tVal->roll = atof( it->second.c_str() ); }; it = cArr->find("yaw"); if( it != cArr->end() ){ tVal->yaw = 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("min_f"); if( it != cArr->end() ){ tVal->min_f = atof( it->second.c_str() ); }; it = cArr->find("max_f"); if( it != cArr->end() ){ tVal->max_f = 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(), CLOG ); }; 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); }; VR.push_back(tVal); }; //?? //delete(cArr); }; }; deleteResult( result ); #ifdef DEBUG sprintf( tQBuf,"Now %d Camera Records in System", VR.size()); LOG(tQBuf); #endif if( m_pDatabase->isConnected()) return true; }; return false; }; DBC_INT_LIST *argusDBC::listNodes( uint32_t SiteID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT id FROM nodes WHERE `site_id`=%d AND `type` > 0", SiteID ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listNewNodes( uint32_t SiteID, 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 `site_id`=%d AND `type` > 0 AND `id` NOT IN(%s)", SiteID, tParam ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listCamMediaStreams( uint32_t CAMID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT id FROM media_streams WHERE `source_id`=%d AND `valid`=1", CAMID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();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_NODE_DATA *argusDBC::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 LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA)); if( tVal != NULL ) { Assoc::iterator it; cArr = result->at(0); it = cArr->find("id"); if( it != cArr->end() ){ tVal->node_id = atoi( it->second.c_str() ); }; it = cArr->find("type"); if( it != cArr->end() ){ tVal->type = (ARGUS_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("name"); if( it != cArr->end() ){ sprintf( tVal->name, it->second.c_str() ); }; it = cArr->find("ts_s"); if( it != cArr->end() ){ tVal->tss = atoi( it->second.c_str() ); }; it = cArr->find("ts_ms"); if( it != cArr->end() ){ tVal->tsm = atoi( it->second.c_str() ); }; it = cArr->find("online"); if( it != cArr->end() ){ tVal->online = (bool)atoi( it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_APF_DATA *argusDBC::getAPFData( uint32_t APFID ){ DBC_APF_DATA * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM `target_filter_apf` WHERE `id`=%d", APFID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_APF_DATA*) malloc(sizeof(DBC_APF_DATA)); if( tVal != NULL ) { memset( tVal, 0 , sizeof(DBC_APF_DATA)); Assoc::iterator it; cArr = result->at(0); it = cArr->find("mode"); if( it != cArr->end() ){ sprintf( tVal->mode, it->second.c_str() ); }; it = cArr->find("port"); if( it != cArr->end() ){ tVal->port = atoi( it->second.c_str() ); }; it = cArr->find("lat"); if( it != cArr->end() ){ tVal->lat = atof( it->second.c_str() ); }; it = cArr->find("lon"); if( it != cArr->end() ){ tVal->lon = atof( it->second.c_str() ); }; it = cArr->find("alt"); if( it != cArr->end() ){ tVal->alt = atof( it->second.c_str() ); }; it = cArr->find("roll"); if( it != cArr->end() ){ tVal->roll = atof( it->second.c_str() ); }; it = cArr->find("pitch"); if( it != cArr->end() ){ tVal->pitch = atof( it->second.c_str() ); }; it = cArr->find("yaw"); if( it != cArr->end() ){ tVal->yaw = atof( it->second.c_str() ); }; it = cArr->find("dx"); if( it != cArr->end() ){ tVal->dx = atof( it->second.c_str() ); }; it = cArr->find("dy"); if( it != cArr->end() ){ tVal->dy = atof( it->second.c_str() ); }; it = cArr->find("dz"); if( it != cArr->end() ){ tVal->dz = atof( it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_FH_DATA *argusDBC::getFlyHunterData( uint32_t ID ){ DBC_FH_DATA * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM `modules_fly_hunter` WHERE `id`=%d", ID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_FH_DATA*) malloc(sizeof(DBC_FH_DATA)); if( tVal != NULL ) { memset( tVal, 0 , sizeof(DBC_FH_DATA)); Assoc::iterator it; cArr = result->at(0); it = cArr->find("name"); if( it != cArr->end() ){ sprintf( tVal->name, it->second.c_str() ); }; it = cArr->find("lat"); if( it != cArr->end() ){ tVal->lat = atof( it->second.c_str() ); }; it = cArr->find("lon"); if( it != cArr->end() ){ tVal->lon = atof( it->second.c_str() ); }; it = cArr->find("alt"); if( it != cArr->end() ){ tVal->alt = atof( it->second.c_str() ); }; it = cArr->find("dc_module_id"); if( it != cArr->end() ){ tVal->dc_id = atoi( it->second.c_str() ); }; it = cArr->find("uart_id"); if( it != cArr->end() ){ tVal->uart_id = atoi( it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_SITE_DATA *argusDBC::getSiteData( uint32_t ID ){ DBC_SITE_DATA * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM sites WHERE `id`=%d", ID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_SITE_DATA*) malloc(sizeof(DBC_SITE_DATA)); if( tVal != NULL ) { memset( tVal, 0 , sizeof(DBC_SITE_DATA)); Assoc::iterator it; cArr = result->at(0); it = cArr->find("account_id"); if( it != cArr->end() ){ tVal->account_id = atoi( it->second.c_str() ); }; it = cArr->find("name"); if( it != cArr->end() ){ sprintf( tVal->name, it->second.c_str() ); }; it = cArr->find("server_ident"); if( it != cArr->end() ){ sprintf( tVal->ident, it->second.c_str() ); }; it = cArr->find("anchor_lat"); if( it != cArr->end() ){ tVal->lat = atof( it->second.c_str() ); }; it = cArr->find("anchor_lon"); if( it != cArr->end() ){ tVal->lon = atof( it->second.c_str() ); }; it = cArr->find("anchor_alt"); if( it != cArr->end() ){ tVal->alt = atof( it->second.c_str() ); }; it = cArr->find("hemisphere"); if( it != cArr->end() ){ tVal->SH = ( it->second.c_str()[0] == 'S' ); }; it = cArr->find("map_id"); if( it != cArr->end() ){ tVal->map_id = atoi( it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_TARGET_FLITER_DATA *argusDBC::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_pDatabase->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 = atoi( it->second.c_str() ); }; it = cArr->find("aux_id"); if( it != cArr->end() ){ tVal->aux_id = atoi( it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_LRF_DATA *argusDBC::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 LOG(tQBuf); #endif result = m_pDatabase->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("lat"); if( it != cArr->end() ){ tVal->lat = atof( it->second.c_str() ); }; it = cArr->find("lon"); if( it != cArr->end() ){ tVal->lon = atof( it->second.c_str() ); }; it = cArr->find("alt"); if( it != cArr->end() ){ tVal->alt = atof( it->second.c_str() ); }; it = cArr->find("pitch"); if( it != cArr->end() ){ tVal->pitch = atof( it->second.c_str() ); }; it = cArr->find("yaw"); if( it != cArr->end() ){ tVal->yaw = atof( it->second.c_str() ); }; it = cArr->find("roll"); if( it != cArr->end() ){ tVal->roll = 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("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() ); }; }; }; }; deleteResult( result ); }; return tVal; }; uint8_t argusDBC::getLRFDataStatus( uint32_t ID ){ uint8_t RetVal = 0; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM `lrf_sensors` WHERE `id`=%d", ID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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; }; uint8_t argusDBC::getLRFPushedCMD( uint32_t ID ){ uint8_t RetVal = 0; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT `pushed_cmd` FROM `lrf_sensors` WHERE `id`=%d", ID ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; Assoc::iterator it; cArr = result->at(0); it = cArr->find("pushed_cmd"); if( it != cArr->end() ){ RetVal = atoi( it->second.c_str() ); }; }; }; deleteResult( result ); sprintf(tQBuf, "UPDATE `lrf_sensors` SET `pushed_cmd`=0 WHERE `id`=%d", ID ); m_pDatabase->SilentQuery( tQBuf ); }; return RetVal; }; DBC_INT_LIST *argusDBC::listModules( uint32_t ServerID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1", ServerID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listFilters( uint32_t SiteID, uint32_t ServerID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1", ServerID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listMediaStreams( uint32_t SiteId, uint32_t MediaServerID, 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 id FROM media_server_streams WHERE `valid`=1 AND `site_id`=%d AND `media_server_id`=%d", SiteId, MediaServerID ); break; case LIST_INVALID_RECORDS: sprintf(tQBuf, "SELECT id FROM media_server_streams WHERE `valid`=0 AND `site_id`=%d AND `media_server_id`=%d", SiteId, MediaServerID ); break; default: sprintf(tQBuf, "SELECT id FROM media_server_streams WHERE `site_id`=%d AND `media_server_id`=%d", SiteId, MediaServerID ); }; #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listDetectionProfileIDs( uint32_t nodeID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT `profile_id` FROM `lrf_detection_profiles` WHERE `aux_id`=%d ", nodeID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();i++) { cArr = result->at(i); it = cArr->find("profile_id"); if( it != cArr->end() ){ tVal->push_back( atoi( it->second.c_str() )); }; }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_INT_LIST *argusDBC::listDetectionProfiles( uint32_t nodeID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT `id` FROM `lrf_detection_profiles` WHERE `aux_id`=%d ", nodeID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listSockets( uint32_t NodeID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT id FROM sockets WHERE `node_id`=%d", NodeID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listResources( uint32_t NodeID ){ DBC_INT_LIST * tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; if( NodeID ) { sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d AND `active`=1", NodeID ); } else { sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `site_id`=%d AND `active`=1", m_nSiteID ); }; #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();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 *argusDBC::listNewModules( uint32_t ServerID, 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 server_modules WHERE `server_id`=%d AND `enabled`=1 AND `id` NOT IN (%s)", ServerID, tParam ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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;isize();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; }; uint32_t argusDBC::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 LOG(tQBuf); #endif result = m_pDatabase->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_CAM_DATA *argusDBC::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 LOG(tQBuf); #endif result = m_pDatabase->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() ){ strncpy( tVal->protocol_aux, it->second.c_str(), 64 ); }; it = cArr->find("uart_id"); if( it != cArr->end() ){ tVal->uart_id = atoi( it->second.c_str() ); }; it = cArr->find("lat"); if( it != cArr->end() ){ tVal->lat = atof( it->second.c_str() ); }; it = cArr->find("lon"); if( it != cArr->end() ){ tVal->lon = atof( it->second.c_str() ); }; it = cArr->find("alt"); if( it != cArr->end() ){ tVal->alt = atof( it->second.c_str() ); }; it = cArr->find("picth"); if( it != cArr->end() ){ tVal->pitch = atof( it->second.c_str() ); }; it = cArr->find("roll"); if( it != cArr->end() ){ tVal->roll = atof( it->second.c_str() ); }; it = cArr->find("yaw"); if( it != cArr->end() ){ tVal->yaw = 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("min_f"); if( it != cArr->end() ){ tVal->min_f = atof( it->second.c_str() ); }; it = cArr->find("max_f"); if( it != cArr->end() ){ tVal->max_f = 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("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); }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_MODULE_DATA *argusDBC::getModuleData( uint32_t ID ){ DBC_MODULE_DATA *tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM server_modules WHERE `id`=%d", ID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->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_SOCKET_DATA *argusDBC::getSocketData( uint32_t ID ){ DBC_SOCKET_DATA *tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM sockets WHERE `id`=%d", ID ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_SOCKET_DATA*) malloc(sizeof(DBC_SOCKET_DATA)); if( tVal != NULL ) { Assoc::iterator it; cArr = result->at(0); it = cArr->find("id"); if( it != cArr->end() ){ tVal->socket_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("binded"); if( it != cArr->end() ){ tVal->binded = (bool)atoi( it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; */ DBC_MEDIA_STREAM_DATA *argusDBC::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 //LOG(tQBuf); #endif result = m_pDatabase->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 ) { 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("site_id"); if( it != cArr->end() ){ tVal->site_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("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; }; DBC_MEDIA_SERVER_DATA *argusDBC::getMediaServerData( uint32_t ID ){ DBC_MEDIA_SERVER_DATA *tVal = NULL; if(m_bIsConnected){ char tQBuf[CTEXT]; AAssoc *result; sprintf(tQBuf, "SELECT * FROM media_servers WHERE `id`=%d", ID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_MEDIA_SERVER_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_DATA)); if( tVal != NULL ) { Assoc::iterator it; cArr = result->at(0); tVal->id = ID; it = cArr->find("secret_key"); if( it != cArr->end() ){ sprintf( tVal->key, it->second.c_str() ); }; it = cArr->find("ip"); if( it != cArr->end() ){ sprintf( tVal->ip, it->second.c_str() ); }; it = cArr->find("REST_port"); if( it != cArr->end() ){ tVal->port = atoi( it->second.c_str() ); }; it = cArr->find("virtual_host"); if( it != cArr->end() ){ sprintf( tVal->host, it->second.c_str() ); }; it = cArr->find("recording"); if( it != cArr->end() ){ tVal->recording = (bool)atoi( it->second.c_str() ); }; it = cArr->find("local"); if( it != cArr->end() ){ tVal->local = (bool)atoi( it->second.c_str() ); }; it = cArr->find("docRoot"); if( it != cArr->end() ){ sprintf( tVal->docRoot, it->second.c_str() ); }; sprintf( tVal->name,"ZLMedia_%d", tVal->id ); }; }; }; deleteResult( result ); }; return tVal; }; DBC_MEDIA_SERVER_STREAM_DATA *argusDBC::getMediaServerStreamDataBySource( uint32_t ID ){ DBC_MEDIA_SERVER_STREAM_DATA *tVal = NULL; if(m_bIsConnected){ char tQBuf[MAX_PATH_LEN]; AAssoc *result; sprintf(tQBuf, "SELECT `id`, `media_stream_id`, `online`, `key`, `mrl` FROM media_server_streams WHERE `media_stream_id`=%d", ID ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_MEDIA_SERVER_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_STREAM_DATA)); if( tVal != NULL ) { Assoc::iterator it; cArr = result->at(0); tVal->id = ID; it = cArr->find("media_stream_id"); if( it != cArr->end() ){ tVal->media_stream_id = atoi( it->second.c_str() ); }; it = cArr->find("online"); if( it != cArr->end() ){ tVal->online = atoi( it->second.c_str() ); }; it = cArr->find("key"); if( it != cArr->end() ){ sprintf( tVal->key, it->second.c_str() ); }; it = cArr->find("mrl"); if( it != cArr->end() ){ sprintf( tVal->mrl, it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_MEDIA_SERVER_STREAM_DATA *argusDBC::getMediaServerStreamData( uint32_t ID ){ DBC_MEDIA_SERVER_STREAM_DATA *tVal = NULL; if(m_bIsConnected){ char tQBuf[MAX_PATH_LEN]; AAssoc *result; sprintf(tQBuf, "SELECT `id`, `media_stream_id`, `online`, `key`, `mrl` FROM media_server_streams WHERE `id`=%d", ID ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (DBC_MEDIA_SERVER_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_STREAM_DATA)); if( tVal != NULL ) { Assoc::iterator it; cArr = result->at(0); tVal->id = ID; it = cArr->find("media_stream_id"); if( it != cArr->end() ){ tVal->media_stream_id = atoi( it->second.c_str() ); }; it = cArr->find("online"); if( it != cArr->end() ){ tVal->online = atoi( it->second.c_str() ); }; it = cArr->find("key"); if( it != cArr->end() ){ sprintf( tVal->key, it->second.c_str() ); }; it = cArr->find("mrl"); if( it != cArr->end() ){ sprintf( tVal->mrl, it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; void argusDBC::registerServer(uint32_t SiteID, CSTR identity ){ if(m_bIsConnected){ m_nSiteID = SiteID; char tQBuf[CTEXT]; sprintf(tQBuf,"INSERT INTO servers SET `site_id`=%d, `identity`='%s', `online`=1 ON DUPLICATE KEY UPDATE `online`=1", SiteID, identity ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); // Clenup Nodes and services ... sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `site_id`=%d ", SiteID ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); sprintf(tQBuf,"UPDATE cameras SET `online`=0 WHERE `site_id`=%d ", SiteID ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); sprintf(tQBuf,"UPDATE media_streams SET `valid`=0 WHERE `site_id`=%d ", SiteID ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); sprintf(tQBuf,"UPDATE media_server_streams SET `valid`=0, `online`=0 WHERE `site_id`=%d ", SiteID ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); uint32_t sid = getServerID( identity ); if( sid ) { sprintf(tQBuf,"INSERT INTO nodes SET `site_id`=%d, `aux_id`='%d', `type`= 0, `online`=1, `name`='Server Node' ON DUPLICATE KEY UPDATE `online`=1", m_nSiteID, sid ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); cleanResources( m_nSiteID, sid ); }; }; }; void argusDBC::unregisterServer(uint32_t ServerID, uint32_t NodeID ){ if(m_bIsConnected){ char tQBuf[CTEXT]; sprintf(tQBuf,"UPDATE servers SET `online`=0 WHERE `id` = %d", ServerID ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); setNodeOffline( NodeID ); }; }; bool argusDBC::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_pDatabase->SilentQuery( tQBuf ); sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id` = %d", NodeID ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; }; return false; }; bool argusDBC::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 //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; }; return false; }; DBC_RESOURCE_DATA *argusDBC::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 //LOG(tQBuf); #endif result = m_pDatabase->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 argusDBC::getResourceID( uint32_t SiteID, 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 `site_id`=%d AND `node_id`=%d AND `type`=%d AND `aux_id`=%d", SiteID, NodeID, type, auxID ); AAssoc *result; result = m_pDatabase->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 argusDBC::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 LOG(tQBuf); #endif AAssoc *result; result = m_pDatabase->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 argusDBC::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 LOG(tQBuf); #endif AAssoc *result; result = m_pDatabase->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 argusDBC::addLocalMediaSource( uint32_t SiteID, 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 media_streams SET `site_id`=%d, `source_id`=0, source_type='A', `valid`=1, `name`='Local Frame Grabber', `token`='%s', `img_width`=%d, `img_height`=%d, `mrl_proto`='v4l2', `mrl_port`='', `picture_proto`='', mrl='%s' ON DUPLICATE KEY UPDATE id=id, `valid`=1, img_width=%d, img_height=%d", SiteID, tM5, width, height, path, width, height ); #ifdef DEBUG //LOG(tQBuf); #endif RetVal = (uint32_t) m_pDatabase->QueryID( tQBuf ); }; }; return RetVal; }; /* uint32_t argusDBC::addNodeSocket( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, uint8_t SocketMode, bool multidrop, CSTR path, CSTR name ){ uint32_t retVal = 0; if(m_bIsConnected){ char tQBuf[2048]; char tM5[33]; CSTR tMD5 = MD5String( path ); if( tMD5 != NULL ) { memcpy( tM5, tMD5, 33 ); free( tMD5 ); if( multidrop ) { sprintf(tQBuf,"INSERT INTO `sockets` SET `node_id`=%d, `type`=%d, `mode`=%d, `multidrop`=1, `md5`='%s', `path`='%s', `name`='%s', `aux_id`=%d ON DUPLICATE KEY UPDATE `id`=`id`, `name`='%s'", NodeID, SocketType, SocketMode, tM5, path, name, auxID, name ); } else { sprintf(tQBuf,"INSERT INTO `sockets` SET `node_id`=%d, `type`=%d, `mode`=%d, `multidrop`=0, `md5`='%s', `path`='%s', `name`='%s', `aux_id`=%d ON DUPLICATE KEY UPDATE `id`=`id`, `name`='%s'", NodeID, SocketType, SocketMode, tM5, path, name, auxID, name ); }; #ifdef DEBUG LOG(tQBuf); #endif //m_pDatabase->SilentQuery( tQBuf ); retVal = (uint32_t) m_pDatabase->QueryID( tQBuf ); }; }; return retVal; }; */ bool argusDBC::addNotification( uint32_t MSG_ID, uint8_t DIS_LVL, uint32_t OBJ_ID ){ if(m_bIsConnected){ char tQBuf[2048]; sprintf(tQBuf,"INSERT INTO `notifications` SET `site_id`=%d, `ts`=NOW(), `lvl`=%d, `type`=%d, `obj_id`=%d ON DUPLICATE KEY UPDATE `status`='A', `ts`=NOW()", m_nSiteID, DIS_LVL, MSG_ID, OBJ_ID ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; uint32_t argusDBC::addResource( uint32_t SiteID, uint32_t NodeID, uint8_t type, CSTR devName, uint32_t auxID ){ uint32_t RetVal = 0; if( m_bIsConnected ){ char tQBuf[2048]; CSTR tMD5 = MD5String( devName ); if( tMD5 ) { sprintf( tQBuf, "INSERT INTO `resources` SET `site_id`=%d, `node_id`=%d, `type`=%d, `md5`='%s', `aux_id`=%d, `device`='%s' ON DUPLICATE KEY UPDATE `active`=1", SiteID, NodeID, type, tMD5, auxID, devName ); free( tMD5 ); #ifdef DEBUG //LOG(tQBuf); #endif RetVal = (uint32_t) m_pDatabase->QueryID( tQBuf ); if( RetVal == 0 ) { RetVal = getResourceID( SiteID, NodeID, type, auxID ); }; }; }; return RetVal; }; /* uint32_t argusDBC::getNodeSocketID( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, CSTR path ){ uint32_t retVal = 0; if(m_bIsConnected){ char tQBuf[2048]; char tM5[33]; CSTR tMD5 = MD5String( path ); if( tMD5 != NULL ) { memcpy( tM5, tMD5, 33 ); free( tMD5 ); sprintf( tQBuf,"SELECT id FROM `sockets` WHERE `node_id`=%d AND `type`=%d AND `md5`='%s' AND `aux_id`=%d", NodeID, SocketType, tM5, auxID ); #ifdef DEBUG LOG(tQBuf); #endif AAssoc *result; result = m_pDatabase->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 argusDBC::getNodeIP4( uint32_t NodeID ){ return getResourceDevice( NodeID, ARGUS_HARDWARE_NETWORK_IPV4 ); }; CSTR argusDBC::getNodeIP6( uint32_t NodeID ){ return getResourceDevice( NodeID, ARGUS_HARDWARE_NETWORK_IPV6 ); }; CSTR argusDBC::getResourceDevice( uint32_t NodeID, uint32_t Type ){ if(m_bIsConnected){ char tQBuf[CTEXT]; sprintf( tQBuf, "SELECT `device` FROM `resources` WHERE `node_id`=%d AND `type`=%d LIMIT 1", NodeID, Type ); #ifdef DEBUG LOG(tQBuf); #endif AAssoc *result; result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc * cArr; Assoc::iterator it; cArr = result->at(0); it = cArr->find("path"); if( it != cArr->end() ){ strncpy( tQBuf, it->second.c_str(), CTEXT ); }; }; }; deleteResult( result ); return strdup(tQBuf); }; return NULL; }; /* CSTR argusDBC::getSocketName( uint32_t NodeID, uint32_t Type ){ if(m_bIsConnected){ char tQBuf[CTEXT]; sprintf( tQBuf, "SELECT `name` FROM `sockets` WHERE `node_id`=%d AND `type`=%d LIMIT 1", NodeID, Type ); #ifdef DEBUG LOG(tQBuf); #endif AAssoc *result; result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc * cArr; Assoc::iterator it; cArr = result->at(0); it = cArr->find("name"); if( it != cArr->end() ){ strncpy( tQBuf, it->second.c_str(), CTEXT ); }; }; }; deleteResult( result ); return strdup(tQBuf); }; return NULL; }; */ KAFKA_SETTINGS *argusDBC::getSettingsKAFKA( uint32_t ModuleID ){ KAFKA_SETTINGS *tVal = NULL; if( m_bIsConnected ) { char tQBuf[CTEXT]; AAssoc *result; sprintf( tQBuf, "SELECT brocker_address, brocker_port FROM modules_kafka WHERE module_id = %d", ModuleID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (KAFKA_SETTINGS*) malloc(sizeof(KAFKA_SETTINGS)); if( tVal != NULL ) { Assoc::iterator it; cArr = result->at(0); it = cArr->find("brocker_address"); if( it != cArr->end() ){ strncpy( tVal->brocker_addr, it->second.c_str(), 255 ); }; it = cArr->find("brocker_port"); if( it != cArr->end() ){ tVal->brocker_port = atoi( it->second.c_str() ); }; }; }; }; deleteResult( result ); }; return tVal; }; KAFKA_TEMPLATE *argusDBC::getKafkaTemplateForEvent( uint32_t ModuleID, uint32_t ClassID, uint32_t EventID ){ KAFKA_TEMPLATE *tVal = NULL; if( m_bIsConnected ) { char tQBuf[CTEXT]; AAssoc *result; sprintf( tQBuf, "SELECT topic, template FROM module_kafka_settings WHERE module_id=%d AND class_id=%d AND event_id=%d", ModuleID, ClassID, EventID ); #ifdef DEBUG LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tVal = (KAFKA_TEMPLATE*) malloc(sizeof(KAFKA_TEMPLATE)); if( tVal != NULL ) { Assoc::iterator it; cArr = result->at(0); it = cArr->find("topic"); if( it != cArr->end() ){ memset( tVal->topic, 0, 128 ); strncpy( tVal->topic , it->second.c_str(), 127 ); }; it = cArr->find("template"); if( it != cArr->end() ){ tVal->tpl = (CSTR)malloc( it->second.length() + 1 ); if( tVal->tpl!= NULL) { memset( tVal->tpl, 0, it->second.length() + 1 ); strncpy( tVal->tpl, it->second.c_str(), it->second.length() ); }; }; }; }; }; deleteResult( result ); }; return tVal; }; DBC_UART_DATA *argusDBC::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 //LOG(tQBuf); #endif result = m_pDatabase->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 argusDBC::getUartID( uint32_t RessourceID ){ uint32_t RetVal = 0; if(m_bIsConnected){ char tQBuf[CTEXT]; sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `resource_id`=%d", RessourceID ); AAssoc *result; result = m_pDatabase->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; }; bool argusDBC::updateMediaServerStream( uint32_t id, CSTR mrl, CSTR key ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; #ifdef DEBUG LOG("Stream Update"); #endif if( key == NULL ) { if( mrl == NULL ) { sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', mrl='', `valid`=0 WHERE `id`=%d", id ); } else { sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', `mrl`='%s', `valid`=1 WHERE `id`=%d", mrl, id ); }; } else { sprintf( tQBuf,"UPDATE media_server_streams SET `online`=1, `key`='%s', `mrl`='%s', `valid`=1 WHERE `id`=%d", key, mrl, id ); }; #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::updateResourceAUX( uint32_t ResID, uint32_t AUX ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; sprintf( tQBuf, "UPDATE `resources` SET `aux_id`=%d WHERE `id`=%d", AUX, ResID ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; uint32_t argusDBC::addLocalDCB( uint32_t SiteID, CSTR PATH, CSTR SERIAL, uint8_t CNT, uint32_t status ){ uint32_t RetVal = 0; if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; sprintf( tQBuf, "INSERT INTO modules_dcr SET site_id=%d, status=%d, channels=%d, serial_id='%s' ON DUPLICATE KEY UPDATE status=%d, channels=%d", SiteID, status, CNT, SERIAL, status, CNT ); #ifdef DEBUG //LOG(tQBuf); #endif uint64_t cid = m_pDatabase->QueryID( tQBuf ); if( cid == 0 ) { AAssoc *result; sprintf( tQBuf, "SELECT id FROM modules_dcr WHERE site_id=%d AND serial_id='%s'", SiteID, SERIAL ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->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() ){ cid = atoi( it->second.c_str() ); }; }; deleteResult( result ); }; }; if( cid!= 0 ) { for( uint8_t idx = 0; idx < CNT; idx++ ) { if( status & ( 1 << idx ) ){ sprintf( tQBuf, "INSERT INTO module_dcr_chanels SET dcr_id=%d, channel_id=%d, mode='O', status='C' ON DUPLICATE KEY UPDATE status='C'", (uint32_t) cid, idx ); } else { sprintf( tQBuf, "INSERT INTO module_dcr_chanels SET dcr_id=%d, channel_id=%d, mode='O', status='O' ON DUPLICATE KEY UPDATE status='O'", (uint32_t) cid, idx ); } #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); }; }; }; return RetVal; }; uint32_t argusDBC::addLocalUART( uint32_t ResID, 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` SET `resource_id`=%d, `type`=%d, `speed`=%d, `bit`=%d, `parity`='%c', `stop_bit`=%.1f, `mode`=%d, `port`=%d, `path`='%s' ON DUPLICATE KEY UPDATE `id`=`id`", ResID, utype, speed, bit, parity, stop_bit, mode, port, PATH ); #ifdef DEBUG //LOG(tQBuf); #endif RetVal = m_pDatabase->QueryID( tQBuf ); if( RetVal == 0 ) { RetVal = getUartID( ResID ); }; }; return RetVal; }; bool argusDBC::addDraw( uint32_t DBID, uint16_t SIZE, CSTR ID ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; sprintf( tQBuf,"INSERT INTO `lrf_draw` SET `node_id`=%d,`ts`=NOW(),`md5`='%s',`size`=%d ON DUPLICATE KEY UPDATE `ts`=NOW(),`md5`='%s',`size`=%d, `rendered`='N'", DBID, ID, SIZE, ID, SIZE ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; uint32_t argusDBC::addEvent( uint32_t SRC, CSTR UUID, uint32_t tss, uint32_t tsms, uint8_t evt_type, uint16_t dig_r, float AZ, float EL, float RANGE, float AREA, float lat, float lon, float alt, uint32_t RefID, CSTR RefUUID ){ uint32_t RetVal = 0; if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; if( RefUUID == NULL ) { sprintf( tQBuf,"INSERT INTO events_cloud SET node_id=%d, uuid='%s', ts=%d, ts_ms=%d, evt_type=%d, range_digital=%d, azimuth=%0.6f, elevation=%0.6f, `range`=%0.2f, area=%0.3f, lat=%0.6f, lon=%0.6f, alt=%0.3f", SRC, UUID, tss, tsms, evt_type, dig_r, AZ, EL, RANGE, AREA, lat, lon, alt ); } else { sprintf( tQBuf,"INSERT INTO events_cloud SET node_id=%d, uuid='%s', ts=%d, ts_ms=%d, evt_type=%d, range_digital=%d, azimuth=%0.6f, elevation=%0.6f, `range`=%0.2f, area=%0.3f, lat=%0.6f, lon=%0.6f, alt=%0.3f, ref_uuid='%s'", SRC, UUID, tss, tsms, evt_type, dig_r, AZ, EL, RANGE, AREA, lat, lon, alt, RefUUID ); }; #ifdef DEBUG // LOG(tQBuf); #endif RetVal = (uint32_t) m_pDatabase->QueryID( tQBuf ); }; return RetVal; }; bool argusDBC::addVideoEvent( uint32_t evtID, uint32_t videoID ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; sprintf( tQBuf, "INSERT INTO `events_video_records` SET `evt_id`=%d, `video_id`=%d", evtID, videoID ); m_pDatabase->SilentQuery( tQBuf ); return true; }; return true; }; uint32_t argusDBC::addVideoRecord( uint32_t streamID, CSTR md5, uint32_t partID ){ uint32_t tID = 0; if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; sprintf( tQBuf, "INSERT INTO `video_records` SET `media_stream_id`=%d, `ts`=NOW(), `md5`='%s', `part_id`=%d ", streamID, md5, partID ); tID = (uint32_t) m_pDatabase->QueryID( tQBuf ); } return tID; }; DBC_KRNL_CMD *argusDBC::getActiveCommand( uint32_t SiteID ){ DBC_KRNL_CMD *tPtr = NULL; if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; AAssoc *result; sprintf( tQBuf, "SELECT * FROM `cmd_queue` WHERE `status`=0 AND `site_id`=%d", SiteID ); result = m_pDatabase->Query( tQBuf ); if( result != NULL ) { if( result->size() > 0 ) { Assoc *cArr; tPtr = (DBC_KRNL_CMD*)malloc(sizeof(DBC_KRNL_CMD)); if( tPtr!= NULL ) { Assoc::iterator it; memset(tPtr,0,sizeof(DBC_KRNL_CMD)); cArr = result->at(0); it = cArr->find("id"); if( it != cArr->end() ){ tPtr->id = atoi( it->second.c_str() ); }; it = cArr->find("cmd"); if( it != cArr->end() ){ tPtr->cmd = atoi( it->second.c_str() ); }; it = cArr->find("param"); if( it != cArr->end() ){ strncpy( tPtr->param, it->second.c_str(), 255 ); }; it = cArr->find("aux_param"); if( it != cArr->end() ){ strncpy( tPtr->aux_param, it->second.c_str(), 255 ); }; }; }; }; deleteResult( result ); }; return tPtr; }; bool argusDBC::updateCommand( uint32_t CMD_ID, uint8_t STATUS ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; sprintf( tQBuf, "UPDATE `cmd_queue` SET `status`=%d WHERE id=%d", STATUS, CMD_ID ); m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::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 `lrf_detection_profiles` SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=b'1'", nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, lrfID, profileID ); } else { sprintf( tQBuf, "UPDATE `lrf_detection_profiles` SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=b'0'", nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, lrfID, profileID ); }; #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; CSTR tES = m_pDatabase->escapeString( Serial ); if( tES ) { sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID ); free( tES ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; }; return false; }; bool argusDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; CSTR tES = m_pDatabase->escapeString( version ); if( tES ) { sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID ); free( tES ); #ifdef DEBUG LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; }; return false; }; bool argusDBC::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_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::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 LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::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 LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::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 LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; bool argusDBC::updateAFPBox( uint32_t filterID, CSTR Tag, float x, float y, float z ){ if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; sprintf( tQBuf, "INSERT INTO `target_filter_apf_boxes` SET `filter_id`=%d, `tag`='%s', `ts`=NOW(), `x`=%0.3f, `y`=%0.3f, `z`=%0.3f ON DUPLICATE KEY UPDATE `ts`=NOW(), `x`=%0.3f, `y`=%0.3f, `z`=%0.3f", filterID, Tag, x,y,z, x,y,z ); #ifdef DEBUG //LOG(tQBuf); #endif m_pDatabase->SilentQuery( tQBuf ); return true; }; return false; }; DBC_EVT_DATA *argusDBC::getEventByUUID( CSTR UUID ){ DBC_EVT_DATA *tPtr = NULL; if( m_bIsConnected ) { char tQBuf[MAX_PATH_LEN]; AAssoc *result; sprintf( tQBuf, "SELECT * FROM events_cloud WHERE uuid = '%s'", UUID ); #ifdef DEBUG //LOG(tQBuf); #endif result = m_pDatabase->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; tPtr = (DBC_EVT_DATA*)malloc(sizeof(DBC_EVT_DATA)); if( tPtr!= NULL ) { Assoc::iterator it; memset(tPtr,0,sizeof(DBC_EVT_DATA)); cArr = result->at(0); it = cArr->find("id"); if( it != cArr->end() ){ tPtr->id = atoi( it->second.c_str() ); }; it = cArr->find("node_id"); if( it != cArr->end() ){ tPtr->node_id = atoi( it->second.c_str() ); }; it = cArr->find("uuid"); if( it != cArr->end() ){ strncpy( tPtr->uuid, it->second.c_str(),64 ); }; it = cArr->find("ts"); if( it != cArr->end() ){ tPtr->ts_s = atoi( it->second.c_str() ); }; it = cArr->find("ts_ms"); if( it != cArr->end() ){ tPtr->ts_ms = atoi( it->second.c_str() ); }; it = cArr->find("evt_type"); if( it != cArr->end() ){ tPtr->type = atoi( it->second.c_str() ); }; it = cArr->find("range_digital"); if( it != cArr->end() ){ tPtr->range_digital = atoi( it->second.c_str() ); }; it = cArr->find("azimuth"); if( it != cArr->end() ){ tPtr->az = atof( it->second.c_str() ); }; it = cArr->find("elevation"); if( it != cArr->end() ){ tPtr->el = atof( it->second.c_str() ); }; it = cArr->find("range"); if( it != cArr->end() ){ tPtr->ra = atof( it->second.c_str() ); }; it = cArr->find("area"); if( it != cArr->end() ){ tPtr->area = atof( it->second.c_str() ); }; it = cArr->find("lat"); if( it != cArr->end() ){ tPtr->lat = atof( it->second.c_str() ); }; it = cArr->find("lon"); if( it != cArr->end() ){ tPtr->lon = atof( it->second.c_str() ); }; it = cArr->find("alt"); if( it != cArr->end() ){ tPtr->alt = atof( it->second.c_str() ); }; it = cArr->find("ref_id"); if( it != cArr->end() ){ tPtr->ref_id = atoi( it->second.c_str() ); }; it = cArr->find("ref_uuid"); if( it != cArr->end() ){ strncpy( tPtr->ref_uuid, it->second.c_str(),64 ); }; }; }; }; deleteResult( result ); }; return tPtr; }; #undef LOG #undef LOG_ERROR