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.

895 lines
34 KiB

8 years ago
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
8 years ago
​
​
​
​
8 years ago
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
8 years ago
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
  1. #include "rgooseDBConnector.h"
  2. #define LOG( X ) m_pLog->Log( "ADBC", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "ADBC", this, _LOG_ERROR, X )
  4. ARGUS_DBC::ARGUS_DBC( config_setting_t * Settings ){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DBC" );
  7. m_bIsConnected = false;
  8. m_pDatabase = NULL;
  9. };
  10. ARGUS_DBC::~ARGUS_DBC(){
  11. };
  12. bool ARGUS_DBC::connect( void ){
  13. config_setting_t * tSetting;
  14. m_bIsConnected = false;
  15. if( m_pSettings!= NULL ) {
  16. tSetting = config_setting_get_member( m_pSettings, "DATABASE_HOST" );
  17. if( tSetting == NULL ){
  18. tSetting = config_setting_add(m_pSettings, "DATABASE_HOST", CONFIG_TYPE_STRING );
  19. config_setting_set_string( tSetting, "localhost" );
  20. basicSettingsProvider::getInstance()->compromise();
  21. LOG("No DB Host Setting! Using default!");
  22. };
  23. sprintf(m_pDbHost, "%s", config_setting_get_string(tSetting));
  24. tSetting = config_setting_get_member( m_pSettings, "DATABASE_NAME" );
  25. if( tSetting == NULL ){
  26. tSetting = config_setting_add(m_pSettings, "DATABASE_NAME", CONFIG_TYPE_STRING );
  27. config_setting_set_string( tSetting, "dfsl_gunter" );
  28. basicSettingsProvider::getInstance()->compromise();
  29. LOG("No DB Database Setting! Using default!");
  30. };
  31. sprintf(m_pDbDatabase, "%s", config_setting_get_string(tSetting));
  32. tSetting = config_setting_get_member( m_pSettings, "DATABASE_USER" );
  33. if( tSetting == NULL ){
  34. tSetting = config_setting_add(m_pSettings, "DATABASE_USER", CONFIG_TYPE_STRING );
  35. config_setting_set_string( tSetting, "dbroot" );
  36. basicSettingsProvider::getInstance()->compromise();
  37. LOG("No DB User Setting! Using default!");
  38. };
  39. sprintf(m_pDbUsername, "%s", config_setting_get_string(tSetting));
  40. tSetting = config_setting_get_member( m_pSettings, "DATABASE_PASSWORD" );
  41. if( tSetting == NULL ){
  42. tSetting = config_setting_add(m_pSettings, "DATABASE_PASSWORD", CONFIG_TYPE_STRING );
  43. config_setting_set_string( tSetting, "123567" );
  44. basicSettingsProvider::getInstance()->compromise();
  45. LOG("No DB Password Setting! Using default!");
  46. };
  47. sprintf(m_pDbPassword, "%s", config_setting_get_string(tSetting));
  48. basicSettingsProvider::getInstance()->validate();
  49. m_pDatabase = new MySQL_Connector();
  50. if( m_pDatabase == NULL )return false;
  51. m_bIsConnected = m_pDatabase->Connect( m_pDbDatabase, m_pDbHost, m_pDbUsername, m_pDbPassword );
  52. } else {
  53. LOG_ERROR("No Settings Provided!");
  54. };
  55. return m_bIsConnected;
  56. };
  57. bool ARGUS_DBC::updateIPCam( uint32_t SiteID, CSTR uuid, CSTR Addr ){
  58. if(m_bIsConnected) {
  59. char tQBuf[MAX_PATH_LEN];
  60. char tIpBuf[CTEXT];
  61. char *tPos;
  62. char *tPos2;
  63. LOG("Cam Update");
  64. tPos = strstr(Addr,"//");
  65. if( tPos != NULL ) {
  66. tPos2 = strstr( tPos + 2,"/");
  67. if( tPos2 != NULL ) {
  68. int len;
  69. len = tPos2 - tPos - 2;
  70. strncpy(tIpBuf,tPos + 2,len);
  71. tIpBuf[len] = (char)0;
  72. } else {
  73. LOG("Pos 2 - err");
  74. tIpBuf[0] = (char)0;
  75. };
  76. } else {
  77. LOG("Pos 0 - err");
  78. tIpBuf[0] = (char)0;
  79. };
  80. sprintf(tQBuf,"INSERT INTO cameras SET `uuid`='%s', `onvif_ctrl`='%s', `ip`='%s', `type`=2, `status`=0, `site_id`=%d ON DUPLICATE KEY UPDATE `onvif_ctrl`='%s', `ip`='%s'",
  81. uuid,
  82. Addr,
  83. tIpBuf,
  84. SiteID,
  85. Addr,
  86. tIpBuf
  87. );
  88. LOG(tQBuf);
  89. m_pDatabase->SilentQuery( tQBuf );
  90. return true;
  91. };
  92. return false;
  93. };
  94. bool ARGUS_DBC::updateIPCam( uint32_t SiteID, resultWoWo *tPtr ){
  95. if(m_bIsConnected) {
  96. char tQBuf[MAX_PATH_LEN];
  97. char tIpBuf[CTEXT];
  98. char *tPos;
  99. char *tPos2;
  100. LOG("Cam Update WoWo");
  101. tPos = strstr(tPtr->XAddrs,"//");
  102. if( tPos != NULL ) {
  103. tPos2 = strstr( tPos + 2,"/");
  104. if( tPos2 != NULL ) {
  105. int len;
  106. len = tPos2 - tPos - 2;
  107. strncpy(tIpBuf,tPos + 2,len);
  108. tIpBuf[len] = (char)0;
  109. } else {
  110. LOG("Pos 2 - err");
  111. tIpBuf[0] = (char)0;
  112. };
  113. } else {
  114. LOG("Pos 0 - err");
  115. tIpBuf[0] = (char)0;
  116. };
  117. sprintf(tQBuf,"INSERT INTO cameras SET `uuid`='%s', `onvif_ctrl`='%s', `ip`='%s', `type`=2, `status`=0, `site_id`=%d, `mac`='%s', `hardware`='%s', `name`='%s' ON DUPLICATE KEY UPDATE `onvif_ctrl`='%s', `ip`='%s'",
  118. tPtr->RefID,
  119. tPtr->XAddrs,
  120. tIpBuf,
  121. SiteID,
  122. tPtr->MAC,
  123. tPtr->Hardware,
  124. tPtr->Name,
  125. tPtr->XAddrs,
  126. tIpBuf
  127. );
  128. LOG(tQBuf);
  129. m_pDatabase->SilentQuery( tQBuf );
  130. return true;
  131. };
  132. return false;
  133. };
  134. bool ARGUS_DBC::transferEvtToArchive( uint32_t SECONDS ){
  135. if(m_bIsConnected) {
  136. if( SECONDS > 0 ) {
  137. char tQBuf[CTEXT];
  138. 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 );
  139. m_pDatabase->SilentQuery( tQBuf );
  140. return true;
  141. };
  142. };
  143. return false;
  144. }
  145. bool ARGUS_DBC::getLastEvents( vectorEventRecords &VR, uint32_t tss, uint32_t tsms ){
  146. if(m_bIsConnected){
  147. char tQBuf[CTEXT];
  148. AAssoc *result;
  149. sprintf(tQBuf, "SELECT * FROM events_cloud WHERE `ts`>%d AND `ts_ms`>%d", tss, tsms );
  150. result = m_pDatabase->Query( tQBuf );
  151. if( result!= NULL ) {
  152. EventRecordDBC *tPtr;
  153. Assoc *cArr;
  154. for(int i=0;i<result->size();i++) {
  155. cArr = result->at(i);
  156. tPtr = (EventRecordDBC*)malloc(sizeof(EventRecordDBC));
  157. if( tPtr!= NULL ) {
  158. Assoc::iterator it;
  159. memset(tPtr,0,sizeof(EventRecordDBC));
  160. it = cArr->find("ts");
  161. if( it != cArr->end() ){
  162. tPtr->ts_s = atoi( it->second.c_str() );
  163. };
  164. it = cArr->find("ts_ms");
  165. if( it != cArr->end() ){
  166. tPtr->ts_ms = atoi( it->second.c_str() );
  167. };
  168. it = cArr->find("evt_type");
  169. if( it != cArr->end() ){
  170. tPtr->type = atoi( it->second.c_str() );
  171. };
  172. char tBuf[CLOG];
  173. sprintf(tBuf,"Evt: %d %d %d", tPtr->type, tPtr->ts_s, tPtr->ts_ms );
  174. LOG(tBuf);
  175. VR.push_back(tPtr);
  176. };
  177. delete(cArr);
  178. };
  179. //result->clear();
  180. //delete(result);
  181. };
  182. deleteResult( result );
  183. if( m_pDatabase->isConnected()) return true;
  184. };
  185. return false;
  186. };
  187. bool ARGUS_DBC::getAllCamRecords( uint32_t SiteID, vectorCamRecords &VR ){
  188. if(m_bIsConnected){
  189. char tQBuf[CTEXT];
  190. AAssoc *result;
  191. sprintf(tQBuf, "SELECT * FROM cameras WHERE `site_id`=%d AND `type`=2 AND `status`=2", SiteID );
  192. #ifdef DEBUG
  193. LOG(tQBuf);
  194. #endif
  195. result = m_pDatabase->Query( tQBuf );
  196. if( result!= NULL ) {
  197. DBC_CAM_DATA *tVal;
  198. Assoc *cArr;
  199. for(int i=0;i<result->size();i++) {
  200. LOG("Cam_Record!");
  201. cArr = result->at(i);
  202. tVal = (DBC_CAM_DATA*)malloc(sizeof(DBC_CAM_DATA));
  203. if( tVal!= NULL ) {
  204. Assoc::iterator it;
  205. memset(tVal,0,sizeof(DBC_CAM_DATA));
  206. it = cArr->find("id");
  207. if( it != cArr->end() ){
  208. tVal->id = atoi( it->second.c_str() );
  209. };
  210. it = cArr->find("enabled");
  211. if( it != cArr->end() ){
  212. tVal->enabled = (bool) atoi( it->second.c_str() );
  213. };
  214. it = cArr->find("online");
  215. if( it != cArr->end() ){
  216. tVal->online = (bool) atoi( it->second.c_str() );
  217. };
  218. it = cArr->find("type");
  219. if( it != cArr->end() ){
  220. tVal->type = atoi( it->second.c_str() );
  221. };
  222. it = cArr->find("caps");
  223. if( it != cArr->end() ){
  224. tVal->caps = atoi( it->second.c_str() );
  225. };
  226. it = cArr->find("status");
  227. if( it != cArr->end() ){
  228. tVal->status = atoi( it->second.c_str() );
  229. };
  230. it = cArr->find("protocol");
  231. if( it != cArr->end() ){
  232. tVal->protocol = atoi( it->second.c_str() );
  233. };
  234. it = cArr->find("lat");
  235. if( it != cArr->end() ){
  236. tVal->lat = atof( it->second.c_str() );
  237. };
  238. it = cArr->find("lon");
  239. if( it != cArr->end() ){
  240. tVal->lon = atof( it->second.c_str() );
  241. };
  242. it = cArr->find("alt");
  243. if( it != cArr->end() ){
  244. tVal->alt = atof( it->second.c_str() );
  245. };
  246. it = cArr->find("picth");
  247. if( it != cArr->end() ){
  248. tVal->pitch = atof( it->second.c_str() );
  249. };
  250. it = cArr->find("roll");
  251. if( it != cArr->end() ){
  252. tVal->roll = atof( it->second.c_str() );
  253. };
  254. it = cArr->find("yaw");
  255. if( it != cArr->end() ){
  256. tVal->yaw = atof( it->second.c_str() );
  257. };
  258. it = cArr->find("min_f");
  259. if( it != cArr->end() ){
  260. tVal->min_f = atof( it->second.c_str() );
  261. };
  262. it = cArr->find("max_f");
  263. if( it != cArr->end() ){
  264. tVal->max_f = atof( it->second.c_str() );
  265. };
  266. it = cArr->find("imager_size");
  267. if( it != cArr->end() ){
  268. tVal->imager_size = atof( it->second.c_str() );
  269. };
  270. it = cArr->find("imager_ratio");
  271. if( it != cArr->end() ){
  272. tVal->imager_ratio = atof( it->second.c_str() );
  273. };
  274. it = cArr->find("uuid");
  275. if( it != cArr->end() ){
  276. strncpy( tVal->uuid, it->second.c_str(), CLOG );
  277. };
  278. it = cArr->find("port");
  279. if( it != cArr->end() ){
  280. strncpy( tVal->port, it->second.c_str(), 5 );
  281. };
  282. it = cArr->find("ip");
  283. if( it != cArr->end() ){
  284. strncpy( tVal->ip, it->second.c_str(), 16 );
  285. };
  286. it = cArr->find("hardware");
  287. if( it != cArr->end() ){
  288. strncpy( tVal->hardware, it->second.c_str(), 16 );
  289. };
  290. it = cArr->find("session");
  291. if( it != cArr->end() ){
  292. strncpy( tVal->session, it->second.c_str(), 16 );
  293. };
  294. it = cArr->find("access_login");
  295. if( it != cArr->end() ){
  296. strncpy( tVal->username, it->second.c_str(), 16 );
  297. };
  298. it = cArr->find("access_pass");
  299. if( it != cArr->end() ){
  300. strncpy( tVal->password, it->second.c_str(), 16 );
  301. };
  302. it = cArr->find("name");
  303. if( it != cArr->end() ){
  304. strncpy( tVal->name, it->second.c_str(), 16 );
  305. };
  306. it = cArr->find("onvif_ctrl");
  307. if( it != cArr->end() ){
  308. strncpy( tVal->onvif_ctrl, it->second.c_str(), 16 );
  309. };
  310. VR.push_back(tVal);
  311. };
  312. delete(cArr);
  313. };
  314. //result->clear();
  315. //delete(result);
  316. };
  317. deleteResult( result );
  318. sprintf( tQBuf,"Now %d Camera Records in System", VR.size());
  319. LOG(tQBuf);
  320. if( m_pDatabase->isConnected()) return true;
  321. };
  322. return false;
  323. };
  324. DBC_INT_LIST *ARGUS_DBC::listNodes( uint32_t SiteID ){
  325. DBC_INT_LIST * tVal = NULL;
  326. if(m_bIsConnected){
  327. char tQBuf[CTEXT];
  328. AAssoc *result;
  329. sprintf(tQBuf, "SELECT id FROM nodes WHERE `site_id`=%d AND `type` > 0", SiteID );
  330. #ifdef DEBUG
  331. LOG(tQBuf);
  332. #endif
  333. result = m_pDatabase->Query( tQBuf );
  334. if( result!= NULL ) {
  335. if( result->size() > 0 ) {
  336. Assoc *cArr;
  337. tVal = new DBC_INT_LIST();
  338. if( tVal != NULL ) {
  339. Assoc::iterator it;
  340. for(int i=0;i<result->size();i++) {
  341. cArr = result->at(i);
  342. it = cArr->find("id");
  343. if( it != cArr->end() ){
  344. tVal->push_back( atoi( it->second.c_str() ));
  345. };
  346. };
  347. };
  348. };
  349. };
  350. deleteResult( result );
  351. };
  352. return tVal;
  353. };
  354. DBC_INT_LIST *ARGUS_DBC::listNewNodes( uint32_t SiteID, DBC_INT_LIST *old_ids ){
  355. DBC_INT_LIST * tVal = NULL;
  356. char tParam[CTEXT];
  357. //memset( tParam, 0, CTEXT );
  358. sprintf(tParam,"0");
  359. if( old_ids != NULL ) {
  360. char tBuffer[CTEXT];
  361. for(int i = 0; i< old_ids->size(); i++ ) {
  362. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  363. strncpy( tParam, tBuffer, CTEXT );
  364. };
  365. };
  366. if( m_bIsConnected ){
  367. char tQBuf[CTEXT];
  368. AAssoc *result;
  369. sprintf(tQBuf, "SELECT id FROM nodes WHERE `site_id`=%d AND `type` > 0 AND `id` NOT IN(%s)", SiteID, tParam );
  370. #ifdef DEBUG
  371. LOG(tQBuf);
  372. #endif
  373. result = m_pDatabase->Query( tQBuf );
  374. if( result!= NULL ) {
  375. if( result->size() > 0 ) {
  376. Assoc *cArr;
  377. tVal = new DBC_INT_LIST();
  378. if( tVal != NULL ) {
  379. Assoc::iterator it;
  380. for(int i=0;i<result->size();i++) {
  381. cArr = result->at(i);
  382. it = cArr->find("id");
  383. if( it != cArr->end() ){
  384. tVal->push_back( atoi( it->second.c_str() ));
  385. };
  386. };
  387. };
  388. };
  389. };
  390. deleteResult( result );
  391. };
  392. return tVal;
  393. };
  394. DBC_NODE_DATA *ARGUS_DBC::getNodeData( uint32_t ID ){
  395. DBC_NODE_DATA * tVal = NULL;
  396. if(m_bIsConnected){
  397. char tQBuf[CTEXT];
  398. AAssoc *result;
  399. sprintf(tQBuf, "SELECT * FROM nodes WHERE `id`=%d", ID );
  400. #ifdef DEBUG
  401. LOG(tQBuf);
  402. #endif
  403. result = m_pDatabase->Query( tQBuf );
  404. if( result!= NULL ) {
  405. if( result->size() > 0 ) {
  406. Assoc *cArr;
  407. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  408. if( tVal != NULL ) {
  409. Assoc::iterator it;
  410. cArr = result->at(0);
  411. it = cArr->find("id");
  412. if( it != cArr->end() ){
  413. tVal->node_id = atoi( it->second.c_str() );
  414. };
  415. it = cArr->find("type");
  416. if( it != cArr->end() ){
  417. tVal->type = (ARGUS_NODE_TYPE) atoi( it->second.c_str() );
  418. };
  419. it = cArr->find("aux_id");
  420. if( it != cArr->end() ){
  421. tVal->aux_id = atoi( it->second.c_str() );
  422. };
  423. it = cArr->find("name");
  424. if( it != cArr->end() ){
  425. sprintf( tVal->name, it->second.c_str() );
  426. };
  427. it = cArr->find("ts_s");
  428. if( it != cArr->end() ){
  429. tVal->tss = atoi( it->second.c_str() );
  430. };
  431. it = cArr->find("ts_ms");
  432. if( it != cArr->end() ){
  433. tVal->tsm = atoi( it->second.c_str() );
  434. };
  435. it = cArr->find("online");
  436. if( it != cArr->end() ){
  437. tVal->online = (bool)atoi( it->second.c_str() );
  438. };
  439. };
  440. };
  441. };
  442. deleteResult( result );
  443. };
  444. return tVal;
  445. };
  446. DBC_INT_LIST *ARGUS_DBC::listModules( uint32_t ServerID ){
  447. DBC_INT_LIST * tVal = NULL;
  448. if(m_bIsConnected){
  449. char tQBuf[CTEXT];
  450. AAssoc *result;
  451. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d", ServerID );
  452. #ifdef DEBUG
  453. //LOG(tQBuf);
  454. #endif
  455. result = m_pDatabase->Query( tQBuf );
  456. if( result!= NULL ) {
  457. if( result->size() > 0 ) {
  458. Assoc *cArr;
  459. tVal = new DBC_INT_LIST();
  460. if( tVal != NULL ) {
  461. Assoc::iterator it;
  462. for(int i=0;i<result->size();i++) {
  463. cArr = result->at(i);
  464. it = cArr->find("id");
  465. if( it != cArr->end() ){
  466. tVal->push_back( atoi( it->second.c_str() ));
  467. };
  468. };
  469. };
  470. };
  471. };
  472. deleteResult( result );
  473. };
  474. return tVal;
  475. };
  476. DBC_INT_LIST *ARGUS_DBC::listNewModules( uint32_t ServerID, DBC_INT_LIST * old_ids ){
  477. DBC_INT_LIST * tVal = NULL;
  478. char tParam[CTEXT];
  479. //memset( tParam, 0, CTEXT );
  480. sprintf(tParam,"0");
  481. if( old_ids != NULL ) {
  482. char tBuffer[CTEXT];
  483. for(int i = 0; i< old_ids->size(); i++ ) {
  484. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  485. strncpy( tParam, tBuffer, CTEXT );
  486. };
  487. };
  488. if(m_bIsConnected){
  489. char tQBuf[CTEXT];
  490. AAssoc *result;
  491. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `id` NOT IN (%s)", ServerID, tParam );
  492. #ifdef DEBUG
  493. //LOG(tQBuf);
  494. #endif
  495. result = m_pDatabase->Query( tQBuf );
  496. if( result!= NULL ) {
  497. if( result->size() > 0 ) {
  498. Assoc *cArr;
  499. tVal = new DBC_INT_LIST();
  500. if( tVal != NULL ) {
  501. Assoc::iterator it;
  502. for(int i=0;i<result->size();i++) {
  503. cArr = result->at(i);
  504. it = cArr->find("id");
  505. if( it != cArr->end() ){
  506. tVal->push_back( atoi( it->second.c_str() ));
  507. };
  508. };
  509. };
  510. };
  511. };
  512. deleteResult( result );
  513. };
  514. return tVal;
  515. };
  516. DBC_CAM_DATA *ARGUS_DBC::getCamData( uint32_t ID ){
  517. DBC_CAM_DATA *tVal = NULL;
  518. if(m_bIsConnected){
  519. char tQBuf[CTEXT];
  520. AAssoc *result;
  521. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  522. #ifdef DEBUG
  523. LOG(tQBuf);
  524. #endif
  525. result = m_pDatabase->Query( tQBuf );
  526. if( result!= NULL ) {
  527. if( result->size() > 0 ) {
  528. Assoc *cArr;
  529. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  530. if( tVal != NULL ) {
  531. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  532. tVal->id = ID;
  533. Assoc::iterator it;
  534. cArr = result->at(0);
  535. it = cArr->find("enabled");
  536. if( it != cArr->end() ){
  537. tVal->enabled = (bool) atoi( it->second.c_str() );
  538. };
  539. it = cArr->find("online");
  540. if( it != cArr->end() ){
  541. tVal->online = (bool) atoi( it->second.c_str() );
  542. };
  543. it = cArr->find("type");
  544. if( it != cArr->end() ){
  545. tVal->type = atoi( it->second.c_str() );
  546. };
  547. it = cArr->find("caps");
  548. if( it != cArr->end() ){
  549. tVal->caps = atoi( it->second.c_str() );
  550. };
  551. it = cArr->find("status");
  552. if( it != cArr->end() ){
  553. tVal->status = atoi( it->second.c_str() );
  554. };
  555. it = cArr->find("protocol");
  556. if( it != cArr->end() ){
  557. tVal->protocol = atoi( it->second.c_str() );
  558. };
  559. it = cArr->find("lat");
  560. if( it != cArr->end() ){
  561. tVal->lat = atof( it->second.c_str() );
  562. };
  563. it = cArr->find("lon");
  564. if( it != cArr->end() ){
  565. tVal->lon = atof( it->second.c_str() );
  566. };
  567. it = cArr->find("alt");
  568. if( it != cArr->end() ){
  569. tVal->alt = atof( it->second.c_str() );
  570. };
  571. it = cArr->find("picth");
  572. if( it != cArr->end() ){
  573. tVal->pitch = atof( it->second.c_str() );
  574. };
  575. it = cArr->find("roll");
  576. if( it != cArr->end() ){
  577. tVal->roll = atof( it->second.c_str() );
  578. };
  579. it = cArr->find("yaw");
  580. if( it != cArr->end() ){
  581. tVal->yaw = atof( it->second.c_str() );
  582. };
  583. it = cArr->find("min_f");
  584. if( it != cArr->end() ){
  585. tVal->min_f = atof( it->second.c_str() );
  586. };
  587. it = cArr->find("max_f");
  588. if( it != cArr->end() ){
  589. tVal->max_f = atof( it->second.c_str() );
  590. };
  591. it = cArr->find("imager_size");
  592. if( it != cArr->end() ){
  593. tVal->imager_size = atof( it->second.c_str() );
  594. };
  595. it = cArr->find("imager_ratio");
  596. if( it != cArr->end() ){
  597. tVal->imager_ratio = atof( it->second.c_str() );
  598. };
  599. it = cArr->find("uuid");
  600. if( it != cArr->end() ){
  601. strncpy( tVal->uuid, it->second.c_str(), CLOG );
  602. };
  603. it = cArr->find("port");
  604. if( it != cArr->end() ){
  605. strncpy( tVal->port, it->second.c_str(), 5 );
  606. };
  607. it = cArr->find("ip");
  608. if( it != cArr->end() ){
  609. strncpy( tVal->ip, it->second.c_str(), 16 );
  610. };
  611. it = cArr->find("hardware");
  612. if( it != cArr->end() ){
  613. strncpy( tVal->hardware, it->second.c_str(), 16 );
  614. };
  615. it = cArr->find("session");
  616. if( it != cArr->end() ){
  617. strncpy( tVal->session, it->second.c_str(), 16 );
  618. };
  619. it = cArr->find("access_login");
  620. if( it != cArr->end() ){
  621. strncpy( tVal->username, it->second.c_str(), 16 );
  622. };
  623. it = cArr->find("access_pass");
  624. if( it != cArr->end() ){
  625. strncpy( tVal->password, it->second.c_str(), 16 );
  626. };
  627. it = cArr->find("name");
  628. if( it != cArr->end() ){
  629. strncpy( tVal->name, it->second.c_str(), 16 );
  630. };
  631. it = cArr->find("onvif_ctrl");
  632. if( it != cArr->end() ){
  633. strncpy( tVal->onvif_ctrl, it->second.c_str(), 16 );
  634. };
  635. };
  636. };
  637. };
  638. deleteResult( result );
  639. };
  640. return tVal;
  641. };
  642. DBC_MODULE_DATA *ARGUS_DBC::getModuleData( uint32_t ID ){
  643. DBC_MODULE_DATA *tVal = NULL;
  644. if(m_bIsConnected){
  645. char tQBuf[CTEXT];
  646. AAssoc *result;
  647. sprintf(tQBuf, "SELECT * FROM server_modules WHERE `id`=%d", ID );
  648. #ifdef DEBUG
  649. LOG(tQBuf);
  650. #endif
  651. result = m_pDatabase->Query( tQBuf );
  652. if( result!= NULL ) {
  653. if( result->size() > 0 ) {
  654. Assoc *cArr;
  655. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  656. if( tVal != NULL ) {
  657. Assoc::iterator it;
  658. cArr = result->at(0);
  659. it = cArr->find("id");
  660. if( it != cArr->end() ){
  661. tVal->module_id = atoi( it->second.c_str() );
  662. };
  663. it = cArr->find("aux_id");
  664. if( it != cArr->end() ){
  665. tVal->aux_id = atoi( it->second.c_str() );
  666. };
  667. it = cArr->find("type");
  668. if( it != cArr->end() ){
  669. tVal->type = atoi( it->second.c_str() );
  670. };
  671. it = cArr->find("enabled");
  672. if( it != cArr->end() ){
  673. tVal->enabled = (bool)atoi( it->second.c_str() );
  674. };
  675. };
  676. //result->clear();
  677. };
  678. //delete( result );
  679. };
  680. deleteResult( result );
  681. };
  682. return tVal;
  683. };
  684. DBC_MEDIA_SERVER_DATA *ARGUS_DBC::getMediaServerData( uint32_t ID ){
  685. DBC_MEDIA_SERVER_DATA *tVal = NULL;
  686. if(m_bIsConnected){
  687. char tQBuf[CTEXT];
  688. AAssoc *result;
  689. sprintf(tQBuf, "SELECT * FROM media_servers WHERE `id`=%d", ID );
  690. #ifdef DEBUG
  691. LOG(tQBuf);
  692. #endif
  693. result = m_pDatabase->Query( tQBuf );
  694. if( result!= NULL ) {
  695. if( result->size() > 0 ) {
  696. Assoc *cArr;
  697. tVal = (DBC_MEDIA_SERVER_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_DATA));
  698. if( tVal != NULL ) {
  699. Assoc::iterator it;
  700. cArr = result->at(0);
  701. it = cArr->find("id");
  702. if( it != cArr->end() ){
  703. tVal->id = atoi( it->second.c_str() );
  704. };
  705. it = cArr->find("secret_key");
  706. if( it != cArr->end() ){
  707. sprintf( tVal->key, it->second.c_str() );
  708. };
  709. it = cArr->find("ip");
  710. if( it != cArr->end() ){
  711. sprintf( tVal->ip, it->second.c_str() );
  712. };
  713. it = cArr->find("REST_port");
  714. if( it != cArr->end() ){
  715. tVal->port = atoi( it->second.c_str() );
  716. };
  717. it = cArr->find("virtual_host");
  718. if( it != cArr->end() ){
  719. sprintf( tVal->host, it->second.c_str() );
  720. };
  721. it = cArr->find("recording");
  722. if( it != cArr->end() ){
  723. tVal->recording = (bool)atoi( it->second.c_str() );
  724. };
  725. sprintf( tVal->name,"ZLMedia_%d", tVal->id );
  726. };
  727. //result->clear();
  728. };
  729. //delete( result );
  730. };
  731. deleteResult( result );
  732. };
  733. return tVal;
  734. };
  735. void ARGUS_DBC::registerServer(uint32_t SiteID, CSTR identity ){
  736. if(m_bIsConnected){
  737. char tQBuf[CTEXT];
  738. sprintf(tQBuf,"INSERT INTO servers SET `site_id`=%d, `identity`='%s', `online`=1 ON DUPLICATE KEY UPDATE `online`=1", SiteID, identity );
  739. #ifdef DEBUG
  740. LOG(tQBuf);
  741. #endif
  742. m_pDatabase->SilentQuery( tQBuf );
  743. uint32_t sid = getServerID( identity );
  744. if( sid ) {
  745. 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", SiteID, sid );
  746. #ifdef DEBUG
  747. LOG(tQBuf);
  748. #endif
  749. m_pDatabase->SilentQuery( tQBuf );
  750. };
  751. };
  752. };
  753. void ARGUS_DBC::unregisterServer(uint32_t ServerID, uint32_t NodeID ){
  754. if(m_bIsConnected){
  755. char tQBuf[CTEXT];
  756. sprintf(tQBuf,"UPDATE servers SET `online`=0 WHERE `id` = %d", ServerID );
  757. #ifdef DEBUG
  758. LOG(tQBuf);
  759. #endif
  760. m_pDatabase->SilentQuery( tQBuf );
  761. setNodeOffline( NodeID );
  762. };
  763. };
  764. void ARGUS_DBC::setNodeOnline( uint32_t NodeID ){
  765. if(m_bIsConnected){
  766. char tQBuf[CTEXT];
  767. if( NodeID ) {
  768. sprintf(tQBuf,"UPDATE nodes SET `online`=1 WHERE `id` = %d", NodeID );
  769. #ifdef DEBUG
  770. LOG(tQBuf);
  771. #endif
  772. m_pDatabase->SilentQuery( tQBuf );
  773. sprintf(tQBuf,"UPDATE sockets SET `state`=1 WHERE `node_id` = %d", NodeID );
  774. #ifdef DEBUG
  775. LOG(tQBuf);
  776. #endif
  777. m_pDatabase->SilentQuery( tQBuf );
  778. };
  779. };
  780. };
  781. void ARGUS_DBC::setNodeOffline( uint32_t NodeID ){
  782. if(m_bIsConnected){
  783. char tQBuf[CTEXT];
  784. if( NodeID ) {
  785. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  786. #ifdef DEBUG
  787. LOG(tQBuf);
  788. #endif
  789. m_pDatabase->SilentQuery( tQBuf );
  790. sprintf(tQBuf,"UPDATE sockets SET `state`=0 WHERE `node_id` = %d", NodeID );
  791. #ifdef DEBUG
  792. LOG(tQBuf);
  793. #endif
  794. m_pDatabase->SilentQuery( tQBuf );
  795. };
  796. };
  797. };
  798. uint32_t ARGUS_DBC::getServerID( CSTR identity ){
  799. uint32_t RetVal = 0;
  800. if(m_bIsConnected){
  801. char tQBuf[CTEXT];
  802. sprintf(tQBuf,"SELECT id FROM servers WHERE `identity`='%s'", identity );
  803. #ifdef DEBUG
  804. LOG(tQBuf);
  805. #endif
  806. AAssoc *result;
  807. result = m_pDatabase->Query( tQBuf );
  808. if( result!= NULL ) {
  809. if( result->size() > 0 ) {
  810. Assoc * cArr;
  811. Assoc::iterator it;
  812. cArr = result->at(0);
  813. it = cArr->find("id");
  814. if( it != cArr->end() ){
  815. RetVal = atoi( it->second.c_str() );
  816. };
  817. //result->clear();
  818. };
  819. //delete( result );
  820. };
  821. deleteResult( result );
  822. };
  823. return RetVal;
  824. };
  825. uint32_t ARGUS_DBC::getServerNode( uint32_t SID ){
  826. uint32_t RetVal = 0;
  827. if(m_bIsConnected){
  828. char tQBuf[CTEXT];
  829. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  830. #ifdef DEBUG
  831. LOG(tQBuf);
  832. #endif
  833. AAssoc *result;
  834. result = m_pDatabase->Query( tQBuf );
  835. if( result!= NULL ) {
  836. if( result->size() > 0 ) {
  837. Assoc * cArr;
  838. Assoc::iterator it;
  839. cArr = result->at(0);
  840. it = cArr->find("id");
  841. if( it != cArr->end() ){
  842. RetVal = atoi( it->second.c_str() );
  843. };
  844. //result->clear();
  845. };
  846. //delete( result );
  847. };
  848. deleteResult( result );
  849. };
  850. return RetVal;
  851. };
  852. void ARGUS_DBC::addNodeSocket( uint32_t NodeID, uint8_t SocketType, CSTR path, CSTR name ){
  853. if(m_bIsConnected){
  854. char tQBuf[CTEXT];
  855. CSTR tMD5;
  856. tMD5 = MD5String( path );
  857. sprintf(tQBuf,"INSERT INTO sockets SET `node_id`=%d, `type`=%d, `md5`='%s', `path`='%s', `state`=1, `name`='%s' ON DUPLICATE KEY UPDATE `state`=1, `name`='%s'",
  858. NodeID, SocketType, tMD5, path, name, name );
  859. #ifdef DEBUG
  860. LOG(tQBuf);
  861. #endif
  862. free( tMD5 );
  863. m_pDatabase->SilentQuery( tQBuf );
  864. };
  865. };
  866. #undef LOG
  867. #undef LOG_ERROR