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.

1932 lines
72 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
​
​
​
​
​
8 years ago
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
8 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
​
​
​
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. DBC_EVT_DATA *tPtr;
  233. Assoc *cArr;
  234. for(int i=0;i<result->size();i++) {
  235. cArr = result->at(i);
  236. tPtr = (DBC_EVT_DATA*)malloc(sizeof(DBC_EVT_DATA));
  237. if( tPtr!= NULL ) {
  238. Assoc::iterator it;
  239. memset(tPtr,0,sizeof(DBC_EVT_DATA));
  240. it = cArr->find("id");
  241. if( it != cArr->end() ){
  242. tPtr->id = atoi( it->second.c_str() );
  243. };
  244. it = cArr->find("node_id");
  245. if( it != cArr->end() ){
  246. tPtr->node_id = atoi( it->second.c_str() );
  247. };
  248. it = cArr->find("uuid");
  249. if( it != cArr->end() ){
  250. strncpy( tPtr->uuid, it->second.c_str(),64 );
  251. };
  252. it = cArr->find("ts");
  253. if( it != cArr->end() ){
  254. tPtr->ts_s = atoi( it->second.c_str() );
  255. };
  256. it = cArr->find("ts_ms");
  257. if( it != cArr->end() ){
  258. tPtr->ts_ms = atoi( it->second.c_str() );
  259. };
  260. it = cArr->find("evt_type");
  261. if( it != cArr->end() ){
  262. tPtr->type = atoi( it->second.c_str() );
  263. };
  264. it = cArr->find("range_digital");
  265. if( it != cArr->end() ){
  266. tPtr->range_digital = atoi( it->second.c_str() );
  267. };
  268. it = cArr->find("azimuth");
  269. if( it != cArr->end() ){
  270. tPtr->az = atof( it->second.c_str() );
  271. };
  272. it = cArr->find("elevation");
  273. if( it != cArr->end() ){
  274. tPtr->el = atof( it->second.c_str() );
  275. };
  276. it = cArr->find("range");
  277. if( it != cArr->end() ){
  278. tPtr->ra = atof( it->second.c_str() );
  279. };
  280. it = cArr->find("area");
  281. if( it != cArr->end() ){
  282. tPtr->area = atof( it->second.c_str() );
  283. };
  284. it = cArr->find("lat");
  285. if( it != cArr->end() ){
  286. tPtr->lat = atof( it->second.c_str() );
  287. };
  288. it = cArr->find("lon");
  289. if( it != cArr->end() ){
  290. tPtr->lon = atof( it->second.c_str() );
  291. };
  292. it = cArr->find("alt");
  293. if( it != cArr->end() ){
  294. tPtr->alt = atof( it->second.c_str() );
  295. };
  296. it = cArr->find("custom");
  297. if( it != cArr->end() ){
  298. strncpy( tPtr->uuid, it->second.c_str(),64 );
  299. };
  300. char tBuf[CLOG];
  301. sprintf(tBuf,"Evt: %d %d %d", tPtr->type, tPtr->ts_s, tPtr->ts_ms );
  302. LOG(tBuf);
  303. VR.push_back(tPtr);
  304. };
  305. };
  306. };
  307. deleteResult( result );
  308. if( m_pDatabase->isConnected()) return true;
  309. };
  310. return false;
  311. };
  312. bool argusDBC::getAllCamRecords( uint32_t SiteID, vectorCamRecords &VR ){
  313. if(m_bIsConnected){
  314. char tQBuf[CTEXT];
  315. AAssoc *result;
  316. sprintf(tQBuf, "SELECT * FROM cameras WHERE `site_id`=%d AND `type`=2 AND `status`=2", SiteID );
  317. #ifdef DEBUG
  318. LOG(tQBuf);
  319. #endif
  320. result = m_pDatabase->Query( tQBuf );
  321. if( result!= NULL ) {
  322. DBC_CAM_DATA *tVal;
  323. Assoc *cArr;
  324. for(int i=0;i<result->size();i++) {
  325. LOG("Cam_Record!");
  326. cArr = result->at(i);
  327. tVal = (DBC_CAM_DATA*)malloc(sizeof(DBC_CAM_DATA));
  328. if( tVal!= NULL ) {
  329. Assoc::iterator it;
  330. memset(tVal,0,sizeof(DBC_CAM_DATA));
  331. it = cArr->find("id");
  332. if( it != cArr->end() ){
  333. tVal->id = atoi( it->second.c_str() );
  334. };
  335. it = cArr->find("site_id");
  336. if( it != cArr->end() ){
  337. tVal->site_id = atoi( it->second.c_str() );
  338. };
  339. it = cArr->find("node_id");
  340. if( it != cArr->end() ){
  341. tVal->node_id = atoi( it->second.c_str() );
  342. };
  343. it = cArr->find("online");
  344. if( it != cArr->end() ){
  345. tVal->online = (bool) atoi( it->second.c_str() );
  346. };
  347. it = cArr->find("type");
  348. if( it != cArr->end() ){
  349. tVal->type = atoi( it->second.c_str() );
  350. };
  351. it = cArr->find("name");
  352. if( it != cArr->end() ){
  353. strncpy( tVal->name, it->second.c_str(), 64 );
  354. };
  355. it = cArr->find("hardware");
  356. if( it != cArr->end() ){
  357. strncpy( tVal->hardware, it->second.c_str(), 32 );
  358. };
  359. it = cArr->find("status");
  360. if( it != cArr->end() ){
  361. tVal->status = atoi( it->second.c_str() );
  362. };
  363. it = cArr->find("session");
  364. if( it != cArr->end() ){
  365. strncpy( tVal->session, it->second.c_str(), 32 );
  366. };
  367. it = cArr->find("protocol");
  368. if( it != cArr->end() ){
  369. tVal->protocol = atoi( it->second.c_str() );
  370. };
  371. it = cArr->find("protocol_aux");
  372. if( it != cArr->end() ){
  373. strncpy( tVal->protocol_aux, it->second.c_str(), 64 );
  374. };
  375. it = cArr->find("lat");
  376. if( it != cArr->end() ){
  377. tVal->lat = atof( it->second.c_str() );
  378. };
  379. it = cArr->find("lon");
  380. if( it != cArr->end() ){
  381. tVal->lon = atof( it->second.c_str() );
  382. };
  383. it = cArr->find("alt");
  384. if( it != cArr->end() ){
  385. tVal->alt = atof( it->second.c_str() );
  386. };
  387. it = cArr->find("picth");
  388. if( it != cArr->end() ){
  389. tVal->pitch = atof( it->second.c_str() );
  390. };
  391. it = cArr->find("roll");
  392. if( it != cArr->end() ){
  393. tVal->roll = atof( it->second.c_str() );
  394. };
  395. it = cArr->find("yaw");
  396. if( it != cArr->end() ){
  397. tVal->yaw = atof( it->second.c_str() );
  398. };
  399. it = cArr->find("imager_size");
  400. if( it != cArr->end() ){
  401. tVal->imager_size = atof( it->second.c_str() );
  402. };
  403. it = cArr->find("imager_ratio");
  404. if( it != cArr->end() ){
  405. tVal->imager_ratio = atof( it->second.c_str() );
  406. };
  407. it = cArr->find("min_f");
  408. if( it != cArr->end() ){
  409. tVal->min_f = atof( it->second.c_str() );
  410. };
  411. it = cArr->find("max_f");
  412. if( it != cArr->end() ){
  413. tVal->max_f = atof( it->second.c_str() );
  414. };
  415. it = cArr->find("access_login");
  416. if( it != cArr->end() ){
  417. strncpy( tVal->username, it->second.c_str(), 16 );
  418. };
  419. it = cArr->find("access_pass");
  420. if( it != cArr->end() ){
  421. strncpy( tVal->password, it->second.c_str(), 16 );
  422. };
  423. it = cArr->find("uuid");
  424. if( it != cArr->end() ){
  425. strncpy( tVal->uuid, it->second.c_str(), CLOG );
  426. };
  427. it = cArr->find("mac");
  428. if( it != cArr->end() ){
  429. strncpy( tVal->mac, it->second.c_str(), 16 );
  430. };
  431. it = cArr->find("ip");
  432. if( it != cArr->end() ){
  433. strncpy( tVal->ip, it->second.c_str(), 16 );
  434. };
  435. it = cArr->find("port");
  436. if( it != cArr->end() ){
  437. tVal->port = atoi( it->second.c_str() );
  438. };
  439. it = cArr->find("onvif_access");
  440. if( it != cArr->end() ){
  441. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  442. };
  443. it = cArr->find("onvif_ctrl");
  444. if( it != cArr->end() ){
  445. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  446. };
  447. it = cArr->find("ptz_token");
  448. if( it != cArr->end() ){
  449. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  450. };
  451. VR.push_back(tVal);
  452. };
  453. //??
  454. //delete(cArr);
  455. };
  456. };
  457. deleteResult( result );
  458. #ifdef DEBUG
  459. sprintf( tQBuf,"Now %d Camera Records in System", VR.size());
  460. LOG(tQBuf);
  461. #endif
  462. if( m_pDatabase->isConnected()) return true;
  463. };
  464. return false;
  465. };
  466. DBC_INT_LIST *argusDBC::listNodes( uint32_t SiteID ){
  467. DBC_INT_LIST * tVal = NULL;
  468. if(m_bIsConnected){
  469. char tQBuf[CTEXT];
  470. AAssoc *result;
  471. sprintf(tQBuf, "SELECT id FROM nodes WHERE `site_id`=%d AND `type` > 0", SiteID );
  472. #ifdef DEBUG
  473. //LOG(tQBuf);
  474. #endif
  475. result = m_pDatabase->Query( tQBuf );
  476. if( result!= NULL ) {
  477. if( result->size() > 0 ) {
  478. Assoc *cArr;
  479. tVal = new DBC_INT_LIST();
  480. if( tVal != NULL ) {
  481. Assoc::iterator it;
  482. for(int i=0;i<result->size();i++) {
  483. cArr = result->at(i);
  484. it = cArr->find("id");
  485. if( it != cArr->end() ){
  486. tVal->push_back( atoi( it->second.c_str() ));
  487. };
  488. };
  489. };
  490. };
  491. };
  492. deleteResult( result );
  493. };
  494. return tVal;
  495. };
  496. DBC_INT_LIST *argusDBC::listNewNodes( uint32_t SiteID, DBC_INT_LIST *old_ids ){
  497. DBC_INT_LIST * tVal = NULL;
  498. char tParam[CTEXT];
  499. //memset( tParam, 0, CTEXT );
  500. sprintf(tParam,"0");
  501. if( old_ids != NULL ) {
  502. char tBuffer[CTEXT];
  503. for(int i = 0; i< old_ids->size(); i++ ) {
  504. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  505. strncpy( tParam, tBuffer, CTEXT );
  506. };
  507. };
  508. if( m_bIsConnected ){
  509. char tQBuf[CTEXT];
  510. AAssoc *result;
  511. sprintf(tQBuf, "SELECT id FROM nodes WHERE `site_id`=%d AND `type` > 0 AND `id` NOT IN(%s)", SiteID, tParam );
  512. #ifdef DEBUG
  513. //LOG(tQBuf);
  514. #endif
  515. result = m_pDatabase->Query( tQBuf );
  516. if( result!= NULL ) {
  517. if( result->size() > 0 ) {
  518. Assoc *cArr;
  519. tVal = new DBC_INT_LIST();
  520. if( tVal != NULL ) {
  521. Assoc::iterator it;
  522. for(int i=0;i<result->size();i++) {
  523. cArr = result->at(i);
  524. it = cArr->find("id");
  525. if( it != cArr->end() ){
  526. tVal->push_back( atoi( it->second.c_str() ));
  527. };
  528. };
  529. };
  530. };
  531. };
  532. deleteResult( result );
  533. };
  534. return tVal;
  535. };
  536. DBC_INT_LIST *argusDBC::listCamMediaStreams( uint32_t CAMID ){
  537. DBC_INT_LIST * tVal = NULL;
  538. if(m_bIsConnected){
  539. char tQBuf[CTEXT];
  540. AAssoc *result;
  541. sprintf(tQBuf, "SELECT id FROM media_streams WHERE `source_id`=%d AND `valid` > 0", CAMID );
  542. #ifdef DEBUG
  543. LOG(tQBuf);
  544. #endif
  545. result = m_pDatabase->Query( tQBuf );
  546. if( result!= NULL ) {
  547. if( result->size() > 0 ) {
  548. Assoc *cArr;
  549. tVal = new DBC_INT_LIST();
  550. if( tVal != NULL ) {
  551. Assoc::iterator it;
  552. for(int i=0;i<result->size();i++) {
  553. cArr = result->at(i);
  554. it = cArr->find("id");
  555. if( it != cArr->end() ){
  556. tVal->push_back( atoi( it->second.c_str() ));
  557. };
  558. };
  559. };
  560. };
  561. };
  562. deleteResult( result );
  563. };
  564. return tVal;
  565. };
  566. DBC_NODE_DATA *argusDBC::getNodeData( uint32_t ID ){
  567. DBC_NODE_DATA * tVal = NULL;
  568. if(m_bIsConnected){
  569. char tQBuf[CTEXT];
  570. AAssoc *result;
  571. sprintf(tQBuf, "SELECT * FROM nodes WHERE `id`=%d", ID );
  572. #ifdef DEBUG
  573. LOG(tQBuf);
  574. #endif
  575. result = m_pDatabase->Query( tQBuf );
  576. if( result!= NULL ) {
  577. if( result->size() > 0 ) {
  578. Assoc *cArr;
  579. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  580. if( tVal != NULL ) {
  581. Assoc::iterator it;
  582. cArr = result->at(0);
  583. it = cArr->find("id");
  584. if( it != cArr->end() ){
  585. tVal->node_id = atoi( it->second.c_str() );
  586. };
  587. it = cArr->find("type");
  588. if( it != cArr->end() ){
  589. tVal->type = (ARGUS_NODE_TYPE) atoi( it->second.c_str() );
  590. };
  591. it = cArr->find("aux_id");
  592. if( it != cArr->end() ){
  593. tVal->aux_id = atoi( it->second.c_str() );
  594. };
  595. it = cArr->find("name");
  596. if( it != cArr->end() ){
  597. sprintf( tVal->name, it->second.c_str() );
  598. };
  599. it = cArr->find("ts_s");
  600. if( it != cArr->end() ){
  601. tVal->tss = atoi( it->second.c_str() );
  602. };
  603. it = cArr->find("ts_ms");
  604. if( it != cArr->end() ){
  605. tVal->tsm = atoi( it->second.c_str() );
  606. };
  607. it = cArr->find("online");
  608. if( it != cArr->end() ){
  609. tVal->online = (bool)atoi( it->second.c_str() );
  610. };
  611. };
  612. };
  613. };
  614. deleteResult( result );
  615. };
  616. return tVal;
  617. };
  618. DBC_FH_DATA *argusDBC::getFlyHunterData( uint32_t ID ){
  619. DBC_FH_DATA * tVal = NULL;
  620. if(m_bIsConnected){
  621. char tQBuf[CTEXT];
  622. AAssoc *result;
  623. sprintf(tQBuf, "SELECT * FROM `module_fh_settings` WHERE `id`=%d", ID );
  624. #ifdef DEBUG
  625. LOG(tQBuf);
  626. #endif
  627. result = m_pDatabase->Query( tQBuf );
  628. if( result!= NULL ) {
  629. if( result->size() > 0 ) {
  630. Assoc *cArr;
  631. tVal = (DBC_FH_DATA*) malloc(sizeof(DBC_FH_DATA));
  632. if( tVal != NULL ) {
  633. memset( tVal, 0 , sizeof(DBC_FH_DATA));
  634. Assoc::iterator it;
  635. cArr = result->at(0);
  636. it = cArr->find("name");
  637. if( it != cArr->end() ){
  638. sprintf( tVal->name, it->second.c_str() );
  639. };
  640. it = cArr->find("lat");
  641. if( it != cArr->end() ){
  642. tVal->lat = atof( it->second.c_str() );
  643. };
  644. it = cArr->find("lon");
  645. if( it != cArr->end() ){
  646. tVal->lon = atof( it->second.c_str() );
  647. };
  648. it = cArr->find("alt");
  649. if( it != cArr->end() ){
  650. tVal->alt = atof( it->second.c_str() );
  651. };
  652. it = cArr->find("dc_module_id");
  653. if( it != cArr->end() ){
  654. tVal->dc_id = atoi( it->second.c_str() );
  655. };
  656. it = cArr->find("uart_id");
  657. if( it != cArr->end() ){
  658. tVal->uart_id = atoi( it->second.c_str() );
  659. };
  660. };
  661. };
  662. };
  663. deleteResult( result );
  664. };
  665. return tVal;
  666. };
  667. DBC_SITE_DATA *argusDBC::getSiteData( uint32_t ID ){
  668. DBC_SITE_DATA * tVal = NULL;
  669. if(m_bIsConnected){
  670. char tQBuf[CTEXT];
  671. AAssoc *result;
  672. sprintf(tQBuf, "SELECT * FROM sites WHERE `id`=%d", ID );
  673. #ifdef DEBUG
  674. LOG(tQBuf);
  675. #endif
  676. result = m_pDatabase->Query( tQBuf );
  677. if( result!= NULL ) {
  678. if( result->size() > 0 ) {
  679. Assoc *cArr;
  680. tVal = (DBC_SITE_DATA*) malloc(sizeof(DBC_SITE_DATA));
  681. if( tVal != NULL ) {
  682. memset( tVal, 0 , sizeof(DBC_SITE_DATA));
  683. Assoc::iterator it;
  684. cArr = result->at(0);
  685. it = cArr->find("account_id");
  686. if( it != cArr->end() ){
  687. tVal->account_id = atoi( it->second.c_str() );
  688. };
  689. it = cArr->find("name");
  690. if( it != cArr->end() ){
  691. sprintf( tVal->name, it->second.c_str() );
  692. };
  693. it = cArr->find("server_ident");
  694. if( it != cArr->end() ){
  695. sprintf( tVal->ident, it->second.c_str() );
  696. };
  697. it = cArr->find("anchor_lat");
  698. if( it != cArr->end() ){
  699. tVal->lat = atof( it->second.c_str() );
  700. };
  701. it = cArr->find("anchor_lon");
  702. if( it != cArr->end() ){
  703. tVal->lon = atof( it->second.c_str() );
  704. };
  705. it = cArr->find("anchor_alt");
  706. if( it != cArr->end() ){
  707. tVal->alt = atof( it->second.c_str() );
  708. };
  709. it = cArr->find("hemisphere");
  710. if( it != cArr->end() ){
  711. tVal->SH = ( it->second.c_str()[0] == 'S' );
  712. };
  713. it = cArr->find("map_id");
  714. if( it != cArr->end() ){
  715. tVal->map_id = atoi( it->second.c_str() );
  716. };
  717. };
  718. };
  719. };
  720. deleteResult( result );
  721. };
  722. return tVal;
  723. };
  724. DBC_LRF_DATA *argusDBC::getLRFData( uint32_t ID ){
  725. DBC_LRF_DATA * tVal = NULL;
  726. if(m_bIsConnected){
  727. char tQBuf[CTEXT];
  728. AAssoc *result;
  729. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
  730. #ifdef DEBUG
  731. LOG(tQBuf);
  732. #endif
  733. result = m_pDatabase->Query( tQBuf );
  734. if( result!= NULL ) {
  735. if( result->size() > 0 ) {
  736. Assoc *cArr;
  737. tVal = (DBC_LRF_DATA*) malloc(sizeof(DBC_LRF_DATA));
  738. if( tVal != NULL ) {
  739. memset( tVal, 0 , sizeof(DBC_LRF_DATA));
  740. Assoc::iterator it;
  741. cArr = result->at(0);
  742. it = cArr->find("lat");
  743. if( it != cArr->end() ){
  744. tVal->lat = atof( it->second.c_str() );
  745. };
  746. it = cArr->find("lon");
  747. if( it != cArr->end() ){
  748. tVal->lon = atof( it->second.c_str() );
  749. };
  750. it = cArr->find("alt");
  751. if( it != cArr->end() ){
  752. tVal->alt = atof( it->second.c_str() );
  753. };
  754. it = cArr->find("pitch");
  755. if( it != cArr->end() ){
  756. tVal->pitch = atof( it->second.c_str() );
  757. };
  758. it = cArr->find("yaw");
  759. if( it != cArr->end() ){
  760. tVal->yaw = atof( it->second.c_str() );
  761. };
  762. it = cArr->find("roll");
  763. if( it != cArr->end() ){
  764. tVal->roll = atof( it->second.c_str() );
  765. };
  766. it = cArr->find("ip_address");
  767. if( it != cArr->end() ){
  768. sprintf( tVal->master_host, it->second.c_str() );
  769. };
  770. it = cArr->find("aux_ip_address");
  771. if( it != cArr->end() ){
  772. sprintf( tVal->slave_host, it->second.c_str() );
  773. };
  774. it = cArr->find("port");
  775. if( it != cArr->end() ){
  776. tVal->master_port = atoi( it->second.c_str() );
  777. };
  778. it = cArr->find("aux_port");
  779. if( it != cArr->end() ){
  780. tVal->slave_port = atoi( it->second.c_str() );
  781. };
  782. };
  783. };
  784. };
  785. deleteResult( result );
  786. };
  787. return tVal;
  788. };
  789. DBC_INT_LIST *argusDBC::listModules( uint32_t ServerID ){
  790. DBC_INT_LIST * tVal = NULL;
  791. if(m_bIsConnected){
  792. char tQBuf[CTEXT];
  793. AAssoc *result;
  794. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1", ServerID );
  795. #ifdef DEBUG
  796. //LOG(tQBuf);
  797. #endif
  798. result = m_pDatabase->Query( tQBuf );
  799. if( result!= NULL ) {
  800. if( result->size() > 0 ) {
  801. Assoc *cArr;
  802. tVal = new DBC_INT_LIST();
  803. if( tVal != NULL ) {
  804. Assoc::iterator it;
  805. for(int i=0;i<result->size();i++) {
  806. cArr = result->at(i);
  807. it = cArr->find("id");
  808. if( it != cArr->end() ){
  809. tVal->push_back( atoi( it->second.c_str() ));
  810. };
  811. };
  812. };
  813. };
  814. };
  815. deleteResult( result );
  816. };
  817. return tVal;
  818. };
  819. DBC_INT_LIST *argusDBC::listMediaStreams( uint32_t SiteId, uint32_t MediaServerID, uint8_t listMode ){
  820. DBC_INT_LIST * tVal = NULL;
  821. if(m_bIsConnected){
  822. char tQBuf[CTEXT];
  823. AAssoc *result;
  824. switch( listMode ){
  825. 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;
  826. 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;
  827. default: sprintf(tQBuf, "SELECT id FROM media_server_streams WHERE `site_id`=%d AND `media_server_id`=%d", SiteId, MediaServerID );
  828. };
  829. #ifdef DEBUG
  830. //LOG(tQBuf);
  831. #endif
  832. result = m_pDatabase->Query( tQBuf );
  833. if( result!= NULL ) {
  834. if( result->size() > 0 ) {
  835. Assoc *cArr;
  836. tVal = new DBC_INT_LIST();
  837. if( tVal != NULL ) {
  838. Assoc::iterator it;
  839. for(int i=0;i<result->size();i++) {
  840. cArr = result->at(i);
  841. it = cArr->find("id");
  842. if( it != cArr->end() ){
  843. tVal->push_back( atoi( it->second.c_str() ));
  844. };
  845. };
  846. };
  847. };
  848. };
  849. deleteResult( result );
  850. };
  851. return tVal;
  852. };
  853. DBC_INT_LIST *argusDBC::listSockets( uint32_t NodeID ){
  854. DBC_INT_LIST * tVal = NULL;
  855. if(m_bIsConnected){
  856. char tQBuf[CTEXT];
  857. AAssoc *result;
  858. sprintf(tQBuf, "SELECT id FROM sockets WHERE `node_id`=%d", NodeID );
  859. #ifdef DEBUG
  860. LOG(tQBuf);
  861. #endif
  862. result = m_pDatabase->Query( tQBuf );
  863. if( result!= NULL ) {
  864. if( result->size() > 0 ) {
  865. Assoc *cArr;
  866. tVal = new DBC_INT_LIST();
  867. if( tVal != NULL ) {
  868. Assoc::iterator it;
  869. for(int i=0;i<result->size();i++) {
  870. cArr = result->at(i);
  871. it = cArr->find("id");
  872. if( it != cArr->end() ){
  873. tVal->push_back( atoi( it->second.c_str() ));
  874. };
  875. };
  876. };
  877. };
  878. };
  879. deleteResult( result );
  880. };
  881. return tVal;
  882. };
  883. DBC_INT_LIST *argusDBC::listNewModules( uint32_t ServerID, DBC_INT_LIST * old_ids ){
  884. DBC_INT_LIST * tVal = NULL;
  885. char tParam[CTEXT];
  886. //memset( tParam, 0, CTEXT );
  887. sprintf(tParam,"0");
  888. if( old_ids != NULL ) {
  889. char tBuffer[CTEXT];
  890. for(int i = 0; i< old_ids->size(); i++ ) {
  891. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  892. strncpy( tParam, tBuffer, CTEXT );
  893. };
  894. };
  895. if(m_bIsConnected){
  896. char tQBuf[CTEXT];
  897. AAssoc *result;
  898. sprintf(tQBuf, "SELECT id FROM server_modules WHERE `server_id`=%d AND `enabled`=1 AND `id` NOT IN (%s)", ServerID, tParam );
  899. #ifdef DEBUG
  900. //LOG(tQBuf);
  901. #endif
  902. result = m_pDatabase->Query( tQBuf );
  903. if( result!= NULL ) {
  904. if( result->size() > 0 ) {
  905. Assoc *cArr;
  906. tVal = new DBC_INT_LIST();
  907. if( tVal != NULL ) {
  908. Assoc::iterator it;
  909. for(int i=0;i<result->size();i++) {
  910. cArr = result->at(i);
  911. it = cArr->find("id");
  912. if( it != cArr->end() ){
  913. tVal->push_back( atoi( it->second.c_str() ));
  914. };
  915. };
  916. };
  917. };
  918. };
  919. deleteResult( result );
  920. };
  921. return tVal;
  922. };
  923. uint32_t argusDBC::getCamId( CSTR UUID ){
  924. uint32_t RetVal = 0;
  925. if(m_bIsConnected){
  926. char tQBuf[CTEXT];
  927. AAssoc *result;
  928. sprintf(tQBuf, "SELECT id FROM cameras WHERE `uuid`='%s'", UUID );
  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. Assoc::iterator it;
  937. cArr = result->at(0);
  938. it = cArr->find("id");
  939. if( it != cArr->end() ){
  940. RetVal = atoi( it->second.c_str() );
  941. };
  942. };
  943. };
  944. deleteResult( result );
  945. };
  946. return RetVal;
  947. };
  948. DBC_CAM_DATA *argusDBC::getCamData( uint32_t ID ){
  949. DBC_CAM_DATA *tVal = NULL;
  950. if(m_bIsConnected){
  951. char tQBuf[CTEXT];
  952. AAssoc *result;
  953. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  954. #ifdef DEBUG
  955. LOG(tQBuf);
  956. #endif
  957. result = m_pDatabase->Query( tQBuf );
  958. if( result!= NULL ) {
  959. if( result->size() > 0 ) {
  960. Assoc *cArr;
  961. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  962. if( tVal != NULL ) {
  963. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  964. tVal->id = ID;
  965. Assoc::iterator it;
  966. cArr = result->at(0);
  967. it = cArr->find("site_id");
  968. if( it != cArr->end() ){
  969. tVal->site_id = atoi( it->second.c_str() );
  970. };
  971. it = cArr->find("node_id");
  972. if( it != cArr->end() ){
  973. tVal->node_id = atoi( it->second.c_str() );
  974. };
  975. it = cArr->find("online");
  976. if( it != cArr->end() ){
  977. tVal->online = (bool) atoi( it->second.c_str() );
  978. };
  979. it = cArr->find("type");
  980. if( it != cArr->end() ){
  981. tVal->type = atoi( it->second.c_str() );
  982. };
  983. it = cArr->find("name");
  984. if( it != cArr->end() ){
  985. strncpy( tVal->name, it->second.c_str(), 64 );
  986. };
  987. it = cArr->find("hardware");
  988. if( it != cArr->end() ){
  989. strncpy( tVal->hardware, it->second.c_str(), 32 );
  990. };
  991. it = cArr->find("status");
  992. if( it != cArr->end() ){
  993. tVal->status = atoi( it->second.c_str() );
  994. };
  995. it = cArr->find("session");
  996. if( it != cArr->end() ){
  997. strncpy( tVal->session, it->second.c_str(), 32 );
  998. };
  999. it = cArr->find("protocol");
  1000. if( it != cArr->end() ){
  1001. tVal->protocol = atoi( it->second.c_str() );
  1002. };
  1003. it = cArr->find("protocol_aux");
  1004. if( it != cArr->end() ){
  1005. strncpy( tVal->protocol_aux, it->second.c_str(), 64 );
  1006. };
  1007. it = cArr->find("uart_id");
  1008. if( it != cArr->end() ){
  1009. tVal->uart_id = atoi( it->second.c_str() );
  1010. };
  1011. it = cArr->find("lat");
  1012. if( it != cArr->end() ){
  1013. tVal->lat = atof( it->second.c_str() );
  1014. };
  1015. it = cArr->find("lon");
  1016. if( it != cArr->end() ){
  1017. tVal->lon = atof( it->second.c_str() );
  1018. };
  1019. it = cArr->find("alt");
  1020. if( it != cArr->end() ){
  1021. tVal->alt = atof( it->second.c_str() );
  1022. };
  1023. it = cArr->find("picth");
  1024. if( it != cArr->end() ){
  1025. tVal->pitch = atof( it->second.c_str() );
  1026. };
  1027. it = cArr->find("roll");
  1028. if( it != cArr->end() ){
  1029. tVal->roll = atof( it->second.c_str() );
  1030. };
  1031. it = cArr->find("yaw");
  1032. if( it != cArr->end() ){
  1033. tVal->yaw = atof( it->second.c_str() );
  1034. };
  1035. it = cArr->find("imager_size");
  1036. if( it != cArr->end() ){
  1037. tVal->imager_size = atof( it->second.c_str() );
  1038. };
  1039. it = cArr->find("imager_ratio");
  1040. if( it != cArr->end() ){
  1041. tVal->imager_ratio = atof( it->second.c_str() );
  1042. };
  1043. it = cArr->find("min_f");
  1044. if( it != cArr->end() ){
  1045. tVal->min_f = atof( it->second.c_str() );
  1046. };
  1047. it = cArr->find("max_f");
  1048. if( it != cArr->end() ){
  1049. tVal->max_f = atof( it->second.c_str() );
  1050. };
  1051. it = cArr->find("pan_model");
  1052. if( it != cArr->end() ){
  1053. tVal->pan_model = atoi( it->second.c_str() );
  1054. };
  1055. it = cArr->find("pan_angles");
  1056. if( it != cArr->end() ){
  1057. tVal->pan_angles = atoi( it->second.c_str() );
  1058. };
  1059. it = cArr->find("tilt_model");
  1060. if( it != cArr->end() ){
  1061. tVal->tilt_model = atoi( it->second.c_str() );
  1062. };
  1063. it = cArr->find("tilt_angles");
  1064. if( it != cArr->end() ){
  1065. tVal->tilt_angles = atoi( it->second.c_str() );
  1066. };
  1067. it = cArr->find("access_login");
  1068. if( it != cArr->end() ){
  1069. strncpy( tVal->username, it->second.c_str(), 16 );
  1070. };
  1071. it = cArr->find("access_pass");
  1072. if( it != cArr->end() ){
  1073. strncpy( tVal->password, it->second.c_str(), 16 );
  1074. };
  1075. it = cArr->find("uuid");
  1076. if( it != cArr->end() ){
  1077. strncpy( tVal->uuid, it->second.c_str(), 64 );
  1078. };
  1079. it = cArr->find("mac");
  1080. if( it != cArr->end() ){
  1081. strncpy( tVal->mac, it->second.c_str(), 16 );
  1082. };
  1083. it = cArr->find("ip");
  1084. if( it != cArr->end() ){
  1085. strncpy( tVal->ip, it->second.c_str(), 16 );
  1086. };
  1087. it = cArr->find("port");
  1088. if( it != cArr->end() ){
  1089. tVal->port = atoi( it->second.c_str() );
  1090. };
  1091. it = cArr->find("onvif_access");
  1092. if( it != cArr->end() ){
  1093. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  1094. };
  1095. it = cArr->find("onvif_ctrl");
  1096. if( it != cArr->end() ){
  1097. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  1098. };
  1099. it = cArr->find("ptz_token");
  1100. if( it != cArr->end() ){
  1101. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  1102. };
  1103. };
  1104. };
  1105. };
  1106. deleteResult( result );
  1107. };
  1108. return tVal;
  1109. };
  1110. DBC_MODULE_DATA *argusDBC::getModuleData( uint32_t ID ){
  1111. DBC_MODULE_DATA *tVal = NULL;
  1112. if(m_bIsConnected){
  1113. char tQBuf[CTEXT];
  1114. AAssoc *result;
  1115. sprintf(tQBuf, "SELECT * FROM server_modules WHERE `id`=%d", ID );
  1116. #ifdef DEBUG
  1117. LOG(tQBuf);
  1118. #endif
  1119. result = m_pDatabase->Query( tQBuf );
  1120. if( result!= NULL ) {
  1121. if( result->size() > 0 ) {
  1122. Assoc *cArr;
  1123. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  1124. if( tVal != NULL ) {
  1125. Assoc::iterator it;
  1126. cArr = result->at(0);
  1127. it = cArr->find("id");
  1128. if( it != cArr->end() ){
  1129. tVal->module_id = atoi( it->second.c_str() );
  1130. };
  1131. it = cArr->find("aux_id");
  1132. if( it != cArr->end() ){
  1133. tVal->aux_id = atoi( it->second.c_str() );
  1134. };
  1135. it = cArr->find("type");
  1136. if( it != cArr->end() ){
  1137. tVal->type = atoi( it->second.c_str() );
  1138. };
  1139. it = cArr->find("enabled");
  1140. if( it != cArr->end() ){
  1141. tVal->enabled = (bool)atoi( it->second.c_str() );
  1142. };
  1143. };
  1144. };
  1145. };
  1146. deleteResult( result );
  1147. };
  1148. return tVal;
  1149. };
  1150. DBC_SOCKET_DATA *argusDBC::getSocketData( uint32_t ID ){
  1151. DBC_SOCKET_DATA *tVal = NULL;
  1152. if(m_bIsConnected){
  1153. char tQBuf[CTEXT];
  1154. AAssoc *result;
  1155. sprintf(tQBuf, "SELECT * FROM sockets WHERE `id`=%d", ID );
  1156. #ifdef DEBUG
  1157. LOG(tQBuf);
  1158. #endif
  1159. result = m_pDatabase->Query( tQBuf );
  1160. if( result!= NULL ) {
  1161. if( result->size() > 0 ) {
  1162. Assoc *cArr;
  1163. tVal = (DBC_SOCKET_DATA*) malloc(sizeof(DBC_SOCKET_DATA));
  1164. if( tVal != NULL ) {
  1165. Assoc::iterator it;
  1166. cArr = result->at(0);
  1167. it = cArr->find("id");
  1168. if( it != cArr->end() ){
  1169. tVal->socket_id = atoi( it->second.c_str() );
  1170. };
  1171. it = cArr->find("aux_id");
  1172. if( it != cArr->end() ){
  1173. tVal->aux_id = atoi( it->second.c_str() );
  1174. };
  1175. it = cArr->find("type");
  1176. if( it != cArr->end() ){
  1177. tVal->type = atoi( it->second.c_str() );
  1178. };
  1179. it = cArr->find("binded");
  1180. if( it != cArr->end() ){
  1181. tVal->binded = (bool)atoi( it->second.c_str() );
  1182. };
  1183. };
  1184. };
  1185. };
  1186. deleteResult( result );
  1187. };
  1188. return tVal;
  1189. };
  1190. DBC_MEDIA_STREAM_DATA *argusDBC::getMediaStreamData( uint32_t ID ){
  1191. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  1192. if(m_bIsConnected){
  1193. char tQBuf[CTEXT];
  1194. AAssoc *result;
  1195. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  1196. #ifdef DEBUG
  1197. //LOG(tQBuf);
  1198. #endif
  1199. result = m_pDatabase->Query( tQBuf );
  1200. if( result!= NULL ) {
  1201. if( result->size() > 0 ) {
  1202. Assoc *cArr;
  1203. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  1204. if( tVal != NULL ) {
  1205. Assoc::iterator it;
  1206. cArr = result->at(0);
  1207. tVal->id = ID;
  1208. it = cArr->find("source_id");
  1209. if( it != cArr->end() ){
  1210. tVal->source_id = atoi( it->second.c_str() );
  1211. };
  1212. it = cArr->find("site_id");
  1213. if( it != cArr->end() ){
  1214. tVal->site_id = atoi( it->second.c_str() );
  1215. };
  1216. it = cArr->find("source_type");
  1217. if( it != cArr->end() ){
  1218. sprintf( tVal->source_type, it->second.c_str() );
  1219. };
  1220. it = cArr->find("valid");
  1221. if( it != cArr->end() ){
  1222. tVal->valid = (bool) atoi( it->second.c_str() );
  1223. };
  1224. it = cArr->find("token");
  1225. if( it != cArr->end() ){
  1226. sprintf( tVal->token, it->second.c_str() );
  1227. };
  1228. it = cArr->find("mrl_proto");
  1229. if( it != cArr->end() ){
  1230. sprintf( tVal->mrl_proto, it->second.c_str() );
  1231. };
  1232. it = cArr->find("mrl_port");
  1233. if( it != cArr->end() ){
  1234. tVal->mrl_port = atoi( it->second.c_str() );
  1235. };
  1236. it = cArr->find("mrl");
  1237. if( it != cArr->end() ){
  1238. sprintf( tVal->mrl, it->second.c_str() );
  1239. };
  1240. };
  1241. };
  1242. };
  1243. deleteResult( result );
  1244. };
  1245. return tVal;
  1246. };
  1247. DBC_MEDIA_SERVER_DATA *argusDBC::getMediaServerData( uint32_t ID ){
  1248. DBC_MEDIA_SERVER_DATA *tVal = NULL;
  1249. if(m_bIsConnected){
  1250. char tQBuf[CTEXT];
  1251. AAssoc *result;
  1252. sprintf(tQBuf, "SELECT * FROM media_servers WHERE `id`=%d", ID );
  1253. #ifdef DEBUG
  1254. LOG(tQBuf);
  1255. #endif
  1256. result = m_pDatabase->Query( tQBuf );
  1257. if( result!= NULL ) {
  1258. if( result->size() > 0 ) {
  1259. Assoc *cArr;
  1260. tVal = (DBC_MEDIA_SERVER_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_DATA));
  1261. if( tVal != NULL ) {
  1262. Assoc::iterator it;
  1263. cArr = result->at(0);
  1264. tVal->id = ID;
  1265. it = cArr->find("secret_key");
  1266. if( it != cArr->end() ){
  1267. sprintf( tVal->key, it->second.c_str() );
  1268. };
  1269. it = cArr->find("ip");
  1270. if( it != cArr->end() ){
  1271. sprintf( tVal->ip, it->second.c_str() );
  1272. };
  1273. it = cArr->find("REST_port");
  1274. if( it != cArr->end() ){
  1275. tVal->port = atoi( it->second.c_str() );
  1276. };
  1277. it = cArr->find("virtual_host");
  1278. if( it != cArr->end() ){
  1279. sprintf( tVal->host, it->second.c_str() );
  1280. };
  1281. it = cArr->find("recording");
  1282. if( it != cArr->end() ){
  1283. tVal->recording = (bool)atoi( it->second.c_str() );
  1284. };
  1285. sprintf( tVal->name,"ZLMedia_%d", tVal->id );
  1286. };
  1287. };
  1288. };
  1289. deleteResult( result );
  1290. };
  1291. return tVal;
  1292. };
  1293. DBC_MEDIA_SERVER_STREAM_DATA *argusDBC::getMediaServerStreamData( uint32_t ID ){
  1294. DBC_MEDIA_SERVER_STREAM_DATA *tVal = NULL;
  1295. if(m_bIsConnected){
  1296. char tQBuf[MAX_PATH_LEN];
  1297. AAssoc *result;
  1298. sprintf(tQBuf, "SELECT `id`, `media_stream_id`, `online`, `key`, `mrl` FROM media_server_streams WHERE `id`=%d", ID );
  1299. #ifdef DEBUG
  1300. //LOG(tQBuf);
  1301. #endif
  1302. result = m_pDatabase->Query( tQBuf );
  1303. if( result!= NULL ) {
  1304. if( result->size() > 0 ) {
  1305. Assoc *cArr;
  1306. tVal = (DBC_MEDIA_SERVER_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_SERVER_STREAM_DATA));
  1307. if( tVal != NULL ) {
  1308. Assoc::iterator it;
  1309. cArr = result->at(0);
  1310. tVal->id = ID;
  1311. it = cArr->find("media_stream_id");
  1312. if( it != cArr->end() ){
  1313. tVal->media_stream_id = atoi( it->second.c_str() );
  1314. };
  1315. it = cArr->find("online");
  1316. if( it != cArr->end() ){
  1317. tVal->online = atoi( it->second.c_str() );
  1318. };
  1319. it = cArr->find("key");
  1320. if( it != cArr->end() ){
  1321. sprintf( tVal->key, it->second.c_str() );
  1322. };
  1323. it = cArr->find("mrl");
  1324. if( it != cArr->end() ){
  1325. sprintf( tVal->mrl, it->second.c_str() );
  1326. };
  1327. };
  1328. };
  1329. };
  1330. deleteResult( result );
  1331. };
  1332. return tVal;
  1333. };
  1334. void argusDBC::registerServer(uint32_t SiteID, CSTR identity ){
  1335. if(m_bIsConnected){
  1336. char tQBuf[CTEXT];
  1337. sprintf(tQBuf,"INSERT INTO servers SET `site_id`=%d, `identity`='%s', `online`=1 ON DUPLICATE KEY UPDATE `online`=1", SiteID, identity );
  1338. #ifdef DEBUG
  1339. LOG(tQBuf);
  1340. #endif
  1341. m_pDatabase->SilentQuery( tQBuf );
  1342. // Clenup Nodes and services ...
  1343. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `site_id`=%d ", SiteID );
  1344. #ifdef DEBUG
  1345. LOG(tQBuf);
  1346. #endif
  1347. m_pDatabase->SilentQuery( tQBuf );
  1348. sprintf(tQBuf,"UPDATE cameras SET `online`=0 WHERE `site_id`=%d ", SiteID );
  1349. #ifdef DEBUG
  1350. LOG(tQBuf);
  1351. #endif
  1352. m_pDatabase->SilentQuery( tQBuf );
  1353. sprintf(tQBuf,"UPDATE media_streams SET `valid`=0 WHERE `site_id`=%d ", SiteID );
  1354. #ifdef DEBUG
  1355. LOG(tQBuf);
  1356. #endif
  1357. m_pDatabase->SilentQuery( tQBuf );
  1358. sprintf(tQBuf,"UPDATE media_server_streams SET `valid`=0, `online`=0 WHERE `site_id`=%d ", SiteID );
  1359. #ifdef DEBUG
  1360. LOG(tQBuf);
  1361. #endif
  1362. m_pDatabase->SilentQuery( tQBuf );
  1363. uint32_t sid = getServerID( identity );
  1364. if( sid ) {
  1365. 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 );
  1366. #ifdef DEBUG
  1367. LOG(tQBuf);
  1368. #endif
  1369. m_pDatabase->SilentQuery( tQBuf );
  1370. };
  1371. };
  1372. };
  1373. void argusDBC::unregisterServer(uint32_t ServerID, uint32_t NodeID ){
  1374. if(m_bIsConnected){
  1375. char tQBuf[CTEXT];
  1376. sprintf(tQBuf,"UPDATE servers SET `online`=0 WHERE `id` = %d", ServerID );
  1377. #ifdef DEBUG
  1378. LOG(tQBuf);
  1379. #endif
  1380. m_pDatabase->SilentQuery( tQBuf );
  1381. setNodeOffline( NodeID );
  1382. };
  1383. };
  1384. void argusDBC::setNodeOnline( uint32_t NodeID ){
  1385. if(m_bIsConnected){
  1386. char tQBuf[CTEXT];
  1387. if( NodeID ) {
  1388. sprintf(tQBuf,"UPDATE nodes SET `online`=1 WHERE `id` = %d", NodeID );
  1389. #ifdef DEBUG
  1390. LOG(tQBuf);
  1391. #endif
  1392. m_pDatabase->SilentQuery( tQBuf );
  1393. //sprintf(tQBuf,"UPDATE sockets SET `state`=1 WHERE `node_id` = %d", NodeID );
  1394. #ifdef DEBUG
  1395. //LOG(tQBuf);
  1396. #endif
  1397. //m_pDatabase->SilentQuery( tQBuf );
  1398. };
  1399. };
  1400. };
  1401. void argusDBC::setNodeOffline( uint32_t NodeID ){
  1402. if(m_bIsConnected){
  1403. char tQBuf[CTEXT];
  1404. if( NodeID ) {
  1405. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  1406. #ifdef DEBUG
  1407. LOG(tQBuf);
  1408. #endif
  1409. m_pDatabase->SilentQuery( tQBuf );
  1410. //sprintf(tQBuf,"UPDATE sockets SET `state`=0 WHERE `node_id` = %d", NodeID );
  1411. #ifdef DEBUG
  1412. //LOG(tQBuf);
  1413. #endif
  1414. //m_pDatabase->SilentQuery( tQBuf );
  1415. };
  1416. };
  1417. };
  1418. uint32_t argusDBC::getServerID( CSTR identity ){
  1419. uint32_t RetVal = 0;
  1420. if(m_bIsConnected){
  1421. char tQBuf[CTEXT];
  1422. sprintf(tQBuf,"SELECT id FROM servers WHERE `identity`='%s'", identity );
  1423. #ifdef DEBUG
  1424. LOG(tQBuf);
  1425. #endif
  1426. AAssoc *result;
  1427. result = m_pDatabase->Query( tQBuf );
  1428. if( result!= NULL ) {
  1429. if( result->size() > 0 ) {
  1430. Assoc * cArr;
  1431. Assoc::iterator it;
  1432. cArr = result->at(0);
  1433. it = cArr->find("id");
  1434. if( it != cArr->end() ){
  1435. RetVal = atoi( it->second.c_str() );
  1436. };
  1437. };
  1438. };
  1439. deleteResult( result );
  1440. };
  1441. return RetVal;
  1442. };
  1443. uint32_t argusDBC::getServerNode( uint32_t SID ){
  1444. uint32_t RetVal = 0;
  1445. if(m_bIsConnected){
  1446. char tQBuf[CTEXT];
  1447. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  1448. #ifdef DEBUG
  1449. LOG(tQBuf);
  1450. #endif
  1451. AAssoc *result;
  1452. result = m_pDatabase->Query( tQBuf );
  1453. if( result!= NULL ) {
  1454. if( result->size() > 0 ) {
  1455. Assoc * cArr;
  1456. Assoc::iterator it;
  1457. cArr = result->at(0);
  1458. it = cArr->find("id");
  1459. if( it != cArr->end() ){
  1460. RetVal = atoi( it->second.c_str() );
  1461. };
  1462. };
  1463. };
  1464. deleteResult( result );
  1465. };
  1466. return RetVal;
  1467. };
  1468. uint64_t argusDBC::addLocalMediaSource( uint32_t SiteID, CSTR path, uint32_t width, uint32_t height ){
  1469. uint64_t RetVal = 0;
  1470. if(m_bIsConnected){
  1471. char tQBuf[1024];
  1472. char tM5[33];
  1473. CSTR tMD5 = MD5String( path );
  1474. if( tMD5 != NULL ) {
  1475. memcpy( tM5, tMD5, 33 );
  1476. tM5[32]=(char)0;
  1477. free( tMD5 );
  1478. 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",
  1479. SiteID, tM5, width, height, path, width, height );
  1480. #ifdef DEBUG
  1481. LOG(tQBuf);
  1482. #endif
  1483. RetVal = m_pDatabase->QueryID( tQBuf );
  1484. };
  1485. };
  1486. LOG("Func Finished");
  1487. return RetVal;
  1488. };
  1489. uint32_t argusDBC::addNodeSocket( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, uint8_t SocketMode, bool multidrop, CSTR path, CSTR name ){
  1490. uint32_t retVal = 0;
  1491. if(m_bIsConnected){
  1492. char tQBuf[2048];
  1493. char tM5[33];
  1494. CSTR tMD5 = MD5String( path );
  1495. if( tMD5 != NULL ) {
  1496. memcpy( tM5, tMD5, 33 );
  1497. free( tMD5 );
  1498. if( multidrop ) {
  1499. 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'",
  1500. NodeID, SocketType, SocketMode, tM5, path, name, auxID, name );
  1501. } else {
  1502. 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'",
  1503. NodeID, SocketType, SocketMode, tM5, path, name, auxID, name );
  1504. };
  1505. #ifdef DEBUG
  1506. LOG(tQBuf);
  1507. #endif
  1508. //m_pDatabase->SilentQuery( tQBuf );
  1509. retVal = (uint32_t) m_pDatabase->QueryID( tQBuf );
  1510. };
  1511. };
  1512. return retVal;
  1513. };
  1514. uint32_t argusDBC::getNodeSocketID( uint32_t NodeID, uint32_t auxID, uint8_t SocketType, CSTR path ){
  1515. uint32_t retVal = 0;
  1516. if(m_bIsConnected){
  1517. char tQBuf[2048];
  1518. char tM5[33];
  1519. CSTR tMD5 = MD5String( path );
  1520. if( tMD5 != NULL ) {
  1521. memcpy( tM5, tMD5, 33 );
  1522. free( tMD5 );
  1523. sprintf( tQBuf,"SELECT id FROM `sockets` WHERE `node_id`=%d AND `type`=%d AND `md5`='%s' AND `aux_id`=%d", NodeID, SocketType, tM5, auxID );
  1524. #ifdef DEBUG
  1525. LOG(tQBuf);
  1526. #endif
  1527. AAssoc *result;
  1528. result = m_pDatabase->Query( tQBuf );
  1529. if( result!= NULL ) {
  1530. if( result->size() > 0 ) {
  1531. Assoc * cArr;
  1532. Assoc::iterator it;
  1533. cArr = result->at(0);
  1534. it = cArr->find("id");
  1535. if( it != cArr->end() ){
  1536. retVal = atoi( it->second.c_str() );
  1537. };
  1538. };
  1539. };
  1540. deleteResult( result );
  1541. };
  1542. };
  1543. return retVal;
  1544. };
  1545. CSTR argusDBC::getNodeIP4( uint32_t NodeID ){
  1546. return getSocketName( NodeID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV4 );
  1547. };
  1548. CSTR argusDBC::getNodeIP6( uint32_t NodeID ){
  1549. return getSocketName( NodeID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV6 );
  1550. };
  1551. CSTR argusDBC::getSocketPath( uint32_t NodeID, uint32_t Type ){
  1552. if(m_bIsConnected){
  1553. char tQBuf[CTEXT];
  1554. sprintf( tQBuf, "SELECT `path` FROM `sockets` WHERE `node_id`=%d AND `type`=%d LIMIT 1",
  1555. NodeID, Type );
  1556. #ifdef DEBUG
  1557. LOG(tQBuf);
  1558. #endif
  1559. AAssoc *result;
  1560. result = m_pDatabase->Query( tQBuf );
  1561. if( result!= NULL ) {
  1562. if( result->size() > 0 ) {
  1563. Assoc * cArr;
  1564. Assoc::iterator it;
  1565. cArr = result->at(0);
  1566. it = cArr->find("path");
  1567. if( it != cArr->end() ){
  1568. strncpy( tQBuf, it->second.c_str(), CTEXT );
  1569. };
  1570. };
  1571. };
  1572. deleteResult( result );
  1573. return strdup(tQBuf);
  1574. };
  1575. return NULL;
  1576. };
  1577. CSTR argusDBC::getSocketName( uint32_t NodeID, uint32_t Type ){
  1578. if(m_bIsConnected){
  1579. char tQBuf[CTEXT];
  1580. sprintf( tQBuf, "SELECT `name` FROM `sockets` WHERE `node_id`=%d AND `type`=%d LIMIT 1",
  1581. NodeID, Type );
  1582. #ifdef DEBUG
  1583. LOG(tQBuf);
  1584. #endif
  1585. AAssoc *result;
  1586. result = m_pDatabase->Query( tQBuf );
  1587. if( result!= NULL ) {
  1588. if( result->size() > 0 ) {
  1589. Assoc * cArr;
  1590. Assoc::iterator it;
  1591. cArr = result->at(0);
  1592. it = cArr->find("name");
  1593. if( it != cArr->end() ){
  1594. strncpy( tQBuf, it->second.c_str(), CTEXT );
  1595. };
  1596. };
  1597. };
  1598. deleteResult( result );
  1599. return strdup(tQBuf);
  1600. };
  1601. return NULL;
  1602. };
  1603. KAFKA_SETTINGS *argusDBC::getSettingsKAFKA( uint32_t ModuleID ){
  1604. KAFKA_SETTINGS *tVal = NULL;
  1605. if( m_bIsConnected ) {
  1606. char tQBuf[CTEXT];
  1607. AAssoc *result;
  1608. sprintf( tQBuf, "SELECT brocker_address, brocker_port FROM modules_kafka WHERE module_id = %d", ModuleID );
  1609. #ifdef DEBUG
  1610. LOG(tQBuf);
  1611. #endif
  1612. result = m_pDatabase->Query( tQBuf );
  1613. if( result!= NULL ) {
  1614. if( result->size() > 0 ) {
  1615. Assoc *cArr;
  1616. tVal = (KAFKA_SETTINGS*) malloc(sizeof(KAFKA_SETTINGS));
  1617. if( tVal != NULL ) {
  1618. Assoc::iterator it;
  1619. cArr = result->at(0);
  1620. it = cArr->find("brocker_address");
  1621. if( it != cArr->end() ){
  1622. strncpy( tVal->brocker_addr, it->second.c_str(), 255 );
  1623. };
  1624. it = cArr->find("brocker_port");
  1625. if( it != cArr->end() ){
  1626. tVal->brocker_port = atoi( it->second.c_str() );
  1627. };
  1628. };
  1629. };
  1630. };
  1631. deleteResult( result );
  1632. };
  1633. return tVal;
  1634. };
  1635. KAFKA_TEMPLATE *argusDBC::getKafkaTemplateForEvent( uint32_t ModuleID, uint32_t ClassID, uint32_t EventID ){
  1636. KAFKA_TEMPLATE *tVal = NULL;
  1637. if( m_bIsConnected ) {
  1638. char tQBuf[CTEXT];
  1639. AAssoc *result;
  1640. sprintf( tQBuf, "SELECT topic, template FROM module_kafka_settings WHERE module_id=%d AND class_id=%d AND event_id=%d", ModuleID, ClassID, EventID );
  1641. #ifdef DEBUG
  1642. LOG(tQBuf);
  1643. #endif
  1644. result = m_pDatabase->Query( tQBuf );
  1645. if( result!= NULL ) {
  1646. if( result->size() > 0 ) {
  1647. Assoc *cArr;
  1648. tVal = (KAFKA_TEMPLATE*) malloc(sizeof(KAFKA_TEMPLATE));
  1649. if( tVal != NULL ) {
  1650. Assoc::iterator it;
  1651. cArr = result->at(0);
  1652. it = cArr->find("topic");
  1653. if( it != cArr->end() ){
  1654. memset( tVal->topic, 0, 128 );
  1655. strncpy( tVal->topic , it->second.c_str(), 127 );
  1656. };
  1657. it = cArr->find("template");
  1658. if( it != cArr->end() ){
  1659. tVal->tpl = (CSTR)malloc( it->second.length() + 1 );
  1660. if( tVal->tpl!= NULL) {
  1661. memset( tVal->tpl, 0, it->second.length() + 1 );
  1662. strncpy( tVal->tpl, it->second.c_str(), it->second.length() );
  1663. };
  1664. };
  1665. };
  1666. };
  1667. };
  1668. deleteResult( result );
  1669. };
  1670. return tVal;
  1671. };
  1672. DBC_UART_DATA *argusDBC::getUartData( uint32_t UartID ){
  1673. DBC_UART_DATA *tVal = NULL;
  1674. if( m_bIsConnected ) {
  1675. char tQBuf[MAX_PATH_LEN];
  1676. AAssoc *result;
  1677. sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
  1678. #ifdef DEBUG
  1679. LOG(tQBuf);
  1680. #endif
  1681. result = m_pDatabase->Query( tQBuf );
  1682. if( result!= NULL ) {
  1683. if( result->size() > 0 ) {
  1684. Assoc *cArr;
  1685. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  1686. if( tVal != NULL ) {
  1687. Assoc::iterator it;
  1688. cArr = result->at(0);
  1689. it = cArr->find("type");
  1690. if( it != cArr->end() ){
  1691. tVal->type = atoi( it->second.c_str() );
  1692. };
  1693. it = cArr->find("speed");
  1694. if( it != cArr->end() ){
  1695. tVal->speed = atoi( it->second.c_str() );
  1696. };
  1697. it = cArr->find("mode");
  1698. if( it != cArr->end() ){
  1699. tVal->mode = atoi( it->second.c_str() );
  1700. };
  1701. it = cArr->find("port");
  1702. if( it != cArr->end() ){
  1703. tVal->port = atoi( it->second.c_str() );
  1704. };
  1705. it = cArr->find("path");
  1706. if( it != cArr->end() ){
  1707. strncpy( tVal->path, it->second.c_str(), 128 );
  1708. };
  1709. };
  1710. };
  1711. };
  1712. deleteResult( result );
  1713. };
  1714. return tVal;
  1715. };
  1716. bool argusDBC::updateMediaServerStream( uint32_t id, CSTR mrl, CSTR key ){
  1717. if( m_bIsConnected ) {
  1718. char tQBuf[MAX_PATH_LEN];
  1719. #ifdef DEBUG
  1720. LOG("Stream Update");
  1721. #endif
  1722. if( key == NULL ) {
  1723. if( mrl == NULL ) {
  1724. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', mrl='', `valid`=0 WHERE `id`=%d", id );
  1725. } else {
  1726. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=0, `key`='', `mrl`='%s', `valid`=1 WHERE `id`=%d", mrl, id );
  1727. };
  1728. } else {
  1729. sprintf( tQBuf,"UPDATE media_server_streams SET `online`=1, `key`='%s', `mrl`='%s', `valid`=1 WHERE `id`=%d",
  1730. key,
  1731. mrl,
  1732. id
  1733. );
  1734. };
  1735. #ifdef DEBUG
  1736. LOG(tQBuf);
  1737. #endif
  1738. m_pDatabase->SilentQuery( tQBuf );
  1739. return true;
  1740. };
  1741. return false;
  1742. };
  1743. bool argusDBC::addEvent( uint32_t SRC, CSTR UUID, uint32_t tss, uint32_t tsms, uint8_t evt_type, uint16_t dig_r, float AZ, float EL, float RANGE, float AREA, float lat, float lon, float alt, CSTR BUUID ){
  1744. if( m_bIsConnected ) {
  1745. char tQBuf[MAX_PATH_LEN];
  1746. if( BUUID == NULL ) {
  1747. sprintf( tQBuf,"INSERT INTO events_cloud SET node_id=%d, uuid='%s', ts=%d, ts_ms=%d, evt_type=%d, range_digital=%d, azimuth=%0.6f, elevation=%0.6f, `range`=%0.2f, area=%0.3f, lat=%0.6f, lon=%0.6f, alt=%0.3f",
  1748. SRC,
  1749. UUID,
  1750. tss,
  1751. tsms,
  1752. evt_type,
  1753. dig_r,
  1754. AZ,
  1755. EL,
  1756. RANGE,
  1757. AREA,
  1758. lat,
  1759. lon,
  1760. alt
  1761. );
  1762. } else {
  1763. sprintf( tQBuf,"INSERT INTO events_cloud SET node_id=%d, uuid='%s', ts=%d, ts_ms=%d, evt_type=%d, range_digital=%d, azimuth=%0.6f, elevation=%0.6f, `range`=%0.2f, area=%0.3f, lat=%0.6f, lon=%0.6f, alt=%0.3f, custom='%s'",
  1764. SRC,
  1765. UUID,
  1766. tss,
  1767. tsms,
  1768. evt_type,
  1769. dig_r,
  1770. AZ,
  1771. EL,
  1772. RANGE,
  1773. AREA,
  1774. lat,
  1775. lon,
  1776. alt,
  1777. BUUID
  1778. );
  1779. };
  1780. #ifdef DEBUG
  1781. // LOG(tQBuf);
  1782. #endif
  1783. m_pDatabase->SilentQuery( tQBuf );
  1784. return true;
  1785. };
  1786. return false;
  1787. };
  1788. DBC_EVT_DATA *argusDBC::getEventByUUID( CSTR UUID ){
  1789. DBC_EVT_DATA *tPtr = NULL;
  1790. if( m_bIsConnected ) {
  1791. char tQBuf[MAX_PATH_LEN];
  1792. AAssoc *result;
  1793. sprintf( tQBuf, "SELECT * FROM events_cloud WHERE uuid = '%s'", UUID );
  1794. #ifdef DEBUG
  1795. //LOG(tQBuf);
  1796. #endif
  1797. result = m_pDatabase->Query( tQBuf );
  1798. if( result!= NULL ) {
  1799. if( result->size() > 0 ) {
  1800. Assoc *cArr;
  1801. tPtr = (DBC_EVT_DATA*)malloc(sizeof(DBC_EVT_DATA));
  1802. if( tPtr!= NULL ) {
  1803. Assoc::iterator it;
  1804. memset(tPtr,0,sizeof(DBC_EVT_DATA));
  1805. cArr = result->at(0);
  1806. it = cArr->find("id");
  1807. if( it != cArr->end() ){
  1808. tPtr->id = atoi( it->second.c_str() );
  1809. };
  1810. it = cArr->find("node_id");
  1811. if( it != cArr->end() ){
  1812. tPtr->node_id = atoi( it->second.c_str() );
  1813. };
  1814. it = cArr->find("uuid");
  1815. if( it != cArr->end() ){
  1816. strncpy( tPtr->uuid, it->second.c_str(),64 );
  1817. };
  1818. it = cArr->find("ts");
  1819. if( it != cArr->end() ){
  1820. tPtr->ts_s = atoi( it->second.c_str() );
  1821. };
  1822. it = cArr->find("ts_ms");
  1823. if( it != cArr->end() ){
  1824. tPtr->ts_ms = atoi( it->second.c_str() );
  1825. };
  1826. it = cArr->find("evt_type");
  1827. if( it != cArr->end() ){
  1828. tPtr->type = atoi( it->second.c_str() );
  1829. };
  1830. it = cArr->find("range_digital");
  1831. if( it != cArr->end() ){
  1832. tPtr->range_digital = atoi( it->second.c_str() );
  1833. };
  1834. it = cArr->find("azimuth");
  1835. if( it != cArr->end() ){
  1836. tPtr->az = atof( it->second.c_str() );
  1837. };
  1838. it = cArr->find("elevation");
  1839. if( it != cArr->end() ){
  1840. tPtr->el = atof( it->second.c_str() );
  1841. };
  1842. it = cArr->find("range");
  1843. if( it != cArr->end() ){
  1844. tPtr->ra = atof( it->second.c_str() );
  1845. };
  1846. it = cArr->find("area");
  1847. if( it != cArr->end() ){
  1848. tPtr->area = atof( it->second.c_str() );
  1849. };
  1850. it = cArr->find("lat");
  1851. if( it != cArr->end() ){
  1852. tPtr->lat = atof( it->second.c_str() );
  1853. };
  1854. it = cArr->find("lon");
  1855. if( it != cArr->end() ){
  1856. tPtr->lon = atof( it->second.c_str() );
  1857. };
  1858. it = cArr->find("alt");
  1859. if( it != cArr->end() ){
  1860. tPtr->alt = atof( it->second.c_str() );
  1861. };
  1862. it = cArr->find("custom");
  1863. if( it != cArr->end() ){
  1864. strncpy( tPtr->custom, it->second.c_str(),64 );
  1865. };
  1866. };
  1867. };
  1868. };
  1869. deleteResult( result );
  1870. };
  1871. return tPtr;
  1872. };
  1873. #undef LOG
  1874. #undef LOG_ERROR