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.
 
 
 
 
 
 

379 lines
11 KiB

#include "protocolPTZ_ONVIF.h"
#include "../../hardware/uart/serialPort.h"
#define LOG( X ) m_pLog->Log( "ONVIF", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "ONVIF", this, _LOG_ERROR, X )
protocolPTZ_ONVIF::protocolPTZ_ONVIF( config_setting_t *Settings ) :protocolPTZ(), m_mInquiryMutex(){
m_pLog = basicLogCollection::getInstance();
m_bLastCommandIssued = CMD_PTZ_NOOP;
m_pSettings = Settings;
m_bAuthMode = 0;
#ifdef USE_PONVIF
m_pOnVif = NULL;
m_pProfile = NULL;
#else
m_pDevice = NULL;
m_pPTZ = NULL;
#endif
memset( m_pDeviceURI, 0, MAX_PATH_LEN );
memset( m_pUser, 0, MAX_CRED_LEN );
memset( m_pPassword, 0, MAX_CRED_LEN );
};
protocolPTZ_ONVIF::protocolPTZ_ONVIF( CSTR ip, uint16_t port, CSTR username, CSTR password, uint8_t AuthMode ) :protocolPTZ(), m_mInquiryMutex(){
m_pLog = basicLogCollection::getInstance();
m_bLastCommandIssued = CMD_PTZ_NOOP;
m_pSettings = NULL;
#ifdef USE_PONVIF
m_pOnVif = NULL;
m_pProfile = NULL;
#else
m_pDevice = NULL;
m_pPTZ = NULL;
#endif
LOG( ip );
LOG( username );
LOG( password );
m_bAuthMode = AuthMode;
sprintf( m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode );
LOG( m_pDeviceURI );
if ( port == 80 ) {
sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip );
} else {
sprintf(m_pDeviceURI, "http://%s:%d/onvif/device_service", ip, port );
}
LOG( m_pDeviceURI );
strncpy( m_pUser, username, MAX_CRED_LEN );
strncpy( m_pPassword, password, MAX_CRED_LEN );
};
protocolPTZ_ONVIF::protocolPTZ_ONVIF( CSTR ctl_url, CSTR username, CSTR password, uint8_t AuthMode ) :protocolPTZ(), m_mInquiryMutex(){
m_pLog = basicLogCollection::getInstance();
m_bLastCommandIssued = CMD_PTZ_NOOP;
m_pSettings = NULL;
#ifdef USE_PONVIF
m_pOnVif = NULL;
m_pProfile = NULL;
#else
m_pDevice = NULL;
m_pPTZ = NULL;
#endif
LOG( ctl_url );
LOG( username );
LOG( password );
m_bAuthMode = AuthMode;
sprintf( m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode );
LOG( m_pDeviceURI );
strncpy( m_pDeviceURI, ctl_url, 250 );
strncpy( m_pUser, username, MAX_CRED_LEN );
strncpy( m_pPassword, password, MAX_CRED_LEN );
};
protocolPTZ_ONVIF::~protocolPTZ_ONVIF(){
};
bool protocolPTZ_ONVIF::setIPAddress( CSTR ip ){
if( ip== NULL ) return false;
if( strlen(ip) == 0 ) return false;
sprintf( m_pDeviceURI, "http://%s/onvif/device_service", ip );
//strncpy( m_pDeviceURI, ip, MAX_PATH_LEN );
return true;
};
bool protocolPTZ_ONVIF::setUsername( CSTR username ){
if( username == NULL ) return false;
if( strlen(username) == 0 ) return false;
strncpy( m_pUser, username, MAX_CRED_LEN );
return true;
};
bool protocolPTZ_ONVIF::setPassword( CSTR password ){
if( password == NULL ) return false;
if( strlen(password) == 0 ) return false;
strncpy( m_pPassword, password, MAX_CRED_LEN );
return true;
};
bool protocolPTZ_ONVIF::setAuthMode( uint8_t Mode ){
m_bAuthMode = Mode;
return true;
};
bool protocolPTZ_ONVIF::create( void ){
if (!protocolPTZ::create()) return false;
if( m_pSettings!= NULL ) {
config_setting_t *pSetting;
pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_USER);
if (pSetting == NULL) return false;
sprintf( m_pUser, "%s", config_setting_get_string(pSetting));
pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_PASS);
if (pSetting == NULL) return false;
sprintf( m_pPassword, "%s", config_setting_get_string(pSetting));
pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_DEV_URI);
if (pSetting == NULL) return false;
sprintf( m_pDeviceURI, "%s", config_setting_get_string(pSetting));
};
#ifdef DEBUG
LOG( m_pDeviceURI );
#endif
if( strlen( m_pDeviceURI ) < 4 ){
LOG_ERROR("No Device URI Is Set!");
return false;
};
#ifdef USE_PONVIF
m_pOnVif = new pOnvif();
if( m_pOnVif == NULL ){
LOG_ERROR("pOnvif NEW FAILURE!");
return false;
};
m_pOnVif->setDeviceUri( m_pDeviceURI );
m_pOnVif->setUsername( m_pUser );
m_pOnVif->setPassword( m_pPassword );
m_pOnVif->setAuthMode( m_bAuthMode );
LOG("PROTOCREATE");
if( m_pOnVif->create() ){
m_pProfile = m_pOnVif->getProfiles()->at(0);
LOG("DONE");
return true;
} else {
LOG_ERROR("pOnvif CREATE FAILURE!");
delete( m_pOnVif );
m_pOnVif = NULL;
return false;
};
#else
std::string A(m_pUser);
std::string B(m_pPassword);
std::string C(m_pDeviceURI);
m_pDevice = new OnvifClientDevice(C, A, B);
if (m_pDevice != NULL){
m_pPTZ = new OnvifClientPTZ(*m_pDevice);
if (m_pPTZ!=NULL){
_tptz__GetNodesResponse tNodes;
int RetVal;
RetVal = m_pPTZ->GetNodes(tNodes);
if (RetVal == 0){
OnvifClientMedia media(*m_pDevice);
_trt__GetProfilesResponse profiles;
media.GetProfiles(profiles);
m_pPTZToken = profiles.Profiles.at(0)->token;// PTZConfiguration->token;
//m_pPTZToken = tNodes.PTZNode.at(0)->token;
//_tptz__GetConfigurationOptionsResponse COResp;
//int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken);
//_tptz__GetConfigurationResponse configResponce;
//configResponce.PTZConfiguration = new tt__PTZConfiguration();
//m_pPTZ->GetConfiguration(configResponce);
//m_pToken = configResponce.PTZConfiguration->token;
setConnectionStatus(CS_CONNECTED);
m_bStatus = 1;
return true;
};
};
};
#endif
return false;
};
#ifndef USE_PONVIF
bool protocolPTZ_ONVIF::initCamera(void){
if (m_bStatus == 0) return false;
//if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false;
m_bLastCommandIssued = CMD_PTZ_INIT;
m_pPTZ->Initialize();
return true;
};
#endif
bool protocolPTZ_ONVIF::move( uint32_t MoveMatrix ){
//if( m_bStatus == 0 ) return false;
#ifdef USE_PONVIF
if( m_pProfile == NULL ) return false;
//if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
float Speed_x, Speed_y, Speed_z;
if( MoveMatrix > 0 ) {
if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0){
Speed_x = 0.0f;
} else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){
Speed_x = -m_dPanSpeed;
} else {
Speed_x = m_dPanSpeed;
};
if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0){
Speed_y = 0.0;
} else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){
Speed_y = m_dTiltSpeed;
} else {
Speed_y = -m_dTiltSpeed;
};
if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0){
Speed_z = 0.0;
} else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0){
Speed_z = m_dZoomSpeed;
} else {
Speed_z = -m_dZoomSpeed;
};
bool res = true;
if( ( MoveMatrix & ( PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK ) ) != 0 ) {
res = m_pOnVif->ptz_ContinuousMove( m_pProfile->profiletoken, Speed_x , Speed_y );
};
if( res ){
if(( MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0 ) {
return true;
};
return m_pOnVif->ptz_ContinuousMoveZoom( m_pProfile->profiletoken, Speed_z );
};
} else {
return m_pOnVif->ptz_Stop( m_pProfile->profiletoken, true, true );
};
return false;
#else
if (m_bStatus == 0) return false;
//if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
_tptz__ContinuousMoveResponse ContMoveResponse;
tt__PTZSpeed tSpeed;
tSpeed.PanTilt = new tt__Vector2D();
tSpeed.Zoom = new tt__Vector1D();
LONG64 to = 1000;
int RetVal;
if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0){
tSpeed.PanTilt->x = 0.0;
} else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){
tSpeed.PanTilt->x = -m_dPanSpeed;
} else {
tSpeed.PanTilt->x = m_dPanSpeed;
};
if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0){
tSpeed.PanTilt->y = 0.0;
} else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){
tSpeed.PanTilt->y = m_dTiltSpeed;
} else {
tSpeed.PanTilt->y = -m_dTiltSpeed;
};
if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0){
tSpeed.Zoom->x = 0.0;
} else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0){
tSpeed.Zoom->x = m_dZoomSpeed;
} else {
tSpeed.Zoom->x = -m_dZoomSpeed;
};
RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken);
delete(tSpeed.PanTilt);
delete(tSpeed.Zoom);
if (RetVal) return false;
return true;
#endif
};
#include <math.h>
bool protocolPTZ_ONVIF::waitMoveIsDone( void ){
ptzStatus tVal;
uint8_t TO = 10; // 3.6 second max
SLEEP( 333 );
while( m_pOnVif->ptz_GetStatus( m_pProfile->profiletoken, &tVal ) && TO ) {
if( tVal.hasMoveStatus ) {
if( tVal.PanTiltIdle && tVal.ZoomIdle ) break;
} else {
//if( ( fabs(tVal.pan - dPan) < 0.0005 ) && ( fabs( tVal.tilt - dTilt) < 0.0005 ) ) break;//&& ( fabs( tVal.zoom - dZoom ) < 0.05 ) ) break;
};
SLEEP( 333 );
TO--;
};
if( TO ) {
#ifdef DEBUG_EXTRA
LOG("Finished Move!");
#endif
return true;
} else LOG_ERROR("Error Fetching PTZ Status!");
return false;
};
bool protocolPTZ_ONVIF::goAbsolute( float dPan, float dTilt, float dZoom ){
#ifdef USE_PONVIF
//if (m_bStatus == 0) return false;
#ifdef DEBUG_EXTRA
LOG("ABS Move");
#endif
if( m_pProfile == NULL ){
LOG_ERROR("No PTZ Profile!!");
return false;
};
if ( dZoom < 0.0f ) dZoom = 0.0f;
if ( m_pOnVif->ptz_AbsoluteMove( m_pProfile->profiletoken, dPan, dTilt, dZoom ) ){
};
return true;
#else
if (m_bStatus == 0) return false;
//if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
_tptz__AbsoluteMoveResponse AbsMoveResponse;
tt__PTZSpeed tSpeed;
tt__PTZVector tPos;
tSpeed.PanTilt = new tt__Vector2D();
tSpeed.Zoom = new tt__Vector1D();
tSpeed.PanTilt->x = 1.f;
tSpeed.PanTilt->y = 1.f;
tSpeed.Zoom->x = 1.f;
tPos.PanTilt = new tt__Vector2D();
tPos.Zoom = new tt__Vector1D();
tPos.PanTilt->x = dPan;
tPos.PanTilt->y = dTilt;
tPos.Zoom->x = dZoom;
int RetVal;
RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
delete(tSpeed.PanTilt);
delete(tSpeed.Zoom);
delete(tPos.PanTilt);
delete(tPos.Zoom);
return true;
#endif
};
bool protocolPTZ_ONVIF::goPreset( char *PresetToken ){
#ifdef USE_PONVIF
//if (m_bStatus == 0) return false;
#ifdef DEBUG_EXTRA
LOG("Preset Move");
#endif
if( m_pProfile == NULL ){
LOG_ERROR("No PTZ Profile!!");
return false;
};
return m_pOnVif->ptz_GotoPreset( m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f );
#else
if (m_bStatus == 0) return false;
//if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
/*
_tptz__AbsoluteMoveResponse AbsMoveResponse;
tt__PTZSpeed tSpeed;
tt__PTZVector tPos;
tSpeed.PanTilt = new tt__Vector2D();
tSpeed.Zoom = new tt__Vector1D();
tSpeed.PanTilt->x = 1.f;
tSpeed.PanTilt->y = 1.f;
tSpeed.Zoom->x = 1.f;
tPos.PanTilt = new tt__Vector2D();
tPos.Zoom = new tt__Vector1D();
tPos.PanTilt->x = dPan;
tPos.PanTilt->y = dTilt;
tPos.Zoom->x = dZoom;
int RetVal;
RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
delete(tSpeed.PanTilt);
delete(tSpeed.Zoom);
delete(tPos.PanTilt);
delete(tPos.Zoom);
*/
return true;
#endif
};
#undef LOG
#undef LOG_ERROR