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.

1305 lines
44 KiB

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_ALL
  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_ALL
  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. uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
  627. uint32_t RetVal = 0;
  628. if(m_bIsConnected){
  629. char tQBuf[CTEXT];
  630. AAssoc *result;
  631. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
  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. Assoc::iterator it;
  640. cArr = result->at(0);
  641. it = cArr->find("id");
  642. if( it != cArr->end() ){
  643. RetVal = atoi( it->second.c_str() );
  644. };
  645. };
  646. };
  647. deleteResult( result );
  648. };
  649. return RetVal;
  650. };
  651. DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
  652. DBC_MODULE_DATA *tVal = NULL;
  653. if(m_bIsConnected){
  654. char tQBuf[CTEXT];
  655. AAssoc *result;
  656. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
  657. #ifdef DEBUG_ALL
  658. LOG(tQBuf);
  659. #endif
  660. result = m_pDataStorage->Query( tQBuf );
  661. if( result!= NULL ) {
  662. if( result->size() > 0 ) {
  663. Assoc *cArr;
  664. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  665. if( tVal != NULL ) {
  666. Assoc::iterator it;
  667. cArr = result->at(0);
  668. it = cArr->find("id");
  669. if( it != cArr->end() ){
  670. tVal->module_id = atoi( it->second.c_str() );
  671. };
  672. it = cArr->find("aux_id");
  673. if( it != cArr->end() ){
  674. tVal->aux_id = atoi( it->second.c_str() );
  675. };
  676. it = cArr->find("type");
  677. if( it != cArr->end() ){
  678. tVal->type = atoi( it->second.c_str() );
  679. };
  680. it = cArr->find("enabled");
  681. if( it != cArr->end() ){
  682. tVal->enabled = (bool)atoi( it->second.c_str() );
  683. };
  684. };
  685. };
  686. };
  687. deleteResult( result );
  688. };
  689. return tVal;
  690. };
  691. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  692. DBC_NET_IF_DATA * tVal = NULL;
  693. if(m_bIsConnected){
  694. char tQBuf[CTEXT];
  695. AAssoc *result;
  696. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  697. #ifdef DEBUG_EXTRA
  698. LOG(tQBuf);
  699. #endif
  700. result = m_pDataStorage->Query( tQBuf );
  701. if( result!= NULL ) {
  702. if( result->size() > 0 ) {
  703. Assoc *cArr;
  704. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  705. if( tVal != NULL ) {
  706. Assoc::iterator it;
  707. tVal->id = nif_id;
  708. cArr = result->at(0);
  709. it = cArr->find("type");
  710. if( it != cArr->end() ){
  711. tVal->type = atoi(it->second.c_str());
  712. };
  713. it = cArr->find("mode");
  714. if( it != cArr->end() ){
  715. tVal->mode = atoi(it->second.c_str());
  716. };
  717. it = cArr->find("device_name");
  718. if( it != cArr->end() ){
  719. strncpy( tVal->device_name, it->second.c_str(),11);
  720. tVal->device_name[10]=(char)0;
  721. };
  722. it = cArr->find("mac");
  723. if( it != cArr->end() ){
  724. strncpy( tVal->mac, it->second.c_str(), 33 );
  725. tVal->mac[32]=(char)0;
  726. };
  727. it = cArr->find("aux_id");
  728. if( it != cArr->end() ){
  729. tVal->aux_id = atoi(it->second.c_str());
  730. };
  731. };
  732. };
  733. };
  734. deleteResult( result );
  735. };
  736. return tVal;
  737. };
  738. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  739. uint32_t tVal = 0;
  740. if(m_bIsConnected){
  741. char tQBuf[CTEXT];
  742. AAssoc *result;
  743. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  744. #ifdef DEBUG_ALL
  745. LOG(tQBuf);
  746. #endif
  747. result = m_pDataStorage->Query( tQBuf );
  748. if( result!= NULL ) {
  749. if( result->size() > 0 ) {
  750. Assoc *cArr;
  751. Assoc::iterator it;
  752. cArr = result->at(0);
  753. it = cArr->find("id");
  754. if( it != cArr->end() ){
  755. tVal = atoi( it->second.c_str() );
  756. };
  757. };
  758. };
  759. deleteResult( result );
  760. };
  761. return tVal;
  762. };
  763. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  764. uint32_t tVal = 0;
  765. if(m_bIsConnected){
  766. char tQBuf[CTEXT];
  767. AAssoc *result;
  768. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  769. #ifdef DEBUG_ALL
  770. LOG(tQBuf);
  771. #endif
  772. result = m_pDataStorage->Query( tQBuf );
  773. if( result!= NULL ) {
  774. if( result->size() > 0 ) {
  775. Assoc *cArr;
  776. Assoc::iterator it;
  777. cArr = result->at(0);
  778. it = cArr->find("id");
  779. if( it != cArr->end() ){
  780. tVal = atoi( it->second.c_str() );
  781. };
  782. };
  783. };
  784. deleteResult( result );
  785. };
  786. return tVal;
  787. };
  788. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  789. uint32_t tVal = 0;
  790. if(m_bIsConnected){
  791. char tQBuf[CTEXT];
  792. AAssoc *result;
  793. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  794. #ifdef DEBUG_ALL
  795. LOG(tQBuf);
  796. #endif
  797. result = m_pDataStorage->Query( tQBuf );
  798. if( result!= NULL ) {
  799. if( result->size() > 0 ) {
  800. Assoc *cArr;
  801. Assoc::iterator it;
  802. cArr = result->at(0);
  803. it = cArr->find("id");
  804. if( it != cArr->end() ){
  805. tVal = atoi( it->second.c_str() );
  806. };
  807. };
  808. };
  809. deleteResult( result );
  810. };
  811. return tVal;
  812. };
  813. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  814. DBC_RESOURCE_DATA *tVal = NULL;
  815. if( m_bIsConnected ){
  816. char tQBuf[CTEXT];
  817. AAssoc *result;
  818. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  819. #ifdef DEBUG_ALL
  820. LOG(tQBuf);
  821. #endif
  822. result = m_pDataStorage->Query( tQBuf );
  823. if( result!= NULL ) {
  824. if( result->size() > 0 ) {
  825. Assoc *cArr;
  826. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  827. if( tVal != NULL ) {
  828. Assoc::iterator it;
  829. cArr = result->at(0);
  830. tVal->id = ResourceID;
  831. it = cArr->find("node_id");
  832. if( it != cArr->end() ){
  833. tVal->node_id = atoi( it->second.c_str() );
  834. };
  835. it = cArr->find("type");
  836. if( it != cArr->end() ){
  837. tVal->type = atoi( it->second.c_str() );
  838. };
  839. it = cArr->find("aux_id");
  840. if( it != cArr->end() ){
  841. tVal->aux_id = atoi( it->second.c_str() );
  842. };
  843. it = cArr->find("device");
  844. if( it != cArr->end() ){
  845. strncpy( tVal->device, it->second.c_str(), 64 );
  846. tVal->device[63] = (char)0;
  847. };
  848. };
  849. };
  850. };
  851. deleteResult( result );
  852. };
  853. return tVal;
  854. };
  855. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  856. uint32_t RetVal = 0;
  857. if(m_bIsConnected){
  858. char tQBuf[CTEXT];
  859. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  860. #ifdef DEBUG_ALL
  861. LOG(tQBuf);
  862. #endif
  863. AAssoc *result;
  864. result = m_pDataStorage->Query( tQBuf );
  865. if( result!= NULL ) {
  866. if( result->size() > 0 ) {
  867. Assoc * cArr;
  868. Assoc::iterator it;
  869. cArr = result->at(0);
  870. it = cArr->find("id");
  871. if( it != cArr->end() ){
  872. RetVal = atoi( it->second.c_str() );
  873. };
  874. };
  875. };
  876. deleteResult( result );
  877. };
  878. return RetVal;
  879. };
  880. uint32_t sqliteDBC::getServerID( CSTR identity ){
  881. uint32_t RetVal = 0;
  882. if(m_bIsConnected){
  883. char tQBuf[CTEXT];
  884. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  885. #ifdef DEBUG_ALL
  886. LOG(tQBuf);
  887. #endif
  888. AAssoc *result;
  889. result = m_pDataStorage->Query( tQBuf );
  890. if( result!= NULL ) {
  891. if( result->size() > 0 ) {
  892. Assoc * cArr;
  893. Assoc::iterator it;
  894. cArr = result->at(0);
  895. it = cArr->find("id");
  896. if( it != cArr->end() ){
  897. RetVal = atoi( it->second.c_str() );
  898. };
  899. };
  900. };
  901. deleteResult( result );
  902. };
  903. return RetVal;
  904. };
  905. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  906. uint32_t RetVal = 0;
  907. if(m_bIsConnected){
  908. char tQBuf[CTEXT];
  909. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  910. #ifdef DEBUG_ALL
  911. LOG(tQBuf);
  912. #endif
  913. AAssoc *result;
  914. result = m_pDataStorage->Query( tQBuf );
  915. if( result!= NULL ) {
  916. if( result->size() > 0 ) {
  917. Assoc * cArr;
  918. Assoc::iterator it;
  919. cArr = result->at(0);
  920. it = cArr->find("id");
  921. if( it != cArr->end() ){
  922. RetVal = atoi( it->second.c_str() );
  923. };
  924. };
  925. };
  926. deleteResult( result );
  927. };
  928. return RetVal;
  929. }
  930. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  931. uint32_t RetVal = 0;
  932. if(m_bIsConnected){
  933. char tQBuf[CTEXT];
  934. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  935. #ifdef DEBUG_ALL
  936. LOG(tQBuf);
  937. #endif
  938. AAssoc *result;
  939. result = m_pDataStorage->Query( tQBuf );
  940. if( result!= NULL ) {
  941. if( result->size() > 0 ) {
  942. Assoc * cArr;
  943. Assoc::iterator it;
  944. cArr = result->at(0);
  945. it = cArr->find("id");
  946. if( it != cArr->end() ){
  947. RetVal = atoi( it->second.c_str() );
  948. };
  949. };
  950. };
  951. deleteResult( result );
  952. };
  953. return RetVal;
  954. };
  955. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  956. uint32_t tVal = 0;
  957. if(m_bIsConnected){
  958. char tQBuf[CTEXT];
  959. AAssoc *result;
  960. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  961. #ifdef DEBUG_ALL
  962. LOG(tQBuf);
  963. #endif
  964. result = m_pDataStorage->Query( tQBuf );
  965. if( result!= NULL ) {
  966. if( result->size() > 0 ) {
  967. Assoc *cArr;
  968. Assoc::iterator it;
  969. cArr = result->at(0);
  970. it = cArr->find("id");
  971. if( it != cArr->end() ){
  972. tVal = atoi( it->second.c_str() );
  973. };
  974. };
  975. };
  976. deleteResult( result );
  977. };
  978. return tVal;
  979. };
  980. DBC_INT_LIST *sqliteDBC::listModules(){
  981. DBC_INT_LIST * tVal = NULL;
  982. if(m_bIsConnected){
  983. AAssoc *result;
  984. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  985. if( result!= NULL ) {
  986. if( result->size() > 0 ) {
  987. Assoc *cArr;
  988. tVal = new DBC_INT_LIST();
  989. if( tVal != NULL ) {
  990. Assoc::iterator it;
  991. for(int i=0;i<result->size();i++) {
  992. cArr = result->at(i);
  993. it = cArr->find("id");
  994. if( it != cArr->end() ){
  995. tVal->push_back( atoi( it->second.c_str() ));
  996. };
  997. };
  998. };
  999. };
  1000. };
  1001. deleteResult( result );
  1002. };
  1003. return tVal;
  1004. };
  1005. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  1006. DBC_INT_LIST * tVal = NULL;
  1007. char tParam[CTEXT];
  1008. //memset( tParam, 0, CTEXT );
  1009. sprintf(tParam,"0");
  1010. if( old_ids != NULL ) {
  1011. char tBuffer[CTEXT];
  1012. for(int i = 0; i< old_ids->size(); i++ ) {
  1013. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  1014. strncpy( tParam, tBuffer, CTEXT );
  1015. };
  1016. };
  1017. if(m_bIsConnected){
  1018. char tQBuf[CTEXT];
  1019. AAssoc *result;
  1020. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  1021. #ifdef DEBUG_ALL
  1022. LOG(tQBuf);
  1023. #endif
  1024. result = m_pDataStorage->Query( tQBuf );
  1025. if( result!= NULL ) {
  1026. if( result->size() > 0 ) {
  1027. Assoc *cArr;
  1028. tVal = new DBC_INT_LIST();
  1029. if( tVal != NULL ) {
  1030. Assoc::iterator it;
  1031. for(int i=0;i<result->size();i++) {
  1032. cArr = result->at(i);
  1033. it = cArr->find("id");
  1034. if( it != cArr->end() ){
  1035. tVal->push_back( atoi( it->second.c_str() ));
  1036. };
  1037. };
  1038. };
  1039. };
  1040. };
  1041. deleteResult( result );
  1042. };
  1043. return tVal;
  1044. };
  1045. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  1046. DBC_INT_LIST * tVal = NULL;
  1047. if(m_bIsConnected){
  1048. char tQBuf[CTEXT];
  1049. AAssoc *result;
  1050. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  1051. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  1052. #ifdef DEBUG
  1053. LOG(tQBuf);
  1054. #endif
  1055. result = m_pDataStorage->Query( tQBuf );
  1056. if( result!= NULL ) {
  1057. if( result->size() > 0 ) {
  1058. Assoc *cArr;
  1059. tVal = new DBC_INT_LIST();
  1060. if( tVal != NULL ) {
  1061. Assoc::iterator it;
  1062. for(int i=0;i<result->size();i++) {
  1063. cArr = result->at(i);
  1064. it = cArr->find("id");
  1065. if( it != cArr->end() ){
  1066. tVal->push_back( atoi( it->second.c_str() ));
  1067. };
  1068. };
  1069. };
  1070. };
  1071. };
  1072. deleteResult( result );
  1073. };
  1074. return tVal;
  1075. };
  1076. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  1077. if( m_bIsConnected ){
  1078. m_nSiteID = SiteID;
  1079. char tQBuf[MAX_PATH_LEN];
  1080. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  1081. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  1082. #ifdef DEBUG_ALL
  1083. LOG(tQBuf);
  1084. #endif
  1085. m_pDataStorage->SilentQuery( tQBuf );
  1086. m_nServerID = getServerID( identity );
  1087. *ServID = m_nServerID;
  1088. if( m_nServerID ) {
  1089. //if ( bRecreateMemStructures )
  1090. initMemDB( m_nServerID );
  1091. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  1092. #ifdef DEBUG_ALL
  1093. LOG(tQBuf);
  1094. #endif
  1095. m_pDataMemory->SilentQuery( tQBuf );
  1096. 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 );
  1097. #ifdef DEBUG_ALL
  1098. LOG(tQBuf);
  1099. #endif
  1100. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  1101. if( m_nServerNodeID== 0 ){
  1102. m_nServerNodeID = getServerNode( m_nServerID );
  1103. };
  1104. *ServNode = m_nServerNodeID;
  1105. // Clenup Nodes and services ...
  1106. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  1107. #ifdef DEBUG_ALL
  1108. LOG(tQBuf);
  1109. #endif
  1110. m_pDataStorage->SilentQuery( tQBuf );
  1111. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  1112. #ifdef DEBUG_ALL
  1113. LOG(tQBuf);
  1114. #endif
  1115. m_pDataStorage->SilentQuery( tQBuf );
  1116. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  1117. #ifdef DEBUG_ALL
  1118. LOG(tQBuf);
  1119. #endif
  1120. m_pDataStorage->SilentQuery( tQBuf );
  1121. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  1122. #ifdef DEBUG_ALL
  1123. // LOG(tQBuf);
  1124. #endif
  1125. // m_pDataStorage->SilentQuery( tQBuf );
  1126. //cleanResources( tServerNodeID );
  1127. setNodeOnline( m_nServerNodeID );
  1128. };
  1129. };
  1130. };
  1131. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  1132. if(m_bIsConnected){
  1133. char tQBuf[CTEXT];
  1134. if( NodeID ) {
  1135. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  1136. #ifdef DEBUG_ALL
  1137. LOG(tQBuf);
  1138. #endif
  1139. m_pDataStorage->SilentQuery( tQBuf );
  1140. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  1141. #ifdef DEBUG_ALL
  1142. LOG(tQBuf);
  1143. #endif
  1144. m_pDataStorage->SilentQuery( tQBuf );
  1145. return true;
  1146. };
  1147. };
  1148. return false;
  1149. };
  1150. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  1151. if(m_bIsConnected){
  1152. char tQBuf[CTEXT];
  1153. if( NodeID ) {
  1154. sprintf(tQBuf,"UPDATE nodes SET `online`=%d WHERE `id`=%d", status, NodeID );
  1155. #ifdef DEBUG_ALL
  1156. LOG(tQBuf);
  1157. #endif
  1158. m_pDataStorage->SilentQuery( tQBuf );
  1159. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  1160. #ifdef DEBUG_ALL
  1161. LOG(tQBuf);
  1162. #endif
  1163. m_pDataStorage->SilentQuery( tQBuf );
  1164. return true;
  1165. };
  1166. };
  1167. return false;
  1168. };
  1169. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  1170. if( m_bIsConnected ) {
  1171. char tQBuf[MAX_PATH_LEN];
  1172. if ( status ) {
  1173. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  1174. } else {
  1175. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  1176. };
  1177. #ifdef DEBUG_ALL
  1178. LOG(tQBuf);
  1179. #endif
  1180. m_pDataStorage->SilentQuery( tQBuf );
  1181. return true;
  1182. };
  1183. return false;
  1184. };
  1185. void sqliteDBC::unregisterServer( void ){
  1186. setNodeOffline( m_nServerNodeID );
  1187. };
  1188. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  1189. if( m_bIsConnected ) {
  1190. char tQBuf[MAX_PATH_LEN];
  1191. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  1192. ptr->OS_INFO,
  1193. ptr->HW_INFO,
  1194. m_nSiteID
  1195. );
  1196. #ifdef DEBUG_ALL
  1197. LOG(tQBuf);
  1198. #endif
  1199. m_pDataStorage->SilentQuery( tQBuf );
  1200. return true;
  1201. };
  1202. return false;
  1203. };
  1204. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  1205. uint32_t RetVal = 0;
  1206. if(m_bIsConnected){
  1207. char tQBuf[CTEXT];
  1208. AAssoc *result;
  1209. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  1210. #ifdef DEBUG_ALL
  1211. LOG(tQBuf);
  1212. #endif
  1213. result = m_pDataStorage->Query( tQBuf );
  1214. if( result!= NULL ) {
  1215. if( result->size() > 0 ) {
  1216. Assoc *cArr;
  1217. Assoc::iterator it;
  1218. cArr = result->at(0);
  1219. it = cArr->find("id");
  1220. if( it != cArr->end() ){
  1221. RetVal = atoi( it->second.c_str() );
  1222. };
  1223. };
  1224. };
  1225. deleteResult( result );
  1226. };
  1227. return RetVal;
  1228. };
  1229. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  1230. switch ( pid ) {
  1231. case 0: return strdup( m_pDbFile );
  1232. };
  1233. return NULL;
  1234. };
  1235. #undef LOG
  1236. #undef LOG_ERROR