4 |
#include "stdafx.h" |
#include "stdafx.h" |
5 |
#include "FlisServer.h" |
#include "FlisServer.h" |
6 |
#include "FlisServerDlg.h" |
#include "FlisServerDlg.h" |
|
#include ".\flisserverdlg.h" |
|
7 |
#include <vector> |
#include <vector> |
8 |
|
|
9 |
#ifdef _DEBUG |
#ifdef _DEBUG |
50 |
: CDialog(CFlisServerDlg::IDD, pParent) |
: CDialog(CFlisServerDlg::IDD, pParent) |
51 |
{ |
{ |
52 |
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); |
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); |
|
db = 0; |
|
53 |
} |
} |
54 |
|
|
55 |
void CFlisServerDlg::DoDataExchange(CDataExchange* pDX) |
void CFlisServerDlg::DoDataExchange(CDataExchange* pDX) |
62 |
ON_WM_SYSCOMMAND() |
ON_WM_SYSCOMMAND() |
63 |
ON_WM_PAINT() |
ON_WM_PAINT() |
64 |
ON_WM_QUERYDRAGICON() |
ON_WM_QUERYDRAGICON() |
65 |
|
ON_MESSAGE(UWM_MYMESSAGE, OnShowString) |
66 |
//}}AFX_MSG_MAP |
//}}AFX_MSG_MAP |
|
ON_BN_CLICKED(IDOK, OnBnClickedOk) |
|
|
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel) |
|
67 |
ON_BN_CLICKED(IDC_test, OnBnClickedtest) |
ON_BN_CLICKED(IDC_test, OnBnClickedtest) |
68 |
|
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
69 |
|
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) |
70 |
END_MESSAGE_MAP() |
END_MESSAGE_MAP() |
71 |
|
|
72 |
|
|
101 |
|
|
102 |
// TODO: Add extra initialization here |
// TODO: Add extra initialization here |
103 |
StartSerial(); |
StartSerial(); |
|
//SetPin(); |
|
104 |
DBConnect(); |
DBConnect(); |
105 |
|
continueThread = true; |
106 |
|
//AfxBeginThread(MyMainThread,0,THREAD_PRIORITY_NORMAL,0,0,NULL); |
107 |
|
|
108 |
return TRUE; // return TRUE unless you set the focus to a control |
return TRUE; // return TRUE unless you set the focus to a control |
109 |
} |
} |
184 |
} |
} |
185 |
return buf; |
return buf; |
186 |
} |
} |
187 |
void CFlisServerDlg::OnBnClickedOk() |
void CFlisServerDlg::OnBnClickedtest() |
|
{ |
|
|
// TODO: Add your control notification handler code here |
|
|
OnOK(); |
|
|
if( Serial.isOpen() ){ |
|
|
Serial.close(); |
|
|
} |
|
|
|
|
|
if (db != 0) |
|
|
{ |
|
|
//db->Close(); |
|
|
delete db; |
|
|
db=0; |
|
|
} |
|
|
} |
|
|
|
|
|
void CFlisServerDlg::OnBnClickedCancel() |
|
188 |
{ |
{ |
189 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
|
OnCancel(); |
|
|
if( Serial.isOpen() ){ |
|
|
Serial.close(); |
|
|
} |
|
190 |
|
|
191 |
if (db != 0) |
/* |
192 |
|
////////////////DBRead stuff////////////////// |
193 |
|
CString testdata; |
194 |
|
std::vector<Commands> data; |
195 |
|
data = DBRead(); |
196 |
|
for (int i=0; i<data.size(); i++) |
197 |
{ |
{ |
198 |
db->Close(); |
testdata.Append(data[i].IDnr); |
199 |
delete db; |
testdata.Append(data[i].CommandID); |
200 |
db = 0; |
testdata.Append(data[i].InstallationsID); |
201 |
} |
} |
202 |
} |
m_Textwindow.SetWindowText(testdata); |
203 |
|
////////////////DBRead stuff////////////////// |
204 |
void CFlisServerDlg::OnBnClickedtest() |
*/ |
205 |
{ |
/* |
206 |
// TODO: Add your control notification handler code here |
///////////////Send sms stuff///////////////// |
207 |
CString tekst; |
vector<unsigned char> tlfnr; |
208 |
std::vector<unsigned char> data; |
vector<unsigned char> smsdata; |
209 |
data.push_back('a'); |
tlfnr.push_back('2'); |
210 |
data.push_back('t'); |
tlfnr.push_back('9'); |
211 |
data.push_back('+'); |
tlfnr.push_back('7'); |
212 |
data.push_back('c'); |
tlfnr.push_back('2'); |
213 |
data.push_back('m'); |
tlfnr.push_back('2'); |
214 |
data.push_back('g'); |
tlfnr.push_back('6'); |
215 |
data.push_back('r'); |
tlfnr.push_back('0'); |
216 |
data.push_back('='); |
tlfnr.push_back('3'); |
217 |
data.push_back('1'); |
|
218 |
|
SendSmsHead(tlfnr); |
219 |
|
|
220 |
|
//35:29860132:75 <--- demo pakke; |
221 |
|
smsdata.push_back('3'); |
222 |
|
smsdata.push_back('5'); |
223 |
|
smsdata.push_back(':'); |
224 |
|
smsdata.push_back('2'); |
225 |
|
smsdata.push_back('9'); |
226 |
|
smsdata.push_back('8'); |
227 |
|
smsdata.push_back('6'); |
228 |
|
smsdata.push_back('0'); |
229 |
|
smsdata.push_back('1'); |
230 |
|
smsdata.push_back('3'); |
231 |
|
smsdata.push_back('2'); |
232 |
|
smsdata.push_back(':'); |
233 |
|
smsdata.push_back('7'); |
234 |
|
smsdata.push_back('5'); |
235 |
|
|
236 |
|
SendSmsData(smsdata); |
237 |
|
///////////////Send sms stuff///////////////// |
238 |
|
*/ |
239 |
|
/* |
240 |
|
////////////Start Tråd//////////////////////// |
241 |
|
MyThread *t = new MyThread(AfxGetMainWnd()); |
242 |
|
t->start(); |
243 |
|
////////////Start Tråd//////////////////////// |
244 |
|
*/ |
245 |
|
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
246 |
|
|
|
writeFrame(data); |
|
|
ReadSms(); |
|
247 |
} |
} |
248 |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
249 |
{ |
{ |
256 |
Sleep(100); |
Sleep(100); |
257 |
|
|
258 |
} |
} |
259 |
int CFlisServerDlg::SetPin(void) |
void CFlisServerDlg::SetPin() |
260 |
{ |
{ |
261 |
CString tekst; |
CString tekst; |
262 |
std::vector<unsigned char> data; |
std::vector<unsigned char> data; |
295 |
} |
} |
296 |
m_Textwindow.SetWindowText(tekst); |
m_Textwindow.SetWindowText(tekst); |
297 |
} |
} |
|
return 0; |
|
298 |
} |
} |
299 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
300 |
{ |
{ |
301 |
|
|
302 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<data.size(); i++) |
303 |
{ |
{ |
304 |
Serial.writeByte( data[i] ); |
Serial.writeByte( data[i] ); |
305 |
Sleep(5); |
Sleep(5); |
306 |
} |
} |
307 |
Serial.writeByte(0x1A); |
Serial.writeByte(0x1A); |
308 |
Sleep(100); |
Sleep(3000); |
309 |
|
if(Serial.getComstat().cbInQue > 0) |
310 |
|
{ |
311 |
|
CString tekst; |
312 |
|
std::vector<unsigned char> answer = readFrame(); |
313 |
|
Sleep(50); |
314 |
|
char array1[25]; |
315 |
|
int i; |
316 |
|
for (int i=0; i<answer.size(); i++) |
317 |
|
{ |
318 |
|
array1[i] = answer[i]; |
319 |
|
} |
320 |
|
|
321 |
|
for (int i=0; i<answer.size(); i++) |
322 |
|
{ |
323 |
|
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
324 |
|
{ |
325 |
|
tekst.AppendChar(array1[i]); |
326 |
|
} |
327 |
|
} |
328 |
|
m_Textwindow.SetWindowText(tekst); |
329 |
|
} |
330 |
|
|
331 |
} |
} |
332 |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data) |
333 |
{ |
{ |
334 |
|
vector<unsigned char> atcommand; |
335 |
|
atcommand.push_back('a'); |
336 |
|
atcommand.push_back('t'); |
337 |
|
atcommand.push_back('+'); |
338 |
|
atcommand.push_back('c'); |
339 |
|
atcommand.push_back('m'); |
340 |
|
atcommand.push_back('g'); |
341 |
|
atcommand.push_back('s'); |
342 |
|
atcommand.push_back('='); |
343 |
|
atcommand.push_back('"'); |
344 |
|
int s = (atcommand.size() -1 ); |
345 |
|
|
346 |
|
for (int i=0; i<(atcommand.size()); i++) |
347 |
|
{ |
348 |
|
Serial.writeByte( atcommand[i] ); |
349 |
|
Sleep(5); |
350 |
|
} |
351 |
|
|
352 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<data.size(); i++) |
353 |
{ |
{ |
354 |
Serial.writeByte( data[i] ); |
Serial.writeByte( data[i] ); |
355 |
Sleep(5); |
Sleep(5); |
356 |
} |
} |
357 |
|
Serial.writeByte(atcommand[s]); |
358 |
Serial.writeByte(0x0D); |
Serial.writeByte(0x0D); |
359 |
Sleep(100); |
Sleep(250); |
|
|
|
360 |
} |
} |
361 |
void CFlisServerDlg::DBConnect() |
void CFlisServerDlg::DBConnect() |
362 |
{ |
{ |
|
db = new CDatabase(); |
|
|
|
|
363 |
CString dsn; |
CString dsn; |
364 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr"); |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); |
365 |
//db->OpenEx(dsn, CDatabase::noOdbcDialog); |
//dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server |
366 |
|
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
367 |
} |
} |
368 |
void CFlisServerDlg::DBRead() |
vector<Commands> CFlisServerDlg::DBRead(void) |
369 |
{ |
{ |
370 |
|
vector<Commands> buffer; |
371 |
|
|
372 |
|
CString SQL, IDnr, CommandID, InstallationsID; |
373 |
|
SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;"; |
374 |
|
CRecordset rs(&db); |
375 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
376 |
|
if (rs.GetRecordCount()>0) |
377 |
|
{ |
378 |
|
rs.MoveFirst(); |
379 |
|
while(!rs.IsEOF()) |
380 |
|
{ |
381 |
|
Commands Mycom; |
382 |
|
rs.GetFieldValue((short)0, IDnr); |
383 |
|
rs.GetFieldValue(3, CommandID); |
384 |
|
rs.GetFieldValue(4, InstallationsID); |
385 |
|
|
386 |
|
Mycom.IDnr = IDnr; |
387 |
|
Mycom.CommandID = CommandID; |
388 |
|
Mycom.InstallationsID = InstallationsID; |
389 |
|
|
390 |
|
buffer.push_back(Mycom); |
391 |
|
rs.MoveNext(); |
392 |
|
} |
393 |
|
} |
394 |
|
rs.Close(); |
395 |
|
return buffer; |
396 |
} |
} |
397 |
void CFlisServerDlg::ReadSms() |
void CFlisServerDlg::ReadSms() |
398 |
{ |
{ |
470 |
oldtekst.Append("\r\n"); |
oldtekst.Append("\r\n"); |
471 |
oldtekst.Append("På næste linie kommer PowerFail. \r\n"); |
oldtekst.Append("På næste linie kommer PowerFail. \r\n"); |
472 |
oldtekst.Append(PowerFail); |
oldtekst.Append(PowerFail); |
473 |
|
oldtekst.Append("\r\n"); |
474 |
|
oldtekst.Append("På næste linie kommer TlfNR. \r\n"); |
475 |
|
oldtekst.Append(TlfNr); |
476 |
m_Textwindow.SetWindowText(oldtekst); |
m_Textwindow.SetWindowText(oldtekst); |
477 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
478 |
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
488 |
fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1); |
fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1); |
489 |
} |
} |
490 |
return Output; |
return Output; |
491 |
|
} |
492 |
|
void CFlisServerDlg::OnBnClickedClose() |
493 |
|
{ |
494 |
|
// TODO: Add your control notification handler code here |
495 |
|
continueThread = false; |
496 |
|
if( Serial.isOpen() ) |
497 |
|
{ |
498 |
|
Serial.close(); |
499 |
|
} |
500 |
|
|
501 |
|
if(db.IsOpen()) |
502 |
|
{ |
503 |
|
db.Close(); |
504 |
|
} |
505 |
|
DeleteSms(); |
506 |
|
OnOK(); |
507 |
|
} |
508 |
|
|
509 |
|
void CFlisServerDlg::OnBnClickedGsmpin() |
510 |
|
{ |
511 |
|
// TODO: Add your control notification handler code here |
512 |
|
SetPin(); |
513 |
|
Sleep(5000); |
514 |
|
} |
515 |
|
void CFlisServerDlg::DeleteSms() |
516 |
|
{ |
517 |
|
vector<unsigned char> atcommand; |
518 |
|
atcommand.push_back('a'); |
519 |
|
atcommand.push_back('t'); |
520 |
|
atcommand.push_back('+'); |
521 |
|
atcommand.push_back('c'); |
522 |
|
atcommand.push_back('m'); |
523 |
|
atcommand.push_back('g'); |
524 |
|
atcommand.push_back('d'); |
525 |
|
atcommand.push_back('='); |
526 |
|
atcommand.push_back('1'); |
527 |
|
atcommand.push_back(','); |
528 |
|
atcommand.push_back('3'); |
529 |
|
|
530 |
|
writeFrame(atcommand); |
531 |
|
Sleep(500); |
532 |
|
if(Serial.getComstat().cbInQue > 0) |
533 |
|
{ |
534 |
|
std::vector<unsigned char> answer = readFrame(); |
535 |
|
Sleep(50); |
536 |
|
CString tekst, oldtekst; |
537 |
|
char array1[250]; |
538 |
|
int i; |
539 |
|
for (int i=0; i<answer.size(); i++) |
540 |
|
{ |
541 |
|
array1[i] = answer[i]; |
542 |
|
} |
543 |
|
|
544 |
|
for (int i=0; i<answer.size(); i++) |
545 |
|
{ |
546 |
|
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
547 |
|
{ |
548 |
|
tekst.AppendChar(array1[i]); |
549 |
|
} |
550 |
|
} |
551 |
|
if(tekst == "OK") |
552 |
|
{ |
553 |
|
m_Textwindow.GetWindowText(oldtekst); |
554 |
|
oldtekst.Append("\r\n"); |
555 |
|
oldtekst.Append("Old Sms messages deleted"); |
556 |
|
m_Textwindow.SetWindowText(oldtekst); |
557 |
|
} |
558 |
|
else |
559 |
|
{ |
560 |
|
m_Textwindow.GetWindowText(oldtekst); |
561 |
|
oldtekst.Append("\r\n"); |
562 |
|
oldtekst.Append(tekst); |
563 |
|
m_Textwindow.SetWindowText(oldtekst); |
564 |
|
} |
565 |
|
} |
566 |
|
} |
567 |
|
LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam) |
568 |
|
{ |
569 |
|
CString *s = (CString*) lParam; |
570 |
|
GetDlgItem(IDC_Textwindow)->SetWindowText(*s); |
571 |
|
|
572 |
|
delete s; |
573 |
|
return 0; |
574 |
|
|
575 |
|
}UINT threadWrapper(LPVOID thread) |
576 |
|
{ |
577 |
|
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
578 |
|
t->runthread(); |
579 |
|
return 0; |
580 |
|
} |
581 |
|
|
582 |
|
void CFlisServerDlg::startthread() |
583 |
|
{ |
584 |
|
AfxBeginThread(threadWrapper, (LPVOID) this); |
585 |
|
} |
586 |
|
|
587 |
|
void CFlisServerDlg::runthread() |
588 |
|
{ |
589 |
|
int count = 0; |
590 |
|
while (continueThread = true) |
591 |
|
{ |
592 |
|
Reader(); |
593 |
|
} |
594 |
|
} |
595 |
|
void CFlisServerDlg::Reader() |
596 |
|
{ |
597 |
|
int ResetSms = 0; |
598 |
|
while(1) |
599 |
|
{ |
600 |
|
if(Serial.getComstat().cbInQue > 0) |
601 |
|
{ |
602 |
|
std::vector<unsigned char> answer = readFrame(); |
603 |
|
Sleep(500); |
604 |
|
CString tekst, oldtekst; |
605 |
|
int lol; |
606 |
|
char array1[250]; |
607 |
|
int i; |
608 |
|
for (int i=0; i<answer.size(); i++) |
609 |
|
{ |
610 |
|
array1[i] = answer[i]; |
611 |
|
} |
612 |
|
|
613 |
|
for (int i=0; i<answer.size(); i++) |
614 |
|
{ |
615 |
|
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
616 |
|
{ |
617 |
|
tekst.AppendChar(array1[i]); |
618 |
|
} |
619 |
|
} |
620 |
|
tekst.Append(":"); |
621 |
|
|
622 |
|
CString command; |
623 |
|
bool plus; |
624 |
|
int pos = tekst.Find('+',0); |
625 |
|
if (pos != -1) |
626 |
|
{ |
627 |
|
plus = true; |
628 |
|
tekst = tekst.Right( tekst.GetLength() - pos -1); |
629 |
|
pos = tekst.Find(':'); |
630 |
|
command = tekst.Left(pos); |
631 |
|
tekst = tekst.Right( tekst.GetLength() - pos -1); |
632 |
|
} |
633 |
|
|
634 |
|
|
635 |
|
if(tekst == "OK") |
636 |
|
{ |
637 |
|
m_Textwindow.GetWindowText(oldtekst); |
638 |
|
oldtekst.Append("\r\n"); |
639 |
|
oldtekst.Append("OK tekst modtaget"); |
640 |
|
m_Textwindow.SetWindowText(oldtekst); |
641 |
|
} |
642 |
|
else if (tekst == "error") |
643 |
|
{ |
644 |
|
m_Textwindow.GetWindowText(oldtekst); |
645 |
|
oldtekst.Append("\r\n"); |
646 |
|
oldtekst.Append("error tekst"); |
647 |
|
oldtekst.Append(tekst); |
648 |
|
m_Textwindow.SetWindowText(oldtekst); |
649 |
|
} |
650 |
|
else if (plus = true) |
651 |
|
{ |
652 |
|
if (command = "cmti") |
653 |
|
{ |
654 |
|
CString smscount; |
655 |
|
int pos = tekst.Find(',',0); |
656 |
|
if (pos != -1) |
657 |
|
{ |
658 |
|
smscount = tekst.Right( tekst.GetLength() - pos -1); |
659 |
|
smscount.Remove(':'); |
660 |
|
ResetSms++; |
661 |
|
} |
662 |
|
////////////////Read sms ting////////////////// |
663 |
|
std::vector<unsigned char> data; |
664 |
|
data.push_back('a'); |
665 |
|
data.push_back('t'); |
666 |
|
data.push_back('+'); |
667 |
|
data.push_back('c'); |
668 |
|
data.push_back('m'); |
669 |
|
data.push_back('g'); |
670 |
|
data.push_back('r'); |
671 |
|
data.push_back('='); |
672 |
|
|
673 |
|
for (int i=0; i< smscount.GetLength(); i++) |
674 |
|
{ |
675 |
|
data.push_back(smscount[i]); |
676 |
|
} |
677 |
|
|
678 |
|
writeFrame(data); |
679 |
|
ReadSms(); |
680 |
|
|
681 |
|
} |
682 |
|
} |
683 |
|
if(ResetSms >= 50) |
684 |
|
{ |
685 |
|
DeleteSms(); |
686 |
|
} |
687 |
|
|
688 |
|
|
689 |
|
} |
690 |
|
} |
691 |
} |
} |