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.

398 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************
  2. *
  3. * DESCRIPTION: ARGUS Kernel source
  4. *
  5. * AUTHOR: gagua
  6. *
  7. * HISTORY:
  8. *
  9. * DATE:5/29/2019
  10. *
  11. *******************************************************************/
  12. #include "Pathfinder_Kernel.h"
  13. #include <sys/types.h>
  14. #define LOG( X ) m_pLog->Log( "KRNL", this, X )
  15. #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X )
  16. PF_Kernel::PF_Kernel( config_setting_t *Settings ):basicThread(){
  17. m_bExitCause = 0;
  18. m_pDBC = NULL;
  19. m_pBNO = NULL;
  20. m_pCamera = NULL;
  21. m_pScanner = NULL;
  22. m_tTimeTSeconds = time( NULL );
  23. m_bArmed = ( bool ) digitalRead ( 4 );
  24. m_pLog = basicLogCollection::getInstance();
  25. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
  26. m_pServiceAnnouncer = new ssdpAnnouncer( m_pSettings );
  27. };
  28. PF_Kernel::~PF_Kernel(){
  29. m_pDBC->unregisterServer();
  30. #ifdef DEBUG
  31. LOG("Destructor");
  32. #endif
  33. WaitFinish(true);
  34. #ifdef DEBUG
  35. LOG("Deleting Components");
  36. #endif
  37. if( m_pCamera!= NULL ) delete( m_pCamera );
  38. if( m_pScanner!= NULL ) delete( m_pScanner );
  39. #ifdef DEBUG
  40. LOG("Destructor Finished");
  41. #endif
  42. };
  43. bool PF_Kernel::create( void ){
  44. char tBuffer[256];
  45. if( m_pSettings != NULL ) {
  46. config_setting_t * pSetting;
  47. int tVal;
  48. const char * tBuf;
  49. //Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port = 3300 );
  50. LOG("Getting Settings ...");
  51. /*if( !config_setting_lookup_string( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  52. char * tVar;
  53. char * tTS;
  54. tTS = getTStr(NULL);
  55. tVar = MD5String(tTS);
  56. free(tTS);
  57. sprintf( m_sIdentity, tVar );
  58. pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  59. config_setting_set_string( pSetting, tVar );
  60. free( tVar );
  61. basicSettingsProvider::getInstance()->compromise();
  62. LOG("Server Identity not defined - generating new!");
  63. } else {
  64. sprintf( m_sIdentity, tBuf );
  65. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  66. LOG( tBuffer );
  67. };
  68. if( !config_setting_lookup_int( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, &tVal ) ){
  69. tVal = m_nSiteID = 1;
  70. pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  71. config_setting_set_int( pSetting, tVal );
  72. basicSettingsProvider::getInstance()->compromise();
  73. LOG("Site ID not defined - using [1] as number!");
  74. } else {
  75. m_nSiteID = tVal;
  76. sprintf( tBuffer, "Site ID Read From Settings: %d", m_nSiteID );
  77. LOG( tBuffer );
  78. };
  79. */
  80. /*
  81. #ifdef __EU_PROJECT__
  82. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_SAFE_SHORE, &tVal ) ){
  83. tVal = m_bSafeShoreSupport = true;
  84. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_SAFE_SHORE, CONFIG_TYPE_BOOL );
  85. config_setting_set_bool( pSetting, tVal );
  86. m_pSettingsProvider->compromise();
  87. LOG("Safe-Shore support not defined");
  88. } else {
  89. m_bSafeShoreSupport = (bool)tVal;
  90. sprintf( tBuffer, "Safe-Shore support Read From Settings: %d", tVal );
  91. LOG( tBuffer );
  92. };
  93. #endif
  94. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DEFENDER, &tVal ) ){
  95. tVal = m_bDefenderSupport = true;
  96. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DEFENDER, CONFIG_TYPE_BOOL );
  97. config_setting_set_bool( pSetting, tVal );
  98. m_pSettingsProvider->compromise();
  99. LOG("Defender support not defined");
  100. } else {
  101. m_bDefenderSupport = (bool)tVal;
  102. sprintf( tBuffer, "Defender support Read From Settings: %d", tVal );
  103. LOG( tBuffer );
  104. };
  105. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, &tVal )){
  106. tVal = m_nWidth = 1000;
  107. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, CONFIG_TYPE_INT);
  108. config_setting_set_int(pSetting, tVal );
  109. m_pSettingsProvider->compromise();
  110. LOG("No Detector Width");
  111. } else {
  112. m_nWidth = tVal;
  113. sprintf( tBuffer, "Detector Width Read From Settings: %d", tVal );
  114. LOG( tBuffer );
  115. };
  116. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, &tVal )){
  117. tVal = m_nKernelFilter = 0x000000ff;
  118. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, CONFIG_TYPE_INT);
  119. config_setting_set_int(pSetting, tVal );
  120. m_pSettingsProvider->compromise();
  121. LOG("No Fltered Kernels Defined - All LRF Will Work");
  122. } else {
  123. m_nKernelFilter = tVal;
  124. sprintf( tBuffer, "Filter Kernels Read From Settings: %d", tVal );
  125. LOG( tBuffer );
  126. };
  127. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_SST, &tVal )){
  128. tVal = m_nSST = 0;
  129. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_SST, CONFIG_TYPE_INT);
  130. config_setting_set_int(pSetting, tVal );
  131. m_pSettingsProvider->compromise();
  132. LOG("No Detector SST");
  133. } else {
  134. m_nSST = tVal;
  135. sprintf( tBuffer, "Detector SST Read From Settings: %d", tVal );
  136. LOG( tBuffer );
  137. };
  138. if( m_pSettingsProvider->isCompromised() ) {
  139. LOG("Save Settings");
  140. m_pSettingsProvider->validate();
  141. };
  142. */
  143. } else {
  144. LOG_ERROR("Failed Read Config Settings!");
  145. return false;
  146. };
  147. m_pBNO = new sensorBNO( m_pSettings );
  148. if( m_pBNO != NULL ) {
  149. if( !m_pBNO->create() ){
  150. LOG_ERROR("BNO Creation failure!");
  151. };
  152. } else {
  153. LOG_ERROR("BNO Construction failure!");
  154. };
  155. if( m_pDBC == NULL ) {
  156. LOG("Explicitly Create DBC Object");
  157. m_pDBC = new PATHFINDER_DBC(m_pSettings);
  158. };
  159. if( m_pDBC == NULL ){
  160. LOG_ERROR("DBC Object Creation Failure!");
  161. return false;
  162. };
  163. if(!m_pDBC->connect()){
  164. LOG_ERROR("DBC Connection Failure!");
  165. SLEEP(5000);
  166. //ToDO: Set Restart Kernel!
  167. return false;
  168. };
  169. #ifdef DEBUG
  170. LOG( "Settings Read Done" );
  171. #endif
  172. m_pDBC->clenupCommandQueue();
  173. m_pDBC->registerServer();
  174. #ifdef DEBUG
  175. LOG( "Start SSDP Beacon" );
  176. #endif
  177. if( m_pServiceAnnouncer != NULL ) {
  178. if( !m_pServiceAnnouncer->init() ){
  179. delete(m_pServiceAnnouncer);
  180. m_pServiceAnnouncer = NULL;
  181. };
  182. };
  183. #ifdef DEBUG
  184. LOG("Init Camera");
  185. #endif
  186. m_pCamera = new gPhotoCamera( m_pSettings );
  187. if( m_pCamera != NULL ) {
  188. m_pCamera->create();
  189. };
  190. #ifdef DEBUG
  191. LOG("Init LRF Scanner");
  192. #endif
  193. m_pScanner = new LRF_techScanner( m_pSettings );
  194. if( m_pScanner != NULL ) {
  195. if( !m_pScanner->create() ){
  196. LOG_ERROR( "Error Create LRF Scanner!" );
  197. } else {
  198. };
  199. };
  200. /*
  201. if( m_vCamRecords.size() ) {
  202. ARGUS_Camera *tPtr = NULL;
  203. for( int i=0; i < m_vCamRecords.size(); i++ ) {
  204. tPtr = new ARGUS_Camera();
  205. if( tPtr != NULL ) {
  206. if( tPtr->create( m_vCamRecords.at(i)->ip, m_vCamRecords.at(i)->username, m_vCamRecords.at(i)->password ) ){
  207. #ifdef DEBUG
  208. LOG("Camera Created!");
  209. #endif
  210. m_vCameras.push_back( tPtr );
  211. tPtr = NULL;
  212. } else {
  213. LOG_ERROR("Camera Creation Failure!");
  214. delete( tPtr );
  215. tPtr = NULL;
  216. };
  217. };
  218. };
  219. };
  220. */
  221. #ifdef DEBUG
  222. LOG( "Kernel Trhead is about to be Created" );
  223. #endif
  224. if( init() == false ){
  225. LOG_ERROR("Error in starting Kernel Thread!");
  226. return false;
  227. };
  228. #ifdef DEBUG
  229. LOG( "Kernel Thread Created" );
  230. #endif
  231. /*
  232. m_pServletWS = new servletWS();
  233. if( m_pServletWS != NULL ) {
  234. LOG("ServletWS Created");
  235. if(m_pServletWS->create(8080)){
  236. LOG("ServletWS Started");
  237. //if( ! m_pServletWS->attachChannel("rtsp://gagua:4GetMyID@192.168.0.109:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1", false ) ){
  238. // LOG("Error Adding Channel!");
  239. //} else {
  240. // LOG("Channel Attached");
  241. //};
  242. };
  243. };
  244. */
  245. #ifdef DEBUG
  246. //sprintf( tBuffer, "Server is registered as id[%d]", m_nServerID );
  247. //LOG( tBuffer );
  248. #endif
  249. return true;
  250. };
  251. void PF_Kernel::WaitFinish( bool Force ){
  252. if( Force ){
  253. #ifdef DEBUG
  254. LOG( "Thread End Forced" );
  255. #endif
  256. } else {
  257. // 3 seconds sleep to settle down processes
  258. SLEEP(3000);
  259. };
  260. waitThreadEnd( Force );
  261. #ifdef DEBUG
  262. LOG( "Thread is Detached" );
  263. #endif
  264. };
  265. /* Main Kernel Thread Procedure */
  266. void PF_Kernel::m_fThreadProc( void ){
  267. char Buffer[CTEXT];
  268. time_t tTimeTSeconds;
  269. int lastTSS = 0;
  270. int lastTMS = 0;
  271. DB_COMMAND * tCMD;
  272. char pidBuf[16];
  273. pid_t pid = gettid();
  274. sprintf(pidBuf,"pid:%d", pid);
  275. LOG(pidBuf);
  276. m_bThreadStatus = TH_WORKING;
  277. while( m_bThreadStatus < TH_ENDED ){
  278. m_tTimeTSeconds = time( NULL );
  279. // Network IP OnVif Discovery
  280. // Command Processing Loop
  281. tCMD = m_pDBC->fetchCommand();
  282. if( tCMD != NULL ){
  283. switch( tCMD->cmd ) {
  284. case 10:{
  285. if( m_pScanner!= NULL ) {
  286. m_pScanner->startScan();
  287. };
  288. break;
  289. };
  290. case 13:{
  291. if( m_pScanner!= NULL ) {
  292. m_pScanner->resetDevice();
  293. };
  294. break;
  295. };
  296. case 15:{
  297. if( m_pScanner!= NULL ) {
  298. m_pScanner->stopScan();
  299. };
  300. break;
  301. };
  302. case 22:{
  303. if( m_pCamera!= NULL ) {
  304. m_pCamera->makeShot();
  305. };
  306. break;
  307. };
  308. case 23:{
  309. if( m_pCamera!= NULL ) {
  310. m_pCamera->startBurst();
  311. };
  312. break;
  313. };
  314. case 24:{
  315. if( m_pCamera!= NULL ) {
  316. m_pCamera->stopBurst();
  317. };
  318. break;
  319. };
  320. };
  321. m_pDBC->finishCommand( tCMD->id );
  322. free( tCMD );
  323. tCMD = NULL;
  324. };
  325. if( m_tTimeTSeconds != tTimeTSeconds ){ // Each Second Process
  326. tTimeTSeconds = m_tTimeTSeconds;
  327. //if( m_pModuleFactory != NULL ) {
  328. // m_pModuleFactory->validate();
  329. //};
  330. //if( m_pNodeFactory != NULL ) {
  331. //m_pNodeFactory->validate();
  332. //};
  333. };
  334. SLEEP( SLEEP_KERNEL_INTERVAL );
  335. };
  336. };
  337. bool PF_Kernel::setStatus( int A ){
  338. m_bThreadStatus = (thredStatus)A;
  339. return true;
  340. };
  341. bool PF_Kernel::signalEvent( void ){
  342. //if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){
  343. //};
  344. return true;
  345. };
  346. void PF_Kernel::armStatusChanged( void ){
  347. int tPinVal = digitalRead ( 4 );
  348. if( tPinVal == 1 && !m_bArmed ) {
  349. LOG("!!!!!!!ARMED!!!!!!!");
  350. m_bArmed = true;
  351. } else {
  352. if( tPinVal == 0 && m_bArmed ) {
  353. LOG("!!!!!DISARMED!!!!!!");
  354. m_bArmed = false;
  355. };
  356. };
  357. };
  358. #undef LOG
  359. #undef LOG_ERROR