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.

346 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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. };
  171. return false;
  172. };
  173. void servaKernel::m_fThreadProc( void ){
  174. time_t tTimeTSeconds = 0;
  175. time_t cTimeTSeconds = 0;
  176. m_bThreadStatus = TH_WORKING;
  177. LOG("Mojo Started!");
  178. while ( m_bThreadStatus < TH_ENDED ) {
  179. cTimeTSeconds = time( NULL );
  180. //Each Second
  181. if( cTimeTSeconds != tTimeTSeconds ){
  182. tTimeTSeconds = cTimeTSeconds;
  183. #ifdef LINUX_KERNEL_WATCHDOG
  184. if( m_iWatchdogFd > -1 ) {
  185. char tB[64];
  186. sprintf( tB, "2000\n" );
  187. //ToDo: Make Error Handling here!
  188. write( m_iWatchdogFd, &tB, strlen( tB ) );
  189. };
  190. #endif
  191. //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  192. DBC_PUSHED_CMD * tCMD = NULL;//m_pDBC->getNodePushedCMD( 0 ); //Kernel
  193. if( tCMD != NULL ) {
  194. //m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  195. switch( tCMD->CMD ){
  196. case CMD_KRNL_RELOAD:{
  197. LOG("Reload Mojo Node!");
  198. m_bThreadStatus = TH_ENDED;
  199. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  200. break;
  201. };
  202. case CMD_KRNL_RESTART:{
  203. LOG("Restart Mojo Node!");
  204. m_bThreadStatus = TH_ENDED;
  205. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  206. break;
  207. };
  208. case CMD_KRNL_FACTORY_RESET:{
  209. LOG("Restart Mojo -> Factory Reset!");
  210. m_bThreadStatus = TH_ENDED;
  211. setExitCause( EXIT_CODE_FACTORY_RESET );
  212. break;
  213. };
  214. case CMD_NODES_RELOAD:{
  215. //if( m_pNodeFactory != NULL ) {
  216. // m_pNodeFactory->reloadNodes();
  217. //};
  218. break;
  219. };
  220. case CMD_NODE_RELOAD:{
  221. uint32_t tNodeID = atoi( tCMD->PARAM );
  222. //if( m_pNodeFactory != NULL ) {
  223. // m_pNodeFactory->reloadNode( tNodeID );
  224. //};
  225. break;
  226. };
  227. case CMD_KRNL_PNP:{
  228. //if( m_pHardwareManager!= NULL ) {
  229. // LOG_ERROR( "Restart and Detect HardWare!" );
  230. // // Must be shutdowned all service that uses hardware prior to detection!
  231. // m_bThreadStatus = TH_ENDED;
  232. // setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  233. //};
  234. break;
  235. };
  236. case CMD_KRNL_UPDATE_IDENT:{
  237. //LOG("Restart Mojo -> Update Ident!");
  238. //sprintf( m_sIdentity, tCMD->PARAM );
  239. //config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  240. //config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  241. //config_setting_set_string( pSetting, tCMD->PARAM );
  242. //basicSettingsProvider::getInstance()->compromise();
  243. //basicSettingsProvider::getInstance()->validate();
  244. //m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  245. m_bThreadStatus = TH_ENDED;
  246. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  247. break;
  248. };
  249. case CMD_KRNL_REPLACE_DATABASE:{
  250. LOG("Restart Mojo -> Replace Database!");
  251. //CSTR tNAME = m_pDBC->getDataParam( 0 );
  252. //CSTR tUSER = m_pDBC->getDataParam( 1 );
  253. //CSTR tGROUP = m_pDBC->getDataParam( 2 );
  254. //if ( tNAME ) {
  255. // char tB[256];
  256. // sprintf( tB,"./data/%s.db", tCMD->PARAM );
  257. // if ( fileExist( tB )) {
  258. // LOG("Restart Mojo -> Close Database!");
  259. //Closing Database
  260. // delete( m_pDBC );
  261. // m_pDBC = NULL;
  262. // LOG("Restart Mojo -> Delete Database!");
  263. // //unlink( tNAME );
  264. // LOG("Restart Mojo -> Copy Database!");
  265. // fileCopy( tB, tNAME );
  266. // LOG("Restart Mojo -> Chown Database!");
  267. // ch_chown( tNAME, tUSER, tGROUP );
  268. // LOG("Restart Mojo -> Delete Old Database!");
  269. // unlink( tB );
  270. // m_bThreadStatus = TH_ENDED;
  271. // LOG("Restart Mojo -> Exit!");
  272. // setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  273. // m_bThreadStatus = TH_ENDED;
  274. // } else {
  275. // LOG_ERROR("New Database Does not Exists!");
  276. // };
  277. //free( tNAME );
  278. //free( tUSER );
  279. //free( tGROUP );
  280. //};
  281. break;
  282. };
  283. case CMD_KRNL_SYNC_TIME:{
  284. uint32_t uts = atoi( tCMD->PARAM );
  285. if( uts ){
  286. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  287. time_t ts = (time_t)uts;
  288. if( stime( &ts ) == 0 ){
  289. // Restert Server;
  290. m_bThreadStatus = TH_ENDED;
  291. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  292. };
  293. #else
  294. timespec ts;
  295. ts.tv_sec = (time_t)uts;
  296. ts.tv_nsec = 0;
  297. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  298. // Restert Server;
  299. m_bThreadStatus = TH_ENDED;
  300. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  301. };
  302. #endif
  303. };
  304. break;
  305. };
  306. case CMD_KRNL_SYSTEM_TASK:{
  307. break;
  308. };
  309. case NODE_FORCE_FINISH_ALARM:{
  310. //stopSession();
  311. break;
  312. };
  313. };
  314. free( tCMD );
  315. };
  316. //if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  317. };
  318. SLEEP(SLEEP_KERNEL_INTERVAL);
  319. };
  320. };
  321. #undef LOG
  322. #undef LOG_ERROR