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.

1519 lines
56 KiB

7 years ago
​
6 years ago
7 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
6 years ago
7 years ago
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
6 years ago
​
6 years ago
​
6 years ago
6 years ago
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
6 years ago
​
​
​
​
​
6 years ago
​
​
6 years ago
6 years ago
7 years ago
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
7 years ago
​
7 years ago
​
​
​
​
6 years ago
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
7 years ago
6 years ago
7 years ago
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
​
​
​
6 years ago
6 years ago
7 years ago
​
​
​
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
​
7 years ago
6 years ago
​
7 years ago
​
6 years ago
7 years ago
​
6 years ago
7 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
6 years ago
6 years ago
​
​
6 years ago
6 years ago
6 years ago
​
​
​
​
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
6 years ago
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
6 years ago
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "rgooseDBConnector.h"
  2. #define LOG( X ) m_pLog->Log( "ADBC", this, X )
  3. #ifdef DEBUG
  4. #define LOG_ERROR( X ) {\
  5. char _tBfrErrMSG2w34[256];\
  6. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  7. m_pLog->Log( "ADBC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  8. }
  9. #else
  10. #define LOG_ERROR( X ) m_pLog->Log( "ADBC", this, _LOG_ERROR, X );
  11. #endif
  12. argusDBC *argusDBC::_instance = NULL;
  13. /**
  14. * Returns the single instance of the object.
  15. */
  16. argusDBC* argusDBC::getInstance( config_setting_t * Settings ) {
  17. // check if the instance has been created yet
  18. if( 0 == _instance ){
  19. // if not, then create it
  20. _instance = new argusDBC( Settings );
  21. };
  22. // return the single instance
  23. return _instance;
  24. };
  25. argusDBC::argusDBC( config_setting_t * Settings ){
  26. m_pLog = basicLogCollection::getInstance();
  27. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DBC" );
  28. m_bIsConnected = false;
  29. m_pDatabase = NULL;
  30. };
  31. argusDBC::~argusDBC(){
  32. };
  33. bool argusDBC::connect( void ){
  34. config_setting_t * tSetting;
  35. m_bIsConnected = false;
  36. if( m_pSettings!= NULL ) {
  37. tSetting = config_setting_get_member( m_pSettings, "DATABASE_HOST" );
  38. if( tSetting == NULL ){
  39. tSetting = config_setting_add(m_pSettings, "DATABASE_HOST", CONFIG_TYPE_STRING );
  40. config_setting_set_string( tSetting, "localhost" );
  41. basicSettingsProvider::getInstance()->compromise();
  42. LOG("No DB Host Setting! Using default!");
  43. };
  44. sprintf(m_pDbHost, "%s", config_setting_get_string(tSetting));
  45. tSetting = config_setting_get_member( m_pSettings, "DATABASE_NAME" );
  46. if( tSetting == NULL ){
  47. tSetting = config_setting_add(m_pSettings, "DATABASE_NAME", CONFIG_TYPE_STRING );
  48. config_setting_set_string( tSetting, "dfsl_gunter" );
  49. basicSettingsProvider::getInstance()->compromise();
  50. LOG("No DB Database Setting! Using default!");
  51. };
  52. sprintf(m_pDbDatabase, "%s", config_setting_get_string(tSetting));
  53. tSetting = config_setting_get_member( m_pSettings, "DATABASE_USER" );
  54. if( tSetting == NULL ){
  55. tSetting = config_setting_add(m_pSettings, "DATABASE_USER", CONFIG_TYPE_STRING );
  56. config_setting_set_string( tSetting, "dbroot" );
  57. basicSettingsProvider::getInstance()->compromise();
  58. LOG("No DB User Setting! Using default!");
  59. };
  60. sprintf(m_pDbUsername, "%s", config_setting_get_string(tSetting));
  61. tSetting = config_setting_get_member( m_pSettings, "DATABASE_PASSWORD" );
  62. if( tSetting == NULL ){
  63. tSetting = config_setting_add(m_pSettings, "DATABASE_PASSWORD", CONFIG_TYPE_STRING );
  64. config_setting_set_string( tSetting, "123567" );
  65. basicSettingsProvider::getInstance()->compromise();
  66. LOG("No DB Password Setting! Using default!");
  67. };
  68. sprintf(m_pDbPassword, "%s", config_setting_get_string(tSetting));
  69. basicSettingsProvider::getInstance()->validate();
  70. m_pDatabase = new MySQL_Connector();
  71. if( m_pDatabase == NULL )return false;
  72. m_bIsConnected = m_pDatabase->Connect( m_pDbDatabase, m_pDbHost, m_pDbUsername, m_pDbPassword );
  73. } else {
  74. LOG_ERROR("No Settings Provided!");
  75. };
  76. return m_bIsConnected;
  77. };
  78. bool argusDBC::updateIPCam( uint32_t SiteID, CSTR uuid, CSTR Addr ){
  79. if(m_bIsConnected) {
  80. char tQBuf[MAX_PATH_LEN];
  81. char tIpBuf[CTEXT];
  82. char *tPos;
  83. char *tPos2;
  84. #ifdef DEBUG
  85. LOG("Cam Update");
  86. #endif
  87. tPos = strstr(Addr,"//");
  88. if( tPos != NULL ) {
  89. tPos2 = strstr( tPos + 2,"/");
  90. if( tPos2 != NULL ) {
  91. int len;
  92. len = tPos2 - tPos - 2;
  93. strncpy(tIpBuf,tPos + 2,len);
  94. tIpBuf[len] = (char)0;
  95. } else {
  96. LOG_ERROR("Pos 2 - err");
  97. tIpBuf[0] = (char)0;
  98. };
  99. } else {
  100. LOG_ERROR("Pos 0 - err");
  101. tIpBuf[0] = (char)0;
  102. };
  103. sprintf(tQBuf,"UPDATE cameras SET `online`=1,`onvif_ctrl`='%s', `ip`='%s' WHERE `uuid`='%s' AND `site_id`=%d",
  104. Addr,
  105. tIpBuf,
  106. uuid,
  107. SiteID
  108. );
  109. #ifdef DEBUG
  110. LOG(tQBuf);
  111. #endif
  112. m_pDatabase->SilentQuery( tQBuf );
  113. return true;
  114. };
  115. return false;
  116. };
  117. bool argusDBC::updateIPCam( uint32_t SiteID, uint32_t CamID, CSTR Addr ){
  118. if(m_bIsConnected) {
  119. char tQBuf[MAX_PATH_LEN];
  120. char tIpBuf[CTEXT];
  121. char *tPos;
  122. char *tPos2;
  123. #ifdef DEBUG
  124. LOG("Cam Update");
  125. #endif
  126. tPos = strstr(Addr,"//");
  127. if( tPos != NULL ) {
  128. tPos2 = strstr( tPos + 2,"/");
  129. if( tPos2 != NULL ) {
  130. int len;
  131. len = tPos2 - tPos - 2;
  132. strncpy(tIpBuf,tPos + 2,len);
  133. tIpBuf[len] = (char)0;
  134. } else {
  135. LOG_ERROR("Pos 2 - err");
  136. tIpBuf[0] = (char)0;
  137. };
  138. } else {
  139. LOG_ERROR("Pos 0 - err");
  140. tIpBuf[0] = (char)0;
  141. };
  142. sprintf(tQBuf,"UPDATE cameras SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE id=%d AND `site_id`=%d",
  143. Addr,
  144. tIpBuf,
  145. CamID,
  146. SiteID
  147. );
  148. #ifdef DEBUG
  149. LOG(tQBuf);
  150. #endif
  151. m_pDatabase->SilentQuery( tQBuf );
  152. sprintf(tQBuf,"UPDATE media_streams SET `valid`=1 WHERE source_id=%d AND source_type='D'", CamID );
  153. #ifdef DEBUG
  154. LOG(tQBuf);
  155. #endif
  156. m_pDatabase->SilentQuery( tQBuf );
  157. return true;
  158. };
  159. return false;
  160. };
  161. bool argusDBC::addIPCam( uint32_t SiteID, resultWoWo *tPtr ){
  162. if(m_bIsConnected) {
  163. char tQBuf[1024];
  164. char tIpBuf[32];
  165. char tProto[32];
  166. char tPath[CTEXT];
  167. char *tPos;
  168. char *tPos2;
  169. #ifdef DEBUG
  170. LOG("Add New Camera Record");
  171. #endif
  172. memcpy( tProto, tPtr->XAddrs, 5 );
  173. tProto[5] = (char)0;
  174. if( tProto[4] == ':') tProto[4] = (char)0;
  175. memset( tPath, 0, CTEXT );
  176. tPos = strstr(tPtr->XAddrs,"//");
  177. if( tPos != NULL ) {
  178. tPos2 = strstr( tPos + 2,"/");
  179. if( tPos2 != NULL ) {
  180. int len,len2,len3;
  181. len = tPos2 - tPos - 2;
  182. strncpy( tIpBuf, tPos + 2, len);
  183. tIpBuf[len] = (char)0;
  184. strncpy( tPath, tPos + 2 + len, CTEXT );
  185. } else {
  186. LOG_ERROR("Pos 2 - err");
  187. tIpBuf[0] = (char)0;
  188. };
  189. } else {
  190. LOG_ERROR("Pos 0 - err");
  191. tIpBuf[0] = (char)0;
  192. };
  193. 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'",
  194. tPtr->RefID,
  195. //tPtr->XAddrs,
  196. tPath,
  197. tIpBuf,
  198. SiteID,
  199. tPtr->MAC,
  200. tPtr->Hardware,
  201. tPtr->Name,
  202. tProto,
  203. //tPtr->XAddrs,
  204. tIpBuf
  205. );
  206. #ifdef DEBUG
  207. LOG(tQBuf);
  208. #endif
  209. m_pDatabase->SilentQuery( tQBuf );
  210. return true;
  211. };
  212. return false;
  213. };
  214. bool argusDBC::transferEvtToArchive( uint32_t SECONDS ){
  215. if(m_bIsConnected) {
  216. if( SECONDS > 0 ) {
  217. char tQBuf[CTEXT];
  218. 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 );
  219. m_pDatabase->SilentQuery( tQBuf );
  220. return true;
  221. };
  222. };
  223. return false;
  224. }
  225. bool argusDBC::getLastEvents( vectorEventRecords &VR, uint32_t tss, uint32_t tsms ){
  226. if(m_bIsConnected){
  227. char tQBuf[CTEXT];
  228. AAssoc *result;
  229. sprintf(tQBuf, "SELECT * FROM events_cloud WHERE `ts`>%d AND `ts_ms`>%d", tss, tsms );
  230. result = m_pDatabase->Query( tQBuf );
  231. if( result!= NULL ) {
  232. EventRecordDBC *tPtr;
  233. Assoc *cArr;
  234. for(int i=0;i<result->size();i++) {
  235. cArr = result->at(i);
  236. tPtr = (EventRecordDBC*)malloc(sizeof(EventRecordDBC));
  237. if( tPtr!= NULL ) {
  238. Assoc::iterator it;
  239. memset(tPtr,0,sizeof(EventRecordDBC));
  240. it = cArr->find("ts");
  241. if( it != cArr->end() ){
  242. tPtr->ts_s = atoi( it->second.c_str() );
  243. };
  244. it = cArr->find("ts_ms");
  245. if( it != cArr->end() ){
  246. tPtr->ts_ms = atoi( it->second.c_str() );
  247. };
  248. it = cArr->find("evt_type");
  249. if( it != cArr->end() ){
  250. tPtr->type = atoi( it->second.c_str() );
  251. };
  252. char tBuf[CLOG];
  253. sprintf(tBuf,"Evt: %d %d %d", tPtr->type, tPtr->ts_s, tPtr->ts_ms );
  254. LOG(tBuf);
  255. VR.push_back(tPtr);
  256. };
  257. //??
  258. //delete(cArr);
  259. };
  260. };
  261. deleteResult( result );
  262. if( m_pDatabase->isConnected()) return true;
  263. };
  264. return false;
  265. };
  266. bool argusDBC::getAllCamRecords( uint32_t SiteID, vectorCamRecords &VR ){
  267. if(m_bIsConnected){
  268. char tQBuf[CTEXT];
  269. AAssoc *result;
  270. sprintf(tQBuf, "SELECT * FROM cameras WHERE `site_id`=%d AND `type`=2 AND `status`=2", SiteID );
  271. #ifdef DEBUG
  272. LOG(tQBuf);
  273. #endif
  274. result = m_pDatabase->Query( tQBuf );
  275. if( result!= NULL ) {
  276. DBC_CAM_DATA *tVal;
  277. Assoc *cArr;
  278. for(int i=0;i<result->size();i++) {
  279. LOG("Cam_Record!");
  280. cArr = result->at(i);
  281. tVal = (DBC_CAM_DATA*)malloc(sizeof(DBC_CAM_DATA));
  282. if( tVal!= NULL ) {
  283. Assoc::iterator it;
  284. memset(tVal,0,sizeof(DBC_CAM_DATA));
  285. it = cArr->find("id");
  286. if( it != cArr->end() ){
  287. tVal->id = atoi( it->second.c_str() );
  288. };
  289. it = cArr->find("site_id");
  290. if( it != cArr->end() ){
  291. tVal->site_id = atoi( it->second.c_str() );
  292. };
  293. it = cArr->find("node_id");
  294. if( it != cArr->end() ){
  295. tVal->node_id = atoi( it->second.c_str() );
  296. };
  297. it = cArr->find("online");
  298. if( it != cArr->end() ){
  299. tVal->online = (bool) atoi( it->second.c_str() );
  300. };
  301. it = cArr->find("type");
  302. if( it != cArr->end() ){
  303. tVal->type = atoi( it->second.c_str() );
  304. };
  305. it = cArr->find("name");
  306. if( it != cArr->end() ){
  307. strncpy( tVal->name, it->second.c_str(), 64 );
  308. };
  309. it = cArr->find("hardware");
  310. if( it != cArr->end() ){
  311. strncpy( tVal->hardware, it->second.c_str(), 32 );
  312. };
  313. it = cArr->find("status");
  314. if( it != cArr->end() ){
  315. tVal->status = atoi( it->second.c_str() );
  316. };
  317. it = cArr->find("session");
  318. if( it != cArr->end() ){
  319. strncpy( tVal->session, it->second.c_str(), 32 );
  320. };
  321. it = cArr->find("protocol");
  322. if( it != cArr->end() ){
  323. tVal->protocol = atoi( it->second.c_str() );
  324. };
  325. it = cArr->find("protocol_aux");
  326. if( it != cArr->end() ){
  327. strncpy( tVal->protocol_aux, it->second.c_str(), 64 );
  328. };
  329. it = cArr->find("lat");
  330. if( it != cArr->end() ){
  331. tVal->lat = atof( it->second.c_str() );
  332. };
  333. it = cArr->find("lon");
  334. if( it != cArr->end() ){
  335. tVal->lon = atof( it->second.c_str() );
  336. };
  337. it = cArr->find("alt");
  338. if( it != cArr->end() ){
  339. tVal->alt = atof( it->second.c_str() );
  340. };
  341. it = cArr->find("picth");
  342. if( it != cArr->end() ){
  343. tVal->pitch = atof( it->second.c_str() );
  344. };
  345. it = cArr->find("roll");
  346. if( it != cArr->end() ){
  347. tVal->roll = atof( it->second.c_str() );
  348. };
  349. it = cArr->find("yaw");
  350. if( it != cArr->end() ){
  351. tVal->yaw = atof( it->second.c_str() );
  352. };
  353. it = cArr->find("imager_size");
  354. if( it != cArr->end() ){
  355. tVal->imager_size = atof( it->second.c_str() );
  356. };
  357. it = cArr->find("imager_ratio");
  358. if( it != cArr->end() ){
  359. tVal->imager_ratio = atof( it->second.c_str() );
  360. };
  361. it = cArr->find("min_f");
  362. if( it != cArr->end() ){
  363. tVal->min_f = atof( it->second.c_str() );
  364. };
  365. it = cArr->find("max_f");
  366. if( it != cArr->end() ){
  367. tVal->max_f = atof( it->second.c_str() );
  368. };
  369. it = cArr->find("access_login");
  370. if( it != cArr->end() ){
  371. strncpy( tVal->username, it->second.c_str(), 16 );
  372. };
  373. it = cArr->find("access_pass");
  374. if( it != cArr->end() ){
  375. strncpy( tVal->password, it->second.c_str(), 16 );
  376. };
  377. it = cArr->find("uuid");
  378. if( it != cArr->end() ){
  379. strncpy( tVal->uuid, it->second.c_str(), CLOG );
  380. };
  381. it = cArr->find("mac");
  382. if( it != cArr->end() ){
  383. strncpy( tVal->mac, it->second.c_str(), 16 );
  384. };
  385. it = cArr->find("ip");
  386. if( it != cArr->end() ){
  387. strncpy( tVal->ip, it->second.c_str(), 16 );
  388. };
  389. it = cArr->find("port");
  390. if( it != cArr->end() ){
  391. tVal->port = atoi( it->second.c_str() );
  392. };
  393. it = cArr->find("onvif_access");
  394. if( it != cArr->end() ){
  395. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  396. };
  397. it = cArr->find("onvif_ctrl");
  398. if( it != cArr->end() ){
  399. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  400. };
  401. it = cArr->find("ptz_token");
  402. if( it != cArr->end() ){
  403. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  404. };
  405. VR.push_back(tVal);
  406. };
  407. //??
  408. //delete(cArr);
  409. };
  410. };
  411. deleteResult( result );
  412. #ifdef DEBUG
  413. sprintf( tQBuf,"Now %d Camera Records in System", VR.size());
  414. LOG(tQBuf);
  415. #endif
  416. if( m_pDatabase->isConnected()) return true;
  417. };
  418. return false;
  419. };
  420. DBC_INT_LIST *argusDBC::listNodes( uint32_t SiteID ){
  421. DBC_INT_LIST * tVal = NULL;
  422. if(m_bIsConnected){
  423. char tQBuf[CTEXT];
  424. AAssoc *result;
  425. sprintf(tQBuf, "SELECT id FROM nodes WHERE `site_id`=%d AND `type` > 0", SiteID );
  426. #ifdef DEBUG
  427. LOG(tQBuf);
  428. #endif
  429. result = m_pDatabase->Query( tQBuf );
  430. if( result!= NULL ) {
  431. if( result->size() > 0 ) {
  432. Assoc *cArr;
  433. tVal = new DBC_INT_LIST();
  434. if( tVal != NULL ) {
  435. Assoc::iterator it;
  436. for(int i=0;i<result->size();i++) {
  437. cArr = result->at(i);
  438. it = cArr->find("id");
  439. if( it != cArr->end() ){
  440. tVal->push_back( atoi( it->second.c_str() ));
  441. };
  442. };
  443. };
  444. };
  445. };
  446. deleteResult( result );
  447. };
  448. return tVal;
  449. };
  450. DBC_INT_LIST *argusDBC::listNewNodes( uint32_t SiteID, DBC_INT_LIST *old_ids ){
  451. DBC_INT_LIST * tVal = NULL;
  452. char tParam[CTEXT];
  453. //memset( tParam, 0, CTEXT );
  454. sprintf(tParam,"0");
  455. if( old_ids != NULL ) {
  456. char tBuffer[CTEXT];
  457. for(int i = 0; i< old_ids->size(); i++ ) {
  458. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  459. strncpy( tParam, tBuffer, CTEXT );
  460. };
  461. };
  462. if( m_bIsConnected ){
  463. char tQBuf[CTEXT];
  464. AAssoc *result;
  465. sprintf(tQBuf, "SELECT id FROM nodes WHERE `site_id`=%d AND `type` > 0 AND `id` NOT IN(%s)", SiteID, tParam );
  466. #ifdef DEBUG
  467. LOG(tQBuf);
  468. #endif
  469. result = m_pDatabase->Query( tQBuf );
  470. if( result!= NULL ) {
  471. if( result->size() > 0 ) {
  472. Assoc *cArr;
  473. tVal = new DBC_INT_LIST();
  474. if( tVal != NULL ) {
  475. Assoc::iterator it;
  476. for(int i=0;i<result->size();i++) {
  477. cArr = result->at(i);
  478. it = cArr->find("id");
  479. if( it != cArr->end() ){
  480. tVal->push_back( atoi( it->second.c_str() ));
  481. };
  482. };
  483. };
  484. };
  485. };
  486. deleteResult( result );
  487. };
  488. return tVal;
  489. };
  490. DBC_NODE_DATA *argusDBC::getNodeData( uint32_t ID ){
  491. DBC_NODE_DATA * tVal = NULL;
  492. if(m_bIsConnected){
  493. char tQBuf[CTEXT];
  494. AAssoc *result;
  495. sprintf(tQBuf, "SELECT * FROM nodes WHERE `id`=%d", ID );
  496. #ifdef DEBUG
  497. LOG(tQBuf);
  498. #endif
  499. result = m_pDatabase->Query( tQBuf );
  500. if( result!= NULL ) {
  501. if( result->size() > 0 ) {
  502. Assoc *cArr;
  503. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  504. if( tVal != NULL ) {
  505. Assoc::iterator it;
  506. cArr = result->at(0);
  507. it = cArr->find("id");
  508. if( it != cArr->end() ){
  509. tVal->node_id = atoi( it->second.c_str() );
  510. };
  511. it = cArr->find("type");
  512. if( it != cArr->end() ){
  513. tVal->type = (ARGUS_NODE_TYPE) atoi( it->second.c_str() );
  514. };
  515. it = cArr->find("aux_id");
  516. if( it != cArr->end() ){
  517. tVal->aux_id = atoi( it->second.c_str() );
  518. };
  519. it = cArr->find("name");
  520. if( it != cArr->end() ){
  521. sprintf( tVal->name, it->second.c_str() );
  522. };
  523. it = cArr->find("ts_s");
  524. if( it != cArr->end() ){
  525. tVal->tss = atoi( it->second.c_str() );
  526. };
  527. it = cArr->find("ts_ms");
  528. if( it != cArr->end() ){
  529. tVal->tsm = atoi( it->second.c_str() );
  530. };
  531. it = cArr->find("online");
  532. if( it != cArr->end() ){
  533. tVal->online = (bool)atoi( it->second.c_str() );
  534. };
  535. };
  536. };
  537. };
  538. deleteResult( result );
  539. };
  540. return tVal;
  541. };
  542. DBC_INT_LIST *argusDBC::listModules( uint32_t ServerID ){
  543. DBC_INT_LIST * tVal = NULL;
  544. if(m_bIsConnected){
  545. char tQBuf[CTEXT];
  546. AAssoc *result;
  547. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1", ServerID );
  548. #ifdef DEBUG
  549. //LOG(tQBuf);
  550. #endif
  551. result = m_pDatabase->Query( tQBuf );
  552. if( result!= NULL ) {
  553. if( result->size() > 0 ) {
  554. Assoc *cArr;
  555. tVal = new DBC_INT_LIST();
  556. if( tVal != NULL ) {
  557. Assoc::iterator it;
  558. for(int i=0;i<result->size();i++) {
  559. cArr = result->at(i);
  560. it = cArr->find("id");
  561. if( it != cArr->end() ){
  562. tVal->push_back( atoi( it->second.c_str() ));
  563. };
  564. };
  565. };
  566. };
  567. };
  568. deleteResult( result );
  569. };
  570. return tVal;
  571. };
  572. DBC_INT_LIST *argusDBC::listMediaStreams( uint32_t SiteId, uint32_t MediaServerID, uint8_t listMode ){
  573. DBC_INT_LIST * tVal = NULL;
  574. if(m_bIsConnected){
  575. char tQBuf[CTEXT];
  576. AAssoc *result;
  577. switch( listMode ){
  578. 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;
  579. 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;
  580. default: sprintf(tQBuf, "SELECT id FROM media_server_streams WHERE `site_id`=%d AND `media_server_id`=%d", SiteId, MediaServerID );
  581. };
  582. #ifdef DEBUG
  583. //LOG(tQBuf);
  584. #endif
  585. result = m_pDatabase->Query( tQBuf );
  586. if( result!= NULL ) {
  587. if( result->size() > 0 ) {
  588. Assoc *cArr;
  589. tVal = new DBC_INT_LIST();
  590. if( tVal != NULL ) {
  591. Assoc::iterator it;
  592. for(int i=0;i<result->size();i++) {
  593. cArr = result->at(i);
  594. it = cArr->find("id");
  595. if( it != cArr->end() ){
  596. tVal->push_back( atoi( it->second.c_str() ));
  597. };
  598. };
  599. };
  600. };
  601. };
  602. deleteResult( result );
  603. };
  604. return tVal;
  605. };
  606. DBC_INT_LIST *argusDBC::listSockets( uint32_t NodeID ){
  607. DBC_INT_LIST * tVal = NULL;
  608. if(m_bIsConnected){
  609. char tQBuf[CTEXT];
  610. AAssoc *result;
  611. sprintf(tQBuf, "SELECT id FROM sockets WHERE `node_id`=%d", NodeID );
  612. #ifdef DEBUG
  613. LOG(tQBuf);
  614. #endif
  615. result = m_pDatabase->Query( tQBuf );
  616. if( result!= NULL ) {
  617. if( result->size() > 0 ) {
  618. Assoc *cArr;
  619. tVal = new DBC_INT_LIST();
  620. if( tVal != NULL ) {
  621. Assoc::iterator it;
  622. for(int i=0;i<result->size();i++) {
  623. cArr = result->at(i);
  624. it = cArr->find("id");
  625. if( it != cArr->end() ){
  626. tVal->push_back( atoi( it->second.c_str() ));
  627. };
  628. };
  629. };
  630. };
  631. };
  632. deleteResult( result );
  633. };
  634. return tVal;
  635. };
  636. DBC_INT_LIST *argusDBC::listNewModules( uint32_t ServerID, DBC_INT_LIST * old_ids ){
  637. DBC_INT_LIST * tVal = NULL;
  638. char tParam[CTEXT];
  639. //memset( tParam, 0, CTEXT );
  640. sprintf(tParam,"0");
  641. if( old_ids != NULL ) {
  642. char tBuffer[CTEXT];
  643. for(int i = 0; i< old_ids->size(); i++ ) {
  644. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  645. strncpy( tParam, tBuffer, CTEXT );
  646. };
  647. };
  648. if(m_bIsConnected){
  649. char tQBuf[CTEXT];
  650. AAssoc *result;
  651. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1 AND `id` NOT IN (%s)", ServerID, tParam );
  652. #ifdef DEBUG
  653. //LOG(tQBuf);
  654. #endif
  655. result = m_pDatabase->Query( tQBuf );
  656. if( result!= NULL ) {
  657. if( result->size() > 0 ) {
  658. Assoc *cArr;
  659. tVal = new DBC_INT_LIST();
  660. if( tVal != NULL ) {
  661. Assoc::iterator it;
  662. for(int i=0;i<result->size();i++) {
  663. cArr = result->at(i);
  664. it = cArr->find("id");
  665. if( it != cArr->end() ){
  666. tVal->push_back( atoi( it->second.c_str() ));
  667. };
  668. };
  669. };
  670. };
  671. };
  672. deleteResult( result );
  673. };
  674. return tVal;
  675. };
  676. uint32_t argusDBC::getCamId( CSTR UUID ){
  677. uint32_t RetVal = 0;
  678. if(m_bIsConnected){
  679. char tQBuf[CTEXT];
  680. AAssoc *result;
  681. sprintf(tQBuf, "SELECT id FROM cameras WHERE `uuid`='%s'", UUID );
  682. #ifdef DEBUG
  683. LOG(tQBuf);
  684. #endif
  685. result = m_pDatabase->Query( tQBuf );
  686. if( result!= NULL ) {
  687. if( result->size() > 0 ) {
  688. Assoc *cArr;
  689. Assoc::iterator it;
  690. cArr = result->at(0);
  691. it = cArr->find("id");
  692. if( it != cArr->end() ){
  693. RetVal = atoi( it->second.c_str() );
  694. };
  695. };
  696. };
  697. deleteResult( result );
  698. };
  699. return RetVal;
  700. };
  701. DBC_CAM_DATA *argusDBC::getCamData( uint32_t ID ){
  702. DBC_CAM_DATA *tVal = NULL;
  703. if(m_bIsConnected){
  704. char tQBuf[CTEXT];
  705. AAssoc *result;
  706. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  707. #ifdef DEBUG
  708. LOG(tQBuf);
  709. #endif
  710. result = m_pDatabase->Query( tQBuf );
  711. if( result!= NULL ) {
  712. if( result->size() > 0 ) {
  713. Assoc *cArr;
  714. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  715. if( tVal != NULL ) {
  716. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  717. tVal->id = ID;
  718. Assoc::iterator it;
  719. cArr = result->at(0);
  720. it = cArr->find("site_id");
  721. if( it != cArr->end() ){
  722. tVal->site_id = atoi( it->second.c_str() );
  723. };
  724. it = cArr->find("node_id");
  725. if( it != cArr->end() ){
  726. tVal->node_id = atoi( it->second.c_str() );
  727. };
  728. it = cArr->find("online");
  729. if( it != cArr->end() ){
  730. tVal->online = (bool) atoi( it->second.c_str() );
  731. };
  732. it = cArr->find("type");
  733. if( it != cArr->end() ){
  734. tVal->type = atoi( it->second.c_str() );
  735. };
  736. it = cArr->find("name");
  737. if( it != cArr->end() ){
  738. strncpy( tVal->name, it->second.c_str(), 64 );
  739. };
  740. it = cArr->find("hardware");
  741. if( it != cArr->end() ){
  742. strncpy( tVal->hardware, it->second.c_str(), 32 );
  743. };
  744. it = cArr->find("status");
  745. if( it != cArr->end() ){
  746. tVal->status = atoi( it->second.c_str() );
  747. };
  748. it = cArr->find("session");
  749. if( it != cArr->end() ){
  750. strncpy( tVal->session, it->second.c_str(), 32 );
  751. };
  752. it = cArr->find("protocol");
  753. if( it != cArr->end() ){
  754. tVal->protocol = atoi( it->second.c_str() );
  755. };
  756. it = cArr->find("protocol_aux");
  757. if( it != cArr->end() ){
  758. strncpy( tVal->protocol_aux, it->second.c_str(), 64 );
  759. };
  760. it = cArr->find("lat");
  761. if( it != cArr->end() ){
  762. tVal->lat = atof( it->second.c_str() );
  763. };
  764. it = cArr->find("lon");
  765. if( it != cArr->end() ){
  766. tVal->lon = atof( it->second.c_str() );
  767. };
  768. it = cArr->find("alt");
  769. if( it != cArr->end() ){
  770. tVal->alt = atof( it->second.c_str() );
  771. };
  772. it = cArr->find("picth");
  773. if( it != cArr->end() ){
  774. tVal->pitch = atof( it->second.c_str() );
  775. };
  776. it = cArr->find("roll");
  777. if( it != cArr->end() ){
  778. tVal->roll = atof( it->second.c_str() );
  779. };
  780. it = cArr->find("yaw");
  781. if( it != cArr->end() ){
  782. tVal->yaw = atof( it->second.c_str() );
  783. };
  784. it = cArr->find("imager_size");
  785. if( it != cArr->end() ){
  786. tVal->imager_size = atof( it->second.c_str() );
  787. };
  788. it = cArr->find("imager_ratio");
  789. if( it != cArr->end() ){
  790. tVal->imager_ratio = atof( it->second.c_str() );
  791. };
  792. it = cArr->find("min_f");
  793. if( it != cArr->end() ){
  794. tVal->min_f = atof( it->second.c_str() );
  795. };
  796. it = cArr->find("max_f");
  797. if( it != cArr->end() ){
  798. tVal->max_f = atof( it->second.c_str() );
  799. };
  800. it = cArr->find("access_login");
  801. if( it != cArr->end() ){
  802. strncpy( tVal->username, it->second.c_str(), 16 );
  803. };
  804. it = cArr->find("access_pass");
  805. if( it != cArr->end() ){
  806. strncpy( tVal->password, it->second.c_str(), 16 );
  807. };
  808. it = cArr->find("uuid");
  809. if( it != cArr->end() ){
  810. strncpy( tVal->uuid, it->second.c_str(), 64 );
  811. };
  812. it = cArr->find("mac");
  813. if( it != cArr->end() ){
  814. strncpy( tVal->mac, it->second.c_str(), 16 );
  815. };
  816. it = cArr->find("ip");
  817. if( it != cArr->end() ){
  818. strncpy( tVal->ip, it->second.c_str(), 16 );
  819. };
  820. it = cArr->find("port");
  821. if( it != cArr->end() ){
  822. tVal->port = atoi( it->second.c_str() );
  823. };
  824. it = cArr->find("onvif_access");
  825. if( it != cArr->end() ){
  826. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  827. };
  828. it = cArr->find("onvif_ctrl");
  829. if( it != cArr->end() ){
  830. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  831. };
  832. it = cArr->find("ptz_token");
  833. if( it != cArr->end() ){
  834. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  835. };
  836. };
  837. };
  838. };
  839. deleteResult( result );
  840. };
  841. return tVal;
  842. };
  843. DBC_MODULE_DATA *argusDBC::getModuleData( uint32_t ID ){
  844. DBC_MODULE_DATA *tVal = NULL;
  845. if(m_bIsConnected){
  846. char tQBuf[CTEXT];
  847. AAssoc *result;
  848. sprintf(tQBuf, "SELECT * FROM server_modules WHERE `id`=%d", ID );
  849. #ifdef DEBUG
  850. LOG(tQBuf);
  851. #endif
  852. result = m_pDatabase->Query( tQBuf );
  853. if( result!= NULL ) {
  854. if( result->size() > 0 ) {
  855. Assoc *cArr;
  856. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  857. if( tVal != NULL ) {
  858. Assoc::iterator it;
  859. cArr = result->at(0);
  860. it = cArr->find("id");
  861. if( it != cArr->end() ){
  862. tVal->module_id = atoi( it->second.c_str() );
  863. };
  864. it = cArr->find("aux_id");
  865. if( it != cArr->end() ){
  866. tVal->aux_id = atoi( it->second.c_str() );
  867. };
  868. it = cArr->find("type");
  869. if( it != cArr->end() ){
  870. tVal->type = atoi( it->second.c_str() );
  871. };
  872. it = cArr->find("enabled");
  873. if( it != cArr->end() ){
  874. tVal->enabled = (bool)atoi( it->second.c_str() );
  875. };
  876. };
  877. };
  878. };
  879. deleteResult( result );
  880. };
  881. return tVal;
  882. };
  883. DBC_SOCKET_DATA *argusDBC::getSocketData( uint32_t ID ){
  884. DBC_SOCKET_DATA *tVal = NULL;
  885. if(m_bIsConnected){
  886. char tQBuf[CTEXT];
  887. AAssoc *result;
  888. sprintf(tQBuf, "SELECT * FROM sockets WHERE `id`=%d", ID );
  889. #ifdef DEBUG
  890. LOG(tQBuf);
  891. #endif
  892. result = m_pDatabase->Query( tQBuf );
  893. if( result!= NULL ) {
  894. if( result->size() > 0 ) {
  895. Assoc *cArr;
  896. tVal = (DBC_SOCKET_DATA*) malloc(sizeof(DBC_SOCKET_DATA));
  897. if( tVal != NULL ) {
  898. Assoc::iterator it;
  899. cArr = result->at(0);
  900. it = cArr->find("id");
  901. if( it != cArr->end() ){
  902. tVal->socket_id = atoi( it->second.c_str() );
  903. };
  904. it = cArr->find("aux_id");
  905. if( it != cArr->end() ){
  906. tVal->aux_id = atoi( it->second.c_str() );
  907. };
  908. it = cArr->find("type");
  909. if( it != cArr->end() ){
  910. tVal->type = atoi( it->second.c_str() );
  911. };
  912. it = cArr->find("binded");
  913. if( it != cArr->end() ){
  914. tVal->binded = (bool)atoi( it->second.c_str() );
  915. };
  916. };
  917. };
  918. };
  919. deleteResult( result );
  920. };
  921. return tVal;
  922. };
  923. DBC_MEDIA_STREAM_DATA *argusDBC::getMediaStreamData( uint32_t ID ){
  924. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  925. if(m_bIsConnected){
  926. char tQBuf[CTEXT];
  927. AAssoc *result;
  928. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  929. #ifdef DEBUG
  930. //LOG(tQBuf);
  931. #endif
  932. result = m_pDatabase->Query( tQBuf );
  933. if( result!= NULL ) {
  934. if( result->size() > 0 ) {
  935. Assoc *cArr;
  936. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  937. if( tVal != NULL ) {
  938. Assoc::iterator it;
  939. cArr = result->at(0);
  940. tVal->id = ID;
  941. it = cArr->find("source_id");
  942. if( it != cArr->end() ){
  943. tVal->source_id = atoi( it->second.c_str() );
  944. };
  945. it = cArr->find("site_id");
  946. if( it != cArr->end() ){
  947. tVal->site_id = atoi( it->second.c_str() );
  948. };
  949. it = cArr->find("source_type");
  950. if( it != cArr->end() ){
  951. sprintf( tVal->source_type, it->second.c_str() );
  952. };
  953. it = cArr->find("valid");
  954. if( it != cArr->end() ){
  955. tVal->valid = (bool) atoi( it->second.c_str() );
  956. };
  957. it = cArr->find("token");
  958. if( it != cArr->end() ){
  959. sprintf( tVal->token, it->second.c_str() );
  960. };
  961. it = cArr->find("mrl_proto");
  962. if( it != cArr->end() ){
  963. sprintf( tVal->mrl_proto, it->second.c_str() );
  964. };
  965. it = cArr->find("mrl_port");
  966. if( it != cArr->end() ){
  967. tVal->mrl_port = atoi( it->second.c_str() );
  968. };
  969. it = cArr->find("mrl");
  970. if( it != cArr->end() ){
  971. sprintf( tVal->mrl, it->second.c_str() );
  972. };
  973. };
  974. };
  975. };
  976. deleteResult( result );
  977. };
  978. return tVal;
  979. };
  980. DBC_MEDIA_SERVER_DATA *argusDBC::getMediaServerData( uint32_t ID ){
  981. DBC_MEDIA_SERVER_DATA *tVal = NULL;
  982. if(m_bIsConnected){
  983. char tQBuf[CTEXT];
  984. AAssoc *result;
  985. sprintf(tQBuf, "SELECT * FROM media_servers WHERE `id`=%d", ID );
  986. #ifdef DEBUG
  987. LOG(tQBuf);
  988. #endif
  989. result = m_pDatabase->Query( tQBuf );
  990. if( result!= NULL ) {
  991. if( result->size() > 0 ) {
  992. Assoc *cArr;
  993. tVal = (DBC_MEDIA_SERVER_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_DATA));
  994. if( tVal != NULL ) {
  995. Assoc::iterator it;
  996. cArr = result->at(0);
  997. tVal->id = ID;
  998. it = cArr->find("secret_key");
  999. if( it != cArr->end() ){
  1000. sprintf( tVal->key, it->second.c_str() );
  1001. };
  1002. it = cArr->find("ip");
  1003. if( it != cArr->end() ){
  1004. sprintf( tVal->ip, it->second.c_str() );
  1005. };
  1006. it = cArr->find("REST_port");
  1007. if( it != cArr->end() ){
  1008. tVal->port = atoi( it->second.c_str() );
  1009. };
  1010. it = cArr->find("virtual_host");
  1011. if( it != cArr->end() ){
  1012. sprintf( tVal->host, it->second.c_str() );
  1013. };
  1014. it = cArr->find("recording");
  1015. if( it != cArr->end() ){
  1016. tVal->recording = (bool)atoi( it->second.c_str() );
  1017. };
  1018. sprintf( tVal->name,"ZLMedia_%d", tVal->id );
  1019. };
  1020. };
  1021. };
  1022. deleteResult( result );
  1023. };
  1024. return tVal;
  1025. };
  1026. DBC_MEDIA_SERVER_STREAM_DATA *argusDBC::getMediaServerStreamData( uint32_t ID ){
  1027. DBC_MEDIA_SERVER_STREAM_DATA *tVal = NULL;
  1028. if(m_bIsConnected){
  1029. char tQBuf[MAX_PATH_LEN];
  1030. AAssoc *result;
  1031. sprintf(tQBuf, "SELECT `id`, `media_stream_id`, `online`, `key`, `mrl` FROM media_server_streams WHERE `id`=%d", ID );
  1032. #ifdef DEBUG
  1033. //LOG(tQBuf);
  1034. #endif
  1035. result = m_pDatabase->Query( tQBuf );
  1036. if( result!= NULL ) {
  1037. if( result->size() > 0 ) {
  1038. Assoc *cArr;
  1039. tVal = (DBC_MEDIA_SERVER_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_STREAM_DATA));
  1040. if( tVal != NULL ) {
  1041. Assoc::iterator it;
  1042. cArr = result->at(0);
  1043. tVal->id = ID;
  1044. it = cArr->find("media_stream_id");
  1045. if( it != cArr->end() ){
  1046. tVal->media_stream_id = atoi( it->second.c_str() );
  1047. };
  1048. it = cArr->find("online");
  1049. if( it != cArr->end() ){
  1050. tVal->online = atoi( it->second.c_str() );
  1051. };
  1052. it = cArr->find("key");
  1053. if( it != cArr->end() ){
  1054. sprintf( tVal->key, it->second.c_str() );
  1055. };
  1056. it = cArr->find("mrl");
  1057. if( it != cArr->end() ){
  1058. sprintf( tVal->mrl, it->second.c_str() );
  1059. };
  1060. };
  1061. };
  1062. };
  1063. deleteResult( result );
  1064. };
  1065. return tVal;
  1066. };
  1067. void argusDBC::registerServer(uint32_t SiteID, CSTR identity ){
  1068. if(m_bIsConnected){
  1069. char tQBuf[CTEXT];
  1070. sprintf(tQBuf,"INSERT INTO servers SET `site_id`=%d, `identity`='%s', `online`=1 ON DUPLICATE KEY UPDATE `online`=1", SiteID, identity );
  1071. #ifdef DEBUG
  1072. LOG(tQBuf);
  1073. #endif
  1074. m_pDatabase->SilentQuery( tQBuf );
  1075. // Clenup Nodes and services ...
  1076. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `site_id`=%d ", SiteID );
  1077. #ifdef DEBUG
  1078. LOG(tQBuf);
  1079. #endif
  1080. m_pDatabase->SilentQuery( tQBuf );
  1081. sprintf(tQBuf,"UPDATE cameras SET `online`=0 WHERE `site_id`=%d ", SiteID );
  1082. #ifdef DEBUG
  1083. LOG(tQBuf);
  1084. #endif
  1085. m_pDatabase->SilentQuery( tQBuf );
  1086. sprintf(tQBuf,"UPDATE media_streams SET `valid`=0 WHERE `site_id`=%d ", SiteID );
  1087. #ifdef DEBUG
  1088. LOG(tQBuf);
  1089. #endif
  1090. m_pDatabase->SilentQuery( tQBuf );
  1091. sprintf(tQBuf,"UPDATE media_server_streams SET `valid`=0, `online`=0 WHERE `site_id`=%d ", SiteID );
  1092. #ifdef DEBUG
  1093. LOG(tQBuf);
  1094. #endif
  1095. m_pDatabase->SilentQuery( tQBuf );
  1096. uint32_t sid = getServerID( identity );
  1097. if( sid ) {
  1098. 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 );
  1099. #ifdef DEBUG
  1100. LOG(tQBuf);
  1101. #endif
  1102. m_pDatabase->SilentQuery( tQBuf );
  1103. };
  1104. };
  1105. };
  1106. void argusDBC::unregisterServer(uint32_t ServerID, uint32_t NodeID ){
  1107. if(m_bIsConnected){
  1108. char tQBuf[CTEXT];
  1109. sprintf(tQBuf,"UPDATE servers SET `online`=0 WHERE `id` = %d", ServerID );
  1110. #ifdef DEBUG
  1111. LOG(tQBuf);
  1112. #endif
  1113. m_pDatabase->SilentQuery( tQBuf );
  1114. setNodeOffline( NodeID );
  1115. };
  1116. };
  1117. void argusDBC::setNodeOnline( uint32_t NodeID ){
  1118. if(m_bIsConnected){
  1119. char tQBuf[CTEXT];
  1120. if( NodeID ) {
  1121. sprintf(tQBuf,"UPDATE nodes SET `online`=1 WHERE `id` = %d", NodeID );
  1122. #ifdef DEBUG
  1123. LOG(tQBuf);
  1124. #endif
  1125. m_pDatabase->SilentQuery( tQBuf );
  1126. //sprintf(tQBuf,"UPDATE sockets SET `state`=1 WHERE `node_id` = %d", NodeID );
  1127. #ifdef DEBUG
  1128. //LOG(tQBuf);
  1129. #endif
  1130. //m_pDatabase->SilentQuery( tQBuf );
  1131. };
  1132. };
  1133. };
  1134. void argusDBC::setNodeOffline( uint32_t NodeID ){
  1135. if(m_bIsConnected){
  1136. char tQBuf[CTEXT];
  1137. if( NodeID ) {
  1138. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  1139. #ifdef DEBUG
  1140. LOG(tQBuf);
  1141. #endif
  1142. m_pDatabase->SilentQuery( tQBuf );
  1143. //sprintf(tQBuf,"UPDATE sockets SET `state`=0 WHERE `node_id` = %d", NodeID );
  1144. #ifdef DEBUG
  1145. //LOG(tQBuf);
  1146. #endif
  1147. //m_pDatabase->SilentQuery( tQBuf );
  1148. };
  1149. };
  1150. };
  1151. uint32_t argusDBC::getServerID( CSTR identity ){
  1152. uint32_t RetVal = 0;
  1153. if(m_bIsConnected){
  1154. char tQBuf[CTEXT];
  1155. sprintf(tQBuf,"SELECT id FROM servers WHERE `identity`='%s'", identity );
  1156. #ifdef DEBUG
  1157. LOG(tQBuf);
  1158. #endif
  1159. AAssoc *result;
  1160. result = m_pDatabase->Query( tQBuf );
  1161. if( result!= NULL ) {
  1162. if( result->size() > 0 ) {
  1163. Assoc * cArr;
  1164. Assoc::iterator it;
  1165. cArr = result->at(0);
  1166. it = cArr->find("id");
  1167. if( it != cArr->end() ){
  1168. RetVal = atoi( it->second.c_str() );
  1169. };
  1170. };
  1171. };
  1172. deleteResult( result );
  1173. };
  1174. return RetVal;
  1175. };
  1176. uint32_t argusDBC::getServerNode( uint32_t SID ){
  1177. uint32_t RetVal = 0;
  1178. if(m_bIsConnected){
  1179. char tQBuf[CTEXT];
  1180. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  1181. #ifdef DEBUG
  1182. LOG(tQBuf);
  1183. #endif
  1184. AAssoc *result;
  1185. result = m_pDatabase->Query( tQBuf );
  1186. if( result!= NULL ) {
  1187. if( result->size() > 0 ) {
  1188. Assoc * cArr;
  1189. Assoc::iterator it;
  1190. cArr = result->at(0);
  1191. it = cArr->find("id");
  1192. if( it != cArr->end() ){
  1193. RetVal = atoi( it->second.c_str() );
  1194. };
  1195. };
  1196. };
  1197. deleteResult( result );
  1198. };
  1199. return RetVal;
  1200. };
  1201. uint64_t argusDBC::addLocalMediaSource( uint32_t SiteID, CSTR path, uint32_t width, uint32_t height ){
  1202. uint64_t RetVal = 0;
  1203. if(m_bIsConnected){
  1204. char tQBuf[1024];
  1205. char tM5[33];
  1206. CSTR tMD5 = MD5String( path );
  1207. if( tMD5 != NULL ) {
  1208. memcpy( tM5, tMD5, 33 );
  1209. tM5[32]=(char)0;
  1210. free( tMD5 );
  1211. 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",
  1212. SiteID, tM5, width, height, path, width, height );
  1213. #ifdef DEBUG
  1214. LOG(tQBuf);
  1215. #endif
  1216. RetVal = m_pDatabase->QueryID( tQBuf );
  1217. };
  1218. };
  1219. LOG("Func Finished");
  1220. return RetVal;
  1221. };
  1222. uint32_t argusDBC::addNodeSocket( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, uint8_t SocketMode, bool multidrop, CSTR path, CSTR name ){
  1223. uint32_t retVal = 0;
  1224. if(m_bIsConnected){
  1225. char tQBuf[2048];
  1226. char tM5[33];
  1227. CSTR tMD5 = MD5String( path );
  1228. if( tMD5 != NULL ) {
  1229. memcpy( tM5, tMD5, 33 );
  1230. free( tMD5 );
  1231. if( multidrop ) {
  1232. 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'",
  1233. NodeID, SocketType, SocketMode, tM5, path, name, auxID, name );
  1234. } else {
  1235. 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'",
  1236. NodeID, SocketType, SocketMode, tM5, path, name, auxID, name );
  1237. };
  1238. #ifdef DEBUG
  1239. LOG(tQBuf);
  1240. #endif
  1241. //m_pDatabase->SilentQuery( tQBuf );
  1242. retVal = (uint32_t) m_pDatabase->QueryID( tQBuf );
  1243. };
  1244. };
  1245. return retVal;
  1246. };
  1247. uint32_t argusDBC::getNodeSocketID( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, CSTR path ){
  1248. uint32_t retVal = 0;
  1249. if(m_bIsConnected){
  1250. char tQBuf[2048];
  1251. char tM5[33];
  1252. CSTR tMD5 = MD5String( path );
  1253. if( tMD5 != NULL ) {
  1254. memcpy( tM5, tMD5, 33 );
  1255. free( tMD5 );
  1256. sprintf( tQBuf,"SELECT id FROM `sockets` WHERE `node_id`=%d AND `type`=%d AND `md5`='%s' AND `aux_id`=%d", NodeID, SocketType, tM5, auxID );
  1257. #ifdef DEBUG
  1258. LOG(tQBuf);
  1259. #endif
  1260. AAssoc *result;
  1261. result = m_pDatabase->Query( tQBuf );
  1262. if( result!= NULL ) {
  1263. if( result->size() > 0 ) {
  1264. Assoc * cArr;
  1265. Assoc::iterator it;
  1266. cArr = result->at(0);
  1267. it = cArr->find("id");
  1268. if( it != cArr->end() ){
  1269. retVal = atoi( it->second.c_str() );
  1270. };
  1271. };
  1272. };
  1273. deleteResult( result );
  1274. };
  1275. };
  1276. return retVal;
  1277. };
  1278. CSTR argusDBC::getNodeIP4( uint32_t NodeID ){
  1279. return getSocketName( NodeID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV4 );
  1280. };
  1281. CSTR argusDBC::getNodeIP6( uint32_t NodeID ){
  1282. return getSocketName( NodeID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV6 );
  1283. };
  1284. CSTR argusDBC::getSocketPath( uint32_t NodeID, uint32_t Type ){
  1285. if(m_bIsConnected){
  1286. char tQBuf[CTEXT];
  1287. sprintf( tQBuf, "SELECT `path` FROM `sockets` WHERE `node_id`=%d AND `type`=%d LIMIT 1",
  1288. NodeID, Type );
  1289. #ifdef DEBUG
  1290. LOG(tQBuf);
  1291. #endif
  1292. AAssoc *result;
  1293. result = m_pDatabase->Query( tQBuf );
  1294. if( result!= NULL ) {
  1295. if( result->size() > 0 ) {
  1296. Assoc * cArr;
  1297. Assoc::iterator it;
  1298. cArr = result->at(0);
  1299. it = cArr->find("path");
  1300. if( it != cArr->end() ){
  1301. strncpy( tQBuf, it->second.c_str(), CTEXT );
  1302. };
  1303. };
  1304. };
  1305. deleteResult( result );
  1306. return strdup(tQBuf);
  1307. };
  1308. return NULL;
  1309. };
  1310. CSTR argusDBC::getSocketName( uint32_t NodeID, uint32_t Type ){
  1311. if(m_bIsConnected){
  1312. char tQBuf[CTEXT];
  1313. sprintf( tQBuf, "SELECT `name` FROM `sockets` WHERE `node_id`=%d AND `type`=%d LIMIT 1",
  1314. NodeID, Type );
  1315. #ifdef DEBUG
  1316. LOG(tQBuf);
  1317. #endif
  1318. AAssoc *result;
  1319. result = m_pDatabase->Query( tQBuf );
  1320. if( result!= NULL ) {
  1321. if( result->size() > 0 ) {
  1322. Assoc * cArr;
  1323. Assoc::iterator it;
  1324. cArr = result->at(0);
  1325. it = cArr->find("name");
  1326. if( it != cArr->end() ){
  1327. strncpy( tQBuf, it->second.c_str(), CTEXT );
  1328. };
  1329. };
  1330. };
  1331. deleteResult( result );
  1332. return strdup(tQBuf);
  1333. };
  1334. return NULL;
  1335. };
  1336. KAFKA_SETTINGS *argusDBC::getSettingsKAFKA( uint32_t ModuleID ){
  1337. KAFKA_SETTINGS *tVal = NULL;
  1338. if( m_bIsConnected ) {
  1339. char tQBuf[CTEXT];
  1340. AAssoc *result;
  1341. sprintf( tQBuf, "SELECT brocker_address, brocker_port FROM modules_kafka WHERE module_id = %d", ModuleID );
  1342. #ifdef DEBUG
  1343. LOG(tQBuf);
  1344. #endif
  1345. result = m_pDatabase->Query( tQBuf );
  1346. if( result!= NULL ) {
  1347. if( result->size() > 0 ) {
  1348. Assoc *cArr;
  1349. tVal = (KAFKA_SETTINGS*) malloc(sizeof(KAFKA_SETTINGS));
  1350. if( tVal != NULL ) {
  1351. Assoc::iterator it;
  1352. cArr = result->at(0);
  1353. it = cArr->find("brocker_address");
  1354. if( it != cArr->end() ){
  1355. strncpy( tVal->brocker_addr, it->second.c_str(), 255 );
  1356. };
  1357. it = cArr->find("brocker_port");
  1358. if( it != cArr->end() ){
  1359. tVal->brocker_port = atoi( it->second.c_str() );
  1360. };
  1361. };
  1362. };
  1363. };
  1364. deleteResult( result );
  1365. };
  1366. return tVal;
  1367. };
  1368. KAFKA_TEMPLATE *argusDBC::getKafkaTemplateForEvent( uint32_t ModuleID, uint32_t ClassID, uint32_t EventID ){
  1369. KAFKA_TEMPLATE *tVal = NULL;
  1370. if( m_bIsConnected ) {
  1371. char tQBuf[CTEXT];
  1372. AAssoc *result;
  1373. sprintf( tQBuf, "SELECT topic, template FROM module_kafka_settings WHERE module_id=%d AND class_id=%d AND event_id=%d", ModuleID, ClassID, EventID );
  1374. #ifdef DEBUG
  1375. LOG(tQBuf);
  1376. #endif
  1377. result = m_pDatabase->Query( tQBuf );
  1378. if( result!= NULL ) {
  1379. if( result->size() > 0 ) {
  1380. Assoc *cArr;
  1381. tVal = (KAFKA_TEMPLATE*) malloc(sizeof(KAFKA_TEMPLATE));
  1382. if( tVal != NULL ) {
  1383. Assoc::iterator it;
  1384. cArr = result->at(0);
  1385. it = cArr->find("topic");
  1386. if( it != cArr->end() ){
  1387. memset( tVal->topic, 0, 128 );
  1388. strncpy( tVal->topic , it->second.c_str(), 127 );
  1389. };
  1390. it = cArr->find("template");
  1391. if( it != cArr->end() ){
  1392. tVal->tpl = (CSTR)malloc( it->second.length() + 1 );
  1393. if( tVal->tpl!= NULL) {
  1394. memset( tVal->tpl, 0, it->second.length() + 1 );
  1395. strncpy( tVal->tpl, it->second.c_str(), it->second.length() );
  1396. };
  1397. };
  1398. };
  1399. };
  1400. };
  1401. deleteResult( result );
  1402. };
  1403. return tVal;
  1404. };
  1405. DBC_UART_DATA *argusDBC::getSettingsUART( uint32_t SocketID ){
  1406. DBC_UART_DATA *tVal = NULL;
  1407. if( m_bIsConnected ) {
  1408. char tQBuf[MAX_PATH_LEN];
  1409. AAssoc *result;
  1410. sprintf( tQBuf, "SELECT us.speed, so.path FROM uart_settings us LEFT JOIN sockets so ON ( so.id = us.socket_id ) WHERE us.socket_id = %d", SocketID );
  1411. #ifdef DEBUG
  1412. LOG(tQBuf);
  1413. #endif
  1414. result = m_pDatabase->Query( tQBuf );
  1415. if( result!= NULL ) {
  1416. if( result->size() > 0 ) {
  1417. Assoc *cArr;
  1418. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  1419. if( tVal != NULL ) {
  1420. Assoc::iterator it;
  1421. cArr = result->at(0);
  1422. //tVal->socket_id = SocketID;
  1423. it = cArr->find("speed");
  1424. if( it != cArr->end() ){
  1425. tVal->speed = atoi( it->second.c_str() );
  1426. };
  1427. it = cArr->find("path");
  1428. if( it != cArr->end() ){
  1429. strncpy( tVal->device, it->second.c_str(), 255 );
  1430. };
  1431. };
  1432. };
  1433. };
  1434. deleteResult( result );
  1435. };
  1436. return tVal;
  1437. };
  1438. bool argusDBC::updateMediaServerStream( uint32_t id, CSTR mrl, CSTR key ){
  1439. if( m_bIsConnected ) {
  1440. char tQBuf[MAX_PATH_LEN];
  1441. #ifdef DEBUG
  1442. LOG("Stream Update");
  1443. #endif
  1444. if( key == NULL ) {
  1445. if( mrl == NULL ) {
  1446. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', mrl='', `valid`=0 WHERE `id`=%d", id );
  1447. } else {
  1448. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', `mrl`='%s', `valid`=1 WHERE `id`=%d", mrl, id );
  1449. };
  1450. } else {
  1451. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=1, `key`='%s', `mrl`='%s', `valid`=1 WHERE `id`=%d",
  1452. key,
  1453. mrl,
  1454. id
  1455. );
  1456. };
  1457. #ifdef DEBUG
  1458. LOG(tQBuf);
  1459. #endif
  1460. m_pDatabase->SilentQuery( tQBuf );
  1461. return true;
  1462. };
  1463. return false;
  1464. };
  1465. #undef LOG
  1466. #undef LOG_ERROR