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.

1855 lines
67 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
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. m_pDataMemory->SilentQuery("PRAGMA journal_mode=WAL");
  135. //Recreate at Register Server
  136. //if ( bRecreateMemStructures ) initMemDB();
  137. } else{
  138. LOG_ERROR("Cannot Connect to DB Memory");
  139. delete( m_pDataStorage );
  140. delete( m_pDataMemory );
  141. m_pDataStorage = NULL;
  142. m_pDataMemory = NULL;
  143. return false;
  144. };
  145. };
  146. m_bIsConnected = true;
  147. return m_bIsConnected;
  148. };
  149. void sqliteDBC::initMemDB( uint32_t sid ){
  150. m_pDataMemory->SilentQuery("DROP TABLE \"nodes_qcq\"");
  151. m_pDataMemory->SilentQuery("CREATE TABLE \"nodes_qcq\" ( \
  152. \"id\" INTEGER NOT NULL,\
  153. \"node_id\" INTEGER NOT NULL,\
  154. \"cmd\" INTEGER NOT NULL,\
  155. \"status\" INTEGER DEFAULT 0,\
  156. \"parameter\" TEXT,\
  157. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  158. );");
  159. m_pDataMemory->SilentQuery("DROP TABLE \"servers\"");
  160. m_pDataMemory->SilentQuery("CREATE TABLE \"servers\" (\
  161. \"id\" INTEGER NOT NULL,\
  162. \"ts\" INTEGER DEFAULT 0,\
  163. \"pid\" INTEGER DEFAULT 0,\
  164. \"hw_info\" TEXT,\
  165. \"os_info\" TEXT,\
  166. \"cpu_cores\" INTEGER DEFAULT 0.0,\
  167. \"stat_cpu1\" REAL DEFAULT 0.0,\
  168. \"stat_cpu5\" REAL DEFAULT 0.0,\
  169. \"stat_cpu15\" REAL DEFAULT 0.0,\
  170. \"stat_mem\" REAL DEFAULT 0.0,\
  171. \"stat_disk\" REAL DEFAULT 0.0,\
  172. \"val_mem_tottal\" INTEGER DEFAULT 0,\
  173. \"val_mem_current\" INTEGER DEFAULT 0,\
  174. \"val_disk_tottal\" INTEGER DEFAULT 0,\
  175. \"val_disk_current\" INTEGER DEFAULT 0,\
  176. \"temp0\" REAL DEFAULT 0.0,\
  177. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  178. );");
  179. char tB[128];
  180. sprintf( tB, "INSERT INTO `servers` (`id`,`ts`) VALUES (%d, strftime('%%s') )", sid );
  181. m_pDataMemory->SilentQuery( tB );
  182. };
  183. DBC_PUSHED_CMD *sqliteDBC::getNodePushedCMD( uint32_t NodeID ){
  184. DBC_PUSHED_CMD * RetVal = NULL;
  185. if ( m_bIsConnected ) {
  186. char QS[MAX_PATH];
  187. AAssoc * result;
  188. sprintf( QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID );
  189. result = m_pDataMemory->Query( QS );
  190. if ( result ) {
  191. if( result->size() > 0 ) {
  192. RetVal = (DBC_PUSHED_CMD*)malloc(sizeof(DBC_PUSHED_CMD));
  193. if( RetVal ) {
  194. Assoc * cArr;
  195. Assoc::iterator it;
  196. memset( RetVal, 0, sizeof(DBC_PUSHED_CMD));
  197. cArr = result->at(0);
  198. it = cArr->find("id");
  199. if( it != cArr->end() ){
  200. RetVal->id = atoi( it->second.c_str() );
  201. };
  202. it = cArr->find("cmd");
  203. if( it != cArr->end() ){
  204. RetVal->CMD = atoi( it->second.c_str() );
  205. };
  206. it = cArr->find("parameter");
  207. if( it != cArr->end() ){
  208. strncpy( RetVal->PARAM, it->second.c_str(), 256 );
  209. RetVal->PARAM[255] = (char)0;
  210. };
  211. };
  212. };
  213. };
  214. m_pDataMemory->deleteResult( result );
  215. };
  216. return RetVal;
  217. };
  218. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  219. if(m_bIsConnected) {
  220. char tQBuf[MAX_PATH_LEN];
  221. char tIpBuf[CTEXT];
  222. char *tPos;
  223. char *tPos2;
  224. tPos = strstr(Addr,"//");
  225. if( tPos != NULL ) {
  226. tPos2 = strstr( tPos + 2,"/");
  227. if( tPos2 != NULL ) {
  228. int len;
  229. len = tPos2 - tPos - 2;
  230. strncpy(tIpBuf,tPos + 2,len);
  231. tIpBuf[len] = (char)0;
  232. } else {
  233. LOG_ERROR("Pos 2 - err");
  234. tIpBuf[0] = (char)0;
  235. };
  236. } else {
  237. LOG_ERROR("Pos 0 - err");
  238. tIpBuf[0] = (char)0;
  239. };
  240. sprintf(tQBuf,"UPDATE cameras SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  241. Addr,
  242. tIpBuf,
  243. CamID
  244. );
  245. #ifdef DEBUG_ALL
  246. LOG(tQBuf);
  247. #endif
  248. m_pDataStorage->SilentQuery( tQBuf );
  249. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  250. #ifdef DEBUG_ALL
  251. LOG(tQBuf);
  252. #endif
  253. m_pDataStorage->SilentQuery( tQBuf );
  254. return true;
  255. };
  256. return false;
  257. };
  258. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  259. if(m_bIsConnected) {
  260. uint32_t cID = getCamId( uuid );
  261. if ( cID ) {
  262. return updateIPCam( cID, Addr );
  263. };
  264. };
  265. return false;
  266. };
  267. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  268. if(m_bIsConnected) {
  269. uint32_t cID = getCamId( uuid );
  270. if ( cID ) {
  271. return updateLocalCam( cID );
  272. };
  273. };
  274. return false;
  275. };
  276. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  277. if(m_bIsConnected) {
  278. char tQBuf[MAX_PATH_LEN];
  279. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  280. #ifdef DEBUG
  281. LOG(tQBuf);
  282. #endif
  283. m_pDataStorage->SilentQuery( tQBuf );
  284. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  285. #ifdef DEBUG
  286. LOG(tQBuf);
  287. #endif
  288. m_pDataStorage->SilentQuery( tQBuf );
  289. return true;
  290. };
  291. return false;
  292. };
  293. bool sqliteDBC::updateNodePushedCMD( uint32_t CmdID, uint8_t status ){
  294. if ( m_bIsConnected ) {
  295. char QS[MAX_PATH];
  296. sprintf( QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID );
  297. return m_pDataMemory->SilentQuery( QS );
  298. };
  299. return false;
  300. };
  301. bool sqliteDBC::cleanNodePushedCMD( void ){
  302. if ( m_bIsConnected ) {
  303. return m_pDataMemory->SilentQuery( "DELETE FROM `nodes_qcq` WHERE `status` > 200" );
  304. };
  305. return false;
  306. };
  307. bool sqliteDBC::updateServerUtilisation( DBC_SYS_LOAD_INFO *ptr ){
  308. if( m_bIsConnected ) {
  309. char tQBuf[MAX_PATH_LEN];
  310. 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",
  311. ptr->CPU_CORES,
  312. ptr->cpu1,
  313. ptr->cpu5,
  314. ptr->cpu15,
  315. ptr->mem,
  316. ptr->disk,
  317. ptr->temperature,
  318. ptr->T_DISK,
  319. ptr->C_DISK,
  320. ptr->T_MEM,
  321. ptr->C_MEM,
  322. m_nServerID
  323. );
  324. #ifdef DEBUG_ALL
  325. LOG(tQBuf);
  326. #endif
  327. m_pDataMemory->SilentQuery( tQBuf );
  328. return true;
  329. };
  330. return false;
  331. };
  332. /********************************************************************
  333. * *
  334. * Slow DB Functions *
  335. * *
  336. ********************************************************************/
  337. bool sqliteDBC::addDeviceDraw( uint32_t DBID, uint8_t type, uint8_t lrf, void *th_data, uint32_t th_size ){
  338. return false;
  339. };
  340. bool sqliteDBC::addIPCam( OBSERVER_RESULT *tPtr ){
  341. if(m_bIsConnected) {
  342. char tQBuf[1024];
  343. char tIpBuf[32];
  344. char tProto[32];
  345. char tPath[CTEXT];
  346. char *tPos;
  347. char *tPos2;
  348. memcpy( tProto, tPtr->XAddrs, 5 );
  349. tProto[5] = (char)0;
  350. if( tProto[4] == ':') tProto[4] = (char)0;
  351. memset( tPath, 0, CTEXT );
  352. tPos = strstr(tPtr->XAddrs,"//");
  353. if( tPos != NULL ) {
  354. tPos2 = strstr( tPos + 2,"/");
  355. if( tPos2 != NULL ) {
  356. int len,len2,len3;
  357. len = tPos2 - tPos - 2;
  358. strncpy( tIpBuf, tPos + 2, len);
  359. tIpBuf[len] = (char)0;
  360. strncpy( tPath, tPos + 2 + len, CTEXT );
  361. } else {
  362. LOG_ERROR("Pos 2 - err");
  363. tIpBuf[0] = (char)0;
  364. };
  365. } else {
  366. LOG_ERROR("Pos 0 - err");
  367. tIpBuf[0] = (char)0;
  368. };
  369. sprintf(tQBuf,"INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s'",
  370. tPtr->RefID,
  371. //tPtr->XAddrs,
  372. tPath,
  373. tIpBuf,
  374. tPtr->MAC,
  375. tPtr->Hardware,
  376. tPtr->Name,
  377. tProto,
  378. //tPtr->XAddrs,
  379. tIpBuf
  380. );
  381. #ifdef DEBUG_ALL
  382. LOG(tQBuf);
  383. #endif
  384. m_pDataStorage->SilentQuery( tQBuf );
  385. return true;
  386. };
  387. return false;
  388. };
  389. uint32_t sqliteDBC::addLocalCam( CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den ){
  390. uint32_t RetVal = 0;
  391. if( m_bIsConnected ) {
  392. char tQBuf[1024];
  393. char tM5[33];
  394. CSTR tMD5 = MD5String( path );
  395. if( tMD5 != NULL ) {
  396. memcpy( tM5, tMD5, 33 );
  397. tM5[32]=(char)0;
  398. free( tMD5 );
  399. sprintf(tQBuf,"INSERT INTO cameras (`online`,`uuid`,`type`,`status`,`hardware`,`name`,`ip`) VALUES (1,'%s',10,0,'%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1",
  400. tM5,
  401. hardware,
  402. name,
  403. path
  404. );
  405. #ifdef DEBUG_ALL
  406. LOG(tQBuf);
  407. #endif
  408. uint32_t camID = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  409. if ( camID == 0 ) {
  410. camID = getCamId( tM5 );
  411. };
  412. sprintf(tQBuf, "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`mrl_proto`,`mrl`,`picture_proto`,`fps`) VALUES(%d,'A',1,'%s','%s',%d,%d,'v4l2','%s','v4l2',%d) ON CONFLICT(`source_id`,`token`) DO UPDATE SET `valid`=1, img_width=%d, img_height=%d, `fps`=%d",
  413. camID, name, tM5, width, height, path, fps, width, height, fps );
  414. #ifdef DEBUG_EXTRA
  415. LOG(tQBuf);
  416. #endif
  417. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  418. if( RetVal == 0 ){
  419. RetVal = getMediaStreamID( camID, tM5 );
  420. };
  421. };
  422. };
  423. return RetVal;
  424. };
  425. uint32_t sqliteDBC::addLocalNetwork( uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast ){
  426. uint32_t RetVal = 0;
  427. if( m_bIsConnected ) {
  428. char tQBuf[MAX_PATH_LEN];
  429. 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'",
  430. NIF_ID,
  431. net_type,
  432. address,
  433. gateway,
  434. mask,
  435. broadcast,
  436. address,
  437. gateway,
  438. mask,
  439. broadcast
  440. );
  441. #ifdef DEBUG_ALL
  442. LOG(tQBuf);
  443. #endif
  444. RetVal = m_pDataStorage->QueryID( tQBuf );
  445. // On Update IF data no need to return ID
  446. //if( RetVal == 0 ) {
  447. // RetVal = getNetworkInterfaceID( tKey );
  448. //};
  449. };
  450. return RetVal;
  451. };
  452. uint32_t sqliteDBC::addLocalNetworkInfrastructure( uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME ){
  453. uint32_t RetVal = 0;
  454. if( m_bIsConnected ) {
  455. char tQBuf[MAX_PATH_LEN];
  456. bool ena_routing = true;
  457. if( OUTBOUND_NIF == 0 ){
  458. ena_routing = false;
  459. };
  460. if( ena_dhcp ){
  461. if ( BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL ) {
  462. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  463. return 0;
  464. };
  465. 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'",
  466. NIF_ID,
  467. ena_routing,
  468. OUTBOUND_NIF,
  469. BRANG,
  470. ERANG,
  471. MASK,
  472. LEASE_TIME,
  473. ena_routing,
  474. OUTBOUND_NIF,
  475. BRANG,
  476. ERANG,
  477. MASK,
  478. LEASE_TIME
  479. );
  480. } else {
  481. if( ena_routing ){
  482. 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",
  483. NIF_ID,
  484. OUTBOUND_NIF,
  485. OUTBOUND_NIF
  486. );
  487. } else {
  488. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  489. return 0;
  490. };
  491. };
  492. #ifdef DEBUG_ALL
  493. LOG(tQBuf);
  494. #endif
  495. RetVal = m_pDataStorage->QueryID( tQBuf );
  496. // On Update IF data no need to return ID
  497. if( RetVal == 0 ) {
  498. RetVal = getNetworkInfrastructureID( NIF_ID );
  499. };
  500. };
  501. return RetVal;
  502. };
  503. uint32_t sqliteDBC::addLocalNIF( CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id ){
  504. uint32_t RetVal = 0;
  505. if( m_bIsConnected ) {
  506. char tQBuf[MAX_PATH_LEN];
  507. char * tKey = MD5String( device );
  508. if( tKey != NULL ) {
  509. 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'",
  510. dtype,
  511. tKey,
  512. device,
  513. mac,
  514. dmode,
  515. aux_id,
  516. dmode,
  517. aux_id,
  518. mac
  519. );
  520. #ifdef DEBUG_ALL
  521. LOG(tQBuf);
  522. #endif
  523. RetVal = m_pDataStorage->QueryID( tQBuf );
  524. if( RetVal == 0 ) {
  525. RetVal = getNIF_ID( tKey );
  526. };
  527. free( tKey );
  528. };
  529. };
  530. return RetVal;
  531. };
  532. 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 ){
  533. uint32_t RetVal = 0;
  534. if( m_bIsConnected ) {
  535. char tQBuf[MAX_PATH_LEN];
  536. 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`",
  537. m_nServerNodeID,
  538. utype,
  539. speed,
  540. bit,
  541. parity,
  542. stop_bit,
  543. mode,
  544. port,
  545. PATH
  546. );
  547. #ifdef DEBUG_ALL
  548. LOG(tQBuf);
  549. #endif
  550. RetVal = m_pDataStorage->QueryID( tQBuf );
  551. if( RetVal == 0 ) {
  552. RetVal = getUartID( m_nServerNodeID, utype, PATH );
  553. };
  554. };
  555. return RetVal;
  556. };
  557. uint32_t sqliteDBC::addLocalWiFi( uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id ){
  558. uint32_t RetVal = 0;
  559. if( m_bIsConnected ) {
  560. char tQBuf[MAX_PATH_LEN];
  561. 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",
  562. NIF_ID,
  563. mode,
  564. router_id,
  565. SSID,
  566. password,
  567. encryption,
  568. mode,
  569. router_id,
  570. SSID,
  571. password,
  572. encryption
  573. );
  574. #ifdef DEBUG_ALL
  575. LOG(tQBuf);
  576. #endif
  577. RetVal = m_pDataStorage->QueryID( tQBuf );
  578. if( RetVal == 0 ) {
  579. RetVal = getWiFi_ID( NIF_ID );
  580. };
  581. };
  582. return RetVal;
  583. };
  584. bool sqliteDBC::cleanResources( bool all ){
  585. if(m_bIsConnected) {
  586. char tQBuf[MAX_PATH_LEN];
  587. if( all ) {
  588. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID );
  589. } else {
  590. 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 );
  591. };
  592. #ifdef DEBUG_ALL
  593. LOG(tQBuf);
  594. #endif
  595. m_pDataStorage->SilentQuery( tQBuf );
  596. return true;
  597. };
  598. return false;
  599. };
  600. uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){
  601. uint32_t RetVal = 0;
  602. if( m_bIsConnected ){
  603. char tQBuf[2048];
  604. sprintf( tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName );
  605. CSTR tMD5 = MD5String( tQBuf );
  606. if( tMD5 ) {
  607. 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",
  608. m_nServerNodeID,
  609. type,
  610. tMD5,
  611. auxID,
  612. devName,
  613. auxID
  614. );
  615. free( tMD5 );
  616. #ifdef DEBUG_ALL
  617. LOG(tQBuf);
  618. #endif
  619. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  620. if( RetVal == 0 ) {
  621. RetVal = getResourceID( m_nServerNodeID, type, auxID );
  622. };
  623. };
  624. };
  625. return RetVal;
  626. };
  627. DBC_MODULE_EMBED_VIDEO_DATA *sqliteDBC::getEmbedVideoData( uint32_t ID ){
  628. DBC_MODULE_EMBED_VIDEO_DATA *tPtr = NULL;
  629. if( m_bIsConnected ) {
  630. char tQBuf[MAX_PATH_LEN];
  631. AAssoc *result;
  632. sprintf( tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID );
  633. #ifdef DEBUG_ALL
  634. LOG(tQBuf);
  635. #endif
  636. result = m_pDataStorage->Query( tQBuf );
  637. if( result!= NULL ) {
  638. if( result->size() > 0 ) {
  639. Assoc *cArr;
  640. tPtr = (DBC_MODULE_EMBED_VIDEO_DATA*)malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  641. if( tPtr!= NULL ) {
  642. Assoc::iterator it;
  643. memset(tPtr,0,sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  644. cArr = result->at(0);
  645. it = cArr->find("output_mode");
  646. if( it != cArr->end() ){
  647. tPtr->output_mode = atoi( it->second.c_str() );
  648. };
  649. it = cArr->find("web_port");
  650. if( it != cArr->end() ){
  651. tPtr->port = atoi( it->second.c_str() );
  652. };
  653. it = cArr->find("width");
  654. if( it != cArr->end() ){
  655. tPtr->width = atoi( it->second.c_str() );
  656. };
  657. it = cArr->find("height");
  658. if( it != cArr->end() ){
  659. tPtr->height = atoi( it->second.c_str() );
  660. };
  661. it = cArr->find("compression_quality");
  662. if( it != cArr->end() ){
  663. tPtr->compression_quality = atoi( it->second.c_str() );
  664. };
  665. it = cArr->find("show_ts");
  666. if( it != cArr->end() ){
  667. tPtr->show_ts = (bool)atoi( it->second.c_str() );
  668. };
  669. it = cArr->find("ts_x");
  670. if( it != cArr->end() ){
  671. tPtr->ts_x = atoi( it->second.c_str() );
  672. };
  673. it = cArr->find("ts_y");
  674. if( it != cArr->end() ){
  675. tPtr->ts_y = atoi( it->second.c_str() );
  676. };
  677. it = cArr->find("ts_font_face");
  678. if( it != cArr->end() ){
  679. tPtr->ts_font_face = atoi( it->second.c_str() );
  680. };
  681. it = cArr->find("ts_font_scale");
  682. if( it != cArr->end() ){
  683. tPtr->ts_font_scale = atof( it->second.c_str() );
  684. };
  685. it = cArr->find("ts_font_thickness");
  686. if( it != cArr->end() ){
  687. tPtr->ts_font_thickness = atoi( it->second.c_str() );
  688. };
  689. it = cArr->find("ts_line_type");
  690. if( it != cArr->end() ){
  691. tPtr->ts_line_type = atoi( it->second.c_str() );
  692. };
  693. it = cArr->find("ts_transperant");
  694. if( it != cArr->end() ){
  695. tPtr->ts_transperant = (bool)atoi( it->second.c_str() );
  696. };
  697. it = cArr->find("show_label");
  698. if( it != cArr->end() ){
  699. tPtr->show_label = (bool)atoi( it->second.c_str() );
  700. };
  701. it = cArr->find("label_x");
  702. if( it != cArr->end() ){
  703. tPtr->label_x = atoi( it->second.c_str() );
  704. };
  705. it = cArr->find("label_y");
  706. if( it != cArr->end() ){
  707. tPtr->label_y = atoi( it->second.c_str() );
  708. };
  709. it = cArr->find("label_font_face");
  710. if( it != cArr->end() ){
  711. tPtr->label_font_face = atoi( it->second.c_str() );
  712. };
  713. it = cArr->find("label_font_scale");
  714. if( it != cArr->end() ){
  715. tPtr->label_font_scale = atof( it->second.c_str() );
  716. };
  717. it = cArr->find("label_font_thickness");
  718. if( it != cArr->end() ){
  719. tPtr->label_font_thickness = atoi( it->second.c_str() );
  720. };
  721. it = cArr->find("label_line_type");
  722. if( it != cArr->end() ){
  723. tPtr->label_line_type = atoi( it->second.c_str() );
  724. };
  725. it = cArr->find("label_transperant");
  726. if( it != cArr->end() ){
  727. tPtr->label_transperant = (bool) atoi( it->second.c_str() );
  728. };
  729. it = cArr->find("show_logo");
  730. if( it != cArr->end() ){
  731. tPtr->show_logo = (bool) atoi( it->second.c_str() );
  732. };
  733. it = cArr->find("logo_w");
  734. if( it != cArr->end() ){
  735. tPtr->logo_w = atoi( it->second.c_str() );
  736. };
  737. it = cArr->find("logo_h");
  738. if( it != cArr->end() ){
  739. tPtr->logo_h = atoi( it->second.c_str() );
  740. };
  741. it = cArr->find("logo_x");
  742. if( it != cArr->end() ){
  743. tPtr->logo_x = atoi( it->second.c_str() );
  744. };
  745. it = cArr->find("logo_y");
  746. if( it != cArr->end() ){
  747. tPtr->logo_y = atoi( it->second.c_str() );
  748. };
  749. it = cArr->find("logo_md5");
  750. if( it != cArr->end() ){
  751. strncpy( tPtr->logo_md5, it->second.c_str(),32);
  752. tPtr->logo_md5[32]=(char)0;
  753. };
  754. };
  755. };
  756. };
  757. deleteResult( result );
  758. };
  759. return tPtr;
  760. };
  761. DBC_MEDIA_STREAM_DATA *sqliteDBC::getMediaStreamData(uint32_t ID){
  762. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  763. if(m_bIsConnected){
  764. char tQBuf[CTEXT];
  765. AAssoc *result;
  766. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  767. #ifdef DEBUG_ALL
  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_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  775. if( tVal != NULL ) {
  776. memset( tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA) );
  777. Assoc::iterator it;
  778. cArr = result->at(0);
  779. tVal->id = ID;
  780. it = cArr->find("source_id");
  781. if( it != cArr->end() ){
  782. tVal->source_id = atoi( it->second.c_str() );
  783. };
  784. it = cArr->find("source_type");
  785. if( it != cArr->end() ){
  786. sprintf( tVal->source_type, it->second.c_str() );
  787. };
  788. it = cArr->find("valid");
  789. if( it != cArr->end() ){
  790. tVal->valid = (bool) atoi( it->second.c_str() );
  791. };
  792. it = cArr->find("token");
  793. if( it != cArr->end() ){
  794. sprintf( tVal->token, it->second.c_str() );
  795. };
  796. it = cArr->find("img_width");
  797. if( it != cArr->end() ){
  798. tVal->width = atoi( it->second.c_str() );
  799. };
  800. it = cArr->find("img_height");
  801. if( it != cArr->end() ){
  802. tVal->height = atoi( it->second.c_str() );
  803. };
  804. it = cArr->find("fps");
  805. if( it != cArr->end() ){
  806. tVal->fps = atoi( it->second.c_str() );
  807. };
  808. it = cArr->find("compression_type");
  809. if( it != cArr->end() ){
  810. sprintf(tVal->c_type, it->second.c_str());
  811. };
  812. it = cArr->find("mrl_proto");
  813. if( it != cArr->end() ){
  814. sprintf( tVal->mrl_proto, it->second.c_str() );
  815. };
  816. it = cArr->find("mrl_port");
  817. if( it != cArr->end() ){
  818. tVal->mrl_port = atoi( it->second.c_str() );
  819. };
  820. it = cArr->find("mrl");
  821. if( it != cArr->end() ){
  822. sprintf( tVal->mrl, it->second.c_str() );
  823. };
  824. };
  825. };
  826. };
  827. deleteResult( result );
  828. };
  829. return tVal;
  830. };
  831. uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
  832. uint32_t RetVal = 0;
  833. if(m_bIsConnected){
  834. char tQBuf[CTEXT];
  835. AAssoc *result;
  836. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
  837. #ifdef DEBUG_ALL
  838. LOG(tQBuf);
  839. #endif
  840. result = m_pDataStorage->Query( tQBuf );
  841. if( result!= NULL ) {
  842. if( result->size() > 0 ) {
  843. Assoc *cArr;
  844. Assoc::iterator it;
  845. cArr = result->at(0);
  846. it = cArr->find("id");
  847. if( it != cArr->end() ){
  848. RetVal = atoi( it->second.c_str() );
  849. };
  850. };
  851. };
  852. deleteResult( result );
  853. };
  854. return RetVal;
  855. };
  856. DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
  857. DBC_MODULE_DATA *tVal = NULL;
  858. if(m_bIsConnected){
  859. char tQBuf[CTEXT];
  860. AAssoc *result;
  861. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
  862. #ifdef DEBUG_ALL
  863. LOG(tQBuf);
  864. #endif
  865. result = m_pDataStorage->Query( tQBuf );
  866. if( result!= NULL ) {
  867. if( result->size() > 0 ) {
  868. Assoc *cArr;
  869. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  870. if( tVal != NULL ) {
  871. Assoc::iterator it;
  872. cArr = result->at(0);
  873. it = cArr->find("id");
  874. if( it != cArr->end() ){
  875. tVal->module_id = atoi( it->second.c_str() );
  876. };
  877. it = cArr->find("aux_id");
  878. if( it != cArr->end() ){
  879. tVal->aux_id = atoi( it->second.c_str() );
  880. };
  881. it = cArr->find("type");
  882. if( it != cArr->end() ){
  883. tVal->type = atoi( it->second.c_str() );
  884. };
  885. it = cArr->find("enabled");
  886. if( it != cArr->end() ){
  887. tVal->enabled = (bool)atoi( it->second.c_str() );
  888. };
  889. };
  890. };
  891. };
  892. deleteResult( result );
  893. };
  894. return tVal;
  895. };
  896. DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
  897. DBC_NODE_DATA * tVal = NULL;
  898. if(m_bIsConnected){
  899. char tQBuf[CTEXT];
  900. AAssoc *result;
  901. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
  902. #ifdef DEBUG_EXTRA
  903. LOG(tQBuf);
  904. #endif
  905. result = m_pDataStorage->Query( tQBuf );
  906. if( result!= NULL ) {
  907. if( result->size() > 0 ) {
  908. Assoc *cArr;
  909. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  910. if( tVal != NULL ) {
  911. memset( tVal, 0, sizeof(DBC_NODE_DATA));
  912. Assoc::iterator it;
  913. tVal->node_id = ID;
  914. cArr = result->at(0);
  915. it = cArr->find("type");
  916. if( it != cArr->end() ){
  917. tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
  918. };
  919. it = cArr->find("aux_id");
  920. if( it != cArr->end() ){
  921. tVal->aux_id = atoi( it->second.c_str() );
  922. };
  923. it = cArr->find("aux_parameter");
  924. if( it != cArr->end() ){
  925. tVal->aux_parameter = atoi( it->second.c_str() );
  926. };
  927. it = cArr->find("online");
  928. if( it != cArr->end() ){
  929. tVal->online = (bool)atoi( it->second.c_str() );
  930. };
  931. it = cArr->find("enable");
  932. if( it != cArr->end() ){
  933. tVal->enabled = (bool)atoi( it->second.c_str() );
  934. };
  935. it = cArr->find("name");
  936. if( it != cArr->end() ){
  937. sprintf( tVal->name, it->second.c_str() );
  938. };
  939. };
  940. };
  941. };
  942. deleteResult( result );
  943. };
  944. return tVal;
  945. };
  946. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  947. DBC_NET_IF_DATA * tVal = NULL;
  948. if(m_bIsConnected){
  949. char tQBuf[CTEXT];
  950. AAssoc *result;
  951. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  952. #ifdef DEBUG_EXTRA
  953. LOG(tQBuf);
  954. #endif
  955. result = m_pDataStorage->Query( tQBuf );
  956. if( result!= NULL ) {
  957. if( result->size() > 0 ) {
  958. Assoc *cArr;
  959. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  960. if( tVal != NULL ) {
  961. Assoc::iterator it;
  962. tVal->id = nif_id;
  963. cArr = result->at(0);
  964. it = cArr->find("type");
  965. if( it != cArr->end() ){
  966. tVal->type = atoi(it->second.c_str());
  967. };
  968. it = cArr->find("mode");
  969. if( it != cArr->end() ){
  970. tVal->mode = atoi(it->second.c_str());
  971. };
  972. it = cArr->find("device_name");
  973. if( it != cArr->end() ){
  974. strncpy( tVal->device_name, it->second.c_str(),11);
  975. tVal->device_name[10]=(char)0;
  976. };
  977. it = cArr->find("mac");
  978. if( it != cArr->end() ){
  979. strncpy( tVal->mac, it->second.c_str(), 33 );
  980. tVal->mac[32]=(char)0;
  981. };
  982. it = cArr->find("aux_id");
  983. if( it != cArr->end() ){
  984. tVal->aux_id = atoi(it->second.c_str());
  985. };
  986. };
  987. };
  988. };
  989. deleteResult( result );
  990. };
  991. return tVal;
  992. };
  993. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  994. uint32_t tVal = 0;
  995. if(m_bIsConnected){
  996. char tQBuf[CTEXT];
  997. AAssoc *result;
  998. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  999. #ifdef DEBUG_ALL
  1000. LOG(tQBuf);
  1001. #endif
  1002. result = m_pDataStorage->Query( tQBuf );
  1003. if( result!= NULL ) {
  1004. if( result->size() > 0 ) {
  1005. Assoc *cArr;
  1006. Assoc::iterator it;
  1007. cArr = result->at(0);
  1008. it = cArr->find("id");
  1009. if( it != cArr->end() ){
  1010. tVal = atoi( it->second.c_str() );
  1011. };
  1012. };
  1013. };
  1014. deleteResult( result );
  1015. };
  1016. return tVal;
  1017. };
  1018. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  1019. uint32_t tVal = 0;
  1020. if(m_bIsConnected){
  1021. char tQBuf[CTEXT];
  1022. AAssoc *result;
  1023. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  1024. #ifdef DEBUG_ALL
  1025. LOG(tQBuf);
  1026. #endif
  1027. result = m_pDataStorage->Query( tQBuf );
  1028. if( result!= NULL ) {
  1029. if( result->size() > 0 ) {
  1030. Assoc *cArr;
  1031. Assoc::iterator it;
  1032. cArr = result->at(0);
  1033. it = cArr->find("id");
  1034. if( it != cArr->end() ){
  1035. tVal = atoi( it->second.c_str() );
  1036. };
  1037. };
  1038. };
  1039. deleteResult( result );
  1040. };
  1041. return tVal;
  1042. };
  1043. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  1044. uint32_t tVal = 0;
  1045. if(m_bIsConnected){
  1046. char tQBuf[CTEXT];
  1047. AAssoc *result;
  1048. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  1049. #ifdef DEBUG_ALL
  1050. LOG(tQBuf);
  1051. #endif
  1052. result = m_pDataStorage->Query( tQBuf );
  1053. if( result!= NULL ) {
  1054. if( result->size() > 0 ) {
  1055. Assoc *cArr;
  1056. Assoc::iterator it;
  1057. cArr = result->at(0);
  1058. it = cArr->find("id");
  1059. if( it != cArr->end() ){
  1060. tVal = atoi( it->second.c_str() );
  1061. };
  1062. };
  1063. };
  1064. deleteResult( result );
  1065. };
  1066. return tVal;
  1067. };
  1068. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  1069. DBC_RESOURCE_DATA *tVal = NULL;
  1070. if( m_bIsConnected ){
  1071. char tQBuf[CTEXT];
  1072. AAssoc *result;
  1073. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  1074. #ifdef DEBUG_ALL
  1075. LOG(tQBuf);
  1076. #endif
  1077. result = m_pDataStorage->Query( tQBuf );
  1078. if( result!= NULL ) {
  1079. if( result->size() > 0 ) {
  1080. Assoc *cArr;
  1081. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  1082. if( tVal != NULL ) {
  1083. Assoc::iterator it;
  1084. cArr = result->at(0);
  1085. tVal->id = ResourceID;
  1086. it = cArr->find("node_id");
  1087. if( it != cArr->end() ){
  1088. tVal->node_id = atoi( it->second.c_str() );
  1089. };
  1090. it = cArr->find("type");
  1091. if( it != cArr->end() ){
  1092. tVal->type = atoi( it->second.c_str() );
  1093. };
  1094. it = cArr->find("aux_id");
  1095. if( it != cArr->end() ){
  1096. tVal->aux_id = atoi( it->second.c_str() );
  1097. };
  1098. it = cArr->find("device");
  1099. if( it != cArr->end() ){
  1100. strncpy( tVal->device, it->second.c_str(), 64 );
  1101. tVal->device[63] = (char)0;
  1102. };
  1103. };
  1104. };
  1105. };
  1106. deleteResult( result );
  1107. };
  1108. return tVal;
  1109. };
  1110. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  1111. uint32_t RetVal = 0;
  1112. if(m_bIsConnected){
  1113. char tQBuf[CTEXT];
  1114. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  1115. #ifdef DEBUG_ALL
  1116. LOG(tQBuf);
  1117. #endif
  1118. AAssoc *result;
  1119. result = m_pDataStorage->Query( tQBuf );
  1120. if( result!= NULL ) {
  1121. if( result->size() > 0 ) {
  1122. Assoc * cArr;
  1123. Assoc::iterator it;
  1124. cArr = result->at(0);
  1125. it = cArr->find("id");
  1126. if( it != cArr->end() ){
  1127. RetVal = atoi( it->second.c_str() );
  1128. };
  1129. };
  1130. };
  1131. deleteResult( result );
  1132. };
  1133. return RetVal;
  1134. };
  1135. uint32_t sqliteDBC::getServerID( CSTR identity ){
  1136. uint32_t RetVal = 0;
  1137. if(m_bIsConnected){
  1138. char tQBuf[CTEXT];
  1139. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  1140. #ifdef DEBUG_ALL
  1141. LOG(tQBuf);
  1142. #endif
  1143. AAssoc *result;
  1144. result = m_pDataStorage->Query( tQBuf );
  1145. if( result!= NULL ) {
  1146. if( result->size() > 0 ) {
  1147. Assoc * cArr;
  1148. Assoc::iterator it;
  1149. cArr = result->at(0);
  1150. it = cArr->find("id");
  1151. if( it != cArr->end() ){
  1152. RetVal = atoi( it->second.c_str() );
  1153. };
  1154. };
  1155. };
  1156. deleteResult( result );
  1157. };
  1158. return RetVal;
  1159. };
  1160. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  1161. uint32_t RetVal = 0;
  1162. if(m_bIsConnected){
  1163. char tQBuf[CTEXT];
  1164. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  1165. #ifdef DEBUG_ALL
  1166. LOG(tQBuf);
  1167. #endif
  1168. AAssoc *result;
  1169. result = m_pDataStorage->Query( tQBuf );
  1170. if( result!= NULL ) {
  1171. if( result->size() > 0 ) {
  1172. Assoc * cArr;
  1173. Assoc::iterator it;
  1174. cArr = result->at(0);
  1175. it = cArr->find("id");
  1176. if( it != cArr->end() ){
  1177. RetVal = atoi( it->second.c_str() );
  1178. };
  1179. };
  1180. };
  1181. deleteResult( result );
  1182. };
  1183. return RetVal;
  1184. }
  1185. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  1186. uint32_t RetVal = 0;
  1187. if(m_bIsConnected){
  1188. char tQBuf[CTEXT];
  1189. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  1190. #ifdef DEBUG_ALL
  1191. LOG(tQBuf);
  1192. #endif
  1193. AAssoc *result;
  1194. result = m_pDataStorage->Query( tQBuf );
  1195. if( result!= NULL ) {
  1196. if( result->size() > 0 ) {
  1197. Assoc * cArr;
  1198. Assoc::iterator it;
  1199. cArr = result->at(0);
  1200. it = cArr->find("id");
  1201. if( it != cArr->end() ){
  1202. RetVal = atoi( it->second.c_str() );
  1203. };
  1204. };
  1205. };
  1206. deleteResult( result );
  1207. };
  1208. return RetVal;
  1209. };
  1210. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  1211. uint32_t tVal = 0;
  1212. if(m_bIsConnected){
  1213. char tQBuf[CTEXT];
  1214. AAssoc *result;
  1215. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  1216. #ifdef DEBUG_ALL
  1217. LOG(tQBuf);
  1218. #endif
  1219. result = m_pDataStorage->Query( tQBuf );
  1220. if( result!= NULL ) {
  1221. if( result->size() > 0 ) {
  1222. Assoc *cArr;
  1223. Assoc::iterator it;
  1224. cArr = result->at(0);
  1225. it = cArr->find("id");
  1226. if( it != cArr->end() ){
  1227. tVal = atoi( it->second.c_str() );
  1228. };
  1229. };
  1230. };
  1231. deleteResult( result );
  1232. };
  1233. return tVal;
  1234. };
  1235. DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
  1236. DBC_INT_LIST * tVal = NULL;
  1237. if(m_bIsConnected){
  1238. char tQBuf[CTEXT];
  1239. AAssoc *result;
  1240. switch( listMode ){
  1241. case LIST_VALID_RECORDS:
  1242. sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1 AND `nd`.`online`=1" );
  1243. break;
  1244. case LIST_INVALID_RECORDS:
  1245. sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1 AND `nd`.`online`=0" );
  1246. break;
  1247. default:
  1248. sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1" );
  1249. };
  1250. #ifdef DEBUG_ALL
  1251. LOG(tQBuf);
  1252. #endif
  1253. result = m_pDataStorage->Query( tQBuf );
  1254. if( result!= NULL ) {
  1255. if( result->size() > 0 ) {
  1256. Assoc *cArr;
  1257. tVal = new DBC_INT_LIST();
  1258. if( tVal != NULL ) {
  1259. Assoc::iterator it;
  1260. for(int i=0;i<result->size();i++) {
  1261. cArr = result->at(i);
  1262. it = cArr->find("id");
  1263. if( it != cArr->end() ){
  1264. tVal->push_back( atoi( it->second.c_str() ));
  1265. };
  1266. };
  1267. };
  1268. };
  1269. };
  1270. deleteResult( result );
  1271. };
  1272. return tVal;
  1273. };
  1274. DBC_INT_LIST *sqliteDBC::listModules(){
  1275. DBC_INT_LIST * tVal = NULL;
  1276. if(m_bIsConnected){
  1277. AAssoc *result;
  1278. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  1279. if( result!= NULL ) {
  1280. if( result->size() > 0 ) {
  1281. Assoc *cArr;
  1282. tVal = new DBC_INT_LIST();
  1283. if( tVal != NULL ) {
  1284. Assoc::iterator it;
  1285. for(int i=0;i<result->size();i++) {
  1286. cArr = result->at(i);
  1287. it = cArr->find("id");
  1288. if( it != cArr->end() ){
  1289. tVal->push_back( atoi( it->second.c_str() ));
  1290. };
  1291. };
  1292. };
  1293. };
  1294. };
  1295. deleteResult( result );
  1296. };
  1297. return tVal;
  1298. };
  1299. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  1300. DBC_INT_LIST * tVal = NULL;
  1301. char tParam[CTEXT];
  1302. //memset( tParam, 0, CTEXT );
  1303. sprintf(tParam,"0");
  1304. if( old_ids != NULL ) {
  1305. char tBuffer[CTEXT];
  1306. for(int i = 0; i< old_ids->size(); i++ ) {
  1307. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  1308. strncpy( tParam, tBuffer, CTEXT );
  1309. };
  1310. };
  1311. if(m_bIsConnected){
  1312. char tQBuf[CTEXT];
  1313. AAssoc *result;
  1314. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  1315. #ifdef DEBUG_ALL
  1316. LOG(tQBuf);
  1317. #endif
  1318. result = m_pDataStorage->Query( tQBuf );
  1319. if( result!= NULL ) {
  1320. if( result->size() > 0 ) {
  1321. Assoc *cArr;
  1322. tVal = new DBC_INT_LIST();
  1323. if( tVal != NULL ) {
  1324. Assoc::iterator it;
  1325. for(int i=0;i<result->size();i++) {
  1326. cArr = result->at(i);
  1327. it = cArr->find("id");
  1328. if( it != cArr->end() ){
  1329. tVal->push_back( atoi( it->second.c_str() ));
  1330. };
  1331. };
  1332. };
  1333. };
  1334. };
  1335. deleteResult( result );
  1336. };
  1337. return tVal;
  1338. };
  1339. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  1340. DBC_INT_LIST * tVal = NULL;
  1341. char tParam[CTEXT];
  1342. //memset( tParam, 0, CTEXT );
  1343. sprintf(tParam,"0");
  1344. if( old_ids != NULL ) {
  1345. char tBuffer[CTEXT];
  1346. for(int i = 0; i< old_ids->size(); i++ ) {
  1347. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  1348. strncpy( tParam, tBuffer, CTEXT );
  1349. };
  1350. };
  1351. if( m_bIsConnected ){
  1352. char tQBuf[CTEXT];
  1353. AAssoc *result;
  1354. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `id` NOT IN(%s)", tParam );
  1355. #ifdef DEBUG_EXTRA
  1356. LOG(tQBuf);
  1357. #endif
  1358. result = m_pDataStorage->Query( tQBuf );
  1359. if( result!= NULL ) {
  1360. if( result->size() > 0 ) {
  1361. Assoc *cArr;
  1362. tVal = new DBC_INT_LIST();
  1363. if( tVal != NULL ) {
  1364. Assoc::iterator it;
  1365. for(int i=0;i<result->size();i++) {
  1366. cArr = result->at(i);
  1367. it = cArr->find("id");
  1368. if( it != cArr->end() ){
  1369. tVal->push_back( atoi( it->second.c_str() ));
  1370. };
  1371. };
  1372. };
  1373. };
  1374. };
  1375. deleteResult( result );
  1376. };
  1377. return tVal;
  1378. };
  1379. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  1380. DBC_INT_LIST * tVal = NULL;
  1381. if( m_bIsConnected ){
  1382. char tQBuf[1024];
  1383. AAssoc *result;
  1384. if( all ) {
  1385. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  1386. } else {
  1387. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0" );
  1388. };
  1389. #ifdef DEBUG_EXTRA
  1390. LOG(tQBuf);
  1391. #endif
  1392. result = m_pDataStorage->Query( tQBuf );
  1393. if( result!= NULL ) {
  1394. if( result->size() > 0 ) {
  1395. Assoc *cArr;
  1396. tVal = new DBC_INT_LIST();
  1397. if( tVal != NULL ) {
  1398. Assoc::iterator it;
  1399. for(int i=0; i <result->size();i++) {
  1400. cArr = result->at(i);
  1401. it = cArr->find("id");
  1402. if( it != cArr->end() ){
  1403. tVal->push_back( atoi( it->second.c_str() ));
  1404. };
  1405. };
  1406. };
  1407. };
  1408. };
  1409. deleteResult( result );
  1410. };
  1411. return tVal;
  1412. };
  1413. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  1414. DBC_INT_LIST * tVal = NULL;
  1415. if(m_bIsConnected){
  1416. char tQBuf[CTEXT];
  1417. AAssoc *result;
  1418. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  1419. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  1420. #ifdef DEBUG
  1421. LOG(tQBuf);
  1422. #endif
  1423. result = m_pDataStorage->Query( tQBuf );
  1424. if( result!= NULL ) {
  1425. if( result->size() > 0 ) {
  1426. Assoc *cArr;
  1427. tVal = new DBC_INT_LIST();
  1428. if( tVal != NULL ) {
  1429. Assoc::iterator it;
  1430. for(int i=0;i<result->size();i++) {
  1431. cArr = result->at(i);
  1432. it = cArr->find("id");
  1433. if( it != cArr->end() ){
  1434. tVal->push_back( atoi( it->second.c_str() ));
  1435. };
  1436. };
  1437. };
  1438. };
  1439. };
  1440. deleteResult( result );
  1441. };
  1442. return tVal;
  1443. };
  1444. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  1445. if( m_bIsConnected ){
  1446. m_nSiteID = SiteID;
  1447. char tQBuf[MAX_PATH_LEN];
  1448. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  1449. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  1450. #ifdef DEBUG_ALL
  1451. LOG(tQBuf);
  1452. #endif
  1453. m_pDataStorage->SilentQuery( tQBuf );
  1454. m_nServerID = getServerID( identity );
  1455. *ServID = m_nServerID;
  1456. if( m_nServerID ) {
  1457. //if ( bRecreateMemStructures )
  1458. initMemDB( m_nServerID );
  1459. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  1460. #ifdef DEBUG_ALL
  1461. LOG(tQBuf);
  1462. #endif
  1463. m_pDataMemory->SilentQuery( tQBuf );
  1464. 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 );
  1465. #ifdef DEBUG_ALL
  1466. LOG(tQBuf);
  1467. #endif
  1468. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  1469. if( m_nServerNodeID== 0 ){
  1470. m_nServerNodeID = getServerNode( m_nServerID );
  1471. };
  1472. *ServNode = m_nServerNodeID;
  1473. // Clenup Nodes and services ...
  1474. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  1475. #ifdef DEBUG_ALL
  1476. LOG(tQBuf);
  1477. #endif
  1478. m_pDataStorage->SilentQuery( tQBuf );
  1479. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  1480. #ifdef DEBUG_ALL
  1481. LOG(tQBuf);
  1482. #endif
  1483. m_pDataStorage->SilentQuery( tQBuf );
  1484. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  1485. #ifdef DEBUG_ALL
  1486. LOG(tQBuf);
  1487. #endif
  1488. m_pDataStorage->SilentQuery( tQBuf );
  1489. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  1490. #ifdef DEBUG_ALL
  1491. // LOG(tQBuf);
  1492. #endif
  1493. // m_pDataStorage->SilentQuery( tQBuf );
  1494. //cleanResources( tServerNodeID );
  1495. setNodeOnline( m_nServerNodeID );
  1496. };
  1497. };
  1498. };
  1499. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  1500. if(m_bIsConnected){
  1501. char tQBuf[CTEXT];
  1502. if( NodeID ) {
  1503. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  1504. #ifdef DEBUG_ALL
  1505. LOG(tQBuf);
  1506. #endif
  1507. m_pDataStorage->SilentQuery( tQBuf );
  1508. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  1509. #ifdef DEBUG_ALL
  1510. LOG(tQBuf);
  1511. #endif
  1512. m_pDataStorage->SilentQuery( tQBuf );
  1513. return true;
  1514. };
  1515. };
  1516. return false;
  1517. };
  1518. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  1519. if(m_bIsConnected){
  1520. char tQBuf[CTEXT];
  1521. if( NodeID ) {
  1522. sprintf(tQBuf,"UPDATE nodes SET `online`=%d WHERE `id`=%d", status, NodeID );
  1523. #ifdef DEBUG_ALL
  1524. LOG(tQBuf);
  1525. #endif
  1526. m_pDataStorage->SilentQuery( tQBuf );
  1527. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  1528. #ifdef DEBUG_ALL
  1529. LOG(tQBuf);
  1530. #endif
  1531. m_pDataStorage->SilentQuery( tQBuf );
  1532. return true;
  1533. };
  1534. };
  1535. return false;
  1536. };
  1537. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  1538. if( m_bIsConnected ) {
  1539. char tQBuf[MAX_PATH_LEN];
  1540. if ( status ) {
  1541. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  1542. } else {
  1543. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  1544. };
  1545. #ifdef DEBUG
  1546. LOG(tQBuf);
  1547. #endif
  1548. m_pDataStorage->SilentQuery( tQBuf );
  1549. return true;
  1550. };
  1551. return false;
  1552. };
  1553. void sqliteDBC::unregisterServer( void ){
  1554. setNodeOffline( m_nServerNodeID );
  1555. };
  1556. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  1557. if( m_bIsConnected ) {
  1558. char tQBuf[MAX_PATH_LEN];
  1559. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  1560. ptr->OS_INFO,
  1561. ptr->HW_INFO,
  1562. m_nSiteID
  1563. );
  1564. #ifdef DEBUG_ALL
  1565. LOG(tQBuf);
  1566. #endif
  1567. m_pDataStorage->SilentQuery( tQBuf );
  1568. return true;
  1569. };
  1570. return false;
  1571. };
  1572. DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
  1573. DBC_CAM_DATA *tVal = NULL;
  1574. if(m_bIsConnected){
  1575. char tQBuf[CTEXT];
  1576. AAssoc *result;
  1577. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  1578. #ifdef DEBUG_ALL
  1579. LOG(tQBuf);
  1580. #endif
  1581. result = m_pDataStorage->Query( tQBuf );
  1582. if( result!= NULL ) {
  1583. if( result->size() > 0 ) {
  1584. Assoc *cArr;
  1585. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  1586. if( tVal != NULL ) {
  1587. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  1588. tVal->id = ID;
  1589. Assoc::iterator it;
  1590. cArr = result->at(0);
  1591. it = cArr->find("type");
  1592. if( it != cArr->end() ){
  1593. tVal->type = atoi( it->second.c_str() );
  1594. };
  1595. it = cArr->find("online");
  1596. if( it != cArr->end() ){
  1597. tVal->online = (bool) atoi( it->second.c_str() );
  1598. };
  1599. it = cArr->find("stream_id");
  1600. if( it != cArr->end() ){
  1601. tVal->stream_id = atoi( it->second.c_str() );
  1602. };
  1603. it = cArr->find("hardware");
  1604. if( it != cArr->end() ){
  1605. strncpy( tVal->hardware, it->second.c_str(), 32 );
  1606. };
  1607. it = cArr->find("name");
  1608. if( it != cArr->end() ){
  1609. strncpy( tVal->name, it->second.c_str(), 64 );
  1610. };
  1611. it = cArr->find("status");
  1612. if( it != cArr->end() ){
  1613. tVal->status = atoi( it->second.c_str() );
  1614. };
  1615. it = cArr->find("session");
  1616. if( it != cArr->end() ){
  1617. strncpy( tVal->session, it->second.c_str(), 32 );
  1618. };
  1619. it = cArr->find("protocol");
  1620. if( it != cArr->end() ){
  1621. tVal->protocol = atoi( it->second.c_str() );
  1622. };
  1623. it = cArr->find("protocol_aux");
  1624. if( it != cArr->end() ){
  1625. tVal->protocol_aux = atoi( it->second.c_str() );
  1626. };
  1627. it = cArr->find("uart_id");
  1628. if( it != cArr->end() ){
  1629. tVal->uart_id = atoi( it->second.c_str() );
  1630. };
  1631. it = cArr->find("x");
  1632. if( it != cArr->end() ){
  1633. tVal->x = atof( it->second.c_str() );
  1634. };
  1635. it = cArr->find("y");
  1636. if( it != cArr->end() ){
  1637. tVal->y = atof( it->second.c_str() );
  1638. };
  1639. it = cArr->find("z");
  1640. if( it != cArr->end() ){
  1641. tVal->z = atof( it->second.c_str() );
  1642. };
  1643. it = cArr->find("x_rot");
  1644. if( it != cArr->end() ){
  1645. tVal->x_rot = atof( it->second.c_str() );
  1646. };
  1647. it = cArr->find("y_rot");
  1648. if( it != cArr->end() ){
  1649. tVal->y_rot = atof( it->second.c_str() );
  1650. };
  1651. it = cArr->find("z_rot");
  1652. if( it != cArr->end() ){
  1653. tVal->z_rot = atof( it->second.c_str() );
  1654. };
  1655. it = cArr->find("imager_type");
  1656. if( it != cArr->end() ){
  1657. tVal->imager_type = atoi( it->second.c_str() );
  1658. };
  1659. it = cArr->find("lens_f");
  1660. if( it != cArr->end() ){
  1661. tVal->lens_f = atoi( it->second.c_str() );
  1662. };
  1663. it = cArr->find("ptz_speed_step");
  1664. if( it != cArr->end() ){
  1665. tVal->ptz_speed_step = atof( it->second.c_str() );
  1666. };
  1667. it = cArr->find("pan_model");
  1668. if( it != cArr->end() ){
  1669. tVal->pan_model = atoi( it->second.c_str() );
  1670. };
  1671. it = cArr->find("pan_angles");
  1672. if( it != cArr->end() ){
  1673. tVal->pan_angles = atoi( it->second.c_str() );
  1674. };
  1675. it = cArr->find("tilt_model");
  1676. if( it != cArr->end() ){
  1677. tVal->tilt_model = atoi( it->second.c_str() );
  1678. };
  1679. it = cArr->find("tilt_angles");
  1680. if( it != cArr->end() ){
  1681. tVal->tilt_angles = atoi( it->second.c_str() );
  1682. };
  1683. it = cArr->find("zoom_model");
  1684. if( it != cArr->end() ){
  1685. tVal->zoom_model = atoi( it->second.c_str() );
  1686. };
  1687. it = cArr->find("zoom");
  1688. if( it != cArr->end() ){
  1689. tVal->zoom = atof( it->second.c_str() );
  1690. };
  1691. //it = cArr->find("max_range");
  1692. //if( it != cArr->end() ){
  1693. // tVal->max_range = atof( it->second.c_str() );
  1694. //};
  1695. it = cArr->find("access_login");
  1696. if( it != cArr->end() ){
  1697. strncpy( tVal->username, it->second.c_str(), 16 );
  1698. };
  1699. it = cArr->find("access_pass");
  1700. if( it != cArr->end() ){
  1701. strncpy( tVal->password, it->second.c_str(), 16 );
  1702. };
  1703. it = cArr->find("uuid");
  1704. if( it != cArr->end() ){
  1705. strncpy( tVal->uuid, it->second.c_str(), 64 );
  1706. };
  1707. it = cArr->find("mac");
  1708. if( it != cArr->end() ){
  1709. strncpy( tVal->mac, it->second.c_str(), 16 );
  1710. };
  1711. it = cArr->find("ip");
  1712. if( it != cArr->end() ){
  1713. strncpy( tVal->ip, it->second.c_str(), 16 );
  1714. };
  1715. it = cArr->find("port");
  1716. if( it != cArr->end() ){
  1717. tVal->port = atoi( it->second.c_str() );
  1718. };
  1719. it = cArr->find("onvif_access");
  1720. if( it != cArr->end() ){
  1721. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  1722. };
  1723. it = cArr->find("onvif_ctrl");
  1724. if( it != cArr->end() ){
  1725. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  1726. };
  1727. it = cArr->find("ptz_token");
  1728. if( it != cArr->end() ){
  1729. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  1730. };
  1731. it = cArr->find("trigger_mode");
  1732. if( it != cArr->end() ){
  1733. tVal->triggering_mode = atoi( it->second.c_str() );
  1734. };
  1735. it = cArr->find("auth_mode");
  1736. if( it != cArr->end() ){
  1737. tVal->auth_mode = atoi( it->second.c_str() );
  1738. };
  1739. };
  1740. };
  1741. };
  1742. deleteResult( result );
  1743. };
  1744. return tVal;
  1745. };
  1746. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  1747. uint32_t RetVal = 0;
  1748. if(m_bIsConnected){
  1749. char tQBuf[CTEXT];
  1750. AAssoc *result;
  1751. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  1752. #ifdef DEBUG_ALL
  1753. LOG(tQBuf);
  1754. #endif
  1755. result = m_pDataStorage->Query( tQBuf );
  1756. if( result!= NULL ) {
  1757. if( result->size() > 0 ) {
  1758. Assoc *cArr;
  1759. Assoc::iterator it;
  1760. cArr = result->at(0);
  1761. it = cArr->find("id");
  1762. if( it != cArr->end() ){
  1763. RetVal = atoi( it->second.c_str() );
  1764. };
  1765. };
  1766. };
  1767. deleteResult( result );
  1768. };
  1769. return RetVal;
  1770. };
  1771. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  1772. switch ( pid ) {
  1773. case 0: return strdup( m_pDbFile );
  1774. };
  1775. return NULL;
  1776. };
  1777. #undef LOG
  1778. #undef LOG_ERROR