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.

1252 lines
44 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2018. All Rights Reserved.
  4. // Version 1.0 12/12/2018
  5. // Last modification: 12/12/2018
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: Kernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "Kernel.h"
  16. //#define _GNU_SOURCE
  17. //#include <stdio.h>
  18. //#include <stdlib.h>
  19. #define LOG( X ) m_pLog->Log( "Kernel", this, X )
  20. #define LOG_ERROR( X ) m_pLog->Log( "Kernel", this, _LOG_ERROR, X )
  21. // Constructor implementation
  22. Kernel::Kernel( CSTR *HM ): basicThread(){
  23. m_bExitCause = 0;
  24. m_pLog = basicLogCollection::getInstance();
  25. m_pEventHeap = basicEventHeap::getInstance();
  26. m_pSettingsProvider = basicSettingsProvider::getInstance();
  27. m_pDataBase = NULL;
  28. m_pNetClientsServer = NULL;
  29. m_pVideoInputs = NULL;
  30. m_pVideoNames = NULL;
  31. m_pMatProtector = NULL;
  32. m_pFinalFrame = NULL;
  33. m_bEngineIsAvlib = false;
  34. m_nPictureRot = 0;
  35. m_bPictureRotated = false;
  36. m_pDBHost = m_pDBUser = m_pDBPass = m_pDBName = m_pStorageBase = NULL;
  37. for( uint8_t index = 0; index < MAX_LRF_SENSORS; index++ )m_nLRF_Background[index] = 0x03FF;
  38. m_nFrameCounter = 0;
  39. m_nAlarmCnt = 0;
  40. m_bRecordIsActive = false;
  41. m_bRecordIsSaving = false;
  42. m_lStartTS = 0ll;
  43. m_nCOnlineMask = 0;
  44. m_nCOKMask = 0;
  45. m_nDetectorSST = 0;
  46. m_fDetectorMinSpeed = -1.f;
  47. #ifdef ASSEMBLE_MP4
  48. m_bAssembleMP4 = true;
  49. #else
  50. m_bAssembleMP4 = false;
  51. #endif
  52. #ifdef AMIR_MODE
  53. m_nCurrentRange = 0;
  54. #endif
  55. #ifdef USE_TIME_LAPSE_RECORDER
  56. m_pVideoOutput = NULL;
  57. #else
  58. for( int index = 0; index < MAX_RECORD_FRAMES; index++ ) {
  59. m_pVidRecFrame[index] = NULL;
  60. };
  61. #endif
  62. m_nVideoInputs = 0;
  63. m_nWidth = 320;
  64. m_nHeight = 240;
  65. m_bHorizontal = true;
  66. m_nCLoopMask = 0xFFFFFFFF;
  67. m_nCLoopCheck = 0;
  68. m_nScheduledTaskCounter = 0;
  69. LOG("K-C1");
  70. if( m_pSettingsProvider ) {
  71. m_pSettings = m_pSettingsProvider->getSubNode( NULL, KNL_SETTINGS_MAIN_TAG );
  72. if( m_pSettingsProvider->isCompromised() ) {
  73. m_pSettingsProvider->validate();
  74. };
  75. } else {
  76. m_pSettings = NULL;
  77. };
  78. LOG("K-CE");
  79. };
  80. // Destructor implementation
  81. Kernel::~Kernel(){
  82. #ifdef DEBUG
  83. LOG("Destructor");
  84. #endif
  85. WaitFinish(true);
  86. #ifdef DEBUG
  87. LOG("Deleting Components");
  88. #endif
  89. if( m_pVideoInputs != NULL ) {
  90. for( int index = 0; index < m_nVideoInputs; index++ ) {
  91. if( m_pVideoInputs[index] != NULL ) delete( m_pVideoInputs[index] );
  92. if( m_pVideoFrame[index] != NULL ) delete( m_pVideoFrame[index] );
  93. };
  94. free(m_pVideoInputs);
  95. free(m_pVideoFrame);
  96. };
  97. if( m_pFinalFrame!= NULL ) {
  98. delete(m_pFinalFrame);
  99. m_pFinalFrame == NULL;
  100. };
  101. if(m_pMatProtector != NULL ) {
  102. m_pMatProtector->unlock();
  103. delete(m_pMatProtector);
  104. };
  105. #ifndef USE_TIME_LAPSE_RECORDER
  106. for( int index = 0; index < MAX_RECORD_FRAMES; index++ ) {
  107. if(m_pVidRecFrame[index]!= NULL )delete(m_pVidRecFrame[index]);
  108. };
  109. #endif
  110. #ifdef DEBUG
  111. LOG("Destructor Finished");
  112. #endif
  113. };
  114. bool Kernel::attachNetServer( networkServer *srvPort ){
  115. if( srvPort != NULL ) {
  116. m_pNetClientsServer = srvPort;
  117. };
  118. if( m_pNetClientsServer!= NULL ) {
  119. LOG("Binding Network Manager to Kernel module");
  120. return srvPort->attachEngine( (basicEngine *)this );
  121. };
  122. return false;
  123. };
  124. bool Kernel::getDevicePath(CSTR Path, CSTR BusID ){
  125. bool tFlag = false;
  126. if(Path == NULL ) return false;
  127. if( BusID == NULL ) return false;
  128. Path[0] = (char)0;
  129. for( int i = 0; i< m_InputInfo.size(); i++ ) {
  130. if( strcmp( m_InputInfo.at(i)->FriendlyName, BusID ) == 0 ){
  131. sprintf( Path, "%s", m_InputInfo.at(i)->DevicePath );
  132. tFlag = true;
  133. break;
  134. };
  135. };
  136. return tFlag;
  137. };
  138. //https://www.youtube.com/watch?time_continue=84&v=a4lY6uX3q1M
  139. //http://flv-player.net/players/mini/
  140. bool Kernel::create( CSTR *ptr, networkServer *srvPort ){
  141. char tBuffer[256];
  142. if( m_pSettings != NULL ) {
  143. config_setting_t *pSetting;
  144. config_setting_t *pVideoInputs;
  145. int tVal, mCount;
  146. double tFVal;
  147. LOG("Attach Network ...");
  148. attachNetServer( srvPort );
  149. LOG("Getting Local Video Sources ...");
  150. if( !enumerateDevices( &m_InputInfo ) ){
  151. LOG_ERROR("Error Geting local Sources!");
  152. } else {
  153. sprintf( tBuffer, "Detected %d Video Sources", m_InputInfo.size() );
  154. LOG( tBuffer );
  155. };
  156. LOG("Getting Settings ...");
  157. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_VIDEO_ENGINE, &tVal)){
  158. tVal = 1;
  159. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_VIDEO_ENGINE, CONFIG_TYPE_INT);
  160. config_setting_set_int(pSetting, tVal );
  161. basicSettingsProvider::getInstance()->compromise();
  162. };
  163. m_bEngineIsAvlib = (tVal == 0);
  164. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DATABASE_ENA, &tVal ) ){
  165. tVal = m_bDatabaseEnable = false;
  166. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DATABASE_ENA, CONFIG_TYPE_BOOL );
  167. config_setting_set_bool( pSetting, tVal );
  168. m_pSettingsProvider->compromise();
  169. LOG("Database support not defined -> Disabling");
  170. } else {
  171. m_bDatabaseEnable = (bool)tVal;
  172. sprintf( tBuffer, "Database Support Read From Settings: %d", m_bDatabaseEnable );
  173. LOG( tBuffer );
  174. };
  175. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_VIDEO_OUTPUT, &tVal ) ){
  176. #ifdef ASSEMBLE_MP4
  177. tVal = m_bAssembleMP4 = true;
  178. #else
  179. tVal = m_bAssembleMP4 = false;
  180. #endif
  181. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_VIDEO_OUTPUT, CONFIG_TYPE_BOOL );
  182. config_setting_set_bool( pSetting, tVal );
  183. m_pSettingsProvider->compromise();
  184. #ifdef ASSEMBLE_MP4
  185. LOG("MP4 support not defined -> Enabling");
  186. #else
  187. LOG("MP4 support not defined -> Disabling");
  188. #endif
  189. } else {
  190. m_bAssembleMP4 = (bool)tVal;
  191. sprintf( tBuffer, "MP4 Support Read From Settings: %d", m_bAssembleMP4 );
  192. LOG( tBuffer );
  193. };
  194. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DETECT_ARMED, &tVal ) ){
  195. tVal = m_bDetectorArmed = false;
  196. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_ARMED, CONFIG_TYPE_BOOL );
  197. config_setting_set_bool( pSetting, tVal );
  198. m_pSettingsProvider->compromise();
  199. LOG("Detector Armed not defined -> Disabling");
  200. } else {
  201. m_bDetectorArmed = (bool)tVal;
  202. sprintf( tBuffer, "Detector Armed Read From Settings: %d", m_bDetectorArmed );
  203. LOG( tBuffer );
  204. };
  205. if( !config_setting_lookup_float( m_pSettings, KNL_SETTINGS_DETECT_LAYER_DIST, &tFVal ) ){
  206. tFVal = (double)(m_fDetectorLayerDistance = -1.f);
  207. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_LAYER_DIST, CONFIG_TYPE_FLOAT );
  208. config_setting_set_float( pSetting, tFVal );
  209. m_pSettingsProvider->compromise();
  210. LOG("Detector Layer Distance not defined -> Disabling");
  211. } else {
  212. m_fDetectorLayerDistance = (float)tFVal;
  213. sprintf( tBuffer, "Detector Layer Distance Read From Settings: %0.2f", m_fDetectorLayerDistance );
  214. LOG( tBuffer );
  215. };
  216. if( !config_setting_lookup_float( m_pSettings, KNL_SETTINGS_DETECT_MAX_RANGE, &tFVal ) ){
  217. tFVal = (double)(m_fDetectorMaxDistance = 3.f);
  218. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_MAX_RANGE, CONFIG_TYPE_FLOAT );
  219. config_setting_set_float( pSetting, tFVal );
  220. m_pSettingsProvider->compromise();
  221. LOG("Detector Max Distance not defined set 3.0 meters");
  222. } else {
  223. m_fDetectorMaxDistance = (float)tFVal;
  224. sprintf( tBuffer, "Detector Max Distance Read From Settings: %0.2f", m_fDetectorMaxDistance );
  225. LOG( tBuffer );
  226. };
  227. if( !config_setting_lookup_float( m_pSettings, KNL_SETTINGS_DETECT_MIN_SPEED, &tFVal ) ){
  228. tFVal = (double)(m_fDetectorMinSpeed = 3.f);
  229. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_MIN_SPEED, CONFIG_TYPE_FLOAT );
  230. config_setting_set_float( pSetting, tFVal );
  231. m_pSettingsProvider->compromise();
  232. LOG("Detector Min Speed not defined set to 3.0 m/s");
  233. } else {
  234. m_fDetectorMinSpeed = (float)tFVal;
  235. sprintf( tBuffer, "Detector Min Speed Read From Settings: %0.2f m/s", m_fDetectorMaxDistance );
  236. LOG( tBuffer );
  237. };
  238. if( !config_setting_lookup_int( m_pSettings, KNL_SETTINGS_DETECT_OFFSET, &tVal ) ){
  239. tVal = m_nDetectorOffset = 20;
  240. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_OFFSET, CONFIG_TYPE_INT );
  241. config_setting_set_int( pSetting, tVal );
  242. m_pSettingsProvider->compromise();
  243. LOG("Detector Offset not defined set 20");
  244. } else {
  245. m_nDetectorOffset = tVal;
  246. sprintf( tBuffer, "Detector Offset Read From Settings: %d", m_nDetectorOffset );
  247. LOG( tBuffer );
  248. };
  249. if( !config_setting_lookup_int( m_pSettings, KNL_SETTINGS_DETECT_SST, &tVal ) ){
  250. tVal = m_nDetectorSST = 0;
  251. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_SST, CONFIG_TYPE_INT );
  252. config_setting_set_int( pSetting, tVal );
  253. m_pSettingsProvider->compromise();
  254. LOG("Detector SST not defined set 0");
  255. } else {
  256. m_nDetectorSST = tVal;
  257. sprintf( tBuffer, "Detector SST Read From Settings: %d", m_nDetectorOffset );
  258. LOG( tBuffer );
  259. };
  260. if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_STORAGE_BASE,(const char**)&m_pStorageBase)){
  261. m_pStorageBase = "./storage/";
  262. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_STORAGE_BASE, CONFIG_TYPE_STRING);
  263. config_setting_set_string(pSetting, m_pStorageBase);
  264. basicSettingsProvider::getInstance()->compromise();
  265. };
  266. if( m_bDatabaseEnable ) {
  267. if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_HOST,(const char**)&m_pDBHost)){
  268. m_pDBHost = "localhost";
  269. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_HOST, CONFIG_TYPE_STRING);
  270. config_setting_set_string(pSetting, m_pDBHost);
  271. basicSettingsProvider::getInstance()->compromise();
  272. };
  273. if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_NAME,(const char**)&m_pDBName)){
  274. m_pDBName = "mastbaum";
  275. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_NAME, CONFIG_TYPE_STRING);
  276. config_setting_set_string(pSetting, m_pDBName);
  277. basicSettingsProvider::getInstance()->compromise();
  278. };
  279. if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_USER,(const char**)&m_pDBUser)){
  280. m_pDBUser = "dbroot";
  281. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_USER, CONFIG_TYPE_STRING);
  282. config_setting_set_string(pSetting, m_pDBUser);
  283. basicSettingsProvider::getInstance()->compromise();
  284. };
  285. if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_PASS,(const char**)&m_pDBPass)){
  286. m_pDBPass = "123567";
  287. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_PASS, CONFIG_TYPE_STRING);
  288. config_setting_set_string(pSetting, m_pDBPass);
  289. basicSettingsProvider::getInstance()->compromise();
  290. };
  291. }
  292. if( m_pSettingsProvider->isCompromised() ) {
  293. LOG("Save Settings");
  294. m_pSettingsProvider->validate();
  295. };
  296. pVideoInputs = m_pSettingsProvider->getSubNode( m_pSettings, KNL_SETTINGS_VIDEO_SECTION );
  297. mCount = config_setting_length( pVideoInputs );
  298. if( mCount ){
  299. sprintf( tBuffer, "Defined %d Video Input", mCount );
  300. LOG(tBuffer);
  301. m_pVideoNames = (CSTR *) calloc( mCount, sizeof(CSTR*) );
  302. //for( int i=0; i < mCount; i++ ){ m_pVideoNames[i]= (CSTR) malloc(256); };
  303. m_pVideoInputs = (basicMediaSource**) calloc( mCount, sizeof(basicMediaSource*) );
  304. m_pVideoFrame = (cv::Mat**) calloc( mCount, sizeof(cv::Mat*));
  305. memset( m_pVideoInputs, 0, sizeof(m_pVideoInputs));
  306. memset( m_pVideoFrame, 0, sizeof(m_pVideoFrame));
  307. if( m_pVideoInputs != NULL ) {
  308. config_setting_t *tRec;
  309. config_setting_t *ttRec;
  310. int tIID, tWIDTH, tHEIGHT, tSCALEW, tSCALEH;
  311. bool tSCALED;
  312. double tFPS;
  313. m_nVideoInputs = mCount;
  314. for( int index = 0; index < m_nVideoInputs; index++ ){
  315. tRec = config_setting_get_elem( pVideoInputs, index );
  316. if( tRec != NULL ){
  317. mCount = config_setting_length( tRec );
  318. sprintf( tBuffer, "Records: %d", mCount );
  319. LOG(tBuffer);
  320. tSCALED = false;
  321. //if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_INPUT_ID, &tIID)) continue;
  322. if( !config_setting_lookup_string( tRec, KNL_SETTINGS_VIDEO_INPUT_ID, (const char**)&m_pVideoNames[index] )) continue;
  323. if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_INPUT_WIDTH, &tWIDTH)) continue;
  324. if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_INPUT_HEIGHT, &tHEIGHT)) continue;
  325. if( !config_setting_lookup_float( tRec, KNL_SETTINGS_VIDEO_INPUT_FPS, &tFPS)) continue;
  326. if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_SCALE_WIDTH, &tSCALEW)) tSCALEW = -1;
  327. if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_SCALE_HEIGHT, &tSCALEH)) tSCALEH = -1;
  328. if(( tSCALEW != -1 )&&( tSCALEH != -1 )) tSCALED = true;
  329. LOG( "Begin" );
  330. LOG( m_pVideoNames[index] );
  331. if( !getDevicePath( tBuffer, m_pVideoNames[index] ) ) continue;
  332. LOG( "Found:" );
  333. LOG( tBuffer );
  334. //sprintf( tBuffer, "/dev/video%d", tIID);
  335. if( m_bEngineIsAvlib ){
  336. m_pVideoInputs[index] = (basicMediaSource*)new videoAVLIBSource();
  337. if( !((videoAVLIBSource*)m_pVideoInputs[index])->_open( tBuffer, false, (float)tFPS, -1, tWIDTH, tHEIGHT, tSCALED, tSCALEW, tSCALEH )){
  338. delete((videoAVLIBSource*)m_pVideoInputs[index]);
  339. m_pVideoInputs[index] = NULL;
  340. LOG_ERROR("Source Open Failure.");
  341. } else {
  342. if(tSCALED) {
  343. if( m_nWidth < tSCALEW ) m_nWidth = tSCALEW;
  344. if( m_nHeight < tSCALEH ) m_nHeight = tSCALEH;
  345. }else{
  346. if( m_nWidth < tWIDTH ) m_nWidth = tWIDTH;
  347. if( m_nHeight < tHEIGHT ) m_nHeight = tHEIGHT;
  348. };
  349. m_pVideoFrame[index] = new cv::Mat( tHEIGHT, tWIDTH, CV_8UC3 );
  350. m_pVideoInputs[index]->attachDestination( this );
  351. LOG("Source Opened OK.");
  352. };
  353. } else {
  354. m_pVideoInputs[index] = (basicMediaSource*)new videoV4L2Source();
  355. if( !((videoV4L2Source*)m_pVideoInputs[index])->_open( tBuffer, false, (float)tFPS, -1, tWIDTH, tHEIGHT, tSCALED, tSCALEW, tSCALEH )){
  356. delete((videoV4L2Source*)m_pVideoInputs[index]);
  357. m_pVideoInputs[index] = NULL;
  358. LOG_ERROR("Source Open Failure.");
  359. } else {
  360. if(tSCALED) {
  361. if( m_nWidth < tSCALEW ) m_nWidth = tSCALEW;
  362. if( m_nHeight < tSCALEH ) m_nHeight = tSCALEH;
  363. }else{
  364. if( m_nWidth < tWIDTH ) m_nWidth = tWIDTH;
  365. if( m_nHeight < tHEIGHT ) m_nHeight = tHEIGHT;
  366. };
  367. m_pVideoFrame[index] = new cv::Mat( tHEIGHT, tWIDTH, CV_8UC3 );
  368. m_pVideoInputs[index]->attachDestination( this );
  369. LOG("Source Opened OK.");
  370. };
  371. };
  372. } else {
  373. LOG_ERROR("Mem Allocation Error!");
  374. };
  375. m_nCLoopCheck |=(1<<index);
  376. };
  377. } else {
  378. m_nVideoInputs = 0;
  379. LOG_ERROR("Mem Allocation Error!");
  380. };
  381. } else {
  382. LOG_ERROR("No Video Inputs Defined!");
  383. return false;
  384. };
  385. if( m_bHorizontal ) {
  386. m_nWidth = m_nWidth * m_nVideoInputs;
  387. } else {
  388. m_nHeight = m_nHeight * m_nVideoInputs;
  389. };
  390. m_nHeight += 16; // Top Info Band
  391. m_pMatProtector = new basicMutex();
  392. m_pFinalFrame = new cv::Mat( m_nHeight, m_nWidth, CV_8UC3 );
  393. #ifndef USE_TIME_LAPSE_RECORDER
  394. for( int index = 0; index < MAX_RECORD_FRAMES; index++ ) {
  395. m_pVidRecFrame[index] = new cv::Mat( m_nHeight, m_nWidth, CV_8UC3 );
  396. };
  397. #endif
  398. m_nRecordCounter = MAX_RECORD_FRAMES;
  399. sprintf( m_sWEBAPIName, "%s/%s", m_pStorageBase, "web_pipe" );
  400. sprintf( m_sFSReportName, "%s/%s", m_pStorageBase, "report" );
  401. sprintf( m_sWEBStatus, "%s/%s", m_pStorageBase, "status" );
  402. LOG(m_sWEBAPIName);
  403. LOG(m_sFSReportName);
  404. m_nInitialFSID = ( uint32_t ) time( NULL );
  405. /* Storage Init */
  406. m_pStorageProvider = storageProvider::getInstance( m_pStorageBase );
  407. #ifdef USE_TIME_LAPSE_RECORDER
  408. if( m_bAssembleMP4 ) {
  409. m_pVideoOutput = new lapsedVideoRecorder();
  410. if( m_pVideoOutput == NULL ) {
  411. LOG_ERROR("TimeLapse Recorder on create Failure!");
  412. m_bAssembleMP4 = false;
  413. } else {
  414. m_pVideoOutput->setFrameLapse( MAX_RECORD_FRAMES );
  415. };
  416. };
  417. #endif
  418. if( m_bDatabaseEnable ) {
  419. /* DataBase */
  420. m_pDataBase = new MySQL_Connector();
  421. if( m_pDataBase!= NULL ) {
  422. if( !m_pDataBase->Connect( m_pDBName, m_pDBHost, m_pDBUser, m_pDBPass ) ){
  423. LOG_ERROR("Error Connecting to Database!");
  424. //return false;
  425. } else {
  426. };
  427. };
  428. };
  429. } else {
  430. LOG_ERROR("Failed Read Config Settings!");
  431. return false;
  432. };
  433. if( m_pEventHeap == NULL ){
  434. LOG_ERROR("No Event Heap attached!");
  435. return false;
  436. };
  437. #ifdef DEBUG
  438. LOG( "Trhead about to be created" );
  439. #endif
  440. if( init() == false ){
  441. LOG_ERROR("Error in starting Kernel Thread!");
  442. return false;
  443. };
  444. m_pProtocol = new protocolMastBaum( m_pSettings );
  445. if( m_pProtocol != NULL ) {
  446. m_pProtocol->attachKernel( this );
  447. m_pProtocol->create();
  448. m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
  449. m_nScheduledTaskID = ST_SEND_SETTINGS;
  450. } else {
  451. LOG_ERROR("Error in starting Kernel Protocol!");
  452. return false;
  453. };
  454. putEvent2FS( FSL_SYSTEM_STARTUP );
  455. postStatus2FS();
  456. m_bTargetOverflow = MAX_TARGET_ACCEPTED_AT_ONCE;
  457. #ifdef DEBUG
  458. LOG( "Created" );
  459. #endif
  460. return true;
  461. };
  462. //https://dist.plone.org/download/
  463. void Kernel::putEvent2FS( uint8_t code, int x1, int y1, int x2, int y2, float speed, bool has_image, CSTR path){
  464. FILE *fd;
  465. fd = fopen(m_sFSReportName, "a+" );
  466. if( fd ){
  467. m_nInitialFSID++;
  468. uint32_t ts = ( uint32_t ) time(NULL);
  469. switch( code ) {
  470. case FSL_OPERATOR_ALARM:
  471. case FSL_ALARM:{
  472. float r1, r2;
  473. r1 = ( m_fDetectorMaxDistance / 8.0f ) * (float) y1;
  474. r2 = ( m_fDetectorMaxDistance / 8.0f ) * (float) y2;
  475. fprintf( fd, "%d,%d,%d,%d,%0.2f,%d,%0.2f,%0.3f,%d,%s\n",
  476. m_nInitialFSID,
  477. ts,
  478. code,
  479. x1, r1, x2, r2, speed, has_image, path
  480. );
  481. break;
  482. };
  483. default: fprintf( fd, "%d,%d,%d\n", m_nInitialFSID, ts, code ); break;
  484. };
  485. fclose(fd);
  486. chmod( m_sFSReportName, STORAGE_FILE_MODE );
  487. };
  488. };
  489. //https://www.ebay.com/itm/Nvidia-Tesla-M2090-6GB-GDDR5-PCIe-Accelerator-Video-Graphics-Card-775NK/123590917738
  490. //http://www.zoomify.com/free.htm
  491. void Kernel::postStatus2FS( bool isActive ){
  492. FILE *fd;
  493. fd = fopen( m_sWEBStatus, "w+" );
  494. if( fd ){
  495. uint32_t ts = ( uint32_t ) time(NULL);
  496. fprintf( fd, "%d,%d,%d,%d,%d,%d,%d,%0.3f,%0.3f,%0.3f \n",
  497. ts,
  498. isActive,
  499. m_bDetectorArmed,
  500. m_nCOnlineMask,
  501. m_nCOKMask,
  502. m_nDetectorOffset,
  503. m_nDetectorSST,
  504. m_fDetectorMaxDistance,
  505. m_fDetectorLayerDistance,
  506. m_fDetectorMinSpeed
  507. );
  508. for( uint8_t index=0; index < MAX_LRF_SENSORS; index++ ) fprintf( fd, "%d\n", m_nLRF_Background[index] );
  509. fclose(fd);
  510. chmod( m_sWEBStatus, STORAGE_FILE_MODE );
  511. };
  512. };
  513. void Kernel::alarm( bool real ){
  514. #ifdef USE_TIME_LAPSE_RECORDER
  515. #ifdef DEBUG
  516. LOG("Get VO Status!");
  517. #endif
  518. if( !m_pVideoOutput->getRecordStatus() ){
  519. m_nAlarmCnt++;
  520. char Buffer[1024];
  521. sprintf( m_sSeriesName, "%s", m_pStorageProvider->getNewFileName());
  522. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  523. #ifdef DEBUG
  524. LOG("Begin VO!");
  525. #endif
  526. m_bRecordIsSaving = m_pVideoOutput->beginOutput( Buffer );
  527. } else {
  528. #ifdef DEBUG
  529. LOG("Cont VO!");
  530. #endif
  531. m_bRecordIsSaving = m_pVideoOutput->continueOutput();
  532. };
  533. m_bRecordIsActive = m_bRecordIsSaving;
  534. m_bEventRegistered = false;
  535. #else //USE_TIME_LAPSE_RECORDER
  536. if( !m_bRecordIsActive ){
  537. m_nAlarmCnt++;
  538. //m_pStorageProvider->startNewEntry();
  539. sprintf( m_sSeriesName, "%s", m_pStorageProvider->getNewFileName());
  540. m_bRecordIsActive = true;
  541. m_bRecordIsSaving = true;
  542. m_bEventRegistered = false;
  543. #endif // USE_TIME_LAPSE_RECORDER
  544. #ifdef NO_GPIO_HANDLER
  545. if( !real ) {
  546. #else
  547. if( real ) {
  548. // Request Alarm Info from HW
  549. #ifdef DEBUG
  550. LOG("HW ALARM is SET!");
  551. #endif
  552. m_pProtocol->requestTargets();
  553. } else {
  554. #endif
  555. char Buffer[1024];
  556. #ifdef USE_TIME_LAPSE_RECORDER
  557. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  558. #else
  559. #ifdef ASSEMBLE_MP4
  560. if( m_bAssembleMP4 ) {
  561. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  562. } else {
  563. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  564. };
  565. #else
  566. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  567. #endif
  568. #endif // USE_TIME_LAPSE_RECORDER
  569. putEvent2FS( FSL_OPERATOR_ALARM, 20,20,20,20,0,true, Buffer );//https://www.deviantart.com/mushroombrain
  570. m_bEventRegistered = true;
  571. #ifdef DEBUG
  572. LOG("SYM ALARM is SET!");
  573. #endif
  574. };
  575. #ifndef USE_TIME_LAPSE_RECORDER
  576. };
  577. #endif
  578. };
  579. void Kernel::WaitFinish( bool Force ){
  580. if( Force ){
  581. #ifdef DEBUG
  582. LOG( "Thread End Forced" );
  583. #endif
  584. } else {
  585. // 3 seconds sleep to settle down processes
  586. SLEEP(3000);
  587. };
  588. waitThreadEnd( Force );
  589. #ifdef DEBUG
  590. LOG( "Thread is Detached" );
  591. #endif
  592. };
  593. #ifndef USE_TIME_LAPSE_RECORDER
  594. void *Kernel::t_fSavePictures_Proc(void *ptr){
  595. Kernel *me;
  596. me = (Kernel*) ptr;
  597. //Save Last Record On disk
  598. vector<int> compression_params;
  599. //compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  600. //compression_params.push_back(9);
  601. compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
  602. compression_params.push_back(75);
  603. vector<uchar> buf;
  604. char Buffer[MAX_PATH];
  605. int DataSize;
  606. int z_index = 0;
  607. while( me->m_rPrerecord.count > 0 ) {
  608. cv:Mat *tMat;
  609. tMat = me->m_rPrerecord.pop();
  610. if( tMat != NULL ) {
  611. FILE *FID;
  612. imencode( ".jpg", *tMat, buf, compression_params );
  613. DataSize = buf.size();
  614. sprintf( Buffer, "%s_%02d_%03d.jpg", me->m_sSeriesName, me->m_nAlarmCnt, z_index );
  615. FID = fopen( Buffer, "wb+");
  616. fwrite(buf.data(),DataSize, 1,FID );
  617. fclose(FID);
  618. z_index++;
  619. };
  620. delete( tMat );
  621. };
  622. for( int index = MAX_RECORD_FRAMES - 1; index > -1; index -- ) {
  623. FILE *FID;
  624. imencode( ".jpg", (*me->m_pVidRecFrame[index]), buf, compression_params );
  625. DataSize = buf.size();
  626. sprintf( Buffer, "%s_%02d_%03d.jpg", me->m_sSeriesName, me->m_nAlarmCnt, z_index );
  627. FID = fopen( Buffer, "wb+");
  628. fwrite(buf.data(),DataSize, 1,FID );
  629. fclose(FID);
  630. chmod( Buffer, STORAGE_FILE_MODE );
  631. z_index++;
  632. };
  633. #ifdef ASSEMBLE_MP4
  634. if( me->m_bAssembleMP4 ) {
  635. // Create Video From Pictures
  636. sprintf( Buffer,
  637. //"ffmpeg -loglevel panic -hide_banner -f image2 -r 15 -i %s_%02d_%%03d.jpg -vb 1200k -y -an -r 15 %s_%02d.flv",
  638. "ffmpeg -loglevel panic -hide_banner -f image2 -r 15 -i %s_%02d_%%03d.jpg -vb 500k -pix_fmt yuv420p -y -an -r 15 %s_%02d.mp4",
  639. me->m_sSeriesName,
  640. me->m_nAlarmCnt,
  641. me->m_sSeriesName,
  642. me->m_nAlarmCnt
  643. );
  644. basicLogCollection::getInstance()->Log( "Kernel-ST", NULL, Buffer );
  645. FILE *fid;
  646. char temp;
  647. fid = popen( Buffer, "r" );
  648. if( fid!= NULL ){
  649. while( !feof( fid ) ) fread( &temp, 1, 1, fid );
  650. pclose( fid );
  651. sprintf( Buffer, "%s_%02d.mp4", me->m_sSeriesName, me->m_nAlarmCnt );
  652. chmod( Buffer, STORAGE_FILE_MODE );
  653. #ifndef KEEP_JPEG_IMAGES
  654. for( int index = ( MAX_RECORD_FRAMES << 1 ) - 1; index > -1; index -- ) {
  655. sprintf( Buffer, "%s_%02d_%03d.jpg", me->m_sSeriesName, me->m_nAlarmCnt, index );
  656. unlink( Buffer );
  657. };
  658. #endif
  659. } else {
  660. basicLogCollection::getInstance()->Log( "Kernel-ST", NULL, "ERROR CREATE POPEN!" );
  661. };
  662. };
  663. #endif
  664. me->m_bRecordIsSaving = false;
  665. me->m_nRecordCounter = MAX_RECORD_FRAMES;
  666. return NULL;
  667. };
  668. #endif //USE_TIME_LAPSE_RECORDER
  669. /* Main Kernel Thread Procedure */
  670. void Kernel::m_fThreadProc( void ){
  671. char Buffer[1024];
  672. m_bThreadStatus = TH_WORKING;
  673. while( m_bThreadStatus < TH_ENDED ){
  674. int cmd = WEB_CMD_NOP, param;
  675. float fparam1, fparam2, fparam3;
  676. /* check Video Subsystem */
  677. for( int index = 0; index < m_nVideoInputs; index++ ){
  678. if( m_pVideoInputs[index] != NULL ){
  679. MediaStateStatus tMediaStatus;
  680. tMediaStatus = ((videoAVLIBSource*)m_pVideoInputs[index])->getStatus();
  681. if( tMediaStatus != MS_PRESENT ) {
  682. if( m_bEngineIsAvlib ){
  683. // Delete Loosed Video Input
  684. LOG("Deleting AVLIB Video Source");
  685. delete((videoAVLIBSource*)m_pVideoInputs[index]);
  686. rectangle( (*m_pVideoFrame[index]), Point( 0, 0 ), Point( m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows ), Scalar( 0, 0, 0 ), CV_FILLED );
  687. putText( (*m_pVideoFrame[index]), "Video Source Failure!", Point( 20 ,40 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 0, 255 ), 1 );
  688. /* Polish Memory */
  689. m_pVideoInputs[index] = NULL;
  690. m_nCLoopMask &= ~(1<<index);
  691. m_nCLoopCheck &= ~(1<<index);
  692. } else {
  693. LOG("Deleting V4L2 Video Source");
  694. delete((videoV4L2Source*)m_pVideoInputs[index]);
  695. rectangle( (*m_pVideoFrame[index]), Point( 0, 0 ), Point( m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows ), Scalar( 0, 0, 0 ), CV_FILLED );
  696. putText( (*m_pVideoFrame[index]), "Video Source Failure!", Point( 20 ,40 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 0, 255 ), 1 );
  697. m_pVideoInputs[index] = NULL;
  698. m_nCLoopMask &= ~(1<<index);
  699. m_nCLoopCheck &= ~(1<<index);
  700. };
  701. };
  702. } else {
  703. char tBuffer[256];
  704. // Handle Unrecoverable Video Input Lose
  705. if( !enumerateDevices( &m_InputInfo ) ) {
  706. LOG_ERROR("Source Enumeration Failure.");
  707. } else if( getDevicePath( tBuffer, m_pVideoNames[index] ) ){
  708. if( m_bEngineIsAvlib ){
  709. m_pVideoInputs[index] = (basicMediaSource*)new videoAVLIBSource();
  710. if( !((videoAVLIBSource*)m_pVideoInputs[index])->_open( tBuffer, false )){
  711. delete((videoAVLIBSource*)m_pVideoInputs[index]);
  712. m_pVideoInputs[index] = NULL;
  713. LOG_ERROR("Source ReOpen Failure.");
  714. } else {
  715. m_pVideoInputs[index]->attachDestination( this );
  716. LOG("Source ReOpened OK.");
  717. };
  718. } else {
  719. m_pVideoInputs[index] = (basicMediaSource*)new videoV4L2Source();
  720. if( !((videoV4L2Source*)m_pVideoInputs[index])->_open( tBuffer, false )){
  721. delete((videoV4L2Source*)m_pVideoInputs[index]);
  722. m_pVideoInputs[index] = NULL;
  723. LOG_ERROR("Source ReOpen Failure.");
  724. } else {
  725. m_pVideoInputs[index]->attachDestination( this );
  726. LOG("Source ReOpened OK.");
  727. };
  728. };
  729. };
  730. };
  731. };
  732. /* Get Commands Via File */
  733. struct stat stFileInfo;
  734. int intStat;
  735. char *cmdBuffer;
  736. intStat = stat( m_sWEBAPIName ,&stFileInfo);
  737. cmdBuffer = NULL;
  738. if( intStat == 0 ) {
  739. // check file size
  740. if( stFileInfo.st_size > 0 ){
  741. FILE *tFD;
  742. int RetVal;
  743. size_t nSize;
  744. tFD = fopen( m_sWEBAPIName, "r" );
  745. if( tFD != NULL ) {
  746. RetVal = getline( &cmdBuffer, &nSize, tFD );
  747. fclose(tFD);
  748. if( RetVal ) {
  749. //sscanf( cmdBuffer, "%d %d %f %f %f", &cmd, &param, &fparam1, &fparam2, &fparam3 );
  750. sscanf( cmdBuffer, "%d", &cmd );
  751. };
  752. };
  753. unlink( m_sWEBAPIName );
  754. };
  755. };
  756. /* Get COmmands Via Database - Dont use now */
  757. if( m_pDataBase ) {
  758. AAssoc *Result;
  759. sprintf( Buffer, "SELECT * FROM web2engine LIMIT 1");
  760. Result = m_pDataBase->Query( Buffer );
  761. if( Result!= NULL ){
  762. int c_id;
  763. c_id = atoi( Result->at(0)->at("id").c_str());
  764. cmd = atoi( Result->at(0)->at("cmd").c_str());
  765. param = atoi( Result->at(0)->at("param").c_str());
  766. // Delete Command From Queue
  767. sprintf( Buffer, "DELETE FROM web2engine WHERE id = %d", c_id );
  768. m_pDataBase->SilentQuery( Buffer );
  769. // Free Assoc
  770. delete( Result );
  771. };
  772. };
  773. switch ( cmd ) {
  774. case WEB_CMD_NOP: break;
  775. case WEB_CMD_RESET_DETECTION:{
  776. LOG( "WEB_CMD_RESET_DETECTION");
  777. if( m_pProtocol->sendSoftReset() ){
  778. putEvent2FS( FSL_SYSTEM_RESTARTED );
  779. m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
  780. m_nScheduledTaskID = ST_CHECK_STATUS;
  781. };
  782. break;
  783. };
  784. case WEB_CMD_CLEAR_LOG:{
  785. m_pStorageProvider->startNewEntry();
  786. putEvent2FS( FSL_LOG_CLEARED );
  787. break;
  788. };
  789. case WEB_CMD_SET_PARAMS:{
  790. LOG("WEB_CMD_SET_PARAMS");
  791. sscanf( cmdBuffer, "%d %d %d %f %f %f", &cmd, &m_nDetectorSST, &m_nDetectorOffset, &m_fDetectorMaxDistance, &m_fDetectorLayerDistance, &m_fDetectorMinSpeed );
  792. //m_nDetectorOffset = param;
  793. //m_fDetectorMaxDistance = fparam1;
  794. //m_fDetectorLayerDistance = fparam2;
  795. //m_fDetectorMinSpeed = fparam3;
  796. if( m_pProtocol->sendSettings(
  797. m_nDetectorSST,
  798. m_nDetectorOffset,
  799. m_fDetectorLayerDistance,
  800. m_fDetectorMinSpeed
  801. )
  802. ){
  803. config_setting_t *pSetting;
  804. putEvent2FS( FSL_DETECTION_CHANGED );
  805. postStatus2FS();
  806. pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_SST);
  807. if( pSetting != NULL ) {
  808. config_setting_set_int(pSetting, m_nDetectorSST);
  809. basicSettingsProvider::getInstance()->compromise();
  810. };
  811. pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_OFFSET);
  812. if( pSetting != NULL ) {
  813. config_setting_set_int(pSetting, m_nDetectorOffset);
  814. basicSettingsProvider::getInstance()->compromise();
  815. };
  816. pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_LAYER_DIST);
  817. if( pSetting != NULL ) {
  818. config_setting_set_float(pSetting, (double)m_fDetectorLayerDistance );
  819. basicSettingsProvider::getInstance()->compromise();
  820. };
  821. pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_MAX_RANGE);
  822. if( pSetting != NULL ) {
  823. config_setting_set_float(pSetting, (double)m_fDetectorMaxDistance);
  824. basicSettingsProvider::getInstance()->compromise();
  825. };
  826. pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_MIN_SPEED);
  827. if( pSetting != NULL ) {
  828. config_setting_set_float(pSetting, (double)m_fDetectorMinSpeed );
  829. basicSettingsProvider::getInstance()->compromise();
  830. };
  831. if (basicSettingsProvider::getInstance()->isCompromised()) basicSettingsProvider::getInstance()->validate();
  832. m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
  833. m_nScheduledTaskID = ST_CHECK_STATUS;
  834. };
  835. break;
  836. };
  837. case WEB_CMD_DISABLE_DETECTION:{
  838. LOG("WEB_CMD_DISABLE_DETECTION");
  839. m_bDetectorArmed = false;
  840. if( m_pProtocol->sendMode( m_bDetectorArmed ) ) {
  841. putEvent2FS( FSL_OPERATOR_DISARMED );
  842. postStatus2FS();
  843. };
  844. config_setting_t *pSetting;
  845. pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_ARMED);
  846. if( pSetting != NULL ) {
  847. config_setting_set_bool( pSetting, m_bDetectorArmed );
  848. basicSettingsProvider::getInstance()->compromise();
  849. };
  850. if (basicSettingsProvider::getInstance()->isCompromised()) basicSettingsProvider::getInstance()->validate();
  851. break;
  852. };
  853. case WEB_CMD_ENABLE_DETECTION:{
  854. LOG("WEB_CMD_ENABLE_DETECTION");
  855. m_bDetectorArmed = true;
  856. if( m_pProtocol->sendMode( m_bDetectorArmed ) ) {
  857. putEvent2FS( FSL_OPERATOR_ARMED );
  858. postStatus2FS();
  859. };
  860. config_setting_t *pSetting;
  861. pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_ARMED);
  862. if( pSetting != NULL ) {
  863. config_setting_set_bool( pSetting, m_bDetectorArmed );
  864. basicSettingsProvider::getInstance()->compromise();
  865. };
  866. if (basicSettingsProvider::getInstance()->isCompromised()) basicSettingsProvider::getInstance()->validate();
  867. break;
  868. };
  869. case WEB_CMD_MAKE_TEST_ALARM:{
  870. LOG("WEB_CMD_MAKE_TEST_ALARM");
  871. if( m_pProtocol->sendLED( true ) ) {
  872. if(m_nScheduledTaskID == WEB_CMD_NOP ) {
  873. m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
  874. m_nScheduledTaskID = ST_TURN_OFF_LED;
  875. } else m_pProtocol->sendLED( false );
  876. alarm( false );
  877. };
  878. break;
  879. };
  880. case WEB_CMD_RESET_DEVICE:{
  881. LOG("WEB_CMD_RESET_DEVICE");
  882. if( m_pProtocol->sendFullReset() ) {
  883. putEvent2FS( FSL_SYSTEM_RESTARTED );
  884. m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
  885. m_nScheduledTaskID = ST_CHECK_STATUS;
  886. };
  887. break;
  888. };
  889. default:
  890. LOG("Unknown Web CMD");
  891. };
  892. if( cmdBuffer != NULL ) {
  893. free( cmdBuffer );
  894. };
  895. #ifdef USE_TIME_LAPSE_RECORDER
  896. if( m_bRecordIsActive ) {
  897. m_bRecordIsActive = m_pVideoOutput->getRecordStatus();
  898. if( !m_bRecordIsActive ) {
  899. if( !m_bTargetOverflow ){
  900. if ( m_bDetectorArmed ){
  901. if( m_pProtocol->sendMode( true ) ) {
  902. putEvent2FS( FSL_DETECTION_RESTORED );
  903. };
  904. };
  905. };
  906. m_bTargetOverflow = MAX_TARGET_ACCEPTED_AT_ONCE;
  907. };
  908. };
  909. #else
  910. if( !m_bRecordIsActive && m_bRecordIsSaving ) {
  911. if( m_pSaveThreadHandler == NULL ){
  912. int RetVal = pthread_create( &m_pSaveThreadHandler, NULL, t_fSavePictures_Proc, ( void * ) this );
  913. if( RetVal == -1 ) {
  914. LOG_ERROR("File Savier Thread Creation Faled.");
  915. m_pSaveThreadHandler = NULL;
  916. } else {
  917. if( !m_bEventRegistered ){
  918. #ifdef USE_TIME_LAPSE_RECORDER
  919. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  920. #else
  921. #ifdef ASSEMBLE_MP4
  922. if( m_bAssembleMP4 ) {
  923. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  924. } else {
  925. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  926. };
  927. #else
  928. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  929. #endif
  930. #endif // USE_TIME_LAPSE_RECORDER
  931. putEvent2FS( FSL_OPERATOR_ALARM, 0,0,0,0,0,true, Buffer );
  932. m_bEventRegistered = true;
  933. };
  934. };
  935. };
  936. };
  937. if(!m_bRecordIsSaving && m_pSaveThreadHandler!= NULL ){
  938. // Reap thread crete event
  939. pthread_join( m_pSaveThreadHandler, NULL );
  940. m_pSaveThreadHandler = NULL;
  941. };
  942. #endif // USE_TIME_LAPSE_RECORDER
  943. if( m_nScheduledTaskCounter ){
  944. m_nScheduledTaskCounter--;
  945. if(m_nScheduledTaskCounter == 0) {
  946. switch( m_nScheduledTaskID ){
  947. case ST_CHECK_STATUS:
  948. LOG("SCHEDULED STATUS");
  949. if( m_pProtocol->requestStatus() ) m_nScheduledTaskID = ST_NO_TASK;
  950. break;
  951. case ST_CHECK_SETTINGS:
  952. LOG("SCHEDULED SETTTNGS");
  953. if( m_pProtocol->requestSettings() ) m_nScheduledTaskID = ST_NO_TASK;
  954. break;
  955. case ST_ISSUE_SOFT_RESET:
  956. LOG("SCHEDULED STUDY/SOFT RESET");
  957. if( m_pProtocol->sendSoftReset() ) m_nScheduledTaskID = ST_NO_TASK;
  958. break;
  959. case ST_TURN_OFF_LED:
  960. LOG("SCHEDULED ST_TURN_OFF_LED");
  961. if( m_pProtocol->sendLED( false ) ) m_nScheduledTaskID = ST_NO_TASK;
  962. break;
  963. case ST_SEND_SETTINGS:
  964. LOG("SCHEDULED ST_SEND_SETTINGS");
  965. if( m_pProtocol->sendSettings(
  966. m_nDetectorSST,
  967. m_nDetectorOffset,
  968. m_fDetectorLayerDistance,
  969. m_fDetectorMinSpeed
  970. ) ) m_nScheduledTaskID = ST_NO_TASK;
  971. m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
  972. m_nScheduledTaskID = ST_SEND_WMODE;
  973. break;
  974. case ST_SEND_WMODE:
  975. if( m_pProtocol->sendMode( m_bDetectorArmed ) ) m_nScheduledTaskID = ST_NO_TASK;
  976. break;
  977. };
  978. };
  979. };
  980. SLEEP( SLEEP_KERNEL_INTERVAL );
  981. };
  982. };
  983. void Kernel::pushFrame( cv::Mat *Frame, void *src ){
  984. if( src == NULL) {
  985. return;
  986. };
  987. if( m_nFrameCounter == 0 ) m_lStartTS = getMilliseconds();
  988. m_nFrameCounter++;
  989. for( int index = 0; index < m_nVideoInputs; index++ ) {
  990. if( src == m_pVideoInputs[index] ) {
  991. Frame->copyTo((*m_pVideoFrame[index]));
  992. m_nCLoopMask &= ~(1<<index);
  993. break;
  994. };
  995. };
  996. //If we get all frames;
  997. if( m_pFinalFrame!= NULL ) {
  998. if( ( m_nCLoopMask & m_nCLoopCheck ) == 0 ){
  999. int cx,cy;
  1000. cx=0; cy=15;
  1001. m_pMatProtector->lock();
  1002. //rectangle( (*m_pFinalFrame), Point(0,0), Point(m_nWidth, m_nHeight), Scalar(0,0,0), CV_FILLED );
  1003. for( int index = 0; index < m_nVideoInputs; index++ ){
  1004. if(m_bHorizontal){
  1005. m_pVideoFrame[index]->copyTo((*m_pFinalFrame)(cv::Rect(cx,cy, m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows )));
  1006. cx+=m_pVideoFrame[index]->cols;
  1007. }else{
  1008. m_pVideoFrame[index]->copyTo((*m_pFinalFrame)(cv::Rect(cx,cy, m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows )));
  1009. cy+=m_pVideoFrame[index]->rows;
  1010. };
  1011. };
  1012. int X, W = 260;
  1013. X = ( m_pFinalFrame->cols - W ) >> 1;
  1014. //rectangle( (*m_pFinalFrame), Point( X, 10 ), Point( X+W, 25 ), Scalar( 0, 0, 0 ), CV_FILLED );
  1015. rectangle( (*m_pFinalFrame), Point( 0, 0 ), Point( m_pFinalFrame->cols, 15 ), Scalar( 0, 0, 0 ), CV_FILLED );
  1016. long long aMilliseconds = getMilliseconds();
  1017. long long tMilliseconds;
  1018. float fps = 0.f;
  1019. tMilliseconds = aMilliseconds - m_lStartTS;
  1020. if( tMilliseconds > 0 && m_nVideoInputs > 0 ){
  1021. fps = ((float)( m_nFrameCounter / m_nVideoInputs ) * 1000.f ) / (float)tMilliseconds;
  1022. };
  1023. char DateStr[200];
  1024. sprintf( DateStr, "TS:%010lu", tMilliseconds );
  1025. putText( (*m_pFinalFrame), DateStr, Point( X, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 255, 255 ), 1 );
  1026. sprintf( DateStr, "FID:%06lu", m_nFrameCounter );
  1027. putText( (*m_pFinalFrame), DateStr, Point( X+110, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 255, 0 ), 1 );
  1028. sprintf( DateStr, "FPS:%03.2f", fps );
  1029. putText( (*m_pFinalFrame), DateStr, Point( X+190, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 255, 0, 255 ), 1 );
  1030. #ifdef AMIR_MODE
  1031. sprintf( DateStr, "Range:%d", m_nCurrentRange );
  1032. putText( (*m_pFinalFrame), DateStr, Point( 10, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 255, 255, 0 ), 1 );
  1033. #endif
  1034. if( m_bRecordIsActive ){
  1035. rectangle( (*m_pFinalFrame), Point( X+260, 3 ), Point( X+275, 12 ), Scalar( 0, 0, 255 ), CV_FILLED );
  1036. #ifndef USE_TIME_LAPSE_RECORDER
  1037. if( m_nRecordCounter ) {
  1038. m_pFinalFrame->copyTo((*m_pVidRecFrame[--m_nRecordCounter]));
  1039. } else {
  1040. m_bRecordIsActive = false;
  1041. };
  1042. #endif
  1043. } else {
  1044. rectangle( (*m_pFinalFrame), Point( X+260, 3 ), Point( X+275, 12 ), Scalar( 0, 255, 0 ), CV_FILLED );
  1045. #ifndef USE_TIME_LAPSE_RECORDER
  1046. if( m_bRecordIsSaving == false ) {
  1047. cv::Mat * tMat;
  1048. if( m_rPrerecord.count >= MAX_RECORD_FRAMES ){
  1049. tMat = m_rPrerecord.pop();
  1050. delete( tMat );
  1051. };
  1052. tMat = new cv::Mat();
  1053. *tMat = m_pFinalFrame->clone();
  1054. m_rPrerecord.add( tMat );
  1055. };
  1056. #endif
  1057. };
  1058. #ifdef USE_TIME_LAPSE_RECORDER
  1059. //if( m_bAssembleMP4 ) {
  1060. m_pVideoOutput->pushFrame( m_pFinalFrame );
  1061. //};
  1062. #endif
  1063. m_pMatProtector->unlock();
  1064. // Assemble Final Frame;
  1065. m_nCLoopMask = 0xFFFFFFFF;
  1066. };
  1067. };
  1068. };
  1069. bool Kernel::copyFrame( cv::Mat &Frame ){
  1070. if( m_pFinalFrame== NULL ) return false;
  1071. m_pMatProtector->lock();
  1072. if( m_pFinalFrame== NULL ) return false;
  1073. m_pFinalFrame->copyTo(Frame);
  1074. m_pMatProtector->unlock();
  1075. return true;
  1076. };
  1077. //https://www.pointsinfocus.com/tools/depth-of-field-and-equivalent-lens-calculator/#{%22c%22:[{%22f%22:9,%22av%22:%222%22,%22fl%22:11,%22d%22:50000,%22cm%22:%220%22}],%22m%22:1}
  1078. bool Kernel::reset( bool FullRestart ){
  1079. if( FullRestart ) {
  1080. //Shutting down Motors and Hardware
  1081. //Starting up Motors and Hardware
  1082. shutdown( true );
  1083. } else {
  1084. // Reset Components
  1085. //m_pHardwareManager->reset( true );
  1086. };
  1087. return true;
  1088. };
  1089. bool Kernel::shutdown( bool Restart ){
  1090. if( Restart ) {
  1091. putEvent2FS( FSL_SYSTEM_RESTARTED );
  1092. LOG("Restarting the Kernel by SHUTDOWN!");
  1093. m_bExitCause = 1;
  1094. } else {
  1095. putEvent2FS( FSL_SYSTEM_SHUTDOWN );
  1096. LOG("Shutting down the Kernel by SHUTDOWN!");
  1097. m_bExitCause = 0;
  1098. }
  1099. postStatus2FS( false );
  1100. setStatus( TH_ENDED );
  1101. return true;
  1102. };
  1103. void Kernel::updateStatus( bool ArmMode, uint8_t nCount, uint16_t *bgArray ){
  1104. if( nCount > MAX_LRF_SENSORS ) return;
  1105. if( bgArray == NULL ) return;
  1106. m_bDetectorArmed = ArmMode;
  1107. for( uint8_t index = 0; index < nCount; index++ ) {
  1108. m_nLRF_Background[index] = bgArray[index];
  1109. };
  1110. LOG("updateStatus");
  1111. postStatus2FS( true );
  1112. };
  1113. void Kernel::updateSettings( uint16_t offset, uint16_t min_time, uint8_t sst, uint32_t ol_status, uint32_t ok_status ){
  1114. LOG("updateSettings");
  1115. m_nDetectorOffset = offset;
  1116. m_fDetectorMinSpeed = m_fDetectorLayerDistance * min_time * 0.000001f;
  1117. m_nDetectorSST = sst;
  1118. m_nCOnlineMask = ol_status;
  1119. m_nCOKMask = ok_status;
  1120. postStatus2FS( true );
  1121. };
  1122. void Kernel::updateTarget( uint8_t LRF, uint8_t Range ){
  1123. #ifndef MULTI_TARGETS
  1124. char Buffer[1024];
  1125. LOG("updateTarget");
  1126. #ifdef NO_GPIO_HANDLER
  1127. alarm( true );
  1128. #endif
  1129. m_bEventRegistered = true;
  1130. #ifdef USE_TIME_LAPSE_RECORDER
  1131. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  1132. #else
  1133. #ifdef ASSEMBLE_MP4
  1134. if( m_bAssembleMP4 ) {
  1135. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  1136. } else {
  1137. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  1138. };
  1139. #else
  1140. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  1141. #endif
  1142. #endif // USE_TIME_LAPSE_RECORDER
  1143. putEvent2FS( FSL_ALARM, LRF, Range, 0, 0, 0, true, Buffer );
  1144. LOG("Done");
  1145. #endif // MULTI_TARGETS
  1146. };
  1147. void Kernel::handleTargets( uint32_t tMask, uint8_t cnt, uint8_t *tRanges ){
  1148. if( cnt == 0 ) return;
  1149. if( tRanges == NULL ) return;
  1150. if( m_bTargetOverflow ){
  1151. m_bTargetOverflow --;
  1152. if( !m_bTargetOverflow ) {
  1153. if( m_pProtocol->sendMode( false ) ) {
  1154. putEvent2FS( FSL_TARGET_OVERFLOW );
  1155. };
  1156. };
  1157. } else return;
  1158. #ifdef MULTI_TARGETS
  1159. char Buffer[1024];
  1160. #ifdef DEBUG
  1161. LOG("handleTargets");
  1162. #endif
  1163. #ifdef NO_GPIO_HANDLER
  1164. alarm( true );
  1165. #endif
  1166. #ifdef USE_TIME_LAPSE_RECORDER
  1167. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  1168. #else
  1169. #ifdef ASSEMBLE_MP4
  1170. if( m_bAssembleMP4 ) {
  1171. sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );
  1172. } else {
  1173. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  1174. };
  1175. #else
  1176. sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt );
  1177. #endif
  1178. #endif // USE_TIME_LAPSE_RECORDER
  1179. uint16_t x1, x2, sum;
  1180. float r1, r2;
  1181. x1 = tMask & 0x1FF; // 9 bit mask
  1182. x2 = ( tMask >> 9 ) & 0x1FF;
  1183. sum = 0;
  1184. for( uint8_t index = 0; index < cnt; index++) {
  1185. sum += tRanges[index];
  1186. };
  1187. r1 = ( m_fDetectorMaxDistance /8.f ) * ( 1.f + ((float)sum) / ((float)cnt));
  1188. r2 = 0;
  1189. putEvent2FS( FSL_ALARM, x1, r1, x2, r2, 0, true, Buffer );
  1190. m_bEventRegistered = true;
  1191. #ifdef DEBUG
  1192. LOG("Done");
  1193. #endif
  1194. #endif // MULTI_TARGETS
  1195. };
  1196. #undef LOG
  1197. #undef LOG_ERROR