forked from NOVACProject/NOVACProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterController.cpp
More file actions
342 lines (270 loc) · 10.3 KB
/
MasterController.cpp
File metadata and controls
342 lines (270 loc) · 10.3 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "StdAfx.h"
#include "mastercontroller.h"
// 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 = NULL;
g_eval = NULL;
g_windMeas = NULL;
g_geometry = NULL;
g_windFieldImport = NULL;
g_report = NULL;
}
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, NULL);
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, NULL);
SetThreadName(g_windFieldImport->m_nThreadID, "WindImp");
/** Start the wind-measurement thread */
g_windMeas = AfxBeginThread(RUNTIME_CLASS(CWindEvaluator),
THREAD_PRIORITY_BELOW_NORMAL, 0, 0, NULL);
SetThreadName(g_windMeas->m_nThreadID, "WindMeas");
/** Start the geometry thread */
g_geometry = AfxBeginThread(RUNTIME_CLASS(CGeometryEvaluator),
THREAD_PRIORITY_BELOW_NORMAL, 0, 0, NULL);
SetThreadName(g_geometry->m_nThreadID, "Geometry");
/* start the communication thread */
g_comm = AfxBeginThread(RUNTIME_CLASS(CCommunicationController),
THREAD_PRIORITY_NORMAL,0,0,NULL);
SetThreadName(g_comm->m_nThreadID, "Comm");
Sleep(1000);
/* start the evaluation thread */
g_eval = AfxBeginThread(RUNTIME_CLASS(CEvaluationController),
THREAD_PRIORITY_NORMAL, 0,0,NULL);
SetThreadName(g_eval->m_nThreadID, "Eval");
/* start the report-writing thread */
g_report = AfxBeginThread(RUNTIME_CLASS(CReportWriter),
THREAD_PRIORITY_NORMAL, 0,0,NULL);
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, NULL, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
message.Format("%s. Compile date: %s", 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 != NULL){
g_geometry->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_geometry, INFINITE);
}
if(g_ftp != NULL){
g_ftp->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_ftp, INFINITE);
}
if(g_report != NULL){
g_report->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(g_report, INFINITE);
}
}
m_fRunning = false;
}
UINT CheckForOldSpectra(LPVOID pParam){
CString path, serial;
Common common;
common.GetExePath();
CList <CString, CString &> fileNames;
// 1. check for spectra in the output\\temp - directory
if(strlen(g_settings.outputDirectory) == 0){
path.Format("%sTemp", common.m_exePath);
}else{
path.Format("%sTemp", g_settings.outputDirectory);
}
CheckForSpectraInDir(path, fileNames);
// 2. Check for spectra in the output\\temp\\SERIAL - directory(-ies)
for(unsigned int i = 0; i < g_settings.scannerNum; ++i){
serial.Format(g_settings.scanner[i].spec[0].serialNumber);
if(strlen(g_settings.outputDirectory) == 0){
path.Format("%sTemp\\%s", common.m_exePath, serial);
}else{
path.Format("%sTemp\\%s", g_settings.outputDirectory, serial);
}
CheckForSpectraInDir(path, fileNames);
}
// 3. Check for spectra in the output\\temp\\RXYZ - directory(-ies)
if(strlen(g_settings.outputDirectory) == 0){
path.Format("%sTemp\\", common.m_exePath);
}else{
path.Format("%sTemp\\", g_settings.outputDirectory);
}
CheckForSpectraInHexDir(path, fileNames);
// 4. Go through all the spectrum files found and evaluate them
if(!fileNames.IsEmpty()){
CPakFileHandler *pakFileHandler = new CPakFileHandler();
POSITION pos = fileNames.GetHeadPosition();
while(pos != NULL){
CString &fn = fileNames.GetNext(pos);
if(IsExistingFile(fn)){
pakFileHandler->ReadDownloadedFile(fn);
}
Sleep(1000);
}
}
return 0;
}
void CheckForSpectraInDir(const CString &path, CList <CString, CString&> &fileList){
WIN32_FIND_DATA FindFileData;
char fileToFind[MAX_PATH];
// Find all .pak-files in the specified directory
sprintf(fileToFind, "%s\\?????????.pak", path);
// Search for the file
HANDLE hFile = FindFirstFile(fileToFind, &FindFileData);
if(hFile == INVALID_HANDLE_VALUE)
return; // no files found
do{
CString fileName;
fileName.Format("%s\\%s", path, FindFileData.cFileName);
if(!Equals(FindFileData.cFileName, "Upload.pak")){
// Tell the user that we've found one scan which hasn't been evaluated
ShowMessage(_T("Found spectra which are not evaluated. Will evaluate them now."));
// Append the found file to the list of files to split and evaluate...
fileList.AddTail(fileName);
}
}while(0 != FindNextFile(hFile, &FindFileData));
FindClose(hFile);
return;
}
/** Checks the settings */
bool CMasterController::CheckSettings(){
unsigned int i, j;
int k, n;
CString message;
FILE *f = NULL;
// Test that there's something configured
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(i = 0; i < g_settings.scannerNum; ++i){
for(j = 0; j < g_settings.scanner[i].specNum; ++j){
for(k = 0; k < g_settings.scanner[i].spec[j].channelNum; ++k){
Evaluation::CFitWindow *window = &g_settings.scanner[i].spec[j].channel[k].fitWindow;
for(n = 0; n < window->nRef; ++n){
f = fopen(window->ref[n].m_path, "r");
if(NULL == f){
message.Format("Cannot read reference file: %s", window->ref[n].m_path);
MessageBox(NULL, message, "Error in settings", MB_OK);
return true;
}
fclose(f);
}
}
}
}
return false;
}
void CheckForSpectraInHexDir(const CString &path, CList <CString, CString&> &fileList){
WIN32_FIND_DATA FindFileData;
char fileToFind[MAX_PATH];
CList <CString, CString &> pathList;
CString pathName;
// Find all RXYZ - directories...
sprintf(fileToFind, "%s\\R???", path);
// Search for the directories
HANDLE hFile = FindFirstFile(fileToFind, &FindFileData);
if(hFile == INVALID_HANDLE_VALUE)
return; // no directories found
do{
pathName.Format("%s\\%s", path, FindFileData.cFileName);
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
// This is a directory, add it to the list of directories to check...
pathList.AddTail(pathName);
}
}while(0 != FindNextFile(hFile, &FindFileData));
FindClose(hFile);
// Check each of the directories found...
POSITION pos = pathList.GetHeadPosition();
while(pos != NULL){
pathName.Format(pathList.GetNext(pos));
// Check the directory...
CheckForSpectraInDir(pathName, fileList);
}
return;
}
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){
}
}