C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
Loading...
Searching...
No Matches
MFCController.cpp
Go to the documentation of this file.
1
5
6// Standard includes
7#include <regex>
8#include <fstream> // ifstream to read files
9#include <sstream> // string manipulation
10
11// Personal includes
12#include "MFCController.h"
13#include "OSDefines.h"
14
15#define LOG(__msg__, __level__,__location__) l_SendLogMessage(__msg__, __level__, "", __location__ )
16
17#if defined _WIN32
18#define DIR_SEP '\\'
19#else
20#define DIR_SEP '/'
21#endif
22
23
24//--------------------------------------------------------------------------//
25// MFC CONTROLLER
26//--------------------------------------------------------------------------//
27void CMFCController::l_SendLogMessage(const std::string & rMessage, const CInstrumentCatalog::eLogLevel & rLevel, const std::string & rContext, const std::string & rLocation)
28{
29 m_ContCat.cLogMessage.RequestControl(CInstrumentCatalog::sLogMessage(rLevel, "MFCManager.Controller"+ rContext, rMessage, rLocation));
30}
31
32void CMFCController::l_SendErrorMessage(const CMFCDevice::sMFCDeviceError & rErrorMessage)
33{
36 m_ContCat.cError.RequestControl(rErrorMessage);
37}
38
39void CMFCController::l_SendSearchProgress(const U8 & rSearchProgress)
40{
41 m_ContCat.cSearchProgress.RequestControl(rSearchProgress);
42}
43
44void CMFCController::l_SendSearchResult(const CInstrumentCatalog::sSearchResult & rSearchResult)
45{
46 m_ContCat.cSearchResult.RequestControl(rSearchResult);
47}
48
49void CMFCController::l_SendMeasResult(const CInstrumentCatalog::sMeasurementResults & rMeasRes)
50{
51 m_ContCat.cMeasResults.RequestControl(rMeasRes);
52}
53
54bool CMFCController::l_IsAbortRequested()
55{
57}
58
59void CMFCController::l_CheckConnectionOnWarning(const CMFCDevice::sMFCDeviceError & rOriginError, CMFCDevice::sMFCDeviceError & rErrorOut)
60{
61 rErrorOut = rOriginError;
62 if (CMFCDevice::sMFCDeviceError::eLevel::kWarning == rOriginError.Level && nullptr != m_pMFCDevice)
63 {
64 if(!m_pMFCDevice->IsInstrumentConnected())
65 {
67 rErrorOut.Description = "No instrument connected";
68 rErrorOut.Location = rErrorOut.Location;
69 }
70 }
71}
72
73void CMFCController::l_AbortInstrument()
74{
76
77 try
78 {
79 if (nullptr != m_pMFCDevice)
80 {
81 if (!m_pMFCDevice->AbortInstrument())
82 {
83 if (!m_pMFCDevice->AbortInstrument())
84 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kError, "Could not abort instrument", MTL__LOCATION__);
85 }
86 }
87 }
88 catch (CMFCDevice::sMFCDeviceError & rE)
89 {
90 // Signal error
91 l_SendErrorMessage(rE);
92 }
93}
94
95void CMFCController::l_ScanResources()
96{
98
99 try
100 {
101 // Check initial conditions
102 MTL_Assert(nullptr != m_pRsrcManager);
103 if (nullptr == m_pRsrcManager)
104 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "No given resource manager", MTL__LOCATION__);
105
106 static CParsedResourceList l_RsrcList, l_NewRsrcList;
107 // Update resource list
108 if (!m_pRsrcManager->FindResources(l_NewRsrcList, "?*"))
109 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kError, "Could not look for resources : " + MTL::Instrument::StatusDescription(m_pRsrcManager->Session(), m_pRsrcManager->Status()), MTL__LOCATION__);
110 if (l_RsrcList != l_NewRsrcList)
111 {
112 l_RsrcList = l_NewRsrcList;
113 m_ContCat.cInstrumentList.RequestControl(l_NewRsrcList);
114 }
115 }
116 catch (CMFCDevice::sMFCDeviceError & rE)
117 {
118 // Signal error
119 l_SendErrorMessage(rE);
120 // Send empty list
121 m_ContCat.cInstrumentList.RequestControl(CParsedResourceList());
122 }
123}
124
125void CMFCController::l_GetRemoteBoxButtonStatus()
126{
127 //LOG(MTL__FUNCTION_NAME__, CInstrumentCatalog::eLogLevel::kDebug, MTL__LOCATION__); //Comment it to reduce nb of message sent in LOG and move it to "if (l_RemoteBoxPressed)"
128 try {
129 CMFCDevice::sMFCDeviceError l_RetError;
130 bool l_RemoteBoxPressed(false);
131 // Check initial conditions
132 MTL_Assert(nullptr != m_pMFCDevice);
133 if (nullptr == m_pMFCDevice)
134 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Given instrument pointer is null", MTL__LOCATION__);
135 l_RetError = m_pMFCDevice->RemoteBoxButtonStatus(l_RemoteBoxPressed);
136 if(l_RetError.HasError())
137 {
138 throw l_RetError;
139 }
140 if (l_RemoteBoxPressed)
141 {
143 m_ContCat.cRemoteBoxStart.RequestControl(true);
144 }
145 }
146 catch (CMFCDevice::sMFCDeviceError & rE)
147 {
148 CMFCDevice::sMFCDeviceError l_Error;
149 l_CheckConnectionOnWarning(rE, l_Error);
150 // Signal error
151 l_SendErrorMessage(l_Error);
152 }
153}
154
155bool CMFCController::l_ScanPA()
156{
157 //LOG(MTL__FUNCTION_NAME__, CInstrumentCatalog::eLogLevel::kDebug, MTL__LOCATION__); //Comment it to reduce nb of message sent in LOG and move it to "if (l_PAInfoChanged)"
158 bool l_PAInfoChanged(false);
159 bool l_PASucceeded(true);
160 CInstrumentCatalog::sInstrumentConfiguration l_InsConf;
161 try
162 {
163 CMFCDevice::sMFCDeviceError l_RetError;
164
165 // Check initial conditions
166 MTL_Assert(nullptr != m_pMFCDevice);
167 if (nullptr == m_pMFCDevice)
168 {
169 if (!m_PAInfo.ProbeList.empty())
170 {
171 // Clear whole PA Info including FCA information
172 m_PAInfo = CInstrumentCatalog::sProbeArrayInformation();
173 l_InsConf = CInstrumentCatalog::sInstrumentConfiguration();
174 l_PAInfoChanged = true;
175 }
176 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Given instrument pointer is null", MTL__LOCATION__);
177 }
178 l_RetError = m_pMFCDevice->ScanPA(m_PAInfo, l_InsConf, l_PAInfoChanged);
179 if(l_RetError.HasError())
180 {
181 throw l_RetError;
182 }
183 }
184 catch (CMFCDevice::sMFCDeviceError & rE)
185 {
186 CMFCDevice::sMFCDeviceError l_Error;
187 l_CheckConnectionOnWarning(rE, l_Error);
188 // Signal error
189 l_SendErrorMessage(l_Error);
190 l_PASucceeded = false;
191 }
192 if (l_PAInfoChanged) // Send new PA Info if changed
193 {
195 m_ContCat.cPAInfo.RequestControl(m_PAInfo);
196 m_ContCat.cInstrumentConfiguration.rRequestControl(l_InsConf);
197 }
198 return l_PASucceeded;
199}
200
201void CMFCController::l_OperateWithInstrument()
202{
204 try
205 {
206 CMFCDevice::sMFCDeviceError l_RetError;
207 // Depending on the requested operating mode
208 switch (m_InstrState.OperatingMode)
209 {
210 //------------------------------------------------
211 // Idle operation
213 // Nothing to do
214 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Cannot operate in kIdle mode", MTL__LOCATION__);
215 break;
216
217 //------------------------------------------------
218 // Search operation
220 {
221 // Check initial conditions
222 // Check that we have an instrument
223 MTL_Assert(nullptr != m_pMFCDevice);
224 if (nullptr == m_pMFCDevice)
225 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Given instrument pointer is null", MTL__LOCATION__);
226 l_RetError = m_pMFCDevice->Search(m_InstrState);
227 if(l_RetError.HasError())
228 {
229 throw l_RetError;
230 }
231 }
232 break;
233
234 //------------------------------------------------
235 // Measure operation
237 {
238 // Check initial conditions
239 // Check that we have an instrument
240 MTL_Assert(nullptr != m_pMFCDevice);
241 if (nullptr == m_pMFCDevice)
242 throw CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Given instrument pointer is null", MTL__LOCATION__);
243 l_RetError = m_pMFCDevice->Measure(m_InstrState);
244 if(l_RetError.HasError())
245 {
246 throw l_RetError;
247 }
248 }
249 break;
250 }
251 }
252 catch (CMFCDevice::sMFCDeviceError & rE)
253 {
254 // Signal error
255 l_SendErrorMessage(rE);
256 }
257}
258
259void CMFCController::l_RequestSelfAbort()
260{
261 Terminate();
262
264 m_ContCat.cState.RequestControl(m_ControllerState);
265}
266
267CMFCController::CMFCController(CMFCManagerCatalog & rCatInstance, const std::string & rCustomPAConfScriptDir)
268 :
269 m_ContCat(rCatInstance),
270 m_Timer(std::bind(&CMFCController::l_PostTimerEvent, this)),
271 m_ControllerState(CMFCManagerCatalog::eControllerState::kIdle),
272 m_pMFCDevice(nullptr),
273 m_CustomPAConfScriptDirectory(rCustomPAConfScriptDir)
274{
276}
277
284
285void CMFCController::l_TimerEventHandler()
286{
287 try
288 {
289 switch (m_ControllerState)
290 {
292 l_ScanResources();
293 m_Timer.Stop();
294 m_Timer.StartSingleShot(std::chrono::milliseconds(RSRC_POLLING_PERIOD_MS));
295 break;
296
298 if(l_ScanPA())
299 l_GetRemoteBoxButtonStatus();
300 m_Timer.Stop();
301 m_Timer.StartSingleShot(std::chrono::milliseconds(PA_POLLING_PERIOD_MS));
302 break;
303
306 l_OperateWithInstrument();
307 Terminate();
308 break;
309
310 default:
311 // Unhandled case
312 MTL_Assert(false);
313 throw false;
314 break;
315 }
316 }
317 catch (...)
318 {
319 l_SendErrorMessage(CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Unhandled exception type", MTL__LOCATION__));
320 Terminate();
321 }
322}
323
324void CMFCController::l_PostTimerEvent()
325{
327}
328
329void CMFCController::l_ThreadTask(eEventType EvtType, sMFCControllerMailBoxMsg * pMsg)
330{
331 switch (EvtType)
332 {
333 case eEventType::kExit:
334 // Abort instrument
335 l_AbortInstrument();
336 // Set new controller state
338 m_ContCat.cState.RequestControl(m_ControllerState);
339 break;
340 case eEventType::kUserMsg:
341 if (nullptr != pMsg)
342 {
343 switch (pMsg->MsgType)
344 {
346 l_TimerEventHandler();
347 break;
348 }
349 }
350 break;
351 }
352}
353
358
360{
362 m_Timer.Stop(); // Must be done after CThreadedMailBox::ThreadStop(). Timer may have been restarted before the thread stop completely.
363}
364
366{
368
369 // Prepare context for scanning instruments
370 // Start timer to trigger periodic scan
371
372 Terminate();
373
374 m_pMFCDevice = nullptr;
375 MTL_Assert(nullptr != pRsrcManager);
376 if (nullptr == pRsrcManager)
377 {
378 l_SendErrorMessage(CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Given resource manager pointer is null", MTL__LOCATION__));
379 return;
380 }
381 m_pRsrcManager = pRsrcManager;
382
384 m_ContCat.cState.RequestControl(m_ControllerState);
385 Operate();
386 m_Timer.StartSingleShot(std::chrono::milliseconds(0));
387}
388
390{
392
393 // Prepare context for scanning PA
394 // Start timer to trigger periodic scan
395
396 Terminate();
397
398 MTL_Assert(nullptr != pMFCDevice);
399 if (nullptr == pMFCDevice)
400 {
401 l_SendErrorMessage(CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Given instrument pointer is null", MTL__LOCATION__));
402 return;
403 }
404 if (pMFCDevice != m_pMFCDevice)
405 {
406 m_PAInfo.clear(); // Clear PA info
407 }
408 m_pMFCDevice = pMFCDevice;
409
411 m_ContCat.cState.RequestControl(m_ControllerState);
412 Operate();
413 m_Timer.StartSingleShot(std::chrono::milliseconds(0));
414}
415
417{
419
420 // Prepare context for changing instrument state
421 // If requested to go Idle, nothing else is done
422 // If requested to operate, trigger operation with a single timer shot
423
424 Terminate();
425
426 MTL_Assert(nullptr != pMFCDevice);
427 if (nullptr == pMFCDevice)
428 {
429 l_SendErrorMessage(CMFCDevice::sMFCDeviceError(CMFCDevice::sMFCDeviceError::eLevel::kFatal, "Given instrument pointer is null", MTL__LOCATION__));
430 return;
431 }
432 m_pMFCDevice = pMFCDevice;
433 m_InstrState = InsState;
434
435 switch (m_InstrState.OperatingMode)
436 {
439 m_ContCat.cState.RequestControl(m_ControllerState);
440 // Nothing to do
441 break;
444 m_ContCat.cState.RequestControl(m_ControllerState);
445 Operate();
446 m_Timer.StartSingleShot(std::chrono::milliseconds(0));
447 break;
450 m_ContCat.cState.RequestControl(m_ControllerState);
451 Operate();
452 m_Timer.StartSingleShot(std::chrono::milliseconds(0));
453 break;
454 }
455}
456
458{
460
461 try {
462 MTL_Assert(nullptr != m_pMFCDevice);
463 if (nullptr == m_pMFCDevice)
465
468 l_RetError = m_pMFCDevice->GetNormalizationDate(l_NewNormDate);
469 if(l_RetError.HasError())
470 {
471 throw l_RetError;
472 }
473 if(l_NewNormDate != m_PAInfo.NormalizationDate)
474 {
475 m_PAInfo.NormalizationDate = l_NewNormDate;
476 m_ContCat.cPAInfo.RequestControl(m_PAInfo); //send updated PA info with new normalization date
477 }
478 }
479 catch (CMFCDevice::sMFCDeviceError & rE)
480 {
482 l_CheckConnectionOnWarning(rE, l_Error);
483 // Signal error
484 l_SendErrorMessage(l_Error);
485 }
486}
#define LOG(__msg__, __level__, __location__)
#define MTL_Assert
Definition Helpers.h:102
#define MTL__LOCATION__
Definition Helpers.h:24
Platform Dependent Definitions.
std::string StatusDescription(ViSession Session, ViStatus Status)
Return user-readable description of the given status code.
enum MTL::sMFCControllerMailBoxMsg::eMsgType MsgType
CMFCController(CMFCManagerCatalog &rCatInstance, const std::string &rCustomPAConfScriptDir)
void ScanPA(CMFCDevice *pMFCDevice)
void ScanInstruments(CVISAResourceManager *pRsrcManager)
void SetInstrumentState(CMFCDevice *pMFCDevice, CInstrumentCatalog::sInstrumentState InsState)
enum MTL::CMFCDevice::sMFCDeviceError::eLevel Level
CControl< CInstrumentCatalog::sLogMessage > cLogMessage
bool StartSingleShot(std::chrono::milliseconds Delay_ms)
void l_SendMsg(const UserMsgT &rMsg)
VISA Resource Manager class.