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.

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