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.

1565 lines
58 KiB

8 years ago
​
6 years ago
8 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
6 years ago
8 years ago
​
6 years ago
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
8 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
8 years ago
​
​
​
​
​
6 years ago
8 years ago
​
​
​
​
​
​
​
​
​
6 years ago
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
8 years ago
​
8 years ago
​
​
​
​
6 years ago
8 years ago
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
6 years ago
8 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
8 years ago
​
​
​
6 years ago
6 years ago
8 years ago
​
​
​
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
​
8 years ago
6 years ago
​
8 years ago
​
6 years ago
8 years ago
​
6 years ago
8 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 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_LRF3D_DATA *argusDBC::getNode3DData( uint32_t ID ){
  543. DBC_LRF3D_DATA * tVal = NULL;
  544. if(m_bIsConnected){
  545. char tQBuf[CTEXT];
  546. AAssoc *result;
  547. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
  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 = (DBC_LRF3D_DATA*) malloc(sizeof(DBC_LRF3D_DATA));
  556. if( tVal != NULL ) {
  557. memset( tVal, 0 , sizeof(DBC_LRF3D_DATA));
  558. Assoc::iterator it;
  559. cArr = result->at(0);
  560. it = cArr->find("ip_address");
  561. if( it != cArr->end() ){
  562. sprintf( tVal->master_host, it->second.c_str() );
  563. };
  564. LOG("1 OK");
  565. it = cArr->find("aux_ip_address");
  566. if( it != cArr->end() ){
  567. sprintf( tVal->slave_host, it->second.c_str() );
  568. };
  569. LOG("2 OK");
  570. it = cArr->find("port");
  571. if( it != cArr->end() ){
  572. tVal->master_port = atoi( it->second.c_str() );
  573. };
  574. LOG("3 OK");
  575. it = cArr->find("aux_port");
  576. if( it != cArr->end() ){
  577. tVal->slave_port = atoi( it->second.c_str() );
  578. };
  579. LOG("4 OK");
  580. };
  581. };
  582. };
  583. deleteResult( result );
  584. };
  585. return tVal;
  586. };
  587. DBC_INT_LIST *argusDBC::listModules( uint32_t ServerID ){
  588. DBC_INT_LIST * tVal = NULL;
  589. if(m_bIsConnected){
  590. char tQBuf[CTEXT];
  591. AAssoc *result;
  592. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1", ServerID );
  593. #ifdef DEBUG
  594. //LOG(tQBuf);
  595. #endif
  596. result = m_pDatabase->Query( tQBuf );
  597. if( result!= NULL ) {
  598. if( result->size() > 0 ) {
  599. Assoc *cArr;
  600. tVal = new DBC_INT_LIST();
  601. if( tVal != NULL ) {
  602. Assoc::iterator it;
  603. for(int i=0;i<result->size();i++) {
  604. cArr = result->at(i);
  605. it = cArr->find("id");
  606. if( it != cArr->end() ){
  607. tVal->push_back( atoi( it->second.c_str() ));
  608. };
  609. };
  610. };
  611. };
  612. };
  613. deleteResult( result );
  614. };
  615. return tVal;
  616. };
  617. DBC_INT_LIST *argusDBC::listMediaStreams( uint32_t SiteId, uint32_t MediaServerID, uint8_t listMode ){
  618. DBC_INT_LIST * tVal = NULL;
  619. if(m_bIsConnected){
  620. char tQBuf[CTEXT];
  621. AAssoc *result;
  622. switch( listMode ){
  623. 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;
  624. 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;
  625. default: sprintf(tQBuf, "SELECT id FROM media_server_streams WHERE `site_id`=%d AND `media_server_id`=%d", SiteId, MediaServerID );
  626. };
  627. #ifdef DEBUG
  628. //LOG(tQBuf);
  629. #endif
  630. result = m_pDatabase->Query( tQBuf );
  631. if( result!= NULL ) {
  632. if( result->size() > 0 ) {
  633. Assoc *cArr;
  634. tVal = new DBC_INT_LIST();
  635. if( tVal != NULL ) {
  636. Assoc::iterator it;
  637. for(int i=0;i<result->size();i++) {
  638. cArr = result->at(i);
  639. it = cArr->find("id");
  640. if( it != cArr->end() ){
  641. tVal->push_back( atoi( it->second.c_str() ));
  642. };
  643. };
  644. };
  645. };
  646. };
  647. deleteResult( result );
  648. };
  649. return tVal;
  650. };
  651. DBC_INT_LIST *argusDBC::listSockets( uint32_t NodeID ){
  652. DBC_INT_LIST * tVal = NULL;
  653. if(m_bIsConnected){
  654. char tQBuf[CTEXT];
  655. AAssoc *result;
  656. sprintf(tQBuf, "SELECT id FROM sockets WHERE `node_id`=%d", NodeID );
  657. #ifdef DEBUG
  658. LOG(tQBuf);
  659. #endif
  660. result = m_pDatabase->Query( tQBuf );
  661. if( result!= NULL ) {
  662. if( result->size() > 0 ) {
  663. Assoc *cArr;
  664. tVal = new DBC_INT_LIST();
  665. if( tVal != NULL ) {
  666. Assoc::iterator it;
  667. for(int i=0;i<result->size();i++) {
  668. cArr = result->at(i);
  669. it = cArr->find("id");
  670. if( it != cArr->end() ){
  671. tVal->push_back( atoi( it->second.c_str() ));
  672. };
  673. };
  674. };
  675. };
  676. };
  677. deleteResult( result );
  678. };
  679. return tVal;
  680. };
  681. DBC_INT_LIST *argusDBC::listNewModules( uint32_t ServerID, DBC_INT_LIST * old_ids ){
  682. DBC_INT_LIST * tVal = NULL;
  683. char tParam[CTEXT];
  684. //memset( tParam, 0, CTEXT );
  685. sprintf(tParam,"0");
  686. if( old_ids != NULL ) {
  687. char tBuffer[CTEXT];
  688. for(int i = 0; i< old_ids->size(); i++ ) {
  689. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  690. strncpy( tParam, tBuffer, CTEXT );
  691. };
  692. };
  693. if(m_bIsConnected){
  694. char tQBuf[CTEXT];
  695. AAssoc *result;
  696. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1 AND `id` NOT IN (%s)", ServerID, tParam );
  697. #ifdef DEBUG
  698. //LOG(tQBuf);
  699. #endif
  700. result = m_pDatabase->Query( tQBuf );
  701. if( result!= NULL ) {
  702. if( result->size() > 0 ) {
  703. Assoc *cArr;
  704. tVal = new DBC_INT_LIST();
  705. if( tVal != NULL ) {
  706. Assoc::iterator it;
  707. for(int i=0;i<result->size();i++) {
  708. cArr = result->at(i);
  709. it = cArr->find("id");
  710. if( it != cArr->end() ){
  711. tVal->push_back( atoi( it->second.c_str() ));
  712. };
  713. };
  714. };
  715. };
  716. };
  717. deleteResult( result );
  718. };
  719. return tVal;
  720. };
  721. uint32_t argusDBC::getCamId( CSTR UUID ){
  722. uint32_t RetVal = 0;
  723. if(m_bIsConnected){
  724. char tQBuf[CTEXT];
  725. AAssoc *result;
  726. sprintf(tQBuf, "SELECT id FROM cameras WHERE `uuid`='%s'", UUID );
  727. #ifdef DEBUG
  728. LOG(tQBuf);
  729. #endif
  730. result = m_pDatabase->Query( tQBuf );
  731. if( result!= NULL ) {
  732. if( result->size() > 0 ) {
  733. Assoc *cArr;
  734. Assoc::iterator it;
  735. cArr = result->at(0);
  736. it = cArr->find("id");
  737. if( it != cArr->end() ){
  738. RetVal = atoi( it->second.c_str() );
  739. };
  740. };
  741. };
  742. deleteResult( result );
  743. };
  744. return RetVal;
  745. };
  746. DBC_CAM_DATA *argusDBC::getCamData( uint32_t ID ){
  747. DBC_CAM_DATA *tVal = NULL;
  748. if(m_bIsConnected){
  749. char tQBuf[CTEXT];
  750. AAssoc *result;
  751. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  752. #ifdef DEBUG
  753. LOG(tQBuf);
  754. #endif
  755. result = m_pDatabase->Query( tQBuf );
  756. if( result!= NULL ) {
  757. if( result->size() > 0 ) {
  758. Assoc *cArr;
  759. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  760. if( tVal != NULL ) {
  761. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  762. tVal->id = ID;
  763. Assoc::iterator it;
  764. cArr = result->at(0);
  765. it = cArr->find("site_id");
  766. if( it != cArr->end() ){
  767. tVal->site_id = atoi( it->second.c_str() );
  768. };
  769. it = cArr->find("node_id");
  770. if( it != cArr->end() ){
  771. tVal->node_id = atoi( it->second.c_str() );
  772. };
  773. it = cArr->find("online");
  774. if( it != cArr->end() ){
  775. tVal->online = (bool) atoi( it->second.c_str() );
  776. };
  777. it = cArr->find("type");
  778. if( it != cArr->end() ){
  779. tVal->type = atoi( it->second.c_str() );
  780. };
  781. it = cArr->find("name");
  782. if( it != cArr->end() ){
  783. strncpy( tVal->name, it->second.c_str(), 64 );
  784. };
  785. it = cArr->find("hardware");
  786. if( it != cArr->end() ){
  787. strncpy( tVal->hardware, it->second.c_str(), 32 );
  788. };
  789. it = cArr->find("status");
  790. if( it != cArr->end() ){
  791. tVal->status = atoi( it->second.c_str() );
  792. };
  793. it = cArr->find("session");
  794. if( it != cArr->end() ){
  795. strncpy( tVal->session, it->second.c_str(), 32 );
  796. };
  797. it = cArr->find("protocol");
  798. if( it != cArr->end() ){
  799. tVal->protocol = atoi( it->second.c_str() );
  800. };
  801. it = cArr->find("protocol_aux");
  802. if( it != cArr->end() ){
  803. strncpy( tVal->protocol_aux, it->second.c_str(), 64 );
  804. };
  805. it = cArr->find("lat");
  806. if( it != cArr->end() ){
  807. tVal->lat = atof( it->second.c_str() );
  808. };
  809. it = cArr->find("lon");
  810. if( it != cArr->end() ){
  811. tVal->lon = atof( it->second.c_str() );
  812. };
  813. it = cArr->find("alt");
  814. if( it != cArr->end() ){
  815. tVal->alt = atof( it->second.c_str() );
  816. };
  817. it = cArr->find("picth");
  818. if( it != cArr->end() ){
  819. tVal->pitch = atof( it->second.c_str() );
  820. };
  821. it = cArr->find("roll");
  822. if( it != cArr->end() ){
  823. tVal->roll = atof( it->second.c_str() );
  824. };
  825. it = cArr->find("yaw");
  826. if( it != cArr->end() ){
  827. tVal->yaw = atof( it->second.c_str() );
  828. };
  829. it = cArr->find("imager_size");
  830. if( it != cArr->end() ){
  831. tVal->imager_size = atof( it->second.c_str() );
  832. };
  833. it = cArr->find("imager_ratio");
  834. if( it != cArr->end() ){
  835. tVal->imager_ratio = atof( it->second.c_str() );
  836. };
  837. it = cArr->find("min_f");
  838. if( it != cArr->end() ){
  839. tVal->min_f = atof( it->second.c_str() );
  840. };
  841. it = cArr->find("max_f");
  842. if( it != cArr->end() ){
  843. tVal->max_f = atof( it->second.c_str() );
  844. };
  845. it = cArr->find("access_login");
  846. if( it != cArr->end() ){
  847. strncpy( tVal->username, it->second.c_str(), 16 );
  848. };
  849. it = cArr->find("access_pass");
  850. if( it != cArr->end() ){
  851. strncpy( tVal->password, it->second.c_str(), 16 );
  852. };
  853. it = cArr->find("uuid");
  854. if( it != cArr->end() ){
  855. strncpy( tVal->uuid, it->second.c_str(), 64 );
  856. };
  857. it = cArr->find("mac");
  858. if( it != cArr->end() ){
  859. strncpy( tVal->mac, it->second.c_str(), 16 );
  860. };
  861. it = cArr->find("ip");
  862. if( it != cArr->end() ){
  863. strncpy( tVal->ip, it->second.c_str(), 16 );
  864. };
  865. it = cArr->find("port");
  866. if( it != cArr->end() ){
  867. tVal->port = atoi( it->second.c_str() );
  868. };
  869. it = cArr->find("onvif_access");
  870. if( it != cArr->end() ){
  871. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  872. };
  873. it = cArr->find("onvif_ctrl");
  874. if( it != cArr->end() ){
  875. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  876. };
  877. it = cArr->find("ptz_token");
  878. if( it != cArr->end() ){
  879. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  880. };
  881. };
  882. };
  883. };
  884. deleteResult( result );
  885. };
  886. return tVal;
  887. };
  888. DBC_MODULE_DATA *argusDBC::getModuleData( uint32_t ID ){
  889. DBC_MODULE_DATA *tVal = NULL;
  890. if(m_bIsConnected){
  891. char tQBuf[CTEXT];
  892. AAssoc *result;
  893. sprintf(tQBuf, "SELECT * FROM server_modules WHERE `id`=%d", ID );
  894. #ifdef DEBUG
  895. LOG(tQBuf);
  896. #endif
  897. result = m_pDatabase->Query( tQBuf );
  898. if( result!= NULL ) {
  899. if( result->size() > 0 ) {
  900. Assoc *cArr;
  901. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  902. if( tVal != NULL ) {
  903. Assoc::iterator it;
  904. cArr = result->at(0);
  905. it = cArr->find("id");
  906. if( it != cArr->end() ){
  907. tVal->module_id = atoi( it->second.c_str() );
  908. };
  909. it = cArr->find("aux_id");
  910. if( it != cArr->end() ){
  911. tVal->aux_id = atoi( it->second.c_str() );
  912. };
  913. it = cArr->find("type");
  914. if( it != cArr->end() ){
  915. tVal->type = atoi( it->second.c_str() );
  916. };
  917. it = cArr->find("enabled");
  918. if( it != cArr->end() ){
  919. tVal->enabled = (bool)atoi( it->second.c_str() );
  920. };
  921. };
  922. };
  923. };
  924. deleteResult( result );
  925. };
  926. return tVal;
  927. };
  928. DBC_SOCKET_DATA *argusDBC::getSocketData( uint32_t ID ){
  929. DBC_SOCKET_DATA *tVal = NULL;
  930. if(m_bIsConnected){
  931. char tQBuf[CTEXT];
  932. AAssoc *result;
  933. sprintf(tQBuf, "SELECT * FROM sockets WHERE `id`=%d", ID );
  934. #ifdef DEBUG
  935. LOG(tQBuf);
  936. #endif
  937. result = m_pDatabase->Query( tQBuf );
  938. if( result!= NULL ) {
  939. if( result->size() > 0 ) {
  940. Assoc *cArr;
  941. tVal = (DBC_SOCKET_DATA*) malloc(sizeof(DBC_SOCKET_DATA));
  942. if( tVal != NULL ) {
  943. Assoc::iterator it;
  944. cArr = result->at(0);
  945. it = cArr->find("id");
  946. if( it != cArr->end() ){
  947. tVal->socket_id = atoi( it->second.c_str() );
  948. };
  949. it = cArr->find("aux_id");
  950. if( it != cArr->end() ){
  951. tVal->aux_id = atoi( it->second.c_str() );
  952. };
  953. it = cArr->find("type");
  954. if( it != cArr->end() ){
  955. tVal->type = atoi( it->second.c_str() );
  956. };
  957. it = cArr->find("binded");
  958. if( it != cArr->end() ){
  959. tVal->binded = (bool)atoi( it->second.c_str() );
  960. };
  961. };
  962. };
  963. };
  964. deleteResult( result );
  965. };
  966. return tVal;
  967. };
  968. DBC_MEDIA_STREAM_DATA *argusDBC::getMediaStreamData( uint32_t ID ){
  969. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  970. if(m_bIsConnected){
  971. char tQBuf[CTEXT];
  972. AAssoc *result;
  973. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  974. #ifdef DEBUG
  975. //LOG(tQBuf);
  976. #endif
  977. result = m_pDatabase->Query( tQBuf );
  978. if( result!= NULL ) {
  979. if( result->size() > 0 ) {
  980. Assoc *cArr;
  981. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  982. if( tVal != NULL ) {
  983. Assoc::iterator it;
  984. cArr = result->at(0);
  985. tVal->id = ID;
  986. it = cArr->find("source_id");
  987. if( it != cArr->end() ){
  988. tVal->source_id = atoi( it->second.c_str() );
  989. };
  990. it = cArr->find("site_id");
  991. if( it != cArr->end() ){
  992. tVal->site_id = atoi( it->second.c_str() );
  993. };
  994. it = cArr->find("source_type");
  995. if( it != cArr->end() ){
  996. sprintf( tVal->source_type, it->second.c_str() );
  997. };
  998. it = cArr->find("valid");
  999. if( it != cArr->end() ){
  1000. tVal->valid = (bool) atoi( it->second.c_str() );
  1001. };
  1002. it = cArr->find("token");
  1003. if( it != cArr->end() ){
  1004. sprintf( tVal->token, it->second.c_str() );
  1005. };
  1006. it = cArr->find("mrl_proto");
  1007. if( it != cArr->end() ){
  1008. sprintf( tVal->mrl_proto, it->second.c_str() );
  1009. };
  1010. it = cArr->find("mrl_port");
  1011. if( it != cArr->end() ){
  1012. tVal->mrl_port = atoi( it->second.c_str() );
  1013. };
  1014. it = cArr->find("mrl");
  1015. if( it != cArr->end() ){
  1016. sprintf( tVal->mrl, it->second.c_str() );
  1017. };
  1018. };
  1019. };
  1020. };
  1021. deleteResult( result );
  1022. };
  1023. return tVal;
  1024. };
  1025. DBC_MEDIA_SERVER_DATA *argusDBC::getMediaServerData( uint32_t ID ){
  1026. DBC_MEDIA_SERVER_DATA *tVal = NULL;
  1027. if(m_bIsConnected){
  1028. char tQBuf[CTEXT];
  1029. AAssoc *result;
  1030. sprintf(tQBuf, "SELECT * FROM media_servers WHERE `id`=%d", ID );
  1031. #ifdef DEBUG
  1032. LOG(tQBuf);
  1033. #endif
  1034. result = m_pDatabase->Query( tQBuf );
  1035. if( result!= NULL ) {
  1036. if( result->size() > 0 ) {
  1037. Assoc *cArr;
  1038. tVal = (DBC_MEDIA_SERVER_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_DATA));
  1039. if( tVal != NULL ) {
  1040. Assoc::iterator it;
  1041. cArr = result->at(0);
  1042. tVal->id = ID;
  1043. it = cArr->find("secret_key");
  1044. if( it != cArr->end() ){
  1045. sprintf( tVal->key, it->second.c_str() );
  1046. };
  1047. it = cArr->find("ip");
  1048. if( it != cArr->end() ){
  1049. sprintf( tVal->ip, it->second.c_str() );
  1050. };
  1051. it = cArr->find("REST_port");
  1052. if( it != cArr->end() ){
  1053. tVal->port = atoi( it->second.c_str() );
  1054. };
  1055. it = cArr->find("virtual_host");
  1056. if( it != cArr->end() ){
  1057. sprintf( tVal->host, it->second.c_str() );
  1058. };
  1059. it = cArr->find("recording");
  1060. if( it != cArr->end() ){
  1061. tVal->recording = (bool)atoi( it->second.c_str() );
  1062. };
  1063. sprintf( tVal->name,"ZLMedia_%d", tVal->id );
  1064. };
  1065. };
  1066. };
  1067. deleteResult( result );
  1068. };
  1069. return tVal;
  1070. };
  1071. DBC_MEDIA_SERVER_STREAM_DATA *argusDBC::getMediaServerStreamData( uint32_t ID ){
  1072. DBC_MEDIA_SERVER_STREAM_DATA *tVal = NULL;
  1073. if(m_bIsConnected){
  1074. char tQBuf[MAX_PATH_LEN];
  1075. AAssoc *result;
  1076. sprintf(tQBuf, "SELECT `id`, `media_stream_id`, `online`, `key`, `mrl` FROM media_server_streams WHERE `id`=%d", ID );
  1077. #ifdef DEBUG
  1078. //LOG(tQBuf);
  1079. #endif
  1080. result = m_pDatabase->Query( tQBuf );
  1081. if( result!= NULL ) {
  1082. if( result->size() > 0 ) {
  1083. Assoc *cArr;
  1084. tVal = (DBC_MEDIA_SERVER_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_STREAM_DATA));
  1085. if( tVal != NULL ) {
  1086. Assoc::iterator it;
  1087. cArr = result->at(0);
  1088. tVal->id = ID;
  1089. it = cArr->find("media_stream_id");
  1090. if( it != cArr->end() ){
  1091. tVal->media_stream_id = atoi( it->second.c_str() );
  1092. };
  1093. it = cArr->find("online");
  1094. if( it != cArr->end() ){
  1095. tVal->online = atoi( it->second.c_str() );
  1096. };
  1097. it = cArr->find("key");
  1098. if( it != cArr->end() ){
  1099. sprintf( tVal->key, it->second.c_str() );
  1100. };
  1101. it = cArr->find("mrl");
  1102. if( it != cArr->end() ){
  1103. sprintf( tVal->mrl, it->second.c_str() );
  1104. };
  1105. };
  1106. };
  1107. };
  1108. deleteResult( result );
  1109. };
  1110. return tVal;
  1111. };
  1112. void argusDBC::registerServer(uint32_t SiteID, CSTR identity ){
  1113. if(m_bIsConnected){
  1114. char tQBuf[CTEXT];
  1115. sprintf(tQBuf,"INSERT INTO servers SET `site_id`=%d, `identity`='%s', `online`=1 ON DUPLICATE KEY UPDATE `online`=1", SiteID, identity );
  1116. #ifdef DEBUG
  1117. LOG(tQBuf);
  1118. #endif
  1119. m_pDatabase->SilentQuery( tQBuf );
  1120. // Clenup Nodes and services ...
  1121. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `site_id`=%d ", SiteID );
  1122. #ifdef DEBUG
  1123. LOG(tQBuf);
  1124. #endif
  1125. m_pDatabase->SilentQuery( tQBuf );
  1126. sprintf(tQBuf,"UPDATE cameras SET `online`=0 WHERE `site_id`=%d ", SiteID );
  1127. #ifdef DEBUG
  1128. LOG(tQBuf);
  1129. #endif
  1130. m_pDatabase->SilentQuery( tQBuf );
  1131. sprintf(tQBuf,"UPDATE media_streams SET `valid`=0 WHERE `site_id`=%d ", SiteID );
  1132. #ifdef DEBUG
  1133. LOG(tQBuf);
  1134. #endif
  1135. m_pDatabase->SilentQuery( tQBuf );
  1136. sprintf(tQBuf,"UPDATE media_server_streams SET `valid`=0, `online`=0 WHERE `site_id`=%d ", SiteID );
  1137. #ifdef DEBUG
  1138. LOG(tQBuf);
  1139. #endif
  1140. m_pDatabase->SilentQuery( tQBuf );
  1141. uint32_t sid = getServerID( identity );
  1142. if( sid ) {
  1143. 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 );
  1144. #ifdef DEBUG
  1145. LOG(tQBuf);
  1146. #endif
  1147. m_pDatabase->SilentQuery( tQBuf );
  1148. };
  1149. };
  1150. };
  1151. void argusDBC::unregisterServer(uint32_t ServerID, uint32_t NodeID ){
  1152. if(m_bIsConnected){
  1153. char tQBuf[CTEXT];
  1154. sprintf(tQBuf,"UPDATE servers SET `online`=0 WHERE `id` = %d", ServerID );
  1155. #ifdef DEBUG
  1156. LOG(tQBuf);
  1157. #endif
  1158. m_pDatabase->SilentQuery( tQBuf );
  1159. setNodeOffline( NodeID );
  1160. };
  1161. };
  1162. void argusDBC::setNodeOnline( uint32_t NodeID ){
  1163. if(m_bIsConnected){
  1164. char tQBuf[CTEXT];
  1165. if( NodeID ) {
  1166. sprintf(tQBuf,"UPDATE nodes SET `online`=1 WHERE `id` = %d", NodeID );
  1167. #ifdef DEBUG
  1168. LOG(tQBuf);
  1169. #endif
  1170. m_pDatabase->SilentQuery( tQBuf );
  1171. //sprintf(tQBuf,"UPDATE sockets SET `state`=1 WHERE `node_id` = %d", NodeID );
  1172. #ifdef DEBUG
  1173. //LOG(tQBuf);
  1174. #endif
  1175. //m_pDatabase->SilentQuery( tQBuf );
  1176. };
  1177. };
  1178. };
  1179. void argusDBC::setNodeOffline( uint32_t NodeID ){
  1180. if(m_bIsConnected){
  1181. char tQBuf[CTEXT];
  1182. if( NodeID ) {
  1183. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  1184. #ifdef DEBUG
  1185. LOG(tQBuf);
  1186. #endif
  1187. m_pDatabase->SilentQuery( tQBuf );
  1188. //sprintf(tQBuf,"UPDATE sockets SET `state`=0 WHERE `node_id` = %d", NodeID );
  1189. #ifdef DEBUG
  1190. //LOG(tQBuf);
  1191. #endif
  1192. //m_pDatabase->SilentQuery( tQBuf );
  1193. };
  1194. };
  1195. };
  1196. uint32_t argusDBC::getServerID( CSTR identity ){
  1197. uint32_t RetVal = 0;
  1198. if(m_bIsConnected){
  1199. char tQBuf[CTEXT];
  1200. sprintf(tQBuf,"SELECT id FROM servers WHERE `identity`='%s'", identity );
  1201. #ifdef DEBUG
  1202. LOG(tQBuf);
  1203. #endif
  1204. AAssoc *result;
  1205. result = m_pDatabase->Query( tQBuf );
  1206. if( result!= NULL ) {
  1207. if( result->size() > 0 ) {
  1208. Assoc * cArr;
  1209. Assoc::iterator it;
  1210. cArr = result->at(0);
  1211. it = cArr->find("id");
  1212. if( it != cArr->end() ){
  1213. RetVal = atoi( it->second.c_str() );
  1214. };
  1215. };
  1216. };
  1217. deleteResult( result );
  1218. };
  1219. return RetVal;
  1220. };
  1221. uint32_t argusDBC::getServerNode( uint32_t SID ){
  1222. uint32_t RetVal = 0;
  1223. if(m_bIsConnected){
  1224. char tQBuf[CTEXT];
  1225. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  1226. #ifdef DEBUG
  1227. LOG(tQBuf);
  1228. #endif
  1229. AAssoc *result;
  1230. result = m_pDatabase->Query( tQBuf );
  1231. if( result!= NULL ) {
  1232. if( result->size() > 0 ) {
  1233. Assoc * cArr;
  1234. Assoc::iterator it;
  1235. cArr = result->at(0);
  1236. it = cArr->find("id");
  1237. if( it != cArr->end() ){
  1238. RetVal = atoi( it->second.c_str() );
  1239. };
  1240. };
  1241. };
  1242. deleteResult( result );
  1243. };
  1244. return RetVal;
  1245. };
  1246. uint64_t argusDBC::addLocalMediaSource( uint32_t SiteID, CSTR path, uint32_t width, uint32_t height ){
  1247. uint64_t RetVal = 0;
  1248. if(m_bIsConnected){
  1249. char tQBuf[1024];
  1250. char tM5[33];
  1251. CSTR tMD5 = MD5String( path );
  1252. if( tMD5 != NULL ) {
  1253. memcpy( tM5, tMD5, 33 );
  1254. tM5[32]=(char)0;
  1255. free( tMD5 );
  1256. 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",
  1257. SiteID, tM5, width, height, path, width, height );
  1258. #ifdef DEBUG
  1259. LOG(tQBuf);
  1260. #endif
  1261. RetVal = m_pDatabase->QueryID( tQBuf );
  1262. };
  1263. };
  1264. LOG("Func Finished");
  1265. return RetVal;
  1266. };
  1267. uint32_t argusDBC::addNodeSocket( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, uint8_t SocketMode, bool multidrop, CSTR path, CSTR name ){
  1268. uint32_t retVal = 0;
  1269. if(m_bIsConnected){
  1270. char tQBuf[2048];
  1271. char tM5[33];
  1272. CSTR tMD5 = MD5String( path );
  1273. if( tMD5 != NULL ) {
  1274. memcpy( tM5, tMD5, 33 );
  1275. free( tMD5 );
  1276. if( multidrop ) {
  1277. 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'",
  1278. NodeID, SocketType, SocketMode, tM5, path, name, auxID, name );
  1279. } else {
  1280. 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'",
  1281. NodeID, SocketType, SocketMode, tM5, path, name, auxID, name );
  1282. };
  1283. #ifdef DEBUG
  1284. LOG(tQBuf);
  1285. #endif
  1286. //m_pDatabase->SilentQuery( tQBuf );
  1287. retVal = (uint32_t) m_pDatabase->QueryID( tQBuf );
  1288. };
  1289. };
  1290. return retVal;
  1291. };
  1292. uint32_t argusDBC::getNodeSocketID( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, CSTR path ){
  1293. uint32_t retVal = 0;
  1294. if(m_bIsConnected){
  1295. char tQBuf[2048];
  1296. char tM5[33];
  1297. CSTR tMD5 = MD5String( path );
  1298. if( tMD5 != NULL ) {
  1299. memcpy( tM5, tMD5, 33 );
  1300. free( tMD5 );
  1301. sprintf( tQBuf,"SELECT id FROM `sockets` WHERE `node_id`=%d AND `type`=%d AND `md5`='%s' AND `aux_id`=%d", NodeID, SocketType, tM5, auxID );
  1302. #ifdef DEBUG
  1303. LOG(tQBuf);
  1304. #endif
  1305. AAssoc *result;
  1306. result = m_pDatabase->Query( tQBuf );
  1307. if( result!= NULL ) {
  1308. if( result->size() > 0 ) {
  1309. Assoc * cArr;
  1310. Assoc::iterator it;
  1311. cArr = result->at(0);
  1312. it = cArr->find("id");
  1313. if( it != cArr->end() ){
  1314. retVal = atoi( it->second.c_str() );
  1315. };
  1316. };
  1317. };
  1318. deleteResult( result );
  1319. };
  1320. };
  1321. return retVal;
  1322. };
  1323. CSTR argusDBC::getNodeIP4( uint32_t NodeID ){
  1324. return getSocketName( NodeID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV4 );
  1325. };
  1326. CSTR argusDBC::getNodeIP6( uint32_t NodeID ){
  1327. return getSocketName( NodeID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV6 );
  1328. };
  1329. CSTR argusDBC::getSocketPath( uint32_t NodeID, uint32_t Type ){
  1330. if(m_bIsConnected){
  1331. char tQBuf[CTEXT];
  1332. sprintf( tQBuf, "SELECT `path` FROM `sockets` WHERE `node_id`=%d AND `type`=%d LIMIT 1",
  1333. NodeID, Type );
  1334. #ifdef DEBUG
  1335. LOG(tQBuf);
  1336. #endif
  1337. AAssoc *result;
  1338. result = m_pDatabase->Query( tQBuf );
  1339. if( result!= NULL ) {
  1340. if( result->size() > 0 ) {
  1341. Assoc * cArr;
  1342. Assoc::iterator it;
  1343. cArr = result->at(0);
  1344. it = cArr->find("path");
  1345. if( it != cArr->end() ){
  1346. strncpy( tQBuf, it->second.c_str(), CTEXT );
  1347. };
  1348. };
  1349. };
  1350. deleteResult( result );
  1351. return strdup(tQBuf);
  1352. };
  1353. return NULL;
  1354. };
  1355. CSTR argusDBC::getSocketName( uint32_t NodeID, uint32_t Type ){
  1356. if(m_bIsConnected){
  1357. char tQBuf[CTEXT];
  1358. sprintf( tQBuf, "SELECT `name` FROM `sockets` WHERE `node_id`=%d AND `type`=%d LIMIT 1",
  1359. NodeID, Type );
  1360. #ifdef DEBUG
  1361. LOG(tQBuf);
  1362. #endif
  1363. AAssoc *result;
  1364. result = m_pDatabase->Query( tQBuf );
  1365. if( result!= NULL ) {
  1366. if( result->size() > 0 ) {
  1367. Assoc * cArr;
  1368. Assoc::iterator it;
  1369. cArr = result->at(0);
  1370. it = cArr->find("name");
  1371. if( it != cArr->end() ){
  1372. strncpy( tQBuf, it->second.c_str(), CTEXT );
  1373. };
  1374. };
  1375. };
  1376. deleteResult( result );
  1377. return strdup(tQBuf);
  1378. };
  1379. return NULL;
  1380. };
  1381. KAFKA_SETTINGS *argusDBC::getSettingsKAFKA( uint32_t ModuleID ){
  1382. KAFKA_SETTINGS *tVal = NULL;
  1383. if( m_bIsConnected ) {
  1384. char tQBuf[CTEXT];
  1385. AAssoc *result;
  1386. sprintf( tQBuf, "SELECT brocker_address, brocker_port FROM modules_kafka WHERE module_id = %d", ModuleID );
  1387. #ifdef DEBUG
  1388. LOG(tQBuf);
  1389. #endif
  1390. result = m_pDatabase->Query( tQBuf );
  1391. if( result!= NULL ) {
  1392. if( result->size() > 0 ) {
  1393. Assoc *cArr;
  1394. tVal = (KAFKA_SETTINGS*) malloc(sizeof(KAFKA_SETTINGS));
  1395. if( tVal != NULL ) {
  1396. Assoc::iterator it;
  1397. cArr = result->at(0);
  1398. it = cArr->find("brocker_address");
  1399. if( it != cArr->end() ){
  1400. strncpy( tVal->brocker_addr, it->second.c_str(), 255 );
  1401. };
  1402. it = cArr->find("brocker_port");
  1403. if( it != cArr->end() ){
  1404. tVal->brocker_port = atoi( it->second.c_str() );
  1405. };
  1406. };
  1407. };
  1408. };
  1409. deleteResult( result );
  1410. };
  1411. return tVal;
  1412. };
  1413. KAFKA_TEMPLATE *argusDBC::getKafkaTemplateForEvent( uint32_t ModuleID, uint32_t ClassID, uint32_t EventID ){
  1414. KAFKA_TEMPLATE *tVal = NULL;
  1415. if( m_bIsConnected ) {
  1416. char tQBuf[CTEXT];
  1417. AAssoc *result;
  1418. sprintf( tQBuf, "SELECT topic, template FROM module_kafka_settings WHERE module_id=%d AND class_id=%d AND event_id=%d", ModuleID, ClassID, EventID );
  1419. #ifdef DEBUG
  1420. LOG(tQBuf);
  1421. #endif
  1422. result = m_pDatabase->Query( tQBuf );
  1423. if( result!= NULL ) {
  1424. if( result->size() > 0 ) {
  1425. Assoc *cArr;
  1426. tVal = (KAFKA_TEMPLATE*) malloc(sizeof(KAFKA_TEMPLATE));
  1427. if( tVal != NULL ) {
  1428. Assoc::iterator it;
  1429. cArr = result->at(0);
  1430. it = cArr->find("topic");
  1431. if( it != cArr->end() ){
  1432. memset( tVal->topic, 0, 128 );
  1433. strncpy( tVal->topic , it->second.c_str(), 127 );
  1434. };
  1435. it = cArr->find("template");
  1436. if( it != cArr->end() ){
  1437. tVal->tpl = (CSTR)malloc( it->second.length() + 1 );
  1438. if( tVal->tpl!= NULL) {
  1439. memset( tVal->tpl, 0, it->second.length() + 1 );
  1440. strncpy( tVal->tpl, it->second.c_str(), it->second.length() );
  1441. };
  1442. };
  1443. };
  1444. };
  1445. };
  1446. deleteResult( result );
  1447. };
  1448. return tVal;
  1449. };
  1450. DBC_UART_DATA *argusDBC::getSettingsUART( uint32_t SocketID ){
  1451. DBC_UART_DATA *tVal = NULL;
  1452. if( m_bIsConnected ) {
  1453. char tQBuf[MAX_PATH_LEN];
  1454. AAssoc *result;
  1455. 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 );
  1456. #ifdef DEBUG
  1457. LOG(tQBuf);
  1458. #endif
  1459. result = m_pDatabase->Query( tQBuf );
  1460. if( result!= NULL ) {
  1461. if( result->size() > 0 ) {
  1462. Assoc *cArr;
  1463. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  1464. if( tVal != NULL ) {
  1465. Assoc::iterator it;
  1466. cArr = result->at(0);
  1467. //tVal->socket_id = SocketID;
  1468. it = cArr->find("speed");
  1469. if( it != cArr->end() ){
  1470. tVal->speed = atoi( it->second.c_str() );
  1471. };
  1472. it = cArr->find("path");
  1473. if( it != cArr->end() ){
  1474. strncpy( tVal->device, it->second.c_str(), 255 );
  1475. };
  1476. };
  1477. };
  1478. };
  1479. deleteResult( result );
  1480. };
  1481. return tVal;
  1482. };
  1483. bool argusDBC::updateMediaServerStream( uint32_t id, CSTR mrl, CSTR key ){
  1484. if( m_bIsConnected ) {
  1485. char tQBuf[MAX_PATH_LEN];
  1486. #ifdef DEBUG
  1487. LOG("Stream Update");
  1488. #endif
  1489. if( key == NULL ) {
  1490. if( mrl == NULL ) {
  1491. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', mrl='', `valid`=0 WHERE `id`=%d", id );
  1492. } else {
  1493. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', `mrl`='%s', `valid`=1 WHERE `id`=%d", mrl, id );
  1494. };
  1495. } else {
  1496. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=1, `key`='%s', `mrl`='%s', `valid`=1 WHERE `id`=%d",
  1497. key,
  1498. mrl,
  1499. id
  1500. );
  1501. };
  1502. #ifdef DEBUG
  1503. LOG(tQBuf);
  1504. #endif
  1505. m_pDatabase->SilentQuery( tQBuf );
  1506. return true;
  1507. };
  1508. return false;
  1509. };
  1510. #undef LOG
  1511. #undef LOG_ERROR