-
Notifications
You must be signed in to change notification settings - Fork 6
/
View_OverView.cpp
298 lines (240 loc) · 9.52 KB
/
View_OverView.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
// OverView.cpp : implementation file
//
#include "stdafx.h"
#include "NovacMasterProgram.h"
#include "View_OverView.h"
#include "Configuration/Configuration.h"
#include "UserSettings.h"
extern CConfigurationSetting g_settings;
extern CUserSettings g_userSettings; // <-- The users preferences
using namespace Graph;
// CView_OverView dialog
IMPLEMENT_DYNAMIC(CView_OverView, CPropertyPage)
CView_OverView::CView_OverView()
: CPropertyPage(CView_OverView::IDD)
{
m_evalDataStorage = nullptr;
m_commDataStorage = nullptr;
}
CView_OverView::~CView_OverView()
{
m_evalDataStorage = nullptr;
m_commDataStorage = nullptr;
for (int i = 0; i < m_graphs.GetCount(); ++i) {
CGraphCtrl* graph = m_graphs[i];
delete graph;
}
m_graphs.RemoveAll();
for (int i = 0; i < m_specLabel.GetCount(); ++i) {
CStatic* label = m_specLabel[i];
delete label;
}
m_specLabel.RemoveAll();
for (int i = 0; i < m_statusLabel.GetCount(); ++i) {
CStatic* label = m_statusLabel[i];
delete label;
}
m_statusLabel.RemoveAll();
for (int i = 0; i < m_fluxLabel.GetCount(); ++i) {
CStatic* label = m_fluxLabel[i];
delete label;
}
m_fluxLabel.RemoveAll();
}
void CView_OverView::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CView_OverView, CPropertyPage)
// Messages from other parts of the program
ON_MESSAGE(WM_EVAL_SUCCESS, OnUpdateFluxes)
ON_MESSAGE(WM_EVAL_FAILURE, OnUpdateFluxes)
END_MESSAGE_MAP()
// CView_OverView message handlers
BOOL CView_OverView::OnInitDialog()
{
CPropertyPage::OnInitDialog();
CString specString[50];
CString statusString[50];
CString fluxString[50];
CRect thisRect, rect, rect2;
int nSpectrometers = g_settings.scannerNum;
int margin = 15;
int labelWidth = 200;
CString fluxAxisLabel;
Common common;
this->GetWindowRect(thisRect);
if (nSpectrometers == 0)
return TRUE;
if (g_userSettings.m_fluxUnit == UNIT_TONDAY) {
fluxAxisLabel.Format("%s [ton/day]", (LPCTSTR)common.GetString(AXIS_FLUX));
}
else {
fluxAxisLabel.Format("%s [kg/s]", (LPCTSTR)common.GetString(AXIS_FLUX));
}
// reduce the margin if there are many instruments, to make the page look nicer
margin = margin / nSpectrometers;
// The height of each graph
int height = (thisRect.Height() - margin * (1 + nSpectrometers)) / nSpectrometers;
rect.left = margin;
rect.right = thisRect.Width() - labelWidth - margin;
rect2.left = rect.right + margin;
rect2.right = thisRect.Width();
// The font we want to use
CFont* font = new CFont();
font->CreateFont((int)(16 - 0.2 * nSpectrometers), 0, 0, 0, FW_BOLD,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, "Arial");
// Create all the graphs
for (int i = 0; i < nSpectrometers; ++i) {
rect.top = i * (height + margin) + margin;
rect.bottom = rect.top + height;
// Create the graph
CGraphCtrl* graph = new CGraphCtrl();
if (i == nSpectrometers - 1) {
graph->SetXUnits(m_common.GetString(AXIS_UTCTIME));
}
else {
graph->HideXScale();
}
graph->Create(WS_VISIBLE | WS_CHILD, rect, this);
graph->SetFontHeight((int)(14 - 0.3 * nSpectrometers));
graph->SetXAxisNumberFormat(FORMAT_DATETIME);
graph->EnableGridLinesX(true);
graph->SetYUnits(fluxAxisLabel);
graph->SetLineWidth(2); // Increases the line width to 2 pixels
graph->SetMinimumRangeY(1.0);
graph->SetPlotColor(RGB(255, 255, 255));
graph->SetGridColor(RGB(255, 255, 255));
graph->SetBackgroundColor(RGB(0, 0, 0));
SetTodayRange(graph);
m_graphs.Add(graph);
// Create the label with the name of the graph
CStatic* label = new CStatic();
rect2.top = rect.top;
rect2.bottom = rect2.top + height;
specString[i].Format("%s - %s", (LPCTSTR)g_settings.scanner[i].spec[0].serialNumber, (LPCTSTR)g_settings.scanner[i].site);
label->Create(specString[i], WS_VISIBLE | WS_CHILD, rect2, this);
label->SetFont(font);
m_specLabel.Add(label);
// Create the label with number of spectra today
CStatic* statusLabel = new CStatic();
rect2.top = rect.top + (rect.bottom - rect.top) * 1 / 5;
rect2.bottom = rect2.top + 15;
statusString[i].Format("Scans received last 24 hours: 0");
statusLabel->Create(statusString[i], WS_VISIBLE | WS_CHILD, rect2, this);
statusLabel->SetFont(font);
m_statusLabel.Add(statusLabel);
// Create the label telling the average and std of the flux
CStatic* fluxLabel = new CStatic();
rect2.top = rect.top + (rect.bottom - rect.top) * 2 / 5;
rect2.bottom = rect2.top + 90;
if (g_userSettings.m_fluxUnit == UNIT_TONDAY) {
fluxString[i].Format("%s\tavg: 0 [ton/day]\r\n\tstd: 0 [ton/day]", (LPCTSTR)common.GetString(AXIS_FLUX));
}
else {
fluxString[i].Format("%s\tavg: 0 [kg/s]\r\n\tstd: 0 [kg/s]", (LPCTSTR)common.GetString(AXIS_FLUX));
}
fluxLabel->Create(fluxString[i], WS_VISIBLE | WS_CHILD, rect2, this);
fluxLabel->SetFont(font);
m_fluxLabel.Add(fluxLabel);
}
DrawFlux();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
LRESULT CView_OverView::OnUpdateFluxes(WPARAM wParam, LPARAM lParam) {
// Re-draw the screen
DrawFlux();
return 0;
}
void CView_OverView::DrawFlux() {
double maxFlux;
const int BUFFER_SIZE = 200;
double allFluxes[BUFFER_SIZE], tid[BUFFER_SIZE];
int fluxOk[BUFFER_SIZE];
double goodFluxes[BUFFER_SIZE], badFluxes[BUFFER_SIZE];
double goodTime[BUFFER_SIZE], badTime[BUFFER_SIZE];
int nGoodFluxes = 0, nBadFluxes = 0;
Common common;
CString serial, statusStr, tempStr, fluxAxisLabel;
double avgFlux, stdFlux;
int nSpectrometers = g_settings.scannerNum;
for (int i = 0; i < nSpectrometers; ++i) {
// The serialnumber
serial.Format(g_settings.scanner[i].spec[0].serialNumber);
// Get the data
int dataLength = m_evalDataStorage->GetFluxData(serial, tid, allFluxes, fluxOk, BUFFER_SIZE);
// sort the data into good values and bad values
nGoodFluxes = 0;
nBadFluxes = 0;
for (int k = 0; k < dataLength; ++k) {
if (fluxOk[k]) {
goodFluxes[nGoodFluxes] = allFluxes[k];
goodTime[nGoodFluxes] = tid[k];
++nGoodFluxes;
}
else {
badFluxes[nBadFluxes] = allFluxes[k];
badTime[nBadFluxes] = tid[k];
++nBadFluxes;
}
}
// The graph to draw in
CGraphCtrl* graph = m_graphs.GetAt(i);
// Clear the plot
graph->CleanPlot();
// Set the unit of the plot
if (g_userSettings.m_fluxUnit == UNIT_TONDAY) {
fluxAxisLabel.Format("%s [ton/day]", (LPCTSTR)common.GetString(AXIS_FLUX));
}
else {
fluxAxisLabel.Format("%s [kg/s]", (LPCTSTR)common.GetString(AXIS_FLUX));
}
graph->SetYUnits(fluxAxisLabel);
// If there's no data, don't draw anything
if (dataLength == 0)
continue;
// Get the ranges for the flux data
maxFlux = Max(allFluxes, dataLength);
// set the range for the plot
graph->SetRangeY(0, maxFlux, 1);
SetTodayRange(graph);
// First draw the bad values, then the good ones
graph->SetCircleColor(RGB(150, 150, 150));
graph->XYPlot(badTime, badFluxes, nBadFluxes, CGraphCtrl::PLOT_FIXED_AXIS | CGraphCtrl::PLOT_CIRCLES);
graph->SetCircleColor(RGB(255, 255, 255));
graph->XYPlot(goodTime, goodFluxes, nGoodFluxes, CGraphCtrl::PLOT_FIXED_AXIS | CGraphCtrl::PLOT_CIRCLES);
// Update the status-label...
statusStr.Format("Scans received last 24 hours: %02d", dataLength);
m_statusLabel.GetAt(i)->SetWindowText(statusStr);
// Update the flux-label
// 1. Flux
m_evalDataStorage->GetFluxStat(serial, avgFlux, stdFlux);
if (UNIT_KGS == g_userSettings.m_fluxUnit) {
tempStr.Format("%s\tavg: %.0lf [kg/s]\r\n\tstd: %.0lf [kg/s]\r\n", (LPCTSTR)common.GetString(AXIS_FLUX), avgFlux, stdFlux);
}
else {
tempStr.Format("%s\tavg: %.0lf [ton/day]\r\n\tstd: %.0lf [ton/day]\r\n", (LPCTSTR)common.GetString(AXIS_FLUX), avgFlux, stdFlux);
}
m_fluxLabel.GetAt(i)->SetWindowText(tempStr);
}
}
BOOL CView_OverView::OnSetActive()
{
// Redraw the screen
DrawFlux();
return CPropertyPage::OnSetActive();
}
void CView_OverView::SetTodayRange(CGraphCtrl* graph) {
// set time range for last day plot
struct tm* tm;
time_t endtime;
time(&endtime);
tm = gmtime(&endtime);
time_t starttime = endtime - 60 * 60 * 24;
graph->SetRangeX(starttime, endtime, 0);
}