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.

1452 lines
49 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "SQLite_dataBinder.h"
  2. #define LOG( X ) m_pLog->Log( "SDBC", 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( "SDBC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  8. }
  9. #else
  10. #define LOG_ERROR( X ) m_pLog->Log( "SDBC", this, _LOG_ERROR, X );
  11. #endif
  12. sqliteDBC *sqliteDBC::_instance = NULL;
  13. /**
  14. * Returns the single instance of the object.
  15. */
  16. sqliteDBC *sqliteDBC::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 sqliteDBC( Settings );
  21. };
  22. // return the single instance
  23. return _instance;
  24. };
  25. sqliteDBC::sqliteDBC( config_setting_t * Settings ):dataBinder(){
  26. m_pLog = basicLogCollection::getInstance();
  27. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DATABASE" );
  28. m_bIsConnected = false;
  29. m_pDataStorage = NULL;
  30. m_pDataMemory = NULL;
  31. m_nServerID = 1;
  32. };
  33. sqliteDBC::~sqliteDBC(){
  34. if ( m_pDataStorage ) delete( m_pDataStorage );
  35. if ( m_pDataMemory ) delete( m_pDataMemory );
  36. _instance = NULL;
  37. };
  38. bool sqliteDBC::connect( void ){
  39. if ( m_pSettings == NULL) return false;
  40. bool bRecreateMemStructures = false;
  41. config_setting_t * pSetting;
  42. const char * tBuff;
  43. //Disconnect if Connected
  44. m_bIsConnected = false;
  45. char web_access_user[64];
  46. char web_access_group[64];
  47. //Apply New Config
  48. #ifdef DEBUG
  49. LOG( "Reading DB Settings ... " );
  50. #endif
  51. if(!config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char**)&tBuff )){
  52. pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
  53. sprintf( m_pDbFile, "data/mysqlitedb.db" );
  54. config_setting_set_string( pSetting, m_pDbFile );
  55. basicSettingsProvider::getInstance()->compromise();
  56. LOG("Data Storage not set - using default");
  57. } else {
  58. sprintf( m_pDbFile, tBuff );
  59. };
  60. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char**)&tBuff )){
  61. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
  62. sprintf( web_access_user, "www-data" );
  63. config_setting_set_string( pSetting, web_access_user );
  64. basicSettingsProvider::getInstance()->compromise();
  65. LOG("Web Access User not set - using default");
  66. } else {
  67. sprintf( web_access_user, tBuff );
  68. };
  69. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char**)&tBuff )){
  70. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
  71. sprintf( web_access_group, "www-data" );
  72. config_setting_set_string( pSetting, web_access_group );
  73. basicSettingsProvider::getInstance()->compromise();
  74. LOG("Web Access Group not set - using default");
  75. } else {
  76. sprintf( web_access_group, tBuff );
  77. };
  78. if( !fileExist( DB_SHMEM_POINT )) {
  79. bRecreateMemStructures = true;
  80. mkdir( DB_SHMEM_POINT, 0775 );// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  81. chown( DB_SHMEM_POINT, web_access_user, web_access_group ); //To make Accessed from PHP
  82. chmod( DB_SHMEM_POINT, 0775 );
  83. };
  84. char tB[128];
  85. if (!config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **)&tBuff)) {
  86. pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
  87. tBuff = tB;
  88. sprintf( tB, "mem_db.db" );
  89. config_setting_set_string( pSetting, tB );
  90. basicSettingsProvider::getInstance()->compromise();
  91. LOG("Memory Storage not set - using default");
  92. };
  93. sprintf( m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff );
  94. basicSettingsProvider::getInstance()->validate();
  95. // Kill Connection if any
  96. if ( m_pDataStorage ) delete( m_pDataStorage );
  97. if ( m_pDataMemory ) delete( m_pDataMemory );
  98. #ifdef DEBUG
  99. LOG( m_pDbFile );
  100. #endif
  101. // Create New Connections
  102. m_pDataStorage = new SQLite_Connector();
  103. if ( m_pDataStorage ) {
  104. if( m_pDataStorage->Connect( m_pDbFile, 0, true ) ){
  105. #ifdef DEBUG
  106. LOG("DB File Opened");
  107. #endif
  108. chown( m_pDbFile, web_access_user, web_access_group );
  109. chmod( m_pDbFile, 0664 );
  110. } else{
  111. LOG_ERROR("Cannot Connect to DB File");
  112. delete( m_pDataStorage );
  113. m_pDataStorage = NULL;
  114. LOG("Storage Deleted!");
  115. return false;
  116. };
  117. };
  118. #ifdef DEBUG
  119. LOG( m_pDbMemory );
  120. #endif
  121. if( !fileExist( m_pDbMemory )) {
  122. bRecreateMemStructures = true;
  123. //Always recreate
  124. //unlink( m_pDbMemory );
  125. };
  126. m_pDataMemory = new SQLite_Connector();
  127. if ( m_pDataMemory ) {
  128. if( m_pDataMemory->Connect( m_pDbMemory, 0, true ) ){
  129. #ifdef DEBUG
  130. LOG("DB Memory Opened");
  131. #endif
  132. chown( m_pDbMemory, web_access_user, web_access_group );
  133. chmod( m_pDbMemory, 0664 );
  134. //Recreate at Register Server
  135. //if ( bRecreateMemStructures ) initMemDB();
  136. } else{
  137. LOG_ERROR("Cannot Connect to DB Memory");
  138. delete( m_pDataStorage );
  139. delete( m_pDataMemory );
  140. m_pDataStorage = NULL;
  141. m_pDataMemory = NULL;
  142. return false;
  143. };
  144. };
  145. m_bIsConnected = true;
  146. return m_bIsConnected;
  147. };
  148. void sqliteDBC::initMemDB( uint32_t sid ){
  149. m_pDataMemory->Query("DROP TABLE \"nodes_qcq\"");
  150. m_pDataMemory->Query("CREATE TABLE \"nodes_qcq\" ( \
  151. \"id\" INTEGER NOT NULL,\
  152. \"node_id\" INTEGER NOT NULL,\
  153. \"cmd\" INTEGER NOT NULL,\
  154. \"status\" INTEGER DEFAULT 0,\
  155. \"parameter\" TEXT,\
  156. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  157. );");
  158. m_pDataMemory->Query("DROP TABLE \"servers\"");
  159. m_pDataMemory->Query("CREATE TABLE \"servers\" (\
  160. \"id\" INTEGER NOT NULL,\
  161. \"ts\" INTEGER DEFAULT 0,\
  162. \"pid\" INTEGER DEFAULT 0,\
  163. \"hw_info\" TEXT,\
  164. \"os_info\" TEXT,\
  165. \"cpu_cores\" INTEGER DEFAULT 0.0,\
  166. \"stat_cpu1\" REAL DEFAULT 0.0,\
  167. \"stat_cpu5\" REAL DEFAULT 0.0,\
  168. \"stat_cpu15\" REAL DEFAULT 0.0,\
  169. \"stat_mem\" REAL DEFAULT 0.0,\
  170. \"stat_disk\" REAL DEFAULT 0.0,\
  171. \"val_mem_tottal\" INTEGER DEFAULT 0,\
  172. \"val_mem_current\" INTEGER DEFAULT 0,\
  173. \"val_disk_tottal\" INTEGER DEFAULT 0,\
  174. \"val_disk_current\" INTEGER DEFAULT 0,\
  175. \"temp0\" REAL DEFAULT 0.0,\
  176. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  177. );");
  178. char tB[128];
  179. sprintf( tB, "INSERT INTO `servers` (`id`,`ts`) VALUES (%d, strftime('%%s') )", sid );
  180. m_pDataMemory->Query( tB );
  181. };
  182. DBC_PUSHED_CMD *sqliteDBC::getNodePushedCMD( uint32_t NodeID ){
  183. DBC_PUSHED_CMD * RetVal = NULL;
  184. if ( m_bIsConnected ) {
  185. char QS[MAX_PATH];
  186. AAssoc * result;
  187. sprintf( QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID );
  188. result = m_pDataMemory->Query( QS );
  189. if ( result ) {
  190. if( result->size() > 0 ) {
  191. RetVal = (DBC_PUSHED_CMD*)malloc(sizeof(DBC_PUSHED_CMD));
  192. if( RetVal ) {
  193. Assoc * cArr;
  194. Assoc::iterator it;
  195. memset( RetVal, 0, sizeof(DBC_PUSHED_CMD));
  196. cArr = result->at(0);
  197. it = cArr->find("id");
  198. if( it != cArr->end() ){
  199. RetVal->id = atoi( it->second.c_str() );
  200. };
  201. it = cArr->find("cmd");
  202. if( it != cArr->end() ){
  203. RetVal->CMD = atoi( it->second.c_str() );
  204. };
  205. it = cArr->find("parameter");
  206. if( it != cArr->end() ){
  207. strncpy( RetVal->PARAM, it->second.c_str(), 256 );
  208. RetVal->PARAM[255] = (char)0;
  209. };
  210. };
  211. };
  212. };
  213. m_pDataMemory->deleteResult( result );
  214. };
  215. return RetVal;
  216. };
  217. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  218. if(m_bIsConnected) {
  219. char tQBuf[MAX_PATH_LEN];
  220. char tIpBuf[CTEXT];
  221. char *tPos;
  222. char *tPos2;
  223. tPos = strstr(Addr,"//");
  224. if( tPos != NULL ) {
  225. tPos2 = strstr( tPos + 2,"/");
  226. if( tPos2 != NULL ) {
  227. int len;
  228. len = tPos2 - tPos - 2;
  229. strncpy(tIpBuf,tPos + 2,len);
  230. tIpBuf[len] = (char)0;
  231. } else {
  232. LOG_ERROR("Pos 2 - err");
  233. tIpBuf[0] = (char)0;
  234. };
  235. } else {
  236. LOG_ERROR("Pos 0 - err");
  237. tIpBuf[0] = (char)0;
  238. };
  239. sprintf(tQBuf,"UPDATE cameras SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  240. Addr,
  241. tIpBuf,
  242. CamID
  243. );
  244. #ifdef DEBUG_ALL
  245. LOG(tQBuf);
  246. #endif
  247. m_pDataStorage->SilentQuery( tQBuf );
  248. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  249. #ifdef DEBUG_ALL
  250. LOG(tQBuf);
  251. #endif
  252. m_pDataStorage->SilentQuery( tQBuf );
  253. return true;
  254. };
  255. return false;
  256. };
  257. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  258. if(m_bIsConnected) {
  259. uint32_t cID = getCamId( uuid );
  260. if ( cID ) {
  261. return updateIPCam( cID, Addr );
  262. };
  263. };
  264. return false;
  265. };
  266. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  267. if(m_bIsConnected) {
  268. uint32_t cID = getCamId( uuid );
  269. if ( cID ) {
  270. return updateLocalCam( cID );
  271. };
  272. };
  273. return false;
  274. };
  275. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  276. if(m_bIsConnected) {
  277. char tQBuf[MAX_PATH_LEN];
  278. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  279. #ifdef DEBUG
  280. LOG(tQBuf);
  281. #endif
  282. m_pDataStorage->SilentQuery( tQBuf );
  283. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  284. #ifdef DEBUG
  285. LOG(tQBuf);
  286. #endif
  287. m_pDataStorage->SilentQuery( tQBuf );
  288. return true;
  289. };
  290. return false;
  291. };
  292. bool sqliteDBC::updateNodePushedCMD( uint32_t CmdID, uint8_t status ){
  293. if ( m_bIsConnected ) {
  294. char QS[MAX_PATH];
  295. sprintf( QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID );
  296. return m_pDataMemory->SilentQuery( QS );
  297. };
  298. return false;
  299. };
  300. bool sqliteDBC::cleanNodePushedCMD( void ){
  301. if ( m_bIsConnected ) {
  302. return m_pDataMemory->SilentQuery( "DELETE FROM `nodes_qcq` WHERE `status` > 200" );
  303. };
  304. return false;
  305. };
  306. bool sqliteDBC::updateServerUtilisation( DBC_SYS_LOAD_INFO *ptr ){
  307. if( m_bIsConnected ) {
  308. char tQBuf[MAX_PATH_LEN];
  309. sprintf(tQBuf, "UPDATE `servers` SET `ts`=STRFTIME('%%s'), `cpu_cores`=%d, `stat_cpu1`=%0.1f, `stat_cpu5`=%0.1f, `stat_cpu15`=%0.1f, `stat_mem`=%0.1f, `stat_disk`=%0.1f, `temp0`=%0.1f, `val_mem_tottal`=%" PRIu64 ", `val_mem_current`=%" PRIu64 ", `val_disk_tottal`=%" PRIu64 ", `val_disk_current`=%" PRIu64 " WHERE `id`=%d",
  310. ptr->CPU_CORES,
  311. ptr->cpu1,
  312. ptr->cpu5,
  313. ptr->cpu15,
  314. ptr->mem,
  315. ptr->disk,
  316. ptr->temperature,
  317. ptr->T_DISK,
  318. ptr->C_DISK,
  319. ptr->T_MEM,
  320. ptr->C_MEM,
  321. m_nServerID
  322. );
  323. #ifdef DEBUG_ALL
  324. LOG(tQBuf);
  325. #endif
  326. m_pDataMemory->SilentQuery( tQBuf );
  327. return true;
  328. };
  329. return false;
  330. };
  331. /********************************************************************
  332. * *
  333. * Slow DB Functions *
  334. * *
  335. ********************************************************************/
  336. bool sqliteDBC::addDeviceDraw( uint32_t DBID, uint8_t type, uint8_t lrf, void *th_data, uint32_t th_size ){
  337. return false;
  338. };
  339. bool sqliteDBC::addIPCam( OBSERVER_RESULT *tPtr ){
  340. if(m_bIsConnected) {
  341. char tQBuf[1024];
  342. char tIpBuf[32];
  343. char tProto[32];
  344. char tPath[CTEXT];
  345. char *tPos;
  346. char *tPos2;
  347. memcpy( tProto, tPtr->XAddrs, 5 );
  348. tProto[5] = (char)0;
  349. if( tProto[4] == ':') tProto[4] = (char)0;
  350. memset( tPath, 0, CTEXT );
  351. tPos = strstr(tPtr->XAddrs,"//");
  352. if( tPos != NULL ) {
  353. tPos2 = strstr( tPos + 2,"/");
  354. if( tPos2 != NULL ) {
  355. int len,len2,len3;
  356. len = tPos2 - tPos - 2;
  357. strncpy( tIpBuf, tPos + 2, len);
  358. tIpBuf[len] = (char)0;
  359. strncpy( tPath, tPos + 2 + len, CTEXT );
  360. } else {
  361. LOG_ERROR("Pos 2 - err");
  362. tIpBuf[0] = (char)0;
  363. };
  364. } else {
  365. LOG_ERROR("Pos 0 - err");
  366. tIpBuf[0] = (char)0;
  367. };
  368. sprintf(tQBuf,"INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',2,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s'",
  369. tPtr->RefID,
  370. //tPtr->XAddrs,
  371. tPath,
  372. tIpBuf,
  373. tPtr->MAC,
  374. tPtr->Hardware,
  375. tPtr->Name,
  376. tProto,
  377. //tPtr->XAddrs,
  378. tIpBuf
  379. );
  380. #ifdef DEBUG_ALL
  381. LOG(tQBuf);
  382. #endif
  383. m_pDataStorage->SilentQuery( tQBuf );
  384. return true;
  385. };
  386. return false;
  387. };
  388. uint32_t sqliteDBC::addLocalCam( CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height ){
  389. uint32_t RetVal = 0;
  390. if( m_bIsConnected ) {
  391. char tQBuf[1024];
  392. char tM5[33];
  393. CSTR tMD5 = MD5String( path );
  394. if( tMD5 != NULL ) {
  395. memcpy( tM5, tMD5, 33 );
  396. tM5[32]=(char)0;
  397. free( tMD5 );
  398. sprintf(tQBuf,"INSERT INTO cameras (`online`,`uuid`,`type`,`status`,`hardware`,`name`,`ip`) VALUES (1,'%s',0,0,'%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1",
  399. tM5,
  400. hardware,
  401. name,
  402. path
  403. );
  404. #ifdef DEBUG_ALL
  405. LOG(tQBuf);
  406. #endif
  407. uint32_t camID = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  408. if ( camID == 0 ) {
  409. camID = getCamId( tM5 );
  410. };
  411. sprintf(tQBuf, "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`mrl_proto`,`mrl`,`picture_proto`) VALUES(%d,'A',1,'%s','%s',%d,%d,'v4l2','%s','v4l2') ON CONFLICT(`source_id`,`token`) DO UPDATE SET `valid`=1, img_width=%d, img_height=%d",
  412. camID, name, tM5, width, height, path, width, height );
  413. #ifdef DEBUG_EXTRA
  414. LOG(tQBuf);
  415. #endif
  416. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  417. if( RetVal == 0 ){
  418. RetVal = getMediaStreamID( camID, tM5 );
  419. };
  420. };
  421. };
  422. return RetVal;
  423. };
  424. uint32_t sqliteDBC::addLocalNetwork( uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast ){
  425. uint32_t RetVal = 0;
  426. if( m_bIsConnected ) {
  427. char tQBuf[MAX_PATH_LEN];
  428. sprintf( tQBuf, "INSERT INTO `networks`(`nif_id`,`type`,`address`,`gateway`,`mask`,`broadcast`) VALUES (%d,%d,'%s','%s','%s','%s') ON CONFLICT(`nif_id`,`type`) DO UPDATE SET `address`='%s', `gateway`='%s', `mask`='%s', `broadcast`='%s'",
  429. NIF_ID,
  430. net_type,
  431. address,
  432. gateway,
  433. mask,
  434. broadcast,
  435. address,
  436. gateway,
  437. mask,
  438. broadcast
  439. );
  440. #ifdef DEBUG_ALL
  441. LOG(tQBuf);
  442. #endif
  443. RetVal = m_pDataStorage->QueryID( tQBuf );
  444. // On Update IF data no need to return ID
  445. //if( RetVal == 0 ) {
  446. // RetVal = getNetworkInterfaceID( tKey );
  447. //};
  448. };
  449. return RetVal;
  450. };
  451. uint32_t sqliteDBC::addLocalNetworkInfrastructure( uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME ){
  452. uint32_t RetVal = 0;
  453. if( m_bIsConnected ) {
  454. char tQBuf[MAX_PATH_LEN];
  455. bool ena_routing = true;
  456. if( OUTBOUND_NIF == 0 ){
  457. ena_routing = false;
  458. };
  459. if( ena_dhcp ){
  460. if ( BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL ) {
  461. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  462. return 0;
  463. };
  464. sprintf(tQBuf, "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`,`enable_dhcp`,`dhcp_range_begin`,`dhcp_range_end`,`dhcp_range_mask`,`dhcp_lease_time`) VALUES (%d,%d,%d,1,'%s','%s','%s','%s') ON CONFLICT(`nif_id`) DO UPDATE SET `enable_routing`=%d, `outbond_nif`=%d, `enable_dhcp`=1, `dhcp_range_begin`='%s', `dhcp_range_end`='%s', `dhcp_range_mask`='%s', `dhcp_lease_time`='%s'",
  465. NIF_ID,
  466. ena_routing,
  467. OUTBOUND_NIF,
  468. BRANG,
  469. ERANG,
  470. MASK,
  471. LEASE_TIME,
  472. ena_routing,
  473. OUTBOUND_NIF,
  474. BRANG,
  475. ERANG,
  476. MASK,
  477. LEASE_TIME
  478. );
  479. } else {
  480. if( ena_routing ){
  481. sprintf(tQBuf, "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`) VALUES(%d,1,%d) ON CONFLICT(`nif_id`) DO UPDATE SET `enable_routing`=1, `outbond_nif`=%d",
  482. NIF_ID,
  483. OUTBOUND_NIF,
  484. OUTBOUND_NIF
  485. );
  486. } else {
  487. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  488. return 0;
  489. };
  490. };
  491. #ifdef DEBUG_ALL
  492. LOG(tQBuf);
  493. #endif
  494. RetVal = m_pDataStorage->QueryID( tQBuf );
  495. // On Update IF data no need to return ID
  496. if( RetVal == 0 ) {
  497. RetVal = getNetworkInfrastructureID( NIF_ID );
  498. };
  499. };
  500. return RetVal;
  501. };
  502. uint32_t sqliteDBC::addLocalNIF( CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id ){
  503. uint32_t RetVal = 0;
  504. if( m_bIsConnected ) {
  505. char tQBuf[MAX_PATH_LEN];
  506. char * tKey = MD5String( device );
  507. if( tKey != NULL ) {
  508. sprintf( tQBuf, "INSERT INTO `network_interfaces` (`type`,`key`,`device_name`,`mac`,`mode`,`aux_id`) VALUES (%d,'%s','%s','%s',%d,%d) ON CONFLICT(`key`) DO UPDATE SET `mode`=%d,`aux_id`=%d,`mac`='%s'",
  509. dtype,
  510. tKey,
  511. device,
  512. mac,
  513. dmode,
  514. aux_id,
  515. dmode,
  516. aux_id,
  517. mac
  518. );
  519. #ifdef DEBUG_ALL
  520. LOG(tQBuf);
  521. #endif
  522. RetVal = m_pDataStorage->QueryID( tQBuf );
  523. if( RetVal == 0 ) {
  524. RetVal = getNIF_ID( tKey );
  525. };
  526. free( tKey );
  527. };
  528. };
  529. return RetVal;
  530. };
  531. uint32_t sqliteDBC::addLocalUART( uint8_t utype, uint32_t speed, uint8_t bit, char parity, float stop_bit, CSTR PATH, uint8_t mode, uint16_t port ){
  532. uint32_t RetVal = 0;
  533. if( m_bIsConnected ) {
  534. char tQBuf[MAX_PATH_LEN];
  535. sprintf( tQBuf, "INSERT INTO `uart_settings`(`node_id`,`type`,`speed`,`bit`,`parity`,`stop_bit`,`mode`,`port`,`path`) VALUES (%d,%d,%d,%d,'%c',%.1f,%d,%d,'%s') ON CONFLICT(`node_id`,`type`,`path`) DO UPDATE SET `id`=`id`",
  536. m_nServerNodeID,
  537. utype,
  538. speed,
  539. bit,
  540. parity,
  541. stop_bit,
  542. mode,
  543. port,
  544. PATH
  545. );
  546. #ifdef DEBUG_ALL
  547. LOG(tQBuf);
  548. #endif
  549. RetVal = m_pDataStorage->QueryID( tQBuf );
  550. if( RetVal == 0 ) {
  551. RetVal = getUartID( m_nServerNodeID, utype, PATH );
  552. };
  553. };
  554. return RetVal;
  555. };
  556. uint32_t sqliteDBC::addLocalWiFi( uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id ){
  557. uint32_t RetVal = 0;
  558. if( m_bIsConnected ) {
  559. char tQBuf[MAX_PATH_LEN];
  560. sprintf( tQBuf, "INSERT INTO `wifis` (`nif_id`,`mode`,`aux_id`,`SSID`,`password`,`encryption_type`) VALUES (%d,%d,%d,'%s','%s',%d) ON CONFLICT (`nif_id`) DO UPDATE SET `mode`=%d, `aux_id`=%d, `SSID`='%s', `password`='%s', `encryption_type`=%d",
  561. NIF_ID,
  562. mode,
  563. router_id,
  564. SSID,
  565. password,
  566. encryption,
  567. mode,
  568. router_id,
  569. SSID,
  570. password,
  571. encryption
  572. );
  573. #ifdef DEBUG_ALL
  574. LOG(tQBuf);
  575. #endif
  576. RetVal = m_pDataStorage->QueryID( tQBuf );
  577. if( RetVal == 0 ) {
  578. RetVal = getWiFi_ID( NIF_ID );
  579. };
  580. };
  581. return RetVal;
  582. };
  583. bool sqliteDBC::cleanResources( bool all ){
  584. if(m_bIsConnected) {
  585. char tQBuf[MAX_PATH_LEN];
  586. if( all ) {
  587. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID );
  588. } else {
  589. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE ( `node_id`=%d ) AND ( `type`=100 OR `type`=101 OR `type`=103 OR `type`=120 )", m_nServerNodeID );
  590. };
  591. #ifdef DEBUG_ALL
  592. LOG(tQBuf);
  593. #endif
  594. m_pDataStorage->SilentQuery( tQBuf );
  595. return true;
  596. };
  597. return false;
  598. };
  599. uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){
  600. uint32_t RetVal = 0;
  601. if( m_bIsConnected ){
  602. char tQBuf[2048];
  603. sprintf( tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName );
  604. CSTR tMD5 = MD5String( tQBuf );
  605. if( tMD5 ) {
  606. sprintf( tQBuf, "INSERT INTO `resources`(`node_id`,`type`,`md5`,`aux_id`,`device`,`active`) VALUES (%d,%d,'%s',%d,'%s',1) ON CONFLICT(`md5`) DO UPDATE SET `active`=1, `aux_id`=%d",
  607. m_nServerNodeID,
  608. type,
  609. tMD5,
  610. auxID,
  611. devName,
  612. auxID
  613. );
  614. free( tMD5 );
  615. #ifdef DEBUG_ALL
  616. LOG(tQBuf);
  617. #endif
  618. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  619. if( RetVal == 0 ) {
  620. RetVal = getResourceID( m_nServerNodeID, type, auxID );
  621. };
  622. };
  623. };
  624. return RetVal;
  625. };
  626. DBC_MEDIA_STREAM_DATA *sqliteDBC::getMediaStreamData(uint32_t ID){
  627. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  628. if(m_bIsConnected){
  629. char tQBuf[CTEXT];
  630. AAssoc *result;
  631. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  632. #ifdef DEBUG_ALL
  633. LOG(tQBuf);
  634. #endif
  635. result = m_pDataStorage->Query( tQBuf );
  636. if( result!= NULL ) {
  637. if( result->size() > 0 ) {
  638. Assoc *cArr;
  639. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  640. if( tVal != NULL ) {
  641. memset( tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA) );
  642. Assoc::iterator it;
  643. cArr = result->at(0);
  644. tVal->id = ID;
  645. it = cArr->find("source_id");
  646. if( it != cArr->end() ){
  647. tVal->source_id = atoi( it->second.c_str() );
  648. };
  649. it = cArr->find("source_type");
  650. if( it != cArr->end() ){
  651. sprintf( tVal->source_type, it->second.c_str() );
  652. };
  653. it = cArr->find("valid");
  654. if( it != cArr->end() ){
  655. tVal->valid = (bool) atoi( it->second.c_str() );
  656. };
  657. it = cArr->find("token");
  658. if( it != cArr->end() ){
  659. sprintf( tVal->token, it->second.c_str() );
  660. };
  661. it = cArr->find("img_width");
  662. if( it != cArr->end() ){
  663. tVal->width = atoi( it->second.c_str() );
  664. };
  665. it = cArr->find("img_height");
  666. if( it != cArr->end() ){
  667. tVal->height = atoi( it->second.c_str() );
  668. };
  669. it = cArr->find("fps");
  670. if( it != cArr->end() ){
  671. tVal->fps = atoi( it->second.c_str() );
  672. };
  673. it = cArr->find("compression_type");
  674. if( it != cArr->end() ){
  675. sprintf(tVal->c_type, it->second.c_str());
  676. };
  677. it = cArr->find("mrl_proto");
  678. if( it != cArr->end() ){
  679. sprintf( tVal->mrl_proto, it->second.c_str() );
  680. };
  681. it = cArr->find("mrl_port");
  682. if( it != cArr->end() ){
  683. tVal->mrl_port = atoi( it->second.c_str() );
  684. };
  685. it = cArr->find("mrl");
  686. if( it != cArr->end() ){
  687. sprintf( tVal->mrl, it->second.c_str() );
  688. };
  689. };
  690. };
  691. };
  692. deleteResult( result );
  693. };
  694. return tVal;
  695. };
  696. uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
  697. uint32_t RetVal = 0;
  698. if(m_bIsConnected){
  699. char tQBuf[CTEXT];
  700. AAssoc *result;
  701. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
  702. #ifdef DEBUG_ALL
  703. LOG(tQBuf);
  704. #endif
  705. result = m_pDataStorage->Query( tQBuf );
  706. if( result!= NULL ) {
  707. if( result->size() > 0 ) {
  708. Assoc *cArr;
  709. Assoc::iterator it;
  710. cArr = result->at(0);
  711. it = cArr->find("id");
  712. if( it != cArr->end() ){
  713. RetVal = atoi( it->second.c_str() );
  714. };
  715. };
  716. };
  717. deleteResult( result );
  718. };
  719. return RetVal;
  720. };
  721. DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
  722. DBC_MODULE_DATA *tVal = NULL;
  723. if(m_bIsConnected){
  724. char tQBuf[CTEXT];
  725. AAssoc *result;
  726. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
  727. #ifdef DEBUG_ALL
  728. LOG(tQBuf);
  729. #endif
  730. result = m_pDataStorage->Query( tQBuf );
  731. if( result!= NULL ) {
  732. if( result->size() > 0 ) {
  733. Assoc *cArr;
  734. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  735. if( tVal != NULL ) {
  736. Assoc::iterator it;
  737. cArr = result->at(0);
  738. it = cArr->find("id");
  739. if( it != cArr->end() ){
  740. tVal->module_id = atoi( it->second.c_str() );
  741. };
  742. it = cArr->find("aux_id");
  743. if( it != cArr->end() ){
  744. tVal->aux_id = atoi( it->second.c_str() );
  745. };
  746. it = cArr->find("type");
  747. if( it != cArr->end() ){
  748. tVal->type = atoi( it->second.c_str() );
  749. };
  750. it = cArr->find("enabled");
  751. if( it != cArr->end() ){
  752. tVal->enabled = (bool)atoi( it->second.c_str() );
  753. };
  754. };
  755. };
  756. };
  757. deleteResult( result );
  758. };
  759. return tVal;
  760. };
  761. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  762. DBC_NET_IF_DATA * tVal = NULL;
  763. if(m_bIsConnected){
  764. char tQBuf[CTEXT];
  765. AAssoc *result;
  766. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  767. #ifdef DEBUG_EXTRA
  768. LOG(tQBuf);
  769. #endif
  770. result = m_pDataStorage->Query( tQBuf );
  771. if( result!= NULL ) {
  772. if( result->size() > 0 ) {
  773. Assoc *cArr;
  774. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  775. if( tVal != NULL ) {
  776. Assoc::iterator it;
  777. tVal->id = nif_id;
  778. cArr = result->at(0);
  779. it = cArr->find("type");
  780. if( it != cArr->end() ){
  781. tVal->type = atoi(it->second.c_str());
  782. };
  783. it = cArr->find("mode");
  784. if( it != cArr->end() ){
  785. tVal->mode = atoi(it->second.c_str());
  786. };
  787. it = cArr->find("device_name");
  788. if( it != cArr->end() ){
  789. strncpy( tVal->device_name, it->second.c_str(),11);
  790. tVal->device_name[10]=(char)0;
  791. };
  792. it = cArr->find("mac");
  793. if( it != cArr->end() ){
  794. strncpy( tVal->mac, it->second.c_str(), 33 );
  795. tVal->mac[32]=(char)0;
  796. };
  797. it = cArr->find("aux_id");
  798. if( it != cArr->end() ){
  799. tVal->aux_id = atoi(it->second.c_str());
  800. };
  801. };
  802. };
  803. };
  804. deleteResult( result );
  805. };
  806. return tVal;
  807. };
  808. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  809. uint32_t tVal = 0;
  810. if(m_bIsConnected){
  811. char tQBuf[CTEXT];
  812. AAssoc *result;
  813. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  814. #ifdef DEBUG_ALL
  815. LOG(tQBuf);
  816. #endif
  817. result = m_pDataStorage->Query( tQBuf );
  818. if( result!= NULL ) {
  819. if( result->size() > 0 ) {
  820. Assoc *cArr;
  821. Assoc::iterator it;
  822. cArr = result->at(0);
  823. it = cArr->find("id");
  824. if( it != cArr->end() ){
  825. tVal = atoi( it->second.c_str() );
  826. };
  827. };
  828. };
  829. deleteResult( result );
  830. };
  831. return tVal;
  832. };
  833. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  834. uint32_t tVal = 0;
  835. if(m_bIsConnected){
  836. char tQBuf[CTEXT];
  837. AAssoc *result;
  838. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  839. #ifdef DEBUG_ALL
  840. LOG(tQBuf);
  841. #endif
  842. result = m_pDataStorage->Query( tQBuf );
  843. if( result!= NULL ) {
  844. if( result->size() > 0 ) {
  845. Assoc *cArr;
  846. Assoc::iterator it;
  847. cArr = result->at(0);
  848. it = cArr->find("id");
  849. if( it != cArr->end() ){
  850. tVal = atoi( it->second.c_str() );
  851. };
  852. };
  853. };
  854. deleteResult( result );
  855. };
  856. return tVal;
  857. };
  858. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  859. uint32_t tVal = 0;
  860. if(m_bIsConnected){
  861. char tQBuf[CTEXT];
  862. AAssoc *result;
  863. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  864. #ifdef DEBUG_ALL
  865. LOG(tQBuf);
  866. #endif
  867. result = m_pDataStorage->Query( tQBuf );
  868. if( result!= NULL ) {
  869. if( result->size() > 0 ) {
  870. Assoc *cArr;
  871. Assoc::iterator it;
  872. cArr = result->at(0);
  873. it = cArr->find("id");
  874. if( it != cArr->end() ){
  875. tVal = atoi( it->second.c_str() );
  876. };
  877. };
  878. };
  879. deleteResult( result );
  880. };
  881. return tVal;
  882. };
  883. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  884. DBC_RESOURCE_DATA *tVal = NULL;
  885. if( m_bIsConnected ){
  886. char tQBuf[CTEXT];
  887. AAssoc *result;
  888. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  889. #ifdef DEBUG_ALL
  890. LOG(tQBuf);
  891. #endif
  892. result = m_pDataStorage->Query( tQBuf );
  893. if( result!= NULL ) {
  894. if( result->size() > 0 ) {
  895. Assoc *cArr;
  896. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  897. if( tVal != NULL ) {
  898. Assoc::iterator it;
  899. cArr = result->at(0);
  900. tVal->id = ResourceID;
  901. it = cArr->find("node_id");
  902. if( it != cArr->end() ){
  903. tVal->node_id = atoi( it->second.c_str() );
  904. };
  905. it = cArr->find("type");
  906. if( it != cArr->end() ){
  907. tVal->type = 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("device");
  914. if( it != cArr->end() ){
  915. strncpy( tVal->device, it->second.c_str(), 64 );
  916. tVal->device[63] = (char)0;
  917. };
  918. };
  919. };
  920. };
  921. deleteResult( result );
  922. };
  923. return tVal;
  924. };
  925. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  926. uint32_t RetVal = 0;
  927. if(m_bIsConnected){
  928. char tQBuf[CTEXT];
  929. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  930. #ifdef DEBUG_ALL
  931. LOG(tQBuf);
  932. #endif
  933. AAssoc *result;
  934. result = m_pDataStorage->Query( tQBuf );
  935. if( result!= NULL ) {
  936. if( result->size() > 0 ) {
  937. Assoc * cArr;
  938. Assoc::iterator it;
  939. cArr = result->at(0);
  940. it = cArr->find("id");
  941. if( it != cArr->end() ){
  942. RetVal = atoi( it->second.c_str() );
  943. };
  944. };
  945. };
  946. deleteResult( result );
  947. };
  948. return RetVal;
  949. };
  950. uint32_t sqliteDBC::getServerID( CSTR identity ){
  951. uint32_t RetVal = 0;
  952. if(m_bIsConnected){
  953. char tQBuf[CTEXT];
  954. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  955. #ifdef DEBUG_ALL
  956. LOG(tQBuf);
  957. #endif
  958. AAssoc *result;
  959. result = m_pDataStorage->Query( tQBuf );
  960. if( result!= NULL ) {
  961. if( result->size() > 0 ) {
  962. Assoc * cArr;
  963. Assoc::iterator it;
  964. cArr = result->at(0);
  965. it = cArr->find("id");
  966. if( it != cArr->end() ){
  967. RetVal = atoi( it->second.c_str() );
  968. };
  969. };
  970. };
  971. deleteResult( result );
  972. };
  973. return RetVal;
  974. };
  975. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  976. uint32_t RetVal = 0;
  977. if(m_bIsConnected){
  978. char tQBuf[CTEXT];
  979. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  980. #ifdef DEBUG_ALL
  981. LOG(tQBuf);
  982. #endif
  983. AAssoc *result;
  984. result = m_pDataStorage->Query( tQBuf );
  985. if( result!= NULL ) {
  986. if( result->size() > 0 ) {
  987. Assoc * cArr;
  988. Assoc::iterator it;
  989. cArr = result->at(0);
  990. it = cArr->find("id");
  991. if( it != cArr->end() ){
  992. RetVal = atoi( it->second.c_str() );
  993. };
  994. };
  995. };
  996. deleteResult( result );
  997. };
  998. return RetVal;
  999. }
  1000. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  1001. uint32_t RetVal = 0;
  1002. if(m_bIsConnected){
  1003. char tQBuf[CTEXT];
  1004. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  1005. #ifdef DEBUG_ALL
  1006. LOG(tQBuf);
  1007. #endif
  1008. AAssoc *result;
  1009. result = m_pDataStorage->Query( tQBuf );
  1010. if( result!= NULL ) {
  1011. if( result->size() > 0 ) {
  1012. Assoc * cArr;
  1013. Assoc::iterator it;
  1014. cArr = result->at(0);
  1015. it = cArr->find("id");
  1016. if( it != cArr->end() ){
  1017. RetVal = atoi( it->second.c_str() );
  1018. };
  1019. };
  1020. };
  1021. deleteResult( result );
  1022. };
  1023. return RetVal;
  1024. };
  1025. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  1026. uint32_t tVal = 0;
  1027. if(m_bIsConnected){
  1028. char tQBuf[CTEXT];
  1029. AAssoc *result;
  1030. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  1031. #ifdef DEBUG_ALL
  1032. LOG(tQBuf);
  1033. #endif
  1034. result = m_pDataStorage->Query( tQBuf );
  1035. if( result!= NULL ) {
  1036. if( result->size() > 0 ) {
  1037. Assoc *cArr;
  1038. Assoc::iterator it;
  1039. cArr = result->at(0);
  1040. it = cArr->find("id");
  1041. if( it != cArr->end() ){
  1042. tVal = atoi( it->second.c_str() );
  1043. };
  1044. };
  1045. };
  1046. deleteResult( result );
  1047. };
  1048. return tVal;
  1049. };
  1050. DBC_INT_LIST *sqliteDBC::listModules(){
  1051. DBC_INT_LIST * tVal = NULL;
  1052. if(m_bIsConnected){
  1053. AAssoc *result;
  1054. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  1055. if( result!= NULL ) {
  1056. if( result->size() > 0 ) {
  1057. Assoc *cArr;
  1058. tVal = new DBC_INT_LIST();
  1059. if( tVal != NULL ) {
  1060. Assoc::iterator it;
  1061. for(int i=0;i<result->size();i++) {
  1062. cArr = result->at(i);
  1063. it = cArr->find("id");
  1064. if( it != cArr->end() ){
  1065. tVal->push_back( atoi( it->second.c_str() ));
  1066. };
  1067. };
  1068. };
  1069. };
  1070. };
  1071. deleteResult( result );
  1072. };
  1073. return tVal;
  1074. };
  1075. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  1076. DBC_INT_LIST * tVal = NULL;
  1077. char tParam[CTEXT];
  1078. //memset( tParam, 0, CTEXT );
  1079. sprintf(tParam,"0");
  1080. if( old_ids != NULL ) {
  1081. char tBuffer[CTEXT];
  1082. for(int i = 0; i< old_ids->size(); i++ ) {
  1083. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  1084. strncpy( tParam, tBuffer, CTEXT );
  1085. };
  1086. };
  1087. if(m_bIsConnected){
  1088. char tQBuf[CTEXT];
  1089. AAssoc *result;
  1090. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  1091. #ifdef DEBUG_ALL
  1092. LOG(tQBuf);
  1093. #endif
  1094. result = m_pDataStorage->Query( tQBuf );
  1095. if( result!= NULL ) {
  1096. if( result->size() > 0 ) {
  1097. Assoc *cArr;
  1098. tVal = new DBC_INT_LIST();
  1099. if( tVal != NULL ) {
  1100. Assoc::iterator it;
  1101. for(int i=0;i<result->size();i++) {
  1102. cArr = result->at(i);
  1103. it = cArr->find("id");
  1104. if( it != cArr->end() ){
  1105. tVal->push_back( atoi( it->second.c_str() ));
  1106. };
  1107. };
  1108. };
  1109. };
  1110. };
  1111. deleteResult( result );
  1112. };
  1113. return tVal;
  1114. };
  1115. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  1116. DBC_INT_LIST * tVal = NULL;
  1117. char tParam[CTEXT];
  1118. //memset( tParam, 0, CTEXT );
  1119. sprintf(tParam,"0");
  1120. if( old_ids != NULL ) {
  1121. char tBuffer[CTEXT];
  1122. for(int i = 0; i< old_ids->size(); i++ ) {
  1123. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  1124. strncpy( tParam, tBuffer, CTEXT );
  1125. };
  1126. };
  1127. if( m_bIsConnected ){
  1128. char tQBuf[CTEXT];
  1129. AAssoc *result;
  1130. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `id` NOT IN(%s)", tParam );
  1131. #ifdef DEBUG_EXTRA
  1132. LOG(tQBuf);
  1133. #endif
  1134. result = m_pDataStorage->Query( tQBuf );
  1135. if( result!= NULL ) {
  1136. if( result->size() > 0 ) {
  1137. Assoc *cArr;
  1138. tVal = new DBC_INT_LIST();
  1139. if( tVal != NULL ) {
  1140. Assoc::iterator it;
  1141. for(int i=0;i<result->size();i++) {
  1142. cArr = result->at(i);
  1143. it = cArr->find("id");
  1144. if( it != cArr->end() ){
  1145. tVal->push_back( atoi( it->second.c_str() ));
  1146. };
  1147. };
  1148. };
  1149. };
  1150. };
  1151. deleteResult( result );
  1152. };
  1153. return tVal;
  1154. };
  1155. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  1156. DBC_INT_LIST * tVal = NULL;
  1157. if( m_bIsConnected ){
  1158. char tQBuf[1024];
  1159. AAssoc *result;
  1160. if( all ) {
  1161. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  1162. } else {
  1163. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0" );
  1164. };
  1165. #ifdef DEBUG_EXTRA
  1166. LOG(tQBuf);
  1167. #endif
  1168. result = m_pDataStorage->Query( tQBuf );
  1169. if( result!= NULL ) {
  1170. if( result->size() > 0 ) {
  1171. Assoc *cArr;
  1172. tVal = new DBC_INT_LIST();
  1173. if( tVal != NULL ) {
  1174. Assoc::iterator it;
  1175. for(int i=0; i <result->size();i++) {
  1176. cArr = result->at(i);
  1177. it = cArr->find("id");
  1178. if( it != cArr->end() ){
  1179. tVal->push_back( atoi( it->second.c_str() ));
  1180. };
  1181. };
  1182. };
  1183. };
  1184. };
  1185. deleteResult( result );
  1186. };
  1187. return tVal;
  1188. };
  1189. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  1190. DBC_INT_LIST * tVal = NULL;
  1191. if(m_bIsConnected){
  1192. char tQBuf[CTEXT];
  1193. AAssoc *result;
  1194. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  1195. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  1196. #ifdef DEBUG
  1197. LOG(tQBuf);
  1198. #endif
  1199. result = m_pDataStorage->Query( tQBuf );
  1200. if( result!= NULL ) {
  1201. if( result->size() > 0 ) {
  1202. Assoc *cArr;
  1203. tVal = new DBC_INT_LIST();
  1204. if( tVal != NULL ) {
  1205. Assoc::iterator it;
  1206. for(int i=0;i<result->size();i++) {
  1207. cArr = result->at(i);
  1208. it = cArr->find("id");
  1209. if( it != cArr->end() ){
  1210. tVal->push_back( atoi( it->second.c_str() ));
  1211. };
  1212. };
  1213. };
  1214. };
  1215. };
  1216. deleteResult( result );
  1217. };
  1218. return tVal;
  1219. };
  1220. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  1221. if( m_bIsConnected ){
  1222. m_nSiteID = SiteID;
  1223. char tQBuf[MAX_PATH_LEN];
  1224. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  1225. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  1226. #ifdef DEBUG_ALL
  1227. LOG(tQBuf);
  1228. #endif
  1229. m_pDataStorage->SilentQuery( tQBuf );
  1230. m_nServerID = getServerID( identity );
  1231. *ServID = m_nServerID;
  1232. if( m_nServerID ) {
  1233. //if ( bRecreateMemStructures )
  1234. initMemDB( m_nServerID );
  1235. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  1236. #ifdef DEBUG_ALL
  1237. LOG(tQBuf);
  1238. #endif
  1239. m_pDataMemory->SilentQuery( tQBuf );
  1240. sprintf(tQBuf,"INSERT INTO `nodes`(`aux_id`,`type`,`online`,`name`) VALUES (%d, 0,1,'Controller') ON CONFLICT(`type`,`aux_id`) DO UPDATE SET `online`=1", *ServID );
  1241. #ifdef DEBUG_ALL
  1242. LOG(tQBuf);
  1243. #endif
  1244. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  1245. if( m_nServerNodeID== 0 ){
  1246. m_nServerNodeID = getServerNode( m_nServerID );
  1247. };
  1248. *ServNode = m_nServerNodeID;
  1249. // Clenup Nodes and services ...
  1250. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  1251. #ifdef DEBUG_ALL
  1252. LOG(tQBuf);
  1253. #endif
  1254. m_pDataStorage->SilentQuery( tQBuf );
  1255. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  1256. #ifdef DEBUG_ALL
  1257. LOG(tQBuf);
  1258. #endif
  1259. m_pDataStorage->SilentQuery( tQBuf );
  1260. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  1261. #ifdef DEBUG_ALL
  1262. LOG(tQBuf);
  1263. #endif
  1264. m_pDataStorage->SilentQuery( tQBuf );
  1265. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  1266. #ifdef DEBUG_ALL
  1267. // LOG(tQBuf);
  1268. #endif
  1269. // m_pDataStorage->SilentQuery( tQBuf );
  1270. //cleanResources( tServerNodeID );
  1271. setNodeOnline( m_nServerNodeID );
  1272. };
  1273. };
  1274. };
  1275. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  1276. if(m_bIsConnected){
  1277. char tQBuf[CTEXT];
  1278. if( NodeID ) {
  1279. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  1280. #ifdef DEBUG_ALL
  1281. LOG(tQBuf);
  1282. #endif
  1283. m_pDataStorage->SilentQuery( tQBuf );
  1284. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  1285. #ifdef DEBUG_ALL
  1286. LOG(tQBuf);
  1287. #endif
  1288. m_pDataStorage->SilentQuery( tQBuf );
  1289. return true;
  1290. };
  1291. };
  1292. return false;
  1293. };
  1294. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  1295. if(m_bIsConnected){
  1296. char tQBuf[CTEXT];
  1297. if( NodeID ) {
  1298. sprintf(tQBuf,"UPDATE nodes SET `online`=%d WHERE `id`=%d", status, NodeID );
  1299. #ifdef DEBUG_ALL
  1300. LOG(tQBuf);
  1301. #endif
  1302. m_pDataStorage->SilentQuery( tQBuf );
  1303. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  1304. #ifdef DEBUG_ALL
  1305. LOG(tQBuf);
  1306. #endif
  1307. m_pDataStorage->SilentQuery( tQBuf );
  1308. return true;
  1309. };
  1310. };
  1311. return false;
  1312. };
  1313. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  1314. if( m_bIsConnected ) {
  1315. char tQBuf[MAX_PATH_LEN];
  1316. if ( status ) {
  1317. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  1318. } else {
  1319. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  1320. };
  1321. #ifdef DEBUG
  1322. LOG(tQBuf);
  1323. #endif
  1324. m_pDataStorage->SilentQuery( tQBuf );
  1325. return true;
  1326. };
  1327. return false;
  1328. };
  1329. void sqliteDBC::unregisterServer( void ){
  1330. setNodeOffline( m_nServerNodeID );
  1331. };
  1332. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  1333. if( m_bIsConnected ) {
  1334. char tQBuf[MAX_PATH_LEN];
  1335. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  1336. ptr->OS_INFO,
  1337. ptr->HW_INFO,
  1338. m_nSiteID
  1339. );
  1340. #ifdef DEBUG_ALL
  1341. LOG(tQBuf);
  1342. #endif
  1343. m_pDataStorage->SilentQuery( tQBuf );
  1344. return true;
  1345. };
  1346. return false;
  1347. };
  1348. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  1349. uint32_t RetVal = 0;
  1350. if(m_bIsConnected){
  1351. char tQBuf[CTEXT];
  1352. AAssoc *result;
  1353. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  1354. #ifdef DEBUG_ALL
  1355. LOG(tQBuf);
  1356. #endif
  1357. result = m_pDataStorage->Query( tQBuf );
  1358. if( result!= NULL ) {
  1359. if( result->size() > 0 ) {
  1360. Assoc *cArr;
  1361. Assoc::iterator it;
  1362. cArr = result->at(0);
  1363. it = cArr->find("id");
  1364. if( it != cArr->end() ){
  1365. RetVal = atoi( it->second.c_str() );
  1366. };
  1367. };
  1368. };
  1369. deleteResult( result );
  1370. };
  1371. return RetVal;
  1372. };
  1373. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  1374. switch ( pid ) {
  1375. case 0: return strdup( m_pDbFile );
  1376. };
  1377. return NULL;
  1378. };
  1379. #undef LOG
  1380. #undef LOG_ERROR