1 |
// FlisServerDlg.cpp : implementation file
|
2 |
//
|
3 |
|
4 |
#include "stdafx.h"
|
5 |
#include "FlisServer.h"
|
6 |
#include "FlisServerDlg.h"
|
7 |
#include ".\flisserverdlg.h"
|
8 |
#include <vector>
|
9 |
|
10 |
#ifdef _DEBUG
|
11 |
#define new DEBUG_NEW
|
12 |
#endif
|
13 |
|
14 |
|
15 |
// CAboutDlg dialog used for App About
|
16 |
|
17 |
class CAboutDlg : public CDialog
|
18 |
{
|
19 |
public:
|
20 |
CAboutDlg();
|
21 |
|
22 |
// Dialog Data
|
23 |
enum { IDD = IDD_ABOUTBOX };
|
24 |
|
25 |
protected:
|
26 |
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
|
27 |
|
28 |
// Implementation
|
29 |
protected:
|
30 |
DECLARE_MESSAGE_MAP()
|
31 |
};
|
32 |
|
33 |
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
|
34 |
{
|
35 |
}
|
36 |
|
37 |
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
|
38 |
{
|
39 |
CDialog::DoDataExchange(pDX);
|
40 |
}
|
41 |
|
42 |
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
|
43 |
END_MESSAGE_MAP()
|
44 |
|
45 |
|
46 |
// CFlisServerDlg dialog
|
47 |
|
48 |
|
49 |
|
50 |
CFlisServerDlg::CFlisServerDlg(CWnd* pParent /*=NULL*/)
|
51 |
: CDialog(CFlisServerDlg::IDD, pParent)
|
52 |
{
|
53 |
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
54 |
db = 0;
|
55 |
}
|
56 |
|
57 |
void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)
|
58 |
{
|
59 |
CDialog::DoDataExchange(pDX);
|
60 |
DDX_Control(pDX, IDC_Textwindow, m_Textwindow);
|
61 |
}
|
62 |
|
63 |
BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialog)
|
64 |
ON_WM_SYSCOMMAND()
|
65 |
ON_WM_PAINT()
|
66 |
ON_WM_QUERYDRAGICON()
|
67 |
//}}AFX_MSG_MAP
|
68 |
ON_BN_CLICKED(IDOK, OnBnClickedOk)
|
69 |
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
|
70 |
ON_BN_CLICKED(IDC_test, OnBnClickedtest)
|
71 |
END_MESSAGE_MAP()
|
72 |
|
73 |
|
74 |
// CFlisServerDlg message handlers
|
75 |
|
76 |
BOOL CFlisServerDlg::OnInitDialog()
|
77 |
{
|
78 |
CDialog::OnInitDialog();
|
79 |
|
80 |
// Add "About..." menu item to system menu.
|
81 |
|
82 |
// IDM_ABOUTBOX must be in the system command range.
|
83 |
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
84 |
ASSERT(IDM_ABOUTBOX < 0xF000);
|
85 |
|
86 |
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
87 |
if (pSysMenu != NULL)
|
88 |
{
|
89 |
CString strAboutMenu;
|
90 |
strAboutMenu.LoadString(IDS_ABOUTBOX);
|
91 |
if (!strAboutMenu.IsEmpty())
|
92 |
{
|
93 |
pSysMenu->AppendMenu(MF_SEPARATOR);
|
94 |
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
|
95 |
}
|
96 |
}
|
97 |
|
98 |
// Set the icon for this dialog. The framework does this automatically
|
99 |
// when the application's main window is not a dialog
|
100 |
SetIcon(m_hIcon, TRUE); // Set big icon
|
101 |
SetIcon(m_hIcon, FALSE); // Set small icon
|
102 |
|
103 |
// TODO: Add extra initialization here
|
104 |
StartSerial();
|
105 |
//SetPin();
|
106 |
DBConnect();
|
107 |
|
108 |
return TRUE; // return TRUE unless you set the focus to a control
|
109 |
}
|
110 |
|
111 |
void CFlisServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
112 |
{
|
113 |
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
114 |
{
|
115 |
CAboutDlg dlgAbout;
|
116 |
dlgAbout.DoModal();
|
117 |
}
|
118 |
else
|
119 |
{
|
120 |
CDialog::OnSysCommand(nID, lParam);
|
121 |
}
|
122 |
}
|
123 |
|
124 |
// If you add a minimize button to your dialog, you will need the code below
|
125 |
// to draw the icon. For MFC applications using the document/view model,
|
126 |
// this is automatically done for you by the framework.
|
127 |
|
128 |
void CFlisServerDlg::OnPaint()
|
129 |
{
|
130 |
if (IsIconic())
|
131 |
{
|
132 |
CPaintDC dc(this); // device context for painting
|
133 |
|
134 |
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
135 |
|
136 |
// Center icon in client rectangle
|
137 |
int cxIcon = GetSystemMetrics(SM_CXICON);
|
138 |
int cyIcon = GetSystemMetrics(SM_CYICON);
|
139 |
CRect rect;
|
140 |
GetClientRect(&rect);
|
141 |
int x = (rect.Width() - cxIcon + 1) / 2;
|
142 |
int y = (rect.Height() - cyIcon + 1) / 2;
|
143 |
|
144 |
// Draw the icon
|
145 |
dc.DrawIcon(x, y, m_hIcon);
|
146 |
}
|
147 |
else
|
148 |
{
|
149 |
CDialog::OnPaint();
|
150 |
}
|
151 |
}
|
152 |
|
153 |
// The system calls this function to obtain the cursor to display while the user drags
|
154 |
// the minimized window.
|
155 |
HCURSOR CFlisServerDlg::OnQueryDragIcon()
|
156 |
{
|
157 |
return static_cast<HCURSOR>(m_hIcon);
|
158 |
}
|
159 |
int CFlisServerDlg::StartSerial(void)
|
160 |
{
|
161 |
int Baud;
|
162 |
CString SerialPort = "COM3";
|
163 |
|
164 |
Baud = 1200;
|
165 |
if( Serial.isOpen() ){
|
166 |
Serial.close();
|
167 |
Serial.open( SerialPort, Baud );
|
168 |
}
|
169 |
else {
|
170 |
Serial.open( SerialPort, Baud );
|
171 |
}
|
172 |
|
173 |
|
174 |
return 0;
|
175 |
}
|
176 |
std::vector<unsigned char> CFlisServerDlg::readFrame()
|
177 |
{
|
178 |
std::vector<unsigned char> buf;
|
179 |
while(Serial.getComstat().cbInQue > 0)
|
180 |
{
|
181 |
unsigned char data = Serial.readByte();
|
182 |
|
183 |
buf.push_back(data);
|
184 |
}
|
185 |
return buf;
|
186 |
}
|
187 |
void CFlisServerDlg::OnBnClickedOk()
|
188 |
{
|
189 |
// TODO: Add your control notification handler code here
|
190 |
OnOK();
|
191 |
if( Serial.isOpen() ){
|
192 |
Serial.close();
|
193 |
}
|
194 |
|
195 |
if (db != 0)
|
196 |
{
|
197 |
//db->Close();
|
198 |
delete db;
|
199 |
db=0;
|
200 |
}
|
201 |
}
|
202 |
|
203 |
void CFlisServerDlg::OnBnClickedCancel()
|
204 |
{
|
205 |
// TODO: Add your control notification handler code here
|
206 |
OnCancel();
|
207 |
if( Serial.isOpen() ){
|
208 |
Serial.close();
|
209 |
}
|
210 |
|
211 |
if (db != 0)
|
212 |
{
|
213 |
db->Close();
|
214 |
delete db;
|
215 |
db = 0;
|
216 |
}
|
217 |
}
|
218 |
|
219 |
void CFlisServerDlg::OnBnClickedtest()
|
220 |
{
|
221 |
// TODO: Add your control notification handler code here
|
222 |
CString tekst;
|
223 |
std::vector<unsigned char> data;
|
224 |
data.push_back('a');
|
225 |
data.push_back('t');
|
226 |
data.push_back('+');
|
227 |
data.push_back('c');
|
228 |
data.push_back('m');
|
229 |
data.push_back('g');
|
230 |
data.push_back('r');
|
231 |
data.push_back('=');
|
232 |
data.push_back('1');
|
233 |
|
234 |
writeFrame(data);
|
235 |
ReadSms();
|
236 |
}
|
237 |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
|
238 |
{
|
239 |
for (int i=0; i<data.size(); i++)
|
240 |
{
|
241 |
Serial.writeByte( data[i] );
|
242 |
Sleep(5);
|
243 |
}
|
244 |
Serial.writeByte(0x0D);
|
245 |
Sleep(100);
|
246 |
|
247 |
}
|
248 |
int CFlisServerDlg::SetPin(void)
|
249 |
{
|
250 |
CString tekst;
|
251 |
std::vector<unsigned char> data;
|
252 |
data.push_back('a');
|
253 |
data.push_back('t');
|
254 |
data.push_back('+');
|
255 |
data.push_back('c');
|
256 |
data.push_back('p');
|
257 |
data.push_back('i');
|
258 |
data.push_back('n');
|
259 |
data.push_back('=');
|
260 |
data.push_back('2');
|
261 |
data.push_back('5');
|
262 |
data.push_back('9');
|
263 |
data.push_back('5');
|
264 |
|
265 |
writeFrame(data);
|
266 |
Sleep(750);
|
267 |
if(Serial.getComstat().cbInQue > 0)
|
268 |
{
|
269 |
std::vector<unsigned char> answer = readFrame();
|
270 |
Sleep(50);
|
271 |
char array1[25];
|
272 |
int i;
|
273 |
for (int i=0; i<answer.size(); i++)
|
274 |
{
|
275 |
array1[i] = answer[i];
|
276 |
}
|
277 |
|
278 |
for (int i=0; i<answer.size(); i++)
|
279 |
{
|
280 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
|
281 |
{
|
282 |
tekst.AppendChar(array1[i]);
|
283 |
}
|
284 |
}
|
285 |
m_Textwindow.SetWindowText(tekst);
|
286 |
}
|
287 |
return 0;
|
288 |
}
|
289 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
|
290 |
{
|
291 |
for (int i=0; i<data.size(); i++)
|
292 |
{
|
293 |
Serial.writeByte( data[i] );
|
294 |
Sleep(5);
|
295 |
}
|
296 |
Serial.writeByte(0x1A);
|
297 |
Sleep(100);
|
298 |
|
299 |
}
|
300 |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)
|
301 |
{
|
302 |
for (int i=0; i<data.size(); i++)
|
303 |
{
|
304 |
Serial.writeByte( data[i] );
|
305 |
Sleep(5);
|
306 |
}
|
307 |
Serial.writeByte(0x0D);
|
308 |
Sleep(100);
|
309 |
|
310 |
}
|
311 |
void CFlisServerDlg::DBConnect()
|
312 |
{
|
313 |
db = new CDatabase();
|
314 |
|
315 |
CString dsn;
|
316 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr");
|
317 |
//db->OpenEx(dsn, CDatabase::noOdbcDialog);
|
318 |
|
319 |
}
|
320 |
void CFlisServerDlg::DBRead()
|
321 |
{
|
322 |
|
323 |
}
|
324 |
void CFlisServerDlg::ReadSms()
|
325 |
{
|
326 |
CString tekst, oldtekst;
|
327 |
Sleep(950);
|
328 |
if(Serial.getComstat().cbInQue > 0)
|
329 |
{
|
330 |
std::vector<unsigned char> answer = readFrame();
|
331 |
Sleep(50);
|
332 |
char array1[250];
|
333 |
int i;
|
334 |
for (int i=0; i<answer.size(); i++)
|
335 |
{
|
336 |
array1[i] = answer[i];
|
337 |
}
|
338 |
|
339 |
for (int i=0; i<answer.size(); i++)
|
340 |
{
|
341 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
|
342 |
{
|
343 |
tekst.AppendChar(array1[i]);
|
344 |
}
|
345 |
}
|
346 |
|
347 |
m_Textwindow.GetWindowText(oldtekst);
|
348 |
oldtekst.Append("\r\n");
|
349 |
oldtekst.Append(tekst);
|
350 |
m_Textwindow.SetWindowText(oldtekst);
|
351 |
SmsSplit(tekst);
|
352 |
}
|
353 |
}
|
354 |
void CFlisServerDlg::SmsSplit(CString data)
|
355 |
{
|
356 |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
|
357 |
char CharData[150];
|
358 |
strcpy(CharData,data);
|
359 |
|
360 |
int s=22;
|
361 |
for (int i=0; i<=7; i++)
|
362 |
{
|
363 |
TlfNr.AppendChar(CharData[s]);
|
364 |
s++;
|
365 |
}
|
366 |
|
367 |
for (int s=55; s<=(data.GetLength()-3); s++)
|
368 |
{
|
369 |
FyrData.AppendChar(CharData[s]);
|
370 |
}
|
371 |
FyrData.Append(":");
|
372 |
|
373 |
SmsCount = Splitter(FyrData);
|
374 |
Temper = Splitter(FyrData);
|
375 |
Flamme = Splitter(FyrData);
|
376 |
Flis = Splitter(FyrData);
|
377 |
FremFejl = Splitter(FyrData);
|
378 |
PowerFail = Splitter(FyrData);
|
379 |
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
380 |
///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
|
381 |
m_Textwindow.GetWindowText(oldtekst);
|
382 |
oldtekst.Append("\r\n");
|
383 |
oldtekst.Append("På næste linie kommer SmsCount. \r\n");
|
384 |
oldtekst.Append(SmsCount);
|
385 |
oldtekst.Append("\r\n");
|
386 |
oldtekst.Append("På næste linie kommer Temper. \r\n");
|
387 |
oldtekst.Append(Temper);
|
388 |
oldtekst.Append("\r\n");
|
389 |
oldtekst.Append("På næste linie kommer Flamme. \r\n");
|
390 |
oldtekst.Append(Flamme);
|
391 |
oldtekst.Append("\r\n");
|
392 |
oldtekst.Append("På næste linie kommer Flis. \r\n");
|
393 |
oldtekst.Append(Flis);
|
394 |
oldtekst.Append("\r\n");
|
395 |
oldtekst.Append("På næste linie kommer FremFejl. \r\n");
|
396 |
oldtekst.Append(FremFejl);
|
397 |
oldtekst.Append("\r\n");
|
398 |
oldtekst.Append("På næste linie kommer PowerFail. \r\n");
|
399 |
oldtekst.Append(PowerFail);
|
400 |
m_Textwindow.SetWindowText(oldtekst);
|
401 |
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
402 |
///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
|
403 |
}
|
404 |
CString CFlisServerDlg::Splitter(CString& fyrdata)
|
405 |
{
|
406 |
CString Output;
|
407 |
|
408 |
int pos = fyrdata.Find(':',0);
|
409 |
if (pos != -1)
|
410 |
{
|
411 |
Output = fyrdata.Left(pos);
|
412 |
fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
|
413 |
}
|
414 |
return Output;
|
415 |
} |