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 |
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 |
67 |
ON_BN_CLICKED(IDC_test, OnBnClickedtest) |
ON_BN_CLICKED(IDC_test, OnBnClickedtest) |
68 |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
102 |
// TODO: Add extra initialization here |
// TODO: Add extra initialization here |
103 |
StartSerial(); |
StartSerial(); |
104 |
DBConnect(); |
DBConnect(); |
105 |
|
continueThread = 1; |
106 |
|
ResetSms = 0; |
107 |
|
/* |
108 |
|
////////////Start Tråd//////////////////////// |
109 |
|
MyThread *t = new MyThread(AfxGetMainWnd()); |
110 |
|
t->start(); |
111 |
|
////////////Start Tråd//////////////////////// |
112 |
|
*/ |
113 |
|
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
114 |
|
|
115 |
return TRUE; // return TRUE unless you set the focus to a control |
return TRUE; // return TRUE unless you set the focus to a control |
116 |
} |
} |
194 |
void CFlisServerDlg::OnBnClickedtest() |
void CFlisServerDlg::OnBnClickedtest() |
195 |
{ |
{ |
196 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
|
/* |
|
|
////////////////Read sms ting////////////////// |
|
|
CString tekst; |
|
|
std::vector<unsigned char> data; |
|
|
data.push_back('a'); |
|
|
data.push_back('t'); |
|
|
data.push_back('+'); |
|
|
data.push_back('c'); |
|
|
data.push_back('m'); |
|
|
data.push_back('g'); |
|
|
data.push_back('r'); |
|
|
data.push_back('='); |
|
|
data.push_back('1'); |
|
197 |
|
|
|
writeFrame(data); |
|
|
ReadSms(); |
|
|
////////////////Read sms ting////////////////// |
|
|
*/ |
|
|
/* |
|
|
////////////////DBRead stuff////////////////// |
|
|
CString testdata; |
|
|
std::vector<Commands> data; |
|
|
data = DBRead(); |
|
|
for (int i=0; i<data.size(); i++) |
|
|
{ |
|
|
testdata.Append(data[i].IDnr); |
|
|
testdata.Append(data[i].CommandID); |
|
|
testdata.Append(data[i].InstallationsID); |
|
|
} |
|
|
m_Textwindow.SetWindowText(testdata); |
|
|
////////////////DBRead stuff////////////////// |
|
|
*/ |
|
198 |
/* |
/* |
199 |
///////////////Send sms stuff///////////////// |
///////////////Send sms stuff///////////////// |
200 |
vector<unsigned char> tlfnr; |
vector<unsigned char> tlfnr; |
211 |
SendSmsHead(tlfnr); |
SendSmsHead(tlfnr); |
212 |
|
|
213 |
//35:29860132:75 <--- demo pakke; |
//35:29860132:75 <--- demo pakke; |
214 |
|
vector<unsigned char> smsdata; |
215 |
smsdata.push_back('3'); |
smsdata.push_back('3'); |
216 |
smsdata.push_back('5'); |
smsdata.push_back('5'); |
217 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
230 |
SendSmsData(smsdata); |
SendSmsData(smsdata); |
231 |
///////////////Send sms stuff///////////////// |
///////////////Send sms stuff///////////////// |
232 |
*/ |
*/ |
233 |
|
|
234 |
} |
} |
235 |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
236 |
{ |
{ |
243 |
Sleep(100); |
Sleep(100); |
244 |
|
|
245 |
} |
} |
246 |
int CFlisServerDlg::SetPin(void) |
void CFlisServerDlg::SetPin() |
247 |
{ |
{ |
248 |
CString tekst; |
CString tekst; |
249 |
std::vector<unsigned char> data; |
std::vector<unsigned char> data; |
261 |
data.push_back('5'); |
data.push_back('5'); |
262 |
|
|
263 |
writeFrame(data); |
writeFrame(data); |
264 |
Sleep(750); |
/* Sleep(750); |
265 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
266 |
{ |
{ |
267 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
282 |
} |
} |
283 |
m_Textwindow.SetWindowText(tekst); |
m_Textwindow.SetWindowText(tekst); |
284 |
} |
} |
285 |
return 0; |
*/ |
286 |
} |
} |
287 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
288 |
{ |
{ |
349 |
void CFlisServerDlg::DBConnect() |
void CFlisServerDlg::DBConnect() |
350 |
{ |
{ |
351 |
CString dsn; |
CString dsn; |
352 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); |
353 |
|
//dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server |
354 |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
355 |
} |
} |
356 |
vector<Commands> CFlisServerDlg::DBRead(void) |
vector<Commands> CFlisServerDlg::DBRead(void) |
418 |
char CharData[150]; |
char CharData[150]; |
419 |
strcpy(CharData,data); |
strcpy(CharData,data); |
420 |
|
|
421 |
int s=22; |
int s=24; |
422 |
for (int i=0; i<=7; i++) |
for (int i=0; i<=7; i++) |
423 |
{ |
{ |
424 |
TlfNr.AppendChar(CharData[s]); |
TlfNr.AppendChar(CharData[s]); |
425 |
s++; |
s++; |
426 |
} |
} |
427 |
|
|
428 |
for (int s=55; s<=(data.GetLength()-3); s++) |
for (int s=57; s<=(data.GetLength()-3); s++) |
429 |
{ |
{ |
430 |
FyrData.AppendChar(CharData[s]); |
FyrData.AppendChar(CharData[s]); |
431 |
} |
} |
458 |
oldtekst.Append("\r\n"); |
oldtekst.Append("\r\n"); |
459 |
oldtekst.Append("På næste linie kommer PowerFail. \r\n"); |
oldtekst.Append("På næste linie kommer PowerFail. \r\n"); |
460 |
oldtekst.Append(PowerFail); |
oldtekst.Append(PowerFail); |
461 |
|
oldtekst.Append("\r\n"); |
462 |
|
oldtekst.Append("På næste linie kommer TlfNR. \r\n"); |
463 |
|
oldtekst.Append(TlfNr); |
464 |
m_Textwindow.SetWindowText(oldtekst); |
m_Textwindow.SetWindowText(oldtekst); |
465 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
466 |
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
480 |
void CFlisServerDlg::OnBnClickedClose() |
void CFlisServerDlg::OnBnClickedClose() |
481 |
{ |
{ |
482 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
483 |
|
continueThread = 0; |
484 |
|
DeleteSms(); //<--- crasher, men gider ikke bruge flere af mine penge på at teste det med sms'er, hele close laver crash, bare ikke når jeg stepper. :S |
485 |
|
|
486 |
|
Sleep(500); |
487 |
if( Serial.isOpen() ) |
if( Serial.isOpen() ) |
488 |
{ |
{ |
489 |
Serial.close(); |
Serial.close(); |
493 |
{ |
{ |
494 |
db.Close(); |
db.Close(); |
495 |
} |
} |
496 |
|
|
497 |
OnOK(); |
OnOK(); |
498 |
|
|
499 |
} |
} |
500 |
|
|
501 |
void CFlisServerDlg::OnBnClickedGsmpin() |
void CFlisServerDlg::OnBnClickedGsmpin() |
502 |
{ |
{ |
503 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
504 |
SetPin(); |
SetPin(); |
505 |
|
//Sleep(5000); |
506 |
|
} |
507 |
|
void CFlisServerDlg::DeleteSms() |
508 |
|
{ |
509 |
|
vector<unsigned char> atcommand; |
510 |
|
atcommand.push_back('a'); |
511 |
|
atcommand.push_back('t'); |
512 |
|
atcommand.push_back('+'); |
513 |
|
atcommand.push_back('c'); |
514 |
|
atcommand.push_back('m'); |
515 |
|
atcommand.push_back('g'); |
516 |
|
atcommand.push_back('d'); |
517 |
|
atcommand.push_back('='); |
518 |
|
atcommand.push_back('1'); |
519 |
|
atcommand.push_back(','); |
520 |
|
atcommand.push_back('3'); |
521 |
|
|
522 |
|
writeFrame(atcommand); |
523 |
|
Sleep(500); |
524 |
|
} |
525 |
|
LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam) |
526 |
|
{ |
527 |
|
CString *s = (CString*) lParam; |
528 |
|
GetDlgItem(IDC_Textwindow)->SetWindowText(*s); |
529 |
|
|
530 |
|
delete s; |
531 |
|
return 0; |
532 |
|
|
533 |
|
}UINT threadWrapper(LPVOID thread) |
534 |
|
{ |
535 |
|
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
536 |
|
t->runthread(); |
537 |
|
return 0; |
538 |
|
} |
539 |
|
|
540 |
|
void CFlisServerDlg::startthread() |
541 |
|
{ |
542 |
|
AfxBeginThread(threadWrapper, (LPVOID) this); |
543 |
|
} |
544 |
|
|
545 |
|
void CFlisServerDlg::runthread() |
546 |
|
{ |
547 |
|
while (continueThread != 0) |
548 |
|
{ |
549 |
|
Reader(); |
550 |
|
} |
551 |
|
} |
552 |
|
void CFlisServerDlg::Reader() |
553 |
|
{ |
554 |
|
if(Serial.getComstat().cbInQue > 0) |
555 |
|
{ |
556 |
|
Sleep(250); |
557 |
|
std::vector<unsigned char> answer = readFrame(); |
558 |
|
Sleep(500); |
559 |
|
CString tekst, oldtekst; |
560 |
|
int lol; |
561 |
|
char array1[250]; |
562 |
|
int i; |
563 |
|
for (int i=0; i<answer.size(); i++) |
564 |
|
{ |
565 |
|
array1[i] = answer[i]; |
566 |
|
} |
567 |
|
|
568 |
|
for (int i=0; i<answer.size(); i++) |
569 |
|
{ |
570 |
|
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
571 |
|
{ |
572 |
|
tekst.AppendChar(array1[i]); |
573 |
|
} |
574 |
|
} |
575 |
|
tekst.Append(":"); |
576 |
|
|
577 |
|
CString command; |
578 |
|
bool plus; |
579 |
|
int pos = tekst.Find('+',0); |
580 |
|
if (pos != -1) |
581 |
|
{ |
582 |
|
plus = true; |
583 |
|
tekst = tekst.Right( tekst.GetLength() - pos -1); |
584 |
|
pos = tekst.Find(':'); |
585 |
|
command = tekst.Left(pos); |
586 |
|
tekst = tekst.Right( tekst.GetLength() - pos -1); |
587 |
|
} |
588 |
|
|
589 |
|
|
590 |
|
if(tekst == "OK") |
591 |
|
{ |
592 |
|
m_Textwindow.GetWindowText(oldtekst); |
593 |
|
oldtekst.Append("\r\n"); |
594 |
|
oldtekst.Append("OK tekst modtaget"); |
595 |
|
m_Textwindow.SetWindowText(oldtekst); |
596 |
|
} |
597 |
|
else if (tekst == "error") |
598 |
|
{ |
599 |
|
m_Textwindow.GetWindowText(oldtekst); |
600 |
|
oldtekst.Append("\r\n"); |
601 |
|
oldtekst.Append("error tekst"); |
602 |
|
oldtekst.Append(tekst); |
603 |
|
m_Textwindow.SetWindowText(oldtekst); |
604 |
|
} |
605 |
|
else if (plus = true) |
606 |
|
{ |
607 |
|
if (command = "cmti") |
608 |
|
{ |
609 |
|
CString smscount, oldteskst; |
610 |
|
int pos = tekst.Find(',',0); |
611 |
|
if (pos != -1) |
612 |
|
{ |
613 |
|
smscount = tekst.Right( tekst.GetLength() - pos -1); |
614 |
|
smscount.Remove(':'); |
615 |
|
ResetSms++; |
616 |
|
} |
617 |
|
////////////////Read sms ting////////////////// |
618 |
|
std::vector<unsigned char> data; |
619 |
|
data.push_back('a'); |
620 |
|
data.push_back('t'); |
621 |
|
data.push_back('+'); |
622 |
|
data.push_back('c'); |
623 |
|
data.push_back('m'); |
624 |
|
data.push_back('g'); |
625 |
|
data.push_back('r'); |
626 |
|
data.push_back('='); |
627 |
|
|
628 |
|
for (int i=0; i< smscount.GetLength(); i++) |
629 |
|
{ |
630 |
|
data.push_back(smscount[i]); |
631 |
|
} |
632 |
|
for (int i=0; i<data.size();i++) |
633 |
|
{ |
634 |
|
oldteskst.AppendChar(data[i]); |
635 |
|
} |
636 |
|
m_Textwindow.SetWindowText(oldteskst); |
637 |
|
writeFrame(data); |
638 |
|
Sleep(200); |
639 |
|
ReadSms(); |
640 |
|
|
641 |
|
} |
642 |
|
else if(command = "wind") |
643 |
|
{ |
644 |
|
m_Textwindow.GetWindowText(oldtekst); |
645 |
|
oldtekst.Append("\r\n"); |
646 |
|
oldtekst.Append("Først lidt tekst på næste linie\r\n"); |
647 |
|
oldtekst.Append(tekst); |
648 |
|
m_Textwindow.SetWindowText(oldtekst); |
649 |
|
} |
650 |
|
} |
651 |
|
if(ResetSms == 50) |
652 |
|
{ |
653 |
|
DeleteSms(); |
654 |
|
} |
655 |
|
|
656 |
|
} |
657 |
|
if (continueThread = 1) |
658 |
|
{ |
659 |
|
|
660 |
|
////////////////DBRead stuff////////////////// |
661 |
|
CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID; |
662 |
|
std::vector<Commands> data; |
663 |
|
data = DBRead(); |
664 |
|
for (int i=0; i<data.size(); i++) |
665 |
|
{ |
666 |
|
testdata.Append(data[i].IDnr); |
667 |
|
testdata.Append(":"); |
668 |
|
testdata.Append(data[i].CommandID); |
669 |
|
testdata.Append(":"); |
670 |
|
testdata.Append(data[i].InstallationsID); |
671 |
|
testdata.Append(":"); |
672 |
|
} |
673 |
|
sIDnr = Splitter(testdata); |
674 |
|
sCommandID = Splitter(testdata); |
675 |
|
sInstallationsID = Splitter(testdata); |
676 |
|
|
677 |
|
testprint.Append("sIDnr på næste linie\r\n"); |
678 |
|
testprint.Append(sIDnr); |
679 |
|
testprint.Append("\r\n"); |
680 |
|
testprint.Append("sCommandID på næste linie\r\n"); |
681 |
|
testprint.Append(sCommandID); |
682 |
|
testprint.Append("\r\n"); |
683 |
|
testprint.Append("sInstallationsID på næste linie\r\n"); |
684 |
|
testprint.Append(sInstallationsID); |
685 |
|
m_Textwindow.SetWindowText(testprint); |
686 |
|
|
687 |
|
|
688 |
|
if (sIDnr.GetLength() > 0) |
689 |
|
{ |
690 |
|
DBReadData(sIDnr,sCommandID,sInstallationsID); |
691 |
|
} |
692 |
|
Sleep(500); |
693 |
|
////////////////DBRead stuff////////////////// |
694 |
|
} |
695 |
} |
} |
696 |
|
void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID) |
697 |
|
{ |
698 |
|
CString TlfNr; |
699 |
|
|
700 |
|
//kald dbreadphone som du kalder dbread |
701 |
|
std::vector<Installation> inst; |
702 |
|
inst = DBReadPhone(InstallationsID); |
703 |
|
for (int i=0; i<inst.size(); i++) |
704 |
|
{ |
705 |
|
CString dataen = inst[i].InstPhoneNr; |
706 |
|
TlfNr.Append(dataen); |
707 |
|
} |
708 |
|
vector<unsigned char> tlfnr; |
709 |
|
for (int i=0; i<TlfNr.GetLength(); i++) |
710 |
|
{ |
711 |
|
tlfnr.push_back(TlfNr[i]); |
712 |
|
} |
713 |
|
|
714 |
|
SendSmsHead(tlfnr); |
715 |
|
Sleep(500); |
716 |
|
//35:29860132:75 <--- demo pakke; |
717 |
|
vector<unsigned char> smsdata; |
718 |
|
smsdata.push_back('3'); |
719 |
|
smsdata.push_back('5'); |
720 |
|
smsdata.push_back(':'); |
721 |
|
smsdata.push_back('2'); |
722 |
|
smsdata.push_back('9'); |
723 |
|
smsdata.push_back('8'); |
724 |
|
smsdata.push_back('6'); |
725 |
|
smsdata.push_back('0'); |
726 |
|
smsdata.push_back('1'); |
727 |
|
smsdata.push_back('3'); |
728 |
|
smsdata.push_back('2'); |
729 |
|
smsdata.push_back(':'); |
730 |
|
smsdata.push_back('7'); |
731 |
|
smsdata.push_back('5'); |
732 |
|
|
733 |
|
SendSmsData(smsdata); |
734 |
|
Sleep(250); |
735 |
|
|
736 |
|
CString SQL; |
737 |
|
|
738 |
|
SQL.Format("update command set executed=now() where id=%s",IDnr); |
739 |
|
db.ExecuteSQL(SQL); |
740 |
|
Sleep(150); |
741 |
|
} |
742 |
|
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
743 |
|
{ |
744 |
|
vector<Installation> buffer; |
745 |
|
|
746 |
|
CString SQL, phonenr; |
747 |
|
SQL.Format("select installationphonenr from installation WHERE id = %s",sInstallationsID); |
748 |
|
CRecordset rs(&db); |
749 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
750 |
|
if (rs.GetRecordCount()>0) |
751 |
|
{ |
752 |
|
rs.MoveFirst(); |
753 |
|
while(!rs.IsEOF()) |
754 |
|
{ |
755 |
|
Installation Myinst; |
756 |
|
rs.GetFieldValue((short)0,phonenr); |
757 |
|
|
758 |
|
Myinst.InstPhoneNr.Append(phonenr); |
759 |
|
|
760 |
|
buffer.push_back(Myinst); |
761 |
|
rs.MoveNext(); |
762 |
|
} |
763 |
|
} |
764 |
|
rs.Close(); |
765 |
|
return buffer; |
766 |
|
} |