-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParaCal.cpp
282 lines (215 loc) · 6.36 KB
/
ParaCal.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
// ParaCal.cpp : implementation file
//
#include "stdafx.h"
#include "轨道清洁检测车监控系统.h"
#include "ParaCal.h"
#include<algorithm>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CParaCal dialog
struct PTYPE{
CString TableTypeName;
int TableTypeID;
CString TableRow[4];
}tabletype[8];
CParaCal::CParaCal(CWnd* pParent /*=NULL*/)
: CDialog(CParaCal::IDD, pParent)
{
//{{AFX_DATA_INIT(CParaCal)
m_Average = _T("");
m_Max = _T("");
m_Min = _T("");
//}}AFX_DATA_INIT
curTableIndex = 0;
curTableName = _T("");
curRowIndex = 0;
curRowName = _T("");
for(int i=0;i<100;i++)
{
TempData[i] = 0;
}
//type init----------------------------------------------------------
tabletype[0].TableTypeName = "清洁车经纬度表"; //current position
tabletype[0].TableTypeID = 0;
tabletype[0].TableRow[0] = "清洁车经度";
tabletype[0].TableRow[1] = "清洁车纬度";
tabletype[1].TableTypeName = "清洁车位置表"; //position
tabletype[1].TableTypeID = 1;
tabletype[1].TableRow[0] = "车左前位置";
tabletype[1].TableRow[1] = "车右前位置";
tabletype[1].TableRow[2] = "车左后位置";
tabletype[1].TableRow[3] = "车右后位置";
tabletype[2].TableTypeName = "清水箱表"; //clean water box
tabletype[2].TableTypeID = 2;
tabletype[2].TableRow[0] = "清水箱水位";
tabletype[3].TableTypeName = "水泵压力表"; //water pressure
tabletype[3].TableTypeID = 3;
tabletype[3].TableRow[0] = "高压水泵压力";
tabletype[3].TableRow[1] = "低压水泵压力";
tabletype[4].TableTypeName = "温度表"; //temperature
tabletype[4].TableTypeID = 4;
tabletype[4].TableRow[0] = "副发动机水温";
tabletype[4].TableRow[1] = "车外环境温度";
tabletype[4].TableRow[2] = "车内温度";
tabletype[5].TableTypeName = "污水箱表"; //dirty water box
tabletype[5].TableTypeID = 5;
tabletype[5].TableRow[0] = "污水箱水位";
tabletype[6].TableTypeName = "吸尘口压力表"; //wind pressure
tabletype[6].TableTypeID = 6;
tabletype[6].TableRow[0] = "前吸尘口压力";
tabletype[6].TableRow[1] = "后吸尘口1压力";
tabletype[6].TableRow[2] = "后吸尘口2压力";
tabletype[7].TableTypeName = "逆变器电压";//volt
tabletype[7].TableTypeID = 7;
tabletype[7].TableRow[0] = "逆变器电压";
DatabaseConnect();
}
void CParaCal::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CParaCal)
DDX_Text(pDX, IDC_EDIT_AVG, m_Average);
DDX_Text(pDX, IDC_EDIT_MAX, m_Max);
DDX_Text(pDX, IDC_EDIT_MIN, m_Min);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CParaCal, CDialog)
//{{AFX_MSG_MAP(CParaCal)
ON_WM_DESTROY()
ON_CBN_SELCHANGE(IDC_COMBO_TABLE, OnSelchangeComboTable)
ON_CBN_SELCHANGE(IDC_COMBO_COL, OnSelchangeComboCol)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CParaCal message handlers
void CParaCal::DatabaseConnect()
{
//初始化com环境
::CoInitialize(NULL);
//创建连接对象和记录集对象
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(
"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=清洁车;Data Source=WENDY-PC",
"","",adModeUnknown);///连接数据库
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
void CParaCal::DatabaseDisconnect()
{
::CoUninitialize();
m_pRecordset->Close();
}
BOOL CParaCal::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
for(int i=0;i<8;i++)
{
m_pCurrentset[i] = JudgeType(i);
}
CString strTemp;
((CComboBox*)GetDlgItem(IDC_COMBO_TABLE))->ResetContent(); //消除现有所有内容
for(int j=0;j<8;j++)
{
strTemp = tabletype[j].TableTypeName;
((CComboBox*)GetDlgItem(IDC_COMBO_TABLE))->InsertString(j,strTemp);
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
_RecordsetPtr CParaCal::JudgeType(int type)
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString sqlstr;
sqlstr.Format("SELECT * FROM %s",tabletype[type].TableTypeName);
HRESULT hr = m_pRecordset->Open(sqlstr.AllocSysString(),
m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pRecordset->MoveFirst();
return m_pRecordset;
}
void CParaCal::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
DatabaseDisconnect();
}
void CParaCal::ifSelect()
{
int iPos=((CComboBox*)GetDlgItem(IDC_COMBO_TABLE))->GetCurSel();
((CComboBox*)GetDlgItem(IDC_COMBO_TABLE))->GetLBText(iPos,curTableName);
for(int i=0;i<8;i++)
{
if(tabletype[i].TableTypeName == curTableName)
{
curTableIndex = i;
break;
}
}
}
void CParaCal::OnSelchangeComboTable()
{
// TODO: Add your control notification handler code here
ifSelect();
CString strTemp;
((CComboBox*)GetDlgItem(IDC_COMBO_COL))->ResetContent(); //消除现有所有内容
((CComboBox*)GetDlgItem(IDC_COMBO_COL))->EnableWindow(TRUE);
for(int j=0;j<4;j++)
{
strTemp = tabletype[curTableIndex].TableRow[j];
if(strTemp.IsEmpty()) {}//do nothing
else ((CComboBox*)GetDlgItem(IDC_COMBO_COL))->InsertString(j,strTemp);
}
}
void CParaCal::OnSelchangeComboCol()
{
// TODO: Add your control notification handler code here
int iPos=((CComboBox*)GetDlgItem(IDC_COMBO_COL))->GetCurSel();
curRowName = tabletype[curTableIndex].TableRow[iPos];
/*CString ipos;
ipos.Format("%d",iPos);
MessageBox(ipos);*/
int i = 0;
m_pCurrentset[curTableIndex]->MoveFirst();
while(!m_pCurrentset[curTableIndex]->adoEOF)
{
//cal this col's sum,max and min
TempData[i] = atof((char*)(_bstr_t)m_pCurrentset[curTableIndex]->GetCollect(_variant_t(curRowName)));
m_pCurrentset[curTableIndex]->MoveNext();
i++;
}
double tempSum = 0;
double tempMax = 0;
double tempMin = 0;
double tempAve = 0;
for(int j=0;j<i;j++)
{
tempSum += TempData[j];
}
tempMax = *max_element(TempData,TempData+i);
tempMin = *min_element(TempData,TempData+i);
CString maxString,minString,aveString;
maxString.Format("%.3f",tempMax);
minString.Format("%.3f",tempMin);
m_Max = maxString;
m_Min = minString;
tempAve = tempSum/i;
aveString.Format("%.3f",tempAve);
m_Average = aveString;
UpdateData(FALSE);
}