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.

275 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "kernelModuleAttentionCloud.h"
  2. #include "deviceNodeFactory.h"
  3. #include "deviceNodeCamera.h"
  4. #define LOG(X) kernelModule::m_pLog->Log("MOD_TCLOUD", this, X)
  5. #define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_TCLOUD", this, _LOG_ERROR, X)
  6. kernelAttentionCloud::kernelAttentionCloud( uint32_t id ): kernelModule( id , KERNEL_MODULE_ATTENTION_CLOUD ){
  7. m_pEvtHeap = basicEventHeap::getInstance();
  8. };
  9. kernelAttentionCloud::~kernelAttentionCloud(){
  10. detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h)
  11. stopService( true );
  12. waitThreadEnd( true );
  13. };
  14. bool kernelAttentionCloud::reloadSettings(){
  15. DBC_ATC_MODULE_DATA *tData = m_pDBC->getAttentionModuleData( m_nDBAUXID );
  16. if( tData ){
  17. memcpy( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ));
  18. free( tData );
  19. return true;
  20. };
  21. return false;
  22. };
  23. bool kernelAttentionCloud::startService(){
  24. LOG("Starting Service!");
  25. DBC_ATC_MODULE_DATA *tData = m_pDBC->getAttentionModuleData( m_nDBAUXID );
  26. if( tData ){
  27. memcpy( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ));
  28. free( tData );
  29. } else {
  30. m_sModuleData.rest_time = 1000; // 1 sec
  31. m_sModuleData.persist_time = 3000; // 3 seconds
  32. m_sModuleData.attention_zone_range = 3.0f; // 3 meters
  33. };
  34. loadCameras();
  35. return init();
  36. };
  37. bool kernelAttentionCloud::stopService( bool force ){
  38. LOG("Stoping Service!");
  39. return uninit();
  40. };
  41. void kernelAttentionCloud::m_fThreadProc(void){
  42. bool bNewSecondLoop = false;
  43. uint32_t cTimeSeconds = 0;
  44. uint32_t oTimeSeconds = 0;
  45. m_bThreadStatus = TH_WORKING;
  46. pthread_setname_np( pthread_self(), "AttentionCloud" );
  47. while( m_bThreadStatus < TH_ENDED ){
  48. uint64_t cts = getMilliseconds();
  49. m_mCloudsProtector.lock();
  50. if( m_vCloud.size() ){
  51. for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); idx++ ){
  52. switch( (*idx)->status ) {
  53. case ATTENTION_STATUS_NEW:{
  54. if ( ( cts - (*idx)->ts ) > ( m_sModuleData.persist_time << 2 ) ){
  55. // Kill Not Sutable Targets
  56. (*idx)->status = ATTENTION_STATUS_ZOMBIE;
  57. break;
  58. };
  59. if( m_vCameras.size() ){
  60. //Try find sutable free camera for that
  61. for( CamCtlAreas::iterator dx = m_vCameras.begin(); dx != m_vCameras.end(); dx++ )
  62. if ( !(*dx)->busy ){
  63. #ifdef DEBUG_EXTRA
  64. char tB[256];
  65. sprintf( tB,"Cam# %ld is not buzy range is: %0.2f meters", (*dx)->cam_id, (*dx)->range );
  66. LOG( tB );
  67. #endif
  68. if( checkLocation( (*idx)->loc, (*dx)->loc, (*dx)->range ) ) {
  69. TARGET_DATA *tData = (TARGET_DATA *) malloc( sizeof( TARGET_DATA ));
  70. if ( tData ) {
  71. memset(tData,0, sizeof(TARGET_DATA));
  72. tData->system_target.x = (*idx)->loc.x;
  73. tData->system_target.y = (*idx)->loc.y;
  74. tData->system_target.z = (*idx)->loc.z;
  75. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_MOVE_TO, NULL, NULL, tData );
  76. if ( tEvt ) {
  77. tEvt->m_db_id = 1; // to comply cam fusion engine
  78. tEvt->m_nDestinationNodeID = (*dx)->cam_id;
  79. m_pEvtHeap->broadcast( tEvt );
  80. };
  81. };
  82. (*idx)->status = ATTENTION_STATUS_SHOWING;
  83. (*idx)->ts = cts;
  84. (*idx)->cam_ctl = (*dx);
  85. (*idx)->cam_ctl->busy = true;
  86. break;
  87. };
  88. };
  89. };
  90. break;
  91. };
  92. case ATTENTION_STATUS_SHOWING:{
  93. if ( ( cts - (*idx)->ts ) > m_sModuleData.persist_time ) {
  94. (*idx)->cam_ctl->busy = false;
  95. (*idx)->cam_ctl = NULL;
  96. (*idx)->status = ATTENTION_STATUS_RESTING;
  97. };
  98. break;
  99. };
  100. case ATTENTION_STATUS_RESTING:{
  101. if ( ( cts - (*idx)->ts ) > m_sModuleData.rest_time ) (*idx)->status = ATTENTION_STATUS_ZOMBIE;
  102. break;
  103. };
  104. };
  105. };
  106. };
  107. m_mCloudsProtector.unlock();
  108. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  109. };
  110. };
  111. bool kernelAttentionCloud::validate(){
  112. m_mCloudsProtector.lock();
  113. if( uint32_t cnt = m_vCloud.size() ){
  114. for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); ){
  115. if( (*idx)->status == ATTENTION_STATUS_ZOMBIE ){
  116. m_vCloud.erase( idx );
  117. } else idx++;
  118. };
  119. };
  120. m_mCloudsProtector.unlock();
  121. if ( m_vCameras.size() == 0 ) {
  122. return loadCameras();
  123. } else {
  124. //Try To Load more Cameras ....
  125. deviceNode ** tA = NULL;
  126. size_t tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
  127. if ( tCNT != m_vCameras.size() ) {
  128. free(tA);
  129. return loadCameras();
  130. };
  131. free(tA);
  132. };
  133. //check settings
  134. DBC_ATC_MODULE_DATA *tData = m_pDBC->getAttentionModuleData( m_nDBAUXID );
  135. if( tData ){
  136. if( memcmp( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ) ) != 0 ){
  137. memcpy( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ));
  138. };
  139. free( tData );
  140. return true;
  141. };
  142. return false;
  143. };
  144. bool kernelAttentionCloud::deleteCameras(){
  145. if ( m_vCameras.size() ) {
  146. for( CamCtlAreas::iterator idx = m_vCameras.begin(); idx != m_vCameras.end(); idx++ ) delete( *idx );
  147. m_vCameras.clear();
  148. };
  149. };
  150. bool kernelAttentionCloud::loadCameras(){
  151. deleteCameras();
  152. deviceNode ** tA = NULL;
  153. size_t tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
  154. if( tA == NULL ) return false;
  155. for ( size_t i = 0; i < tCNT; i++) {
  156. CamCtlArea *tData = new CamCtlArea();
  157. if ( tData ) {
  158. deviceNodeCamera *tCam = (deviceNodeCamera *)tA[i];
  159. tData->cam_id = tCam->getNodeID();
  160. tData->loc.x = tCam->getSystemLocationX();
  161. tData->loc.y = tCam->getSystemLocationY();
  162. tData->loc.z = tCam->getSystemLocationZ();
  163. tData->busy = false;
  164. tData->range = tCam->getMaxRange();
  165. m_vCameras.push_back( tData );
  166. };
  167. };
  168. free(tA);
  169. tA = NULL;
  170. return true;
  171. };
  172. bool kernelAttentionCloud::checkLocation( C3D_DECART BASE, C3D_DECART N, float range ){
  173. #ifdef DEBUG_EXTRA
  174. char tB[128];
  175. sprintf( tB, " Comp: %0.2fx%0.2f %0.2fx%0.2f %0.2fx%0.2f",BASE.x,N.x,BASE.y,N.y,BASE.z, N.z );
  176. LOG( tB );
  177. #endif
  178. float A = (BASE.x - N.x)*(BASE.x - N.x);
  179. float B = (BASE.y - N.y)*(BASE.y - N.y);
  180. float C = (BASE.z - N.z)*(BASE.z - N.z);
  181. float D = sqrtf( A+B+C );
  182. #ifdef DEBUG_EXTRA
  183. sprintf( tB, " Final Comp: %0.2f %0.2f", D, range );
  184. LOG( tB );
  185. #endif
  186. return ( D < range );
  187. };
  188. AttentionZone *kernelAttentionCloud::getOldest(){
  189. AttentionZone *RetVal = NULL;
  190. m_mCloudsProtector.lock();
  191. if( m_vCloud.size() ){
  192. uint64_t max_ts = getMilliseconds();
  193. for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); idx++ )
  194. if( (*idx)->ts < max_ts && (*idx)->status == ATTENTION_STATUS_NEW ) {
  195. RetVal = *idx;
  196. max_ts = (*idx)->ts;
  197. break;
  198. };
  199. };
  200. m_mCloudsProtector.unlock();
  201. return RetVal;
  202. };
  203. bool kernelAttentionCloud::registerAttention( /*uint32_t TargetRefID,*/ C3D_DECART L ){
  204. AttentionZone * cAZ = NULL;
  205. m_mCloudsProtector.lock();
  206. if( m_vCloud.size() ){
  207. for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); idx++ )
  208. if ( checkLocation( (*idx)->loc, L, (*idx)->range ) ) {
  209. cAZ = *idx;
  210. break;
  211. };
  212. };
  213. if ( cAZ ) {
  214. //Found in collection -> update
  215. cAZ->loc.x = L.x;
  216. cAZ->loc.y = L.y;
  217. cAZ->loc.z = L.z;
  218. if( cAZ->status == ATTENTION_STATUS_ZOMBIE ){
  219. cAZ->status = ATTENTION_STATUS_NEW;
  220. cAZ->ts = getMilliseconds();
  221. };
  222. } else {
  223. //Not Found in Current Active Zones
  224. AttentionZone * tAZ = new AttentionZone();
  225. if ( tAZ ) {
  226. tAZ->loc.x = L.x;
  227. tAZ->loc.y = L.y;
  228. tAZ->loc.z = L.z;
  229. tAZ->range = m_sModuleData.attention_zone_range;
  230. tAZ->ts = getMilliseconds();
  231. tAZ->status = ATTENTION_STATUS_NEW;
  232. m_vCloud.push_back( tAZ );
  233. };
  234. };
  235. m_mCloudsProtector.unlock();
  236. return true;
  237. };
  238. bool kernelAttentionCloud::dispatchEvent(EVENT_CONTAINER * E){
  239. if( E == NULL ) return false;
  240. switch( E->m_eType ){
  241. case EVT_ROI_ACTIVE:
  242. case EVT_TARGET_1D:
  243. case EVT_TARGET_2D_BLACK:
  244. case EVT_TARGET_2D_WHITE:
  245. case EVT_TARGET_3D_BLACK:
  246. case EVT_TARGET_3D_WHITE:{
  247. TARGET_DATA * tDATA;
  248. tDATA = (TARGET_DATA *) E->m_pEvtData;
  249. if( tDATA ) {
  250. //LOG("Reg New Movement!");
  251. registerAttention( tDATA->system_target );
  252. }
  253. break;
  254. };
  255. default: break;
  256. };
  257. return true;
  258. };
  259. #undef LOG_ERROR
  260. #undef LOG