Old fashion design for PC-104
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.

292 lines
7.6 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: SensorCLD.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of SensorCLD.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "SensorCLD.h"
  16. #include "SensorArray.h"
  17. #include "..\..\LgLib\Device\LgDeviceCLD.h"
  18. #include "..\..\LgLib\Version.h"
  19. CSensorCLD::CSensorCLD()
  20. {
  21. m_nType = CLD;
  22. m_strName = "CLD";
  23. m_bAlarm = FALSE;
  24. m_dAlarmRange = 0;
  25. m_nTimeoutPostAlarm.QuadPart = 100000000i64;
  26. m_nLastTargetTime.QuadPart = 0i64;
  27. m_SurfaceCLD.Create( 200, 200 );
  28. m_bConnected = TRUE;
  29. // log info
  30. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSensorCLD::CSensorCLD") );
  31. }
  32. CSensorCLD::~CSensorCLD()
  33. {
  34. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSensorCLD::~CSensorCLD") );
  35. }
  36. void CSensorCLD::ConnectToDevice( CDeviceCLD *pCLD )
  37. {
  38. pCLD->/*CLgNetDeviceCLD::*/CLgLinkSP<CLgNetObject,CLgNetObject,CLgNetObjectParent>::Link( this );
  39. CLgLinkP<CLgNetObject,CLgNetObject,CLgNetObjectChild>::Link( (CLgNetDeviceCLD*)pCLD );
  40. // store id
  41. m_OutMsg.SetSenderID( GetID() );
  42. m_OutMsg.SetDistinationID( pCLD->CLgNetDeviceCLD::GetID() );
  43. // perform reset at the begining
  44. Reset();
  45. }
  46. void CSensorCLD::Reset()
  47. {
  48. // send message to device
  49. if( !m_OutMsg.BuildCmdReset() )
  50. {
  51. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CSensorCLD::Reset"), _T("m_OutMsg.BuildCmdReset failed") );
  52. return;
  53. }
  54. if( !SendMessageToChild( m_OutMsg ) )
  55. {
  56. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CSensorCLD::Reset"), _T("SendMessageToChild failed") );
  57. return;
  58. }
  59. m_bAlarm = FALSE;
  60. m_dAlarmRange = 0;
  61. m_SurfaceCLD.Create( 200, 200 );
  62. CSensor::Reset();
  63. }
  64. void CSensorCLD::CheckState()
  65. {
  66. SYSTEMTIME st;
  67. ::GetLocalTime( &st );
  68. LARGE_INTEGER lt;
  69. ::SystemTimeToFileTime( &st, (LPFILETIME)&lt );
  70. while( PopMessage( m_InMsg ) )
  71. {
  72. // store sender id
  73. m_OutMsg.SetSenderID( GetID() );
  74. m_OutMsg.SetDistinationID( m_InMsg.GetSenderID() );
  75. BYTE bCommand;
  76. if( !m_InMsg.GetCommand( &bCommand ) )
  77. {
  78. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CSensorCLD::CheckState"), _T("m_InMsg.GetCommand failed") );
  79. continue;
  80. }
  81. EVENT event;
  82. switch( bCommand )
  83. {
  84. case CLgDeviceCLD::EVT_RESET:
  85. break;
  86. case CLgDeviceCLD::EVT_ALARM:
  87. m_InMsg.GetAlarm( &m_dAlarmRange );
  88. // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CSensorCLD::CheckState"), _T("Test %0.1f"), m_dAlarmRange );
  89. // scale to current maximal range
  90. m_dAlarmRange *= m_dRange;
  91. m_bAlarm = TRUE;
  92. if( GetState() != ALARM )
  93. SetState( ALARM );
  94. event.id = LGS_EVENT_ALARM_SHOW;
  95. event.data8 = 0;
  96. event.data16 = GetArrayIndex();
  97. event.data32 = 0;
  98. event.color = GetStateColor( FALSE );
  99. event.time = lt;
  100. event.pSensor = this;
  101. event.target.bType = LGS::TT_NORMAL;
  102. event.target.bDevice = 0;
  103. event.target.dAngle = 0;
  104. event.target.dRange = m_dAlarmRange;
  105. sprintf( event.pText, _T("%s %0.1f m."), (LPCTSTR)GetName(), m_dAlarmRange );
  106. m_pSensorArray->GetEventQueue()->PostEvent( event );
  107. m_SurfaceCLD.DrawTarget( m_dAlarmRange, RGB( 255,0,0 ) );
  108. m_nLastTargetTime = lt;
  109. break;
  110. case CLgDeviceCLD::EVT_CONNECTION:
  111. // store last synchron time
  112. m_InMsg.GetEvtConnection( &m_bConnected );
  113. break;
  114. default:
  115. // discard messages
  116. break;
  117. }
  118. }
  119. if( GetState() != DISABLE )
  120. {
  121. if( !m_bConnected )
  122. {
  123. if( GetState() != TIMEOUT )
  124. {
  125. SetState( TIMEOUT );
  126. EVENT event;
  127. event.id = LGS_EVENT_TIMEOUT_APPEAR;
  128. event.data8 = 0;
  129. event.data16 = GetArrayIndex();
  130. event.data32 = 0;
  131. event.color = GetStateColor( FALSE );
  132. event.pSensor = this;
  133. _stprintf( event.pText, _T("%s sensor timeout"), (LPCTSTR)GetName() );
  134. event.time = lt;
  135. m_pSensorArray->GetEventQueue()->PostEvent( event );
  136. }
  137. }
  138. else
  139. {
  140. if( m_bAlarm )
  141. {
  142. if( lt.QuadPart - m_nLastTargetTime.QuadPart > m_nTimeoutPostAlarm.QuadPart )
  143. {
  144. if( GetState() != POSTALARM )
  145. {
  146. SetState( POSTALARM );
  147. }
  148. }
  149. else
  150. {
  151. if( GetState() != ALARM )
  152. SetState( ALARM );
  153. }
  154. }
  155. else
  156. {
  157. if( GetState() != NORMAL )
  158. {
  159. SetState( NORMAL );
  160. EVENT event;
  161. event.id = LGS_EVENT_NORMAL;
  162. event.data8 = 0;
  163. event.data16 = GetArrayIndex();
  164. event.data32 = 0;
  165. event.pSensor = this;
  166. event.color = GetStateColor( FALSE );
  167. _stprintf( event.pText, _T("%s sensor normal"), (LPCTSTR)GetName() );
  168. ::GetLocalTime( &st );
  169. ::SystemTimeToFileTime( &st, (LPFILETIME)&event.time );
  170. m_pSensorArray->GetEventQueue()->PostEvent( event );
  171. }
  172. }
  173. }
  174. }
  175. // all the time update events
  176. OnEvent( COFDeviceCLD::CLD_EVT_ALARM, GetState() == ALARM );
  177. OnEvent( COFDeviceCLD::CLD_EVT_POSTALARM, GetState() == POSTALARM );
  178. OnEvent( COFDeviceCLD::CLD_EVT_TIMEOUT, GetState() == TIMEOUT );
  179. OnEvent( COFDeviceCLD::CLD_EVT_NORMAL, GetState() == NORMAL );
  180. }
  181. void CSensorCLD::OnUIMapDraw( CLgUIMapObjectWnd *pMap, CDC *pDC, BOOL bActive, BOOL bOddUpdate )
  182. {
  183. // log info
  184. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CSensorCLD::OnUIMapDraw", "Start..." );
  185. CLgDPoint dp( m_Position.x, m_Position.y );
  186. // draw sensor representation
  187. CPoint p = pMap->AreaToWindow( &dp );
  188. CRect rect;
  189. if( bActive && !bOddUpdate ) rect.SetRect( p.x - 4, p.y - 4, p.x + 4, p.y + 4 );
  190. else rect.SetRect( p.x - 2, p.y - 2, p.x + 2, p.y + 2 );
  191. pDC->FillSolidRect( &rect, GetStateColor( bOddUpdate ) );
  192. double dMeterToPixelScale = pMap->GetMeterToPixelScale();
  193. // draw targets
  194. if( m_bAlarm )
  195. {
  196. double a = m_dCommonZero * 3.14 / 180;
  197. CPoint tp( p.x + (LONG)( cos(a) * m_dAlarmRange * dMeterToPixelScale ),
  198. p.y - (LONG)( sin(a) * m_dAlarmRange * dMeterToPixelScale ) );
  199. // paint them
  200. rect.SetRect( tp.x - 2, tp.y - 2, tp.x + 2, tp.y + 2 );
  201. pDC->FillSolidRect( &rect, GetStateColor( bOddUpdate ) );
  202. }
  203. // log info
  204. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CSensorCLD::OnUIMapDraw", "Done" );
  205. }
  206. void CSensorCLD::OnDrawBackground( CDC *pDC, CPoint p, double dMeterToPixelScale )
  207. {
  208. // log info
  209. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CSensorCLD::OnUIMapDrawBackground", "Start..." );
  210. // draw line
  211. double a = m_dCommonZero * 3.14 / 180;
  212. CPoint tp( p.x + (LONG)( cos(a) * m_dRange * dMeterToPixelScale ),
  213. p.y - (LONG)( sin(a) * m_dRange * dMeterToPixelScale ) );
  214. CPen pen( PS_SOLID, 3, RGB( 0xAF, 0xAF, 0 ) );
  215. CPen *pPen = (CPen*)pDC->SelectObject( &pen );
  216. pDC->MoveTo( p );
  217. pDC->LineTo( tp );
  218. pDC->SelectObject( pPen );
  219. // log info
  220. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CSensorCLD::OnUIMapDrawBackground", "Done" );
  221. }
  222. BOOL CSensorCLD::OnUIMapHitTest( CLgUIMapObjectWnd *pMap, CLgDRect *pRect )
  223. {
  224. // check main point
  225. if( CVisual::OnUIMapHitTest( pMap, pRect ) )
  226. return TRUE;
  227. CLgDPoint dp( m_Position.x, m_Position.y );
  228. CRect r = pMap->AreaToWindow( pRect );
  229. CPoint p = pMap->AreaToWindow( &dp );
  230. double dMeterToPixelScale = pMap->GetMeterToPixelScale();
  231. if( m_bAlarm )
  232. {
  233. double a = m_dCommonZero * 3.14 / 180;
  234. CPoint tp( p.x + (LONG)( cos(a) * m_dAlarmRange * dMeterToPixelScale ),
  235. p.y - (LONG)( sin(a) * m_dAlarmRange * dMeterToPixelScale ) );
  236. // check it
  237. if( r.PtInRect( tp ) )
  238. return TRUE;
  239. }
  240. return pRect->PtInRect( dp );
  241. }