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 |
}
|
55 |
|
56 |
void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)
|
57 |
{
|
58 |
CDialog::DoDataExchange(pDX);
|
59 |
DDX_Control(pDX, IDC_Textwindow, m_Textwindow);
|
60 |
}
|
61 |
|
62 |
BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialog)
|
63 |
ON_WM_SYSCOMMAND()
|
64 |
ON_WM_PAINT()
|
65 |
ON_WM_QUERYDRAGICON()
|
66 |
//}}AFX_MSG_MAP
|
67 |
ON_BN_CLICKED(IDC_test, OnBnClickedtest)
|
68 |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
|
69 |
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
|
70 |
END_MESSAGE_MAP()
|
71 |
|
72 |
|
73 |
// CFlisServerDlg message handlers
|
74 |
|
75 |
BOOL CFlisServerDlg::OnInitDialog()
|
76 |
{
|
77 |
CDialog::OnInitDialog();
|
78 |
|
79 |
// Add "About..." menu item to system menu.
|
80 |
|
81 |
// IDM_ABOUTBOX must be in the system command range.
|
82 |
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
83 |
ASSERT(IDM_ABOUTBOX < 0xF000);
|
84 |
|
85 |
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
86 |
if (pSysMenu != NULL)
|
87 |
{
|
88 |
CString strAboutMenu;
|
89 |
strAboutMenu.LoadString(IDS_ABOUTBOX);
|
90 |
if (!strAboutMenu.IsEmpty())
|
91 |
{
|
92 |
pSysMenu->AppendMenu(MF_SEPARATOR);
|
93 |
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
|
94 |
}
|
95 |
}
|
96 |
|
97 |
// Set the icon for this dialog. The framework does this automatically
|
98 |
// when the application's main window is not a dialog
|
99 |
SetIcon(m_hIcon, TRUE); // Set big icon
|
100 |
SetIcon(m_hIcon, FALSE); // Set small icon
|
101 |
|
102 |
// TODO: Add extra initialization here
|
103 |
StartSerial();
|
104 |
DBConnect();
|
105 |
|
106 |
return TRUE; // return TRUE unless you set the focus to a control
|
107 |
}
|
108 |
|
109 |
void CFlisServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
110 |
{
|
111 |
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
112 |
{
|
113 |
CAboutDlg dlgAbout;
|
114 |
dlgAbout.DoModal();
|
115 |
}
|
116 |
else
|
117 |
{
|
118 |
CDialog::OnSysCommand(nID, lParam);
|
119 |
}
|
120 |
}
|
121 |
|
122 |
// If you add a minimize button to your dialog, you will need the code below
|
123 |
// to draw the icon. For MFC applications using the document/view model,
|
124 |
// this is automatically done for you by the framework.
|
125 |
|
126 |
void CFlisServerDlg::OnPaint()
|
127 |
{
|
128 |
if (IsIconic())
|
129 |
{
|
130 |
CPaintDC dc(this); // device context for painting
|
131 |
|
132 |
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
133 |
|
134 |
// Center icon in client rectangle
|
135 |
int cxIcon = GetSystemMetrics(SM_CXICON);
|
136 |
int cyIcon = GetSystemMetrics(SM_CYICON);
|
137 |
CRect rect;
|
138 |
GetClientRect(&rect);
|
139 |
int x = (rect.Width() - cxIcon + 1) / 2;
|
140 |
int y = (rect.Height() - cyIcon + 1) / 2;
|
141 |
|
142 |
// Draw the icon
|
143 |
dc.DrawIcon(x, y, m_hIcon);
|
144 |
}
|
145 |
else
|
146 |
{
|
147 |
CDialog::OnPaint();
|
148 |
}
|
149 |
}
|
150 |
|
151 |
// The system calls this function to obtain the cursor to display while the user drags
|
152 |
// the minimized window.
|
153 |
HCURSOR CFlisServerDlg::OnQueryDragIcon()
|
154 |
{
|
155 |
return static_cast<HCURSOR>(m_hIcon);
|
156 |
}
|
157 |
int CFlisServerDlg::StartSerial(void)
|
158 |
{
|
159 |
int Baud;
|
160 |
CString SerialPort = "COM3";
|
161 |
|
162 |
Baud = 1200;
|
163 |
if( Serial.isOpen() ){
|
164 |
Serial.close();
|
165 |
Serial.open( SerialPort, Baud );
|
166 |
}
|
167 |
else {
|
168 |
Serial.open( SerialPort, Baud );
|
169 |
}
|
170 |
|
171 |
|
172 |
return 0;
|
173 |
}
|
174 |
std::vector<unsigned char> CFlisServerDlg::readFrame()
|
175 |
{
|
176 |
std::vector<unsigned char> buf;
|
177 |
while(Serial.getComstat().cbInQue > 0)
|
178 |
{
|
179 |
unsigned char data = Serial.readByte();
|
180 |
|
181 |
buf.push_back(data);
|
182 |
}
|
183 |
return buf;
|
184 |
}
|
185 |
void CFlisServerDlg::OnBnClickedtest()
|
186 |
{
|
187 |
// TODO: Add your control notification handler code here
|
188 |
/*
|
189 |
////////////////Read sms ting//////////////////
|
190 |
CString tekst;
|
191 |
std::vector<unsigned char> data;
|
192 |
data.push_back('a');
|
193 |
data.push_back('t');
|
194 |
data.push_back('+');
|
195 |
data.push_back('c');
|
196 |
data.push_back('m');
|
197 |
data.push_back('g');
|
198 |
data.push_back('r');
|
199 |
data.push_back('=');
|
200 |
data.push_back('1');
|
201 |
|
202 |
writeFrame(data);
|
203 |
ReadSms();
|
204 |
////////////////Read sms ting//////////////////
|
205 |
*/
|
206 |
/*
|
207 |
////////////////DBRead stuff//////////////////
|
208 |
CString testdata;
|
209 |
std::vector<Commands> data;
|
210 |
data = DBRead();
|
211 |
for (int i=0; i<data.size(); i++)
|
212 |
{
|
213 |
testdata.Append(data[i].IDnr);
|
214 |
testdata.Append(data[i].CommandID);
|
215 |
testdata.Append(data[i].InstallationsID);
|
216 |
}
|
217 |
m_Textwindow.SetWindowText(testdata);
|
218 |
////////////////DBRead stuff//////////////////
|
219 |
*/
|
220 |
/*
|
221 |
///////////////Send sms stuff/////////////////
|
222 |
vector<unsigned char> tlfnr;
|
223 |
vector<unsigned char> smsdata;
|
224 |
tlfnr.push_back('2');
|
225 |
tlfnr.push_back('9');
|
226 |
tlfnr.push_back('7');
|
227 |
tlfnr.push_back('2');
|
228 |
tlfnr.push_back('2');
|
229 |
tlfnr.push_back('6');
|
230 |
tlfnr.push_back('0');
|
231 |
tlfnr.push_back('3');
|
232 |
|
233 |
SendSmsHead(tlfnr);
|
234 |
|
235 |
//35:29860132:75 <--- demo pakke;
|
236 |
smsdata.push_back('3');
|
237 |
smsdata.push_back('5');
|
238 |
smsdata.push_back(':');
|
239 |
smsdata.push_back('2');
|
240 |
smsdata.push_back('9');
|
241 |
smsdata.push_back('8');
|
242 |
smsdata.push_back('6');
|
243 |
smsdata.push_back('0');
|
244 |
smsdata.push_back('1');
|
245 |
smsdata.push_back('3');
|
246 |
smsdata.push_back('2');
|
247 |
smsdata.push_back(':');
|
248 |
smsdata.push_back('7');
|
249 |
smsdata.push_back('5');
|
250 |
|
251 |
SendSmsData(smsdata);
|
252 |
///////////////Send sms stuff/////////////////
|
253 |
*/
|
254 |
}
|
255 |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
|
256 |
{
|
257 |
for (int i=0; i<data.size(); i++)
|
258 |
{
|
259 |
Serial.writeByte( data[i] );
|
260 |
Sleep(5);
|
261 |
}
|
262 |
Serial.writeByte(0x0D);
|
263 |
Sleep(100);
|
264 |
|
265 |
}
|
266 |
int CFlisServerDlg::SetPin(void)
|
267 |
{
|
268 |
CString tekst;
|
269 |
std::vector<unsigned char> data;
|
270 |
data.push_back('a');
|
271 |
data.push_back('t');
|
272 |
data.push_back('+');
|
273 |
data.push_back('c');
|
274 |
data.push_back('p');
|
275 |
data.push_back('i');
|
276 |
data.push_back('n');
|
277 |
data.push_back('=');
|
278 |
data.push_back('2');
|
279 |
data.push_back('5');
|
280 |
data.push_back('9');
|
281 |
data.push_back('5');
|
282 |
|
283 |
writeFrame(data);
|
284 |
Sleep(750);
|
285 |
if(Serial.getComstat().cbInQue > 0)
|
286 |
{
|
287 |
std::vector<unsigned char> answer = readFrame();
|
288 |
Sleep(50);
|
289 |
char array1[25];
|
290 |
int i;
|
291 |
for (int i=0; i<answer.size(); i++)
|
292 |
{
|
293 |
array1[i] = answer[i];
|
294 |
}
|
295 |
|
296 |
for (int i=0; i<answer.size(); i++)
|
297 |
{
|
298 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
|
299 |
{
|
300 |
tekst.AppendChar(array1[i]);
|
301 |
}
|
302 |
}
|
303 |
m_Textwindow.SetWindowText(tekst);
|
304 |
}
|
305 |
return 0;
|
306 |
}
|
307 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
|
308 |
{
|
309 |
|
310 |
for (int i=0; i<data.size(); i++)
|
311 |
{
|
312 |
Serial.writeByte( data[i] );
|
313 |
Sleep(5);
|
314 |
}
|
315 |
Serial.writeByte(0x1A);
|
316 |
Sleep(3000);
|
317 |
if(Serial.getComstat().cbInQue > 0)
|
318 |
{
|
319 |
CString tekst;
|
320 |
std::vector<unsigned char> answer = readFrame();
|
321 |
Sleep(50);
|
322 |
char array1[25];
|
323 |
int i;
|
324 |
for (int i=0; i<answer.size(); i++)
|
325 |
{
|
326 |
array1[i] = answer[i];
|
327 |
}
|
328 |
|
329 |
for (int i=0; i<answer.size(); i++)
|
330 |
{
|
331 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
|
332 |
{
|
333 |
tekst.AppendChar(array1[i]);
|
334 |
}
|
335 |
}
|
336 |
m_Textwindow.SetWindowText(tekst);
|
337 |
}
|
338 |
|
339 |
}
|
340 |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)
|
341 |
{
|
342 |
vector<unsigned char> atcommand;
|
343 |
atcommand.push_back('a');
|
344 |
atcommand.push_back('t');
|
345 |
atcommand.push_back('+');
|
346 |
atcommand.push_back('c');
|
347 |
atcommand.push_back('m');
|
348 |
atcommand.push_back('g');
|
349 |
atcommand.push_back('s');
|
350 |
atcommand.push_back('=');
|
351 |
atcommand.push_back('"');
|
352 |
int s = (atcommand.size() -1 );
|
353 |
|
354 |
for (int i=0; i<(atcommand.size()); i++)
|
355 |
{
|
356 |
Serial.writeByte( atcommand[i] );
|
357 |
Sleep(5);
|
358 |
}
|
359 |
|
360 |
for (int i=0; i<data.size(); i++)
|
361 |
{
|
362 |
Serial.writeByte( data[i] );
|
363 |
Sleep(5);
|
364 |
}
|
365 |
Serial.writeByte(atcommand[s]);
|
366 |
Serial.writeByte(0x0D);
|
367 |
Sleep(250);
|
368 |
}
|
369 |
void CFlisServerDlg::DBConnect()
|
370 |
{
|
371 |
CString dsn;
|
372 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");
|
373 |
db.OpenEx(dsn, CDatabase::noOdbcDialog);
|
374 |
}
|
375 |
vector<Commands> CFlisServerDlg::DBRead(void)
|
376 |
{
|
377 |
vector<Commands> buffer;
|
378 |
|
379 |
CString SQL, IDnr, CommandID, InstallationsID;
|
380 |
SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;";
|
381 |
CRecordset rs(&db);
|
382 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
|
383 |
if (rs.GetRecordCount()>0)
|
384 |
{
|
385 |
rs.MoveFirst();
|
386 |
while(!rs.IsEOF())
|
387 |
{
|
388 |
Commands Mycom;
|
389 |
rs.GetFieldValue((short)0, IDnr);
|
390 |
rs.GetFieldValue(3, CommandID);
|
391 |
rs.GetFieldValue(4, InstallationsID);
|
392 |
|
393 |
Mycom.IDnr = IDnr;
|
394 |
Mycom.CommandID = CommandID;
|
395 |
Mycom.InstallationsID = InstallationsID;
|
396 |
|
397 |
buffer.push_back(Mycom);
|
398 |
rs.MoveNext();
|
399 |
}
|
400 |
}
|
401 |
rs.Close();
|
402 |
return buffer;
|
403 |
}
|
404 |
void CFlisServerDlg::ReadSms()
|
405 |
{
|
406 |
CString tekst, oldtekst;
|
407 |
Sleep(950);
|
408 |
if(Serial.getComstat().cbInQue > 0)
|
409 |
{
|
410 |
std::vector<unsigned char> answer = readFrame();
|
411 |
Sleep(50);
|
412 |
char array1[250];
|
413 |
int i;
|
414 |
for (int i=0; i<answer.size(); i++)
|
415 |
{
|
416 |
array1[i] = answer[i];
|
417 |
}
|
418 |
|
419 |
for (int i=0; i<answer.size(); i++)
|
420 |
{
|
421 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
|
422 |
{
|
423 |
tekst.AppendChar(array1[i]);
|
424 |
}
|
425 |
}
|
426 |
|
427 |
m_Textwindow.GetWindowText(oldtekst);
|
428 |
oldtekst.Append("\r\n");
|
429 |
oldtekst.Append(tekst);
|
430 |
m_Textwindow.SetWindowText(oldtekst);
|
431 |
SmsSplit(tekst);
|
432 |
}
|
433 |
}
|
434 |
void CFlisServerDlg::SmsSplit(CString data)
|
435 |
{
|
436 |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
|
437 |
char CharData[150];
|
438 |
strcpy(CharData,data);
|
439 |
|
440 |
int s=22;
|
441 |
for (int i=0; i<=7; i++)
|
442 |
{
|
443 |
TlfNr.AppendChar(CharData[s]);
|
444 |
s++;
|
445 |
}
|
446 |
|
447 |
for (int s=55; s<=(data.GetLength()-3); s++)
|
448 |
{
|
449 |
FyrData.AppendChar(CharData[s]);
|
450 |
}
|
451 |
FyrData.Append(":");
|
452 |
|
453 |
SmsCount = Splitter(FyrData);
|
454 |
Temper = Splitter(FyrData);
|
455 |
Flamme = Splitter(FyrData);
|
456 |
Flis = Splitter(FyrData);
|
457 |
FremFejl = Splitter(FyrData);
|
458 |
PowerFail = Splitter(FyrData);
|
459 |
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
460 |
///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
|
461 |
m_Textwindow.GetWindowText(oldtekst);
|
462 |
oldtekst.Append("\r\n");
|
463 |
oldtekst.Append("På næste linie kommer SmsCount. \r\n");
|
464 |
oldtekst.Append(SmsCount);
|
465 |
oldtekst.Append("\r\n");
|
466 |
oldtekst.Append("På næste linie kommer Temper. \r\n");
|
467 |
oldtekst.Append(Temper);
|
468 |
oldtekst.Append("\r\n");
|
469 |
oldtekst.Append("På næste linie kommer Flamme. \r\n");
|
470 |
oldtekst.Append(Flamme);
|
471 |
oldtekst.Append("\r\n");
|
472 |
oldtekst.Append("På næste linie kommer Flis. \r\n");
|
473 |
oldtekst.Append(Flis);
|
474 |
oldtekst.Append("\r\n");
|
475 |
oldtekst.Append("På næste linie kommer FremFejl. \r\n");
|
476 |
oldtekst.Append(FremFejl);
|
477 |
oldtekst.Append("\r\n");
|
478 |
oldtekst.Append("På næste linie kommer PowerFail. \r\n");
|
479 |
oldtekst.Append(PowerFail);
|
480 |
m_Textwindow.SetWindowText(oldtekst);
|
481 |
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
482 |
///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
|
483 |
}
|
484 |
CString CFlisServerDlg::Splitter(CString& fyrdata)
|
485 |
{
|
486 |
CString Output;
|
487 |
|
488 |
int pos = fyrdata.Find(':',0);
|
489 |
if (pos != -1)
|
490 |
{
|
491 |
Output = fyrdata.Left(pos);
|
492 |
fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
|
493 |
}
|
494 |
return Output;
|
495 |
}
|
496 |
void CFlisServerDlg::OnBnClickedClose()
|
497 |
{
|
498 |
// TODO: Add your control notification handler code here
|
499 |
|
500 |
if( Serial.isOpen() )
|
501 |
{
|
502 |
Serial.close();
|
503 |
}
|
504 |
|
505 |
if(db.IsOpen())
|
506 |
{
|
507 |
db.Close();
|
508 |
}
|
509 |
OnOK();
|
510 |
}
|
511 |
|
512 |
void CFlisServerDlg::OnBnClickedGsmpin()
|
513 |
{
|
514 |
// TODO: Add your control notification handler code here
|
515 |
SetPin();
|
516 |
}
|