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.

3213 lines
118 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
​
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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. #include "../../components/base/basicEvent.h"
  3. #define LOG( X ) m_pLog->Log( "SDBC", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG2w34[256];\
  7. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "SDBC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "SDBC", this, _LOG_ERROR, X );
  12. #endif
  13. sqliteDBC *sqliteDBC::_instance = NULL;
  14. /**
  15. * Returns the single instance of the object.
  16. */
  17. sqliteDBC *sqliteDBC::getInstance( config_setting_t * Settings ) {
  18. // check if the instance has been created yet
  19. if( 0 == _instance ){
  20. // if not, then create it
  21. _instance = new sqliteDBC( Settings );
  22. };
  23. // return the single instance
  24. return _instance;
  25. };
  26. sqliteDBC::sqliteDBC( config_setting_t * Settings ):dataBinder(){
  27. m_pLog = basicLogCollection::getInstance();
  28. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DATABASE" );
  29. m_bIsConnected = false;
  30. m_pDataStorage = NULL;
  31. m_pDataMemory = NULL;
  32. m_pDataReports = NULL;
  33. m_nServerID = 1;
  34. };
  35. sqliteDBC::~sqliteDBC(){
  36. if ( m_pDataStorage ) delete( m_pDataStorage );
  37. if ( m_pDataMemory ) delete( m_pDataMemory );
  38. if ( m_pDataReports ) delete( m_pDataReports );
  39. _instance = NULL;
  40. };
  41. bool sqliteDBC::connect( void ){
  42. if ( m_pSettings == NULL) return false;
  43. bool bRecreateMemStructures = false;
  44. config_setting_t * pSetting;
  45. const char * tBuff;
  46. //Disconnect if Connected
  47. m_bIsConnected = false;
  48. char web_access_user[64];
  49. char web_access_group[64];
  50. //Apply New Config
  51. #ifdef DEBUG
  52. LOG( "Reading DB Settings ... " );
  53. #endif
  54. if(!config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char**)&tBuff )){
  55. pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
  56. sprintf( m_pDbFile, "data/mysqlitedb.db" );
  57. config_setting_set_string( pSetting, m_pDbFile );
  58. basicSettingsProvider::getInstance()->compromise();
  59. LOG("Data Storage not set - using default");
  60. } else {
  61. sprintf( m_pDbFile, tBuff );
  62. };
  63. if(!config_setting_lookup_string(m_pSettings, "REPORT_FILE_NAME", (const char**)&tBuff )){
  64. pSetting = config_setting_add(m_pSettings, "REPORT_FILE_NAME", CONFIG_TYPE_STRING);
  65. sprintf( m_pDbReport, "data/reports.db" );
  66. config_setting_set_string( pSetting, m_pDbReport );
  67. basicSettingsProvider::getInstance()->compromise();
  68. LOG("Reports Storage not set - using default");
  69. } else {
  70. sprintf( m_pDbReport, tBuff );
  71. };
  72. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char**)&tBuff )){
  73. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
  74. sprintf( web_access_user, "www-data" );
  75. config_setting_set_string( pSetting, web_access_user );
  76. basicSettingsProvider::getInstance()->compromise();
  77. LOG("Web Access User not set - using default");
  78. } else {
  79. sprintf( web_access_user, tBuff );
  80. };
  81. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char**)&tBuff )){
  82. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
  83. sprintf( web_access_group, "www-data" );
  84. config_setting_set_string( pSetting, web_access_group );
  85. basicSettingsProvider::getInstance()->compromise();
  86. LOG("Web Access Group not set - using default");
  87. } else {
  88. sprintf( web_access_group, tBuff );
  89. };
  90. if( !fileExist( DB_SHMEM_POINT )) {
  91. bRecreateMemStructures = true;
  92. mkdir( DB_SHMEM_POINT, 0775 );// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  93. chown( DB_SHMEM_POINT, web_access_user, web_access_group ); //To make Accessed from PHP
  94. chmod( DB_SHMEM_POINT, 0775 );
  95. };
  96. char tB[128];
  97. if (!config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **)&tBuff)) {
  98. pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
  99. tBuff = tB;
  100. sprintf( tB, "mem_db.db" );
  101. config_setting_set_string( pSetting, tB );
  102. basicSettingsProvider::getInstance()->compromise();
  103. LOG("Memory Storage not set - using default");
  104. };
  105. sprintf( m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff );
  106. basicSettingsProvider::getInstance()->validate();
  107. // Kill Connection if any
  108. if ( m_pDataStorage ) delete( m_pDataStorage );
  109. if ( m_pDataMemory ) delete( m_pDataMemory );
  110. if ( m_pDataReports ) delete( m_pDataReports );
  111. #ifdef DEBUG
  112. LOG( m_pDbFile );
  113. #endif
  114. // Create New Connections
  115. m_pDataStorage = new SQLite_Connector();
  116. if ( m_pDataStorage ) {
  117. if( m_pDataStorage->Connect( m_pDbFile, 0, true ) ){
  118. #ifdef DEBUG
  119. LOG("DB File Opened");
  120. #endif
  121. chown( m_pDbFile, web_access_user, web_access_group );
  122. chmod( m_pDbFile, 0664 );
  123. } else{
  124. LOG_ERROR("Cannot Connect to DB File");
  125. delete( m_pDataStorage );
  126. m_pDataStorage = NULL;
  127. LOG("Storage Deleted!");
  128. return false;
  129. };
  130. };
  131. #ifdef DEBUG
  132. LOG( m_pDbMemory );
  133. #endif
  134. if( !fileExist( m_pDbMemory )) {
  135. bRecreateMemStructures = true;
  136. //Always recreate
  137. unlink( m_pDbMemory );
  138. };
  139. m_pDataMemory = new SQLite_Connector();
  140. if ( m_pDataMemory ) {
  141. if( m_pDataMemory->Connect( m_pDbMemory, 0, true ) ){
  142. #ifdef DEBUG
  143. LOG("DB Memory Opened");
  144. #endif
  145. chown( m_pDbMemory, web_access_user, web_access_group );
  146. chmod( m_pDbMemory, 0664 );
  147. m_pDataMemory->SilentQuery("PRAGMA journal_mode=WAL");
  148. //Recreate at Register Server
  149. //if ( bRecreateMemStructures ) initMemDB();
  150. } else{
  151. LOG_ERROR("Cannot Connect to DB Memory");
  152. delete( m_pDataStorage );
  153. delete( m_pDataMemory );
  154. m_pDataStorage = NULL;
  155. m_pDataMemory = NULL;
  156. return false;
  157. };
  158. };
  159. m_pDataReports = new SQLite_Connector();
  160. if ( m_pDataReports ) {
  161. if( m_pDataReports->Connect( m_pDbReport, 0, true ) ){
  162. #ifdef DEBUG
  163. LOG("Report File Opened");
  164. #endif
  165. chown( m_pDbReport, web_access_user, web_access_group );
  166. chmod( m_pDbReport, 0664 );
  167. } else{
  168. LOG_ERROR("Cannot Connect to DB File");
  169. delete( m_pDataReports );
  170. m_pDataReports = NULL;
  171. LOG("Storage Deleted!");
  172. return false;
  173. };
  174. };
  175. m_bIsConnected = true;
  176. return m_bIsConnected;
  177. };
  178. void sqliteDBC::initMemDB( uint32_t sid ){
  179. m_pDataMemory->SilentQuery("DROP TABLE \"nodes_qcq\"");
  180. m_pDataMemory->SilentQuery("CREATE TABLE \"nodes_qcq\" ( \
  181. \"id\" INTEGER NOT NULL,\
  182. \"node_id\" INTEGER NOT NULL,\
  183. \"cmd\" INTEGER NOT NULL,\
  184. \"status\" INTEGER DEFAULT 0,\
  185. \"parameter\" TEXT,\
  186. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  187. );");
  188. m_pDataMemory->SilentQuery("DROP TABLE \"servers\"");
  189. m_pDataMemory->SilentQuery("CREATE TABLE \"servers\" (\
  190. \"id\" INTEGER NOT NULL,\
  191. \"ts\" INTEGER DEFAULT 0,\
  192. \"pid\" INTEGER DEFAULT 0,\
  193. \"hw_info\" TEXT,\
  194. \"os_info\" TEXT,\
  195. \"cpu_cores\" INTEGER DEFAULT 1,\
  196. \"stat_cpu1\" REAL DEFAULT 0.0,\
  197. \"stat_cpu5\" REAL DEFAULT 0.0,\
  198. \"stat_cpu15\" REAL DEFAULT 0.0,\
  199. \"stat_mem\" REAL DEFAULT 0.0,\
  200. \"stat_disk\" REAL DEFAULT 0.0,\
  201. \"val_mem_tottal\" INTEGER DEFAULT 0,\
  202. \"val_mem_current\" INTEGER DEFAULT 0,\
  203. \"val_disk_tottal\" INTEGER DEFAULT 0,\
  204. \"val_disk_current\" INTEGER DEFAULT 0,\
  205. \"temp0\" REAL DEFAULT 0.0,\
  206. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  207. );");
  208. char tB[128];
  209. sprintf( tB, "INSERT INTO `servers` (`id`,`ts`) VALUES (%d, strftime('%%s') )", sid );
  210. m_pDataMemory->SilentQuery( tB );
  211. };
  212. DBC_PUSHED_CMD *sqliteDBC::getNodePushedCMD( uint32_t NodeID ){
  213. DBC_PUSHED_CMD * RetVal = NULL;
  214. if ( m_bIsConnected ) {
  215. char QS[MAX_PATH];
  216. AAssoc * result;
  217. sprintf( QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID );
  218. #ifdef DEBUG_ALL
  219. LOG( QS );
  220. #endif
  221. result = m_pDataMemory->Query( QS );
  222. if ( result ) {
  223. if( result->size() > 0 ) {
  224. RetVal = (DBC_PUSHED_CMD*)malloc(sizeof(DBC_PUSHED_CMD));
  225. if( RetVal ) {
  226. Assoc * cArr;
  227. Assoc::iterator it;
  228. memset( RetVal, 0, sizeof(DBC_PUSHED_CMD));
  229. cArr = result->at(0);
  230. it = cArr->find("id");
  231. if( it != cArr->end() ){
  232. RetVal->id = atoi( it->second.c_str() );
  233. };
  234. it = cArr->find("cmd");
  235. if( it != cArr->end() ){
  236. RetVal->CMD = atoi( it->second.c_str() );
  237. };
  238. it = cArr->find("parameter");
  239. if( it != cArr->end() ){
  240. strncpy( RetVal->PARAM, it->second.c_str(), 256 );
  241. RetVal->PARAM[255] = (char)0;
  242. };
  243. };
  244. #ifdef DEBUG_ALL
  245. } else {
  246. LOG("No Rows Fetched!");
  247. #endif
  248. };
  249. };
  250. m_pDataMemory->deleteResult( result );
  251. };
  252. return RetVal;
  253. };
  254. bool sqliteDBC::updateNodePushedCMD( uint32_t CmdID, uint8_t status ){
  255. if ( m_bIsConnected ) {
  256. char QS[MAX_PATH];
  257. sprintf( QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID );
  258. return m_pDataMemory->SilentQuery( QS );
  259. };
  260. return false;
  261. };
  262. bool sqliteDBC::cleanNodePushedCMD( void ){
  263. if ( m_bIsConnected ) {
  264. return m_pDataMemory->SilentQuery( "DELETE FROM `nodes_qcq` WHERE `status` > 200" );
  265. };
  266. return false;
  267. };
  268. void sqliteDBC::deleteNIF( uint32_t NifID ){
  269. if ( m_bIsConnected ) {
  270. char tQBuf[MAX_PATH_LEN];
  271. sprintf( tQBuf, "", NifID );
  272. #ifdef DEBUG_ALL
  273. LOG( tQBuf );
  274. #endif
  275. m_pDataStorage->SilentQuery( tQBuf );
  276. };
  277. };
  278. void sqliteDBC::deleteNIS( uint32_t NisID ){
  279. };
  280. void sqliteDBC::deleteNetwork( uint32_t NetID ){
  281. };
  282. void sqliteDBC::deleteResource( uint32_t ResourceID ){
  283. };
  284. void sqliteDBC::deleteSensor( uint32_t SensorID ){
  285. };
  286. void sqliteDBC::deleteUART( uint32_t UARTID ){
  287. };
  288. bool sqliteDBC::updateServerUtilisation( DBC_SYS_LOAD_INFO *ptr ){
  289. if( m_bIsConnected ) {
  290. char tQBuf[MAX_PATH_LEN];
  291. 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",
  292. ptr->CPU_CORES,
  293. ptr->cpu1,
  294. ptr->cpu5,
  295. ptr->cpu15,
  296. ptr->mem,
  297. ptr->disk,
  298. ptr->temperature,
  299. ptr->T_DISK,
  300. ptr->C_DISK,
  301. ptr->T_MEM,
  302. ptr->C_MEM,
  303. m_nServerID
  304. );
  305. #ifdef DEBUG_ALL
  306. LOG(tQBuf);
  307. #endif
  308. m_pDataMemory->SilentQuery( tQBuf );
  309. return true;
  310. };
  311. return false;
  312. };
  313. /********************************************************************
  314. * *
  315. * Slow DB Functions *
  316. * *
  317. ********************************************************************/
  318. bool sqliteDBC::addDeviceDraw( uint32_t DBID, uint8_t type, uint8_t lrf, void *th_data, uint32_t th_size ){
  319. char tQBuf[1024];
  320. if ( type == 0 ) {
  321. CSTR uid = getUniqID_MD5();
  322. if( uid == NULL ) return false;
  323. sprintf( tQBuf, "data/device_draws/%s.raw", uid );
  324. FILE *fd = fopen( tQBuf, "wb+" );
  325. if( fd!= NULL ) {
  326. uint32_t sisi = th_size >> 2;
  327. fwrite( &sisi, 4, 1, fd );
  328. fwrite( th_data, 4, sisi, fd );
  329. fclose( fd );
  330. sprintf( tQBuf, "INSERT INTO `lrf_draw`(`node_id`,`lrf_id`,`type`,`ts`,`data`) VALUES(%d,%d,%d,STRFTIME('%%s'),'%s')",
  331. DBID, lrf, type, uid );
  332. free ( uid );
  333. #ifdef DEBUG
  334. LOG(tQBuf);
  335. #endif
  336. m_pDataStorage->SilentQuery( tQBuf );
  337. return true;
  338. };
  339. free( uid );
  340. } else {
  341. LOG("No RAW DATA!");
  342. };
  343. return false;
  344. };
  345. uint32_t sqliteDBC::addEvent( DBC_EVT_RECORD *ptr ){
  346. if ( ptr == NULL ) return 0;
  347. if (m_bIsConnected) {
  348. char tQBuf[1024];
  349. switch ( ptr->type ) {
  350. case EVT_FLT_LATF_CHK:
  351. case EVT_FLT_LATF_UP_DETEC:
  352. case EVT_FLT_LATF_BT_DETEC:
  353. case EVT_FLT_LATF_TRP_DET:
  354. case EVT_FLT_LATF_TRP_UPDT:
  355. case EVT_FLT_LATF_SUSP_START:
  356. case EVT_FLT_LATF_SUSP_NAK:
  357. case EVT_FLT_LATF_REGULAR:
  358. case EVT_FLT_LATF_DISABLED:
  359. case EVT_FLT_LATF_SUSP_DET:
  360. case EVT_FLT_LATF_DIFF_DET:
  361. case EVT_LRF_START_SESSION:
  362. case EVT_LRF_END_SESSION_TO:
  363. case EVT_LRF_END_SESSION_MA:
  364. case EVT_STUDY:
  365. case EVT_BUSY:
  366. case EVT_FLIP:
  367. case EVT_READY:{
  368. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)",
  369. ptr->node_id,
  370. ptr->uuid,
  371. ptr->ts_s,
  372. ptr->ts_ms,
  373. ptr->type
  374. );
  375. break;
  376. };
  377. case EVT_ROI_ACTIVE:
  378. case EVT_TARGET_3D_WHITE:
  379. case EVT_TARGET_3D_BLACK:{
  380. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`range_digital`,`azimuth`,`elevation`,`range`,`area`,`lat`,`lon`,`alt`)" \
  381. " VALUES(%d,'%s',%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  382. ptr->node_id,
  383. ptr->uuid,
  384. ptr->ts_s,
  385. ptr->ts_ms,
  386. ptr->type,
  387. ptr->range_digital,
  388. ptr->az,
  389. ptr->el,
  390. ptr->ra,
  391. ptr->area,
  392. ptr->lat,
  393. ptr->lon,
  394. ptr->alt
  395. );
  396. break;
  397. };
  398. case EVT_TARGET_2D_WHITE:
  399. case EVT_TARGET_2D_BLACK:{
  400. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)" \
  401. " VALUES(%d,'%s',%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  402. ptr->node_id,
  403. ptr->uuid,
  404. ptr->ts_s,
  405. ptr->ts_ms,
  406. ptr->type,
  407. ptr->range_digital,
  408. ptr->az,
  409. ptr->ra,
  410. ptr->area,
  411. ptr->lat,
  412. ptr->lon,
  413. ptr->alt
  414. );
  415. break;
  416. };
  417. default: return 0;
  418. };
  419. LOG( tQBuf );
  420. return ptr->id = m_pDataReports->QueryID( tQBuf );
  421. };
  422. return 0;
  423. };
  424. bool sqliteDBC::addIPCam( OBSERVER_RESULT *tPtr ){
  425. if(m_bIsConnected) {
  426. char tQBuf[1024];
  427. char tIpBuf[32];
  428. char tProto[32];
  429. char tPath[CTEXT];
  430. char *tPos;
  431. char *tPos2;
  432. uint16_t tPort = 0;
  433. memcpy( tProto, tPtr->XAddrs, 5 );
  434. tProto[5] = (char)0;
  435. if( tProto[4] == ':') tProto[4] = (char)0;
  436. memset( tPath, 0, CTEXT );
  437. tPos = strstr(tPtr->XAddrs,"//");
  438. if( tPos != NULL ) {
  439. tPos2 = strstr( tPos + 2,":");
  440. if( tPos2 != NULL ) {
  441. LOG("IP Port Defined!");
  442. int len, len2, len3;
  443. len = tPos2 - tPos - 2;
  444. strncpy( tIpBuf, tPos + 2, len);
  445. tIpBuf[len] = (char)0;
  446. tPort = atoi( tPos2 + 1 );
  447. strncpy( tPath, tPos + 2 + len, CTEXT );
  448. } else {
  449. tPos2 = strstr( tPos + 2,"/");
  450. if( tPos2 != NULL ) {
  451. int len, len2, len3;
  452. len = tPos2 - tPos - 2;
  453. strncpy( tIpBuf, tPos + 2, len);
  454. tIpBuf[len] = (char)0;
  455. strncpy( tPath, tPos + 2 + len, CTEXT );
  456. } else {
  457. tPos2 = strstr( tPos + 2,"/");
  458. LOG_ERROR("Pos 2 - err");
  459. tIpBuf[0] = (char)0;
  460. };
  461. };
  462. } else {
  463. LOG_ERROR("Pos 0 - err");
  464. tIpBuf[0] = (char)0;
  465. };
  466. if ( tPort ) {
  467. sprintf(tQBuf, "INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`port`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',%d,12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s', `port`=%d",
  468. tPtr->RefID,
  469. //tPtr->XAddrs,
  470. tPath,
  471. tIpBuf,
  472. tPort,
  473. tPtr->MAC,
  474. tPtr->Hardware,
  475. tPtr->Name,
  476. tProto,
  477. //tPtr->XAddrs,
  478. tIpBuf,
  479. tPort
  480. );
  481. } else {
  482. 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'",
  483. tPtr->RefID,
  484. //tPtr->XAddrs,
  485. tPath,
  486. tIpBuf,
  487. tPtr->MAC,
  488. tPtr->Hardware,
  489. tPtr->Name,
  490. tProto,
  491. //tPtr->XAddrs,
  492. tIpBuf
  493. );
  494. }
  495. #ifdef DEBUG
  496. LOG(tQBuf);
  497. #endif
  498. m_pDataStorage->SilentQuery( tQBuf );
  499. return true;
  500. };
  501. return false;
  502. };
  503. uint32_t sqliteDBC::addLocalCam( CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den ){
  504. uint32_t RetVal = 0;
  505. if( m_bIsConnected ) {
  506. char tQBuf[1024];
  507. char tM5[33];
  508. CSTR tMD5 = MD5String( path );
  509. if( tMD5 != NULL ) {
  510. memcpy( tM5, tMD5, 33 );
  511. tM5[32]=(char)0;
  512. free( tMD5 );
  513. 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",
  514. tM5,
  515. hardware,
  516. name,
  517. path
  518. );
  519. #ifdef DEBUG_ALL
  520. LOG(tQBuf);
  521. #endif
  522. uint32_t camID = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  523. if ( camID == 0 ) {
  524. camID = getCamId( tM5 );
  525. };
  526. 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",
  527. camID, name, tM5, width, height, path, fps, width, height, fps );
  528. #ifdef DEBUG_EXTRA
  529. LOG(tQBuf);
  530. #endif
  531. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  532. if( RetVal == 0 ){
  533. RetVal = getMediaStreamID( camID, tM5 );
  534. };
  535. };
  536. };
  537. return RetVal;
  538. };
  539. uint32_t sqliteDBC::addLocalNetwork( uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast ){
  540. uint32_t RetVal = 0;
  541. if( m_bIsConnected ) {
  542. char tQBuf[MAX_PATH_LEN];
  543. 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'",
  544. NIF_ID,
  545. net_type,
  546. address,
  547. gateway,
  548. mask,
  549. broadcast,
  550. address,
  551. gateway,
  552. mask,
  553. broadcast
  554. );
  555. #ifdef DEBUG_ALL
  556. LOG(tQBuf);
  557. #endif
  558. RetVal = m_pDataStorage->QueryID( tQBuf );
  559. // On Update IF data no need to return ID
  560. //if( RetVal == 0 ) {
  561. // RetVal = getNetworkInterfaceID( tKey );
  562. //};
  563. };
  564. return RetVal;
  565. };
  566. uint32_t sqliteDBC::addLocalNetworkInfrastructure( uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME ){
  567. uint32_t RetVal = 0;
  568. if( m_bIsConnected ) {
  569. char tQBuf[MAX_PATH_LEN];
  570. bool ena_routing = true;
  571. if( OUTBOUND_NIF == 0 ){
  572. ena_routing = false;
  573. };
  574. if( ena_dhcp ){
  575. if ( BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL ) {
  576. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  577. return 0;
  578. };
  579. 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'",
  580. NIF_ID,
  581. ena_routing,
  582. OUTBOUND_NIF,
  583. BRANG,
  584. ERANG,
  585. MASK,
  586. LEASE_TIME,
  587. ena_routing,
  588. OUTBOUND_NIF,
  589. BRANG,
  590. ERANG,
  591. MASK,
  592. LEASE_TIME
  593. );
  594. } else {
  595. if( ena_routing ){
  596. 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",
  597. NIF_ID,
  598. OUTBOUND_NIF,
  599. OUTBOUND_NIF
  600. );
  601. } else {
  602. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  603. return 0;
  604. };
  605. };
  606. #ifdef DEBUG_ALL
  607. LOG(tQBuf);
  608. #endif
  609. RetVal = m_pDataStorage->QueryID( tQBuf );
  610. // On Update IF data no need to return ID
  611. if( RetVal == 0 ) {
  612. RetVal = getNetworkInfrastructureID( NIF_ID );
  613. };
  614. };
  615. return RetVal;
  616. };
  617. uint32_t sqliteDBC::addLocalNIF( CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id ){
  618. uint32_t RetVal = 0;
  619. if( m_bIsConnected ) {
  620. char tQBuf[MAX_PATH_LEN];
  621. char * tKey = MD5String( device );
  622. if( tKey != NULL ) {
  623. 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'",
  624. dtype,
  625. tKey,
  626. device,
  627. mac,
  628. dmode,
  629. aux_id,
  630. dmode,
  631. aux_id,
  632. mac
  633. );
  634. #ifdef DEBUG_ALL
  635. LOG(tQBuf);
  636. #endif
  637. RetVal = m_pDataStorage->QueryID( tQBuf );
  638. if( RetVal == 0 ) {
  639. RetVal = getNIF_ID( tKey );
  640. };
  641. free( tKey );
  642. };
  643. };
  644. return RetVal;
  645. };
  646. 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 ){
  647. uint32_t RetVal = 0;
  648. if( m_bIsConnected ) {
  649. char tQBuf[MAX_PATH_LEN];
  650. 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`",
  651. m_nServerNodeID,
  652. utype,
  653. speed,
  654. bit,
  655. parity,
  656. stop_bit,
  657. mode,
  658. port,
  659. PATH
  660. );
  661. #ifdef DEBUG_ALL
  662. LOG(tQBuf);
  663. #endif
  664. RetVal = m_pDataStorage->QueryID( tQBuf );
  665. if( RetVal == 0 ) {
  666. RetVal = getUartID( m_nServerNodeID, utype, PATH );
  667. };
  668. };
  669. return RetVal;
  670. };
  671. uint32_t sqliteDBC::addLocalWiFi( uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id ){
  672. uint32_t RetVal = 0;
  673. if( m_bIsConnected ) {
  674. char tQBuf[MAX_PATH_LEN];
  675. 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",
  676. NIF_ID,
  677. mode,
  678. router_id,
  679. SSID,
  680. password,
  681. encryption,
  682. mode,
  683. router_id,
  684. SSID,
  685. password,
  686. encryption
  687. );
  688. #ifdef DEBUG_ALL
  689. LOG(tQBuf);
  690. #endif
  691. RetVal = m_pDataStorage->QueryID( tQBuf );
  692. if( RetVal == 0 ) {
  693. RetVal = getWiFi_ID( NIF_ID );
  694. };
  695. };
  696. return RetVal;
  697. };
  698. bool sqliteDBC::cleanLRFMask( uint32_t maskID, bool deleteMask ){
  699. if(m_bIsConnected) {
  700. char tQBuf[MAX_PATH_LEN];
  701. sprintf( tQBuf, "DELETE FROM `lrf_mask_trace_zones` WHERE `zone_id` IN ( SELECT `id` FROM `lrf_mask_traces` WHERE `mask_id`=%d )", maskID );
  702. #ifdef DEBUG_EXTRA
  703. LOG(tQBuf);
  704. #endif
  705. m_pDataStorage->SilentQuery( tQBuf );
  706. sprintf( tQBuf, "DELETE FROM `lrf_mask_traces` WHERE `mask_id`=%d", maskID );
  707. #ifdef DEBUG_EXTRA
  708. LOG(tQBuf);
  709. #endif
  710. m_pDataStorage->SilentQuery( tQBuf );
  711. sprintf( tQBuf, "DELETE FROM `lrf_mask_vertex_points` WHERE `vertex_id` IN ( SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d )", maskID );
  712. #ifdef DEBUG_EXTRA
  713. LOG(tQBuf);
  714. #endif
  715. m_pDataStorage->SilentQuery( tQBuf );
  716. sprintf( tQBuf, "DELETE FROM `lrf_mask_vertexes` WHERE `mask_id`=%d", maskID );
  717. #ifdef DEBUG_EXTRA
  718. LOG(tQBuf);
  719. #endif
  720. m_pDataStorage->SilentQuery( tQBuf );
  721. //Delete Mask Itself!
  722. if ( deleteMask ) {
  723. sprintf( tQBuf, "DELETE FROM `lrf_masks` WHERE `id`=%d", maskID );
  724. #ifdef DEBUG_EXTRA
  725. LOG(tQBuf);
  726. #endif
  727. m_pDataStorage->SilentQuery( tQBuf );
  728. };
  729. return true;
  730. };
  731. return false;
  732. };
  733. bool sqliteDBC::cleanResources( bool all ){
  734. if(m_bIsConnected) {
  735. char tQBuf[MAX_PATH_LEN];
  736. if( all ) {
  737. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID );
  738. } else {
  739. 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 );
  740. };
  741. #ifdef DEBUG_ALL
  742. LOG(tQBuf);
  743. #endif
  744. m_pDataStorage->SilentQuery( tQBuf );
  745. return true;
  746. };
  747. return false;
  748. };
  749. bool sqliteDBC::wipeServerResources(){
  750. if( m_bIsConnected ) {
  751. char tQBuf[MAX_PATH_LEN];
  752. sprintf( tQBuf, "DELETE FROM `resources` WHERE `node_id`=%d", m_nServerNodeID );
  753. #ifdef DEBUG_ALL
  754. LOG(tQBuf);
  755. #endif
  756. m_pDataStorage->SilentQuery( tQBuf );
  757. return true;
  758. };
  759. return false;
  760. };
  761. uint32_t sqliteDBC::addLRFMask( uint32_t devID, uint8_t lrfID, uint8_t layerID ){
  762. uint32_t RetVal = 0;
  763. if( m_bIsConnected ) {
  764. char tQBuf[MAX_PATH_LEN];
  765. sprintf( tQBuf, "INSERT INTO `lrf_masks`(`sensor_id`,`lrf_id`,`layer`) VALUES(%d,%d,%d) ON CONFLICT(`sensor_id`,`lrf_id`,`layer`) DO UPDATE SET `layer`=%d",
  766. devID,
  767. lrfID,
  768. layerID,
  769. layerID
  770. );
  771. #ifdef DEBUG
  772. LOG(tQBuf);
  773. #endif
  774. RetVal = m_pDataStorage->QueryID( tQBuf );
  775. if( RetVal == 0 ) {
  776. RetVal = getLRFMaskID( devID, lrfID, layerID );
  777. };
  778. };
  779. return RetVal;
  780. };
  781. uint32_t sqliteDBC::addLRFMaskPolygone( uint32_t maskID, uint8_t r, uint8_t g, uint8_t b ){
  782. uint32_t RetVal = 0;
  783. if( m_bIsConnected ) {
  784. char tQBuf[MAX_PATH_LEN];
  785. sprintf( tQBuf, "INSERT INTO `lrf_mask_vertexes`(`mask_id`,`r`,`g`,`b`) VALUES(%d,%d,%d,%d)",
  786. maskID,
  787. r,
  788. g,
  789. b
  790. );
  791. #ifdef DEBUG
  792. LOG(tQBuf);
  793. #endif
  794. RetVal = m_pDataStorage->QueryID( tQBuf );
  795. };
  796. return RetVal;
  797. };
  798. bool sqliteDBC::addLRFMaskPolygonePoint( uint32_t polyID, int16_t x, int16_t y ){
  799. if( m_bIsConnected ) {
  800. char tQBuf[MAX_PATH_LEN];
  801. sprintf( tQBuf, "INSERT INTO `lrf_mask_vertex_points`(`vertex_id`,`x`,`y`) VALUES(%d,%d,%d)",
  802. polyID,
  803. x,
  804. y
  805. );
  806. #ifdef DEBUG
  807. LOG(tQBuf);
  808. #endif
  809. m_pDataStorage->SilentQuery( tQBuf );
  810. return true;
  811. };
  812. return false;
  813. };
  814. uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){
  815. uint32_t RetVal = 0;
  816. if( m_bIsConnected ){
  817. char tQBuf[2048];
  818. sprintf( tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName );
  819. CSTR tMD5 = MD5String( tQBuf );
  820. if( tMD5 ) {
  821. 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",
  822. m_nServerNodeID,
  823. type,
  824. tMD5,
  825. auxID,
  826. devName,
  827. auxID
  828. );
  829. free( tMD5 );
  830. #ifdef DEBUG_ALL
  831. LOG(tQBuf);
  832. #endif
  833. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  834. if( RetVal == 0 ) {
  835. RetVal = getResourceID( m_nServerNodeID, type, auxID );
  836. };
  837. };
  838. };
  839. return RetVal;
  840. };
  841. bool sqliteDBC::addVideoEvent( uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms ){
  842. if( m_bIsConnected ){
  843. char tQBuf[2048];
  844. sprintf( tQBuf, "INSERT INTO `events_2_videos`(`evt_id`,`video_id`,`frame`,`tss`,`tms`) VALUES(%d,%d,%d,%d,%d)",
  845. evtID,
  846. videoID,
  847. frame,
  848. tss,
  849. tms
  850. );
  851. #ifdef DEBUG
  852. LOG(tQBuf);
  853. #endif
  854. m_pDataReports->SilentQuery( tQBuf );
  855. };
  856. return false;
  857. };
  858. uint32_t sqliteDBC::addVideoRecord( uint32_t streamID, CSTR md5, CSTR ext, uint32_t tss, uint32_t tms, uint32_t fps ){
  859. uint32_t RetVal = 0;
  860. if( m_bIsConnected ){
  861. char tQBuf[2048];
  862. sprintf( tQBuf, "INSERT INTO `videos`(`media_stream_id`,`md5`,`uts`,`uts_ms`,`ext`,`fps`) VALUES(%d,'%s',%d,%d,'%s',%d)",
  863. streamID,
  864. md5,
  865. tss,
  866. tms,
  867. ext,
  868. fps
  869. );
  870. #ifdef DEBUG
  871. LOG(tQBuf);
  872. #endif
  873. RetVal = (uint32_t) m_pDataReports->QueryID( tQBuf );
  874. };
  875. return RetVal;
  876. };
  877. DBC_MODULE_EMBED_VIDEO_DATA *sqliteDBC::getEmbedVideoData( uint32_t ID ){
  878. DBC_MODULE_EMBED_VIDEO_DATA *tPtr = NULL;
  879. if( m_bIsConnected ) {
  880. char tQBuf[MAX_PATH_LEN];
  881. AAssoc *result;
  882. sprintf( tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID );
  883. #ifdef DEBUG_ALL
  884. LOG(tQBuf);
  885. #endif
  886. result = m_pDataStorage->Query( tQBuf );
  887. if( result!= NULL ) {
  888. if( result->size() > 0 ) {
  889. Assoc *cArr;
  890. tPtr = (DBC_MODULE_EMBED_VIDEO_DATA*)malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  891. if( tPtr!= NULL ) {
  892. Assoc::iterator it;
  893. memset(tPtr,0,sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  894. cArr = result->at(0);
  895. it = cArr->find("output_mode");
  896. if( it != cArr->end() ){
  897. tPtr->output_mode = atoi( it->second.c_str() );
  898. };
  899. it = cArr->find("web_port");
  900. if( it != cArr->end() ){
  901. tPtr->port = atoi( it->second.c_str() );
  902. };
  903. it = cArr->find("width");
  904. if( it != cArr->end() ){
  905. tPtr->width = atoi( it->second.c_str() );
  906. };
  907. it = cArr->find("height");
  908. if( it != cArr->end() ){
  909. tPtr->height = atoi( it->second.c_str() );
  910. };
  911. it = cArr->find("compression_quality");
  912. if( it != cArr->end() ){
  913. tPtr->compression_quality = atoi( it->second.c_str() );
  914. };
  915. it = cArr->find("show_ts");
  916. if( it != cArr->end() ){
  917. tPtr->show_ts = (bool)atoi( it->second.c_str() );
  918. };
  919. it = cArr->find("ts_x");
  920. if( it != cArr->end() ){
  921. tPtr->ts_x = atoi( it->second.c_str() );
  922. };
  923. it = cArr->find("ts_y");
  924. if( it != cArr->end() ){
  925. tPtr->ts_y = atoi( it->second.c_str() );
  926. };
  927. it = cArr->find("ts_font_face");
  928. if( it != cArr->end() ){
  929. tPtr->ts_font_face = atoi( it->second.c_str() );
  930. };
  931. it = cArr->find("ts_font_scale");
  932. if( it != cArr->end() ){
  933. tPtr->ts_font_scale = atof( it->second.c_str() );
  934. };
  935. it = cArr->find("ts_font_thickness");
  936. if( it != cArr->end() ){
  937. tPtr->ts_font_thickness = atoi( it->second.c_str() );
  938. };
  939. it = cArr->find("ts_line_type");
  940. if( it != cArr->end() ){
  941. tPtr->ts_line_type = atoi( it->second.c_str() );
  942. };
  943. it = cArr->find("ts_transperant");
  944. if( it != cArr->end() ){
  945. tPtr->ts_transperant = (bool)atoi( it->second.c_str() );
  946. };
  947. it = cArr->find("show_label");
  948. if( it != cArr->end() ){
  949. tPtr->show_label = (bool)atoi( it->second.c_str() );
  950. };
  951. it = cArr->find("label_x");
  952. if( it != cArr->end() ){
  953. tPtr->label_x = atoi( it->second.c_str() );
  954. };
  955. it = cArr->find("label_y");
  956. if( it != cArr->end() ){
  957. tPtr->label_y = atoi( it->second.c_str() );
  958. };
  959. it = cArr->find("label_font_face");
  960. if( it != cArr->end() ){
  961. tPtr->label_font_face = atoi( it->second.c_str() );
  962. };
  963. it = cArr->find("label_font_scale");
  964. if( it != cArr->end() ){
  965. tPtr->label_font_scale = atof( it->second.c_str() );
  966. };
  967. it = cArr->find("label_font_thickness");
  968. if( it != cArr->end() ){
  969. tPtr->label_font_thickness = atoi( it->second.c_str() );
  970. };
  971. it = cArr->find("label_line_type");
  972. if( it != cArr->end() ){
  973. tPtr->label_line_type = atoi( it->second.c_str() );
  974. };
  975. it = cArr->find("label_transperant");
  976. if( it != cArr->end() ){
  977. tPtr->label_transperant = (bool) atoi( it->second.c_str() );
  978. };
  979. it = cArr->find("show_logo");
  980. if( it != cArr->end() ){
  981. tPtr->show_logo = (bool) atoi( it->second.c_str() );
  982. };
  983. it = cArr->find("logo_w");
  984. if( it != cArr->end() ){
  985. tPtr->logo_w = atoi( it->second.c_str() );
  986. };
  987. it = cArr->find("logo_h");
  988. if( it != cArr->end() ){
  989. tPtr->logo_h = atoi( it->second.c_str() );
  990. };
  991. it = cArr->find("logo_x");
  992. if( it != cArr->end() ){
  993. tPtr->logo_x = atoi( it->second.c_str() );
  994. };
  995. it = cArr->find("logo_y");
  996. if( it != cArr->end() ){
  997. tPtr->logo_y = atoi( it->second.c_str() );
  998. };
  999. it = cArr->find("logo_md5");
  1000. if( it != cArr->end() ){
  1001. strncpy( tPtr->logo_md5, it->second.c_str(),32);
  1002. tPtr->logo_md5[32]=(char)0;
  1003. };
  1004. };
  1005. };
  1006. };
  1007. deleteResult( result );
  1008. };
  1009. return tPtr;
  1010. };
  1011. DBC_MEDIA_STREAM_DATA *sqliteDBC::getMediaStreamData(uint32_t ID){
  1012. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  1013. if(m_bIsConnected){
  1014. char tQBuf[CTEXT];
  1015. AAssoc *result;
  1016. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  1017. #ifdef DEBUG_ALL
  1018. LOG(tQBuf);
  1019. #endif
  1020. result = m_pDataStorage->Query( tQBuf );
  1021. if( result!= NULL ) {
  1022. if( result->size() > 0 ) {
  1023. Assoc *cArr;
  1024. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  1025. if( tVal != NULL ) {
  1026. memset( tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA) );
  1027. Assoc::iterator it;
  1028. cArr = result->at(0);
  1029. tVal->id = ID;
  1030. it = cArr->find("source_id");
  1031. if( it != cArr->end() ){
  1032. tVal->source_id = atoi( it->second.c_str() );
  1033. };
  1034. it = cArr->find("source_type");
  1035. if( it != cArr->end() ){
  1036. sprintf( tVal->source_type, it->second.c_str() );
  1037. };
  1038. it = cArr->find("valid");
  1039. if( it != cArr->end() ){
  1040. tVal->valid = (bool) atoi( it->second.c_str() );
  1041. };
  1042. it = cArr->find("token");
  1043. if( it != cArr->end() ){
  1044. sprintf( tVal->token, it->second.c_str() );
  1045. };
  1046. it = cArr->find("img_width");
  1047. if( it != cArr->end() ){
  1048. tVal->width = atoi( it->second.c_str() );
  1049. };
  1050. it = cArr->find("img_height");
  1051. if( it != cArr->end() ){
  1052. tVal->height = atoi( it->second.c_str() );
  1053. };
  1054. it = cArr->find("fps");
  1055. if( it != cArr->end() ){
  1056. tVal->fps = atoi( it->second.c_str() );
  1057. };
  1058. it = cArr->find("compression_type");
  1059. if( it != cArr->end() ){
  1060. sprintf(tVal->c_type, it->second.c_str());
  1061. };
  1062. it = cArr->find("mrl_proto");
  1063. if( it != cArr->end() ){
  1064. sprintf( tVal->mrl_proto, it->second.c_str() );
  1065. };
  1066. it = cArr->find("mrl_port");
  1067. if( it != cArr->end() ){
  1068. tVal->mrl_port = atoi( it->second.c_str() );
  1069. };
  1070. it = cArr->find("mrl");
  1071. if( it != cArr->end() ){
  1072. sprintf( tVal->mrl, it->second.c_str() );
  1073. };
  1074. };
  1075. };
  1076. };
  1077. deleteResult( result );
  1078. };
  1079. return tVal;
  1080. };
  1081. uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
  1082. uint32_t RetVal = 0;
  1083. if(m_bIsConnected){
  1084. char tQBuf[CTEXT];
  1085. AAssoc *result;
  1086. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
  1087. #ifdef DEBUG_ALL
  1088. LOG(tQBuf);
  1089. #endif
  1090. result = m_pDataStorage->Query( tQBuf );
  1091. if( result!= NULL ) {
  1092. if( result->size() > 0 ) {
  1093. Assoc *cArr;
  1094. Assoc::iterator it;
  1095. cArr = result->at(0);
  1096. it = cArr->find("id");
  1097. if( it != cArr->end() ){
  1098. RetVal = atoi( it->second.c_str() );
  1099. };
  1100. };
  1101. };
  1102. deleteResult( result );
  1103. };
  1104. return RetVal;
  1105. };
  1106. DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
  1107. DBC_MODULE_DATA *tVal = NULL;
  1108. if(m_bIsConnected){
  1109. char tQBuf[CTEXT];
  1110. AAssoc *result;
  1111. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
  1112. #ifdef DEBUG_ALL
  1113. LOG(tQBuf);
  1114. #endif
  1115. result = m_pDataStorage->Query( tQBuf );
  1116. if( result!= NULL ) {
  1117. if( result->size() > 0 ) {
  1118. Assoc *cArr;
  1119. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  1120. if( tVal != NULL ) {
  1121. Assoc::iterator it;
  1122. cArr = result->at(0);
  1123. it = cArr->find("id");
  1124. if( it != cArr->end() ){
  1125. tVal->module_id = atoi( it->second.c_str() );
  1126. };
  1127. it = cArr->find("aux_id");
  1128. if( it != cArr->end() ){
  1129. tVal->aux_id = atoi( it->second.c_str() );
  1130. };
  1131. it = cArr->find("type");
  1132. if( it != cArr->end() ){
  1133. tVal->type = atoi( it->second.c_str() );
  1134. };
  1135. it = cArr->find("enabled");
  1136. if( it != cArr->end() ){
  1137. tVal->enabled = (bool)atoi( it->second.c_str() );
  1138. };
  1139. };
  1140. };
  1141. };
  1142. deleteResult( result );
  1143. };
  1144. return tVal;
  1145. };
  1146. DBC_LRF_INI *sqliteDBC::getINIData( uint32_t lrfID ){
  1147. DBC_LRF_INI * tVal = NULL;
  1148. if(m_bIsConnected){
  1149. char tQBuf[CTEXT];
  1150. AAssoc *result;
  1151. sprintf(tQBuf, "SELECT * FROM `lrf_ini_settings` WHERE `sensor_id`=%d", lrfID );
  1152. #ifdef DEBUG
  1153. LOG(tQBuf);
  1154. #endif
  1155. result = m_pDataStorage->Query( tQBuf );
  1156. if( result!= NULL ) {
  1157. if( result->size() > 0 ) {
  1158. Assoc *cArr;
  1159. tVal = (DBC_LRF_INI*) malloc(sizeof(DBC_LRF_INI));
  1160. if( tVal != NULL ) {
  1161. memset( tVal, 0 , sizeof(DBC_LRF_INI));
  1162. Assoc::iterator it;
  1163. cArr = result->at(0);
  1164. it = cArr->find("AUTORESET_ENABLE");
  1165. if( it != cArr->end() ){
  1166. tVal->_AUTO_RESET_ENABLE = (bool) atoi( it->second.c_str() );
  1167. };
  1168. it = cArr->find("AUTORESET_INTERLEAVE");
  1169. if( it != cArr->end() ){
  1170. tVal->_AUTO_RESET_INTERLEAVE = atoi( it->second.c_str() );
  1171. };
  1172. it = cArr->find("AUTORESET_TIMEOUT");
  1173. if( it != cArr->end() ){
  1174. tVal->_AUTO_RESET_TIMEOUT = atoi( it->second.c_str() );
  1175. };
  1176. it = cArr->find("BOARD_TEMPERATURE");
  1177. if( it != cArr->end() ){
  1178. tVal->_BOARD_TEMPERATURE = atoi( it->second.c_str() );
  1179. };
  1180. it = cArr->find("BOARD_ZEROSHIFT");
  1181. if( it != cArr->end() ){
  1182. tVal->_BOARD_ZEROSHIFT = atoi( it->second.c_str() );
  1183. };
  1184. it = cArr->find("BOARD_SPEED");
  1185. if( it != cArr->end() ){
  1186. tVal->_BOARD_SPEED = atoi( it->second.c_str() );
  1187. };
  1188. it = cArr->find("RUN_DIR");
  1189. if( it != cArr->end() ){
  1190. tVal->_RUN_DIR = atoi( it->second.c_str() );
  1191. };
  1192. it = cArr->find("JUST_MOVING_TARGETS");
  1193. if( it != cArr->end() ){
  1194. tVal->_JUST_MOVING_TARGETS = (bool) atoi( it->second.c_str() );
  1195. };
  1196. it = cArr->find("JUST_MOVING_RADIAL");
  1197. if( it != cArr->end() ){
  1198. tVal->_JUST_MOVING_RADIAL = (bool) atoi( it->second.c_str() );
  1199. };
  1200. it = cArr->find("JUST_MOVING_DISTANCE");
  1201. if( it != cArr->end() ){
  1202. tVal->_JUST_MOVING_DISTANCE = (bool) atoi( it->second.c_str() );
  1203. };
  1204. it = cArr->find("FREEZ_FIRST_TH");
  1205. if( it != cArr->end() ){
  1206. tVal->_FREEZ_FIRST_TH = (bool) atoi( it->second.c_str() );
  1207. };
  1208. it = cArr->find("SAVE_FIRST_TH");
  1209. if( it != cArr->end() ){
  1210. tVal->_SAVE_FIRST_TH = (bool) atoi( it->second.c_str() );
  1211. };
  1212. };
  1213. };
  1214. };
  1215. deleteResult( result );
  1216. };
  1217. return tVal;
  1218. };
  1219. bool sqliteDBC::getLRFEventSessionStatus( uint32_t nodeID ){
  1220. bool rv = false;
  1221. if(m_bIsConnected){
  1222. char tQBuf[CTEXT];
  1223. AAssoc *result;
  1224. sprintf( tQBuf, "SELECT `evt_type` FROM `events` WHERE `node_id`=%d AND ( `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ) ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", nodeID, EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO );
  1225. #ifdef DEBUG_ALL
  1226. LOG(tQBuf);
  1227. #endif
  1228. result = m_pDataReports->Query( tQBuf );
  1229. if( result!= NULL ) {
  1230. if( result->size() > 0 ) {
  1231. Assoc *cArr;
  1232. Assoc::iterator it;
  1233. cArr = result->at(0);
  1234. it = cArr->find("evt_type");
  1235. if( it != cArr->end() ){
  1236. if( atoi( it->second.c_str() ) == EVT_LRF_START_SESSION ) rv = true;
  1237. };
  1238. };
  1239. };
  1240. deleteResult( result );
  1241. };
  1242. return rv;
  1243. };
  1244. DBC_LRF_DATA *sqliteDBC::getLRFData( uint32_t ID ){
  1245. DBC_LRF_DATA * tVal = NULL;
  1246. if(m_bIsConnected){
  1247. char tQBuf[CTEXT];
  1248. AAssoc *result;
  1249. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
  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 = (DBC_LRF_DATA*) malloc(sizeof(DBC_LRF_DATA));
  1258. if( tVal != NULL ) {
  1259. memset( tVal, 0 , sizeof(DBC_LRF_DATA));
  1260. Assoc::iterator it;
  1261. tVal->id = ID;
  1262. cArr = result->at(0);
  1263. it = cArr->find("data_status");
  1264. if( it != cArr->end() ){
  1265. tVal->data_status = atoi( it->second.c_str() );
  1266. };
  1267. it = cArr->find("serial");
  1268. if( it != cArr->end() ){
  1269. strncpy( tVal->serial, it->second.c_str(), sizeof(tVal->serial)-1 );
  1270. tVal->serial[sizeof(tVal->serial)-1]=(char)0;
  1271. };
  1272. it = cArr->find("version");
  1273. if( it != cArr->end() ){
  1274. strncpy( tVal->version, it->second.c_str(), sizeof(tVal->version)-1 );
  1275. tVal->version[sizeof(tVal->version)-1]=(char)0;
  1276. };
  1277. it = cArr->find("x");
  1278. if( it != cArr->end() ){
  1279. tVal->x = atof( it->second.c_str() );
  1280. };
  1281. it = cArr->find("y");
  1282. if( it != cArr->end() ){
  1283. tVal->y = atof( it->second.c_str() );
  1284. };
  1285. it = cArr->find("z");
  1286. if( it != cArr->end() ){
  1287. tVal->z = atof( it->second.c_str() );
  1288. };
  1289. it = cArr->find("x_rot");
  1290. if( it != cArr->end() ){
  1291. tVal->x_rot = atof( it->second.c_str() );
  1292. };
  1293. it = cArr->find("y_rot");
  1294. if( it != cArr->end() ){
  1295. tVal->y_rot = atof( it->second.c_str() );
  1296. };
  1297. it = cArr->find("z_rot");
  1298. if( it != cArr->end() ){
  1299. tVal->z_rot = atof( it->second.c_str() );
  1300. };
  1301. it = cArr->find("uart_id");
  1302. if( it != cArr->end() ){
  1303. tVal->uart_id = atoi( it->second.c_str() );
  1304. };
  1305. it = cArr->find("device_id");
  1306. if( it != cArr->end() ){
  1307. tVal->device_id = atoi( it->second.c_str() );
  1308. };
  1309. it = cArr->find("ip_address");
  1310. if( it != cArr->end() ){
  1311. sprintf( tVal->master_host, it->second.c_str() );
  1312. };
  1313. it = cArr->find("aux_ip_address");
  1314. if( it != cArr->end() ){
  1315. sprintf( tVal->slave_host, it->second.c_str() );
  1316. };
  1317. it = cArr->find("port");
  1318. if( it != cArr->end() ){
  1319. tVal->master_port = atoi( it->second.c_str() );
  1320. };
  1321. it = cArr->find("aux_port");
  1322. if( it != cArr->end() ){
  1323. tVal->slave_port = atoi( it->second.c_str() );
  1324. };
  1325. it = cArr->find("auto_draw");
  1326. if( it != cArr->end() ){
  1327. tVal->auto_draw = atoi( it->second.c_str() );
  1328. };
  1329. it = cArr->find("filter_id");
  1330. if( it != cArr->end() ){
  1331. tVal->filter_id = atoi( it->second.c_str() );
  1332. };
  1333. //it = cArr->find("enabled_lrf");
  1334. //if( it != cArr->end() ){
  1335. // tVal->enabled_lrf = atoi( it->second.c_str() );
  1336. //};
  1337. it = cArr->find("max_range");
  1338. if( it != cArr->end() ){
  1339. tVal->max_range = atof( it->second.c_str() );
  1340. };
  1341. it = cArr->find("base_shift");
  1342. if( it != cArr->end() ){
  1343. tVal->base_shift = atoi( it->second.c_str() );
  1344. };
  1345. it = cArr->find("mirror_shift");
  1346. if( it != cArr->end() ){
  1347. tVal->mirror_shift = atoi( it->second.c_str() );
  1348. };
  1349. it = cArr->find("detection_profile_id");
  1350. if( it != cArr->end() ){
  1351. tVal->detection_profile_id = atoi( it->second.c_str() );
  1352. };
  1353. it = cArr->find("draw_lrf_id");
  1354. if( it != cArr->end() ){
  1355. tVal->draw_lrf_id = atoi( it->second.c_str() );
  1356. };
  1357. it = cArr->find("width_table_id");
  1358. if( it != cArr->end() ){
  1359. tVal->width_table_id = atoi( it->second.c_str() );
  1360. };
  1361. it = cArr->find("zoom_table_id");
  1362. if( it != cArr->end() ){
  1363. tVal->zoom_table_id = atoi( it->second.c_str() );
  1364. };
  1365. it = cArr->find("embd_cam");
  1366. if( it != cArr->end() ){
  1367. tVal->embd_cam = atoi( it->second.c_str() );
  1368. };
  1369. it = cArr->find("embd_cam_auto");
  1370. if( it != cArr->end() ){
  1371. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  1372. };
  1373. it = cArr->find("embd_dc");
  1374. if( it != cArr->end() ){
  1375. tVal->embd_dc = atoi( it->second.c_str() );
  1376. };
  1377. it = cArr->find("autoreset");
  1378. if( it != cArr->end() ){
  1379. tVal->autoreset = (bool)atoi( it->second.c_str() );
  1380. };
  1381. it = cArr->find("force_settings");
  1382. if( it != cArr->end() ){
  1383. tVal->force_settings = (bool)atoi( it->second.c_str() );
  1384. };
  1385. };
  1386. };
  1387. };
  1388. deleteResult( result );
  1389. };
  1390. return tVal;
  1391. };
  1392. PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master ){
  1393. PLGS_2D_PARAMETERS retVal = NULL;
  1394. if(m_bIsConnected){
  1395. char tQBuf[CTEXT];
  1396. AAssoc *result;
  1397. sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID,((master)?1:0) );
  1398. #ifdef DEBUG
  1399. LOG(tQBuf);
  1400. #endif
  1401. result = m_pDataStorage->Query( tQBuf );
  1402. if( result!= NULL ) {
  1403. if( result->size() > 0 ) {
  1404. retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
  1405. memset( retVal, 0, sizeof(LGS_2D_PARAMETERS));
  1406. Assoc *cArr;
  1407. Assoc::iterator it;
  1408. cArr = result->at(0);
  1409. it = cArr->find("order");
  1410. if( it != cArr->end() ){
  1411. retVal->nOrder = atoi(it->second.c_str());
  1412. };
  1413. it = cArr->find("offset");
  1414. if( it != cArr->end() ){
  1415. retVal->nOffset = atoi(it->second.c_str());
  1416. };
  1417. it = cArr->find("delta");
  1418. if( it != cArr->end() ){
  1419. retVal->nDelta = atoi(it->second.c_str());
  1420. };
  1421. it = cArr->find("flip");
  1422. if( it != cArr->end() ){
  1423. retVal->nFlip = atoi(it->second.c_str());
  1424. };
  1425. it = cArr->find("width");
  1426. if( it != cArr->end() ){
  1427. retVal->nWidth = atoi(it->second.c_str());
  1428. };
  1429. it = cArr->find("sst");
  1430. if( it != cArr->end() ){
  1431. retVal->bSST = atoi(it->second.c_str());
  1432. };
  1433. it = cArr->find("sun");
  1434. if( it != cArr->end() ){
  1435. retVal->nSun = atoi(it->second.c_str());
  1436. };
  1437. };
  1438. };
  1439. deleteResult( result );
  1440. };
  1441. return retVal;
  1442. };
  1443. uint8_t sqliteDBC::getLRFDataStatus( uint32_t ID ){
  1444. uint8_t RetVal = 0;
  1445. if(m_bIsConnected){
  1446. char tQBuf[CTEXT];
  1447. AAssoc *result;
  1448. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID );
  1449. #ifdef DEBUG
  1450. LOG(tQBuf);
  1451. #endif
  1452. result = m_pDataStorage->Query( tQBuf );
  1453. if( result!= NULL ) {
  1454. if( result->size() > 0 ) {
  1455. Assoc *cArr;
  1456. Assoc::iterator it;
  1457. cArr = result->at(0);
  1458. it = cArr->find("data_status");
  1459. if( it != cArr->end() ){
  1460. RetVal = atoi( it->second.c_str() );
  1461. };
  1462. };
  1463. };
  1464. deleteResult( result );
  1465. };
  1466. return RetVal;
  1467. };
  1468. uint32_t sqliteDBC::getLRFMaskID( uint32_t deviceID, uint8_t lrfID, uint8_t layer ){
  1469. uint32_t RetVal = 0;
  1470. if(m_bIsConnected){
  1471. char tQBuf[CTEXT];
  1472. AAssoc *result;
  1473. sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer );
  1474. #ifdef DEBUG
  1475. LOG(tQBuf);
  1476. #endif
  1477. result = m_pDataStorage->Query( tQBuf );
  1478. if( result!= NULL ) {
  1479. if( result->size() > 0 ) {
  1480. Assoc *cArr;
  1481. Assoc::iterator it;
  1482. cArr = result->at(0);
  1483. it = cArr->find("id");
  1484. if( it != cArr->end() ){
  1485. RetVal = atoi( it->second.c_str() );
  1486. };
  1487. };
  1488. };
  1489. deleteResult( result );
  1490. };
  1491. return RetVal;
  1492. };
  1493. DBC_LRF_UPLOAD_DATA *sqliteDBC::getLRFUploadData( uint32_t upID ){
  1494. DBC_LRF_UPLOAD_DATA *RetVal = NULL;
  1495. if(m_bIsConnected){
  1496. char tQBuf[CTEXT];
  1497. AAssoc *result;
  1498. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID );
  1499. #ifdef DEBUG
  1500. LOG(tQBuf);
  1501. #endif
  1502. result = m_pDataStorage->Query( tQBuf );
  1503. if( result->size() > 0 ) {
  1504. Assoc *cArr;
  1505. RetVal = (DBC_LRF_UPLOAD_DATA*) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  1506. if( RetVal != NULL ) {
  1507. memset( RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  1508. Assoc::iterator it;
  1509. RetVal->id = upID;
  1510. cArr = result->at(0);
  1511. it = cArr->find("md5");
  1512. if( it != cArr->end() ){
  1513. strncpy( RetVal->md5, it->second.c_str(), 32 );
  1514. };
  1515. it = cArr->find("file");
  1516. if( it != cArr->end() ){
  1517. strncpy( RetVal->fname, it->second.c_str(), 16 );
  1518. };
  1519. it = cArr->find("issue_reset");
  1520. if( it != cArr->end() ){
  1521. RetVal->transfer_mode = atoi( it->second.c_str() );
  1522. };
  1523. };
  1524. };
  1525. deleteResult( result );
  1526. };
  1527. return RetVal;
  1528. };
  1529. uint32_t sqliteDBC::getNodeAuxParam( uint32_t NodeID ){
  1530. uint32_t retVal = 0;
  1531. if(m_bIsConnected){
  1532. char tQBuf[CTEXT];
  1533. AAssoc *result;
  1534. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID );
  1535. #ifdef DEBUG_EXTRA
  1536. LOG(tQBuf);
  1537. #endif
  1538. result = m_pDataStorage->Query( tQBuf );
  1539. if( result!= NULL ) {
  1540. if( result->size() > 0 ) {
  1541. Assoc *cArr;
  1542. Assoc::iterator it;
  1543. cArr = result->at(0);
  1544. it = cArr->find("aux_parameter");
  1545. if( it != cArr->end() ){
  1546. retVal = atoi( it->second.c_str() );
  1547. };
  1548. };
  1549. };
  1550. deleteResult( result );
  1551. };
  1552. return retVal;
  1553. };
  1554. DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
  1555. DBC_NODE_DATA * tVal = NULL;
  1556. if(m_bIsConnected){
  1557. char tQBuf[CTEXT];
  1558. AAssoc *result;
  1559. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
  1560. #ifdef DEBUG_EXTRA
  1561. LOG(tQBuf);
  1562. #endif
  1563. result = m_pDataStorage->Query( tQBuf );
  1564. if( result!= NULL ) {
  1565. if( result->size() > 0 ) {
  1566. Assoc *cArr;
  1567. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  1568. if( tVal != NULL ) {
  1569. memset( tVal, 0, sizeof(DBC_NODE_DATA));
  1570. Assoc::iterator it;
  1571. tVal->node_id = ID;
  1572. cArr = result->at(0);
  1573. it = cArr->find("type");
  1574. if( it != cArr->end() ){
  1575. tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
  1576. };
  1577. it = cArr->find("aux_id");
  1578. if( it != cArr->end() ){
  1579. tVal->aux_id = atoi( it->second.c_str() );
  1580. };
  1581. it = cArr->find("aux_parameter");
  1582. if( it != cArr->end() ){
  1583. tVal->aux_parameter = atoi( it->second.c_str() );
  1584. };
  1585. it = cArr->find("online");
  1586. if( it != cArr->end() ){
  1587. tVal->online = (bool)atoi( it->second.c_str() );
  1588. };
  1589. it = cArr->find("enable");
  1590. if( it != cArr->end() ){
  1591. tVal->enabled = (bool)atoi( it->second.c_str() );
  1592. };
  1593. it = cArr->find("name");
  1594. if( it != cArr->end() ){
  1595. sprintf( tVal->name, it->second.c_str() );
  1596. };
  1597. };
  1598. };
  1599. };
  1600. deleteResult( result );
  1601. };
  1602. return tVal;
  1603. };
  1604. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  1605. DBC_NET_IF_DATA * tVal = NULL;
  1606. if(m_bIsConnected){
  1607. char tQBuf[CTEXT];
  1608. AAssoc *result;
  1609. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  1610. #ifdef DEBUG_EXTRA
  1611. LOG(tQBuf);
  1612. #endif
  1613. result = m_pDataStorage->Query( tQBuf );
  1614. if( result!= NULL ) {
  1615. if( result->size() > 0 ) {
  1616. Assoc *cArr;
  1617. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  1618. if( tVal != NULL ) {
  1619. Assoc::iterator it;
  1620. tVal->id = nif_id;
  1621. cArr = result->at(0);
  1622. it = cArr->find("type");
  1623. if( it != cArr->end() ){
  1624. tVal->type = atoi(it->second.c_str());
  1625. };
  1626. it = cArr->find("mode");
  1627. if( it != cArr->end() ){
  1628. tVal->mode = atoi(it->second.c_str());
  1629. };
  1630. it = cArr->find("device_name");
  1631. if( it != cArr->end() ){
  1632. strncpy( tVal->device_name, it->second.c_str(),11);
  1633. tVal->device_name[10]=(char)0;
  1634. };
  1635. it = cArr->find("mac");
  1636. if( it != cArr->end() ){
  1637. strncpy( tVal->mac, it->second.c_str(), 33 );
  1638. tVal->mac[32]=(char)0;
  1639. };
  1640. it = cArr->find("aux_id");
  1641. if( it != cArr->end() ){
  1642. tVal->aux_id = atoi(it->second.c_str());
  1643. };
  1644. };
  1645. };
  1646. };
  1647. deleteResult( result );
  1648. };
  1649. return tVal;
  1650. };
  1651. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  1652. uint32_t tVal = 0;
  1653. if(m_bIsConnected){
  1654. char tQBuf[CTEXT];
  1655. AAssoc *result;
  1656. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  1657. #ifdef DEBUG_ALL
  1658. LOG(tQBuf);
  1659. #endif
  1660. result = m_pDataStorage->Query( tQBuf );
  1661. if( result!= NULL ) {
  1662. if( result->size() > 0 ) {
  1663. Assoc *cArr;
  1664. Assoc::iterator it;
  1665. cArr = result->at(0);
  1666. it = cArr->find("id");
  1667. if( it != cArr->end() ){
  1668. tVal = atoi( it->second.c_str() );
  1669. };
  1670. };
  1671. };
  1672. deleteResult( result );
  1673. };
  1674. return tVal;
  1675. };
  1676. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  1677. uint32_t tVal = 0;
  1678. if(m_bIsConnected){
  1679. char tQBuf[CTEXT];
  1680. AAssoc *result;
  1681. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  1682. #ifdef DEBUG_ALL
  1683. LOG(tQBuf);
  1684. #endif
  1685. result = m_pDataStorage->Query( tQBuf );
  1686. if( result!= NULL ) {
  1687. if( result->size() > 0 ) {
  1688. Assoc *cArr;
  1689. Assoc::iterator it;
  1690. cArr = result->at(0);
  1691. it = cArr->find("id");
  1692. if( it != cArr->end() ){
  1693. tVal = atoi( it->second.c_str() );
  1694. };
  1695. };
  1696. };
  1697. deleteResult( result );
  1698. };
  1699. return tVal;
  1700. };
  1701. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  1702. uint32_t tVal = 0;
  1703. if(m_bIsConnected){
  1704. char tQBuf[CTEXT];
  1705. AAssoc *result;
  1706. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  1707. #ifdef DEBUG_ALL
  1708. LOG(tQBuf);
  1709. #endif
  1710. result = m_pDataStorage->Query( tQBuf );
  1711. if( result!= NULL ) {
  1712. if( result->size() > 0 ) {
  1713. Assoc *cArr;
  1714. Assoc::iterator it;
  1715. cArr = result->at(0);
  1716. it = cArr->find("id");
  1717. if( it != cArr->end() ){
  1718. tVal = atoi( it->second.c_str() );
  1719. };
  1720. };
  1721. };
  1722. deleteResult( result );
  1723. };
  1724. return tVal;
  1725. };
  1726. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  1727. DBC_RESOURCE_DATA *tVal = NULL;
  1728. if( m_bIsConnected ){
  1729. char tQBuf[CTEXT];
  1730. AAssoc *result;
  1731. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  1732. #ifdef DEBUG_ALL
  1733. LOG(tQBuf);
  1734. #endif
  1735. result = m_pDataStorage->Query( tQBuf );
  1736. if( result!= NULL ) {
  1737. if( result->size() > 0 ) {
  1738. Assoc *cArr;
  1739. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  1740. if( tVal != NULL ) {
  1741. Assoc::iterator it;
  1742. cArr = result->at(0);
  1743. tVal->id = ResourceID;
  1744. it = cArr->find("node_id");
  1745. if( it != cArr->end() ){
  1746. tVal->node_id = atoi( it->second.c_str() );
  1747. };
  1748. it = cArr->find("type");
  1749. if( it != cArr->end() ){
  1750. tVal->type = atoi( it->second.c_str() );
  1751. };
  1752. it = cArr->find("aux_id");
  1753. if( it != cArr->end() ){
  1754. tVal->aux_id = atoi( it->second.c_str() );
  1755. };
  1756. it = cArr->find("device");
  1757. if( it != cArr->end() ){
  1758. strncpy( tVal->device, it->second.c_str(), 64 );
  1759. tVal->device[63] = (char)0;
  1760. };
  1761. };
  1762. };
  1763. };
  1764. deleteResult( result );
  1765. };
  1766. return tVal;
  1767. };
  1768. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  1769. uint32_t RetVal = 0;
  1770. if(m_bIsConnected){
  1771. char tQBuf[CTEXT];
  1772. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  1773. #ifdef DEBUG_ALL
  1774. LOG(tQBuf);
  1775. #endif
  1776. AAssoc *result;
  1777. result = m_pDataStorage->Query( tQBuf );
  1778. if( result!= NULL ) {
  1779. if( result->size() > 0 ) {
  1780. Assoc * cArr;
  1781. Assoc::iterator it;
  1782. cArr = result->at(0);
  1783. it = cArr->find("id");
  1784. if( it != cArr->end() ){
  1785. RetVal = atoi( it->second.c_str() );
  1786. };
  1787. };
  1788. };
  1789. deleteResult( result );
  1790. };
  1791. return RetVal;
  1792. };
  1793. uint32_t sqliteDBC::getServerID( CSTR identity ){
  1794. uint32_t RetVal = 0;
  1795. if(m_bIsConnected){
  1796. char tQBuf[CTEXT];
  1797. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  1798. #ifdef DEBUG_ALL
  1799. LOG(tQBuf);
  1800. #endif
  1801. AAssoc *result;
  1802. result = m_pDataStorage->Query( tQBuf );
  1803. if( result!= NULL ) {
  1804. if( result->size() > 0 ) {
  1805. Assoc * cArr;
  1806. Assoc::iterator it;
  1807. cArr = result->at(0);
  1808. it = cArr->find("id");
  1809. if( it != cArr->end() ){
  1810. RetVal = atoi( it->second.c_str() );
  1811. };
  1812. };
  1813. };
  1814. deleteResult( result );
  1815. };
  1816. return RetVal;
  1817. };
  1818. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  1819. uint32_t RetVal = 0;
  1820. if(m_bIsConnected){
  1821. char tQBuf[CTEXT];
  1822. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  1823. #ifdef DEBUG_ALL
  1824. LOG(tQBuf);
  1825. #endif
  1826. AAssoc *result;
  1827. result = m_pDataStorage->Query( tQBuf );
  1828. if( result!= NULL ) {
  1829. if( result->size() > 0 ) {
  1830. Assoc * cArr;
  1831. Assoc::iterator it;
  1832. cArr = result->at(0);
  1833. it = cArr->find("id");
  1834. if( it != cArr->end() ){
  1835. RetVal = atoi( it->second.c_str() );
  1836. };
  1837. };
  1838. };
  1839. deleteResult( result );
  1840. };
  1841. return RetVal;
  1842. }
  1843. DBC_TARGET_FLITER_DATA *sqliteDBC::getTargetFilterData( uint32_t ID ){
  1844. DBC_TARGET_FLITER_DATA * tVal = NULL;
  1845. if(m_bIsConnected){
  1846. char tQBuf[CTEXT];
  1847. AAssoc *result;
  1848. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID );
  1849. #ifdef DEBUG
  1850. LOG(tQBuf);
  1851. #endif
  1852. result = m_pDataStorage->Query( tQBuf );
  1853. if( result!= NULL ) {
  1854. if( result->size() > 0 ) {
  1855. Assoc *cArr;
  1856. tVal = (DBC_TARGET_FLITER_DATA*) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  1857. if( tVal != NULL ) {
  1858. memset( tVal, 0 , sizeof(DBC_TARGET_FLITER_DATA));
  1859. Assoc::iterator it;
  1860. tVal->id = ID;
  1861. cArr = result->at(0);
  1862. it = cArr->find("type");
  1863. if( it != cArr->end() ){
  1864. tVal->type = (TARGET_FILTER_TYPE) atoi( it->second.c_str() );
  1865. };
  1866. it = cArr->find("aux_id");
  1867. if( it != cArr->end() ){
  1868. tVal->aux_id = atoi( it->second.c_str() );
  1869. };
  1870. it = cArr->find("enabled");
  1871. if( it != cArr->end() ){
  1872. tVal->enabled = (bool) atoi( it->second.c_str() );
  1873. };
  1874. };
  1875. };
  1876. };
  1877. deleteResult( result );
  1878. };
  1879. return tVal;
  1880. };
  1881. TRAIN_FILTER_DATA *sqliteDBC::getTrainFilterData( uint32_t ID ){
  1882. TRAIN_FILTER_DATA * tVal = NULL;
  1883. if(m_bIsConnected){
  1884. char tQBuf[CTEXT];
  1885. AAssoc *result;
  1886. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID );
  1887. #ifdef DEBUG_EXTRA
  1888. LOG(tQBuf);
  1889. #endif
  1890. result = m_pDataStorage->Query( tQBuf );
  1891. if( result!= NULL ) {
  1892. if( result->size() > 0 ) {
  1893. Assoc *cArr;
  1894. tVal = (TRAIN_FILTER_DATA*) malloc(sizeof(TRAIN_FILTER_DATA));
  1895. if( tVal != NULL ) {
  1896. memset( tVal, 0 , sizeof(TRAIN_FILTER_DATA));
  1897. Assoc::iterator it;
  1898. cArr = result->at(0);
  1899. it = cArr->find("UPPER_SENSOR");
  1900. if( it != cArr->end() ){
  1901. tVal->nUpperSensorID = atoi( it->second.c_str() );
  1902. };
  1903. it = cArr->find("LOWER_SENSOR");
  1904. if( it != cArr->end() ){
  1905. tVal->nBottomSensorID = atoi( it->second.c_str() );
  1906. };
  1907. it = cArr->find("TRAIN_DETECT");
  1908. if( it != cArr->end() ){
  1909. tVal->bDetectTrain = (bool)atoi( it->second.c_str() );
  1910. };
  1911. it = cArr->find("TRAIN_WIDTH");
  1912. if( it != cArr->end() ){
  1913. tVal->nTrainWidth = atoi( it->second.c_str() );
  1914. };
  1915. it = cArr->find("DECISION_TIME");
  1916. if( it != cArr->end() ){
  1917. tVal->nDecisionTime = atoi( it->second.c_str() );
  1918. };
  1919. it = cArr->find("SUSPECT_TIME");
  1920. if( it != cArr->end() ){
  1921. tVal->nSuspectTime = atoi( it->second.c_str() );
  1922. };
  1923. it = cArr->find("TRACK_TRAIN");
  1924. if( it != cArr->end() ){
  1925. tVal->bTrackTrain = (bool)atoi( it->second.c_str() );
  1926. };
  1927. it = cArr->find("DEBUG");
  1928. if( it != cArr->end() ){
  1929. tVal->bDebugMode = (bool)atoi( it->second.c_str() );
  1930. };
  1931. };
  1932. };
  1933. };
  1934. deleteResult( result );
  1935. };
  1936. return tVal;
  1937. };
  1938. DBC_UART_DATA *sqliteDBC::getUartData( uint32_t UartID ){
  1939. DBC_UART_DATA *tVal = NULL;
  1940. if( m_bIsConnected ) {
  1941. char tQBuf[MAX_PATH_LEN];
  1942. AAssoc *result;
  1943. sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
  1944. #ifdef DEBUG_ALL
  1945. LOG(tQBuf);
  1946. #endif
  1947. result = m_pDataStorage->Query( tQBuf );
  1948. if( result!= NULL ) {
  1949. if( result->size() > 0 ) {
  1950. Assoc *cArr;
  1951. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  1952. if( tVal != NULL ) {
  1953. Assoc::iterator it;
  1954. cArr = result->at(0);
  1955. it = cArr->find("type");
  1956. if( it != cArr->end() ){
  1957. tVal->type = atoi( it->second.c_str() );
  1958. };
  1959. it = cArr->find("speed");
  1960. if( it != cArr->end() ){
  1961. tVal->speed = atoi( it->second.c_str() );
  1962. };
  1963. it = cArr->find("mode");
  1964. if( it != cArr->end() ){
  1965. tVal->mode = atoi( it->second.c_str() );
  1966. };
  1967. it = cArr->find("port");
  1968. if( it != cArr->end() ){
  1969. tVal->port = atoi( it->second.c_str() );
  1970. };
  1971. it = cArr->find("path");
  1972. if( it != cArr->end() ){
  1973. strncpy( tVal->path, it->second.c_str(), 128 );
  1974. };
  1975. };
  1976. };
  1977. };
  1978. deleteResult( result );
  1979. };
  1980. return tVal;
  1981. };
  1982. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  1983. uint32_t RetVal = 0;
  1984. if(m_bIsConnected){
  1985. char tQBuf[CTEXT];
  1986. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  1987. #ifdef DEBUG_ALL
  1988. LOG(tQBuf);
  1989. #endif
  1990. AAssoc *result;
  1991. result = m_pDataStorage->Query( tQBuf );
  1992. if( result!= NULL ) {
  1993. if( result->size() > 0 ) {
  1994. Assoc * cArr;
  1995. Assoc::iterator it;
  1996. cArr = result->at(0);
  1997. it = cArr->find("id");
  1998. if( it != cArr->end() ){
  1999. RetVal = atoi( it->second.c_str() );
  2000. };
  2001. };
  2002. };
  2003. deleteResult( result );
  2004. };
  2005. return RetVal;
  2006. };
  2007. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  2008. uint32_t tVal = 0;
  2009. if(m_bIsConnected){
  2010. char tQBuf[CTEXT];
  2011. AAssoc *result;
  2012. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  2013. #ifdef DEBUG_ALL
  2014. LOG(tQBuf);
  2015. #endif
  2016. result = m_pDataStorage->Query( tQBuf );
  2017. if( result!= NULL ) {
  2018. if( result->size() > 0 ) {
  2019. Assoc *cArr;
  2020. Assoc::iterator it;
  2021. cArr = result->at(0);
  2022. it = cArr->find("id");
  2023. if( it != cArr->end() ){
  2024. tVal = atoi( it->second.c_str() );
  2025. };
  2026. };
  2027. };
  2028. deleteResult( result );
  2029. };
  2030. return tVal;
  2031. };
  2032. DBC_INT_LIST *sqliteDBC::listFilters( uint32_t ServerID ){
  2033. DBC_INT_LIST * tVal = NULL;
  2034. if(m_bIsConnected){
  2035. char tQBuf[CTEXT];
  2036. AAssoc *result;
  2037. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1" );
  2038. #ifdef DEBUG
  2039. LOG(tQBuf);
  2040. #endif
  2041. result = m_pDataStorage->Query( tQBuf );
  2042. if( result!= NULL ) {
  2043. if( result->size() > 0 ) {
  2044. Assoc *cArr;
  2045. tVal = new DBC_INT_LIST();
  2046. if( tVal != NULL ) {
  2047. Assoc::iterator it;
  2048. for(int i=0;i<result->size();i++) {
  2049. cArr = result->at(i);
  2050. it = cArr->find("id");
  2051. if( it != cArr->end() ){
  2052. tVal->push_back( atoi( it->second.c_str() ));
  2053. };
  2054. };
  2055. };
  2056. };
  2057. };
  2058. deleteResult( result );
  2059. };
  2060. return tVal;
  2061. };
  2062. DBC_INT_LIST *sqliteDBC::listLRFUploads( uint32_t deviceID ){
  2063. DBC_INT_LIST * tVal = NULL;
  2064. if(m_bIsConnected){
  2065. char tQBuf[CTEXT];
  2066. AAssoc *result;
  2067. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID );
  2068. #ifdef DEBUG
  2069. LOG(tQBuf);
  2070. #endif
  2071. result = m_pDataStorage->Query( tQBuf );
  2072. if( result!= NULL ) {
  2073. if( result->size() > 0 ) {
  2074. Assoc *cArr;
  2075. tVal = new DBC_INT_LIST();
  2076. if( tVal != NULL ) {
  2077. Assoc::iterator it;
  2078. for(int i=0;i<result->size();i++) {
  2079. cArr = result->at(i);
  2080. it = cArr->find("id");
  2081. if( it != cArr->end() ){
  2082. tVal->push_back( atoi( it->second.c_str() ));
  2083. };
  2084. };
  2085. };
  2086. };
  2087. };
  2088. deleteResult( result );
  2089. };
  2090. return tVal;
  2091. };
  2092. DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
  2093. DBC_INT_LIST * tVal = NULL;
  2094. if(m_bIsConnected){
  2095. char tQBuf[CTEXT];
  2096. AAssoc *result;
  2097. switch( listMode ){
  2098. case LIST_VALID_RECORDS:
  2099. 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" );
  2100. break;
  2101. case LIST_INVALID_RECORDS:
  2102. 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" );
  2103. break;
  2104. default:
  2105. 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" );
  2106. };
  2107. #ifdef DEBUG_ALL
  2108. LOG(tQBuf);
  2109. #endif
  2110. result = m_pDataStorage->Query( tQBuf );
  2111. if( result!= NULL ) {
  2112. if( result->size() > 0 ) {
  2113. Assoc *cArr;
  2114. tVal = new DBC_INT_LIST();
  2115. if( tVal != NULL ) {
  2116. Assoc::iterator it;
  2117. for(int i=0;i<result->size();i++) {
  2118. cArr = result->at(i);
  2119. it = cArr->find("id");
  2120. if( it != cArr->end() ){
  2121. tVal->push_back( atoi( it->second.c_str() ));
  2122. };
  2123. };
  2124. };
  2125. };
  2126. };
  2127. deleteResult( result );
  2128. };
  2129. return tVal;
  2130. };
  2131. DBC_INT_LIST *sqliteDBC::listModules(){
  2132. DBC_INT_LIST * tVal = NULL;
  2133. if(m_bIsConnected){
  2134. AAssoc *result;
  2135. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  2136. if( result!= NULL ) {
  2137. if( result->size() > 0 ) {
  2138. Assoc *cArr;
  2139. tVal = new DBC_INT_LIST();
  2140. if( tVal != NULL ) {
  2141. Assoc::iterator it;
  2142. for(int i=0;i<result->size();i++) {
  2143. cArr = result->at(i);
  2144. it = cArr->find("id");
  2145. if( it != cArr->end() ){
  2146. tVal->push_back( atoi( it->second.c_str() ));
  2147. };
  2148. };
  2149. };
  2150. };
  2151. };
  2152. deleteResult( result );
  2153. };
  2154. return tVal;
  2155. };
  2156. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  2157. DBC_INT_LIST * tVal = NULL;
  2158. char tParam[CTEXT];
  2159. //memset( tParam, 0, CTEXT );
  2160. sprintf(tParam,"0");
  2161. if( old_ids != NULL ) {
  2162. char tBuffer[CTEXT];
  2163. for(int i = 0; i< old_ids->size(); i++ ) {
  2164. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  2165. strncpy( tParam, tBuffer, CTEXT );
  2166. };
  2167. };
  2168. if(m_bIsConnected){
  2169. char tQBuf[CTEXT];
  2170. AAssoc *result;
  2171. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  2172. #ifdef DEBUG_ALL
  2173. LOG(tQBuf);
  2174. #endif
  2175. result = m_pDataStorage->Query( tQBuf );
  2176. if( result!= NULL ) {
  2177. if( result->size() > 0 ) {
  2178. Assoc *cArr;
  2179. tVal = new DBC_INT_LIST();
  2180. if( tVal != NULL ) {
  2181. Assoc::iterator it;
  2182. for(int i=0;i<result->size();i++) {
  2183. cArr = result->at(i);
  2184. it = cArr->find("id");
  2185. if( it != cArr->end() ){
  2186. tVal->push_back( atoi( it->second.c_str() ));
  2187. };
  2188. };
  2189. };
  2190. };
  2191. };
  2192. deleteResult( result );
  2193. };
  2194. return tVal;
  2195. };
  2196. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  2197. DBC_INT_LIST * tVal = NULL;
  2198. char tParam[CTEXT];
  2199. //memset( tParam, 0, CTEXT );
  2200. sprintf(tParam,"0");
  2201. if( old_ids != NULL ) {
  2202. char tBuffer[CTEXT];
  2203. for(int i = 0; i< old_ids->size(); i++ ) {
  2204. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  2205. strncpy( tParam, tBuffer, CTEXT );
  2206. };
  2207. };
  2208. if( m_bIsConnected ){
  2209. char tQBuf[CTEXT];
  2210. AAssoc *result;
  2211. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam );
  2212. #ifdef DEBUG_EXTRA
  2213. LOG(tQBuf);
  2214. #endif
  2215. result = m_pDataStorage->Query( tQBuf );
  2216. if( result!= NULL ) {
  2217. if( result->size() > 0 ) {
  2218. Assoc *cArr;
  2219. tVal = new DBC_INT_LIST();
  2220. if( tVal != NULL ) {
  2221. Assoc::iterator it;
  2222. for(int i=0;i<result->size();i++) {
  2223. cArr = result->at(i);
  2224. it = cArr->find("id");
  2225. if( it != cArr->end() ){
  2226. tVal->push_back( atoi( it->second.c_str() ));
  2227. };
  2228. };
  2229. };
  2230. };
  2231. };
  2232. deleteResult( result );
  2233. };
  2234. return tVal;
  2235. };
  2236. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  2237. DBC_INT_LIST * tVal = NULL;
  2238. if( m_bIsConnected ){
  2239. char tQBuf[1024];
  2240. AAssoc *result;
  2241. if( all ) {
  2242. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  2243. } else {
  2244. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1" );
  2245. };
  2246. #ifdef DEBUG_EXTRA
  2247. LOG(tQBuf);
  2248. #endif
  2249. result = m_pDataStorage->Query( tQBuf );
  2250. if( result!= NULL ) {
  2251. if( result->size() > 0 ) {
  2252. Assoc *cArr;
  2253. tVal = new DBC_INT_LIST();
  2254. if( tVal != NULL ) {
  2255. Assoc::iterator it;
  2256. for(int i=0; i <result->size();i++) {
  2257. cArr = result->at(i);
  2258. it = cArr->find("id");
  2259. if( it != cArr->end() ){
  2260. tVal->push_back( atoi( it->second.c_str() ));
  2261. };
  2262. };
  2263. };
  2264. };
  2265. };
  2266. deleteResult( result );
  2267. };
  2268. return tVal;
  2269. };
  2270. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  2271. DBC_INT_LIST * tVal = NULL;
  2272. if(m_bIsConnected){
  2273. char tQBuf[CTEXT];
  2274. AAssoc *result;
  2275. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  2276. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  2277. #ifdef DEBUG
  2278. LOG(tQBuf);
  2279. #endif
  2280. result = m_pDataStorage->Query( tQBuf );
  2281. if( result!= NULL ) {
  2282. if( result->size() > 0 ) {
  2283. Assoc *cArr;
  2284. tVal = new DBC_INT_LIST();
  2285. if( tVal != NULL ) {
  2286. Assoc::iterator it;
  2287. for(int i=0;i<result->size();i++) {
  2288. cArr = result->at(i);
  2289. it = cArr->find("id");
  2290. if( it != cArr->end() ){
  2291. tVal->push_back( atoi( it->second.c_str() ));
  2292. };
  2293. };
  2294. };
  2295. };
  2296. };
  2297. deleteResult( result );
  2298. };
  2299. return tVal;
  2300. };
  2301. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  2302. if( m_bIsConnected ){
  2303. m_nSiteID = SiteID;
  2304. char tQBuf[MAX_PATH_LEN];
  2305. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  2306. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  2307. #ifdef DEBUG_ALL
  2308. LOG(tQBuf);
  2309. #endif
  2310. m_pDataStorage->SilentQuery( tQBuf );
  2311. m_nServerID = getServerID( identity );
  2312. *ServID = m_nServerID;
  2313. if( m_nServerID ) {
  2314. //if ( bRecreateMemStructures )
  2315. initMemDB( m_nServerID );
  2316. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  2317. #ifdef DEBUG_ALL
  2318. LOG(tQBuf);
  2319. #endif
  2320. m_pDataMemory->SilentQuery( tQBuf );
  2321. 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 );
  2322. #ifdef DEBUG_ALL
  2323. LOG(tQBuf);
  2324. #endif
  2325. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  2326. if( m_nServerNodeID== 0 ){
  2327. m_nServerNodeID = getServerNode( m_nServerID );
  2328. };
  2329. *ServNode = m_nServerNodeID;
  2330. // Clenup Nodes and services ...
  2331. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  2332. #ifdef DEBUG_ALL
  2333. LOG(tQBuf);
  2334. #endif
  2335. m_pDataStorage->SilentQuery( tQBuf );
  2336. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0 WHERE `manual`=0" );
  2337. #ifdef DEBUG_ALL
  2338. LOG(tQBuf);
  2339. #endif
  2340. m_pDataStorage->SilentQuery( tQBuf );
  2341. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )" );
  2342. #ifdef DEBUG_ALL
  2343. LOG(tQBuf);
  2344. #endif
  2345. m_pDataStorage->SilentQuery( tQBuf );
  2346. //sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  2347. #ifdef DEBUG_ALL
  2348. //LOG(tQBuf);
  2349. #endif
  2350. //m_pDataStorage->SilentQuery( tQBuf );
  2351. //sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  2352. #ifdef DEBUG_ALL
  2353. //LOG(tQBuf);
  2354. #endif
  2355. //m_pDataStorage->SilentQuery( tQBuf );
  2356. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  2357. #ifdef DEBUG_ALL
  2358. // LOG(tQBuf);
  2359. #endif
  2360. // m_pDataStorage->SilentQuery( tQBuf );
  2361. //cleanResources( tServerNodeID );
  2362. setNodeOnline( m_nServerNodeID );
  2363. };
  2364. };
  2365. };
  2366. bool sqliteDBC::setLRFDataStatus( uint32_t lrfID, uint8_t ST ){
  2367. if( m_bIsConnected ) {
  2368. char tQBuf[MAX_PATH_LEN];
  2369. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID );
  2370. #ifdef DEBUG
  2371. LOG(tQBuf);
  2372. #endif
  2373. m_pDataStorage->SilentQuery( tQBuf );
  2374. return true;
  2375. };
  2376. return false;
  2377. };
  2378. bool sqliteDBC::setLRFDetectionProfile( uint32_t lrfID, uint8_t profileID ){
  2379. if( m_bIsConnected ) {
  2380. char tQBuf[MAX_PATH_LEN];
  2381. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID );
  2382. #ifdef DEBUG_ALL
  2383. LOG(tQBuf);
  2384. #endif
  2385. m_pDataStorage->SilentQuery( tQBuf );
  2386. return true;
  2387. };
  2388. return false;
  2389. };
  2390. bool sqliteDBC::setLRFMaskStatus( uint32_t maskID, bool status ){
  2391. if( m_bIsConnected ) {
  2392. char tQBuf[MAX_PATH_LEN];
  2393. char S[2];
  2394. if (status ) {
  2395. S[0] = 'E';
  2396. S[1] = (char)0;
  2397. } else {
  2398. S[0] = 'D';
  2399. S[1] = (char)0;
  2400. };
  2401. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='%s' WHERE `id`=%d", S, maskID );
  2402. #ifdef DEBUG_ALL
  2403. LOG(tQBuf);
  2404. #endif
  2405. m_pDataStorage->SilentQuery( tQBuf );
  2406. return true;
  2407. };
  2408. return false;
  2409. };
  2410. bool sqliteDBC::setLRFAltSerial( uint32_t lrfID, CSTR Serial ){
  2411. if( m_bIsConnected ) {
  2412. char tQBuf[MAX_PATH_LEN];
  2413. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID );
  2414. #ifdef DEBUG_ALL
  2415. LOG(tQBuf);
  2416. #endif
  2417. m_pDataStorage->SilentQuery( tQBuf );
  2418. return true;
  2419. };
  2420. return false;
  2421. };
  2422. bool sqliteDBC::setLRFAltVerison( uint32_t lrfID, CSTR version ){
  2423. if( m_bIsConnected ) {
  2424. char tQBuf[MAX_PATH_LEN];
  2425. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID );
  2426. #ifdef DEBUG_ALL
  2427. LOG(tQBuf);
  2428. #endif
  2429. m_pDataStorage->SilentQuery( tQBuf );
  2430. return true;
  2431. };
  2432. return false;
  2433. };
  2434. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  2435. if(m_bIsConnected){
  2436. char tQBuf[CTEXT];
  2437. if( NodeID ) {
  2438. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  2439. #ifdef DEBUG_ALL
  2440. LOG(tQBuf);
  2441. #endif
  2442. m_pDataStorage->SilentQuery( tQBuf );
  2443. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  2444. #ifdef DEBUG_ALL
  2445. LOG(tQBuf);
  2446. #endif
  2447. m_pDataStorage->SilentQuery( tQBuf );
  2448. return true;
  2449. };
  2450. };
  2451. return false;
  2452. };
  2453. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  2454. if(m_bIsConnected){
  2455. char tQBuf[CTEXT];
  2456. if( NodeID ) {
  2457. sprintf(tQBuf,"UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID );
  2458. #ifdef DEBUG
  2459. LOG(tQBuf);
  2460. #endif
  2461. m_pDataStorage->SilentQuery( tQBuf );
  2462. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  2463. #ifdef DEBUG
  2464. LOG(tQBuf);
  2465. #endif
  2466. m_pDataStorage->SilentQuery( tQBuf );
  2467. return true;
  2468. };
  2469. };
  2470. return false;
  2471. };
  2472. bool sqliteDBC::setRessourceActive( uint32_t RessourceID, bool Status ){
  2473. if(m_bIsConnected){
  2474. char tQBuf[CTEXT];
  2475. if( RessourceID ) {
  2476. sprintf(tQBuf,"UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID );
  2477. #ifdef DEBUG
  2478. LOG(tQBuf);
  2479. #endif
  2480. m_pDataStorage->SilentQuery( tQBuf );
  2481. return true;
  2482. };
  2483. };
  2484. return false;
  2485. };
  2486. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  2487. if( m_bIsConnected ) {
  2488. char tQBuf[MAX_PATH_LEN];
  2489. if ( status ) {
  2490. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  2491. } else {
  2492. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  2493. };
  2494. #ifdef DEBUG
  2495. LOG(tQBuf);
  2496. #endif
  2497. m_pDataStorage->SilentQuery( tQBuf );
  2498. return true;
  2499. };
  2500. return false;
  2501. };
  2502. void sqliteDBC::unregisterServer( void ){
  2503. if( m_bIsConnected ) {
  2504. char tQBuf[MAX_PATH_LEN];
  2505. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  2506. #ifdef DEBUG_ALL
  2507. LOG(tQBuf);
  2508. #endif
  2509. m_pDataStorage->SilentQuery( tQBuf );
  2510. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  2511. #ifdef DEBUG_ALL
  2512. LOG(tQBuf);
  2513. #endif
  2514. m_pDataStorage->SilentQuery( tQBuf );
  2515. };
  2516. setNodeOffline( m_nServerNodeID );
  2517. };
  2518. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  2519. if( m_bIsConnected ) {
  2520. char tQBuf[MAX_PATH_LEN];
  2521. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  2522. ptr->OS_INFO,
  2523. ptr->HW_INFO,
  2524. m_nSiteID
  2525. );
  2526. #ifdef DEBUG_ALL
  2527. LOG(tQBuf);
  2528. #endif
  2529. m_pDataStorage->SilentQuery( tQBuf );
  2530. return true;
  2531. };
  2532. return false;
  2533. };
  2534. bool sqliteDBC::fillShadow2D( uint32_t ID, SHADOW & SH ){
  2535. if( m_bIsConnected ){
  2536. char tQBuf[CTEXT];
  2537. AAssoc *result;
  2538. sprintf(tQBuf, "SELECT `lv`.`id`, `lm`.`layer`, `lm`.`status` FROM `lrf_mask_vertexes` `lv` LEFT JOIN `lrf_masks` `lm` ON (`lm`.`id` = `lv`.`mask_id`) WHERE `lm`.`sensor_id`=%d ORDER BY `lv`.`id`", ID );
  2539. LOG(tQBuf);
  2540. result = m_pDataStorage->Query( tQBuf );
  2541. if( result!= NULL ) {
  2542. if( result->size() > 0 ) {
  2543. SH.m_nEnabled = 0xFFFFFFFF;
  2544. SH.m_nPolygonCount = result->size();
  2545. SH.pPolygones = (SHADOW_POLY *) calloc( SH.m_nPolygonCount, sizeof( SHADOW_POLY ) );
  2546. if ( SH.pPolygones ) {
  2547. Assoc * cArr;
  2548. for ( uint8_t idx = 0; idx < result->size(); idx++ ) {
  2549. Assoc::iterator it;
  2550. cArr = result->at(idx);
  2551. it = cArr->find("id");
  2552. if( it != cArr->end() ){
  2553. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  2554. };
  2555. it = cArr->find("layer");
  2556. if( it != cArr->end() ){
  2557. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  2558. };
  2559. it = cArr->find("status");
  2560. if( it != cArr->end() ){
  2561. SH.m_nEnabled &= ( it->second.c_str()[0] == 'E' )?0xFFFFFFFF:~(1<<SH.pPolygones[idx].bType);
  2562. };
  2563. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID );
  2564. LOG(tQBuf);
  2565. AAssoc *v_result = m_pDataStorage->Query( tQBuf );
  2566. if( v_result!= NULL ) {
  2567. if( v_result->size() > 0 ) {
  2568. SH.pPolygones[idx].wCount = v_result->size();
  2569. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc( SH.pPolygones[idx].wCount, sizeof( HZ_POINT ) );
  2570. if(SH.pPolygones[idx].pPoint ) {
  2571. for ( uint32_t zdx =0; zdx < SH.pPolygones[idx].wCount; zdx++ ) {
  2572. cArr = v_result->at(zdx);
  2573. it = cArr->find("x");
  2574. if( it != cArr->end() ){
  2575. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  2576. };
  2577. it = cArr->find("y");
  2578. if( it != cArr->end() ){
  2579. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  2580. };
  2581. LOG("Point Added!");
  2582. };
  2583. } else {
  2584. LOG("No mem for Points!");
  2585. };
  2586. } else {
  2587. LOG("No Points!");
  2588. SH.pPolygones[idx].wCount = 0;
  2589. SH.pPolygones[idx].pPoint = NULL;
  2590. };
  2591. deleteResult( v_result );
  2592. };
  2593. };
  2594. };
  2595. };
  2596. deleteResult( result );
  2597. };
  2598. return true;
  2599. };
  2600. return false;
  2601. };
  2602. bool sqliteDBC::getAppDebugMode(){
  2603. bool rVal = false;
  2604. if(m_bIsConnected){
  2605. char tQBuf[CTEXT];
  2606. AAssoc *result;
  2607. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1" );
  2608. #ifdef DEBUG_ALL
  2609. LOG(tQBuf);
  2610. #endif
  2611. result = m_pDataStorage->Query( tQBuf );
  2612. if( result!= NULL ) {
  2613. if( result->size() > 0 ) {
  2614. Assoc *cArr;
  2615. Assoc::iterator it;
  2616. cArr = result->at(0);
  2617. it = cArr->find("work_mode");
  2618. if( it != cArr->end() ){
  2619. rVal = (bool) atoi( it->second.c_str() );
  2620. };
  2621. };
  2622. };
  2623. deleteResult( result );
  2624. };
  2625. return rVal;
  2626. };
  2627. DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
  2628. DBC_CAM_DATA *tVal = NULL;
  2629. if(m_bIsConnected){
  2630. char tQBuf[CTEXT];
  2631. AAssoc *result;
  2632. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  2633. #ifdef DEBUG_ALL
  2634. LOG(tQBuf);
  2635. #endif
  2636. result = m_pDataStorage->Query( tQBuf );
  2637. if( result!= NULL ) {
  2638. if( result->size() > 0 ) {
  2639. Assoc *cArr;
  2640. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  2641. if( tVal != NULL ) {
  2642. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  2643. tVal->id = ID;
  2644. Assoc::iterator it;
  2645. cArr = result->at(0);
  2646. it = cArr->find("type");
  2647. if( it != cArr->end() ){
  2648. tVal->type = atoi( it->second.c_str() );
  2649. };
  2650. it = cArr->find("online");
  2651. if( it != cArr->end() ){
  2652. tVal->online = (bool) atoi( it->second.c_str() );
  2653. };
  2654. it = cArr->find("stream_id");
  2655. if( it != cArr->end() ){
  2656. tVal->stream_id = atoi( it->second.c_str() );
  2657. };
  2658. it = cArr->find("hardware");
  2659. if( it != cArr->end() ){
  2660. strncpy( tVal->hardware, it->second.c_str(), 32 );
  2661. };
  2662. it = cArr->find("name");
  2663. if( it != cArr->end() ){
  2664. strncpy( tVal->name, it->second.c_str(), 64 );
  2665. };
  2666. it = cArr->find("status");
  2667. if( it != cArr->end() ){
  2668. tVal->status = atoi( it->second.c_str() );
  2669. };
  2670. it = cArr->find("session");
  2671. if( it != cArr->end() ){
  2672. strncpy( tVal->session, it->second.c_str(), 32 );
  2673. };
  2674. it = cArr->find("protocol");
  2675. if( it != cArr->end() ){
  2676. tVal->protocol = atoi( it->second.c_str() );
  2677. };
  2678. it = cArr->find("protocol_aux");
  2679. if( it != cArr->end() ){
  2680. tVal->protocol_aux = atoi( it->second.c_str() );
  2681. };
  2682. it = cArr->find("uart_id");
  2683. if( it != cArr->end() ){
  2684. tVal->uart_id = atoi( it->second.c_str() );
  2685. };
  2686. it = cArr->find("x");
  2687. if( it != cArr->end() ){
  2688. tVal->x = atof( it->second.c_str() );
  2689. };
  2690. it = cArr->find("y");
  2691. if( it != cArr->end() ){
  2692. tVal->y = atof( it->second.c_str() );
  2693. };
  2694. it = cArr->find("z");
  2695. if( it != cArr->end() ){
  2696. tVal->z = atof( it->second.c_str() );
  2697. };
  2698. it = cArr->find("x_rot");
  2699. if( it != cArr->end() ){
  2700. tVal->x_rot = atof( it->second.c_str() );
  2701. };
  2702. it = cArr->find("y_rot");
  2703. if( it != cArr->end() ){
  2704. tVal->y_rot = atof( it->second.c_str() );
  2705. };
  2706. it = cArr->find("z_rot");
  2707. if( it != cArr->end() ){
  2708. tVal->z_rot = atof( it->second.c_str() );
  2709. };
  2710. it = cArr->find("imager_type");
  2711. if( it != cArr->end() ){
  2712. tVal->imager_type = atoi( it->second.c_str() );
  2713. };
  2714. it = cArr->find("lens_f");
  2715. if( it != cArr->end() ){
  2716. tVal->lens_f = atoi( it->second.c_str() );
  2717. };
  2718. it = cArr->find("ptz_speed_step");
  2719. if( it != cArr->end() ){
  2720. tVal->ptz_speed_step = atof( it->second.c_str() );
  2721. };
  2722. it = cArr->find("pan_model");
  2723. if( it != cArr->end() ){
  2724. tVal->pan_model = atoi( it->second.c_str() );
  2725. };
  2726. it = cArr->find("pan_angles");
  2727. if( it != cArr->end() ){
  2728. tVal->pan_angles = atoi( it->second.c_str() );
  2729. };
  2730. it = cArr->find("tilt_model");
  2731. if( it != cArr->end() ){
  2732. tVal->tilt_model = atoi( it->second.c_str() );
  2733. };
  2734. it = cArr->find("tilt_angles");
  2735. if( it != cArr->end() ){
  2736. tVal->tilt_angles = atoi( it->second.c_str() );
  2737. };
  2738. it = cArr->find("zoom_model");
  2739. if( it != cArr->end() ){
  2740. tVal->zoom_model = atoi( it->second.c_str() );
  2741. };
  2742. it = cArr->find("zoom");
  2743. if( it != cArr->end() ){
  2744. tVal->zoom = atof( it->second.c_str() );
  2745. };
  2746. //it = cArr->find("max_range");
  2747. //if( it != cArr->end() ){
  2748. // tVal->max_range = atof( it->second.c_str() );
  2749. //};
  2750. it = cArr->find("access_login");
  2751. if( it != cArr->end() ){
  2752. strncpy( tVal->username, it->second.c_str(), 16 );
  2753. };
  2754. it = cArr->find("access_pass");
  2755. if( it != cArr->end() ){
  2756. strncpy( tVal->password, it->second.c_str(), 16 );
  2757. };
  2758. it = cArr->find("uuid");
  2759. if( it != cArr->end() ){
  2760. strncpy( tVal->uuid, it->second.c_str(), 64 );
  2761. };
  2762. it = cArr->find("mac");
  2763. if( it != cArr->end() ){
  2764. strncpy( tVal->mac, it->second.c_str(), 16 );
  2765. };
  2766. it = cArr->find("ip");
  2767. if( it != cArr->end() ){
  2768. strncpy( tVal->ip, it->second.c_str(), 16 );
  2769. };
  2770. it = cArr->find("port");
  2771. if( it != cArr->end() ){
  2772. tVal->port = atoi( it->second.c_str() );
  2773. };
  2774. it = cArr->find("onvif_access");
  2775. if( it != cArr->end() ){
  2776. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  2777. };
  2778. it = cArr->find("onvif_ctrl");
  2779. if( it != cArr->end() ){
  2780. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  2781. };
  2782. it = cArr->find("ptz_token");
  2783. if( it != cArr->end() ){
  2784. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  2785. };
  2786. it = cArr->find("trigger_mode");
  2787. if( it != cArr->end() ){
  2788. tVal->triggering_mode = atoi( it->second.c_str() );
  2789. };
  2790. it = cArr->find("auth_mode");
  2791. if( it != cArr->end() ){
  2792. tVal->auth_mode = atoi( it->second.c_str() );
  2793. };
  2794. it = cArr->find("ptz_axel");
  2795. if( it != cArr->end() ){
  2796. tVal->ptz_axel = atoi( it->second.c_str() );
  2797. };
  2798. it = cArr->find("manual");
  2799. if( it != cArr->end() ){
  2800. tVal->manual = (bool) atoi( it->second.c_str() );
  2801. };
  2802. it = cArr->find("homing_enable");
  2803. if( it != cArr->end() ){
  2804. tVal->homing_enable = (bool) atoi( it->second.c_str() );
  2805. };
  2806. it = cArr->find("homing_to");
  2807. if( it != cArr->end() ){
  2808. tVal->homing_to = atoi( it->second.c_str() );
  2809. };
  2810. it = cArr->find("home_token");
  2811. if( it != cArr->end() ){
  2812. strncpy( tVal->home_token, it->second.c_str(), 32 );
  2813. };
  2814. };
  2815. };
  2816. };
  2817. deleteResult( result );
  2818. };
  2819. return tVal;
  2820. };
  2821. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  2822. uint32_t RetVal = 0;
  2823. if(m_bIsConnected){
  2824. char tQBuf[CTEXT];
  2825. AAssoc *result;
  2826. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  2827. #ifdef DEBUG_ALL
  2828. LOG(tQBuf);
  2829. #endif
  2830. result = m_pDataStorage->Query( tQBuf );
  2831. if( result!= NULL ) {
  2832. if( result->size() > 0 ) {
  2833. Assoc *cArr;
  2834. Assoc::iterator it;
  2835. cArr = result->at(0);
  2836. it = cArr->find("id");
  2837. if( it != cArr->end() ){
  2838. RetVal = atoi( it->second.c_str() );
  2839. };
  2840. };
  2841. };
  2842. deleteResult( result );
  2843. };
  2844. return RetVal;
  2845. };
  2846. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  2847. switch ( pid ) {
  2848. case 0: return strdup( m_pDbFile );
  2849. };
  2850. return NULL;
  2851. };
  2852. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  2853. if(m_bIsConnected) {
  2854. char tQBuf[MAX_PATH_LEN];
  2855. char tIpBuf[CTEXT];
  2856. char *tPos;
  2857. char *tPos2;
  2858. uint16_t tPort = 0;
  2859. tPos = strstr(Addr,"//");
  2860. if( tPos != NULL ) {
  2861. tPos2 = strstr( tPos + 2,":");
  2862. if( tPos2 != NULL ) {
  2863. LOG("IP Port Defined!");
  2864. int len, len2, len3;
  2865. len = tPos2 - tPos - 2;
  2866. strncpy( tIpBuf, tPos + 2, len);
  2867. tIpBuf[len] = (char)0;
  2868. tPort = atoi( tPos2 + 1 );
  2869. //strncpy( tPath, tPos + 2 + len, CTEXT );
  2870. } else {
  2871. tPos2 = strstr( tPos + 2,"/");
  2872. if( tPos2 != NULL ) {
  2873. int len, len2, len3;
  2874. len = tPos2 - tPos - 2;
  2875. strncpy( tIpBuf, tPos + 2, len);
  2876. tIpBuf[len] = (char)0;
  2877. //strncpy( tPath, tPos + 2 + len, CTEXT );
  2878. } else {
  2879. tPos2 = strstr( tPos + 2,"/");
  2880. LOG_ERROR("Pos 2 - err");
  2881. tIpBuf[0] = (char)0;
  2882. };
  2883. };
  2884. } else {
  2885. LOG_ERROR("Pos 0 - err");
  2886. tIpBuf[0] = (char)0;
  2887. };
  2888. if ( tPort ) {
  2889. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d",
  2890. Addr,
  2891. tIpBuf,
  2892. tPort,
  2893. CamID
  2894. );
  2895. } else {
  2896. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  2897. Addr,
  2898. tIpBuf,
  2899. CamID
  2900. );
  2901. }
  2902. #ifdef DEBUG
  2903. LOG(tQBuf);
  2904. #endif
  2905. m_pDataStorage->SilentQuery( tQBuf );
  2906. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  2907. #ifdef DEBUG_ALL
  2908. LOG(tQBuf);
  2909. #endif
  2910. m_pDataStorage->SilentQuery( tQBuf );
  2911. return true;
  2912. };
  2913. return false;
  2914. };
  2915. bool sqliteDBC::updateCamOnlineStatus( uint32_t camID, bool online ){
  2916. if(m_bIsConnected) {
  2917. char tQBuf[MAX_PATH_LEN];
  2918. if ( online ) {
  2919. sprintf( tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID );
  2920. } else {
  2921. sprintf( tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID );
  2922. };
  2923. m_pDataStorage->SilentQuery(tQBuf);
  2924. if ( online ) {
  2925. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  2926. } else {
  2927. sprintf( tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID );
  2928. };
  2929. m_pDataStorage->SilentQuery(tQBuf);
  2930. return true;
  2931. };
  2932. return false;
  2933. };
  2934. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  2935. if(m_bIsConnected) {
  2936. uint32_t cID = getCamId( uuid );
  2937. if ( cID ) {
  2938. return updateIPCam( cID, Addr );
  2939. };
  2940. };
  2941. return false;
  2942. };
  2943. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  2944. if(m_bIsConnected) {
  2945. uint32_t cID = getCamId( uuid );
  2946. if ( cID ) {
  2947. return updateLocalCam( cID );
  2948. };
  2949. };
  2950. return false;
  2951. };
  2952. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  2953. if(m_bIsConnected) {
  2954. char tQBuf[MAX_PATH_LEN];
  2955. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  2956. #ifdef DEBUG
  2957. LOG(tQBuf);
  2958. #endif
  2959. m_pDataStorage->SilentQuery( tQBuf );
  2960. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  2961. #ifdef DEBUG
  2962. LOG(tQBuf);
  2963. #endif
  2964. m_pDataStorage->SilentQuery( tQBuf );
  2965. return true;
  2966. };
  2967. return false;
  2968. };
  2969. bool sqliteDBC::updateLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master, uint16_t nOrder, uint16_t nOffset, uint16_t nDelta, uint16_t nWidth, uint16_t nFlip, uint16_t nSun, uint8_t bSST, uint32_t GP, uint32_t spikes ){
  2970. if( m_bIsConnected ) {
  2971. char tQBuf[MAX_PATH_LEN];
  2972. if( master ) {
  2973. sprintf( tQBuf, "INSERT INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,1,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  2974. lrfID,
  2975. profileID,
  2976. nOrder,
  2977. nOffset,
  2978. nDelta,
  2979. nFlip,
  2980. nWidth,
  2981. bSST,
  2982. nSun,
  2983. GP,
  2984. spikes,
  2985. nOrder,
  2986. nOffset,
  2987. nDelta,
  2988. nFlip,
  2989. nWidth,
  2990. bSST,
  2991. nSun,
  2992. GP,
  2993. spikes );
  2994. } else {
  2995. sprintf( tQBuf, "INSERT INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,0,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  2996. lrfID,
  2997. profileID,
  2998. nOrder,
  2999. nOffset,
  3000. nDelta,
  3001. nFlip,
  3002. nWidth,
  3003. bSST,
  3004. nSun,
  3005. GP,
  3006. spikes,
  3007. nOrder,
  3008. nOffset,
  3009. nDelta,
  3010. nFlip,
  3011. nWidth,
  3012. bSST,
  3013. nSun,
  3014. GP,
  3015. spikes );
  3016. };
  3017. #ifdef DEBUG_ALL
  3018. LOG(tQBuf);
  3019. #endif
  3020. m_pDataStorage->SilentQuery( tQBuf );
  3021. return true;
  3022. };
  3023. return false;
  3024. };
  3025. bool sqliteDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){
  3026. if( m_bIsConnected ) {
  3027. char tQBuf[MAX_PATH_LEN];
  3028. CSTR tES = m_pDataStorage->escapeString( Serial );
  3029. if( tES ) {
  3030. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID );
  3031. free( tES );
  3032. #ifdef DEBUG_ALL
  3033. LOG(tQBuf);
  3034. #endif
  3035. m_pDataStorage->SilentQuery( tQBuf );
  3036. return true;
  3037. };
  3038. };
  3039. return false;
  3040. };
  3041. bool sqliteDBC::updateLRFUpload( uint32_t id, uint8_t status ){
  3042. if( m_bIsConnected ) {
  3043. char tQBuf[MAX_PATH_LEN];
  3044. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id );
  3045. #ifdef DEBUG_ALL
  3046. LOG(tQBuf);
  3047. #endif
  3048. m_pDataStorage->SilentQuery( tQBuf );
  3049. return true;
  3050. };
  3051. return false;
  3052. };
  3053. bool sqliteDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){
  3054. if( m_bIsConnected ) {
  3055. char tQBuf[MAX_PATH_LEN];
  3056. CSTR tES = m_pDataStorage->escapeString( version );
  3057. if( tES ) {
  3058. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID );
  3059. free( tES );
  3060. #ifdef DEBUG_ALL
  3061. LOG(tQBuf);
  3062. #endif
  3063. m_pDataStorage->SilentQuery( tQBuf );
  3064. return true;
  3065. };
  3066. };
  3067. return false;
  3068. };
  3069. bool sqliteDBC::updateServerIdentity( uint32_t m_nServerID, CSTR m_sIdentity ){
  3070. if( m_bIsConnected ) {
  3071. char tQBuf[MAX_PATH_LEN];
  3072. CSTR tES = m_pDataStorage->escapeString( m_sIdentity );
  3073. if( tES ) {
  3074. sprintf( tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID );
  3075. free( tES );
  3076. #ifdef DEBUG_ALL
  3077. LOG(tQBuf);
  3078. #endif
  3079. m_pDataStorage->SilentQuery( tQBuf );
  3080. return true;
  3081. };
  3082. };
  3083. return false;
  3084. };
  3085. #undef LOG
  3086. #undef LOG_ERROR