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.

347 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2022. All Rights Reserved.
  4. // Version 1.0 11/19/2022
  5. // Last modification: 11/19/2022
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: mojoKernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // LRF_Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "servaKernel.h"
  16. #include "../data/json/json_tool.h"
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <time.h>
  20. #include <json/value.h>
  21. #include <json/writer.h>
  22. #include <json/reader.h>
  23. #include "../common/kernelEventFilterAPF.h"
  24. #include "../common/kernelEventFilterTrain.h"
  25. #include "../common/kernelModuleEmbedVideo.h"
  26. #include "../common/serverMySQL_dataBinder.h"
  27. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  28. #ifdef DEBUG
  29. #define LOG_ERROR( X ) {\
  30. char _tBfrErrMSG234[256];\
  31. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  32. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  33. }
  34. #else
  35. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  36. #endif
  37. servaKernel *servaKernel::_instance = NULL;
  38. servaKernel *servaKernel::getInstance( config_setting_t *ptr ){
  39. if ( _instance == NULL ) {
  40. _instance = new servaKernel( ptr );
  41. };
  42. return _instance;
  43. };
  44. servaKernel::servaKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
  45. //m_pEventHeap = basicEventHeap::getInstance();
  46. // memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
  47. //m_pShadow = NULL;
  48. //m_pSystemAnchor = NULL;
  49. // m_bSessionStarted = false;
  50. // m_bWorkWithRemoteServer = false;
  51. // m_bKeepLocalReport = true;
  52. // m_bMediaMode = 0;
  53. m_pLifeMonitor = servaLifeMonitoring::getInstance();
  54. m_pRPC = NULL;//??
  55. setWM( WM_NONE );
  56. };
  57. servaKernel::~servaKernel(){
  58. WaitFinish( true );
  59. //stopSession( false );
  60. //while(1){
  61. // char tB[200];
  62. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  63. // LOG( "BopBop!" );
  64. // SLEEP(1000);
  65. //};
  66. #ifdef LINUX_KERNEL_WATCHDOG
  67. LOG("Disable WD!");
  68. if( m_iWatchdogFd > -1 ) {
  69. char tB[64];
  70. sprintf( tB, "-1\n" );
  71. //ToDo: Make Error Handling here!
  72. write( m_iWatchdogFd, &tB, strlen( tB ) );
  73. };
  74. LOG("WatchDog turned Off!");
  75. #endif
  76. _instance = NULL;
  77. };
  78. bool servaKernel::create(){
  79. if( m_pSettings != NULL ) {
  80. config_setting_t * pSetting;
  81. int tVal;
  82. const char * tBuf;
  83. char tBuffer[128];
  84. LOG("Getting Settings ...");
  85. /*if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  86. char * tVar;
  87. char * tTS;
  88. tTS = getTStr(NULL);
  89. tVar = MD5String(tTS);
  90. free(tTS);
  91. sprintf( m_sIdentity, tVar );
  92. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  93. config_setting_set_string( pSetting, tVar );
  94. free( tVar );
  95. basicSettingsProvider::getInstance()->compromise();
  96. LOG("Server Identity not defined - generating new!");
  97. } else {
  98. sprintf( m_sIdentity, tBuf );
  99. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  100. LOG( tBuffer );
  101. };
  102. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  103. tVal = m_nInstanceID = 1;
  104. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  105. config_setting_set_int( pSetting, tVal );
  106. basicSettingsProvider::getInstance()->compromise();
  107. LOG("Site ID not defined - using [1] as number!");
  108. } else {
  109. m_nInstanceID = tVal;
  110. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  111. LOG( tBuffer );
  112. };*/
  113. basicSettingsProvider::getInstance()->validate();
  114. } else {
  115. LOG_ERROR("Failed Read Config Settings!");
  116. return false;
  117. };
  118. LOG("Register Server ... ");
  119. //m_pDBC->registerServer( m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID );
  120. char tBuffer[128];
  121. //sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID );
  122. //LOG( tBuffer );
  123. //DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 );
  124. // if( tSiteData ) {
  125. // C3D_WGS84_LOC t;
  126. // t.lat = tSiteData->lat;
  127. // t.lon = tSiteData->lon;
  128. // t.alt = tSiteData->alt;
  129. //m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t, tSiteData->local_binding );
  130. //memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA ));
  131. //free( tSiteData );
  132. // } else {
  133. // LOG_ERROR("Error Loading Site DATA!");
  134. // C3D_WGS84_LOC t;
  135. // t.lat = 0;
  136. // t.lon = 0;
  137. // t.alt = 0;
  138. //memset( &m_sSiteData, 0, sizeof( DBC_SITE_DATA ));
  139. //m_pSystemAnchor = systemAnchor::getInstance( false, &t, true );
  140. //};
  141. //Check date/Time if correct!
  142. // On some systems the System Date may be in past cause last session was interrupted by power loose
  143. /*
  144. uint32_t tTS = m_pDBC->getLastEventTS();
  145. if ( tTS ) {
  146. // Have last TS
  147. struct timespec tS = { 0 };
  148. if( clock_gettime( CLOCK_REALTIME, &tS ) == 0 ){
  149. //time_t cTS = time(NULL);
  150. if ( tTS > tS.tv_sec ) {
  151. // System Time is in the Past
  152. LOG("Correcting System Time!");
  153. tS.tv_sec = tTS;
  154. tS.tv_nsec = tTS * 1000000;
  155. if (clock_settime(CLOCK_REALTIME, &tS) != 0) LOG_ERROR("Time Correction Failed!");
  156. };
  157. };
  158. }
  159. #ifdef DEBUG
  160. else LOG("No Last TS fetched from DB!")
  161. #endif
  162. ;
  163. */
  164. //m_bDebugMode = m_pDBC->getAppDebugMode();
  165. //m_bSessionStarted = m_pDBC->getEventSessionStatus( m_nServerNodeID );
  166. //stopSession( false );
  167. m_pRPC = servaRemoteClient::getInstance( this );
  168. if ( m_pRPC ) {
  169. if( m_pRPC->start() ) return init();
  170. } else return false;
  171. };
  172. void servaKernel::m_fThreadProc( void ){
  173. time_t tTimeTSeconds = 0;
  174. time_t cTimeTSeconds = 0;
  175. m_bThreadStatus = TH_WORKING;
  176. LOG("Mojo Started!");
  177. while ( m_bThreadStatus < TH_ENDED ) {
  178. cTimeTSeconds = time( NULL );
  179. //Each Second
  180. if( cTimeTSeconds != tTimeTSeconds ){
  181. tTimeTSeconds = cTimeTSeconds;
  182. #ifdef LINUX_KERNEL_WATCHDOG
  183. if( m_iWatchdogFd > -1 ) {
  184. char tB[64];
  185. sprintf( tB, "2000\n" );
  186. //ToDo: Make Error Handling here!
  187. write( m_iWatchdogFd, &tB, strlen( tB ) );
  188. };
  189. #endif
  190. //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  191. DBC_PUSHED_CMD * tCMD = NULL;//m_pDBC->getNodePushedCMD( 0 ); //Kernel
  192. if( tCMD != NULL ) {
  193. //m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  194. switch( tCMD->CMD ){
  195. case CMD_KRNL_RELOAD:{
  196. LOG("Reload Mojo Node!");
  197. m_bThreadStatus = TH_ENDED;
  198. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  199. break;
  200. };
  201. case CMD_KRNL_RESTART:{
  202. LOG("Restart Mojo Node!");
  203. m_bThreadStatus = TH_ENDED;
  204. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  205. break;
  206. };
  207. case CMD_KRNL_FACTORY_RESET:{
  208. LOG("Restart Mojo -> Factory Reset!");
  209. m_bThreadStatus = TH_ENDED;
  210. setExitCause( EXIT_CODE_FACTORY_RESET );
  211. break;
  212. };
  213. case CMD_NODES_RELOAD:{
  214. //if( m_pNodeFactory != NULL ) {
  215. // m_pNodeFactory->reloadNodes();
  216. //};
  217. break;
  218. };
  219. case CMD_NODE_RELOAD:{
  220. uint32_t tNodeID = atoi( tCMD->PARAM );
  221. //if( m_pNodeFactory != NULL ) {
  222. // m_pNodeFactory->reloadNode( tNodeID );
  223. //};
  224. break;
  225. };
  226. case CMD_KRNL_PNP:{
  227. //if( m_pHardwareManager!= NULL ) {
  228. // LOG_ERROR( "Restart and Detect HardWare!" );
  229. // // Must be shutdowned all service that uses hardware prior to detection!
  230. // m_bThreadStatus = TH_ENDED;
  231. // setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  232. //};
  233. break;
  234. };
  235. case CMD_KRNL_UPDATE_IDENT:{
  236. //LOG("Restart Mojo -> Update Ident!");
  237. //sprintf( m_sIdentity, tCMD->PARAM );
  238. //config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  239. //config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  240. //config_setting_set_string( pSetting, tCMD->PARAM );
  241. //basicSettingsProvider::getInstance()->compromise();
  242. //basicSettingsProvider::getInstance()->validate();
  243. //m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  244. m_bThreadStatus = TH_ENDED;
  245. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  246. break;
  247. };
  248. case CMD_KRNL_REPLACE_DATABASE:{
  249. LOG("Restart Mojo -> Replace Database!");
  250. //CSTR tNAME = m_pDBC->getDataParam( 0 );
  251. //CSTR tUSER = m_pDBC->getDataParam( 1 );
  252. //CSTR tGROUP = m_pDBC->getDataParam( 2 );
  253. //if ( tNAME ) {
  254. // char tB[256];
  255. // sprintf( tB,"./data/%s.db", tCMD->PARAM );
  256. // if ( fileExist( tB )) {
  257. // LOG("Restart Mojo -> Close Database!");
  258. //Closing Database
  259. // delete( m_pDBC );
  260. // m_pDBC = NULL;
  261. // LOG("Restart Mojo -> Delete Database!");
  262. // //unlink( tNAME );
  263. // LOG("Restart Mojo -> Copy Database!");
  264. // fileCopy( tB, tNAME );
  265. // LOG("Restart Mojo -> Chown Database!");
  266. // ch_chown( tNAME, tUSER, tGROUP );
  267. // LOG("Restart Mojo -> Delete Old Database!");
  268. // unlink( tB );
  269. // m_bThreadStatus = TH_ENDED;
  270. // LOG("Restart Mojo -> Exit!");
  271. // setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  272. // m_bThreadStatus = TH_ENDED;
  273. // } else {
  274. // LOG_ERROR("New Database Does not Exists!");
  275. // };
  276. //free( tNAME );
  277. //free( tUSER );
  278. //free( tGROUP );
  279. //};
  280. break;
  281. };
  282. case CMD_KRNL_SYNC_TIME:{
  283. uint32_t uts = atoi( tCMD->PARAM );
  284. if( uts ){
  285. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  286. time_t ts = (time_t)uts;
  287. if( stime( &ts ) == 0 ){
  288. // Restert Server;
  289. m_bThreadStatus = TH_ENDED;
  290. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  291. };
  292. #else
  293. timespec ts;
  294. ts.tv_sec = (time_t)uts;
  295. ts.tv_nsec = 0;
  296. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  297. // Restert Server;
  298. m_bThreadStatus = TH_ENDED;
  299. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  300. };
  301. #endif
  302. };
  303. break;
  304. };
  305. case CMD_KRNL_SYSTEM_TASK:{
  306. break;
  307. };
  308. case NODE_FORCE_FINISH_ALARM:{
  309. //stopSession();
  310. break;
  311. };
  312. };
  313. free( tCMD );
  314. };
  315. //if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  316. };
  317. SLEEP(SLEEP_KERNEL_INTERVAL);
  318. };
  319. };
  320. #undef LOG
  321. #undef LOG_ERROR