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.

341 lines
13 KiB

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