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.

2213 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
12 months ago
2 years ago
12 months 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(); ) {
  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. idx++;
  675. };
  676. };
  677. if( tCNT ) {
  678. C3D_SPH_DEGREES L = { 0 };
  679. L.fi = (float) ( tA /(double) tCNT );
  680. L.tetta = (float) ( tB /(double) tCNT );
  681. L.r = (float) ( tC /(double) tCNT );
  682. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  683. ptr->ts_ms = cts;
  684. float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange );
  685. ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  686. ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  687. ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation
  688. ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  689. return true;
  690. };
  691. };
  692. return false;
  693. };
  694. CAM_ROI *deviceNodeCamera::fusion_fetchROI( void ){
  695. CAM_ROI *tVAL = NULL;
  696. m_mROIsProtector.lock();
  697. if( m_vROIS.size() ){
  698. vCAM_ROIS::iterator sRec;
  699. uint64_t cts = 0;
  700. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end(); idx++ ){
  701. if ( tVAL == NULL ) {
  702. sRec = idx;
  703. tVAL = (*idx);
  704. cts = tVAL->ts_ms;
  705. } else {
  706. #ifdef CAM_FETCH_ROI_NEWEST
  707. if ( (*idx)->ts_ms > cts )
  708. #else
  709. if ( (*idx)->ts_ms < cts )
  710. #endif
  711. {
  712. sRec = idx;
  713. tVAL = (*idx);
  714. cts = tVAL->ts_ms;
  715. };
  716. };
  717. };
  718. m_vROIS.erase( sRec );
  719. };
  720. m_mROIsProtector.unlock();
  721. if ( m_bNoAbsPositionning && tVAL ) {
  722. // Skip targets older 3 seconds
  723. if ( getMilliseconds() - tVAL->ts_ms > 3000 )
  724. #ifdef DEBUG_EXTRA
  725. {
  726. LOG("Targetd too old! ---> Skiping");
  727. utilizeROI( tVAL );
  728. };
  729. #else
  730. utilizeROI( tVAL );
  731. #endif
  732. };
  733. return tVAL;
  734. };
  735. DBC_SNAPSHOT_DATA *deviceNodeCamera::fusion_translate_ROI( CAM_ROI *(&ptr) ){
  736. DBC_SNAPSHOT_DATA *rVal = NULL;
  737. if( ptr == NULL ) return NULL;
  738. if( ptr->targets.size() ){
  739. rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA));
  740. if( rVal ){
  741. memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) );
  742. rVal->stream_id = m_sNodeData.aux_parameter;
  743. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ )
  744. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  745. };
  746. ptr->targets.clear();
  747. } else {
  748. LOG_ERROR("No Events ID to ROI");
  749. };
  750. utilizeROI( ptr );
  751. // Not Setting MD5 and TS -> will set at capture time
  752. return rVal;
  753. };
  754. void deviceNodeCamera::fusion_validate( void ){
  755. if ( m_bNoAbsPositionning ) return;
  756. m_mROIsProtector.lock();
  757. if( m_vROIS.size() ){
  758. #ifdef DEBUG_EXTRA
  759. char tB[128];
  760. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  761. LOG( tB );
  762. #endif
  763. uint64_t cts = getMilliseconds();
  764. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end(); ){
  765. if( !fusion_validateROI( cts, *idx ) ) {
  766. utilizeROI( (*idx) );
  767. m_vROIS.erase( idx );
  768. } else idx++;
  769. };
  770. };
  771. m_mROIsProtector.unlock();
  772. };
  773. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  774. if( m_sCamData.online ){
  775. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  776. if( tTask ){
  777. memset(tTask, 0, sizeof(CAM_TASK));
  778. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  779. if( tTask->CMD ){
  780. tTask->CMD->CMD = PTZ_ABSOLUTE;
  781. tTask->CMD->PAN = PanPOS;
  782. tTask->CMD->TILT = TiltPOS;
  783. tTask->CMD->ZOOM = ZoomPOS;
  784. tTask->movemet_needed = true;
  785. tTask->source_evt_id = TargetRefID;
  786. setWM( CAM_WS_BUSY );
  787. return pushCamTask( tTask );
  788. } else {
  789. LOG_ERROR("CMD malloc Error!");
  790. free( tTask );
  791. };
  792. } else LOG_ERROR("Task malloc error!");
  793. };
  794. return false;
  795. };
  796. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  797. if( m_sCamData.online ){
  798. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  799. if( tTask ){
  800. memset(tTask, 0, sizeof(CAM_TASK));
  801. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  802. if( tTask->CMD ){
  803. tTask->CMD->CMD = PTZ_ABSOLUTE;
  804. tTask->CMD->PAN = O.pan;
  805. tTask->CMD->TILT = O.tilt;
  806. tTask->CMD->ZOOM = O.zoom;
  807. tTask->movemet_needed = true;
  808. tTask->source_evt_id = TargetRefID;
  809. setWM( CAM_WS_BUSY );
  810. return pushCamTask( tTask );
  811. } else {
  812. LOG_ERROR("CMD malloc Error!");
  813. free( tTask );
  814. };
  815. } else LOG_ERROR("Task malloc error!");
  816. };
  817. return false;
  818. };
  819. uint8_t deviceNodeCamera::getPtzStatus(){
  820. if ( m_sCamData.protocol == 0 ) return 1;
  821. if ( getWM() > CAM_WS_INIT ) return 1;
  822. return 0;
  823. };
  824. uint8_t deviceNodeCamera::getStreamStatus(){
  825. return 1;
  826. };
  827. void deviceNodeCamera::m_fThreadProc( void ){
  828. DBC_PUSHED_CMD * PUSHED = NULL;
  829. m_bThreadStatus = TH_WORKING;
  830. bool bNewSecondLoop = false;
  831. uint32_t cTimeSeconds = 0;
  832. uint32_t oTimeSeconds = 0;
  833. while( m_bThreadStatus < TH_ENDED ) {
  834. cTimeSeconds = (uint32_t) time( NULL );
  835. if ( bNewSecondLoop = ( cTimeSeconds != oTimeSeconds ) ) oTimeSeconds = cTimeSeconds;
  836. uint8_t tState = getWM();
  837. char tB[128];
  838. fusion_validate();
  839. switch( tState ) {
  840. case CAM_WS_INIT:{
  841. if ( m_pProtocol != NULL ) {
  842. switch( m_sCamData.protocol ) {
  843. case CAM_CTRL_PROTO_VISCA:
  844. delete( (protocolPTZ_VISCA *) m_pProtocol );
  845. break;
  846. case CAM_CTRL_PROTO_PELCO:
  847. delete( (protocolPTZ_PELCO *) m_pProtocol );
  848. break;
  849. case CAM_CTRL_PROTO_ONVIF:
  850. delete( (protocolPTZ_ONVIF *) m_pProtocol );
  851. break;
  852. default:
  853. delete( m_pProtocol );
  854. };
  855. m_pProtocol = NULL;
  856. m_sCamData.online = false;
  857. };
  858. switch( m_sCamData.protocol ){
  859. case CAM_CTRL_PROTO_VISCA: {
  860. #ifdef DEBUG
  861. LOG("VISCA");
  862. #endif
  863. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux );
  864. if( m_pProtocol!= NULL ) {
  865. basicTransport *tTransport = NULL;
  866. hardwareManager *tHM = hardwareManager::getInstance();
  867. if( tHM ){
  868. hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  869. if( tRES != NULL ) {
  870. if( tRES->isValid()) {
  871. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  872. if( m_pProtocol->setTransport( tTransport )){
  873. if( !m_pProtocol->create() ){
  874. LOG_ERROR("Protocol <<< Create >>> Failure!");
  875. };
  876. } else {
  877. LOG_ERROR("Binding Port Failure!");
  878. };
  879. };
  880. #ifdef DEBUG
  881. } else {
  882. LOG("HWM Has No UART PORT!");
  883. #endif
  884. };
  885. #ifdef DEBUG
  886. } else {
  887. LOG_ERROR("Can not get Hardware Manager!");
  888. #endif
  889. };
  890. if( tTransport!= NULL ) {
  891. LOG( "Bind Transport Port!" );
  892. } else {
  893. LOG_ERROR("Transport Creation Failure!");
  894. delete( ( protocolPTZ_VISCA * )m_pProtocol );
  895. m_pProtocol = NULL;
  896. };
  897. #ifdef DEBUG
  898. } else {
  899. LOG_ERROR("Protocol Creation failure!");
  900. #endif
  901. };
  902. break;
  903. };
  904. case CAM_CTRL_PROTO_PELCO:{
  905. #ifdef DEBUG
  906. LOG("PELCO");
  907. #endif
  908. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux );
  909. if( m_pProtocol!= NULL ) {
  910. basicTransport *tTransport = NULL;
  911. hardwareManager *tHM = hardwareManager::getInstance();
  912. if ( tHM ) {
  913. hardwareResource * tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  914. if( tRES != NULL ) {
  915. if( tRES->isValid()) {
  916. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  917. if( m_pProtocol->setTransport( tTransport )){
  918. if( !m_pProtocol->create() ){
  919. LOG_ERROR("Protocol <<< Create >>> Failure!");
  920. };
  921. } else {
  922. LOG_ERROR("Binding Port Failure!");
  923. };
  924. };
  925. #ifdef DEBUG
  926. } else {
  927. LOG("HWM Has No UART PORT!");
  928. #endif
  929. };
  930. #ifdef DEBUG
  931. } else {
  932. LOG_ERROR("Can not get Hardware Manager!");
  933. #endif
  934. };
  935. if( tTransport!= NULL ) {
  936. LOG( "Bind Transport Port!" );
  937. } else {
  938. LOG_ERROR("Transport Creation Failure!");
  939. delete( ( protocolPTZ_PELCO * )m_pProtocol );
  940. m_pProtocol = NULL;
  941. };
  942. #ifdef DEBUG
  943. } else {
  944. LOG_ERROR("Protocol Creation failure!");
  945. #endif
  946. };
  947. break;
  948. };
  949. case CAM_CTRL_PROTO_ONVIF:{
  950. #ifdef DEBUG_EXTRA
  951. LOG("ONVIF");
  952. #endif
  953. //m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  954. if( strstr( m_sCamData.onvif_ctrl, "http" ) == NULL ) {
  955. char tURI[256];
  956. if ( m_sCamData.port ) {
  957. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  958. } else {
  959. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  960. };
  961. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF( tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  962. } else {
  963. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  964. };
  965. #ifdef DEBUG_EXTRA
  966. LOG("ONVIF-END");
  967. #endif
  968. if( m_pProtocol != NULL ) {
  969. if( !m_pProtocol->create() ) {
  970. LOG_ERROR("Error Creating Proto!");
  971. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  972. m_pProtocol = NULL;
  973. };
  974. #ifdef DEBUG_EXTRA
  975. LOG("PROTO CREATE DONE");
  976. #endif
  977. };
  978. break;
  979. };
  980. default:{
  981. #ifdef DEBUG
  982. LOG("Camera Has no defined control protocol");
  983. #endif
  984. };
  985. };
  986. if ( m_pProtocol != NULL ) {
  987. //Load Presets only if we have protocol
  988. load_presets();
  989. mojoKernel *tKRNL = mojoKernel::getInstance();
  990. if( tKRNL ) {
  991. m_bSkeepPositioningWait = (( tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO ) == EVE_OUTPUT_VIDEO );
  992. #ifdef DEBUG_EXTRA
  993. char ttTT[128];
  994. sprintf( ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode() );
  995. LOG( ttTT );
  996. if ( m_bSkeepPositioningWait ) {
  997. LOG("Camera No Wait On Pos!");
  998. } else {
  999. LOG("Camera Will Wait On Pos!");
  1000. };
  1001. #endif
  1002. } else {
  1003. LOG_ERROR("No Kernel Instance!");
  1004. };
  1005. m_sCamData.online = true;
  1006. setWM(CAM_WS_POWER_INIT);
  1007. };
  1008. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  1009. break;
  1010. };
  1011. case CAM_WS_POWER_INIT:{
  1012. #ifdef DEBUG
  1013. LOG("Init Camera - PowerMode");
  1014. #endif
  1015. //setBusy( true );
  1016. if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE );
  1017. //setBusy( false );
  1018. break;
  1019. };
  1020. case CAM_WS_WHITE_BALLANCE:{
  1021. #ifdef DEBUG
  1022. LOG("Set WB");
  1023. #endif
  1024. //setBusy( true );
  1025. if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT );
  1026. //setBusy( false );
  1027. break;
  1028. };
  1029. case CAM_WS_BACKLIGHT:{
  1030. #ifdef DEBUG
  1031. LOG("Set BLC");
  1032. #endif
  1033. //setBusy( true );
  1034. if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS );
  1035. //setBusy( false );
  1036. break;
  1037. };
  1038. case CAM_WS_AUTO_FOCUS:{
  1039. #ifdef DEBUG
  1040. LOG("Set AF");
  1041. #endif
  1042. //setBusy( true );
  1043. if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME );
  1044. //setBusy( false );
  1045. break;
  1046. };
  1047. case CAM_WS_INIT_GO_HOME:{
  1048. m_nHomingCounter = 0;
  1049. /* Skip For Restartin bug
  1050. if( !m_bNoAbsPositionning ) {
  1051. #ifdef DEBUG
  1052. LOG("INIT GO -1 0 -1");
  1053. #endif
  1054. if (m_pProtocol->goAbsolute(-1, 0, -1)) {
  1055. m_fCP_Pan = -1;
  1056. m_fCP_Tilt = 0;
  1057. m_fCP_Zoom = -1;
  1058. setWM( CAM_WS_READY );
  1059. };
  1060. } else {
  1061. #ifdef DEBUG
  1062. LOG("Go Home!");
  1063. #endif
  1064. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1065. if( TK ) {
  1066. if ( !m_bCameraOnHomePosition ) {
  1067. if( !m_pProtocol->goPreset( TK )){
  1068. LOG_ERROR("Cannot Issue goPreset Command!");
  1069. };
  1070. m_bCameraOnHomePosition = true;
  1071. };
  1072. };
  1073. setWM( CAM_WS_READY );
  1074. };*/
  1075. setWM( CAM_WS_READY );
  1076. break;
  1077. };
  1078. case CAM_WS_AUTOMATED_MOVE:{
  1079. m_bCameraOnHomePosition = false;
  1080. m_nRelativeMoveCounter++;
  1081. if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) {
  1082. //Pan Move
  1083. if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) {
  1084. m_fRM_Pan -= m_fSpeedRise;
  1085. } else {
  1086. m_fRM_Pan += m_fSpeedRise;
  1087. };
  1088. };
  1089. if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) {
  1090. //TILT Move
  1091. if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) {
  1092. m_fRM_Tilt -= m_fSpeedRise;
  1093. } else {
  1094. m_fRM_Tilt += m_fSpeedRise;
  1095. }
  1096. };
  1097. if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) {
  1098. //ZOOM Move
  1099. if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) {
  1100. //Wide
  1101. m_fRM_Zoom -= m_fSpeedRise;
  1102. } else {
  1103. //Tele
  1104. m_fRM_Zoom += m_fSpeedRise;
  1105. };
  1106. };
  1107. if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f;
  1108. if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f;
  1109. if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f;
  1110. if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f;
  1111. if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f;
  1112. if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f;
  1113. m_fCP_Zoom += 0.5f*m_fRM_Zoom;
  1114. if( m_fCP_Zoom < -1.0f ) m_fCP_Zoom = -1.0f;
  1115. if( m_fCP_Zoom > 1.0f ) m_fCP_Zoom = 1.0f;
  1116. if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){
  1117. m_nRelativeMoveCounter = 0;
  1118. setWM( CAM_WS_READY );
  1119. break;
  1120. };
  1121. if( m_sCamData.ptz_axel ) { //Manual
  1122. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) {
  1123. m_nRelativeMoveCounter = 0;
  1124. setWM( CAM_WS_READY );
  1125. };
  1126. } else {
  1127. // Stepping Axel by Zoom
  1128. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  1129. if( m_nRelativeMoveCounter > tVal ) {
  1130. m_nRelativeMoveCounter = 0;
  1131. setWM( CAM_WS_READY );
  1132. };
  1133. };
  1134. break;
  1135. };
  1136. case CAM_WS_STOP_MOVE:{
  1137. m_nRelativeMoveCounter++;
  1138. m_fRM_Pan = .0f;
  1139. m_fRM_Pan = .0f;
  1140. m_fRM_Tilt = .0f;
  1141. m_fRM_Tilt = .0f;
  1142. m_fRM_Zoom = .0f;
  1143. m_fRM_Zoom = .0f;
  1144. if( relativeStop() ) {
  1145. m_nHomingCounter = 0;
  1146. m_nRelativeMoveCounter = 0;
  1147. setWM( CAM_WS_READY );
  1148. break;
  1149. };
  1150. if( m_sCamData.ptz_axel ) {
  1151. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) setWM(CAM_WS_READY);
  1152. } else {
  1153. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  1154. if (m_nRelativeMoveCounter > tVal) setWM(CAM_WS_READY);
  1155. };
  1156. break;
  1157. };
  1158. case CAM_WS_GO_ABS_ROI:
  1159. case CAM_WS_GO_ABS_POSITION:{
  1160. m_bCameraOnHomePosition = false;
  1161. setWM( CAM_WS_BUSY );
  1162. if( m_pCurrentROI != NULL ){
  1163. //LOG("ROI Ok");
  1164. m_nHomingCounter = 0;
  1165. if( m_pBEH ){
  1166. EVENT_CONTAINER *tEVT = createBasicEvent(
  1167. m_sNodeData.node_id,
  1168. EVT_CLASS_CAMERA_CTRL,
  1169. EVT_CAMERA_MOVE_TO,
  1170. NULL, NULL, NULL );
  1171. if( tEVT ) m_pBEH->broadcast( tEVT );
  1172. };
  1173. if( m_bNoAbsPositionning ){
  1174. //LOG("ABS to PRESET");
  1175. uint8_t COL, ROW;
  1176. translateROI2PRESET(
  1177. m_pCurrentROI->cam_orientation.pan,
  1178. m_pCurrentROI->cam_orientation.tilt,
  1179. m_pCurrentROI->cam_orientation.zoom, COL, ROW );
  1180. //char tB[64];
  1181. //sprintf( tB, "Get ABS Preset Col %d Row %d!", COL, ROW );
  1182. //LOG( tB );
  1183. //sprintf( tB, "Old Preset Col %d Row %d!", m_bCurrentPresetCol, m_bCurrentPresetRow );
  1184. //LOG( tB );
  1185. if ( ( m_bCurrentPresetCol == COL ) && ( m_bCurrentPresetRow == ROW ) ) {
  1186. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1187. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1188. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1189. #ifdef DEBUG_EXTRA
  1190. LOG("INSTANT BCAST CAM ONPOS");
  1191. #endif
  1192. // Send Capture event
  1193. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1194. if( tData != NULL ) {
  1195. if( m_pBEH ){
  1196. EVENT_CONTAINER *tEVT = createBasicEvent(
  1197. m_sNodeData.node_id,
  1198. EVT_CLASS_CAMERA_CTRL,
  1199. EVT_CAMERA_ON_POSITION,
  1200. NULL, NULL, tData );
  1201. if( tEVT ){
  1202. m_pBEH->broadcast( tEVT );
  1203. } else {
  1204. free( tData );
  1205. };
  1206. } else {
  1207. free( tData );
  1208. };
  1209. };
  1210. setWM( CAM_WS_READY );
  1211. } else {
  1212. CSTR token = getPresetToken( COL, ROW );
  1213. if ( token ) {
  1214. //update position Info
  1215. m_bCurrentPresetCol = COL;
  1216. m_bCurrentPresetRow = ROW;
  1217. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1218. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1219. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1220. if( !m_pProtocol->goPreset( token ) ){
  1221. LOG_ERROR("Cannot Issue goPreset Command!->Restart Camera?");
  1222. setWM( CAM_WS_END );
  1223. } else {
  1224. // Waiting Movement finish
  1225. if( m_bSkeepPositioningWait && 0 ){
  1226. #ifdef DEBUG_EXTRA
  1227. LOG("INSTANT BCAST CAM ONPOS");
  1228. #endif
  1229. // Send Capture event
  1230. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1231. if( tData != NULL ) {
  1232. if( m_pBEH ){
  1233. EVENT_CONTAINER *tEVT = createBasicEvent(
  1234. m_sNodeData.node_id,
  1235. EVT_CLASS_CAMERA_CTRL,
  1236. EVT_CAMERA_ON_POSITION,
  1237. NULL, NULL, tData );
  1238. if( tEVT ){
  1239. m_pBEH->broadcast( tEVT );
  1240. } else {
  1241. free( tData );
  1242. };
  1243. } else {
  1244. free( tData );
  1245. };
  1246. };
  1247. setWM( CAM_WS_READY );
  1248. } else {
  1249. //utilizeROI( m_pCurrentROI );
  1250. setWM( CAM_WS_WAIT_ONPOS );
  1251. };
  1252. };
  1253. } else {
  1254. #ifdef DEBUG_EXTRA
  1255. LOG("Not Found!");
  1256. #endif
  1257. utilizeROI( m_pCurrentROI );
  1258. setWM( CAM_WS_READY );
  1259. };
  1260. };
  1261. } else {
  1262. //update position Info
  1263. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1264. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1265. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1266. // Do Movement
  1267. if( !m_pProtocol->goAbsolute(
  1268. m_pCurrentROI->cam_orientation.pan,
  1269. m_pCurrentROI->cam_orientation.tilt,
  1270. ( m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection )
  1271. )){
  1272. LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?");
  1273. setWM( CAM_WS_END );
  1274. } else {
  1275. // Waiting Movement finish
  1276. if( m_bSkeepPositioningWait ){
  1277. #ifdef DEBUG_EXTRA
  1278. LOG("INSTANT BCAST CAM ONPOS");
  1279. #endif
  1280. // Send Capture event
  1281. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1282. if( tData != NULL ) {
  1283. if( m_pBEH ){
  1284. EVENT_CONTAINER *tEVT = createBasicEvent(
  1285. m_sNodeData.node_id,
  1286. EVT_CLASS_CAMERA_CTRL,
  1287. EVT_CAMERA_ON_POSITION,
  1288. NULL, NULL, tData );
  1289. if( tEVT ){
  1290. m_pBEH->broadcast( tEVT );
  1291. } else {
  1292. free( tData );
  1293. };
  1294. } else {
  1295. free( tData );
  1296. };
  1297. };
  1298. setWM( CAM_WS_READY );
  1299. } else {
  1300. setWM(CAM_WS_WAIT_ONPOS);
  1301. };
  1302. };
  1303. };
  1304. } else setWM( CAM_WS_READY );
  1305. break;
  1306. };
  1307. case CAM_WS_WAIT_ONPOS:{
  1308. //LOG("WaitOnPos...");
  1309. DBC_SNAPSHOT_DATA *tData = NULL;
  1310. if( m_pCurrentROI != NULL ){
  1311. // Send Capture event
  1312. tData = fusion_translate_ROI( m_pCurrentROI );
  1313. };
  1314. // To Accept any new ROI's ??
  1315. // setWM( CAM_WS_READY );
  1316. #ifdef DEBUG_EXTRA
  1317. LOG("Start Waiting Cam On Position!");
  1318. #endif
  1319. if ( !m_bSkeepPositioningWait ) {
  1320. if (!m_pProtocol->waitMoveIsDone()) m_bSkeepPositioningWait = true;
  1321. };
  1322. if ( m_bSkeepPositioningWait ) SLEEP( 3000 );
  1323. #ifdef DEBUG_EXTRA
  1324. LOG("Done Waiting Cam On Position!");
  1325. #endif
  1326. if( tData != NULL ) {
  1327. if( m_pBEH ){
  1328. EVENT_CONTAINER *tEVT = createBasicEvent(
  1329. m_sNodeData.node_id,
  1330. EVT_CLASS_CAMERA_CTRL,
  1331. EVT_CAMERA_ON_POSITION,
  1332. NULL, NULL, tData );
  1333. if( tEVT ){
  1334. #ifdef DEBUG_EXTRA
  1335. LOG("BCAST CAM ONPOS");
  1336. #endif
  1337. m_pBEH->broadcast( tEVT );
  1338. } else {
  1339. free( tData );
  1340. };
  1341. } else {
  1342. free( tData );
  1343. };
  1344. tData = NULL;
  1345. };
  1346. setWM( CAM_WS_READY );
  1347. break;
  1348. };
  1349. case CAM_WS_GO_HOMING_PRESET:{
  1350. if ( !m_bCameraOnHomePosition ) {
  1351. // For new Camera Presets
  1352. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1353. if( TK ) {
  1354. #ifdef DEBUG_EXTRA
  1355. LOG("Go Home!");
  1356. LOG( TK );
  1357. #endif
  1358. if( !m_pProtocol->goPreset( TK )){
  1359. LOG_ERROR("Cannot Issue goPreset Command!");
  1360. };
  1361. } else {
  1362. //Old Method
  1363. if( m_sCamData.home_token[0] != (char)0 ) {
  1364. #ifdef DEBUG_EXTRA
  1365. LOG("Go Home!");
  1366. LOG( m_sCamData.home_token );
  1367. #endif
  1368. if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1369. LOG_ERROR("Cannot Issue goPreset Command!");
  1370. };
  1371. };
  1372. };
  1373. m_bCameraOnHomePosition = true;
  1374. m_bCurrentPresetCol = 0;
  1375. m_bCurrentPresetRow = 0;
  1376. };
  1377. setWM( CAM_WS_READY );
  1378. break;
  1379. };
  1380. case CAM_WS_GO_PRESET:{
  1381. LOG("Go Preset!");
  1382. //if( m_sCamData.home_token[0] != (char)0 ) {
  1383. // LOG("Go Home!");
  1384. // LOG( m_sCamData.home_token );
  1385. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1386. // LOG_ERROR("Cannot Issue goPreset Command!");
  1387. // };
  1388. //};
  1389. setWM( CAM_WS_READY );
  1390. break;
  1391. };
  1392. case CAM_WS_SET_PRESET:{
  1393. LOG("Set Preset!");
  1394. //if( m_sCamData.home_token[0] != (char)0 ) {
  1395. // LOG("Go Home!");
  1396. // LOG( m_sCamData.home_token );
  1397. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1398. // LOG_ERROR("Cannot Issue goPreset Command!");
  1399. // };
  1400. //};
  1401. setWM( CAM_WS_READY );
  1402. break;
  1403. };
  1404. case CAM_WS_BUSY:{
  1405. //Handle Operation TO
  1406. setWM( CAM_WS_READY );
  1407. break;
  1408. };
  1409. case CAM_WS_READY:{
  1410. m_pCurrentROI = fusion_fetchROI();
  1411. if( m_pCurrentROI != NULL ) {
  1412. #ifdef DEBUG_EXTRA
  1413. LOG("HAVE ROI to see!");
  1414. #endif
  1415. setWM( CAM_WS_GO_ABS_POSITION );
  1416. } else {
  1417. //Idle Work --->
  1418. if ( m_sCamData.eco_ir ) {
  1419. m_mIRProtector.lock();
  1420. if( m_bIRIsOn ){
  1421. if ( m_bIRTO ) {
  1422. if ( bNewSecondLoop ) m_bIRTO--;
  1423. } else {
  1424. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_IR_OFF );
  1425. if ( TK ) if( m_pProtocol->goPreset( TK ) ) m_bIRIsOn = false;
  1426. };
  1427. };
  1428. m_mIRProtector.unlock();
  1429. };
  1430. if (m_sCamData.homing_enable) {
  1431. if ( bNewSecondLoop ) {
  1432. if ( m_nHomingCounter < m_sCamData.homing_to ) {
  1433. m_nHomingCounter++;
  1434. } else {
  1435. //LOG("Go Home Position!");
  1436. m_nHomingCounter = 0;
  1437. setWM( CAM_WS_GO_HOMING_PRESET );
  1438. break;
  1439. };
  1440. };
  1441. };
  1442. CAM_TASK * tTASK = fetchCamTask();
  1443. if ( tTASK != NULL ) {
  1444. if( tTASK->movemet_needed ){
  1445. CAM_PTZ_CMD *tCMD = tTASK->CMD;
  1446. if( tCMD ) {
  1447. switch( tCMD->CMD ) {
  1448. case PTZ_STOP:{
  1449. m_nHomingCounter = 0;
  1450. setWM( CAM_WS_STOP_MOVE );
  1451. break;
  1452. };
  1453. case PTZ_MOVE:{
  1454. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1455. m_bRelativeMoveMatrix = 0;
  1456. m_fRM_Pan = tCMD->PAN;
  1457. m_fRM_Tilt = tCMD->TILT;
  1458. m_fRM_Zoom = tCMD->ZOOM;
  1459. m_nHomingCounter = 0;
  1460. setWM( CAM_WS_AUTOMATED_MOVE );
  1461. break;
  1462. };
  1463. case PTZ_ABSOLUTE:{
  1464. m_pCurrentROI = createCAM_ROI();
  1465. if( m_pCurrentROI != NULL ){
  1466. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD));
  1467. if ( tTarget != NULL ) {
  1468. memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) );
  1469. tTarget->evt_db_ID = tTASK->source_evt_id;
  1470. m_pCurrentROI->targets.push_back( tTarget );
  1471. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  1472. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  1473. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  1474. m_nHomingCounter = 0;
  1475. setWM(CAM_WS_GO_ABS_POSITION);
  1476. } else {
  1477. utilizeROI( m_pCurrentROI );
  1478. };
  1479. };
  1480. break;
  1481. };
  1482. case PTZ_RELOAD_CAMERA:{
  1483. LOG("Exit Command Rcpt");
  1484. m_bThreadStatus = TH_ENDED;
  1485. };
  1486. };
  1487. free( tCMD );
  1488. tTASK->CMD = NULL;
  1489. };
  1490. } else {
  1491. LOG("No Movement -> INSTCAP" );
  1492. // No Movement -> Just Capture
  1493. if( m_pBEH ){
  1494. EVENT_CONTAINER *tEVT = createBasicEvent(
  1495. m_sNodeData.node_id,
  1496. EVT_CLASS_CAMERA_CTRL,
  1497. EVT_CAMERA_ON_POSITION,
  1498. NULL, NULL, NULL );
  1499. if( tEVT ){
  1500. tEVT->m_db_id = tTASK->source_evt_id;
  1501. m_pBEH->broadcast(tEVT);
  1502. };
  1503. };
  1504. };
  1505. free( tTASK );
  1506. } else {
  1507. // Lowest Priority to Web Client Commands
  1508. #ifdef DEBUG_EXTRA__
  1509. LOG("Check Web CMD!");
  1510. #endif
  1511. PUSHED = m_pDBC->getNodePushedCMD( m_sNodeData.node_id );
  1512. if( PUSHED != NULL ) {
  1513. #ifdef DEBUG_EXTRA__
  1514. LOG("HAVE Web CMD!");
  1515. #endif
  1516. m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN );
  1517. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  1518. switch( PUSHED->CMD ) {
  1519. case 0x80:{
  1520. //Call Home Preset
  1521. LOG("CALL HOME PRESET!");
  1522. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1523. if ( TK ) {
  1524. LOG(TK);
  1525. if( m_pProtocol->goPreset( TK )){
  1526. m_bCameraOnHomePosition = true;
  1527. m_nHomingCounter = 0;
  1528. };
  1529. };
  1530. break;
  1531. };
  1532. case 0x81:{
  1533. //Call IR On Preset
  1534. LOG("CALL IR ON PRESET!");
  1535. m_mIRProtector.lock();
  1536. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_ON);
  1537. if ( TK ) {
  1538. LOG(TK);
  1539. if( m_pProtocol->goPreset( TK )){
  1540. m_bIRIsOn = true;
  1541. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1542. };
  1543. } else LOG("PRESET NOT FOUND!");
  1544. m_mIRProtector.unlock();
  1545. break;
  1546. };
  1547. case 0x82:{
  1548. //Call IR Off Preset
  1549. LOG("CALL IR OFF PRESET!");
  1550. m_mIRProtector.lock();
  1551. CSTR TK = ( m_sCamData.eco_ir )?getPresetToken(ARGUS_PRESET_TYPE_IR_OFF):getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1552. if ( TK ) {
  1553. LOG(TK);
  1554. if( m_pProtocol->goPreset( TK )){
  1555. m_bIRIsOn = false;
  1556. m_bIRTO = 0;
  1557. };
  1558. } else LOG("PRESET NOT FOUND!");
  1559. m_mIRProtector.unlock();
  1560. break;
  1561. };
  1562. case 0x83:{
  1563. //Call IR Auto Preset
  1564. LOG("CALL IR AUTO PRESET!");
  1565. m_mIRProtector.lock();
  1566. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1567. if ( TK ) {
  1568. LOG(TK);
  1569. if( m_pProtocol->goPreset( TK )){
  1570. m_bIRIsOn = true;
  1571. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1572. };
  1573. } else LOG("PRESET NOT FOUND!");
  1574. m_mIRProtector.unlock();
  1575. break;
  1576. };
  1577. case 0x84:{
  1578. //Call Wisher Preset
  1579. LOG("CALL WIPER PRESET!");
  1580. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_WISHER);
  1581. if ( TK ) {
  1582. LOG(TK);
  1583. m_pProtocol->goPreset( TK );
  1584. };
  1585. break;
  1586. };
  1587. };
  1588. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){ //No Way to get here
  1589. //Direct Movment Mode
  1590. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  1591. m_nHomingCounter = 0;
  1592. setWM( CAM_WS_STOP_MOVE );
  1593. } else {
  1594. char tB[64];
  1595. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1596. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  1597. m_nHomingCounter = 0;
  1598. setWM( CAM_WS_AUTOMATED_MOVE );
  1599. };
  1600. };
  1601. free( PUSHED );
  1602. PUSHED = NULL;
  1603. };
  1604. };
  1605. };
  1606. break;
  1607. };
  1608. case CAM_WS_END:{
  1609. LOG("Exit Command Rcpt");
  1610. m_bThreadStatus = TH_ENDED;
  1611. break;
  1612. };
  1613. };
  1614. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  1615. };
  1616. #ifdef DEBUG
  1617. LOG("CTRL Loop End!");
  1618. #endif
  1619. };
  1620. bool deviceNodeCamera::setNoPositionWait( bool FL ){
  1621. m_bSkeepPositioningWait = FL;
  1622. return true;
  1623. };
  1624. bool deviceNodeCamera::testFOV( C3D_DECART L ){
  1625. //System Coords -> trans to local and go to funct
  1626. systemAnchor* tSystem = systemAnchor::getInstance();
  1627. if ( tSystem == NULL ) return false;
  1628. C3D_SPH_DEGREES SL;
  1629. #ifdef DEBUG_EXTRA
  1630. LOG("Test FOV local Decart coords");
  1631. #endif
  1632. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1633. return testFOV( SL );
  1634. };
  1635. bool deviceNodeCamera::testFOV( C3D_WGS84_LOC L ){
  1636. //Global Coords
  1637. systemAnchor* tSystem = systemAnchor::getInstance();
  1638. if ( tSystem == NULL ) return false;
  1639. C3D_SPH_DEGREES SL;
  1640. #ifdef DEBUG_EXTRA
  1641. LOG("Test FOV WGS coords");
  1642. #endif
  1643. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1644. return testFOV( SL );
  1645. };
  1646. bool deviceNodeCamera::testFOV( C3D_SPH_RADIANS L ){
  1647. //Local Coords
  1648. C3D_SPH_DEGREES SL;
  1649. #ifdef DEBUG_EXTRA
  1650. LOG("Test FOV local Spherical(rad) coords");
  1651. #endif
  1652. spherical2spherical( L, SL );
  1653. return testFOV( SL );
  1654. };
  1655. bool deviceNodeCamera::testFOV( C3D_SPH_DEGREES SL ){
  1656. #ifdef DEBUG
  1657. char tB[128];
  1658. #endif
  1659. #ifdef DEBUG_EXTRA
  1660. LOG("Test FOV local Spherical(deg) coords");
  1661. #endif
  1662. switch ( m_sCamData.triggering_mode ) {
  1663. case CAM_TRIGGER_STRICT:{
  1664. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1665. #ifdef DEBUG_EXTRA
  1666. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1667. LOG( tB );
  1668. #endif
  1669. return false;
  1670. };
  1671. break;
  1672. };
  1673. case CAM_TRIGGER_ALL_NEAR:{
  1674. if( SL.r > m_fMaxRange ){
  1675. #ifdef DEBUG_EXTRA
  1676. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1677. LOG( tB );
  1678. #endif
  1679. return false;
  1680. };
  1681. break;
  1682. };
  1683. case CAM_TRIGGER_ALL_FAR:{
  1684. if( SL.r < m_fMinRange ){
  1685. #ifdef DEBUG_EXTRA
  1686. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1687. LOG( tB );
  1688. #endif
  1689. return false;
  1690. };
  1691. break;
  1692. };
  1693. case CAM_TRIGGER_ALL: return true;
  1694. };
  1695. float HS = AC_imager_width[m_sCamData.imager_type];
  1696. float VS = AC_imager_height[m_sCamData.imager_type];
  1697. float HS2 = HS * HS;
  1698. float VS2 = VS * VS;
  1699. float IR = HS / VS;
  1700. float IR2 = IR * IR;
  1701. if( m_sCamData.pan_angles == 0 ) {
  1702. // Fixed Camera
  1703. float tA = fabsf( SL.fi );
  1704. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1705. if ( tA > tZ ) return false;
  1706. } else {
  1707. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1708. // Partial Pan
  1709. switch( m_sCamData.pan_model ) {
  1710. case 1: {
  1711. // Regular Pan
  1712. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1713. float tRad = deg2rad( SL.fi );
  1714. #ifdef DEBUG_EXTRA
  1715. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1716. LOG( tB );
  1717. #endif
  1718. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1719. break;
  1720. };
  1721. case 2:{
  1722. break;
  1723. };
  1724. case 3:{
  1725. break;
  1726. };
  1727. };
  1728. };
  1729. };
  1730. if( m_sCamData.tilt_angles == 0 ){
  1731. //Fixed Camera
  1732. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1733. } else {
  1734. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1735. // Partial Tilt
  1736. switch( m_sCamData.tilt_model ) {
  1737. case 1: {
  1738. // Regular Tilt
  1739. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1740. float tRad = deg2rad( SL.tetta );
  1741. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1742. break;
  1743. };
  1744. case 2:{
  1745. break;
  1746. };
  1747. };
  1748. };
  1749. };
  1750. return true;
  1751. };
  1752. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1753. systemAnchor* tSystem = systemAnchor::getInstance();
  1754. if( tSystem == NULL ) return false;
  1755. C3D_SPH_DEGREES SL;
  1756. #ifdef DEBUG_EXTRA
  1757. LOG("Look at WGS coords!");
  1758. #endif
  1759. tSystem->convertToLocal(&m_oPosition, L, SL );
  1760. return lookAt(TargetRefID, SL );
  1761. };
  1762. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1763. systemAnchor* tSystem = systemAnchor::getInstance();
  1764. if( tSystem == NULL ) return false;
  1765. C3D_SPH_DEGREES SL;
  1766. #ifdef DEBUG_EXTRA
  1767. LOG("Look at System Decart coords!");
  1768. #endif
  1769. tSystem->convertToLocal( &m_oPosition, L, SL );
  1770. return lookAt(TargetRefID, SL );
  1771. };
  1772. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1773. C3D_SPH_DEGREES SL;
  1774. #ifdef DEBUG_EXTRA
  1775. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  1776. #endif
  1777. spherical2spherical( L, SL );
  1778. return lookAt(TargetRefID, SL );
  1779. };
  1780. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1781. if ( m_bCameraIsStatic ) {
  1782. DBC_SNAPSHOT_DATA *tData = (DBC_SNAPSHOT_DATA*)malloc(sizeof(DBC_SNAPSHOT_DATA));
  1783. if ( tData ) {
  1784. memset( tData, 0, sizeof( DBC_SNAPSHOT_DATA ));
  1785. tData->stream_id = m_sNodeData.aux_parameter;
  1786. tData->assoc_event_ids.push_back( TargetRefID );
  1787. if (m_pBEH) {
  1788. EVENT_CONTAINER *tEVT = createBasicEvent(
  1789. m_sNodeData.node_id,
  1790. EVT_CLASS_CAMERA_CTRL,
  1791. EVT_CAMERA_ON_POSITION,
  1792. NULL, NULL, tData );
  1793. if( tEVT ){
  1794. #ifdef DEBUG_EXTRA
  1795. LOG("INSTANT BCAST CAM ONPOS");
  1796. #endif
  1797. m_pBEH->broadcast( tEVT );
  1798. } else {
  1799. free( tData );
  1800. };
  1801. } else {
  1802. free( tData );
  1803. };
  1804. };
  1805. return true;
  1806. };
  1807. //char tB[128];
  1808. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1809. //LOG( tB );
  1810. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1811. //LOG( tB );
  1812. #ifdef DEBUG_EXTRA
  1813. LOG("Look at local spherical deg. coords!(final)");
  1814. #endif
  1815. //if( getWM() == CAM_WS_READY ){
  1816. // C3D_ORIENT_ONVIF LL;
  1817. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1818. // return absoluteMove( TargetRefID, LL );
  1819. //};
  1820. // Camera is Busy
  1821. if( m_bShineOnTarget ){
  1822. m_mIRProtector.lock();
  1823. if ( !m_bIRIsOn ) {
  1824. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1825. if ( TK ) {
  1826. if( m_pProtocol->goPreset( TK )){
  1827. m_bIRIsOn = true;
  1828. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1829. };
  1830. };
  1831. } else m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1832. m_mIRProtector.unlock();
  1833. };
  1834. // targeting by Presets -> instant ROI
  1835. if ( m_bNoAbsPositionning ) {
  1836. CAM_ROI * tROI = createCAM_ROI();
  1837. if( tROI != NULL ){
  1838. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD));
  1839. if ( tTarget != NULL ) {
  1840. memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) );
  1841. tTarget->evt_db_ID = TargetRefID;
  1842. tROI->targets.push_back( tTarget );
  1843. tROI->ts_ms = getMilliseconds();
  1844. spherical2onvif( L, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1845. tROI->cam_position.fi = L.fi;
  1846. tROI->cam_position.tetta = L.tetta;
  1847. tROI->cam_position.r = L.r;
  1848. m_mROIsProtector.lock();
  1849. m_vROIS.push_back( tROI );
  1850. m_mROIsProtector.unlock();
  1851. } else {
  1852. LOG_ERROR("No MEM 4 TARGTEG DATA");
  1853. utilizeROI( tROI );
  1854. };
  1855. } else LOG_ERROR("No MEM 4 ROI");
  1856. return true;
  1857. };
  1858. return fusion_consumeData(TargetRefID, L);
  1859. };
  1860. bool deviceNodeCamera::makeSnap( uint32_t TargetRefID ){
  1861. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1862. if( tTask ){
  1863. memset(tTask, 0, sizeof(CAM_TASK));
  1864. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1865. if( tTask->CMD ){
  1866. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1867. tTask->movemet_needed = false;
  1868. tTask->source_evt_id = TargetRefID;
  1869. return pushCamTask( tTask );
  1870. } else {
  1871. free( tTask );
  1872. };
  1873. };
  1874. return false;
  1875. };
  1876. bool deviceNodeCamera::connectToStream( uint32_t id ){
  1877. /*
  1878. if( id ){
  1879. argusKernelModule * tKM = NULL;
  1880. argusKernel * tKRNL = NULL;
  1881. argusModuleFactory * tMF = NULL;
  1882. if( m_pDBC == NULL ) {
  1883. LOG_ERROR("No Database Connector!");
  1884. return false;
  1885. };
  1886. tKRNL = argusKernel::getInstance();
  1887. if( tKRNL ) {
  1888. tMF = tKRNL->getModuleFactory();
  1889. if( tMF ) {
  1890. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1891. } else {
  1892. LOG_ERROR("No ModuleFactory Instance!");
  1893. return false;
  1894. };
  1895. } else {
  1896. LOG_ERROR("No Kernel Instance!");
  1897. return false;
  1898. };
  1899. if( tKM ) {
  1900. // New Module
  1901. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1902. if( tDATA ) {
  1903. m_sMediaSource.mediaStreamId = id;
  1904. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1905. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  1906. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  1907. } else {
  1908. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  1909. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  1910. };
  1911. if( m_pSource != NULL ) {
  1912. char tMRL[1024];
  1913. #ifdef DEBUG
  1914. LOG("Source OK");
  1915. #endif
  1916. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  1917. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1918. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1919. int pix = -1;
  1920. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  1921. bool out_16_9 = (((tW*100) / tH ) > 140 );
  1922. updateOpticalParameters( tW, tH );
  1923. if( strlen( tDATA->c_type ) ){
  1924. };
  1925. //if( tW > 0 ){
  1926. // //m_pSource->setProportionalByWidth( tW );
  1927. // m_pSource->setSize( tW, tH );
  1928. //};
  1929. if( in_16_9 != out_16_9 ){
  1930. if( in_16_9 ){
  1931. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  1932. } else {
  1933. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  1934. };
  1935. };
  1936. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  1937. sprintf( tMRL, tDATA->mrl );
  1938. } else {
  1939. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1940. if( tCAM ) {
  1941. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1942. free( tCAM );
  1943. } else {
  1944. LOG_ERROR("Error Getting CAM Data!");
  1945. delete( m_pSource );
  1946. m_pSource = NULL;
  1947. return false;
  1948. };
  1949. };
  1950. LOG( tMRL );
  1951. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  1952. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1953. if ( !m_bSourceConnected ) {
  1954. //delete( m_pSource );
  1955. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1956. delete((videoAVLIBSource*)m_pSource);
  1957. } else {
  1958. delete((videoLIVE555Source*)m_pSource);
  1959. };
  1960. m_pSource = NULL;
  1961. } else {
  1962. m_nActiveStreamID = id;
  1963. #ifdef DEBUG
  1964. LOG("Source is Connected to Output.");
  1965. #endif
  1966. };
  1967. } else {
  1968. LOG_ERROR("Can not Open Video Source!");
  1969. //delete( m_pSource );
  1970. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1971. delete((videoAVLIBSource*)m_pSource);
  1972. } else {
  1973. delete((videoLIVE555Source*)m_pSource);
  1974. };
  1975. m_pSource = NULL;
  1976. };
  1977. } else {
  1978. LOG_ERROR("Video Source Stream Failure!");
  1979. };
  1980. free( tDATA );
  1981. };
  1982. }
  1983. #ifdef DEBUG
  1984. else LOG("No Video Module!")
  1985. #endif
  1986. ;
  1987. };
  1988. LOG("Connect Finished!");
  1989. return m_bSourceConnected;
  1990. */
  1991. return true;
  1992. };
  1993. bool deviceNodeCamera::disconnectFromStream( void ){
  1994. /*if( m_bSourceConnected ) {
  1995. if( m_pSource ){
  1996. delete( m_pSource );
  1997. m_pSource = NULL;
  1998. };
  1999. m_bSourceConnected = false;
  2000. };
  2001. return !m_bSourceConnected;
  2002. */
  2003. return true;
  2004. };
  2005. bool deviceNodeCamera::validateStream( void ){
  2006. /*
  2007. if( m_bSourceConnected ) {
  2008. if( m_pSource ) {
  2009. if( m_pSource->isAttached() ){
  2010. if( m_pSource->getStatus() < MS_STOPED ) return true;
  2011. #ifdef DEBUG
  2012. LOG("Source is Finished!");
  2013. #endif
  2014. };
  2015. #ifdef DEBUG
  2016. LOG("Source is Disconnected!");
  2017. #endif
  2018. } else LOG_ERROR("No Source Object! Set Disconnected!");
  2019. disconnectFromStream();
  2020. };
  2021. return false;
  2022. */
  2023. return true;
  2024. };
  2025. bool deviceNodeCamera::validate(){
  2026. if ( m_pDBC == NULL ) return false;
  2027. DBC_CAM_DATA *tDATA = m_pDBC->getCamData( m_sCamData.id );
  2028. if ( tDATA == NULL ) return false;
  2029. if ( memcmp( tDATA, &m_sCamData, sizeof(DBC_CAM_DATA) ) != 0 ) {
  2030. //Settings Changed
  2031. //memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
  2032. free( tDATA );
  2033. return false;
  2034. };
  2035. free( tDATA );
  2036. if (!validateStream()) return false;
  2037. return deviceNode::validate();
  2038. };
  2039. bool deviceNodeCamera::load_presets( void ){
  2040. bool bHaveIROFF = false;
  2041. bool bHaveIRON = false;
  2042. bool bHaveIRAUTO = false;
  2043. // Request PRESETS FROM CAMERA?! for some PTZ cameras without presets inpossible to
  2044. // operate by presets
  2045. if( m_pProtocol->getCameraPresets() ){
  2046. LOG("Camera Presets Loadded");
  2047. } else LOG_ERROR("Error while loadding Camera Presets!");
  2048. if ( m_pDBC == NULL ) return false;
  2049. DBC_PRESETS_INFO *tData = m_pDBC->getCamPresetInfo(m_sCamData.id);
  2050. if ( tData ) {
  2051. memcpy( &m_sCamPresets, tData, sizeof(DBC_PRESETS_INFO));
  2052. #ifdef DEBUG
  2053. char tB[128];
  2054. LOG("Loading Presets!");
  2055. sprintf( tB, "Cam Presets Info: CNT: %d Cols: %d Rows: %d ", m_sCamPresets.maxCount, m_sCamPresets.gCols, m_sCamPresets.gRows );
  2056. LOG( tB );
  2057. #endif
  2058. DBC_INT_LIST *tList = m_pDBC->listCamPresets( m_sCamData.id );
  2059. if ( tList ) {
  2060. for( DBC_INT_LIST::iterator idx= tList->begin(); idx < tList->end(); idx++ ) {
  2061. CAM_PRESET *tData = m_pDBC->getCamPreset( *idx );
  2062. if ( tData ) {
  2063. m_sCamPresets.presets.push_back(tData);
  2064. bHaveIRON = ( tData->role == ARGUS_PRESET_TYPE_IR_ON )?true:bHaveIRON;
  2065. bHaveIROFF = ( tData->role == ARGUS_PRESET_TYPE_IR_OFF )?true:bHaveIROFF;
  2066. bHaveIRAUTO = ( tData->role == ARGUS_PRESET_TYPE_IR_AUTO )?true:bHaveIRAUTO;
  2067. };
  2068. };
  2069. delete(tList);
  2070. };
  2071. if ( m_sCamData.eco_ir ) {
  2072. m_bShineOnTarget = (bHaveIROFF && bHaveIRAUTO);
  2073. } else m_bShineOnTarget = false;
  2074. return true;
  2075. #ifdef DEBUG
  2076. } else {
  2077. LOG("No Camera Presets!");
  2078. #endif
  2079. };
  2080. return false;
  2081. };
  2082. void deviceNodeCamera::translateROI2PRESET( float P, float T, float Z, uint8_t &col, uint8_t &row ){
  2083. char tB[128];
  2084. row = (uint8_t) ( Z * ((float)m_sCamPresets.gRows));
  2085. if ( P < 0.5 ){
  2086. P = P + 1.5f;
  2087. } else {
  2088. P = P - 0.5f;
  2089. };
  2090. col = (uint8_t) ( P * ((float)(m_sCamPresets.gCols * (1 << row)) / 2.0f ));
  2091. row++;
  2092. };
  2093. CSTR deviceNodeCamera::getPresetToken( uint8_t CCOL, uint8_t CROW ){
  2094. if( m_sCamPresets.presets.size() > 0 )
  2095. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  2096. if ( (*idx)->role == ARGUS_PRESET_TYPE_POSITION )
  2097. if ( ( (*idx)->row == CROW ) && ( (*idx)->col == CCOL ) ) return (CSTR)(*idx)->token;
  2098. return NULL;
  2099. };
  2100. CSTR deviceNodeCamera::getPresetToken( uint8_t specialRole ){
  2101. if( m_sCamPresets.presets.size() > 0 )
  2102. for ( V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++ )
  2103. if ( (*idx)->role == specialRole ) return (CSTR)(*idx)->token;
  2104. return NULL;
  2105. };
  2106. #undef LOG
  2107. #undef LOG_ERROR
  2108. //#undef DEBUG_EXTRA