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

#include "kernelModuleAttentionCloud.h"
#include "deviceNodeFactory.h"
#include "deviceNodeCamera.h"
#define LOG(X) kernelModule::m_pLog->Log("MOD_TCLOUD", this, X)
#define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_TCLOUD", this, _LOG_ERROR, X)
kernelAttentionCloud::kernelAttentionCloud( uint32_t id ): kernelModule( id , KERNEL_MODULE_ATTENTION_CLOUD ){
m_pEvtHeap = basicEventHeap::getInstance();
};
kernelAttentionCloud::~kernelAttentionCloud(){
stopService( true );
waitThreadEnd( true );
};
bool kernelAttentionCloud::reloadSettings(){
DBC_ATC_MODULE_DATA *tData = m_pDBC->getAttentionModuleData( m_nDBAUXID );
if( tData ){
memcpy( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ));
free( tData );
return true;
};
return false;
};
bool kernelAttentionCloud::startService(){
LOG("Starting Service!");
DBC_ATC_MODULE_DATA *tData = m_pDBC->getAttentionModuleData( m_nDBAUXID );
if( tData ){
memcpy( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ));
free( tData );
} else {
m_sModuleData.rest_time = 1000; // 1 sec
m_sModuleData.persist_time = 3000; // 3 seconds
m_sModuleData.attention_zone_range = 3.0f; // 3 meters
};
loadCameras();
return init();
};
bool kernelAttentionCloud::stopService( bool force ){
LOG("Stoping Service!");
return uninit();
};
void kernelAttentionCloud::m_fThreadProc(void){
bool bNewSecondLoop = false;
uint32_t cTimeSeconds = 0;
uint32_t oTimeSeconds = 0;
m_bThreadStatus = TH_WORKING;
pthread_setname_np( pthread_self(), "AttentionCloud" );
while( m_bThreadStatus < TH_ENDED ){
uint64_t cts = getMilliseconds();
m_mCloudsProtector.lock();
if( m_vCloud.size() ){
for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); idx++ ){
switch( (*idx)->status ) {
case ATTENTION_STATUS_NEW:{
if ( ( cts - (*idx)->ts ) > ( m_sModuleData.persist_time << 2 ) ){
// Kill Not Sutable Targets
(*idx)->status = ATTENTION_STATUS_ZOMBIE;
break;
};
if( m_vCameras.size() ){
//Try find sutable free camera for that
for( CamCtlAreas::iterator dx = m_vCameras.begin(); dx != m_vCameras.end(); dx++ )
if ( !(*dx)->busy ){
#ifdef DEBUG_EXTRA
char tB[256];
sprintf( tB,"Cam# %ld is not buzy range is: %0.2f meters", (*dx)->cam_id, (*dx)->range );
LOG( tB );
#endif
if( checkLocation( (*idx)->loc, (*dx)->loc, (*dx)->range ) ) {
TARGET_DATA *tData = (TARGET_DATA *) malloc( sizeof( TARGET_DATA ));
if ( tData ) {
memset(tData,0, sizeof(TARGET_DATA));
tData->system_target.x = (*idx)->loc.x;
tData->system_target.y = (*idx)->loc.y;
tData->system_target.z = (*idx)->loc.z;
EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_MOVE_TO, NULL, NULL, tData );
if ( tEvt ) {
tEvt->m_db_id = 1; // to comply cam fusion engine
tEvt->m_nDestinationNodeID = (*dx)->cam_id;
m_pEvtHeap->broadcast( tEvt );
};
};
(*idx)->status = ATTENTION_STATUS_SHOWING;
(*idx)->ts = cts;
(*idx)->cam_ctl = (*dx);
(*idx)->cam_ctl->busy = true;
break;
};
};
};
break;
};
case ATTENTION_STATUS_SHOWING:{
if ( ( cts - (*idx)->ts ) > m_sModuleData.persist_time ) {
(*idx)->cam_ctl->busy = false;
(*idx)->cam_ctl = NULL;
(*idx)->status = ATTENTION_STATUS_RESTING;
};
break;
};
case ATTENTION_STATUS_RESTING:{
if ( ( cts - (*idx)->ts ) > m_sModuleData.rest_time ) (*idx)->status = ATTENTION_STATUS_ZOMBIE;
break;
};
};
};
};
m_mCloudsProtector.unlock();
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
};
};
bool kernelAttentionCloud::validate(){
m_mCloudsProtector.lock();
if( uint32_t cnt = m_vCloud.size() ){
for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); ){
if( (*idx)->status == ATTENTION_STATUS_ZOMBIE ){
m_vCloud.erase( idx );
} else idx++;
};
};
m_mCloudsProtector.unlock();
if ( m_vCameras.size() == 0 ) {
return loadCameras();
} else {
//Try To Load more Cameras ....
deviceNode ** tA = NULL;
size_t tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
if ( tCNT != m_vCameras.size() ) {
free(tA);
return loadCameras();
};
free(tA);
};
//check settings
DBC_ATC_MODULE_DATA *tData = m_pDBC->getAttentionModuleData( m_nDBAUXID );
if( tData ){
if( memcmp( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ) ) != 0 ){
memcpy( &m_sModuleData, tData, sizeof( DBC_ATC_MODULE_DATA ));
};
free( tData );
return true;
};
return false;
};
bool kernelAttentionCloud::deleteCameras(){
if ( m_vCameras.size() ) {
for( CamCtlAreas::iterator idx = m_vCameras.begin(); idx != m_vCameras.end(); idx++ ) delete( *idx );
m_vCameras.clear();
};
};
bool kernelAttentionCloud::loadCameras(){
deleteCameras();
deviceNode ** tA = NULL;
size_t tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
if( tA == NULL ) return false;
for ( size_t i = 0; i < tCNT; i++) {
CamCtlArea *tData = new CamCtlArea();
if ( tData ) {
deviceNodeCamera *tCam = (deviceNodeCamera *)tA[i];
tData->cam_id = tCam->getNodeID();
tData->loc.x = tCam->getSystemLocationX();
tData->loc.y = tCam->getSystemLocationY();
tData->loc.z = tCam->getSystemLocationZ();
tData->busy = false;
tData->range = tCam->getMaxRange();
m_vCameras.push_back( tData );
};
};
free(tA);
tA = NULL;
return true;
};
bool kernelAttentionCloud::checkLocation( C3D_DECART BASE, C3D_DECART N, float range ){
#ifdef DEBUG_EXTRA
char tB[128];
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 );
LOG( tB );
#endif
float A = (BASE.x - N.x)*(BASE.x - N.x);
float B = (BASE.y - N.y)*(BASE.y - N.y);
float C = (BASE.z - N.z)*(BASE.z - N.z);
float D = sqrtf( A+B+C );
#ifdef DEBUG_EXTRA
sprintf( tB, " Final Comp: %0.2f %0.2f", D, range );
LOG( tB );
#endif
return ( D < range );
};
AttentionZone *kernelAttentionCloud::getOldest(){
AttentionZone *RetVal = NULL;
m_mCloudsProtector.lock();
if( m_vCloud.size() ){
uint64_t max_ts = getMilliseconds();
for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); idx++ )
if( (*idx)->ts < max_ts && (*idx)->status == ATTENTION_STATUS_NEW ) {
RetVal = *idx;
max_ts = (*idx)->ts;
break;
};
};
m_mCloudsProtector.unlock();
return RetVal;
};
bool kernelAttentionCloud::registerAttention( /*uint32_t TargetRefID,*/ C3D_DECART L ){
AttentionZone * cAZ = NULL;
m_mCloudsProtector.lock();
if( m_vCloud.size() ){
for( AttentionCloud::iterator idx = m_vCloud.begin(); idx != m_vCloud.end(); idx++ )
if ( checkLocation( (*idx)->loc, L, (*idx)->range ) ) {
cAZ = *idx;
break;
};
};
if ( cAZ ) {
//Found in collection -> update
cAZ->loc.x = L.x;
cAZ->loc.y = L.y;
cAZ->loc.z = L.z;
if( cAZ->status == ATTENTION_STATUS_ZOMBIE ){
cAZ->status = ATTENTION_STATUS_NEW;
cAZ->ts = getMilliseconds();
};
} else {
//Not Found in Current Active Zones
AttentionZone * tAZ = new AttentionZone();
if ( tAZ ) {
tAZ->loc.x = L.x;
tAZ->loc.y = L.y;
tAZ->loc.z = L.z;
tAZ->range = m_sModuleData.attention_zone_range;
tAZ->ts = getMilliseconds();
tAZ->status = ATTENTION_STATUS_NEW;
m_vCloud.push_back( tAZ );
};
};
m_mCloudsProtector.unlock();
return true;
};
bool kernelAttentionCloud::dispatchEvent(EVENT_CONTAINER * E){
if( E == NULL ) return false;
switch( E->m_eType ){
case EVT_ROI_ACTIVE:
case EVT_TARGET_1D:
case EVT_TARGET_2D_BLACK:
case EVT_TARGET_2D_WHITE:
case EVT_TARGET_3D_BLACK:
case EVT_TARGET_3D_WHITE:{
TARGET_DATA * tDATA;
tDATA = (TARGET_DATA *) E->m_pEvtData;
if( tDATA ) {
//LOG("Reg New Movement!");
registerAttention( tDATA->system_target );
}
break;
};
default: break;
};
return true;
};
#undef LOG_ERROR
#undef LOG