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.

1326 lines
47 KiB

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