forked from NOVACProject/NOVACProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterController.cpp
298 lines (247 loc) · 9.5 KB
/
MasterController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "StdAfx.h"
#include "mastercontroller.h"
#ifdef _MSC_VER
#pragma warning (push, 4)
#endif
// Include synchronization classes
#include <afxmt.h>
// include the settings
#include "Configuration/Configuration.h"
// and all the threads that are needed
#include "WindMeasurement/WindEvaluator.h"
#include "Geometry/GeometryEvaluator.h"
#include "Evaluation/EvaluationController.h"
#include "Communication/CommunicationController.h"
#include "Communication/FTPServerContacter.h"
#include "WindFileController.h"
#include "Common/ReportWriter.h"
using namespace Evaluation;
using namespace Communication;
using namespace WindSpeedMeasurement;
using namespace Geometry;
using namespace FileHandler;
extern CConfigurationSetting g_settings; // <-- The settings
/** The communication controller */
CWinThread *g_comm;
/** The evaluation thread */
CWinThread *g_eval;
/** The FTP-upload thread */
CWinThread *g_ftp;
/** The thread that evaluates the wind-measurements */
CWinThread *g_windMeas;
/** The thread that combines the scans into plume-height calculations */
CWinThread *g_geometry;
/** The thread that re-reads (and downloads) the wind-field file */
CWinThread *g_windFieldImport;
/** The Report-writing thread */
CWinThread *g_report;
// -------------- We also need to synchronize the threads somewhat -----------
/** This critical section tries to make sure that only one
thread at a time tries to read from one of the small
evaluation-log - files */
CCriticalSection g_evalLogCritSect;
/** This function looks through the output directories and sees if there's any
old spectra there that should be evaluted. This function is only called at
startup to make sure that there's no left-overs from previous runs of the
program (if the program is exited while evaluating a pak-file, the pak file
will still remain in the output directory) */
UINT CheckForOldSpectra(LPVOID pParam);
//void CheckForSpectraInDir(const CString &path, CList <CString, CString&> &fileList);
//void CheckForSpectraInHexDir(const CString &path, CList <CString, CString&> &fileList);
void SetThreadName(DWORD dwThreadID, LPCTSTR szThreadName);
#define MS_VC_EXCEPTION 0x406d1388
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in same addr space)
DWORD dwThreadID; // thread ID (-1 caller thread)
DWORD dwFlags; // reserved for future use, most be zero
} THREADNAME_INFO;
CMasterController::CMasterController(void)
{
m_fRunning = false;
g_comm = nullptr;
g_eval = nullptr;
g_windMeas = nullptr;
g_geometry = nullptr;
g_windFieldImport = nullptr;
g_report = nullptr;
}
CMasterController::~CMasterController(void)
{
}
void CMasterController::Start() {
CString message;
// Start by checking the settings in the program
if (CheckSettings()) {
ShowMessage("Fail to start program. Please check settings and restart");
return;
}
/** Start the FTP-uploading thread */
g_ftp = AfxBeginThread(RUNTIME_CLASS(CFTPServerContacter),
THREAD_PRIORITY_ABOVE_NORMAL, 0, 0, nullptr);
g_ftp->PostThreadMessage(WM_START_FTP, NULL, NULL);
SetThreadName(g_ftp->m_nThreadID, "FTPUpload");
/** Start the wind-field importin file */
g_windFieldImport = AfxBeginThread(RUNTIME_CLASS(CWindFileController),
THREAD_PRIORITY_BELOW_NORMAL, 0, 0, nullptr);
SetThreadName(g_windFieldImport->m_nThreadID, "WindImp");
/** Start the wind-measurement thread */
g_windMeas = AfxBeginThread(RUNTIME_CLASS(CWindEvaluator),
THREAD_PRIORITY_BELOW_NORMAL, 0, 0, nullptr);
SetThreadName(g_windMeas->m_nThreadID, "WindMeas");
/** Start the geometry thread */
g_geometry = AfxBeginThread(RUNTIME_CLASS(CGeometryEvaluator),
THREAD_PRIORITY_BELOW_NORMAL, 0, 0, nullptr);
SetThreadName(g_geometry->m_nThreadID, "Geometry");
/* start the communication thread */
g_comm = AfxBeginThread(RUNTIME_CLASS(CCommunicationController),
THREAD_PRIORITY_NORMAL, 0, 0, nullptr);
SetThreadName(g_comm->m_nThreadID, "Comm");
Sleep(1000);
/* start the evaluation thread */
g_eval = AfxBeginThread(RUNTIME_CLASS(CEvaluationController),
THREAD_PRIORITY_NORMAL, 0, 0, nullptr);
SetThreadName(g_eval->m_nThreadID, "Eval");
/* start the report-writing thread */
g_report = AfxBeginThread(RUNTIME_CLASS(CReportWriter),
THREAD_PRIORITY_NORMAL, 0, 0, nullptr);
SetThreadName(g_report->m_nThreadID, "Report");
m_fRunning = true;
// Wait a little while, to give time for the threads to start
Sleep(500);
// Check if there's any old pak-files lying around that should be taken care of
AfxBeginThread(CheckForOldSpectra, nullptr, THREAD_PRIORITY_NORMAL, 0, 0, nullptr);
message.Format("%s. Compile date: %s", (LPCTSTR)m_common.GetString(MSG_PROGRAM_STARTED_SUCESSFULLY), __DATE__);
ShowMessage(message);
}
void CMasterController::Stop()
{
/** Stop the evaluation and wind-speed correlation thread */
if (m_fRunning) {
g_eval->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_eval, INFINITE);
g_windMeas->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_windMeas, INFINITE);
if (g_geometry != nullptr)
{
g_geometry->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_geometry, INFINITE);
}
if (g_ftp != nullptr)
{
g_ftp->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_ftp, INFINITE);
}
if (g_report != nullptr)
{
g_report->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_report, INFINITE);
}
}
m_fRunning = false;
}
UINT CheckForOldSpectra(LPVOID /*pParam*/)
{
Common common;
common.GetExePath();
CList <CString, CString &> pakFilesToEvaluate;
// 1. check for spectra in the output\\temp - directory
CString path;
if (strlen(g_settings.outputDirectory) == 0)
{
path.Format("%sTemp", (LPCTSTR)common.m_exePath);
}
else
{
path.Format("%sTemp", (LPCTSTR)g_settings.outputDirectory);
}
common.CheckForSpectraInDir(path, pakFilesToEvaluate);
// 2. Check for spectra in the output\\temp\\SERIAL - directory(-ies)
for (unsigned int i = 0; i < g_settings.scannerNum; ++i)
{
const CString serial = g_settings.scanner[i].spec[0].serialNumber;
if (strlen(g_settings.outputDirectory) == 0)
{
path.Format("%sTemp\\%s", (LPCTSTR)common.m_exePath, (LPCTSTR)serial);
}
else
{
path.Format("%sTemp\\%s", (LPCTSTR)g_settings.outputDirectory, (LPCTSTR)serial);
}
common.CheckForSpectraInDir(path, pakFilesToEvaluate);
}
// 3. Check for spectra in the output\\temp\\RXYZ - directory(-ies)
if (strlen(g_settings.outputDirectory) == 0)
{
path.Format("%sTemp\\", (LPCTSTR)common.m_exePath);
}
else
{
path.Format("%sTemp\\", (LPCTSTR)g_settings.outputDirectory);
}
common.CheckForSpectraInHexDir(path, pakFilesToEvaluate);
// 4. Go through all the spectrum files found and evaluate them
if (!pakFilesToEvaluate.IsEmpty())
{
CPakFileHandler pakFileHandler;
POSITION pos = pakFilesToEvaluate.GetHeadPosition();
while (pos != nullptr)
{
CString &fn = pakFilesToEvaluate.GetNext(pos);
if (IsExistingFile(fn))
{
pakFileHandler.ReadDownloadedFile(fn);
}
Sleep(1000);
}
}
return 0;
}
bool CMasterController::CheckSettings()
{
if (g_settings.scannerNum <= 0)
{
MessageBox(NULL, "No scanners are configured, please use the configuration dialog to configure the instruments and restart the program", "Error in configuration", MB_OK);
return true;
}
// Test the reference files
for (unsigned long i = 0; i < g_settings.scannerNum; ++i)
{
for (unsigned long j = 0; j < g_settings.scanner[i].specNum; ++j)
{
for (int k = 0; k < g_settings.scanner[i].spec[j].channelNum; ++k)
{
const Evaluation::CFitWindow *window = &g_settings.scanner[i].spec[j].channel[k].fitWindow;
for (int n = 0; n < window->nRef; ++n)
{
FILE* f = fopen(window->ref[n].m_path.c_str(), "r");
if (nullptr == f)
{
CString message;
message.Format("Cannot read reference file: %s", window->ref[n].m_path.c_str());
MessageBox(NULL, message, "Error in settings", MB_OK);
return true;
}
fclose(f);
}
}
}
}
return false;
}
void SetThreadName(DWORD dwThreadID, LPCTSTR szThreadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info);
}
__except (EXCEPTION_CONTINUE_EXECUTION) {
}
}
#ifdef _MSC_VER
#pragma warning (pop)
#endif