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.

2212 lines
86 KiB

​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
3 years ago
​
​
​
​
3 years ago
3 years ago
​
​
​
2 years ago
3 years ago
​
​
3 years ago
​
​
​
3 years ago
​
​
​
​
​
​
​
2 years ago
3 years ago
​
​
​
​
3 years ago
​
3 years ago
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
  1. #include "deviceNodeCamera.h"
  2. #include "../../components/base/basicCameraInfo.h"
  3. #include "../../components/object/objectSystemAnchor.h"
  4. #include "../../hardware/uart/serialPort.h"
  5. #include "../../hardware/uart/serialSOL.h"
  6. #include "../../data/protocol/protocolPTZ_VISCA.h"
  7. #include "../../data/protocol/protocolPTZ_ONVIF.h"
  8. #include "../../data/protocol/protocolPTZ_PELCO.h"
  9. #include "hardwareManager.h"
  10. #include "hardwareResourceSerialProvider.h"
  11. #include "kernelModuleEmbedVideo.h"
  12. #include "../mojo/mojoKernel.h"
  13. #define LOG( X ) deviceNode::m_pLog->Log( "RCAM", this, X )
  14. //#define DEBUG_EXTRA
  15. #ifdef DEBUG
  16. #define LOG_ERROR( X ) {\
  17. char _tBfrErrMSG2w34[256];\
  18. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  19. deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  20. }
  21. #else
  22. #define LOG_ERROR( X ) deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, X );
  23. #endif
  24. // all sizes in mm
  25. static const float AC_imager_diag[12] = { 5.0f, 5.68f, 6.0f, 6.592f, 8.0f, 8.933f, 11.0f, 16.0f, 22.5f, 30.1f, 43.3f, 69.7f };
  26. static const float AC_imager_width[12] = { 4.0f, 4.536f, 4.8f, 5.27f, 6.4f, 7.176f, 8.8f, 12.8f, 18.0f, 25.1f, 36.0f, 56.0f };
  27. static const float AC_imager_height[12] = { 3.0f, 3.416f, 3.6f, 3.96f, 4.8f, 5.319f, 6.6f, 9.6f, 13.5f, 16.7f, 24.0f, 41.5f };
  28. #define CAM_FOCAL_LENGTH_CNT 49
  29. static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT]={
  30. 2.7f,
  31. 3.0f,
  32. 3.2f,
  33. 3.5f,
  34. 3.7f,
  35. 3.9f,
  36. 4.0f,
  37. 4.3f,
  38. 4.8f,
  39. 5.0f,
  40. 5.1f,
  41. 5.5f,
  42. 5.8f,
  43. 6.0f,
  44. 6.2f,
  45. 6.6f,
  46. 7.0f,
  47. 7.1f,
  48. 7.5f,
  49. 7.6f,
  50. 8.0f,
  51. 8.6f,
  52. 8.9f,
  53. 9.0f,
  54. 9.2f,
  55. 9.3f,
  56. 10.0f,
  57. 10.5f,
  58. 10.6f,
  59. 10.7f,
  60. 11.0f,
  61. 11.3f,
  62. 12.0f,
  63. 12.9f,
  64. 13.0f,
  65. 13.5f,
  66. 13.6f,
  67. 14.0f,
  68. 14.6f,
  69. 15.0f,
  70. 15.1f,
  71. 15.5f,
  72. 16.0f,
  73. 17.0f,
  74. 17.5f,
  75. 17.7f,
  76. 18.0f,
  77. 18.2f,
  78. 19.0f
  79. };
  80. deviceNodeCamera::deviceNodeCamera( uint32_t NodeID ): deviceNode( NodeID ), basicThread(){
  81. //m_pLog = basicLogCollection::getInstance();
  82. m_pBEH = basicEventHeap::getInstance();
  83. m_bSkeepPositioningWait = true;
  84. m_bNoAbsPositionning = false;
  85. m_bCameraOnHomePosition = false;
  86. m_tTimeTSeconds = 0;
  87. m_pProtocol = NULL;
  88. //m_pTransport = NULL;
  89. m_pCurrentROI = NULL;
  90. m_nRealFrameWidth = 640;
  91. //m_bState = 0;
  92. //m_bBusy = true;
  93. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  94. m_nRelativeMoveCounter = 0;
  95. m_bRelativeMoveMatrix = 0;
  96. m_bCurrentPresetCol = 0;
  97. m_bCurrentPresetRow = 0;
  98. m_bShineOnTarget = false;
  99. m_bCameraIsStatic = false;
  100. m_bIRIsOn = false;
  101. m_bIRTO = 0;
  102. m_fSpeedRise = 0.03f;
  103. m_fZoomCorrection = 1.0f;
  104. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  105. //m_bSourceConnected = false;
  106. //m_pSource = NULL;
  107. };
  108. deviceNodeCamera::deviceNodeCamera( DBC_NODE_DATA *Data ): deviceNode( Data ), basicThread(){
  109. //m_pLog = basicLogCollection::getInstance();
  110. m_pBEH = basicEventHeap::getInstance();
  111. m_bSkeepPositioningWait = true;
  112. m_bNoAbsPositionning = false;
  113. m_bCameraOnHomePosition = false;
  114. m_tTimeTSeconds = 0;
  115. m_pProtocol = NULL;
  116. //m_pTransport = NULL;
  117. m_pCurrentROI = NULL;
  118. m_nRealFrameWidth = 640;
  119. //m_bState = 0;
  120. //m_bBusy = true;
  121. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  122. m_nRelativeMoveCounter = 0;
  123. m_bRelativeMoveMatrix = 0;
  124. m_bCurrentPresetCol = 0;
  125. m_bCurrentPresetRow = 0;
  126. m_bShineOnTarget = false;
  127. m_bIRIsOn = false;
  128. m_bIRTO = 0;
  129. m_fSpeedRise = 0.03f;
  130. m_fZoomCorrection = 1.0f;
  131. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  132. //m_bSourceConnected = false;
  133. //m_pSource = NULL;
  134. };
  135. deviceNodeCamera::~deviceNodeCamera(){
  136. LOG("Destruct");
  137. //m_mQPROTECTOR.unlock();
  138. waitThreadEnd( true );
  139. LOG("Thread Del OK");
  140. /*if( m_pTransport!= NULL ) {
  141. if( m_sCamData.uart_id ) {
  142. DBC_UART_DATA *tData = NULL;
  143. tData = m_pDBC->getUartData( m_sCamData.uart_id );
  144. switch( tData->type ) {
  145. case UART_TYPE_HARDWARE:{
  146. delete((serialPort*)m_pTransport);
  147. break;
  148. };
  149. case UART_TYPE_SOL:{
  150. delete((serialSOL*)m_pTransport);
  151. break;
  152. };
  153. };
  154. };
  155. };
  156. LOG("Transport Del OK");
  157. */
  158. if( m_pProtocol != NULL ) {
  159. switch( m_sCamData.protocol ) {
  160. case CAM_CTRL_PROTO_VISCA:
  161. delete( (protocolPTZ_VISCA *) m_pProtocol );
  162. break;
  163. case CAM_CTRL_PROTO_PELCO:
  164. delete( (protocolPTZ_PELCO *) m_pProtocol );
  165. break;
  166. case CAM_CTRL_PROTO_ONVIF:
  167. delete( (protocolPTZ_ONVIF *) m_pProtocol );
  168. break;
  169. default:
  170. delete( m_pProtocol );
  171. };
  172. };
  173. LOG("Proto Del OK");
  174. //if( m_pPosition != NULL ) delete( m_pPosition );
  175. //LOG("Pos Del OK");
  176. //if( m_bSourceConnected ){
  177. // if( m_pSource != NULL ){
  178. // m_pSource->_close();
  179. // delete( m_pSource );
  180. // };
  181. //};
  182. //LOG("Stream Del OK");
  183. };
  184. void deviceNodeCamera::updateOpticalParameters( uint16_t frame_width, uint16_t frame_heigth ){
  185. #ifdef DEBUG
  186. char tB[128];
  187. LOG("Set Optical Parameters");
  188. sprintf( tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth );
  189. LOG(tB);
  190. #endif
  191. if( frame_width > 0 && frame_heigth > 0 ) {
  192. m_nRealFrameWidth = frame_width;
  193. m_nRealFrameHeight = frame_heigth;
  194. float tFrameDiag = sqrtf( m_nRealFrameHeight*m_nRealFrameHeight+m_nRealFrameWidth*m_nRealFrameWidth );
  195. switch( m_sCamData.zoom_model ) {
  196. case 0:{
  197. #ifdef DEBUG
  198. LOG("\tFixed Zoom");
  199. #endif
  200. m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  201. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  202. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  203. m_fR2FL = .0f;
  204. m_fZoomCorrection = .0f;
  205. m_pDBC->updateCamRange( m_sCamData.id, m_fMaxRange );
  206. break;
  207. };
  208. case 1:{
  209. #ifdef DEBUG
  210. LOG("\tAutomated Zoom");
  211. #endif
  212. m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  213. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  214. m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom;
  215. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  216. if( m_fMaxRange > m_fMinRange ){
  217. m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange));
  218. } else {
  219. m_fR2FL = .0f;
  220. };
  221. m_fZoomCorrection = 1.0f;
  222. m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange);
  223. break;
  224. };
  225. case 2:{
  226. #ifdef DEBUG
  227. LOG("\tManual Zoom");
  228. #endif
  229. m_fMinRange = 5.0f; //Minimal 5 meters
  230. if ( m_sCamData.max_range < m_fMinRange ) {
  231. m_fMaxRange = m_fMinRange;
  232. } else {
  233. m_fMaxRange = m_sCamData.max_range;
  234. };
  235. m_fZoomCorrection = m_sCamData.zoom * 0.01f;
  236. m_fMinFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMinRange, tFrameDiag );
  237. m_fMaxFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMaxRange, tFrameDiag );
  238. break;
  239. };
  240. };
  241. m_fMinHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMaxFL ));
  242. m_fMaxHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMinFL ));
  243. m_fMinVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMaxFL ));
  244. m_fMaxVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMinFL ));
  245. #ifdef DEBUG
  246. sprintf( tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL );
  247. LOG(tB);
  248. sprintf( tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange );
  249. LOG(tB);
  250. sprintf( tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov );
  251. LOG(tB);
  252. sprintf( tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov );
  253. LOG(tB);
  254. #endif
  255. };
  256. };
  257. bool deviceNodeCamera::create(){
  258. if ( m_bThreadStatus == TH_WORKING ) return true;
  259. setWM(CAM_WS_INIT);
  260. if( m_sNodeData.aux_id == 0 ) return false;
  261. DBC_CAM_DATA * tData = m_pDBC->getCamData( m_sNodeData.aux_id );
  262. if( tData != NULL ) {
  263. memcpy( &m_sCamData, tData, sizeof(DBC_CAM_DATA) );
  264. free( tData );
  265. } else {
  266. LOG_ERROR("Cannot Fetch CAM Data from Database!");
  267. return false;
  268. };
  269. if( !( m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT ) ) m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1;
  270. updateOpticalParameters( 1280, 720 ); //HD Ready
  271. if( m_sCamData.ptz_speed_step > .0f ) {
  272. m_fSpeedRise = m_sCamData.ptz_speed_step;
  273. };
  274. //m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw );
  275. //if( m_pPosition == NULL ) {
  276. // LOG_ERROR("Position Constructor Failure!");
  277. // return false;
  278. //};
  279. //m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z );
  280. //m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot );
  281. systemAnchor * SA = systemAnchor::getInstance();
  282. if( SA ){
  283. LOG("SysAnchor OK");
  284. if ( SA->beLocal() ) {
  285. LOG("SysAnchor beLocal!");
  286. m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z, 0 ); //SA->getUTMZone() );
  287. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  288. } else {
  289. LOG("SysAnchor is Global Position System!");
  290. C3D_DECART LD = SA->getXYZ();
  291. m_oPosition.setPositionXYZ( LD.x + m_sCamData.x, LD.y + m_sCamData.y, LD.z + m_sCamData.z, SA->getUTMZone() );
  292. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  293. };
  294. } else {
  295. LOG("No SysAnchor!");
  296. m_oPosition.setPositionXYZ( m_sCamData.x , m_sCamData.y, m_sCamData.z );
  297. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  298. };
  299. m_oPosition.updateMatrix();
  300. #ifdef DEBUG
  301. char tB[128];
  302. sprintf( tB, "Finalized Camera Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() );
  303. LOG( tB );
  304. sprintf( tB, "Finalized Camera Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() );
  305. LOG( tB );
  306. sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() );
  307. LOG( tB );
  308. #endif
  309. if( m_pProtocol != NULL ){
  310. LOG_ERROR("Cannot Start: Service Already Running!");
  311. return false;
  312. };
  313. #ifdef CAM_POST_INIT
  314. if( ( m_sCamData.type == ARGUS_IP_PRESET_CAMERA ) || ( m_sCamData.type == ARGUS_ANALOG_PRESET_CAMERA ) ) m_bNoAbsPositionning = true;
  315. if( m_sCamData.protocol == 0 ) {
  316. m_bCameraIsStatic = true;
  317. m_bSkeepPositioningWait = true;
  318. m_sCamData.online = true;
  319. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  320. return m_sCamData.online;
  321. };
  322. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  323. return init();
  324. #else //!CAM_POST_INIT
  325. switch( m_sCamData.protocol ){
  326. case CAM_CTRL_PROTO_VISCA: {
  327. #ifdef DEBUG
  328. LOG("VISCA");
  329. #endif
  330. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux );
  331. if( m_pProtocol!= NULL ) {
  332. basicTransport *tTransport = NULL;
  333. hardwareManager *tHM = hardwareManager::getInstance();
  334. if( tHM ){
  335. hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  336. if( tRES != NULL ) {
  337. if( tRES->isValid()) {
  338. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  339. if( m_pProtocol->setTransport( tTransport )){
  340. if( !m_pProtocol->create() ){
  341. LOG_ERROR("Protocol <<< Create >>> Failure!");
  342. };
  343. } else {
  344. LOG_ERROR("Binding Port Failure!");
  345. };
  346. };
  347. #ifdef DEBUG
  348. } else {
  349. LOG("HWM Has No UART PORT!");
  350. #endif
  351. };
  352. #ifdef DEBUG
  353. } else {
  354. LOG_ERROR("Can not get Hardware Manager!");
  355. #endif
  356. };
  357. if( tTransport!= NULL ) {
  358. LOG( "Bind Transport Port!" );
  359. } else {
  360. LOG_ERROR("Transport Creation Failure!");
  361. delete( ( protocolPTZ_VISCA * )m_pProtocol );
  362. m_pProtocol = NULL;
  363. };
  364. #ifdef DEBUG
  365. } else {
  366. LOG_ERROR("Protocol Creation failure!");
  367. #endif
  368. };
  369. break;
  370. };
  371. case CAM_CTRL_PROTO_PELCO:{
  372. #ifdef DEBUG
  373. LOG("PELCO");
  374. #endif
  375. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux );
  376. if( m_pProtocol!= NULL ) {
  377. basicTransport *tTransport = NULL;
  378. hardwareManager *tHM = hardwareManager::getInstance();
  379. if ( tHM ) {
  380. hardwareResource * tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  381. if( tRES != NULL ) {
  382. if( tRES->isValid()) {
  383. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  384. if( m_pProtocol->setTransport( tTransport )){
  385. if( !m_pProtocol->create() ){
  386. LOG_ERROR("Protocol <<< Create >>> Failure!");
  387. };
  388. } else {
  389. LOG_ERROR("Binding Port Failure!");
  390. };
  391. };
  392. #ifdef DEBUG
  393. } else {
  394. LOG("HWM Has No UART PORT!");
  395. #endif
  396. };
  397. #ifdef DEBUG
  398. } else {
  399. LOG_ERROR("Can not get Hardware Manager!");
  400. #endif
  401. };
  402. if( tTransport!= NULL ) {
  403. LOG( "Bind Transport Port!" );
  404. } else {
  405. LOG_ERROR("Transport Creation Failure!");
  406. delete( ( protocolPTZ_PELCO * )m_pProtocol );
  407. m_pProtocol = NULL;
  408. };
  409. #ifdef DEBUG
  410. } else {
  411. LOG_ERROR("Protocol Creation failure!");
  412. #endif
  413. };
  414. break;
  415. };
  416. case CAM_CTRL_PROTO_ONVIF:{
  417. #ifdef DEBUG_EXTRA
  418. LOG("ONVIF");
  419. #endif
  420. //m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  421. if( strstr( m_sCamData.onvif_ctrl, "http" ) == NULL ) {
  422. char tURI[256];
  423. if ( m_sCamData.port ) {
  424. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  425. } else {
  426. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  427. };
  428. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF( tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  429. } else {
  430. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  431. };
  432. #ifdef DEBUG_EXTRA
  433. LOG("ONVIF-END");
  434. #endif
  435. if( m_pProtocol != NULL ) {
  436. if( !m_pProtocol->create() ) {
  437. LOG_ERROR("Error Creating Proto!");
  438. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  439. m_pProtocol = NULL;
  440. };
  441. #ifdef DEBUG_EXTRA
  442. LOG("PROTO CREATE DONE");
  443. #endif
  444. };
  445. break;
  446. };
  447. default:{
  448. #ifdef DEBUG
  449. LOG("Camera Has no defined control protocol");
  450. #endif
  451. };
  452. };
  453. //Check - No ABS Movement!
  454. if( ( m_sCamData.type == ARGUS_IP_PRESET_CAMERA ) || ( m_sCamData.type == ARGUS_ANALOG_PRESET_CAMERA ) ) m_bNoAbsPositionning = true;
  455. if( m_pProtocol == NULL ){
  456. if( m_sCamData.protocol == 0 ) {
  457. m_bCameraIsStatic = true;
  458. m_bSkeepPositioningWait = true;
  459. m_sCamData.online = true;
  460. } else LOG_ERROR("No Proto Created!");
  461. } else {
  462. #ifdef DEBUG_EXTRA
  463. LOG("Proto is OK");
  464. #endif
  465. //Load Presets only if we have protocol
  466. load_presets();
  467. mojoKernel *tKRNL = mojoKernel::getInstance();
  468. if( tKRNL ) {
  469. m_bSkeepPositioningWait = (( tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO ) == EVE_OUTPUT_VIDEO );
  470. #ifdef DEBUG_EXTRA
  471. char ttTT[128];
  472. sprintf( ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode() );
  473. LOG( ttTT );
  474. if ( m_bSkeepPositioningWait ) {
  475. LOG("Camera No Wait On Pos!");
  476. } else {
  477. LOG("Camera Will Wait On Pos!");
  478. };
  479. #endif
  480. } else {
  481. LOG_ERROR("No Kernel Instance!");
  482. };
  483. m_sCamData.online = init();
  484. };
  485. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  486. return m_sCamData.online;
  487. #endif //!CAM_POST_INIT
  488. };
  489. //void deviceNodeCamera::setBusy( bool Status ){
  490. //m_bBusy = Status;
  491. //};
  492. bool deviceNodeCamera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){
  493. if( m_sCamData.online ) {
  494. uint32_t MoveMatrix = 0;
  495. if( PanSPD != .0f ){
  496. if( PanSPD > 0 ) {
  497. if ( m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 2 ) {
  498. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  499. } else {
  500. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  501. };
  502. } else {
  503. if ( m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 2 ) {
  504. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  505. } else {
  506. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  507. };
  508. };
  509. };
  510. if( TiltSPD != .0f ){
  511. if( TiltSPD > 0 ) {
  512. if ( m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 1 ) {
  513. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  514. } else {
  515. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  516. };
  517. } else {
  518. if ( m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 1 ) {
  519. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  520. } else {
  521. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  522. };
  523. };
  524. };
  525. if( ZoomSPD != .0f ){
  526. if( ZoomSPD > 0 ) {
  527. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  528. } else {
  529. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  530. };
  531. };
  532. if( MoveMatrix != 0 ) {
  533. m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) );
  534. };
  535. return m_pProtocol->move( MoveMatrix );
  536. };
  537. return false;
  538. };
  539. bool deviceNodeCamera::relativeStop(){
  540. if( m_sCamData.online ) {
  541. return m_pProtocol->move( 0 );
  542. };
  543. return false;
  544. };
  545. CAM_TASK *deviceNodeCamera::fetchCamTask( void ){
  546. CAM_TASK *rVal = NULL;
  547. m_mQPROTECTOR.lock();
  548. if (m_qCAM_TASK.size()) {
  549. rVal = m_qCAM_TASK.front();
  550. m_qCAM_TASK.pop();
  551. };
  552. m_mQPROTECTOR.unlock();
  553. return rVal;
  554. };
  555. bool deviceNodeCamera::pushCamTask( CAM_TASK *ptr ){
  556. if ( ptr == NULL ) return false;
  557. m_mQPROTECTOR.lock();
  558. m_qCAM_TASK.push( ptr );
  559. m_mQPROTECTOR.unlock();
  560. return true;
  561. };
  562. bool deviceNodeCamera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){
  563. if( ptr == NULL ) return false;
  564. float tFI = fabsf( ptr->cam_position.fi - tp.fi );
  565. float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta );
  566. #ifdef DEBUG_ALL
  567. char tB[128];
  568. sprintf(tB,"FC: FOV:[%.5f:%.5f] dTG:[%.5f:%.5f] %.2f<%.2f>%.2f",ptr->h_fov ,ptr->v_fov, tFI, tTETTA, ptr->min_range, tp.r, ptr->max_range );
  569. LOG(tB);
  570. #endif
  571. if( tFI > ptr->h_fov ) return false;
  572. if( tTETTA > ptr->v_fov ) return false;
  573. if( tp.r < ptr->min_range ) return false;
  574. if( tp.r > ptr->max_range ) return false;
  575. return true;
  576. };
  577. bool deviceNodeCamera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){
  578. if( db_id == 0 ) return false;
  579. CAM_TRACK_RECORD *tTrackRecord = (CAM_TRACK_RECORD *)malloc(sizeof(CAM_TRACK_RECORD));
  580. if( tTrackRecord == NULL ) return false;
  581. #ifdef DEBUG_EXTRA
  582. LOG("Fusion: Consuming new Data!");
  583. #endif
  584. memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD));
  585. tTrackRecord->target_direction.fi = tOR.fi;
  586. tTrackRecord->target_direction.r = tOR.r;
  587. tTrackRecord->target_direction.tetta = tOR.tetta;
  588. tTrackRecord->evt_db_ID = db_id;
  589. tTrackRecord->ts_ms = getMilliseconds();
  590. m_mROIsProtector.lock();
  591. if( m_vROIS.size() ){
  592. vCAM_ROIS::iterator idx = m_vROIS.begin();
  593. for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break;
  594. if ( idx != m_vROIS.end() ) {
  595. // have Match!
  596. #ifdef DEBUG_EXTRA
  597. LOG("ROI MATCHED!");
  598. #endif
  599. (*idx)->targets.push_back( tTrackRecord );
  600. fusion_validateROI( tTrackRecord->ts_ms, (*idx) );
  601. } else {
  602. #ifdef DEBUG_EXTRA
  603. LOG("NO MATCH FOUND!");
  604. #endif
  605. // No mathced ROI -> create new one
  606. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  607. if ( tROI ) {
  608. memset( tROI, 0 , sizeof(CAM_ROI) );
  609. tROI->ts_ms = tTrackRecord->ts_ms;
  610. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  611. tROI->cam_position.fi = tOR.fi;
  612. tROI->cam_position.tetta = tOR.tetta;
  613. tROI->cam_position.r = tOR.r;
  614. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange );
  615. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  616. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  617. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function
  618. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen
  619. tROI->targets.push_back( tTrackRecord );
  620. m_vROIS.push_back( tROI );
  621. };
  622. };
  623. } else {
  624. #ifdef DEBUG_EXTRA
  625. LOG("Fusion: NO ROI -> CREATE!");
  626. #endif
  627. //
  628. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  629. if ( tROI ) {
  630. memset( tROI, 0 , sizeof(CAM_ROI) );
  631. tROI->ts_ms = tTrackRecord->ts_ms;
  632. //Set vector Cam Coordinates
  633. tROI->cam_position.fi = tOR.fi;
  634. tROI->cam_position.tetta = tOR.tetta;
  635. tROI->cam_position.r = tOR.r;
  636. // Set vector OnVIF coordinates
  637. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  638. // make fusion data BOX
  639. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange );
  640. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  641. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  642. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation
  643. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  644. // Add ROI to vector
  645. tROI->targets.push_back( tTrackRecord );
  646. m_vROIS.push_back( tROI );
  647. } else LOG_ERROR("No MEM4ROI");
  648. };
  649. m_mROIsProtector.unlock();
  650. return true;
  651. };
  652. bool deviceNodeCamera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){
  653. if( ptr == NULL ) return false;
  654. if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false;
  655. #ifdef DEBUG_EXTRA
  656. char tB[128];
  657. sprintf( tB, "\tROI [%d]", ptr->targets.size() );
  658. LOG( tB );
  659. #endif
  660. if( ptr->targets.size() ) {
  661. double tA = 0.0;
  662. double tB = 0.0;
  663. double tC = 0.0;
  664. uint32_t tCNT = 0;
  665. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  666. if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) {
  667. free( (*idx) );
  668. ptr->targets.erase( idx );
  669. } else {
  670. tA = tA + (*idx)->target_direction.fi;
  671. tB = tB + (*idx)->target_direction.tetta;
  672. tC = tC + (*idx)->target_direction.r;
  673. tCNT++;
  674. };
  675. };
  676. if( tCNT ) {
  677. C3D_SPH_DEGREES L = { 0 };
  678. L.fi = (float) ( tA /(double) tCNT );
  679. L.tetta = (float) ( tB /(double) tCNT );
  680. L.r = (float) ( tC /(double) tCNT );
  681. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  682. ptr->ts_ms = cts;
  683. float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange );
  684. ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  685. ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  686. ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation
  687. ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  688. return true;
  689. };
  690. };
  691. return false;
  692. };
  693. CAM_ROI *deviceNodeCamera::fusion_fetchROI( void ){
  694. CAM_ROI *tVAL = NULL;
  695. m_mROIsProtector.lock();
  696. if( m_vROIS.size() ){
  697. vCAM_ROIS::iterator sRec;
  698. uint64_t cts = 0;
  699. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  700. if ( tVAL == NULL ) {
  701. sRec = idx;
  702. tVAL = (*idx);
  703. cts = tVAL->ts_ms;
  704. } else {
  705. #ifdef CAM_FETCH_ROI_NEWEST
  706. if ( (*idx)->ts_ms > cts )
  707. #else
  708. if ( (*idx)->ts_ms < cts )
  709. #endif
  710. {
  711. sRec = idx;
  712. tVAL = (*idx);
  713. cts = tVAL->ts_ms;
  714. };
  715. };
  716. };
  717. m_vROIS.erase( sRec );
  718. };
  719. m_mROIsProtector.unlock();
  720. if ( m_bNoAbsPositionning && tVAL ) {
  721. // Skip targets older 3 seconds
  722. if ( getMilliseconds() - tVAL->ts_ms > 3000 )
  723. #ifdef DEBUG_EXTRA
  724. {
  725. LOG("Targetd too old! ---> Skiping");
  726. utilizeROI( tVAL );
  727. };
  728. #else
  729. utilizeROI( tVAL );
  730. #endif
  731. };
  732. return tVAL;
  733. };
  734. DBC_SNAPSHOT_DATA *deviceNodeCamera::fusion_translate_ROI( CAM_ROI *(&ptr) ){
  735. DBC_SNAPSHOT_DATA *rVal = NULL;
  736. if( ptr == NULL ) return NULL;
  737. if( ptr->targets.size() ){
  738. rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA));
  739. if( rVal ){
  740. memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) );
  741. rVal->stream_id = m_sNodeData.aux_parameter;
  742. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ )
  743. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  744. };
  745. ptr->targets.clear();
  746. } else {
  747. LOG_ERROR("No Events ID to ROI");
  748. };
  749. utilizeROI( ptr );
  750. // Not Setting MD5 and TS -> will set at capture time
  751. return rVal;
  752. };
  753. void deviceNodeCamera::fusion_validate( void ){
  754. if ( m_bNoAbsPositionning ) return;
  755. m_mROIsProtector.lock();
  756. if( m_vROIS.size() ){
  757. #ifdef DEBUG_EXTRA
  758. char tB[128];
  759. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  760. LOG( tB );
  761. #endif
  762. uint64_t cts = getMilliseconds();
  763. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  764. if( !fusion_validateROI( cts, *idx ) ) {
  765. utilizeROI( (*idx) );
  766. m_vROIS.erase( idx );
  767. };
  768. };
  769. };
  770. m_mROIsProtector.unlock();
  771. };
  772. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  773. if( m_sCamData.online ){
  774. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  775. if( tTask ){
  776. memset(tTask, 0, sizeof(CAM_TASK));
  777. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  778. if( tTask->CMD ){
  779. tTask->CMD->CMD = PTZ_ABSOLUTE;
  780. tTask->CMD->PAN = PanPOS;
  781. tTask->CMD->TILT = TiltPOS;
  782. tTask->CMD->ZOOM = ZoomPOS;
  783. tTask->movemet_needed = true;
  784. tTask->source_evt_id = TargetRefID;
  785. setWM( CAM_WS_BUSY );
  786. return pushCamTask( tTask );
  787. } else {
  788. LOG_ERROR("CMD malloc Error!");
  789. free( tTask );
  790. };
  791. } else LOG_ERROR("Task malloc error!");
  792. };
  793. return false;
  794. };
  795. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  796. if( m_sCamData.online ){
  797. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  798. if( tTask ){
  799. memset(tTask, 0, sizeof(CAM_TASK));
  800. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  801. if( tTask->CMD ){
  802. tTask->CMD->CMD = PTZ_ABSOLUTE;
  803. tTask->CMD->PAN = O.pan;
  804. tTask->CMD->TILT = O.tilt;
  805. tTask->CMD->ZOOM = O.zoom;
  806. tTask->movemet_needed = true;
  807. tTask->source_evt_id = TargetRefID;
  808. setWM( CAM_WS_BUSY );
  809. return pushCamTask( tTask );
  810. } else {
  811. LOG_ERROR("CMD malloc Error!");
  812. free( tTask );
  813. };
  814. } else LOG_ERROR("Task malloc error!");
  815. };
  816. return false;
  817. };
  818. uint8_t deviceNodeCamera::getPtzStatus(){
  819. if ( m_sCamData.protocol == 0 ) return 1;
  820. if ( getWM() > CAM_WS_INIT ) return 1;
  821. return 0;
  822. };
  823. uint8_t deviceNodeCamera::getStreamStatus(){
  824. return 1;
  825. };
  826. void deviceNodeCamera::m_fThreadProc( void ){
  827. DBC_PUSHED_CMD * PUSHED = NULL;
  828. m_bThreadStatus = TH_WORKING;
  829. bool bNewSecondLoop = false;
  830. uint32_t cTimeSeconds = 0;
  831. uint32_t oTimeSeconds = 0;
  832. while( m_bThreadStatus < TH_ENDED ) {
  833. cTimeSeconds = (uint32_t) time( NULL );
  834. if ( bNewSecondLoop = ( cTimeSeconds != oTimeSeconds ) ) oTimeSeconds = cTimeSeconds;
  835. uint8_t tState = getWM();
  836. char tB[128];
  837. fusion_validate();
  838. switch( tState ) {
  839. case CAM_WS_INIT:{
  840. if ( m_pProtocol != NULL ) {
  841. switch( m_sCamData.protocol ) {
  842. case CAM_CTRL_PROTO_VISCA:
  843. delete( (protocolPTZ_VISCA *) m_pProtocol );
  844. break;
  845. case CAM_CTRL_PROTO_PELCO:
  846. delete( (protocolPTZ_PELCO *) m_pProtocol );
  847. break;
  848. case CAM_CTRL_PROTO_ONVIF:
  849. delete( (protocolPTZ_ONVIF *) m_pProtocol );
  850. break;
  851. default:
  852. delete( m_pProtocol );
  853. };
  854. m_pProtocol = NULL;
  855. m_sCamData.online = false;
  856. };
  857. switch( m_sCamData.protocol ){
  858. case CAM_CTRL_PROTO_VISCA: {
  859. #ifdef DEBUG
  860. LOG("VISCA");
  861. #endif
  862. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux );
  863. if( m_pProtocol!= NULL ) {
  864. basicTransport *tTransport = NULL;
  865. hardwareManager *tHM = hardwareManager::getInstance();
  866. if( tHM ){
  867. hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  868. if( tRES != NULL ) {
  869. if( tRES->isValid()) {
  870. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  871. if( m_pProtocol->setTransport( tTransport )){
  872. if( !m_pProtocol->create() ){
  873. LOG_ERROR("Protocol <<< Create >>> Failure!");
  874. };
  875. } else {
  876. LOG_ERROR("Binding Port Failure!");
  877. };
  878. };
  879. #ifdef DEBUG
  880. } else {
  881. LOG("HWM Has No UART PORT!");
  882. #endif
  883. };
  884. #ifdef DEBUG
  885. } else {
  886. LOG_ERROR("Can not get Hardware Manager!");
  887. #endif
  888. };
  889. if( tTransport!= NULL ) {
  890. LOG( "Bind Transport Port!" );
  891. } else {
  892. LOG_ERROR("Transport Creation Failure!");
  893. delete( ( protocolPTZ_VISCA * )m_pProtocol );
  894. m_pProtocol = NULL;
  895. };
  896. #ifdef DEBUG
  897. } else {
  898. LOG_ERROR("Protocol Creation failure!");
  899. #endif
  900. };
  901. break;
  902. };
  903. case CAM_CTRL_PROTO_PELCO:{
  904. #ifdef DEBUG
  905. LOG("PELCO");
  906. #endif
  907. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux );
  908. if( m_pProtocol!= NULL ) {
  909. basicTransport *tTransport = NULL;
  910. hardwareManager *tHM = hardwareManager::getInstance();
  911. if ( tHM ) {
  912. hardwareResource * tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  913. if( tRES != NULL ) {
  914. if( tRES->isValid()) {
  915. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  916. if( m_pProtocol->setTransport( tTransport )){
  917. if( !m_pProtocol->create() ){
  918. LOG_ERROR("Protocol <<< Create >>> Failure!");
  919. };
  920. } else {
  921. LOG_ERROR("Binding Port Failure!");
  922. };
  923. };
  924. #ifdef DEBUG
  925. } else {
  926. LOG("HWM Has No UART PORT!");
  927. #endif
  928. };
  929. #ifdef DEBUG
  930. } else {
  931. LOG_ERROR("Can not get Hardware Manager!");
  932. #endif
  933. };
  934. if( tTransport!= NULL ) {
  935. LOG( "Bind Transport Port!" );
  936. } else {
  937. LOG_ERROR("Transport Creation Failure!");
  938. delete( ( protocolPTZ_PELCO * )m_pProtocol );
  939. m_pProtocol = NULL;
  940. };
  941. #ifdef DEBUG
  942. } else {
  943. LOG_ERROR("Protocol Creation failure!");
  944. #endif
  945. };
  946. break;
  947. };
  948. case CAM_CTRL_PROTO_ONVIF:{
  949. #ifdef DEBUG_EXTRA
  950. LOG("ONVIF");
  951. #endif
  952. //m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  953. if( strstr( m_sCamData.onvif_ctrl, "http" ) == NULL ) {
  954. char tURI[256];
  955. if ( m_sCamData.port ) {
  956. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  957. } else {
  958. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  959. };
  960. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF( tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  961. } else {
  962. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  963. };
  964. #ifdef DEBUG_EXTRA
  965. LOG("ONVIF-END");
  966. #endif
  967. if( m_pProtocol != NULL ) {
  968. if( !m_pProtocol->create() ) {
  969. LOG_ERROR("Error Creating Proto!");
  970. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  971. m_pProtocol = NULL;
  972. };
  973. #ifdef DEBUG_EXTRA
  974. LOG("PROTO CREATE DONE");
  975. #endif
  976. };
  977. break;
  978. };
  979. default:{
  980. #ifdef DEBUG
  981. LOG("Camera Has no defined control protocol");
  982. #endif
  983. };
  984. };
  985. if ( m_pProtocol != NULL ) {
  986. //Load Presets only if we have protocol
  987. load_presets();
  988. mojoKernel *tKRNL = mojoKernel::getInstance();
  989. if( tKRNL ) {
  990. m_bSkeepPositioningWait = (( tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO ) == EVE_OUTPUT_VIDEO );
  991. #ifdef DEBUG_EXTRA
  992. char ttTT[128];
  993. sprintf( ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode() );
  994. LOG( ttTT );
  995. if ( m_bSkeepPositioningWait ) {
  996. LOG("Camera No Wait On Pos!");
  997. } else {
  998. LOG("Camera Will Wait On Pos!");
  999. };
  1000. #endif
  1001. } else {
  1002. LOG_ERROR("No Kernel Instance!");
  1003. };
  1004. m_sCamData.online = true;
  1005. setWM(CAM_WS_POWER_INIT);
  1006. };
  1007. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  1008. break;
  1009. };
  1010. case CAM_WS_POWER_INIT:{
  1011. #ifdef DEBUG
  1012. LOG("Init Camera - PowerMode");
  1013. #endif
  1014. //setBusy( true );
  1015. if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE );
  1016. //setBusy( false );
  1017. break;
  1018. };
  1019. case CAM_WS_WHITE_BALLANCE:{
  1020. #ifdef DEBUG
  1021. LOG("Set WB");
  1022. #endif
  1023. //setBusy( true );
  1024. if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT );
  1025. //setBusy( false );
  1026. break;
  1027. };
  1028. case CAM_WS_BACKLIGHT:{
  1029. #ifdef DEBUG
  1030. LOG("Set BLC");
  1031. #endif
  1032. //setBusy( true );
  1033. if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS );
  1034. //setBusy( false );
  1035. break;
  1036. };
  1037. case CAM_WS_AUTO_FOCUS:{
  1038. #ifdef DEBUG
  1039. LOG("Set AF");
  1040. #endif
  1041. //setBusy( true );
  1042. if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME );
  1043. //setBusy( false );
  1044. break;
  1045. };
  1046. case CAM_WS_INIT_GO_HOME:{
  1047. m_nHomingCounter = 0;
  1048. /* Skip For Restartin bug
  1049. if( !m_bNoAbsPositionning ) {
  1050. #ifdef DEBUG
  1051. LOG("INIT GO -1 0 -1");
  1052. #endif
  1053. if (m_pProtocol->goAbsolute(-1, 0, -1)) {
  1054. m_fCP_Pan = -1;
  1055. m_fCP_Tilt = 0;
  1056. m_fCP_Zoom = -1;
  1057. setWM( CAM_WS_READY );
  1058. };
  1059. } else {
  1060. #ifdef DEBUG
  1061. LOG("Go Home!");
  1062. #endif
  1063. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1064. if( TK ) {
  1065. if ( !m_bCameraOnHomePosition ) {
  1066. if( !m_pProtocol->goPreset( TK )){
  1067. LOG_ERROR("Cannot Issue goPreset Command!");
  1068. };
  1069. m_bCameraOnHomePosition = true;
  1070. };
  1071. };
  1072. setWM( CAM_WS_READY );
  1073. };*/
  1074. setWM( CAM_WS_READY );
  1075. break;
  1076. };
  1077. case CAM_WS_AUTOMATED_MOVE:{
  1078. m_bCameraOnHomePosition = false;
  1079. m_nRelativeMoveCounter++;
  1080. if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) {
  1081. //Pan Move
  1082. if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) {
  1083. m_fRM_Pan -= m_fSpeedRise;
  1084. } else {
  1085. m_fRM_Pan += m_fSpeedRise;
  1086. };
  1087. };
  1088. if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) {
  1089. //TILT Move
  1090. if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) {
  1091. m_fRM_Tilt -= m_fSpeedRise;
  1092. } else {
  1093. m_fRM_Tilt += m_fSpeedRise;
  1094. }
  1095. };
  1096. if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) {
  1097. //ZOOM Move
  1098. if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) {
  1099. //Wide
  1100. m_fRM_Zoom -= m_fSpeedRise;
  1101. } else {
  1102. //Tele
  1103. m_fRM_Zoom += m_fSpeedRise;
  1104. };
  1105. };
  1106. if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f;
  1107. if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f;
  1108. if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f;
  1109. if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f;
  1110. if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f;
  1111. if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f;
  1112. m_fCP_Zoom += 0.5f*m_fRM_Zoom;
  1113. if( m_fCP_Zoom < -1.0f ) m_fCP_Zoom = -1.0f;
  1114. if( m_fCP_Zoom > 1.0f ) m_fCP_Zoom = 1.0f;
  1115. if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){
  1116. m_nRelativeMoveCounter = 0;
  1117. setWM( CAM_WS_READY );
  1118. break;
  1119. };
  1120. if( m_sCamData.ptz_axel ) { //Manual
  1121. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) {
  1122. m_nRelativeMoveCounter = 0;
  1123. setWM( CAM_WS_READY );
  1124. };
  1125. } else {
  1126. // Stepping Axel by Zoom
  1127. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  1128. if( m_nRelativeMoveCounter > tVal ) {
  1129. m_nRelativeMoveCounter = 0;
  1130. setWM( CAM_WS_READY );
  1131. };
  1132. };
  1133. break;
  1134. };
  1135. case CAM_WS_STOP_MOVE:{
  1136. m_nRelativeMoveCounter++;
  1137. m_fRM_Pan = .0f;
  1138. m_fRM_Pan = .0f;
  1139. m_fRM_Tilt = .0f;
  1140. m_fRM_Tilt = .0f;
  1141. m_fRM_Zoom = .0f;
  1142. m_fRM_Zoom = .0f;
  1143. if( relativeStop() ) {
  1144. m_nHomingCounter = 0;
  1145. m_nRelativeMoveCounter = 0;
  1146. setWM( CAM_WS_READY );
  1147. break;
  1148. };
  1149. if( m_sCamData.ptz_axel ) {
  1150. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) setWM(CAM_WS_READY);
  1151. } else {
  1152. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  1153. if (m_nRelativeMoveCounter > tVal) setWM(CAM_WS_READY);
  1154. };
  1155. break;
  1156. };
  1157. case CAM_WS_GO_ABS_ROI:
  1158. case CAM_WS_GO_ABS_POSITION:{
  1159. m_bCameraOnHomePosition = false;
  1160. setWM( CAM_WS_BUSY );
  1161. if( m_pCurrentROI != NULL ){
  1162. //LOG("ROI Ok");
  1163. m_nHomingCounter = 0;
  1164. if( m_pBEH ){
  1165. EVENT_CONTAINER *tEVT = createBasicEvent(
  1166. m_sNodeData.node_id,
  1167. EVT_CLASS_CAMERA_CTRL,
  1168. EVT_CAMERA_MOVE_TO,
  1169. NULL, NULL, NULL );
  1170. if( tEVT ) m_pBEH->broadcast( tEVT );
  1171. };
  1172. if( m_bNoAbsPositionning ){
  1173. //LOG("ABS to PRESET");
  1174. uint8_t COL, ROW;
  1175. translateROI2PRESET(
  1176. m_pCurrentROI->cam_orientation.pan,
  1177. m_pCurrentROI->cam_orientation.tilt,
  1178. m_pCurrentROI->cam_orientation.zoom, COL, ROW );
  1179. //char tB[64];
  1180. //sprintf( tB, "Get ABS Preset Col %d Row %d!", COL, ROW );
  1181. //LOG( tB );
  1182. //sprintf( tB, "Old Preset Col %d Row %d!", m_bCurrentPresetCol, m_bCurrentPresetRow );
  1183. //LOG( tB );
  1184. if ( ( m_bCurrentPresetCol == COL ) && ( m_bCurrentPresetRow == ROW ) ) {
  1185. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1186. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1187. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1188. #ifdef DEBUG_EXTRA
  1189. LOG("INSTANT BCAST CAM ONPOS");
  1190. #endif
  1191. // Send Capture event
  1192. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1193. if( tData != NULL ) {
  1194. if( m_pBEH ){
  1195. EVENT_CONTAINER *tEVT = createBasicEvent(
  1196. m_sNodeData.node_id,
  1197. EVT_CLASS_CAMERA_CTRL,
  1198. EVT_CAMERA_ON_POSITION,
  1199. NULL, NULL, tData );
  1200. if( tEVT ){
  1201. m_pBEH->broadcast( tEVT );
  1202. } else {
  1203. free( tData );
  1204. };
  1205. } else {
  1206. free( tData );
  1207. };
  1208. };
  1209. setWM( CAM_WS_READY );
  1210. } else {
  1211. CSTR token = getPresetToken( COL, ROW );
  1212. if ( token ) {
  1213. //update position Info
  1214. m_bCurrentPresetCol = COL;
  1215. m_bCurrentPresetRow = ROW;
  1216. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1217. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1218. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1219. if( !m_pProtocol->goPreset( token ) ){
  1220. LOG_ERROR("Cannot Issue goPreset Command!->Restart Camera?");
  1221. setWM( CAM_WS_END );
  1222. } else {
  1223. // Waiting Movement finish
  1224. if( m_bSkeepPositioningWait && 0 ){
  1225. #ifdef DEBUG_EXTRA
  1226. LOG("INSTANT BCAST CAM ONPOS");
  1227. #endif
  1228. // Send Capture event
  1229. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1230. if( tData != NULL ) {
  1231. if( m_pBEH ){
  1232. EVENT_CONTAINER *tEVT = createBasicEvent(
  1233. m_sNodeData.node_id,
  1234. EVT_CLASS_CAMERA_CTRL,
  1235. EVT_CAMERA_ON_POSITION,
  1236. NULL, NULL, tData );
  1237. if( tEVT ){
  1238. m_pBEH->broadcast( tEVT );
  1239. } else {
  1240. free( tData );
  1241. };
  1242. } else {
  1243. free( tData );
  1244. };
  1245. };
  1246. setWM( CAM_WS_READY );
  1247. } else {
  1248. //utilizeROI( m_pCurrentROI );
  1249. setWM( CAM_WS_WAIT_ONPOS );
  1250. };
  1251. };
  1252. } else {
  1253. #ifdef DEBUG_EXTRA
  1254. LOG("Not Found!");
  1255. #endif
  1256. utilizeROI( m_pCurrentROI );
  1257. setWM( CAM_WS_READY );
  1258. };
  1259. };
  1260. } else {
  1261. //update position Info
  1262. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1263. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1264. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1265. // Do Movement
  1266. if( !m_pProtocol->goAbsolute(
  1267. m_pCurrentROI->cam_orientation.pan,
  1268. m_pCurrentROI->cam_orientation.tilt,
  1269. ( m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection )
  1270. )){
  1271. LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?");
  1272. setWM( CAM_WS_END );
  1273. } else {
  1274. // Waiting Movement finish
  1275. if( m_bSkeepPositioningWait ){
  1276. #ifdef DEBUG_EXTRA
  1277. LOG("INSTANT BCAST CAM ONPOS");
  1278. #endif
  1279. // Send Capture event
  1280. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1281. if( tData != NULL ) {
  1282. if( m_pBEH ){
  1283. EVENT_CONTAINER *tEVT = createBasicEvent(
  1284. m_sNodeData.node_id,
  1285. EVT_CLASS_CAMERA_CTRL,
  1286. EVT_CAMERA_ON_POSITION,
  1287. NULL, NULL, tData );
  1288. if( tEVT ){
  1289. m_pBEH->broadcast( tEVT );
  1290. } else {
  1291. free( tData );
  1292. };
  1293. } else {
  1294. free( tData );
  1295. };
  1296. };
  1297. setWM( CAM_WS_READY );
  1298. } else {
  1299. setWM(CAM_WS_WAIT_ONPOS);
  1300. };
  1301. };
  1302. };
  1303. } else setWM( CAM_WS_READY );
  1304. break;
  1305. };
  1306. case CAM_WS_WAIT_ONPOS:{
  1307. //LOG("WaitOnPos...");
  1308. DBC_SNAPSHOT_DATA *tData = NULL;
  1309. if( m_pCurrentROI != NULL ){
  1310. // Send Capture event
  1311. tData = fusion_translate_ROI( m_pCurrentROI );
  1312. };
  1313. // To Accept any new ROI's ??
  1314. // setWM( CAM_WS_READY );
  1315. #ifdef DEBUG_EXTRA
  1316. LOG("Start Waiting Cam On Position!");
  1317. #endif
  1318. if ( !m_bSkeepPositioningWait ) {
  1319. if (!m_pProtocol->waitMoveIsDone()) m_bSkeepPositioningWait = true;
  1320. };
  1321. if ( m_bSkeepPositioningWait ) SLEEP( 3000 );
  1322. #ifdef DEBUG_EXTRA
  1323. LOG("Done Waiting Cam On Position!");
  1324. #endif
  1325. if( tData != NULL ) {
  1326. if( m_pBEH ){
  1327. EVENT_CONTAINER *tEVT = createBasicEvent(
  1328. m_sNodeData.node_id,
  1329. EVT_CLASS_CAMERA_CTRL,
  1330. EVT_CAMERA_ON_POSITION,
  1331. NULL, NULL, tData );
  1332. if( tEVT ){
  1333. #ifdef DEBUG_EXTRA
  1334. LOG("BCAST CAM ONPOS");
  1335. #endif
  1336. m_pBEH->broadcast( tEVT );
  1337. } else {
  1338. free( tData );
  1339. };
  1340. } else {
  1341. free( tData );
  1342. };
  1343. tData = NULL;
  1344. };
  1345. setWM( CAM_WS_READY );
  1346. break;
  1347. };
  1348. case CAM_WS_GO_HOMING_PRESET:{
  1349. if ( !m_bCameraOnHomePosition ) {
  1350. // For new Camera Presets
  1351. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1352. if( TK ) {
  1353. #ifdef DEBUG_EXTRA
  1354. LOG("Go Home!");
  1355. LOG( TK );
  1356. #endif
  1357. if( !m_pProtocol->goPreset( TK )){
  1358. LOG_ERROR("Cannot Issue goPreset Command!");
  1359. };
  1360. } else {
  1361. //Old Method
  1362. if( m_sCamData.home_token[0] != (char)0 ) {
  1363. #ifdef DEBUG_EXTRA
  1364. LOG("Go Home!");
  1365. LOG( m_sCamData.home_token );
  1366. #endif
  1367. if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1368. LOG_ERROR("Cannot Issue goPreset Command!");
  1369. };
  1370. };
  1371. };
  1372. m_bCameraOnHomePosition = true;
  1373. m_bCurrentPresetCol = 0;
  1374. m_bCurrentPresetRow = 0;
  1375. };
  1376. setWM( CAM_WS_READY );
  1377. break;
  1378. };
  1379. case CAM_WS_GO_PRESET:{
  1380. LOG("Go Preset!");
  1381. //if( m_sCamData.home_token[0] != (char)0 ) {
  1382. // LOG("Go Home!");
  1383. // LOG( m_sCamData.home_token );
  1384. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1385. // LOG_ERROR("Cannot Issue goPreset Command!");
  1386. // };
  1387. //};
  1388. setWM( CAM_WS_READY );
  1389. break;
  1390. };
  1391. case CAM_WS_SET_PRESET:{
  1392. LOG("Set Preset!");
  1393. //if( m_sCamData.home_token[0] != (char)0 ) {
  1394. // LOG("Go Home!");
  1395. // LOG( m_sCamData.home_token );
  1396. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1397. // LOG_ERROR("Cannot Issue goPreset Command!");
  1398. // };
  1399. //};
  1400. setWM( CAM_WS_READY );
  1401. break;
  1402. };
  1403. case CAM_WS_BUSY:{
  1404. //Handle Operation TO
  1405. setWM( CAM_WS_READY );
  1406. break;
  1407. };
  1408. case CAM_WS_READY:{
  1409. m_pCurrentROI = fusion_fetchROI();
  1410. if( m_pCurrentROI != NULL ) {
  1411. #ifdef DEBUG_EXTRA
  1412. LOG("HAVE ROI to see!");
  1413. #endif
  1414. setWM( CAM_WS_GO_ABS_POSITION );
  1415. } else {
  1416. //Idle Work --->
  1417. if ( m_sCamData.eco_ir ) {
  1418. m_mIRProtector.lock();
  1419. if( m_bIRIsOn ){
  1420. if ( m_bIRTO ) {
  1421. if ( bNewSecondLoop ) m_bIRTO--;
  1422. } else {
  1423. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_IR_OFF );
  1424. if ( TK ) if( m_pProtocol->goPreset( TK ) ) m_bIRIsOn = false;
  1425. };
  1426. };
  1427. m_mIRProtector.unlock();
  1428. };
  1429. if (m_sCamData.homing_enable) {
  1430. if ( bNewSecondLoop ) {
  1431. if ( m_nHomingCounter < m_sCamData.homing_to ) {
  1432. m_nHomingCounter++;
  1433. } else {
  1434. //LOG("Go Home Position!");
  1435. m_nHomingCounter = 0;
  1436. setWM( CAM_WS_GO_HOMING_PRESET );
  1437. break;
  1438. };
  1439. };
  1440. };
  1441. CAM_TASK * tTASK = fetchCamTask();
  1442. if ( tTASK != NULL ) {
  1443. if( tTASK->movemet_needed ){
  1444. CAM_PTZ_CMD *tCMD = tTASK->CMD;
  1445. if( tCMD ) {
  1446. switch( tCMD->CMD ) {
  1447. case PTZ_STOP:{
  1448. m_nHomingCounter = 0;
  1449. setWM( CAM_WS_STOP_MOVE );
  1450. break;
  1451. };
  1452. case PTZ_MOVE:{
  1453. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1454. m_bRelativeMoveMatrix = 0;
  1455. m_fRM_Pan = tCMD->PAN;
  1456. m_fRM_Tilt = tCMD->TILT;
  1457. m_fRM_Zoom = tCMD->ZOOM;
  1458. m_nHomingCounter = 0;
  1459. setWM( CAM_WS_AUTOMATED_MOVE );
  1460. break;
  1461. };
  1462. case PTZ_ABSOLUTE:{
  1463. m_pCurrentROI = createCAM_ROI();
  1464. if( m_pCurrentROI != NULL ){
  1465. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD));
  1466. if ( tTarget != NULL ) {
  1467. memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) );
  1468. tTarget->evt_db_ID = tTASK->source_evt_id;
  1469. m_pCurrentROI->targets.push_back( tTarget );
  1470. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  1471. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  1472. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  1473. m_nHomingCounter = 0;
  1474. setWM(CAM_WS_GO_ABS_POSITION);
  1475. } else {
  1476. utilizeROI( m_pCurrentROI );
  1477. };
  1478. };
  1479. break;
  1480. };
  1481. case PTZ_RELOAD_CAMERA:{
  1482. LOG("Exit Command Rcpt");
  1483. m_bThreadStatus = TH_ENDED;
  1484. };
  1485. };
  1486. free( tCMD );
  1487. tTASK->CMD = NULL;
  1488. };
  1489. } else {
  1490. LOG("No Movement -> INSTCAP" );
  1491. // No Movement -> Just Capture
  1492. if( m_pBEH ){
  1493. EVENT_CONTAINER *tEVT = createBasicEvent(
  1494. m_sNodeData.node_id,
  1495. EVT_CLASS_CAMERA_CTRL,
  1496. EVT_CAMERA_ON_POSITION,
  1497. NULL, NULL, NULL );
  1498. if( tEVT ){
  1499. tEVT->m_db_id = tTASK->source_evt_id;
  1500. m_pBEH->broadcast(tEVT);
  1501. };
  1502. };
  1503. };
  1504. free( tTASK );
  1505. } else {
  1506. // Lowest Priority to Web Client Commands
  1507. #ifdef DEBUG_EXTRA__
  1508. LOG("Check Web CMD!");
  1509. #endif
  1510. PUSHED = m_pDBC->getNodePushedCMD( m_sNodeData.node_id );
  1511. if( PUSHED != NULL ) {
  1512. #ifdef DEBUG_EXTRA__
  1513. LOG("HAVE Web CMD!");
  1514. #endif
  1515. m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN );
  1516. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  1517. switch( PUSHED->CMD ) {
  1518. case 0x80:{
  1519. //Call Home Preset
  1520. LOG("CALL HOME PRESET!");
  1521. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1522. if ( TK ) {
  1523. LOG(TK);
  1524. if( m_pProtocol->goPreset( TK )){
  1525. m_bCameraOnHomePosition = true;
  1526. m_nHomingCounter = 0;
  1527. };
  1528. };
  1529. break;
  1530. };
  1531. case 0x81:{
  1532. //Call IR On Preset
  1533. LOG("CALL IR ON PRESET!");
  1534. m_mIRProtector.lock();
  1535. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_ON);
  1536. if ( TK ) {
  1537. LOG(TK);
  1538. if( m_pProtocol->goPreset( TK )){
  1539. m_bIRIsOn = true;
  1540. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1541. };
  1542. } else LOG("PRESET NOT FOUND!");
  1543. m_mIRProtector.unlock();
  1544. break;
  1545. };
  1546. case 0x82:{
  1547. //Call IR Off Preset
  1548. LOG("CALL IR OFF PRESET!");
  1549. m_mIRProtector.lock();
  1550. CSTR TK = ( m_sCamData.eco_ir )?getPresetToken(ARGUS_PRESET_TYPE_IR_OFF):getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1551. if ( TK ) {
  1552. LOG(TK);
  1553. if( m_pProtocol->goPreset( TK )){
  1554. m_bIRIsOn = false;
  1555. m_bIRTO = 0;
  1556. };
  1557. } else LOG("PRESET NOT FOUND!");
  1558. m_mIRProtector.unlock();
  1559. break;
  1560. };
  1561. case 0x83:{
  1562. //Call IR Auto Preset
  1563. LOG("CALL IR AUTO PRESET!");
  1564. m_mIRProtector.lock();
  1565. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1566. if ( TK ) {
  1567. LOG(TK);
  1568. if( m_pProtocol->goPreset( TK )){
  1569. m_bIRIsOn = true;
  1570. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1571. };
  1572. } else LOG("PRESET NOT FOUND!");
  1573. m_mIRProtector.unlock();
  1574. break;
  1575. };
  1576. case 0x84:{
  1577. //Call Wisher Preset
  1578. LOG("CALL WIPER PRESET!");
  1579. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_WISHER);
  1580. if ( TK ) {
  1581. LOG(TK);
  1582. m_pProtocol->goPreset( TK );
  1583. };
  1584. break;
  1585. };
  1586. };
  1587. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){ //No Way to get here
  1588. //Direct Movment Mode
  1589. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  1590. m_nHomingCounter = 0;
  1591. setWM( CAM_WS_STOP_MOVE );
  1592. } else {
  1593. char tB[64];
  1594. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1595. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  1596. m_nHomingCounter = 0;
  1597. setWM( CAM_WS_AUTOMATED_MOVE );
  1598. };
  1599. };
  1600. free( PUSHED );
  1601. PUSHED = NULL;
  1602. };
  1603. };
  1604. };
  1605. break;
  1606. };
  1607. case CAM_WS_END:{
  1608. LOG("Exit Command Rcpt");
  1609. m_bThreadStatus = TH_ENDED;
  1610. break;
  1611. };
  1612. };
  1613. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  1614. };
  1615. #ifdef DEBUG
  1616. LOG("CTRL Loop End!");
  1617. #endif
  1618. };
  1619. bool deviceNodeCamera::setNoPositionWait( bool FL ){
  1620. m_bSkeepPositioningWait = FL;
  1621. return true;
  1622. };
  1623. bool deviceNodeCamera::testFOV( C3D_DECART L ){
  1624. //System Coords -> trans to local and go to funct
  1625. systemAnchor* tSystem = systemAnchor::getInstance();
  1626. if ( tSystem == NULL ) return false;
  1627. C3D_SPH_DEGREES SL;
  1628. #ifdef DEBUG_EXTRA
  1629. LOG("Test FOV local Decart coords");
  1630. #endif
  1631. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1632. return testFOV( SL );
  1633. };
  1634. bool deviceNodeCamera::testFOV( C3D_WGS84_LOC L ){
  1635. //Global Coords
  1636. systemAnchor* tSystem = systemAnchor::getInstance();
  1637. if ( tSystem == NULL ) return false;
  1638. C3D_SPH_DEGREES SL;
  1639. #ifdef DEBUG_EXTRA
  1640. LOG("Test FOV WGS coords");
  1641. #endif
  1642. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1643. return testFOV( SL );
  1644. };
  1645. bool deviceNodeCamera::testFOV( C3D_SPH_RADIANS L ){
  1646. //Local Coords
  1647. C3D_SPH_DEGREES SL;
  1648. #ifdef DEBUG_EXTRA
  1649. LOG("Test FOV local Spherical(rad) coords");
  1650. #endif
  1651. spherical2spherical( L, SL );
  1652. return testFOV( SL );
  1653. };
  1654. bool deviceNodeCamera::testFOV( C3D_SPH_DEGREES SL ){
  1655. #ifdef DEBUG
  1656. char tB[128];
  1657. #endif
  1658. #ifdef DEBUG_EXTRA
  1659. LOG("Test FOV local Spherical(deg) coords");
  1660. #endif
  1661. switch ( m_sCamData.triggering_mode ) {
  1662. case CAM_TRIGGER_STRICT:{
  1663. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1664. #ifdef DEBUG_EXTRA
  1665. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1666. LOG( tB );
  1667. #endif
  1668. return false;
  1669. };
  1670. break;
  1671. };
  1672. case CAM_TRIGGER_ALL_NEAR:{
  1673. if( SL.r > m_fMaxRange ){
  1674. #ifdef DEBUG_EXTRA
  1675. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1676. LOG( tB );
  1677. #endif
  1678. return false;
  1679. };
  1680. break;
  1681. };
  1682. case CAM_TRIGGER_ALL_FAR:{
  1683. if( SL.r < m_fMinRange ){
  1684. #ifdef DEBUG_EXTRA
  1685. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1686. LOG( tB );
  1687. #endif
  1688. return false;
  1689. };
  1690. break;
  1691. };
  1692. case CAM_TRIGGER_ALL: return true;
  1693. };
  1694. float HS = AC_imager_width[m_sCamData.imager_type];
  1695. float VS = AC_imager_height[m_sCamData.imager_type];
  1696. float HS2 = HS * HS;
  1697. float VS2 = VS * VS;
  1698. float IR = HS / VS;
  1699. float IR2 = IR * IR;
  1700. if( m_sCamData.pan_angles == 0 ) {
  1701. // Fixed Camera
  1702. float tA = fabsf( SL.fi );
  1703. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1704. if ( tA > tZ ) return false;
  1705. } else {
  1706. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1707. // Partial Pan
  1708. switch( m_sCamData.pan_model ) {
  1709. case 1: {
  1710. // Regular Pan
  1711. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1712. float tRad = deg2rad( SL.fi );
  1713. #ifdef DEBUG_EXTRA
  1714. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1715. LOG( tB );
  1716. #endif
  1717. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1718. break;
  1719. };
  1720. case 2:{
  1721. break;
  1722. };
  1723. case 3:{
  1724. break;
  1725. };
  1726. };
  1727. };
  1728. };
  1729. if( m_sCamData.tilt_angles == 0 ){
  1730. //Fixed Camera
  1731. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1732. } else {
  1733. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1734. // Partial Tilt
  1735. switch( m_sCamData.tilt_model ) {
  1736. case 1: {
  1737. // Regular Tilt
  1738. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1739. float tRad = deg2rad( SL.tetta );
  1740. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1741. break;
  1742. };
  1743. case 2:{
  1744. break;
  1745. };
  1746. };
  1747. };
  1748. };
  1749. return true;
  1750. };
  1751. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1752. systemAnchor* tSystem = systemAnchor::getInstance();
  1753. if( tSystem == NULL ) return false;
  1754. C3D_SPH_DEGREES SL;
  1755. #ifdef DEBUG_EXTRA
  1756. LOG("Look at WGS coords!");
  1757. #endif
  1758. tSystem->convertToLocal(&m_oPosition, L, SL );
  1759. return lookAt(TargetRefID, SL );
  1760. };
  1761. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1762. systemAnchor* tSystem = systemAnchor::getInstance();
  1763. if( tSystem == NULL ) return false;
  1764. C3D_SPH_DEGREES SL;
  1765. #ifdef DEBUG_EXTRA
  1766. LOG("Look at System Decart coords!");
  1767. #endif
  1768. tSystem->convertToLocal( &m_oPosition, L, SL );
  1769. return lookAt(TargetRefID, SL );
  1770. };
  1771. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1772. C3D_SPH_DEGREES SL;
  1773. #ifdef DEBUG_EXTRA
  1774. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  1775. #endif
  1776. spherical2spherical( L, SL );
  1777. return lookAt(TargetRefID, SL );
  1778. };
  1779. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1780. if ( m_bCameraIsStatic ) {
  1781. DBC_SNAPSHOT_DATA *tData = (DBC_SNAPSHOT_DATA*)malloc(sizeof(DBC_SNAPSHOT_DATA));
  1782. if ( tData ) {
  1783. memset( tData, 0, sizeof( DBC_SNAPSHOT_DATA ));
  1784. tData->stream_id = m_sNodeData.aux_parameter;
  1785. tData->assoc_event_ids.push_back( TargetRefID );
  1786. if (m_pBEH) {
  1787. EVENT_CONTAINER *tEVT = createBasicEvent(
  1788. m_sNodeData.node_id,
  1789. EVT_CLASS_CAMERA_CTRL,
  1790. EVT_CAMERA_ON_POSITION,
  1791. NULL, NULL, tData );
  1792. if( tEVT ){
  1793. #ifdef DEBUG_EXTRA
  1794. LOG("INSTANT BCAST CAM ONPOS");
  1795. #endif
  1796. m_pBEH->broadcast( tEVT );
  1797. } else {
  1798. free( tData );
  1799. };
  1800. } else {
  1801. free( tData );
  1802. };
  1803. };
  1804. return true;
  1805. };
  1806. //char tB[128];
  1807. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1808. //LOG( tB );
  1809. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1810. //LOG( tB );
  1811. #ifdef DEBUG_EXTRA
  1812. LOG("Look at local spherical deg. coords!(final)");
  1813. #endif
  1814. //if( getWM() == CAM_WS_READY ){
  1815. // C3D_ORIENT_ONVIF LL;
  1816. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1817. // return absoluteMove( TargetRefID, LL );
  1818. //};
  1819. // Camera is Busy
  1820. if( m_bShineOnTarget ){
  1821. m_mIRProtector.lock();
  1822. if ( !m_bIRIsOn ) {
  1823. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1824. if ( TK ) {
  1825. if( m_pProtocol->goPreset( TK )){
  1826. m_bIRIsOn = true;
  1827. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1828. };
  1829. };
  1830. } else m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1831. m_mIRProtector.unlock();
  1832. };
  1833. // targeting by Presets -> instant ROI
  1834. if ( m_bNoAbsPositionning ) {
  1835. CAM_ROI * tROI = createCAM_ROI();
  1836. if( tROI != NULL ){
  1837. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD));
  1838. if ( tTarget != NULL ) {
  1839. memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) );
  1840. tTarget->evt_db_ID = TargetRefID;
  1841. tROI->targets.push_back( tTarget );
  1842. tROI->ts_ms = getMilliseconds();
  1843. spherical2onvif( L, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1844. tROI->cam_position.fi = L.fi;
  1845. tROI->cam_position.tetta = L.tetta;
  1846. tROI->cam_position.r = L.r;
  1847. m_mROIsProtector.lock();
  1848. m_vROIS.push_back( tROI );
  1849. m_mROIsProtector.unlock();
  1850. } else {
  1851. LOG_ERROR("No MEM 4 TARGTEG DATA");
  1852. utilizeROI( tROI );
  1853. };
  1854. } else LOG_ERROR("No MEM 4 ROI");
  1855. return true;
  1856. };
  1857. return fusion_consumeData(TargetRefID, L);
  1858. };
  1859. bool deviceNodeCamera::makeSnap( uint32_t TargetRefID ){
  1860. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1861. if( tTask ){
  1862. memset(tTask, 0, sizeof(CAM_TASK));
  1863. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1864. if( tTask->CMD ){
  1865. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1866. tTask->movemet_needed = false;
  1867. tTask->source_evt_id = TargetRefID;
  1868. return pushCamTask( tTask );
  1869. } else {
  1870. free( tTask );
  1871. };
  1872. };
  1873. return false;
  1874. };
  1875. bool deviceNodeCamera::connectToStream( uint32_t id ){
  1876. /*
  1877. if( id ){
  1878. argusKernelModule * tKM = NULL;
  1879. argusKernel * tKRNL = NULL;
  1880. argusModuleFactory * tMF = NULL;
  1881. if( m_pDBC == NULL ) {
  1882. LOG_ERROR("No Database Connector!");
  1883. return false;
  1884. };
  1885. tKRNL = argusKernel::getInstance();
  1886. if( tKRNL ) {
  1887. tMF = tKRNL->getModuleFactory();
  1888. if( tMF ) {
  1889. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1890. } else {
  1891. LOG_ERROR("No ModuleFactory Instance!");
  1892. return false;
  1893. };
  1894. } else {
  1895. LOG_ERROR("No Kernel Instance!");
  1896. return false;
  1897. };
  1898. if( tKM ) {
  1899. // New Module
  1900. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1901. if( tDATA ) {
  1902. m_sMediaSource.mediaStreamId = id;
  1903. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1904. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  1905. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  1906. } else {
  1907. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  1908. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  1909. };
  1910. if( m_pSource != NULL ) {
  1911. char tMRL[1024];
  1912. #ifdef DEBUG
  1913. LOG("Source OK");
  1914. #endif
  1915. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  1916. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1917. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1918. int pix = -1;
  1919. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  1920. bool out_16_9 = (((tW*100) / tH ) > 140 );
  1921. updateOpticalParameters( tW, tH );
  1922. if( strlen( tDATA->c_type ) ){
  1923. };
  1924. //if( tW > 0 ){
  1925. // //m_pSource->setProportionalByWidth( tW );
  1926. // m_pSource->setSize( tW, tH );
  1927. //};
  1928. if( in_16_9 != out_16_9 ){
  1929. if( in_16_9 ){
  1930. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  1931. } else {
  1932. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  1933. };
  1934. };
  1935. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  1936. sprintf( tMRL, tDATA->mrl );
  1937. } else {
  1938. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1939. if( tCAM ) {
  1940. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1941. free( tCAM );
  1942. } else {
  1943. LOG_ERROR("Error Getting CAM Data!");
  1944. delete( m_pSource );
  1945. m_pSource = NULL;
  1946. return false;
  1947. };
  1948. };
  1949. LOG( tMRL );
  1950. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  1951. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1952. if ( !m_bSourceConnected ) {
  1953. //delete( m_pSource );
  1954. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1955. delete((videoAVLIBSource*)m_pSource);
  1956. } else {
  1957. delete((videoLIVE555Source*)m_pSource);
  1958. };
  1959. m_pSource = NULL;
  1960. } else {
  1961. m_nActiveStreamID = id;
  1962. #ifdef DEBUG
  1963. LOG("Source is Connected to Output.");
  1964. #endif
  1965. };
  1966. } else {
  1967. LOG_ERROR("Can not Open Video Source!");
  1968. //delete( m_pSource );
  1969. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1970. delete((videoAVLIBSource*)m_pSource);
  1971. } else {
  1972. delete((videoLIVE555Source*)m_pSource);
  1973. };
  1974. m_pSource = NULL;
  1975. };
  1976. } else {
  1977. LOG_ERROR("Video Source Stream Failure!");
  1978. };
  1979. free( tDATA );
  1980. };
  1981. }
  1982. #ifdef DEBUG
  1983. else LOG("No Video Module!")
  1984. #endif
  1985. ;
  1986. };
  1987. LOG("Connect Finished!");
  1988. return m_bSourceConnected;
  1989. */
  1990. return true;
  1991. };
  1992. bool deviceNodeCamera::disconnectFromStream( void ){
  1993. /*if( m_bSourceConnected ) {
  1994. if( m_pSource ){
  1995. delete( m_pSource );
  1996. m_pSource = NULL;
  1997. };
  1998. m_bSourceConnected = false;
  1999. };
  2000. return !m_bSourceConnected;
  2001. */
  2002. return true;
  2003. };
  2004. bool deviceNodeCamera::validateStream( void ){
  2005. /*
  2006. if( m_bSourceConnected ) {
  2007. if( m_pSource ) {
  2008. if( m_pSource->isAttached() ){
  2009. if( m_pSource->getStatus() < MS_STOPED ) return true;
  2010. #ifdef DEBUG
  2011. LOG("Source is Finished!");
  2012. #endif
  2013. };
  2014. #ifdef DEBUG
  2015. LOG("Source is Disconnected!");
  2016. #endif
  2017. } else LOG_ERROR("No Source Object! Set Disconnected!");
  2018. disconnectFromStream();
  2019. };
  2020. return false;
  2021. */
  2022. return true;
  2023. };
  2024. bool deviceNodeCamera::validate(){
  2025. if ( m_pDBC == NULL ) return false;
  2026. DBC_CAM_DATA *tDATA = m_pDBC->getCamData( m_sCamData.id );
  2027. if ( tDATA == NULL ) return false;
  2028. if ( memcmp( tDATA, &m_sCamData, sizeof(DBC_CAM_DATA) ) != 0 ) {
  2029. //Settings Changed
  2030. //memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
  2031. free( tDATA );
  2032. return false;
  2033. };
  2034. free( tDATA );
  2035. if (!validateStream()) return false;
  2036. return deviceNode::validate();
  2037. };
  2038. bool deviceNodeCamera::load_presets( void ){
  2039. bool bHaveIROFF = false;
  2040. bool bHaveIRON = false;
  2041. bool bHaveIRAUTO = false;
  2042. // Request PRESETS FROM CAMERA?! for some PTZ cameras without presets inpossible to
  2043. // operate by presets
  2044. if( m_pProtocol->getCameraPresets() ){
  2045. LOG("Camera Presets Loadded");
  2046. } else LOG_ERROR("Error while loadding Camera Presets!");
  2047. if ( m_pDBC == NULL ) return false;
  2048. DBC_PRESETS_INFO *tData = m_pDBC->getCamPresetInfo(m_sCamData.id);
  2049. if ( tData ) {
  2050. memcpy( &m_sCamPresets, tData, sizeof(DBC_PRESETS_INFO));
  2051. #ifdef DEBUG
  2052. char tB[128];
  2053. LOG("Loading Presets!");
  2054. sprintf( tB, "Cam Presets Info: CNT: %d Cols: %d Rows: %d ", m_sCamPresets.maxCount, m_sCamPresets.gCols, m_sCamPresets.gRows );
  2055. LOG( tB );
  2056. #endif
  2057. DBC_INT_LIST *tList = m_pDBC->listCamPresets( m_sCamData.id );
  2058. if ( tList ) {
  2059. for( DBC_INT_LIST::iterator idx= tList->begin(); idx < tList->end(); idx++ ) {
  2060. CAM_PRESET *tData = m_pDBC->getCamPreset( *idx );
  2061. if ( tData ) {
  2062. m_sCamPresets.presets.push_back(tData);
  2063. bHaveIRON = ( tData->role == ARGUS_PRESET_TYPE_IR_ON )?true:bHaveIRON;
  2064. bHaveIROFF = ( tData->role == ARGUS_PRESET_TYPE_IR_OFF )?true:bHaveIROFF;
  2065. bHaveIRAUTO = ( tData->role == ARGUS_PRESET_TYPE_IR_AUTO )?true:bHaveIRAUTO;
  2066. };
  2067. };
  2068. delete(tList);
  2069. };
  2070. if ( m_sCamData.eco_ir ) {
  2071. m_bShineOnTarget = (bHaveIROFF && bHaveIRAUTO);
  2072. } else m_bShineOnTarget = false;
  2073. return true;
  2074. #ifdef DEBUG
  2075. } else {
  2076. LOG("No Camera Presets!");
  2077. #endif
  2078. };
  2079. return false;
  2080. };
  2081. void deviceNodeCamera::translateROI2PRESET( float P, float T, float Z, uint8_t &col, uint8_t &row ){
  2082. char tB[128];
  2083. row = (uint8_t) ( Z * ((float)m_sCamPresets.gRows));
  2084. if ( P < 0.5 ){
  2085. P = P + 1.5f;
  2086. } else {
  2087. P = P - 0.5f;
  2088. };
  2089. col = (uint8_t) ( P * ((float)(m_sCamPresets.gCols * (1 << row)) / 2.0f ));
  2090. row++;
  2091. };
  2092. CSTR deviceNodeCamera::getPresetToken( uint8_t CCOL, uint8_t CROW ){
  2093. if( m_sCamPresets.presets.size() > 0 )
  2094. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  2095. if ( (*idx)->role == ARGUS_PRESET_TYPE_POSITION )
  2096. if ( ( (*idx)->row == CROW ) && ( (*idx)->col == CCOL ) ) return (CSTR)(*idx)->token;
  2097. return NULL;
  2098. };
  2099. CSTR deviceNodeCamera::getPresetToken( uint8_t specialRole ){
  2100. if( m_sCamPresets.presets.size() > 0 )
  2101. for ( V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++ )
  2102. if ( (*idx)->role == specialRole ) return (CSTR)(*idx)->token;
  2103. return NULL;
  2104. };
  2105. #undef LOG
  2106. #undef LOG_ERROR
  2107. //#undef DEBUG_EXTRA