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.

2233 lines
81 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
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::updateNodePushedCMD( uint32_t CmdID, uint8_t status ){
  219. if ( m_bIsConnected ) {
  220. char QS[MAX_PATH];
  221. sprintf( QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID );
  222. return m_pDataMemory->SilentQuery( QS );
  223. };
  224. return false;
  225. };
  226. bool sqliteDBC::cleanNodePushedCMD( void ){
  227. if ( m_bIsConnected ) {
  228. return m_pDataMemory->SilentQuery( "DELETE FROM `nodes_qcq` WHERE `status` > 200" );
  229. };
  230. return false;
  231. };
  232. bool sqliteDBC::updateServerUtilisation( DBC_SYS_LOAD_INFO *ptr ){
  233. if( m_bIsConnected ) {
  234. char tQBuf[MAX_PATH_LEN];
  235. 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",
  236. ptr->CPU_CORES,
  237. ptr->cpu1,
  238. ptr->cpu5,
  239. ptr->cpu15,
  240. ptr->mem,
  241. ptr->disk,
  242. ptr->temperature,
  243. ptr->T_DISK,
  244. ptr->C_DISK,
  245. ptr->T_MEM,
  246. ptr->C_MEM,
  247. m_nServerID
  248. );
  249. #ifdef DEBUG_ALL
  250. LOG(tQBuf);
  251. #endif
  252. m_pDataMemory->SilentQuery( tQBuf );
  253. return true;
  254. };
  255. return false;
  256. };
  257. /********************************************************************
  258. * *
  259. * Slow DB Functions *
  260. * *
  261. ********************************************************************/
  262. bool sqliteDBC::addDeviceDraw( uint32_t DBID, uint8_t type, uint8_t lrf, void *th_data, uint32_t th_size ){
  263. char tQBuf[1024];
  264. if ( type == 0 ) {
  265. CSTR uid = getUniqID_MD5();
  266. if( uid == NULL ) return false;
  267. sprintf( tQBuf, "data/device_draws/%s.raw", uid );
  268. FILE *fd = fopen( tQBuf, "wb+" );
  269. if( fd!= NULL ) {
  270. uint32_t sisi = th_size >> 2;
  271. fwrite( &sisi, 4, 1, fd );
  272. fwrite( th_data, 4, sisi, fd );
  273. fclose( fd );
  274. sprintf( tQBuf, "INSERT INTO `lrf_draw`(`node_id`,`lrf_id`,`type`,`ts`,`data`) VALUES(%d,%d,%d,STRFTIME('%%s'),'%s')",
  275. DBID, lrf, type, uid );
  276. free ( uid );
  277. #ifdef DEBUG
  278. LOG(tQBuf);
  279. #endif
  280. m_pDataStorage->SilentQuery( tQBuf );
  281. return true;
  282. };
  283. free( uid );
  284. } else {
  285. LOG("No RAW DATA!");
  286. };
  287. return false;
  288. };
  289. uint32_t sqliteDBC::addEvent( DBC_EVT_RECORD *ptr ){
  290. LOG("EVENT");
  291. return 0;
  292. };
  293. bool sqliteDBC::addIPCam( OBSERVER_RESULT *tPtr ){
  294. if(m_bIsConnected) {
  295. char tQBuf[1024];
  296. char tIpBuf[32];
  297. char tProto[32];
  298. char tPath[CTEXT];
  299. char *tPos;
  300. char *tPos2;
  301. memcpy( tProto, tPtr->XAddrs, 5 );
  302. tProto[5] = (char)0;
  303. if( tProto[4] == ':') tProto[4] = (char)0;
  304. memset( tPath, 0, CTEXT );
  305. tPos = strstr(tPtr->XAddrs,"//");
  306. if( tPos != NULL ) {
  307. tPos2 = strstr( tPos + 2,"/");
  308. if( tPos2 != NULL ) {
  309. int len,len2,len3;
  310. len = tPos2 - tPos - 2;
  311. strncpy( tIpBuf, tPos + 2, len);
  312. tIpBuf[len] = (char)0;
  313. strncpy( tPath, tPos + 2 + len, CTEXT );
  314. } else {
  315. LOG_ERROR("Pos 2 - err");
  316. tIpBuf[0] = (char)0;
  317. };
  318. } else {
  319. LOG_ERROR("Pos 0 - err");
  320. tIpBuf[0] = (char)0;
  321. };
  322. 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'",
  323. tPtr->RefID,
  324. //tPtr->XAddrs,
  325. tPath,
  326. tIpBuf,
  327. tPtr->MAC,
  328. tPtr->Hardware,
  329. tPtr->Name,
  330. tProto,
  331. //tPtr->XAddrs,
  332. tIpBuf
  333. );
  334. #ifdef DEBUG_ALL
  335. LOG(tQBuf);
  336. #endif
  337. m_pDataStorage->SilentQuery( tQBuf );
  338. return true;
  339. };
  340. return false;
  341. };
  342. uint32_t sqliteDBC::addLocalCam( CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den ){
  343. uint32_t RetVal = 0;
  344. if( m_bIsConnected ) {
  345. char tQBuf[1024];
  346. char tM5[33];
  347. CSTR tMD5 = MD5String( path );
  348. if( tMD5 != NULL ) {
  349. memcpy( tM5, tMD5, 33 );
  350. tM5[32]=(char)0;
  351. free( tMD5 );
  352. 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",
  353. tM5,
  354. hardware,
  355. name,
  356. path
  357. );
  358. #ifdef DEBUG_ALL
  359. LOG(tQBuf);
  360. #endif
  361. uint32_t camID = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  362. if ( camID == 0 ) {
  363. camID = getCamId( tM5 );
  364. };
  365. 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",
  366. camID, name, tM5, width, height, path, fps, width, height, fps );
  367. #ifdef DEBUG_EXTRA
  368. LOG(tQBuf);
  369. #endif
  370. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  371. if( RetVal == 0 ){
  372. RetVal = getMediaStreamID( camID, tM5 );
  373. };
  374. };
  375. };
  376. return RetVal;
  377. };
  378. uint32_t sqliteDBC::addLocalNetwork( uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast ){
  379. uint32_t RetVal = 0;
  380. if( m_bIsConnected ) {
  381. char tQBuf[MAX_PATH_LEN];
  382. 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'",
  383. NIF_ID,
  384. net_type,
  385. address,
  386. gateway,
  387. mask,
  388. broadcast,
  389. address,
  390. gateway,
  391. mask,
  392. broadcast
  393. );
  394. #ifdef DEBUG_ALL
  395. LOG(tQBuf);
  396. #endif
  397. RetVal = m_pDataStorage->QueryID( tQBuf );
  398. // On Update IF data no need to return ID
  399. //if( RetVal == 0 ) {
  400. // RetVal = getNetworkInterfaceID( tKey );
  401. //};
  402. };
  403. return RetVal;
  404. };
  405. uint32_t sqliteDBC::addLocalNetworkInfrastructure( uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME ){
  406. uint32_t RetVal = 0;
  407. if( m_bIsConnected ) {
  408. char tQBuf[MAX_PATH_LEN];
  409. bool ena_routing = true;
  410. if( OUTBOUND_NIF == 0 ){
  411. ena_routing = false;
  412. };
  413. if( ena_dhcp ){
  414. if ( BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL ) {
  415. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  416. return 0;
  417. };
  418. 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'",
  419. NIF_ID,
  420. ena_routing,
  421. OUTBOUND_NIF,
  422. BRANG,
  423. ERANG,
  424. MASK,
  425. LEASE_TIME,
  426. ena_routing,
  427. OUTBOUND_NIF,
  428. BRANG,
  429. ERANG,
  430. MASK,
  431. LEASE_TIME
  432. );
  433. } else {
  434. if( ena_routing ){
  435. 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",
  436. NIF_ID,
  437. OUTBOUND_NIF,
  438. OUTBOUND_NIF
  439. );
  440. } else {
  441. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  442. return 0;
  443. };
  444. };
  445. #ifdef DEBUG_ALL
  446. LOG(tQBuf);
  447. #endif
  448. RetVal = m_pDataStorage->QueryID( tQBuf );
  449. // On Update IF data no need to return ID
  450. if( RetVal == 0 ) {
  451. RetVal = getNetworkInfrastructureID( NIF_ID );
  452. };
  453. };
  454. return RetVal;
  455. };
  456. uint32_t sqliteDBC::addLocalNIF( CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id ){
  457. uint32_t RetVal = 0;
  458. if( m_bIsConnected ) {
  459. char tQBuf[MAX_PATH_LEN];
  460. char * tKey = MD5String( device );
  461. if( tKey != NULL ) {
  462. 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'",
  463. dtype,
  464. tKey,
  465. device,
  466. mac,
  467. dmode,
  468. aux_id,
  469. dmode,
  470. aux_id,
  471. mac
  472. );
  473. #ifdef DEBUG_ALL
  474. LOG(tQBuf);
  475. #endif
  476. RetVal = m_pDataStorage->QueryID( tQBuf );
  477. if( RetVal == 0 ) {
  478. RetVal = getNIF_ID( tKey );
  479. };
  480. free( tKey );
  481. };
  482. };
  483. return RetVal;
  484. };
  485. 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 ){
  486. uint32_t RetVal = 0;
  487. if( m_bIsConnected ) {
  488. char tQBuf[MAX_PATH_LEN];
  489. 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`",
  490. m_nServerNodeID,
  491. utype,
  492. speed,
  493. bit,
  494. parity,
  495. stop_bit,
  496. mode,
  497. port,
  498. PATH
  499. );
  500. #ifdef DEBUG_ALL
  501. LOG(tQBuf);
  502. #endif
  503. RetVal = m_pDataStorage->QueryID( tQBuf );
  504. if( RetVal == 0 ) {
  505. RetVal = getUartID( m_nServerNodeID, utype, PATH );
  506. };
  507. };
  508. return RetVal;
  509. };
  510. uint32_t sqliteDBC::addLocalWiFi( uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id ){
  511. uint32_t RetVal = 0;
  512. if( m_bIsConnected ) {
  513. char tQBuf[MAX_PATH_LEN];
  514. 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",
  515. NIF_ID,
  516. mode,
  517. router_id,
  518. SSID,
  519. password,
  520. encryption,
  521. mode,
  522. router_id,
  523. SSID,
  524. password,
  525. encryption
  526. );
  527. #ifdef DEBUG_ALL
  528. LOG(tQBuf);
  529. #endif
  530. RetVal = m_pDataStorage->QueryID( tQBuf );
  531. if( RetVal == 0 ) {
  532. RetVal = getWiFi_ID( NIF_ID );
  533. };
  534. };
  535. return RetVal;
  536. };
  537. bool sqliteDBC::cleanResources( bool all ){
  538. if(m_bIsConnected) {
  539. char tQBuf[MAX_PATH_LEN];
  540. if( all ) {
  541. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID );
  542. } else {
  543. 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 );
  544. };
  545. #ifdef DEBUG_ALL
  546. LOG(tQBuf);
  547. #endif
  548. m_pDataStorage->SilentQuery( tQBuf );
  549. return true;
  550. };
  551. return false;
  552. };
  553. uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){
  554. uint32_t RetVal = 0;
  555. if( m_bIsConnected ){
  556. char tQBuf[2048];
  557. sprintf( tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName );
  558. CSTR tMD5 = MD5String( tQBuf );
  559. if( tMD5 ) {
  560. 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",
  561. m_nServerNodeID,
  562. type,
  563. tMD5,
  564. auxID,
  565. devName,
  566. auxID
  567. );
  568. free( tMD5 );
  569. #ifdef DEBUG_ALL
  570. LOG(tQBuf);
  571. #endif
  572. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  573. if( RetVal == 0 ) {
  574. RetVal = getResourceID( m_nServerNodeID, type, auxID );
  575. };
  576. };
  577. };
  578. return RetVal;
  579. };
  580. DBC_MODULE_EMBED_VIDEO_DATA *sqliteDBC::getEmbedVideoData( uint32_t ID ){
  581. DBC_MODULE_EMBED_VIDEO_DATA *tPtr = NULL;
  582. if( m_bIsConnected ) {
  583. char tQBuf[MAX_PATH_LEN];
  584. AAssoc *result;
  585. sprintf( tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID );
  586. #ifdef DEBUG_ALL
  587. LOG(tQBuf);
  588. #endif
  589. result = m_pDataStorage->Query( tQBuf );
  590. if( result!= NULL ) {
  591. if( result->size() > 0 ) {
  592. Assoc *cArr;
  593. tPtr = (DBC_MODULE_EMBED_VIDEO_DATA*)malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  594. if( tPtr!= NULL ) {
  595. Assoc::iterator it;
  596. memset(tPtr,0,sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  597. cArr = result->at(0);
  598. it = cArr->find("output_mode");
  599. if( it != cArr->end() ){
  600. tPtr->output_mode = atoi( it->second.c_str() );
  601. };
  602. it = cArr->find("web_port");
  603. if( it != cArr->end() ){
  604. tPtr->port = atoi( it->second.c_str() );
  605. };
  606. it = cArr->find("width");
  607. if( it != cArr->end() ){
  608. tPtr->width = atoi( it->second.c_str() );
  609. };
  610. it = cArr->find("height");
  611. if( it != cArr->end() ){
  612. tPtr->height = atoi( it->second.c_str() );
  613. };
  614. it = cArr->find("compression_quality");
  615. if( it != cArr->end() ){
  616. tPtr->compression_quality = atoi( it->second.c_str() );
  617. };
  618. it = cArr->find("show_ts");
  619. if( it != cArr->end() ){
  620. tPtr->show_ts = (bool)atoi( it->second.c_str() );
  621. };
  622. it = cArr->find("ts_x");
  623. if( it != cArr->end() ){
  624. tPtr->ts_x = atoi( it->second.c_str() );
  625. };
  626. it = cArr->find("ts_y");
  627. if( it != cArr->end() ){
  628. tPtr->ts_y = atoi( it->second.c_str() );
  629. };
  630. it = cArr->find("ts_font_face");
  631. if( it != cArr->end() ){
  632. tPtr->ts_font_face = atoi( it->second.c_str() );
  633. };
  634. it = cArr->find("ts_font_scale");
  635. if( it != cArr->end() ){
  636. tPtr->ts_font_scale = atof( it->second.c_str() );
  637. };
  638. it = cArr->find("ts_font_thickness");
  639. if( it != cArr->end() ){
  640. tPtr->ts_font_thickness = atoi( it->second.c_str() );
  641. };
  642. it = cArr->find("ts_line_type");
  643. if( it != cArr->end() ){
  644. tPtr->ts_line_type = atoi( it->second.c_str() );
  645. };
  646. it = cArr->find("ts_transperant");
  647. if( it != cArr->end() ){
  648. tPtr->ts_transperant = (bool)atoi( it->second.c_str() );
  649. };
  650. it = cArr->find("show_label");
  651. if( it != cArr->end() ){
  652. tPtr->show_label = (bool)atoi( it->second.c_str() );
  653. };
  654. it = cArr->find("label_x");
  655. if( it != cArr->end() ){
  656. tPtr->label_x = atoi( it->second.c_str() );
  657. };
  658. it = cArr->find("label_y");
  659. if( it != cArr->end() ){
  660. tPtr->label_y = atoi( it->second.c_str() );
  661. };
  662. it = cArr->find("label_font_face");
  663. if( it != cArr->end() ){
  664. tPtr->label_font_face = atoi( it->second.c_str() );
  665. };
  666. it = cArr->find("label_font_scale");
  667. if( it != cArr->end() ){
  668. tPtr->label_font_scale = atof( it->second.c_str() );
  669. };
  670. it = cArr->find("label_font_thickness");
  671. if( it != cArr->end() ){
  672. tPtr->label_font_thickness = atoi( it->second.c_str() );
  673. };
  674. it = cArr->find("label_line_type");
  675. if( it != cArr->end() ){
  676. tPtr->label_line_type = atoi( it->second.c_str() );
  677. };
  678. it = cArr->find("label_transperant");
  679. if( it != cArr->end() ){
  680. tPtr->label_transperant = (bool) atoi( it->second.c_str() );
  681. };
  682. it = cArr->find("show_logo");
  683. if( it != cArr->end() ){
  684. tPtr->show_logo = (bool) atoi( it->second.c_str() );
  685. };
  686. it = cArr->find("logo_w");
  687. if( it != cArr->end() ){
  688. tPtr->logo_w = atoi( it->second.c_str() );
  689. };
  690. it = cArr->find("logo_h");
  691. if( it != cArr->end() ){
  692. tPtr->logo_h = atoi( it->second.c_str() );
  693. };
  694. it = cArr->find("logo_x");
  695. if( it != cArr->end() ){
  696. tPtr->logo_x = atoi( it->second.c_str() );
  697. };
  698. it = cArr->find("logo_y");
  699. if( it != cArr->end() ){
  700. tPtr->logo_y = atoi( it->second.c_str() );
  701. };
  702. it = cArr->find("logo_md5");
  703. if( it != cArr->end() ){
  704. strncpy( tPtr->logo_md5, it->second.c_str(),32);
  705. tPtr->logo_md5[32]=(char)0;
  706. };
  707. };
  708. };
  709. };
  710. deleteResult( result );
  711. };
  712. return tPtr;
  713. };
  714. DBC_MEDIA_STREAM_DATA *sqliteDBC::getMediaStreamData(uint32_t ID){
  715. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  716. if(m_bIsConnected){
  717. char tQBuf[CTEXT];
  718. AAssoc *result;
  719. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  720. #ifdef DEBUG_ALL
  721. LOG(tQBuf);
  722. #endif
  723. result = m_pDataStorage->Query( tQBuf );
  724. if( result!= NULL ) {
  725. if( result->size() > 0 ) {
  726. Assoc *cArr;
  727. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  728. if( tVal != NULL ) {
  729. memset( tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA) );
  730. Assoc::iterator it;
  731. cArr = result->at(0);
  732. tVal->id = ID;
  733. it = cArr->find("source_id");
  734. if( it != cArr->end() ){
  735. tVal->source_id = atoi( it->second.c_str() );
  736. };
  737. it = cArr->find("source_type");
  738. if( it != cArr->end() ){
  739. sprintf( tVal->source_type, it->second.c_str() );
  740. };
  741. it = cArr->find("valid");
  742. if( it != cArr->end() ){
  743. tVal->valid = (bool) atoi( it->second.c_str() );
  744. };
  745. it = cArr->find("token");
  746. if( it != cArr->end() ){
  747. sprintf( tVal->token, it->second.c_str() );
  748. };
  749. it = cArr->find("img_width");
  750. if( it != cArr->end() ){
  751. tVal->width = atoi( it->second.c_str() );
  752. };
  753. it = cArr->find("img_height");
  754. if( it != cArr->end() ){
  755. tVal->height = atoi( it->second.c_str() );
  756. };
  757. it = cArr->find("fps");
  758. if( it != cArr->end() ){
  759. tVal->fps = atoi( it->second.c_str() );
  760. };
  761. it = cArr->find("compression_type");
  762. if( it != cArr->end() ){
  763. sprintf(tVal->c_type, it->second.c_str());
  764. };
  765. it = cArr->find("mrl_proto");
  766. if( it != cArr->end() ){
  767. sprintf( tVal->mrl_proto, it->second.c_str() );
  768. };
  769. it = cArr->find("mrl_port");
  770. if( it != cArr->end() ){
  771. tVal->mrl_port = atoi( it->second.c_str() );
  772. };
  773. it = cArr->find("mrl");
  774. if( it != cArr->end() ){
  775. sprintf( tVal->mrl, it->second.c_str() );
  776. };
  777. };
  778. };
  779. };
  780. deleteResult( result );
  781. };
  782. return tVal;
  783. };
  784. uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
  785. uint32_t RetVal = 0;
  786. if(m_bIsConnected){
  787. char tQBuf[CTEXT];
  788. AAssoc *result;
  789. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
  790. #ifdef DEBUG_ALL
  791. LOG(tQBuf);
  792. #endif
  793. result = m_pDataStorage->Query( tQBuf );
  794. if( result!= NULL ) {
  795. if( result->size() > 0 ) {
  796. Assoc *cArr;
  797. Assoc::iterator it;
  798. cArr = result->at(0);
  799. it = cArr->find("id");
  800. if( it != cArr->end() ){
  801. RetVal = atoi( it->second.c_str() );
  802. };
  803. };
  804. };
  805. deleteResult( result );
  806. };
  807. return RetVal;
  808. };
  809. DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
  810. DBC_MODULE_DATA *tVal = NULL;
  811. if(m_bIsConnected){
  812. char tQBuf[CTEXT];
  813. AAssoc *result;
  814. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
  815. #ifdef DEBUG_ALL
  816. LOG(tQBuf);
  817. #endif
  818. result = m_pDataStorage->Query( tQBuf );
  819. if( result!= NULL ) {
  820. if( result->size() > 0 ) {
  821. Assoc *cArr;
  822. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  823. if( tVal != NULL ) {
  824. Assoc::iterator it;
  825. cArr = result->at(0);
  826. it = cArr->find("id");
  827. if( it != cArr->end() ){
  828. tVal->module_id = atoi( it->second.c_str() );
  829. };
  830. it = cArr->find("aux_id");
  831. if( it != cArr->end() ){
  832. tVal->aux_id = atoi( it->second.c_str() );
  833. };
  834. it = cArr->find("type");
  835. if( it != cArr->end() ){
  836. tVal->type = atoi( it->second.c_str() );
  837. };
  838. it = cArr->find("enabled");
  839. if( it != cArr->end() ){
  840. tVal->enabled = (bool)atoi( it->second.c_str() );
  841. };
  842. };
  843. };
  844. };
  845. deleteResult( result );
  846. };
  847. return tVal;
  848. };
  849. DBC_LRF_DATA *sqliteDBC::getLRFData( uint32_t ID ){
  850. DBC_LRF_DATA * tVal = NULL;
  851. if(m_bIsConnected){
  852. char tQBuf[CTEXT];
  853. AAssoc *result;
  854. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
  855. #ifdef DEBUG_ALL
  856. LOG(tQBuf);
  857. #endif
  858. result = m_pDataStorage->Query( tQBuf );
  859. if( result!= NULL ) {
  860. if( result->size() > 0 ) {
  861. Assoc *cArr;
  862. tVal = (DBC_LRF_DATA*) malloc(sizeof(DBC_LRF_DATA));
  863. if( tVal != NULL ) {
  864. memset( tVal, 0 , sizeof(DBC_LRF_DATA));
  865. Assoc::iterator it;
  866. tVal->id = ID;
  867. cArr = result->at(0);
  868. it = cArr->find("data_status");
  869. if( it != cArr->end() ){
  870. tVal->data_status = atoi( it->second.c_str() );
  871. };
  872. it = cArr->find("serial");
  873. if( it != cArr->end() ){
  874. strncpy( tVal->serial, it->second.c_str(), sizeof(tVal->serial)-1 );
  875. tVal->serial[sizeof(tVal->serial)-1]=(char)0;
  876. };
  877. it = cArr->find("version");
  878. if( it != cArr->end() ){
  879. strncpy( tVal->version, it->second.c_str(), sizeof(tVal->version)-1 );
  880. tVal->version[sizeof(tVal->version)-1]=(char)0;
  881. };
  882. it = cArr->find("x");
  883. if( it != cArr->end() ){
  884. tVal->x = atof( it->second.c_str() );
  885. };
  886. it = cArr->find("y");
  887. if( it != cArr->end() ){
  888. tVal->y = atof( it->second.c_str() );
  889. };
  890. it = cArr->find("z");
  891. if( it != cArr->end() ){
  892. tVal->z = atof( it->second.c_str() );
  893. };
  894. it = cArr->find("x_rot");
  895. if( it != cArr->end() ){
  896. tVal->x_rot = atof( it->second.c_str() );
  897. };
  898. it = cArr->find("y_rot");
  899. if( it != cArr->end() ){
  900. tVal->y_rot = atof( it->second.c_str() );
  901. };
  902. it = cArr->find("z_rot");
  903. if( it != cArr->end() ){
  904. tVal->z_rot = atof( it->second.c_str() );
  905. };
  906. it = cArr->find("uart_id");
  907. if( it != cArr->end() ){
  908. tVal->uart_id = atoi( it->second.c_str() );
  909. };
  910. it = cArr->find("device_id");
  911. if( it != cArr->end() ){
  912. tVal->device_id = atoi( it->second.c_str() );
  913. };
  914. it = cArr->find("ip_address");
  915. if( it != cArr->end() ){
  916. sprintf( tVal->master_host, it->second.c_str() );
  917. };
  918. it = cArr->find("aux_ip_address");
  919. if( it != cArr->end() ){
  920. sprintf( tVal->slave_host, it->second.c_str() );
  921. };
  922. it = cArr->find("port");
  923. if( it != cArr->end() ){
  924. tVal->master_port = atoi( it->second.c_str() );
  925. };
  926. it = cArr->find("aux_port");
  927. if( it != cArr->end() ){
  928. tVal->slave_port = atoi( it->second.c_str() );
  929. };
  930. it = cArr->find("auto_draw");
  931. if( it != cArr->end() ){
  932. tVal->auto_draw = atoi( it->second.c_str() );
  933. };
  934. it = cArr->find("filter_id");
  935. if( it != cArr->end() ){
  936. tVal->filter_id = atoi( it->second.c_str() );
  937. };
  938. //it = cArr->find("enabled_lrf");
  939. //if( it != cArr->end() ){
  940. // tVal->enabled_lrf = atoi( it->second.c_str() );
  941. //};
  942. it = cArr->find("max_range");
  943. if( it != cArr->end() ){
  944. tVal->max_range = atof( it->second.c_str() );
  945. };
  946. it = cArr->find("base_shift");
  947. if( it != cArr->end() ){
  948. tVal->base_shift = atoi( it->second.c_str() );
  949. };
  950. it = cArr->find("mirror_shift");
  951. if( it != cArr->end() ){
  952. tVal->mirror_shift = atoi( it->second.c_str() );
  953. };
  954. it = cArr->find("detection_profile_id");
  955. if( it != cArr->end() ){
  956. tVal->detection_profile_id = atoi( it->second.c_str() );
  957. };
  958. it = cArr->find("draw_lrf_id");
  959. if( it != cArr->end() ){
  960. tVal->draw_lrf_id = atoi( it->second.c_str() );
  961. };
  962. it = cArr->find("width_table_id");
  963. if( it != cArr->end() ){
  964. tVal->width_table_id = atoi( it->second.c_str() );
  965. };
  966. it = cArr->find("zoom_table_id");
  967. if( it != cArr->end() ){
  968. tVal->zoom_table_id = atoi( it->second.c_str() );
  969. };
  970. it = cArr->find("embd_cam");
  971. if( it != cArr->end() ){
  972. tVal->embd_cam = atoi( it->second.c_str() );
  973. };
  974. it = cArr->find("embd_cam_auto");
  975. if( it != cArr->end() ){
  976. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  977. };
  978. it = cArr->find("embd_dc");
  979. if( it != cArr->end() ){
  980. tVal->embd_dc = atoi( it->second.c_str() );
  981. };
  982. it = cArr->find("autoreset");
  983. if( it != cArr->end() ){
  984. tVal->autoreset = (bool)atoi( it->second.c_str() );
  985. };
  986. it = cArr->find("force_settings");
  987. if( it != cArr->end() ){
  988. tVal->force_settings = (bool)atoi( it->second.c_str() );
  989. };
  990. };
  991. };
  992. };
  993. deleteResult( result );
  994. };
  995. return tVal;
  996. };
  997. uint8_t sqliteDBC::getLRFDataStatus( uint32_t ID ){
  998. uint8_t RetVal = 0;
  999. if(m_bIsConnected){
  1000. char tQBuf[CTEXT];
  1001. AAssoc *result;
  1002. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID );
  1003. #ifdef DEBUG
  1004. LOG(tQBuf);
  1005. #endif
  1006. result = m_pDataStorage->Query( tQBuf );
  1007. if( result!= NULL ) {
  1008. if( result->size() > 0 ) {
  1009. Assoc *cArr;
  1010. Assoc::iterator it;
  1011. cArr = result->at(0);
  1012. it = cArr->find("data_status");
  1013. if( it != cArr->end() ){
  1014. RetVal = atoi( it->second.c_str() );
  1015. };
  1016. };
  1017. };
  1018. deleteResult( result );
  1019. };
  1020. return RetVal;
  1021. };
  1022. DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
  1023. DBC_NODE_DATA * tVal = NULL;
  1024. if(m_bIsConnected){
  1025. char tQBuf[CTEXT];
  1026. AAssoc *result;
  1027. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
  1028. #ifdef DEBUG_EXTRA
  1029. LOG(tQBuf);
  1030. #endif
  1031. result = m_pDataStorage->Query( tQBuf );
  1032. if( result!= NULL ) {
  1033. if( result->size() > 0 ) {
  1034. Assoc *cArr;
  1035. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  1036. if( tVal != NULL ) {
  1037. memset( tVal, 0, sizeof(DBC_NODE_DATA));
  1038. Assoc::iterator it;
  1039. tVal->node_id = ID;
  1040. cArr = result->at(0);
  1041. it = cArr->find("type");
  1042. if( it != cArr->end() ){
  1043. tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
  1044. };
  1045. it = cArr->find("aux_id");
  1046. if( it != cArr->end() ){
  1047. tVal->aux_id = atoi( it->second.c_str() );
  1048. };
  1049. it = cArr->find("aux_parameter");
  1050. if( it != cArr->end() ){
  1051. tVal->aux_parameter = atoi( it->second.c_str() );
  1052. };
  1053. it = cArr->find("online");
  1054. if( it != cArr->end() ){
  1055. tVal->online = (bool)atoi( it->second.c_str() );
  1056. };
  1057. it = cArr->find("enable");
  1058. if( it != cArr->end() ){
  1059. tVal->enabled = (bool)atoi( it->second.c_str() );
  1060. };
  1061. it = cArr->find("name");
  1062. if( it != cArr->end() ){
  1063. sprintf( tVal->name, it->second.c_str() );
  1064. };
  1065. };
  1066. };
  1067. };
  1068. deleteResult( result );
  1069. };
  1070. return tVal;
  1071. };
  1072. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  1073. DBC_NET_IF_DATA * tVal = NULL;
  1074. if(m_bIsConnected){
  1075. char tQBuf[CTEXT];
  1076. AAssoc *result;
  1077. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  1078. #ifdef DEBUG_EXTRA
  1079. LOG(tQBuf);
  1080. #endif
  1081. result = m_pDataStorage->Query( tQBuf );
  1082. if( result!= NULL ) {
  1083. if( result->size() > 0 ) {
  1084. Assoc *cArr;
  1085. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  1086. if( tVal != NULL ) {
  1087. Assoc::iterator it;
  1088. tVal->id = nif_id;
  1089. cArr = result->at(0);
  1090. it = cArr->find("type");
  1091. if( it != cArr->end() ){
  1092. tVal->type = atoi(it->second.c_str());
  1093. };
  1094. it = cArr->find("mode");
  1095. if( it != cArr->end() ){
  1096. tVal->mode = atoi(it->second.c_str());
  1097. };
  1098. it = cArr->find("device_name");
  1099. if( it != cArr->end() ){
  1100. strncpy( tVal->device_name, it->second.c_str(),11);
  1101. tVal->device_name[10]=(char)0;
  1102. };
  1103. it = cArr->find("mac");
  1104. if( it != cArr->end() ){
  1105. strncpy( tVal->mac, it->second.c_str(), 33 );
  1106. tVal->mac[32]=(char)0;
  1107. };
  1108. it = cArr->find("aux_id");
  1109. if( it != cArr->end() ){
  1110. tVal->aux_id = atoi(it->second.c_str());
  1111. };
  1112. };
  1113. };
  1114. };
  1115. deleteResult( result );
  1116. };
  1117. return tVal;
  1118. };
  1119. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  1120. uint32_t tVal = 0;
  1121. if(m_bIsConnected){
  1122. char tQBuf[CTEXT];
  1123. AAssoc *result;
  1124. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  1125. #ifdef DEBUG_ALL
  1126. LOG(tQBuf);
  1127. #endif
  1128. result = m_pDataStorage->Query( tQBuf );
  1129. if( result!= NULL ) {
  1130. if( result->size() > 0 ) {
  1131. Assoc *cArr;
  1132. Assoc::iterator it;
  1133. cArr = result->at(0);
  1134. it = cArr->find("id");
  1135. if( it != cArr->end() ){
  1136. tVal = atoi( it->second.c_str() );
  1137. };
  1138. };
  1139. };
  1140. deleteResult( result );
  1141. };
  1142. return tVal;
  1143. };
  1144. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  1145. uint32_t tVal = 0;
  1146. if(m_bIsConnected){
  1147. char tQBuf[CTEXT];
  1148. AAssoc *result;
  1149. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  1150. #ifdef DEBUG_ALL
  1151. LOG(tQBuf);
  1152. #endif
  1153. result = m_pDataStorage->Query( tQBuf );
  1154. if( result!= NULL ) {
  1155. if( result->size() > 0 ) {
  1156. Assoc *cArr;
  1157. Assoc::iterator it;
  1158. cArr = result->at(0);
  1159. it = cArr->find("id");
  1160. if( it != cArr->end() ){
  1161. tVal = atoi( it->second.c_str() );
  1162. };
  1163. };
  1164. };
  1165. deleteResult( result );
  1166. };
  1167. return tVal;
  1168. };
  1169. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  1170. uint32_t tVal = 0;
  1171. if(m_bIsConnected){
  1172. char tQBuf[CTEXT];
  1173. AAssoc *result;
  1174. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  1175. #ifdef DEBUG_ALL
  1176. LOG(tQBuf);
  1177. #endif
  1178. result = m_pDataStorage->Query( tQBuf );
  1179. if( result!= NULL ) {
  1180. if( result->size() > 0 ) {
  1181. Assoc *cArr;
  1182. Assoc::iterator it;
  1183. cArr = result->at(0);
  1184. it = cArr->find("id");
  1185. if( it != cArr->end() ){
  1186. tVal = atoi( it->second.c_str() );
  1187. };
  1188. };
  1189. };
  1190. deleteResult( result );
  1191. };
  1192. return tVal;
  1193. };
  1194. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  1195. DBC_RESOURCE_DATA *tVal = NULL;
  1196. if( m_bIsConnected ){
  1197. char tQBuf[CTEXT];
  1198. AAssoc *result;
  1199. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  1200. #ifdef DEBUG_ALL
  1201. LOG(tQBuf);
  1202. #endif
  1203. result = m_pDataStorage->Query( tQBuf );
  1204. if( result!= NULL ) {
  1205. if( result->size() > 0 ) {
  1206. Assoc *cArr;
  1207. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  1208. if( tVal != NULL ) {
  1209. Assoc::iterator it;
  1210. cArr = result->at(0);
  1211. tVal->id = ResourceID;
  1212. it = cArr->find("node_id");
  1213. if( it != cArr->end() ){
  1214. tVal->node_id = atoi( it->second.c_str() );
  1215. };
  1216. it = cArr->find("type");
  1217. if( it != cArr->end() ){
  1218. tVal->type = atoi( it->second.c_str() );
  1219. };
  1220. it = cArr->find("aux_id");
  1221. if( it != cArr->end() ){
  1222. tVal->aux_id = atoi( it->second.c_str() );
  1223. };
  1224. it = cArr->find("device");
  1225. if( it != cArr->end() ){
  1226. strncpy( tVal->device, it->second.c_str(), 64 );
  1227. tVal->device[63] = (char)0;
  1228. };
  1229. };
  1230. };
  1231. };
  1232. deleteResult( result );
  1233. };
  1234. return tVal;
  1235. };
  1236. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  1237. uint32_t RetVal = 0;
  1238. if(m_bIsConnected){
  1239. char tQBuf[CTEXT];
  1240. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  1241. #ifdef DEBUG_ALL
  1242. LOG(tQBuf);
  1243. #endif
  1244. AAssoc *result;
  1245. result = m_pDataStorage->Query( tQBuf );
  1246. if( result!= NULL ) {
  1247. if( result->size() > 0 ) {
  1248. Assoc * cArr;
  1249. Assoc::iterator it;
  1250. cArr = result->at(0);
  1251. it = cArr->find("id");
  1252. if( it != cArr->end() ){
  1253. RetVal = atoi( it->second.c_str() );
  1254. };
  1255. };
  1256. };
  1257. deleteResult( result );
  1258. };
  1259. return RetVal;
  1260. };
  1261. uint32_t sqliteDBC::getServerID( CSTR identity ){
  1262. uint32_t RetVal = 0;
  1263. if(m_bIsConnected){
  1264. char tQBuf[CTEXT];
  1265. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  1266. #ifdef DEBUG_ALL
  1267. LOG(tQBuf);
  1268. #endif
  1269. AAssoc *result;
  1270. result = m_pDataStorage->Query( tQBuf );
  1271. if( result!= NULL ) {
  1272. if( result->size() > 0 ) {
  1273. Assoc * cArr;
  1274. Assoc::iterator it;
  1275. cArr = result->at(0);
  1276. it = cArr->find("id");
  1277. if( it != cArr->end() ){
  1278. RetVal = atoi( it->second.c_str() );
  1279. };
  1280. };
  1281. };
  1282. deleteResult( result );
  1283. };
  1284. return RetVal;
  1285. };
  1286. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  1287. uint32_t RetVal = 0;
  1288. if(m_bIsConnected){
  1289. char tQBuf[CTEXT];
  1290. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  1291. #ifdef DEBUG_ALL
  1292. LOG(tQBuf);
  1293. #endif
  1294. AAssoc *result;
  1295. result = m_pDataStorage->Query( tQBuf );
  1296. if( result!= NULL ) {
  1297. if( result->size() > 0 ) {
  1298. Assoc * cArr;
  1299. Assoc::iterator it;
  1300. cArr = result->at(0);
  1301. it = cArr->find("id");
  1302. if( it != cArr->end() ){
  1303. RetVal = atoi( it->second.c_str() );
  1304. };
  1305. };
  1306. };
  1307. deleteResult( result );
  1308. };
  1309. return RetVal;
  1310. }
  1311. DBC_UART_DATA *sqliteDBC::getUartData( uint32_t UartID ){
  1312. DBC_UART_DATA *tVal = NULL;
  1313. if( m_bIsConnected ) {
  1314. char tQBuf[MAX_PATH_LEN];
  1315. AAssoc *result;
  1316. sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
  1317. #ifdef DEBUG_ALL
  1318. LOG(tQBuf);
  1319. #endif
  1320. result = m_pDataStorage->Query( tQBuf );
  1321. if( result!= NULL ) {
  1322. if( result->size() > 0 ) {
  1323. Assoc *cArr;
  1324. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  1325. if( tVal != NULL ) {
  1326. Assoc::iterator it;
  1327. cArr = result->at(0);
  1328. it = cArr->find("type");
  1329. if( it != cArr->end() ){
  1330. tVal->type = atoi( it->second.c_str() );
  1331. };
  1332. it = cArr->find("speed");
  1333. if( it != cArr->end() ){
  1334. tVal->speed = atoi( it->second.c_str() );
  1335. };
  1336. it = cArr->find("mode");
  1337. if( it != cArr->end() ){
  1338. tVal->mode = atoi( it->second.c_str() );
  1339. };
  1340. it = cArr->find("port");
  1341. if( it != cArr->end() ){
  1342. tVal->port = atoi( it->second.c_str() );
  1343. };
  1344. it = cArr->find("path");
  1345. if( it != cArr->end() ){
  1346. strncpy( tVal->path, it->second.c_str(), 128 );
  1347. };
  1348. };
  1349. };
  1350. };
  1351. deleteResult( result );
  1352. };
  1353. return tVal;
  1354. };
  1355. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  1356. uint32_t RetVal = 0;
  1357. if(m_bIsConnected){
  1358. char tQBuf[CTEXT];
  1359. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  1360. #ifdef DEBUG_ALL
  1361. LOG(tQBuf);
  1362. #endif
  1363. AAssoc *result;
  1364. result = m_pDataStorage->Query( tQBuf );
  1365. if( result!= NULL ) {
  1366. if( result->size() > 0 ) {
  1367. Assoc * cArr;
  1368. Assoc::iterator it;
  1369. cArr = result->at(0);
  1370. it = cArr->find("id");
  1371. if( it != cArr->end() ){
  1372. RetVal = atoi( it->second.c_str() );
  1373. };
  1374. };
  1375. };
  1376. deleteResult( result );
  1377. };
  1378. return RetVal;
  1379. };
  1380. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  1381. uint32_t tVal = 0;
  1382. if(m_bIsConnected){
  1383. char tQBuf[CTEXT];
  1384. AAssoc *result;
  1385. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  1386. #ifdef DEBUG_ALL
  1387. LOG(tQBuf);
  1388. #endif
  1389. result = m_pDataStorage->Query( tQBuf );
  1390. if( result!= NULL ) {
  1391. if( result->size() > 0 ) {
  1392. Assoc *cArr;
  1393. Assoc::iterator it;
  1394. cArr = result->at(0);
  1395. it = cArr->find("id");
  1396. if( it != cArr->end() ){
  1397. tVal = atoi( it->second.c_str() );
  1398. };
  1399. };
  1400. };
  1401. deleteResult( result );
  1402. };
  1403. return tVal;
  1404. };
  1405. DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
  1406. DBC_INT_LIST * tVal = NULL;
  1407. if(m_bIsConnected){
  1408. char tQBuf[CTEXT];
  1409. AAssoc *result;
  1410. switch( listMode ){
  1411. case LIST_VALID_RECORDS:
  1412. 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" );
  1413. break;
  1414. case LIST_INVALID_RECORDS:
  1415. 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" );
  1416. break;
  1417. default:
  1418. 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" );
  1419. };
  1420. #ifdef DEBUG_ALL
  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. DBC_INT_LIST *sqliteDBC::listModules(){
  1445. DBC_INT_LIST * tVal = NULL;
  1446. if(m_bIsConnected){
  1447. AAssoc *result;
  1448. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  1449. if( result!= NULL ) {
  1450. if( result->size() > 0 ) {
  1451. Assoc *cArr;
  1452. tVal = new DBC_INT_LIST();
  1453. if( tVal != NULL ) {
  1454. Assoc::iterator it;
  1455. for(int i=0;i<result->size();i++) {
  1456. cArr = result->at(i);
  1457. it = cArr->find("id");
  1458. if( it != cArr->end() ){
  1459. tVal->push_back( atoi( it->second.c_str() ));
  1460. };
  1461. };
  1462. };
  1463. };
  1464. };
  1465. deleteResult( result );
  1466. };
  1467. return tVal;
  1468. };
  1469. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  1470. DBC_INT_LIST * tVal = NULL;
  1471. char tParam[CTEXT];
  1472. //memset( tParam, 0, CTEXT );
  1473. sprintf(tParam,"0");
  1474. if( old_ids != NULL ) {
  1475. char tBuffer[CTEXT];
  1476. for(int i = 0; i< old_ids->size(); i++ ) {
  1477. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  1478. strncpy( tParam, tBuffer, CTEXT );
  1479. };
  1480. };
  1481. if(m_bIsConnected){
  1482. char tQBuf[CTEXT];
  1483. AAssoc *result;
  1484. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  1485. #ifdef DEBUG_ALL
  1486. LOG(tQBuf);
  1487. #endif
  1488. result = m_pDataStorage->Query( tQBuf );
  1489. if( result!= NULL ) {
  1490. if( result->size() > 0 ) {
  1491. Assoc *cArr;
  1492. tVal = new DBC_INT_LIST();
  1493. if( tVal != NULL ) {
  1494. Assoc::iterator it;
  1495. for(int i=0;i<result->size();i++) {
  1496. cArr = result->at(i);
  1497. it = cArr->find("id");
  1498. if( it != cArr->end() ){
  1499. tVal->push_back( atoi( it->second.c_str() ));
  1500. };
  1501. };
  1502. };
  1503. };
  1504. };
  1505. deleteResult( result );
  1506. };
  1507. return tVal;
  1508. };
  1509. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  1510. DBC_INT_LIST * tVal = NULL;
  1511. char tParam[CTEXT];
  1512. //memset( tParam, 0, CTEXT );
  1513. sprintf(tParam,"0");
  1514. if( old_ids != NULL ) {
  1515. char tBuffer[CTEXT];
  1516. for(int i = 0; i< old_ids->size(); i++ ) {
  1517. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  1518. strncpy( tParam, tBuffer, CTEXT );
  1519. };
  1520. };
  1521. if( m_bIsConnected ){
  1522. char tQBuf[CTEXT];
  1523. AAssoc *result;
  1524. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam );
  1525. #ifdef DEBUG_EXTRA
  1526. LOG(tQBuf);
  1527. #endif
  1528. result = m_pDataStorage->Query( tQBuf );
  1529. if( result!= NULL ) {
  1530. if( result->size() > 0 ) {
  1531. Assoc *cArr;
  1532. tVal = new DBC_INT_LIST();
  1533. if( tVal != NULL ) {
  1534. Assoc::iterator it;
  1535. for(int i=0;i<result->size();i++) {
  1536. cArr = result->at(i);
  1537. it = cArr->find("id");
  1538. if( it != cArr->end() ){
  1539. tVal->push_back( atoi( it->second.c_str() ));
  1540. };
  1541. };
  1542. };
  1543. };
  1544. };
  1545. deleteResult( result );
  1546. };
  1547. return tVal;
  1548. };
  1549. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  1550. DBC_INT_LIST * tVal = NULL;
  1551. if( m_bIsConnected ){
  1552. char tQBuf[1024];
  1553. AAssoc *result;
  1554. if( all ) {
  1555. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  1556. } else {
  1557. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1" );
  1558. };
  1559. #ifdef DEBUG_EXTRA
  1560. LOG(tQBuf);
  1561. #endif
  1562. result = m_pDataStorage->Query( tQBuf );
  1563. if( result!= NULL ) {
  1564. if( result->size() > 0 ) {
  1565. Assoc *cArr;
  1566. tVal = new DBC_INT_LIST();
  1567. if( tVal != NULL ) {
  1568. Assoc::iterator it;
  1569. for(int i=0; i <result->size();i++) {
  1570. cArr = result->at(i);
  1571. it = cArr->find("id");
  1572. if( it != cArr->end() ){
  1573. tVal->push_back( atoi( it->second.c_str() ));
  1574. };
  1575. };
  1576. };
  1577. };
  1578. };
  1579. deleteResult( result );
  1580. };
  1581. return tVal;
  1582. };
  1583. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  1584. DBC_INT_LIST * tVal = NULL;
  1585. if(m_bIsConnected){
  1586. char tQBuf[CTEXT];
  1587. AAssoc *result;
  1588. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  1589. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  1590. #ifdef DEBUG
  1591. LOG(tQBuf);
  1592. #endif
  1593. result = m_pDataStorage->Query( tQBuf );
  1594. if( result!= NULL ) {
  1595. if( result->size() > 0 ) {
  1596. Assoc *cArr;
  1597. tVal = new DBC_INT_LIST();
  1598. if( tVal != NULL ) {
  1599. Assoc::iterator it;
  1600. for(int i=0;i<result->size();i++) {
  1601. cArr = result->at(i);
  1602. it = cArr->find("id");
  1603. if( it != cArr->end() ){
  1604. tVal->push_back( atoi( it->second.c_str() ));
  1605. };
  1606. };
  1607. };
  1608. };
  1609. };
  1610. deleteResult( result );
  1611. };
  1612. return tVal;
  1613. };
  1614. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  1615. if( m_bIsConnected ){
  1616. m_nSiteID = SiteID;
  1617. char tQBuf[MAX_PATH_LEN];
  1618. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  1619. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  1620. #ifdef DEBUG_ALL
  1621. LOG(tQBuf);
  1622. #endif
  1623. m_pDataStorage->SilentQuery( tQBuf );
  1624. m_nServerID = getServerID( identity );
  1625. *ServID = m_nServerID;
  1626. if( m_nServerID ) {
  1627. //if ( bRecreateMemStructures )
  1628. initMemDB( m_nServerID );
  1629. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  1630. #ifdef DEBUG_ALL
  1631. LOG(tQBuf);
  1632. #endif
  1633. m_pDataMemory->SilentQuery( tQBuf );
  1634. 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 );
  1635. #ifdef DEBUG_ALL
  1636. LOG(tQBuf);
  1637. #endif
  1638. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  1639. if( m_nServerNodeID== 0 ){
  1640. m_nServerNodeID = getServerNode( m_nServerID );
  1641. };
  1642. *ServNode = m_nServerNodeID;
  1643. // Clenup Nodes and services ...
  1644. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  1645. #ifdef DEBUG_ALL
  1646. LOG(tQBuf);
  1647. #endif
  1648. m_pDataStorage->SilentQuery( tQBuf );
  1649. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  1650. #ifdef DEBUG_ALL
  1651. LOG(tQBuf);
  1652. #endif
  1653. m_pDataStorage->SilentQuery( tQBuf );
  1654. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  1655. #ifdef DEBUG_ALL
  1656. LOG(tQBuf);
  1657. #endif
  1658. m_pDataStorage->SilentQuery( tQBuf );
  1659. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  1660. #ifdef DEBUG_ALL
  1661. // LOG(tQBuf);
  1662. #endif
  1663. // m_pDataStorage->SilentQuery( tQBuf );
  1664. //cleanResources( tServerNodeID );
  1665. setNodeOnline( m_nServerNodeID );
  1666. };
  1667. };
  1668. };
  1669. bool sqliteDBC::setLRFDataStatus( uint32_t lrfID, uint8_t ST ){
  1670. if( m_bIsConnected ) {
  1671. char tQBuf[MAX_PATH_LEN];
  1672. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID );
  1673. #ifdef DEBUG
  1674. LOG(tQBuf);
  1675. #endif
  1676. m_pDataStorage->SilentQuery( tQBuf );
  1677. return true;
  1678. };
  1679. return false;
  1680. };
  1681. bool sqliteDBC::setLRFDetectionProfile( uint32_t lrfID, uint8_t profileID ){
  1682. if( m_bIsConnected ) {
  1683. char tQBuf[MAX_PATH_LEN];
  1684. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID );
  1685. #ifdef DEBUG_ALL
  1686. LOG(tQBuf);
  1687. #endif
  1688. m_pDataStorage->SilentQuery( tQBuf );
  1689. return true;
  1690. };
  1691. return false;
  1692. };
  1693. bool sqliteDBC::setLRFMaskStatus( uint32_t maskID, bool status ){
  1694. if( m_bIsConnected ) {
  1695. char tQBuf[MAX_PATH_LEN];
  1696. char S[2];
  1697. if (status ) {
  1698. S[0] = 'E';
  1699. S[1] = (char)0;
  1700. } else {
  1701. S[0] = 'D';
  1702. S[1] = (char)0;
  1703. };
  1704. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='%s' WHERE `id`=%d", S, maskID );
  1705. #ifdef DEBUG_ALL
  1706. LOG(tQBuf);
  1707. #endif
  1708. m_pDataStorage->SilentQuery( tQBuf );
  1709. return true;
  1710. };
  1711. return false;
  1712. };
  1713. bool sqliteDBC::setLRFAltSerial( uint32_t lrfID, CSTR Serial ){
  1714. if( m_bIsConnected ) {
  1715. char tQBuf[MAX_PATH_LEN];
  1716. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID );
  1717. #ifdef DEBUG_ALL
  1718. LOG(tQBuf);
  1719. #endif
  1720. m_pDataStorage->SilentQuery( tQBuf );
  1721. return true;
  1722. };
  1723. return false;
  1724. };
  1725. bool sqliteDBC::setLRFAltVerison( uint32_t lrfID, CSTR version ){
  1726. if( m_bIsConnected ) {
  1727. char tQBuf[MAX_PATH_LEN];
  1728. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID );
  1729. #ifdef DEBUG_ALL
  1730. LOG(tQBuf);
  1731. #endif
  1732. m_pDataStorage->SilentQuery( tQBuf );
  1733. return true;
  1734. };
  1735. return false;
  1736. };
  1737. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  1738. if(m_bIsConnected){
  1739. char tQBuf[CTEXT];
  1740. if( NodeID ) {
  1741. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  1742. #ifdef DEBUG_ALL
  1743. LOG(tQBuf);
  1744. #endif
  1745. m_pDataStorage->SilentQuery( tQBuf );
  1746. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  1747. #ifdef DEBUG_ALL
  1748. LOG(tQBuf);
  1749. #endif
  1750. m_pDataStorage->SilentQuery( tQBuf );
  1751. return true;
  1752. };
  1753. };
  1754. return false;
  1755. };
  1756. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  1757. if(m_bIsConnected){
  1758. char tQBuf[CTEXT];
  1759. if( NodeID ) {
  1760. sprintf(tQBuf,"UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID );
  1761. #ifdef DEBUG
  1762. LOG(tQBuf);
  1763. #endif
  1764. m_pDataStorage->SilentQuery( tQBuf );
  1765. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  1766. #ifdef DEBUG
  1767. LOG(tQBuf);
  1768. #endif
  1769. m_pDataStorage->SilentQuery( tQBuf );
  1770. return true;
  1771. };
  1772. };
  1773. return false;
  1774. };
  1775. bool sqliteDBC::setRessourceActive( uint32_t RessourceID, bool Status ){
  1776. if(m_bIsConnected){
  1777. char tQBuf[CTEXT];
  1778. if( RessourceID ) {
  1779. sprintf(tQBuf,"UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID );
  1780. #ifdef DEBUG
  1781. LOG(tQBuf);
  1782. #endif
  1783. m_pDataStorage->SilentQuery( tQBuf );
  1784. return true;
  1785. };
  1786. };
  1787. return false;
  1788. };
  1789. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  1790. if( m_bIsConnected ) {
  1791. char tQBuf[MAX_PATH_LEN];
  1792. if ( status ) {
  1793. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  1794. } else {
  1795. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  1796. };
  1797. #ifdef DEBUG
  1798. LOG(tQBuf);
  1799. #endif
  1800. m_pDataStorage->SilentQuery( tQBuf );
  1801. return true;
  1802. };
  1803. return false;
  1804. };
  1805. void sqliteDBC::unregisterServer( void ){
  1806. setNodeOffline( m_nServerNodeID );
  1807. };
  1808. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  1809. if( m_bIsConnected ) {
  1810. char tQBuf[MAX_PATH_LEN];
  1811. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  1812. ptr->OS_INFO,
  1813. ptr->HW_INFO,
  1814. m_nSiteID
  1815. );
  1816. #ifdef DEBUG_ALL
  1817. LOG(tQBuf);
  1818. #endif
  1819. m_pDataStorage->SilentQuery( tQBuf );
  1820. return true;
  1821. };
  1822. return false;
  1823. };
  1824. DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
  1825. DBC_CAM_DATA *tVal = NULL;
  1826. if(m_bIsConnected){
  1827. char tQBuf[CTEXT];
  1828. AAssoc *result;
  1829. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  1830. #ifdef DEBUG_ALL
  1831. LOG(tQBuf);
  1832. #endif
  1833. result = m_pDataStorage->Query( tQBuf );
  1834. if( result!= NULL ) {
  1835. if( result->size() > 0 ) {
  1836. Assoc *cArr;
  1837. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  1838. if( tVal != NULL ) {
  1839. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  1840. tVal->id = ID;
  1841. Assoc::iterator it;
  1842. cArr = result->at(0);
  1843. it = cArr->find("type");
  1844. if( it != cArr->end() ){
  1845. tVal->type = atoi( it->second.c_str() );
  1846. };
  1847. it = cArr->find("online");
  1848. if( it != cArr->end() ){
  1849. tVal->online = (bool) atoi( it->second.c_str() );
  1850. };
  1851. it = cArr->find("stream_id");
  1852. if( it != cArr->end() ){
  1853. tVal->stream_id = atoi( it->second.c_str() );
  1854. };
  1855. it = cArr->find("hardware");
  1856. if( it != cArr->end() ){
  1857. strncpy( tVal->hardware, it->second.c_str(), 32 );
  1858. };
  1859. it = cArr->find("name");
  1860. if( it != cArr->end() ){
  1861. strncpy( tVal->name, it->second.c_str(), 64 );
  1862. };
  1863. it = cArr->find("status");
  1864. if( it != cArr->end() ){
  1865. tVal->status = atoi( it->second.c_str() );
  1866. };
  1867. it = cArr->find("session");
  1868. if( it != cArr->end() ){
  1869. strncpy( tVal->session, it->second.c_str(), 32 );
  1870. };
  1871. it = cArr->find("protocol");
  1872. if( it != cArr->end() ){
  1873. tVal->protocol = atoi( it->second.c_str() );
  1874. };
  1875. it = cArr->find("protocol_aux");
  1876. if( it != cArr->end() ){
  1877. tVal->protocol_aux = atoi( it->second.c_str() );
  1878. };
  1879. it = cArr->find("uart_id");
  1880. if( it != cArr->end() ){
  1881. tVal->uart_id = atoi( it->second.c_str() );
  1882. };
  1883. it = cArr->find("x");
  1884. if( it != cArr->end() ){
  1885. tVal->x = atof( it->second.c_str() );
  1886. };
  1887. it = cArr->find("y");
  1888. if( it != cArr->end() ){
  1889. tVal->y = atof( it->second.c_str() );
  1890. };
  1891. it = cArr->find("z");
  1892. if( it != cArr->end() ){
  1893. tVal->z = atof( it->second.c_str() );
  1894. };
  1895. it = cArr->find("x_rot");
  1896. if( it != cArr->end() ){
  1897. tVal->x_rot = atof( it->second.c_str() );
  1898. };
  1899. it = cArr->find("y_rot");
  1900. if( it != cArr->end() ){
  1901. tVal->y_rot = atof( it->second.c_str() );
  1902. };
  1903. it = cArr->find("z_rot");
  1904. if( it != cArr->end() ){
  1905. tVal->z_rot = atof( it->second.c_str() );
  1906. };
  1907. it = cArr->find("imager_type");
  1908. if( it != cArr->end() ){
  1909. tVal->imager_type = atoi( it->second.c_str() );
  1910. };
  1911. it = cArr->find("lens_f");
  1912. if( it != cArr->end() ){
  1913. tVal->lens_f = atoi( it->second.c_str() );
  1914. };
  1915. it = cArr->find("ptz_speed_step");
  1916. if( it != cArr->end() ){
  1917. tVal->ptz_speed_step = atof( it->second.c_str() );
  1918. };
  1919. it = cArr->find("pan_model");
  1920. if( it != cArr->end() ){
  1921. tVal->pan_model = atoi( it->second.c_str() );
  1922. };
  1923. it = cArr->find("pan_angles");
  1924. if( it != cArr->end() ){
  1925. tVal->pan_angles = atoi( it->second.c_str() );
  1926. };
  1927. it = cArr->find("tilt_model");
  1928. if( it != cArr->end() ){
  1929. tVal->tilt_model = atoi( it->second.c_str() );
  1930. };
  1931. it = cArr->find("tilt_angles");
  1932. if( it != cArr->end() ){
  1933. tVal->tilt_angles = atoi( it->second.c_str() );
  1934. };
  1935. it = cArr->find("zoom_model");
  1936. if( it != cArr->end() ){
  1937. tVal->zoom_model = atoi( it->second.c_str() );
  1938. };
  1939. it = cArr->find("zoom");
  1940. if( it != cArr->end() ){
  1941. tVal->zoom = atof( it->second.c_str() );
  1942. };
  1943. //it = cArr->find("max_range");
  1944. //if( it != cArr->end() ){
  1945. // tVal->max_range = atof( it->second.c_str() );
  1946. //};
  1947. it = cArr->find("access_login");
  1948. if( it != cArr->end() ){
  1949. strncpy( tVal->username, it->second.c_str(), 16 );
  1950. };
  1951. it = cArr->find("access_pass");
  1952. if( it != cArr->end() ){
  1953. strncpy( tVal->password, it->second.c_str(), 16 );
  1954. };
  1955. it = cArr->find("uuid");
  1956. if( it != cArr->end() ){
  1957. strncpy( tVal->uuid, it->second.c_str(), 64 );
  1958. };
  1959. it = cArr->find("mac");
  1960. if( it != cArr->end() ){
  1961. strncpy( tVal->mac, it->second.c_str(), 16 );
  1962. };
  1963. it = cArr->find("ip");
  1964. if( it != cArr->end() ){
  1965. strncpy( tVal->ip, it->second.c_str(), 16 );
  1966. };
  1967. it = cArr->find("port");
  1968. if( it != cArr->end() ){
  1969. tVal->port = atoi( it->second.c_str() );
  1970. };
  1971. it = cArr->find("onvif_access");
  1972. if( it != cArr->end() ){
  1973. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  1974. };
  1975. it = cArr->find("onvif_ctrl");
  1976. if( it != cArr->end() ){
  1977. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  1978. };
  1979. it = cArr->find("ptz_token");
  1980. if( it != cArr->end() ){
  1981. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  1982. };
  1983. it = cArr->find("trigger_mode");
  1984. if( it != cArr->end() ){
  1985. tVal->triggering_mode = atoi( it->second.c_str() );
  1986. };
  1987. it = cArr->find("auth_mode");
  1988. if( it != cArr->end() ){
  1989. tVal->auth_mode = atoi( it->second.c_str() );
  1990. };
  1991. it = cArr->find("ptz_axel");
  1992. if( it != cArr->end() ){
  1993. tVal->ptz_axel = atoi( it->second.c_str() );
  1994. };
  1995. };
  1996. };
  1997. };
  1998. deleteResult( result );
  1999. };
  2000. return tVal;
  2001. };
  2002. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  2003. uint32_t RetVal = 0;
  2004. if(m_bIsConnected){
  2005. char tQBuf[CTEXT];
  2006. AAssoc *result;
  2007. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  2008. #ifdef DEBUG_ALL
  2009. LOG(tQBuf);
  2010. #endif
  2011. result = m_pDataStorage->Query( tQBuf );
  2012. if( result!= NULL ) {
  2013. if( result->size() > 0 ) {
  2014. Assoc *cArr;
  2015. Assoc::iterator it;
  2016. cArr = result->at(0);
  2017. it = cArr->find("id");
  2018. if( it != cArr->end() ){
  2019. RetVal = atoi( it->second.c_str() );
  2020. };
  2021. };
  2022. };
  2023. deleteResult( result );
  2024. };
  2025. return RetVal;
  2026. };
  2027. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  2028. switch ( pid ) {
  2029. case 0: return strdup( m_pDbFile );
  2030. };
  2031. return NULL;
  2032. };
  2033. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  2034. if(m_bIsConnected) {
  2035. char tQBuf[MAX_PATH_LEN];
  2036. char tIpBuf[CTEXT];
  2037. char *tPos;
  2038. char *tPos2;
  2039. tPos = strstr(Addr,"//");
  2040. if( tPos != NULL ) {
  2041. tPos2 = strstr( tPos + 2,"/");
  2042. if( tPos2 != NULL ) {
  2043. int len;
  2044. len = tPos2 - tPos - 2;
  2045. strncpy(tIpBuf,tPos + 2,len);
  2046. tIpBuf[len] = (char)0;
  2047. } else {
  2048. LOG_ERROR("Pos 2 - err");
  2049. tIpBuf[0] = (char)0;
  2050. };
  2051. } else {
  2052. LOG_ERROR("Pos 0 - err");
  2053. tIpBuf[0] = (char)0;
  2054. };
  2055. sprintf(tQBuf,"UPDATE cameras SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  2056. Addr,
  2057. tIpBuf,
  2058. CamID
  2059. );
  2060. #ifdef DEBUG_ALL
  2061. LOG(tQBuf);
  2062. #endif
  2063. m_pDataStorage->SilentQuery( tQBuf );
  2064. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  2065. #ifdef DEBUG_ALL
  2066. LOG(tQBuf);
  2067. #endif
  2068. m_pDataStorage->SilentQuery( tQBuf );
  2069. return true;
  2070. };
  2071. return false;
  2072. };
  2073. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  2074. if(m_bIsConnected) {
  2075. uint32_t cID = getCamId( uuid );
  2076. if ( cID ) {
  2077. return updateIPCam( cID, Addr );
  2078. };
  2079. };
  2080. return false;
  2081. };
  2082. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  2083. if(m_bIsConnected) {
  2084. uint32_t cID = getCamId( uuid );
  2085. if ( cID ) {
  2086. return updateLocalCam( cID );
  2087. };
  2088. };
  2089. return false;
  2090. };
  2091. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  2092. if(m_bIsConnected) {
  2093. char tQBuf[MAX_PATH_LEN];
  2094. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  2095. #ifdef DEBUG
  2096. LOG(tQBuf);
  2097. #endif
  2098. m_pDataStorage->SilentQuery( tQBuf );
  2099. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  2100. #ifdef DEBUG
  2101. LOG(tQBuf);
  2102. #endif
  2103. m_pDataStorage->SilentQuery( tQBuf );
  2104. return true;
  2105. };
  2106. return false;
  2107. };
  2108. bool sqliteDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){
  2109. if( m_bIsConnected ) {
  2110. char tQBuf[MAX_PATH_LEN];
  2111. CSTR tES = m_pDataStorage->escapeString( Serial );
  2112. if( tES ) {
  2113. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID );
  2114. free( tES );
  2115. #ifdef DEBUG_ALL
  2116. LOG(tQBuf);
  2117. #endif
  2118. m_pDataStorage->SilentQuery( tQBuf );
  2119. return true;
  2120. };
  2121. };
  2122. return false;
  2123. };
  2124. bool sqliteDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){
  2125. if( m_bIsConnected ) {
  2126. char tQBuf[MAX_PATH_LEN];
  2127. CSTR tES = m_pDataStorage->escapeString( version );
  2128. if( tES ) {
  2129. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID );
  2130. free( tES );
  2131. #ifdef DEBUG_ALL
  2132. LOG(tQBuf);
  2133. #endif
  2134. m_pDataStorage->SilentQuery( tQBuf );
  2135. return true;
  2136. };
  2137. };
  2138. return false;
  2139. };
  2140. #undef LOG
  2141. #undef LOG_ERROR