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.

3280 lines
120 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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. uint8_t sqliteDBC::getLRFMasksStatus( uint32_t deviceID ){
  1494. uint8_t RetVal = 0;
  1495. if(m_bIsConnected){
  1496. char tQBuf[CTEXT];
  1497. AAssoc *result;
  1498. sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID );
  1499. #ifdef DEBUG
  1500. LOG(tQBuf);
  1501. #endif
  1502. result = m_pDataStorage->Query( tQBuf );
  1503. if( result!= NULL ) {
  1504. if( result->size() > 0 ) {
  1505. Assoc *cArr;
  1506. Assoc::iterator it;
  1507. for(int i=0;i<result->size();i++) {
  1508. cArr = result->at(i);
  1509. it = cArr->find("layer");
  1510. if( it != cArr->end() ){
  1511. RetVal |= ( 1<< atoi( it->second.c_str() ));
  1512. };
  1513. };
  1514. };
  1515. };
  1516. deleteResult( result );
  1517. };
  1518. return RetVal;
  1519. };
  1520. DBC_LRF_UPLOAD_DATA *sqliteDBC::getLRFUploadData( uint32_t upID ){
  1521. DBC_LRF_UPLOAD_DATA *RetVal = NULL;
  1522. if(m_bIsConnected){
  1523. char tQBuf[CTEXT];
  1524. AAssoc *result;
  1525. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID );
  1526. #ifdef DEBUG
  1527. LOG(tQBuf);
  1528. #endif
  1529. result = m_pDataStorage->Query( tQBuf );
  1530. if( result->size() > 0 ) {
  1531. Assoc *cArr;
  1532. RetVal = (DBC_LRF_UPLOAD_DATA*) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  1533. if( RetVal != NULL ) {
  1534. memset( RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  1535. Assoc::iterator it;
  1536. RetVal->id = upID;
  1537. cArr = result->at(0);
  1538. it = cArr->find("md5");
  1539. if( it != cArr->end() ){
  1540. strncpy( RetVal->md5, it->second.c_str(), 32 );
  1541. };
  1542. it = cArr->find("file");
  1543. if( it != cArr->end() ){
  1544. strncpy( RetVal->fname, it->second.c_str(), 16 );
  1545. };
  1546. it = cArr->find("issue_reset");
  1547. if( it != cArr->end() ){
  1548. RetVal->transfer_mode = atoi( it->second.c_str() );
  1549. };
  1550. };
  1551. };
  1552. deleteResult( result );
  1553. };
  1554. return RetVal;
  1555. };
  1556. uint32_t sqliteDBC::getNodeAuxParam( uint32_t NodeID ){
  1557. uint32_t retVal = 0;
  1558. if(m_bIsConnected){
  1559. char tQBuf[CTEXT];
  1560. AAssoc *result;
  1561. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID );
  1562. #ifdef DEBUG_EXTRA
  1563. LOG(tQBuf);
  1564. #endif
  1565. result = m_pDataStorage->Query( tQBuf );
  1566. if( result!= NULL ) {
  1567. if( result->size() > 0 ) {
  1568. Assoc *cArr;
  1569. Assoc::iterator it;
  1570. cArr = result->at(0);
  1571. it = cArr->find("aux_parameter");
  1572. if( it != cArr->end() ){
  1573. retVal = atoi( it->second.c_str() );
  1574. };
  1575. };
  1576. };
  1577. deleteResult( result );
  1578. };
  1579. return retVal;
  1580. };
  1581. DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
  1582. DBC_NODE_DATA * tVal = NULL;
  1583. if(m_bIsConnected){
  1584. char tQBuf[CTEXT];
  1585. AAssoc *result;
  1586. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
  1587. #ifdef DEBUG_EXTRA
  1588. LOG(tQBuf);
  1589. #endif
  1590. result = m_pDataStorage->Query( tQBuf );
  1591. if( result!= NULL ) {
  1592. if( result->size() > 0 ) {
  1593. Assoc *cArr;
  1594. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  1595. if( tVal != NULL ) {
  1596. memset( tVal, 0, sizeof(DBC_NODE_DATA));
  1597. Assoc::iterator it;
  1598. tVal->node_id = ID;
  1599. cArr = result->at(0);
  1600. it = cArr->find("type");
  1601. if( it != cArr->end() ){
  1602. tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
  1603. };
  1604. it = cArr->find("aux_id");
  1605. if( it != cArr->end() ){
  1606. tVal->aux_id = atoi( it->second.c_str() );
  1607. };
  1608. it = cArr->find("aux_parameter");
  1609. if( it != cArr->end() ){
  1610. tVal->aux_parameter = atoi( it->second.c_str() );
  1611. };
  1612. it = cArr->find("online");
  1613. if( it != cArr->end() ){
  1614. tVal->online = (bool)atoi( it->second.c_str() );
  1615. };
  1616. it = cArr->find("enable");
  1617. if( it != cArr->end() ){
  1618. tVal->enabled = (bool)atoi( it->second.c_str() );
  1619. };
  1620. it = cArr->find("name");
  1621. if( it != cArr->end() ){
  1622. sprintf( tVal->name, it->second.c_str() );
  1623. };
  1624. };
  1625. };
  1626. };
  1627. deleteResult( result );
  1628. };
  1629. return tVal;
  1630. };
  1631. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  1632. DBC_NET_IF_DATA * tVal = NULL;
  1633. if(m_bIsConnected){
  1634. char tQBuf[CTEXT];
  1635. AAssoc *result;
  1636. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  1637. #ifdef DEBUG_EXTRA
  1638. LOG(tQBuf);
  1639. #endif
  1640. result = m_pDataStorage->Query( tQBuf );
  1641. if( result!= NULL ) {
  1642. if( result->size() > 0 ) {
  1643. Assoc *cArr;
  1644. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  1645. if( tVal != NULL ) {
  1646. Assoc::iterator it;
  1647. tVal->id = nif_id;
  1648. cArr = result->at(0);
  1649. it = cArr->find("type");
  1650. if( it != cArr->end() ){
  1651. tVal->type = atoi(it->second.c_str());
  1652. };
  1653. it = cArr->find("mode");
  1654. if( it != cArr->end() ){
  1655. tVal->mode = atoi(it->second.c_str());
  1656. };
  1657. it = cArr->find("device_name");
  1658. if( it != cArr->end() ){
  1659. strncpy( tVal->device_name, it->second.c_str(),11);
  1660. tVal->device_name[10]=(char)0;
  1661. };
  1662. it = cArr->find("mac");
  1663. if( it != cArr->end() ){
  1664. strncpy( tVal->mac, it->second.c_str(), 33 );
  1665. tVal->mac[32]=(char)0;
  1666. };
  1667. it = cArr->find("aux_id");
  1668. if( it != cArr->end() ){
  1669. tVal->aux_id = atoi(it->second.c_str());
  1670. };
  1671. };
  1672. };
  1673. };
  1674. deleteResult( result );
  1675. };
  1676. return tVal;
  1677. };
  1678. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  1679. uint32_t tVal = 0;
  1680. if(m_bIsConnected){
  1681. char tQBuf[CTEXT];
  1682. AAssoc *result;
  1683. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  1684. #ifdef DEBUG_ALL
  1685. LOG(tQBuf);
  1686. #endif
  1687. result = m_pDataStorage->Query( tQBuf );
  1688. if( result!= NULL ) {
  1689. if( result->size() > 0 ) {
  1690. Assoc *cArr;
  1691. Assoc::iterator it;
  1692. cArr = result->at(0);
  1693. it = cArr->find("id");
  1694. if( it != cArr->end() ){
  1695. tVal = atoi( it->second.c_str() );
  1696. };
  1697. };
  1698. };
  1699. deleteResult( result );
  1700. };
  1701. return tVal;
  1702. };
  1703. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  1704. uint32_t tVal = 0;
  1705. if(m_bIsConnected){
  1706. char tQBuf[CTEXT];
  1707. AAssoc *result;
  1708. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  1709. #ifdef DEBUG_ALL
  1710. LOG(tQBuf);
  1711. #endif
  1712. result = m_pDataStorage->Query( tQBuf );
  1713. if( result!= NULL ) {
  1714. if( result->size() > 0 ) {
  1715. Assoc *cArr;
  1716. Assoc::iterator it;
  1717. cArr = result->at(0);
  1718. it = cArr->find("id");
  1719. if( it != cArr->end() ){
  1720. tVal = atoi( it->second.c_str() );
  1721. };
  1722. };
  1723. };
  1724. deleteResult( result );
  1725. };
  1726. return tVal;
  1727. };
  1728. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  1729. uint32_t tVal = 0;
  1730. if(m_bIsConnected){
  1731. char tQBuf[CTEXT];
  1732. AAssoc *result;
  1733. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  1734. #ifdef DEBUG_ALL
  1735. LOG(tQBuf);
  1736. #endif
  1737. result = m_pDataStorage->Query( tQBuf );
  1738. if( result!= NULL ) {
  1739. if( result->size() > 0 ) {
  1740. Assoc *cArr;
  1741. Assoc::iterator it;
  1742. cArr = result->at(0);
  1743. it = cArr->find("id");
  1744. if( it != cArr->end() ){
  1745. tVal = atoi( it->second.c_str() );
  1746. };
  1747. };
  1748. };
  1749. deleteResult( result );
  1750. };
  1751. return tVal;
  1752. };
  1753. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  1754. DBC_RESOURCE_DATA *tVal = NULL;
  1755. if( m_bIsConnected ){
  1756. char tQBuf[CTEXT];
  1757. AAssoc *result;
  1758. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  1759. #ifdef DEBUG_ALL
  1760. LOG(tQBuf);
  1761. #endif
  1762. result = m_pDataStorage->Query( tQBuf );
  1763. if( result!= NULL ) {
  1764. if( result->size() > 0 ) {
  1765. Assoc *cArr;
  1766. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  1767. if( tVal != NULL ) {
  1768. Assoc::iterator it;
  1769. cArr = result->at(0);
  1770. tVal->id = ResourceID;
  1771. it = cArr->find("node_id");
  1772. if( it != cArr->end() ){
  1773. tVal->node_id = atoi( it->second.c_str() );
  1774. };
  1775. it = cArr->find("type");
  1776. if( it != cArr->end() ){
  1777. tVal->type = atoi( it->second.c_str() );
  1778. };
  1779. it = cArr->find("aux_id");
  1780. if( it != cArr->end() ){
  1781. tVal->aux_id = atoi( it->second.c_str() );
  1782. };
  1783. it = cArr->find("device");
  1784. if( it != cArr->end() ){
  1785. strncpy( tVal->device, it->second.c_str(), 64 );
  1786. tVal->device[63] = (char)0;
  1787. };
  1788. };
  1789. };
  1790. };
  1791. deleteResult( result );
  1792. };
  1793. return tVal;
  1794. };
  1795. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  1796. uint32_t RetVal = 0;
  1797. if(m_bIsConnected){
  1798. char tQBuf[CTEXT];
  1799. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  1800. #ifdef DEBUG_ALL
  1801. LOG(tQBuf);
  1802. #endif
  1803. AAssoc *result;
  1804. result = m_pDataStorage->Query( tQBuf );
  1805. if( result!= NULL ) {
  1806. if( result->size() > 0 ) {
  1807. Assoc * cArr;
  1808. Assoc::iterator it;
  1809. cArr = result->at(0);
  1810. it = cArr->find("id");
  1811. if( it != cArr->end() ){
  1812. RetVal = atoi( it->second.c_str() );
  1813. };
  1814. };
  1815. };
  1816. deleteResult( result );
  1817. };
  1818. return RetVal;
  1819. };
  1820. uint32_t sqliteDBC::getServerID( CSTR identity ){
  1821. uint32_t RetVal = 0;
  1822. if(m_bIsConnected){
  1823. char tQBuf[CTEXT];
  1824. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  1825. #ifdef DEBUG_ALL
  1826. LOG(tQBuf);
  1827. #endif
  1828. AAssoc *result;
  1829. result = m_pDataStorage->Query( tQBuf );
  1830. if( result!= NULL ) {
  1831. if( result->size() > 0 ) {
  1832. Assoc * cArr;
  1833. Assoc::iterator it;
  1834. cArr = result->at(0);
  1835. it = cArr->find("id");
  1836. if( it != cArr->end() ){
  1837. RetVal = atoi( it->second.c_str() );
  1838. };
  1839. };
  1840. };
  1841. deleteResult( result );
  1842. };
  1843. return RetVal;
  1844. };
  1845. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  1846. uint32_t RetVal = 0;
  1847. if(m_bIsConnected){
  1848. char tQBuf[CTEXT];
  1849. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  1850. #ifdef DEBUG_ALL
  1851. LOG(tQBuf);
  1852. #endif
  1853. AAssoc *result;
  1854. result = m_pDataStorage->Query( tQBuf );
  1855. if( result!= NULL ) {
  1856. if( result->size() > 0 ) {
  1857. Assoc * cArr;
  1858. Assoc::iterator it;
  1859. cArr = result->at(0);
  1860. it = cArr->find("id");
  1861. if( it != cArr->end() ){
  1862. RetVal = atoi( it->second.c_str() );
  1863. };
  1864. };
  1865. };
  1866. deleteResult( result );
  1867. };
  1868. return RetVal;
  1869. }
  1870. DBC_TARGET_FLITER_DATA *sqliteDBC::getTargetFilterData( uint32_t ID ){
  1871. DBC_TARGET_FLITER_DATA * tVal = NULL;
  1872. if(m_bIsConnected){
  1873. char tQBuf[CTEXT];
  1874. AAssoc *result;
  1875. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID );
  1876. #ifdef DEBUG
  1877. LOG(tQBuf);
  1878. #endif
  1879. result = m_pDataStorage->Query( tQBuf );
  1880. if( result!= NULL ) {
  1881. if( result->size() > 0 ) {
  1882. Assoc *cArr;
  1883. tVal = (DBC_TARGET_FLITER_DATA*) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  1884. if( tVal != NULL ) {
  1885. memset( tVal, 0 , sizeof(DBC_TARGET_FLITER_DATA));
  1886. Assoc::iterator it;
  1887. tVal->id = ID;
  1888. cArr = result->at(0);
  1889. it = cArr->find("type");
  1890. if( it != cArr->end() ){
  1891. tVal->type = (TARGET_FILTER_TYPE) atoi( it->second.c_str() );
  1892. };
  1893. it = cArr->find("aux_id");
  1894. if( it != cArr->end() ){
  1895. tVal->aux_id = atoi( it->second.c_str() );
  1896. };
  1897. it = cArr->find("enabled");
  1898. if( it != cArr->end() ){
  1899. tVal->enabled = (bool) atoi( it->second.c_str() );
  1900. };
  1901. };
  1902. };
  1903. };
  1904. deleteResult( result );
  1905. };
  1906. return tVal;
  1907. };
  1908. TRAIN_FILTER_DATA *sqliteDBC::getTrainFilterData( uint32_t ID ){
  1909. TRAIN_FILTER_DATA * tVal = NULL;
  1910. if(m_bIsConnected){
  1911. char tQBuf[CTEXT];
  1912. AAssoc *result;
  1913. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID );
  1914. #ifdef DEBUG_EXTRA
  1915. LOG(tQBuf);
  1916. #endif
  1917. result = m_pDataStorage->Query( tQBuf );
  1918. if( result!= NULL ) {
  1919. if( result->size() > 0 ) {
  1920. Assoc *cArr;
  1921. tVal = (TRAIN_FILTER_DATA*) malloc(sizeof(TRAIN_FILTER_DATA));
  1922. if( tVal != NULL ) {
  1923. memset( tVal, 0 , sizeof(TRAIN_FILTER_DATA));
  1924. Assoc::iterator it;
  1925. cArr = result->at(0);
  1926. it = cArr->find("UPPER_SENSOR");
  1927. if( it != cArr->end() ){
  1928. tVal->nUpperSensorID = atoi( it->second.c_str() );
  1929. };
  1930. it = cArr->find("LOWER_SENSOR");
  1931. if( it != cArr->end() ){
  1932. tVal->nBottomSensorID = atoi( it->second.c_str() );
  1933. };
  1934. it = cArr->find("TRAIN_DETECT");
  1935. if( it != cArr->end() ){
  1936. tVal->bDetectTrain = (bool)atoi( it->second.c_str() );
  1937. };
  1938. it = cArr->find("TRAIN_WIDTH");
  1939. if( it != cArr->end() ){
  1940. tVal->nTrainWidth = atoi( it->second.c_str() );
  1941. };
  1942. it = cArr->find("DECISION_TIME");
  1943. if( it != cArr->end() ){
  1944. tVal->nDecisionTime = 1000 * atoi( it->second.c_str() );
  1945. };
  1946. it = cArr->find("SUSPECT_TIME");
  1947. if( it != cArr->end() ){
  1948. tVal->nSuspectTime = 1000 * atoi( it->second.c_str() );
  1949. };
  1950. it = cArr->find("TRACK_TRAIN");
  1951. if( it != cArr->end() ){
  1952. tVal->bTrackTrain = (bool)atoi( it->second.c_str() );
  1953. };
  1954. it = cArr->find("DEBUG");
  1955. if( it != cArr->end() ){
  1956. tVal->bDebugMode = (bool)atoi( it->second.c_str() );
  1957. };
  1958. };
  1959. };
  1960. };
  1961. deleteResult( result );
  1962. };
  1963. return tVal;
  1964. };
  1965. DBC_UART_DATA *sqliteDBC::getUartData( uint32_t UartID ){
  1966. DBC_UART_DATA *tVal = NULL;
  1967. if( m_bIsConnected ) {
  1968. char tQBuf[MAX_PATH_LEN];
  1969. AAssoc *result;
  1970. sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
  1971. #ifdef DEBUG_ALL
  1972. LOG(tQBuf);
  1973. #endif
  1974. result = m_pDataStorage->Query( tQBuf );
  1975. if( result!= NULL ) {
  1976. if( result->size() > 0 ) {
  1977. Assoc *cArr;
  1978. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  1979. if( tVal != NULL ) {
  1980. Assoc::iterator it;
  1981. cArr = result->at(0);
  1982. it = cArr->find("type");
  1983. if( it != cArr->end() ){
  1984. tVal->type = atoi( it->second.c_str() );
  1985. };
  1986. it = cArr->find("speed");
  1987. if( it != cArr->end() ){
  1988. tVal->speed = atoi( it->second.c_str() );
  1989. };
  1990. it = cArr->find("mode");
  1991. if( it != cArr->end() ){
  1992. tVal->mode = atoi( it->second.c_str() );
  1993. };
  1994. it = cArr->find("port");
  1995. if( it != cArr->end() ){
  1996. tVal->port = atoi( it->second.c_str() );
  1997. };
  1998. it = cArr->find("path");
  1999. if( it != cArr->end() ){
  2000. strncpy( tVal->path, it->second.c_str(), 128 );
  2001. };
  2002. };
  2003. };
  2004. };
  2005. deleteResult( result );
  2006. };
  2007. return tVal;
  2008. };
  2009. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  2010. uint32_t RetVal = 0;
  2011. if(m_bIsConnected){
  2012. char tQBuf[CTEXT];
  2013. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  2014. #ifdef DEBUG_ALL
  2015. LOG(tQBuf);
  2016. #endif
  2017. AAssoc *result;
  2018. result = m_pDataStorage->Query( tQBuf );
  2019. if( result!= NULL ) {
  2020. if( result->size() > 0 ) {
  2021. Assoc * cArr;
  2022. Assoc::iterator it;
  2023. cArr = result->at(0);
  2024. it = cArr->find("id");
  2025. if( it != cArr->end() ){
  2026. RetVal = atoi( it->second.c_str() );
  2027. };
  2028. };
  2029. };
  2030. deleteResult( result );
  2031. };
  2032. return RetVal;
  2033. };
  2034. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  2035. uint32_t tVal = 0;
  2036. if(m_bIsConnected){
  2037. char tQBuf[CTEXT];
  2038. AAssoc *result;
  2039. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  2040. #ifdef DEBUG_ALL
  2041. LOG(tQBuf);
  2042. #endif
  2043. result = m_pDataStorage->Query( tQBuf );
  2044. if( result!= NULL ) {
  2045. if( result->size() > 0 ) {
  2046. Assoc *cArr;
  2047. Assoc::iterator it;
  2048. cArr = result->at(0);
  2049. it = cArr->find("id");
  2050. if( it != cArr->end() ){
  2051. tVal = atoi( it->second.c_str() );
  2052. };
  2053. };
  2054. };
  2055. deleteResult( result );
  2056. };
  2057. return tVal;
  2058. };
  2059. DBC_INT_LIST *sqliteDBC::listFilters( uint32_t ServerID ){
  2060. DBC_INT_LIST * tVal = NULL;
  2061. if(m_bIsConnected){
  2062. char tQBuf[CTEXT];
  2063. AAssoc *result;
  2064. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1" );
  2065. #ifdef DEBUG
  2066. LOG(tQBuf);
  2067. #endif
  2068. result = m_pDataStorage->Query( tQBuf );
  2069. if( result!= NULL ) {
  2070. if( result->size() > 0 ) {
  2071. Assoc *cArr;
  2072. tVal = new DBC_INT_LIST();
  2073. if( tVal != NULL ) {
  2074. Assoc::iterator it;
  2075. for(int i=0;i<result->size();i++) {
  2076. cArr = result->at(i);
  2077. it = cArr->find("id");
  2078. if( it != cArr->end() ){
  2079. tVal->push_back( atoi( it->second.c_str() ));
  2080. };
  2081. };
  2082. };
  2083. };
  2084. };
  2085. deleteResult( result );
  2086. };
  2087. return tVal;
  2088. };
  2089. DBC_INT_LIST *sqliteDBC::listLRFUploads( uint32_t deviceID ){
  2090. DBC_INT_LIST * tVal = NULL;
  2091. if(m_bIsConnected){
  2092. char tQBuf[CTEXT];
  2093. AAssoc *result;
  2094. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID );
  2095. #ifdef DEBUG
  2096. LOG(tQBuf);
  2097. #endif
  2098. result = m_pDataStorage->Query( tQBuf );
  2099. if( result!= NULL ) {
  2100. if( result->size() > 0 ) {
  2101. Assoc *cArr;
  2102. tVal = new DBC_INT_LIST();
  2103. if( tVal != NULL ) {
  2104. Assoc::iterator it;
  2105. for(int i=0;i<result->size();i++) {
  2106. cArr = result->at(i);
  2107. it = cArr->find("id");
  2108. if( it != cArr->end() ){
  2109. tVal->push_back( atoi( it->second.c_str() ));
  2110. };
  2111. };
  2112. };
  2113. };
  2114. };
  2115. deleteResult( result );
  2116. };
  2117. return tVal;
  2118. };
  2119. DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
  2120. DBC_INT_LIST * tVal = NULL;
  2121. if(m_bIsConnected){
  2122. char tQBuf[CTEXT];
  2123. AAssoc *result;
  2124. switch( listMode ){
  2125. case LIST_VALID_RECORDS:
  2126. 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" );
  2127. break;
  2128. case LIST_INVALID_RECORDS:
  2129. 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" );
  2130. break;
  2131. default:
  2132. 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" );
  2133. };
  2134. #ifdef DEBUG_ALL
  2135. LOG(tQBuf);
  2136. #endif
  2137. result = m_pDataStorage->Query( tQBuf );
  2138. if( result!= NULL ) {
  2139. if( result->size() > 0 ) {
  2140. Assoc *cArr;
  2141. tVal = new DBC_INT_LIST();
  2142. if( tVal != NULL ) {
  2143. Assoc::iterator it;
  2144. for(int i=0;i<result->size();i++) {
  2145. cArr = result->at(i);
  2146. it = cArr->find("id");
  2147. if( it != cArr->end() ){
  2148. tVal->push_back( atoi( it->second.c_str() ));
  2149. };
  2150. };
  2151. };
  2152. };
  2153. };
  2154. deleteResult( result );
  2155. };
  2156. return tVal;
  2157. };
  2158. DBC_INT_LIST *sqliteDBC::listModules(){
  2159. DBC_INT_LIST * tVal = NULL;
  2160. if(m_bIsConnected){
  2161. AAssoc *result;
  2162. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  2163. if( result!= NULL ) {
  2164. if( result->size() > 0 ) {
  2165. Assoc *cArr;
  2166. tVal = new DBC_INT_LIST();
  2167. if( tVal != NULL ) {
  2168. Assoc::iterator it;
  2169. for(int i=0;i<result->size();i++) {
  2170. cArr = result->at(i);
  2171. it = cArr->find("id");
  2172. if( it != cArr->end() ){
  2173. tVal->push_back( atoi( it->second.c_str() ));
  2174. };
  2175. };
  2176. };
  2177. };
  2178. };
  2179. deleteResult( result );
  2180. };
  2181. return tVal;
  2182. };
  2183. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  2184. DBC_INT_LIST * tVal = NULL;
  2185. char tParam[CTEXT];
  2186. //memset( tParam, 0, CTEXT );
  2187. sprintf(tParam,"0");
  2188. if( old_ids != NULL ) {
  2189. char tBuffer[CTEXT];
  2190. for(int i = 0; i< old_ids->size(); i++ ) {
  2191. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  2192. strncpy( tParam, tBuffer, CTEXT );
  2193. };
  2194. };
  2195. if(m_bIsConnected){
  2196. char tQBuf[CTEXT];
  2197. AAssoc *result;
  2198. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  2199. #ifdef DEBUG_ALL
  2200. LOG(tQBuf);
  2201. #endif
  2202. result = m_pDataStorage->Query( tQBuf );
  2203. if( result!= NULL ) {
  2204. if( result->size() > 0 ) {
  2205. Assoc *cArr;
  2206. tVal = new DBC_INT_LIST();
  2207. if( tVal != NULL ) {
  2208. Assoc::iterator it;
  2209. for(int i=0;i<result->size();i++) {
  2210. cArr = result->at(i);
  2211. it = cArr->find("id");
  2212. if( it != cArr->end() ){
  2213. tVal->push_back( atoi( it->second.c_str() ));
  2214. };
  2215. };
  2216. };
  2217. };
  2218. };
  2219. deleteResult( result );
  2220. };
  2221. return tVal;
  2222. };
  2223. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  2224. DBC_INT_LIST * tVal = NULL;
  2225. char tParam[CTEXT];
  2226. //memset( tParam, 0, CTEXT );
  2227. sprintf(tParam,"0");
  2228. if( old_ids != NULL ) {
  2229. char tBuffer[CTEXT];
  2230. for(int i = 0; i< old_ids->size(); i++ ) {
  2231. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  2232. strncpy( tParam, tBuffer, CTEXT );
  2233. };
  2234. };
  2235. if( m_bIsConnected ){
  2236. char tQBuf[CTEXT];
  2237. AAssoc *result;
  2238. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam );
  2239. #ifdef DEBUG_EXTRA
  2240. LOG(tQBuf);
  2241. #endif
  2242. result = m_pDataStorage->Query( tQBuf );
  2243. if( result!= NULL ) {
  2244. if( result->size() > 0 ) {
  2245. Assoc *cArr;
  2246. tVal = new DBC_INT_LIST();
  2247. if( tVal != NULL ) {
  2248. Assoc::iterator it;
  2249. for(int i=0;i<result->size();i++) {
  2250. cArr = result->at(i);
  2251. it = cArr->find("id");
  2252. if( it != cArr->end() ){
  2253. tVal->push_back( atoi( it->second.c_str() ));
  2254. };
  2255. };
  2256. };
  2257. };
  2258. };
  2259. deleteResult( result );
  2260. };
  2261. return tVal;
  2262. };
  2263. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  2264. DBC_INT_LIST * tVal = NULL;
  2265. if( m_bIsConnected ){
  2266. char tQBuf[1024];
  2267. AAssoc *result;
  2268. if( all ) {
  2269. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  2270. } else {
  2271. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1" );
  2272. };
  2273. #ifdef DEBUG_EXTRA
  2274. LOG(tQBuf);
  2275. #endif
  2276. result = m_pDataStorage->Query( tQBuf );
  2277. if( result!= NULL ) {
  2278. if( result->size() > 0 ) {
  2279. Assoc *cArr;
  2280. tVal = new DBC_INT_LIST();
  2281. if( tVal != NULL ) {
  2282. Assoc::iterator it;
  2283. for(int i=0; i <result->size();i++) {
  2284. cArr = result->at(i);
  2285. it = cArr->find("id");
  2286. if( it != cArr->end() ){
  2287. tVal->push_back( atoi( it->second.c_str() ));
  2288. };
  2289. };
  2290. };
  2291. };
  2292. };
  2293. deleteResult( result );
  2294. };
  2295. return tVal;
  2296. };
  2297. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  2298. DBC_INT_LIST * tVal = NULL;
  2299. if(m_bIsConnected){
  2300. char tQBuf[CTEXT];
  2301. AAssoc *result;
  2302. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  2303. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  2304. #ifdef DEBUG
  2305. LOG(tQBuf);
  2306. #endif
  2307. result = m_pDataStorage->Query( tQBuf );
  2308. if( result!= NULL ) {
  2309. if( result->size() > 0 ) {
  2310. Assoc *cArr;
  2311. tVal = new DBC_INT_LIST();
  2312. if( tVal != NULL ) {
  2313. Assoc::iterator it;
  2314. for(int i=0;i<result->size();i++) {
  2315. cArr = result->at(i);
  2316. it = cArr->find("id");
  2317. if( it != cArr->end() ){
  2318. tVal->push_back( atoi( it->second.c_str() ));
  2319. };
  2320. };
  2321. };
  2322. };
  2323. };
  2324. deleteResult( result );
  2325. };
  2326. return tVal;
  2327. };
  2328. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  2329. if( m_bIsConnected ){
  2330. m_nSiteID = SiteID;
  2331. char tQBuf[MAX_PATH_LEN];
  2332. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  2333. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  2334. #ifdef DEBUG_ALL
  2335. LOG(tQBuf);
  2336. #endif
  2337. m_pDataStorage->SilentQuery( tQBuf );
  2338. m_nServerID = getServerID( identity );
  2339. *ServID = m_nServerID;
  2340. if( m_nServerID ) {
  2341. //if ( bRecreateMemStructures )
  2342. initMemDB( m_nServerID );
  2343. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  2344. #ifdef DEBUG_ALL
  2345. LOG(tQBuf);
  2346. #endif
  2347. m_pDataMemory->SilentQuery( tQBuf );
  2348. 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 );
  2349. #ifdef DEBUG_ALL
  2350. LOG(tQBuf);
  2351. #endif
  2352. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  2353. if( m_nServerNodeID== 0 ){
  2354. m_nServerNodeID = getServerNode( m_nServerID );
  2355. };
  2356. *ServNode = m_nServerNodeID;
  2357. // Clenup Nodes and services ...
  2358. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  2359. #ifdef DEBUG_ALL
  2360. LOG(tQBuf);
  2361. #endif
  2362. m_pDataStorage->SilentQuery( tQBuf );
  2363. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0 WHERE `manual`=0" );
  2364. #ifdef DEBUG_ALL
  2365. LOG(tQBuf);
  2366. #endif
  2367. m_pDataStorage->SilentQuery( tQBuf );
  2368. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )" );
  2369. #ifdef DEBUG_ALL
  2370. LOG(tQBuf);
  2371. #endif
  2372. m_pDataStorage->SilentQuery( tQBuf );
  2373. //sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  2374. #ifdef DEBUG_ALL
  2375. //LOG(tQBuf);
  2376. #endif
  2377. //m_pDataStorage->SilentQuery( tQBuf );
  2378. //sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  2379. #ifdef DEBUG_ALL
  2380. //LOG(tQBuf);
  2381. #endif
  2382. //m_pDataStorage->SilentQuery( tQBuf );
  2383. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  2384. #ifdef DEBUG_ALL
  2385. // LOG(tQBuf);
  2386. #endif
  2387. // m_pDataStorage->SilentQuery( tQBuf );
  2388. //cleanResources( tServerNodeID );
  2389. setNodeOnline( m_nServerNodeID );
  2390. };
  2391. };
  2392. };
  2393. bool sqliteDBC::setLRFDataStatus( uint32_t lrfID, uint8_t ST ){
  2394. if( m_bIsConnected ) {
  2395. char tQBuf[MAX_PATH_LEN];
  2396. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID );
  2397. #ifdef DEBUG
  2398. LOG(tQBuf);
  2399. #endif
  2400. m_pDataStorage->SilentQuery( tQBuf );
  2401. return true;
  2402. };
  2403. return false;
  2404. };
  2405. bool sqliteDBC::setLRFDetectionProfile( uint32_t lrfID, uint8_t profileID ){
  2406. if( m_bIsConnected ) {
  2407. char tQBuf[MAX_PATH_LEN];
  2408. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID );
  2409. #ifdef DEBUG_ALL
  2410. LOG(tQBuf);
  2411. #endif
  2412. m_pDataStorage->SilentQuery( tQBuf );
  2413. return true;
  2414. };
  2415. return false;
  2416. };
  2417. bool sqliteDBC::setLRFMaskStatus( uint32_t maskID, bool status ){
  2418. if( m_bIsConnected ) {
  2419. char tQBuf[MAX_PATH_LEN];
  2420. char S[2];
  2421. if (status ) {
  2422. S[0] = 'E';
  2423. S[1] = (char)0;
  2424. } else {
  2425. S[0] = 'D';
  2426. S[1] = (char)0;
  2427. };
  2428. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='%s' WHERE `id`=%d", S, maskID );
  2429. #ifdef DEBUG_ALL
  2430. LOG(tQBuf);
  2431. #endif
  2432. m_pDataStorage->SilentQuery( tQBuf );
  2433. return true;
  2434. };
  2435. return false;
  2436. };
  2437. bool sqliteDBC::setLRFAltSerial( uint32_t lrfID, CSTR Serial ){
  2438. if( m_bIsConnected ) {
  2439. char tQBuf[MAX_PATH_LEN];
  2440. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID );
  2441. #ifdef DEBUG_ALL
  2442. LOG(tQBuf);
  2443. #endif
  2444. m_pDataStorage->SilentQuery( tQBuf );
  2445. return true;
  2446. };
  2447. return false;
  2448. };
  2449. bool sqliteDBC::setLRFAltVerison( uint32_t lrfID, CSTR version ){
  2450. if( m_bIsConnected ) {
  2451. char tQBuf[MAX_PATH_LEN];
  2452. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID );
  2453. #ifdef DEBUG_ALL
  2454. LOG(tQBuf);
  2455. #endif
  2456. m_pDataStorage->SilentQuery( tQBuf );
  2457. return true;
  2458. };
  2459. return false;
  2460. };
  2461. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  2462. if(m_bIsConnected){
  2463. char tQBuf[CTEXT];
  2464. if( NodeID ) {
  2465. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  2466. #ifdef DEBUG_ALL
  2467. LOG(tQBuf);
  2468. #endif
  2469. m_pDataStorage->SilentQuery( tQBuf );
  2470. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  2471. #ifdef DEBUG_ALL
  2472. LOG(tQBuf);
  2473. #endif
  2474. m_pDataStorage->SilentQuery( tQBuf );
  2475. return true;
  2476. };
  2477. };
  2478. return false;
  2479. };
  2480. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  2481. if(m_bIsConnected){
  2482. char tQBuf[CTEXT];
  2483. if( NodeID ) {
  2484. sprintf(tQBuf,"UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID );
  2485. #ifdef DEBUG
  2486. LOG(tQBuf);
  2487. #endif
  2488. m_pDataStorage->SilentQuery( tQBuf );
  2489. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  2490. #ifdef DEBUG
  2491. LOG(tQBuf);
  2492. #endif
  2493. m_pDataStorage->SilentQuery( tQBuf );
  2494. return true;
  2495. };
  2496. };
  2497. return false;
  2498. };
  2499. bool sqliteDBC::setRessourceActive( uint32_t RessourceID, bool Status ){
  2500. if(m_bIsConnected){
  2501. char tQBuf[CTEXT];
  2502. if( RessourceID ) {
  2503. sprintf(tQBuf,"UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID );
  2504. #ifdef DEBUG
  2505. LOG(tQBuf);
  2506. #endif
  2507. m_pDataStorage->SilentQuery( tQBuf );
  2508. return true;
  2509. };
  2510. };
  2511. return false;
  2512. };
  2513. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  2514. if( m_bIsConnected ) {
  2515. char tQBuf[MAX_PATH_LEN];
  2516. if ( status ) {
  2517. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  2518. } else {
  2519. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  2520. };
  2521. #ifdef DEBUG
  2522. LOG(tQBuf);
  2523. #endif
  2524. m_pDataStorage->SilentQuery( tQBuf );
  2525. return true;
  2526. };
  2527. return false;
  2528. };
  2529. void sqliteDBC::unregisterServer( void ){
  2530. if( m_bIsConnected ) {
  2531. char tQBuf[MAX_PATH_LEN];
  2532. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  2533. #ifdef DEBUG_ALL
  2534. LOG(tQBuf);
  2535. #endif
  2536. m_pDataStorage->SilentQuery( tQBuf );
  2537. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  2538. #ifdef DEBUG_ALL
  2539. LOG(tQBuf);
  2540. #endif
  2541. m_pDataStorage->SilentQuery( tQBuf );
  2542. };
  2543. setNodeOffline( m_nServerNodeID );
  2544. };
  2545. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  2546. if( m_bIsConnected ) {
  2547. char tQBuf[MAX_PATH_LEN];
  2548. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  2549. ptr->OS_INFO,
  2550. ptr->HW_INFO,
  2551. m_nSiteID
  2552. );
  2553. #ifdef DEBUG_ALL
  2554. LOG(tQBuf);
  2555. #endif
  2556. m_pDataStorage->SilentQuery( tQBuf );
  2557. return true;
  2558. };
  2559. return false;
  2560. };
  2561. bool sqliteDBC::fillShadow2D( uint32_t ID, SHADOW & SH ){
  2562. if( m_bIsConnected ){
  2563. char tQBuf[CTEXT];
  2564. AAssoc *result;
  2565. 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 );
  2566. LOG(tQBuf);
  2567. result = m_pDataStorage->Query( tQBuf );
  2568. if( result!= NULL ) {
  2569. if( result->size() > 0 ) {
  2570. SH.m_nEnabled = 0xFFFFFFFF;
  2571. SH.m_nPolygonCount = result->size();
  2572. SH.pPolygones = (SHADOW_POLY *) calloc( SH.m_nPolygonCount, sizeof( SHADOW_POLY ) );
  2573. if ( SH.pPolygones ) {
  2574. Assoc * cArr;
  2575. for ( uint8_t idx = 0; idx < result->size(); idx++ ) {
  2576. Assoc::iterator it;
  2577. cArr = result->at(idx);
  2578. it = cArr->find("id");
  2579. if( it != cArr->end() ){
  2580. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  2581. };
  2582. it = cArr->find("layer");
  2583. if( it != cArr->end() ){
  2584. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  2585. };
  2586. it = cArr->find("status");
  2587. if( it != cArr->end() ){
  2588. SH.m_nEnabled &= ( it->second.c_str()[0] == 'E' )?0xFFFFFFFF:~(1<<SH.pPolygones[idx].bType);
  2589. };
  2590. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID );
  2591. LOG(tQBuf);
  2592. AAssoc *v_result = m_pDataStorage->Query( tQBuf );
  2593. if( v_result!= NULL ) {
  2594. if( v_result->size() > 0 ) {
  2595. SH.pPolygones[idx].wCount = v_result->size();
  2596. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc( SH.pPolygones[idx].wCount, sizeof( HZ_POINT ) );
  2597. if(SH.pPolygones[idx].pPoint ) {
  2598. for ( uint32_t zdx =0; zdx < SH.pPolygones[idx].wCount; zdx++ ) {
  2599. cArr = v_result->at(zdx);
  2600. it = cArr->find("x");
  2601. if( it != cArr->end() ){
  2602. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  2603. };
  2604. it = cArr->find("y");
  2605. if( it != cArr->end() ){
  2606. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  2607. };
  2608. LOG("Point Added!");
  2609. };
  2610. } else {
  2611. LOG("No mem for Points!");
  2612. };
  2613. } else {
  2614. LOG("No Points!");
  2615. SH.pPolygones[idx].wCount = 0;
  2616. SH.pPolygones[idx].pPoint = NULL;
  2617. };
  2618. deleteResult( v_result );
  2619. };
  2620. };
  2621. };
  2622. };
  2623. deleteResult( result );
  2624. };
  2625. return true;
  2626. };
  2627. return false;
  2628. };
  2629. bool sqliteDBC::getAppDebugMode(){
  2630. bool rVal = false;
  2631. if(m_bIsConnected){
  2632. char tQBuf[CTEXT];
  2633. AAssoc *result;
  2634. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1" );
  2635. #ifdef DEBUG_ALL
  2636. LOG(tQBuf);
  2637. #endif
  2638. result = m_pDataStorage->Query( tQBuf );
  2639. if( result!= NULL ) {
  2640. if( result->size() > 0 ) {
  2641. Assoc *cArr;
  2642. Assoc::iterator it;
  2643. cArr = result->at(0);
  2644. it = cArr->find("work_mode");
  2645. if( it != cArr->end() ){
  2646. rVal = (bool) atoi( it->second.c_str() );
  2647. };
  2648. };
  2649. };
  2650. deleteResult( result );
  2651. };
  2652. return rVal;
  2653. };
  2654. DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
  2655. DBC_CAM_DATA *tVal = NULL;
  2656. if(m_bIsConnected){
  2657. char tQBuf[CTEXT];
  2658. AAssoc *result;
  2659. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  2660. #ifdef DEBUG_ALL
  2661. LOG(tQBuf);
  2662. #endif
  2663. result = m_pDataStorage->Query( tQBuf );
  2664. if( result!= NULL ) {
  2665. if( result->size() > 0 ) {
  2666. Assoc *cArr;
  2667. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  2668. if( tVal != NULL ) {
  2669. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  2670. tVal->id = ID;
  2671. Assoc::iterator it;
  2672. cArr = result->at(0);
  2673. it = cArr->find("type");
  2674. if( it != cArr->end() ){
  2675. tVal->type = atoi( it->second.c_str() );
  2676. };
  2677. it = cArr->find("online");
  2678. if( it != cArr->end() ){
  2679. tVal->online = (bool) atoi( it->second.c_str() );
  2680. };
  2681. it = cArr->find("stream_id");
  2682. if( it != cArr->end() ){
  2683. tVal->stream_id = atoi( it->second.c_str() );
  2684. };
  2685. it = cArr->find("hardware");
  2686. if( it != cArr->end() ){
  2687. strncpy( tVal->hardware, it->second.c_str(), 32 );
  2688. };
  2689. it = cArr->find("name");
  2690. if( it != cArr->end() ){
  2691. strncpy( tVal->name, it->second.c_str(), 64 );
  2692. };
  2693. it = cArr->find("status");
  2694. if( it != cArr->end() ){
  2695. tVal->status = atoi( it->second.c_str() );
  2696. };
  2697. it = cArr->find("session");
  2698. if( it != cArr->end() ){
  2699. strncpy( tVal->session, it->second.c_str(), 32 );
  2700. };
  2701. it = cArr->find("protocol");
  2702. if( it != cArr->end() ){
  2703. tVal->protocol = atoi( it->second.c_str() );
  2704. };
  2705. it = cArr->find("protocol_aux");
  2706. if( it != cArr->end() ){
  2707. tVal->protocol_aux = atoi( it->second.c_str() );
  2708. };
  2709. it = cArr->find("uart_id");
  2710. if( it != cArr->end() ){
  2711. tVal->uart_id = atoi( it->second.c_str() );
  2712. };
  2713. it = cArr->find("x");
  2714. if( it != cArr->end() ){
  2715. tVal->x = atof( it->second.c_str() );
  2716. };
  2717. it = cArr->find("y");
  2718. if( it != cArr->end() ){
  2719. tVal->y = atof( it->second.c_str() );
  2720. };
  2721. it = cArr->find("z");
  2722. if( it != cArr->end() ){
  2723. tVal->z = atof( it->second.c_str() );
  2724. };
  2725. it = cArr->find("x_rot");
  2726. if( it != cArr->end() ){
  2727. tVal->x_rot = atof( it->second.c_str() );
  2728. };
  2729. it = cArr->find("y_rot");
  2730. if( it != cArr->end() ){
  2731. tVal->y_rot = atof( it->second.c_str() );
  2732. };
  2733. it = cArr->find("z_rot");
  2734. if( it != cArr->end() ){
  2735. tVal->z_rot = atof( it->second.c_str() );
  2736. };
  2737. it = cArr->find("imager_type");
  2738. if( it != cArr->end() ){
  2739. tVal->imager_type = atoi( it->second.c_str() );
  2740. };
  2741. it = cArr->find("lens_f");
  2742. if( it != cArr->end() ){
  2743. tVal->lens_f = atoi( it->second.c_str() );
  2744. };
  2745. it = cArr->find("ptz_speed_step");
  2746. if( it != cArr->end() ){
  2747. tVal->ptz_speed_step = atof( it->second.c_str() );
  2748. };
  2749. it = cArr->find("pan_model");
  2750. if( it != cArr->end() ){
  2751. tVal->pan_model = atoi( it->second.c_str() );
  2752. };
  2753. it = cArr->find("pan_angles");
  2754. if( it != cArr->end() ){
  2755. tVal->pan_angles = atoi( it->second.c_str() );
  2756. };
  2757. it = cArr->find("tilt_model");
  2758. if( it != cArr->end() ){
  2759. tVal->tilt_model = atoi( it->second.c_str() );
  2760. };
  2761. it = cArr->find("tilt_angles");
  2762. if( it != cArr->end() ){
  2763. tVal->tilt_angles = atoi( it->second.c_str() );
  2764. };
  2765. it = cArr->find("zoom_model");
  2766. if( it != cArr->end() ){
  2767. tVal->zoom_model = atoi( it->second.c_str() );
  2768. };
  2769. it = cArr->find("zoom");
  2770. if( it != cArr->end() ){
  2771. tVal->zoom = atof( it->second.c_str() );
  2772. };
  2773. //it = cArr->find("max_range");
  2774. //if( it != cArr->end() ){
  2775. // tVal->max_range = atof( it->second.c_str() );
  2776. //};
  2777. it = cArr->find("access_login");
  2778. if( it != cArr->end() ){
  2779. strncpy( tVal->username, it->second.c_str(), 16 );
  2780. };
  2781. it = cArr->find("access_pass");
  2782. if( it != cArr->end() ){
  2783. strncpy( tVal->password, it->second.c_str(), 16 );
  2784. };
  2785. it = cArr->find("uuid");
  2786. if( it != cArr->end() ){
  2787. strncpy( tVal->uuid, it->second.c_str(), 64 );
  2788. };
  2789. it = cArr->find("mac");
  2790. if( it != cArr->end() ){
  2791. strncpy( tVal->mac, it->second.c_str(), 16 );
  2792. };
  2793. it = cArr->find("ip");
  2794. if( it != cArr->end() ){
  2795. strncpy( tVal->ip, it->second.c_str(), 16 );
  2796. };
  2797. it = cArr->find("port");
  2798. if( it != cArr->end() ){
  2799. tVal->port = atoi( it->second.c_str() );
  2800. };
  2801. it = cArr->find("onvif_access");
  2802. if( it != cArr->end() ){
  2803. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  2804. };
  2805. it = cArr->find("onvif_ctrl");
  2806. if( it != cArr->end() ){
  2807. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  2808. };
  2809. it = cArr->find("ptz_token");
  2810. if( it != cArr->end() ){
  2811. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  2812. };
  2813. it = cArr->find("trigger_mode");
  2814. if( it != cArr->end() ){
  2815. tVal->triggering_mode = atoi( it->second.c_str() );
  2816. };
  2817. it = cArr->find("auth_mode");
  2818. if( it != cArr->end() ){
  2819. tVal->auth_mode = atoi( it->second.c_str() );
  2820. };
  2821. it = cArr->find("ptz_axel");
  2822. if( it != cArr->end() ){
  2823. tVal->ptz_axel = atoi( it->second.c_str() );
  2824. };
  2825. it = cArr->find("manual");
  2826. if( it != cArr->end() ){
  2827. tVal->manual = (bool) atoi( it->second.c_str() );
  2828. };
  2829. it = cArr->find("homing_enable");
  2830. if( it != cArr->end() ){
  2831. tVal->homing_enable = (bool) atoi( it->second.c_str() );
  2832. };
  2833. it = cArr->find("homing_to");
  2834. if( it != cArr->end() ){
  2835. tVal->homing_to = atoi( it->second.c_str() );
  2836. };
  2837. it = cArr->find("home_token");
  2838. if( it != cArr->end() ){
  2839. strncpy( tVal->home_token, it->second.c_str(), 32 );
  2840. };
  2841. };
  2842. };
  2843. };
  2844. deleteResult( result );
  2845. };
  2846. return tVal;
  2847. };
  2848. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  2849. uint32_t RetVal = 0;
  2850. if(m_bIsConnected){
  2851. char tQBuf[CTEXT];
  2852. AAssoc *result;
  2853. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  2854. #ifdef DEBUG_ALL
  2855. LOG(tQBuf);
  2856. #endif
  2857. result = m_pDataStorage->Query( tQBuf );
  2858. if( result!= NULL ) {
  2859. if( result->size() > 0 ) {
  2860. Assoc *cArr;
  2861. Assoc::iterator it;
  2862. cArr = result->at(0);
  2863. it = cArr->find("id");
  2864. if( it != cArr->end() ){
  2865. RetVal = atoi( it->second.c_str() );
  2866. };
  2867. };
  2868. };
  2869. deleteResult( result );
  2870. };
  2871. return RetVal;
  2872. };
  2873. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  2874. switch ( pid ) {
  2875. case 0: return strdup( m_pDbFile );
  2876. };
  2877. return NULL;
  2878. };
  2879. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  2880. if(m_bIsConnected) {
  2881. char tQBuf[MAX_PATH_LEN];
  2882. char tIpBuf[CTEXT];
  2883. char *tPos;
  2884. char *tPos2;
  2885. uint16_t tPort = 0;
  2886. tPos = strstr(Addr,"//");
  2887. if( tPos != NULL ) {
  2888. tPos2 = strstr( tPos + 2,":");
  2889. if( tPos2 != NULL ) {
  2890. LOG("IP Port Defined!");
  2891. int len, len2, len3;
  2892. len = tPos2 - tPos - 2;
  2893. strncpy( tIpBuf, tPos + 2, len);
  2894. tIpBuf[len] = (char)0;
  2895. tPort = atoi( tPos2 + 1 );
  2896. //strncpy( tPath, tPos + 2 + len, CTEXT );
  2897. } else {
  2898. tPos2 = strstr( tPos + 2,"/");
  2899. if( tPos2 != NULL ) {
  2900. int len, len2, len3;
  2901. len = tPos2 - tPos - 2;
  2902. strncpy( tIpBuf, tPos + 2, len);
  2903. tIpBuf[len] = (char)0;
  2904. //strncpy( tPath, tPos + 2 + len, CTEXT );
  2905. } else {
  2906. tPos2 = strstr( tPos + 2,"/");
  2907. LOG_ERROR("Pos 2 - err");
  2908. tIpBuf[0] = (char)0;
  2909. };
  2910. };
  2911. } else {
  2912. LOG_ERROR("Pos 0 - err");
  2913. tIpBuf[0] = (char)0;
  2914. };
  2915. if ( tPort ) {
  2916. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d",
  2917. Addr,
  2918. tIpBuf,
  2919. tPort,
  2920. CamID
  2921. );
  2922. } else {
  2923. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  2924. Addr,
  2925. tIpBuf,
  2926. CamID
  2927. );
  2928. }
  2929. #ifdef DEBUG
  2930. LOG(tQBuf);
  2931. #endif
  2932. m_pDataStorage->SilentQuery( tQBuf );
  2933. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  2934. #ifdef DEBUG_ALL
  2935. LOG(tQBuf);
  2936. #endif
  2937. m_pDataStorage->SilentQuery( tQBuf );
  2938. return true;
  2939. };
  2940. return false;
  2941. };
  2942. bool sqliteDBC::updateCamOnlineStatus( uint32_t camID, bool online ){
  2943. if(m_bIsConnected) {
  2944. char tQBuf[MAX_PATH_LEN];
  2945. if ( online ) {
  2946. sprintf( tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID );
  2947. } else {
  2948. sprintf( tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID );
  2949. };
  2950. m_pDataStorage->SilentQuery(tQBuf);
  2951. if ( online ) {
  2952. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  2953. } else {
  2954. sprintf( tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID );
  2955. };
  2956. m_pDataStorage->SilentQuery(tQBuf);
  2957. return true;
  2958. };
  2959. return false;
  2960. };
  2961. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  2962. if(m_bIsConnected) {
  2963. uint32_t cID = getCamId( uuid );
  2964. if ( cID ) {
  2965. return updateIPCam( cID, Addr );
  2966. };
  2967. };
  2968. return false;
  2969. };
  2970. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  2971. if(m_bIsConnected) {
  2972. uint32_t cID = getCamId( uuid );
  2973. if ( cID ) {
  2974. return updateLocalCam( cID );
  2975. };
  2976. };
  2977. return false;
  2978. };
  2979. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  2980. if(m_bIsConnected) {
  2981. char tQBuf[MAX_PATH_LEN];
  2982. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  2983. #ifdef DEBUG
  2984. LOG(tQBuf);
  2985. #endif
  2986. m_pDataStorage->SilentQuery( tQBuf );
  2987. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  2988. #ifdef DEBUG
  2989. LOG(tQBuf);
  2990. #endif
  2991. m_pDataStorage->SilentQuery( tQBuf );
  2992. return true;
  2993. };
  2994. return false;
  2995. };
  2996. bool sqliteDBC::updateLRFComStat( uint32_t lrfID, uint32_t ErrorPerMinute ){
  2997. if( m_bIsConnected ) {
  2998. char tQBuf[MAX_PATH_LEN];
  2999. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID );
  3000. #ifdef DEBUG_ALL
  3001. LOG(tQBuf);
  3002. #endif
  3003. m_pDataStorage->SilentQuery( tQBuf );
  3004. return true;
  3005. };
  3006. return false;
  3007. };
  3008. bool sqliteDBC::updateLRFFrozenStatus( uint32_t lrfID, bool FFT, bool SFT ){
  3009. if( m_bIsConnected ) {
  3010. char tQBuf[MAX_PATH_LEN];
  3011. sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID );
  3012. #ifdef DEBUG
  3013. LOG(tQBuf);
  3014. #endif
  3015. m_pDataStorage->SilentQuery( tQBuf );
  3016. return true;
  3017. };
  3018. return false;
  3019. };
  3020. 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 ){
  3021. if( m_bIsConnected ) {
  3022. char tQBuf[MAX_PATH_LEN];
  3023. if( master ) {
  3024. sprintf( tQBuf, "UPDATE 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",
  3025. lrfID,
  3026. profileID,
  3027. nOrder,
  3028. nOffset,
  3029. nDelta,
  3030. nFlip,
  3031. nWidth,
  3032. bSST,
  3033. nSun,
  3034. GP,
  3035. spikes,
  3036. nOrder,
  3037. nOffset,
  3038. nDelta,
  3039. nFlip,
  3040. nWidth,
  3041. bSST,
  3042. nSun,
  3043. GP,
  3044. spikes );
  3045. } else {
  3046. 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",
  3047. lrfID,
  3048. profileID,
  3049. nOrder,
  3050. nOffset,
  3051. nDelta,
  3052. nFlip,
  3053. nWidth,
  3054. bSST,
  3055. nSun,
  3056. GP,
  3057. spikes,
  3058. nOrder,
  3059. nOffset,
  3060. nDelta,
  3061. nFlip,
  3062. nWidth,
  3063. bSST,
  3064. nSun,
  3065. GP,
  3066. spikes );
  3067. };
  3068. #ifdef DEBUG_ALL
  3069. LOG(tQBuf);
  3070. #endif
  3071. m_pDataStorage->SilentQuery( tQBuf );
  3072. return true;
  3073. };
  3074. return false;
  3075. };
  3076. bool sqliteDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){
  3077. if( m_bIsConnected ) {
  3078. char tQBuf[MAX_PATH_LEN];
  3079. CSTR tES = m_pDataStorage->escapeString( Serial );
  3080. if( tES ) {
  3081. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID );
  3082. free( tES );
  3083. #ifdef DEBUG_ALL
  3084. LOG(tQBuf);
  3085. #endif
  3086. m_pDataStorage->SilentQuery( tQBuf );
  3087. return true;
  3088. };
  3089. };
  3090. return false;
  3091. };
  3092. bool sqliteDBC::updateLRFUpload( uint32_t id, uint8_t status ){
  3093. if( m_bIsConnected ) {
  3094. char tQBuf[MAX_PATH_LEN];
  3095. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id );
  3096. #ifdef DEBUG_ALL
  3097. LOG(tQBuf);
  3098. #endif
  3099. m_pDataStorage->SilentQuery( tQBuf );
  3100. return true;
  3101. };
  3102. return false;
  3103. };
  3104. bool sqliteDBC::updateLRFUnfinishedUploads( uint32_t deviceID ){
  3105. if( m_bIsConnected ) {
  3106. char tQBuf[MAX_PATH_LEN];
  3107. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID );
  3108. #ifdef DEBUG_ALL
  3109. LOG(tQBuf);
  3110. #endif
  3111. m_pDataStorage->SilentQuery( tQBuf );
  3112. return true;
  3113. };
  3114. return false;
  3115. };
  3116. bool sqliteDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){
  3117. if( m_bIsConnected ) {
  3118. char tQBuf[MAX_PATH_LEN];
  3119. CSTR tES = m_pDataStorage->escapeString( version );
  3120. if( tES ) {
  3121. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID );
  3122. free( tES );
  3123. #ifdef DEBUG_ALL
  3124. LOG(tQBuf);
  3125. #endif
  3126. m_pDataStorage->SilentQuery( tQBuf );
  3127. return true;
  3128. };
  3129. };
  3130. return false;
  3131. };
  3132. bool sqliteDBC::updateServerIdentity( uint32_t m_nServerID, CSTR m_sIdentity ){
  3133. if( m_bIsConnected ) {
  3134. char tQBuf[MAX_PATH_LEN];
  3135. CSTR tES = m_pDataStorage->escapeString( m_sIdentity );
  3136. if( tES ) {
  3137. sprintf( tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID );
  3138. free( tES );
  3139. #ifdef DEBUG_ALL
  3140. LOG(tQBuf);
  3141. #endif
  3142. m_pDataStorage->SilentQuery( tQBuf );
  3143. return true;
  3144. };
  3145. };
  3146. return false;
  3147. };
  3148. #undef LOG
  3149. #undef LOG_ERROR