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.

413 lines
16 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. //processVideoFile( "1647bde08730af03aefdef979aa12ec9.mjpeg", 2323, "1647bde08730af03aefdef979aa12ec9.mp4" );
  169. servaMediaEncoder *ME = new servaMediaEncoder();
  170. if ( ME ) {
  171. LOG("CC OK");
  172. if ( ME->create( "./997958795a78c61aa2f7ac7a2bf7830f.mjpeg", "./997958795a78c61aa2f7ac7a2bf7830f.mp4", 25 ) ) {
  173. LOG("COK");
  174. } else LOG("CFAIL");
  175. } else LOG("CC FAIL");
  176. if (m_pRPC) {
  177. if( m_pRPC->start() ) return init();
  178. };
  179. return false;
  180. };
  181. void servaKernel::m_fThreadProc( void ){
  182. time_t tTimeTSeconds = 0;
  183. time_t cTimeTSeconds = 0;
  184. m_bThreadStatus = TH_WORKING;
  185. LOG("Mojo Started!");
  186. while ( m_bThreadStatus < TH_ENDED ) {
  187. cTimeTSeconds = time( NULL );
  188. //Each Second
  189. if( cTimeTSeconds != tTimeTSeconds ){
  190. tTimeTSeconds = cTimeTSeconds;
  191. #ifdef LINUX_KERNEL_WATCHDOG
  192. if( m_iWatchdogFd > -1 ) {
  193. char tB[64];
  194. sprintf( tB, "2000\n" );
  195. //ToDo: Make Error Handling here!
  196. write( m_iWatchdogFd, &tB, strlen( tB ) );
  197. };
  198. #endif
  199. if ( m_vEncodersPool.size() ) {
  200. for ( V_MEDIA_ENCODERS::iterator idx = m_vEncodersPool.begin(); idx < m_vEncodersPool.end(); idx++ ) {
  201. if ( (*idx)->getStatus() == ENCODER_FINISHED ) {
  202. handleEncodedVideo( (*idx)->getDBID() );
  203. //delete( *idx );
  204. m_vEncodersPool.erase( idx );
  205. };
  206. };
  207. };
  208. if ( uint64_t new_id = m_pDBC->getRecentMediaId() ) {
  209. LOG("Start Encoder!");
  210. if ( processVideoFile( new_id ) ) updateEncodeStatus( new_id, MEDIA_ENCODE_PROGRESS, 0 );
  211. };
  212. //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  213. DBC_PUSHED_CMD * tCMD = NULL;//m_pDBC->getNodePushedCMD( 0 ); //Kernel
  214. if( tCMD != NULL ) {
  215. //m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  216. switch( tCMD->CMD ){
  217. case CMD_KRNL_RELOAD:{
  218. LOG("Reload Serva Node!");
  219. m_bThreadStatus = TH_ENDED;
  220. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  221. break;
  222. };
  223. case CMD_KRNL_RESTART:{
  224. LOG("Restart Serva Node!");
  225. m_bThreadStatus = TH_ENDED;
  226. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  227. break;
  228. };
  229. case CMD_KRNL_FACTORY_RESET:{
  230. LOG("Restart Serva -> Factory Reset!");
  231. m_bThreadStatus = TH_ENDED;
  232. setExitCause( EXIT_CODE_FACTORY_RESET );
  233. break;
  234. };
  235. case CMD_NODES_RELOAD:{
  236. //if( m_pNodeFactory != NULL ) {
  237. // m_pNodeFactory->reloadNodes();
  238. //};
  239. break;
  240. };
  241. case CMD_NODE_RELOAD:{
  242. uint32_t tNodeID = atoi( tCMD->PARAM );
  243. //if( m_pNodeFactory != NULL ) {
  244. // m_pNodeFactory->reloadNode( tNodeID );
  245. //};
  246. break;
  247. };
  248. case CMD_KRNL_PNP:{
  249. //if( m_pHardwareManager!= NULL ) {
  250. // LOG_ERROR( "Restart and Detect HardWare!" );
  251. // // Must be shutdowned all service that uses hardware prior to detection!
  252. // m_bThreadStatus = TH_ENDED;
  253. // setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  254. //};
  255. break;
  256. };
  257. case CMD_KRNL_UPDATE_IDENT:{
  258. //LOG("Restart Mojo -> Update Ident!");
  259. //sprintf( m_sIdentity, tCMD->PARAM );
  260. //config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  261. //config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  262. //config_setting_set_string( pSetting, tCMD->PARAM );
  263. //basicSettingsProvider::getInstance()->compromise();
  264. //basicSettingsProvider::getInstance()->validate();
  265. //m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  266. m_bThreadStatus = TH_ENDED;
  267. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  268. break;
  269. };
  270. case CMD_KRNL_REPLACE_DATABASE:{
  271. LOG("Restart Mojo -> Replace Database!");
  272. //CSTR tNAME = m_pDBC->getDataParam( 0 );
  273. //CSTR tUSER = m_pDBC->getDataParam( 1 );
  274. //CSTR tGROUP = m_pDBC->getDataParam( 2 );
  275. //if ( tNAME ) {
  276. // char tB[256];
  277. // sprintf( tB,"./data/%s.db", tCMD->PARAM );
  278. // if ( fileExist( tB )) {
  279. // LOG("Restart Mojo -> Close Database!");
  280. //Closing Database
  281. // delete( m_pDBC );
  282. // m_pDBC = NULL;
  283. // LOG("Restart Mojo -> Delete Database!");
  284. // //unlink( tNAME );
  285. // LOG("Restart Mojo -> Copy Database!");
  286. // fileCopy( tB, tNAME );
  287. // LOG("Restart Mojo -> Chown Database!");
  288. // ch_chown( tNAME, tUSER, tGROUP );
  289. // LOG("Restart Mojo -> Delete Old Database!");
  290. // unlink( tB );
  291. // m_bThreadStatus = TH_ENDED;
  292. // LOG("Restart Mojo -> Exit!");
  293. // setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  294. // m_bThreadStatus = TH_ENDED;
  295. // } else {
  296. // LOG_ERROR("New Database Does not Exists!");
  297. // };
  298. //free( tNAME );
  299. //free( tUSER );
  300. //free( tGROUP );
  301. //};
  302. break;
  303. };
  304. case CMD_KRNL_SYNC_TIME:{
  305. uint32_t uts = atoi( tCMD->PARAM );
  306. if( uts ){
  307. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  308. time_t ts = (time_t)uts;
  309. if( stime( &ts ) == 0 ){
  310. // Restert Server;
  311. m_bThreadStatus = TH_ENDED;
  312. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  313. };
  314. #else
  315. timespec ts;
  316. ts.tv_sec = (time_t)uts;
  317. ts.tv_nsec = 0;
  318. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  319. // Restert Server;
  320. m_bThreadStatus = TH_ENDED;
  321. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  322. };
  323. #endif
  324. };
  325. break;
  326. };
  327. case CMD_KRNL_SYSTEM_TASK:{
  328. break;
  329. };
  330. case NODE_FORCE_FINISH_ALARM:{
  331. //stopSession();
  332. break;
  333. };
  334. };
  335. free( tCMD );
  336. };
  337. //if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  338. };
  339. SLEEP(SLEEP_KERNEL_INTERVAL);
  340. };
  341. };
  342. void servaKernel::updateEncodeStatus( uint64_t dbid, uint8_t status, uint8_t perc ){
  343. m_pDBC->updateEncodeStatus( dbid, status, perc );
  344. };
  345. bool servaKernel::processVideoFile( uint64_t dbid ){
  346. if ( dbid == 0 ) return false;
  347. DBC_RPC_MEDIA_INFO *NFO = m_pDBC->getRemoteMediaInfo( dbid );
  348. if ( NFO ) {
  349. servaMediaEncoder *ptr = new servaMediaEncoder( dbid );
  350. if ( ptr ) {
  351. LOG("Encoder Constructed!");
  352. char tA[MAX_PATH];
  353. char tB[MAX_PATH];
  354. sprintf( tA,"data/%s.%s", NFO->uuid, NFO->fmt );
  355. sprintf( tB,"data/%s.mp4", NFO->uuid );
  356. if ( ptr->create( tA, tB, /*NFO->fps*/ 25 ) ) {
  357. LOG("Encoder Created!");
  358. m_vEncodersPool.push_back( ptr );
  359. return true;
  360. };
  361. delete( ptr );
  362. };
  363. free( NFO );
  364. };
  365. return false;
  366. };
  367. void servaKernel::handleEncodedVideo( uint64_t dbid ){
  368. };
  369. uint64_t servaKernel::registerRemoteVideo( CSTR uuid, uint64_t src, uint32_t ts, uint16_t tsms, uint16_t width, uint16_t height, CSTR fmt ){
  370. if ( m_pDBC ) {
  371. LOG("Video Register 1");
  372. CSTR tMD5 = getUniqID_MD5();
  373. if ( tMD5 ) {
  374. LOG( fmt );
  375. LOG( tMD5 );
  376. uint64_t rv = m_pDBC->addMediaQueueRecord( uuid, tMD5, src, ts, tsms, width, height, fmt );
  377. free( tMD5 );
  378. return rv;
  379. };
  380. };
  381. return 0;
  382. };
  383. #undef LOG
  384. #undef LOG_ERROR