5 |
#include "FlisServer.h" |
#include "FlisServer.h" |
6 |
#include "FlisServerDlg.h" |
#include "FlisServerDlg.h" |
7 |
#include <vector> |
#include <vector> |
8 |
|
#include ".\flisserverdlg.h" |
9 |
|
|
10 |
#ifdef _DEBUG |
#ifdef _DEBUG |
11 |
#define new DEBUG_NEW |
#define new DEBUG_NEW |
65 |
ON_WM_QUERYDRAGICON() |
ON_WM_QUERYDRAGICON() |
66 |
ON_MESSAGE(UWM_MYMESSAGE, OnShowString) |
ON_MESSAGE(UWM_MYMESSAGE, OnShowString) |
67 |
//}}AFX_MSG_MAP |
//}}AFX_MSG_MAP |
|
ON_BN_CLICKED(IDC_test, OnBnClickedtest) |
|
68 |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
69 |
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) |
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) |
70 |
|
ON_BN_CLICKED(IDC_Start, OnBnClickedStart) |
71 |
END_MESSAGE_MAP() |
END_MESSAGE_MAP() |
72 |
|
|
73 |
|
|
103 |
// TODO: Add extra initialization here |
// TODO: Add extra initialization here |
104 |
StartSerial(); |
StartSerial(); |
105 |
DBConnect(); |
DBConnect(); |
106 |
continueThread = true; |
ResetSms = 0; |
|
//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 |
} |
} |
|
void CFlisServerDlg::OnBnClickedtest() |
|
|
{ |
|
|
// TODO: Add your control notification handler code here |
|
|
|
|
|
/* |
|
|
////////////////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////////////////// |
|
|
*/ |
|
|
/* |
|
|
///////////////Send sms stuff///////////////// |
|
|
vector<unsigned char> tlfnr; |
|
|
vector<unsigned char> smsdata; |
|
|
tlfnr.push_back('2'); |
|
|
tlfnr.push_back('9'); |
|
|
tlfnr.push_back('7'); |
|
|
tlfnr.push_back('2'); |
|
|
tlfnr.push_back('2'); |
|
|
tlfnr.push_back('6'); |
|
|
tlfnr.push_back('0'); |
|
|
tlfnr.push_back('3'); |
|
|
|
|
|
SendSmsHead(tlfnr); |
|
|
|
|
|
//35:29860132:75 <--- demo pakke; |
|
|
smsdata.push_back('3'); |
|
|
smsdata.push_back('5'); |
|
|
smsdata.push_back(':'); |
|
|
smsdata.push_back('2'); |
|
|
smsdata.push_back('9'); |
|
|
smsdata.push_back('8'); |
|
|
smsdata.push_back('6'); |
|
|
smsdata.push_back('0'); |
|
|
smsdata.push_back('1'); |
|
|
smsdata.push_back('3'); |
|
|
smsdata.push_back('2'); |
|
|
smsdata.push_back(':'); |
|
|
smsdata.push_back('7'); |
|
|
smsdata.push_back('5'); |
|
|
|
|
|
SendSmsData(smsdata); |
|
|
///////////////Send sms stuff///////////////// |
|
|
*/ |
|
|
/* |
|
|
////////////Start Tråd//////////////////////// |
|
|
MyThread *t = new MyThread(AfxGetMainWnd()); |
|
|
t->start(); |
|
|
////////////Start Tråd//////////////////////// |
|
|
*/ |
|
|
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
|
187 |
|
|
|
} |
|
188 |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
189 |
{ |
{ |
190 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<data.size(); i++) |
214 |
data.push_back('5'); |
data.push_back('5'); |
215 |
|
|
216 |
writeFrame(data); |
writeFrame(data); |
217 |
Sleep(750); |
/* Sleep(750); |
218 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
219 |
{ |
{ |
220 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
235 |
} |
} |
236 |
m_Textwindow.SetWindowText(tekst); |
m_Textwindow.SetWindowText(tekst); |
237 |
} |
} |
238 |
|
*/ |
239 |
} |
} |
240 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
241 |
{ |
{ |
252 |
CString tekst; |
CString tekst; |
253 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
254 |
Sleep(50); |
Sleep(50); |
255 |
char array1[25]; |
char array1[150]; |
256 |
int i; |
int i; |
257 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
258 |
{ |
{ |
302 |
void CFlisServerDlg::DBConnect() |
void CFlisServerDlg::DBConnect() |
303 |
{ |
{ |
304 |
CString dsn; |
CString dsn; |
305 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; 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"); //Torben |
306 |
//dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server |
307 |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
308 |
} |
} |
309 |
vector<Commands> CFlisServerDlg::DBRead(void) |
vector<Commands> CFlisServerDlg::DBRead(void) |
319 |
rs.MoveFirst(); |
rs.MoveFirst(); |
320 |
while(!rs.IsEOF()) |
while(!rs.IsEOF()) |
321 |
{ |
{ |
322 |
Commands Mycom; |
Commands Mycom; |
323 |
rs.GetFieldValue((short)0, IDnr); |
rs.GetFieldValue((short)0, IDnr); |
324 |
rs.GetFieldValue(3, CommandID); |
rs.GetFieldValue(3, CommandID); |
325 |
rs.GetFieldValue(4, InstallationsID); |
rs.GetFieldValue(4, InstallationsID); |
326 |
|
|
327 |
Mycom.IDnr = IDnr; |
Mycom.IDnr = IDnr; |
328 |
Mycom.CommandID = CommandID; |
Mycom.CommandID = CommandID; |
329 |
Mycom.InstallationsID = InstallationsID; |
Mycom.InstallationsID = InstallationsID; |
330 |
|
|
331 |
buffer.push_back(Mycom); |
buffer.push_back(Mycom); |
332 |
rs.MoveNext(); |
rs.MoveNext(); |
333 |
} |
} |
334 |
} |
} |
335 |
rs.Close(); |
rs.Close(); |
371 |
char CharData[150]; |
char CharData[150]; |
372 |
strcpy(CharData,data); |
strcpy(CharData,data); |
373 |
|
|
374 |
int s=22; |
int s=24; |
375 |
for (int i=0; i<=7; i++) |
for (int i=0; i<=7; i++) |
376 |
{ |
{ |
377 |
TlfNr.AppendChar(CharData[s]); |
TlfNr.AppendChar(CharData[s]); |
378 |
s++; |
s++; |
379 |
} |
} |
380 |
|
|
381 |
for (int s=55; s<=(data.GetLength()-3); s++) |
for (int s=57; s<=(data.GetLength()-3); s++) |
382 |
{ |
{ |
383 |
FyrData.AppendChar(CharData[s]); |
FyrData.AppendChar(CharData[s]); |
384 |
} |
} |
391 |
FremFejl = Splitter(FyrData); |
FremFejl = Splitter(FyrData); |
392 |
PowerFail = Splitter(FyrData); |
PowerFail = Splitter(FyrData); |
393 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
394 |
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
///////////////////// Her skal sendes data til databasen ////NÅR HEDIN KAN SENDE SMS/////////////////// |
395 |
m_Textwindow.GetWindowText(oldtekst); |
m_Textwindow.GetWindowText(oldtekst); |
396 |
oldtekst.Append("\r\n"); |
oldtekst.Append("\r\n"); |
397 |
oldtekst.Append("På næste linie kommer SmsCount. \r\n"); |
oldtekst.Append("På næste linie kommer SmsCount. \r\n"); |
433 |
void CFlisServerDlg::OnBnClickedClose() |
void CFlisServerDlg::OnBnClickedClose() |
434 |
{ |
{ |
435 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
436 |
continueThread = false; |
continueThread = 0; |
437 |
|
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 |
438 |
|
|
439 |
|
Sleep(500); |
440 |
if( Serial.isOpen() ) |
if( Serial.isOpen() ) |
441 |
{ |
{ |
442 |
Serial.close(); |
Serial.close(); |
446 |
{ |
{ |
447 |
db.Close(); |
db.Close(); |
448 |
} |
} |
449 |
DeleteSms(); |
|
450 |
OnOK(); |
OnOK(); |
451 |
|
|
452 |
} |
} |
453 |
|
|
454 |
void CFlisServerDlg::OnBnClickedGsmpin() |
void CFlisServerDlg::OnBnClickedGsmpin() |
455 |
{ |
{ |
456 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
457 |
SetPin(); |
SetPin(); |
458 |
Sleep(5000); |
Sleep(35000); |
459 |
|
OnBnClickedStart(); |
460 |
} |
} |
461 |
void CFlisServerDlg::DeleteSms() |
void CFlisServerDlg::DeleteSms() |
462 |
{ |
{ |
475 |
|
|
476 |
writeFrame(atcommand); |
writeFrame(atcommand); |
477 |
Sleep(500); |
Sleep(500); |
|
if(Serial.getComstat().cbInQue > 0) |
|
|
{ |
|
|
std::vector<unsigned char> answer = readFrame(); |
|
|
Sleep(50); |
|
|
CString tekst, oldtekst; |
|
|
char array1[250]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
|
|
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
|
|
{ |
|
|
tekst.AppendChar(array1[i]); |
|
|
} |
|
|
} |
|
|
if(tekst == "OK") |
|
|
{ |
|
|
m_Textwindow.GetWindowText(oldtekst); |
|
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append("Old Sms messages deleted"); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
} |
|
|
else |
|
|
{ |
|
|
m_Textwindow.GetWindowText(oldtekst); |
|
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
} |
|
|
} |
|
478 |
} |
} |
479 |
LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam) |
LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam) |
480 |
{ |
{ |
498 |
|
|
499 |
void CFlisServerDlg::runthread() |
void CFlisServerDlg::runthread() |
500 |
{ |
{ |
501 |
int count = 0; |
while (continueThread != 0) |
|
while (continueThread = true) |
|
502 |
{ |
{ |
503 |
Reader(); |
Reader(); |
504 |
} |
} |
505 |
} |
} |
506 |
void CFlisServerDlg::Reader() |
void CFlisServerDlg::Reader() |
507 |
{ |
{ |
508 |
int ResetSms = 0; |
if(Serial.getComstat().cbInQue > 0) |
|
while(1) |
|
|
{ |
|
|
if(Serial.getComstat().cbInQue > 0) |
|
509 |
{ |
{ |
510 |
|
Sleep(250); |
511 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
512 |
Sleep(500); |
Sleep(500); |
513 |
CString tekst, oldtekst; |
CString tekst, oldtekst; |
560 |
{ |
{ |
561 |
if (command = "cmti") |
if (command = "cmti") |
562 |
{ |
{ |
563 |
CString smscount; |
CString smscount, oldteskst; |
564 |
int pos = tekst.Find(',',0); |
int pos = tekst.Find(',',0); |
565 |
if (pos != -1) |
if (pos != -1) |
566 |
{ |
{ |
583 |
{ |
{ |
584 |
data.push_back(smscount[i]); |
data.push_back(smscount[i]); |
585 |
} |
} |
586 |
|
for (int i=0; i<data.size();i++) |
587 |
|
{ |
588 |
|
oldteskst.AppendChar(data[i]); |
589 |
|
} |
590 |
|
m_Textwindow.SetWindowText(oldteskst); |
591 |
writeFrame(data); |
writeFrame(data); |
592 |
|
Sleep(200); |
593 |
ReadSms(); |
ReadSms(); |
594 |
|
ResetSms++; |
595 |
|
|
596 |
} |
} |
597 |
|
else if(command = "wind") |
598 |
|
{ |
599 |
|
m_Textwindow.GetWindowText(oldtekst); |
600 |
|
oldtekst.Append("\r\n"); |
601 |
|
oldtekst.Append("Først lidt tekst på næste linie\r\n"); |
602 |
|
oldtekst.Append(tekst); |
603 |
|
m_Textwindow.SetWindowText(oldtekst); |
604 |
|
} |
605 |
} |
} |
606 |
if(ResetSms >= 50) |
if(ResetSms == 50) |
607 |
{ |
{ |
608 |
DeleteSms(); |
DeleteSms(); |
609 |
} |
} |
610 |
|
|
611 |
|
} |
612 |
|
if (continueThread = 1) |
613 |
|
{ |
614 |
|
|
615 |
|
CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei; |
616 |
|
int commandtest = 0; |
617 |
|
int iAll = 1; |
618 |
|
std::vector<Commands> data; |
619 |
|
data = DBRead(); |
620 |
|
for (int i=0; i<data.size(); i++) |
621 |
|
{ |
622 |
|
testdata.Append(data[i].IDnr); |
623 |
|
testdata.Append(":"); |
624 |
|
testdata.Append(data[i].CommandID); |
625 |
|
testdata.Append(":"); |
626 |
|
commandtest = atoi(data[i].CommandID); |
627 |
|
|
628 |
|
if(commandtest > 1) |
629 |
|
{ |
630 |
|
testdata.Append(data[i].InstallationsID); |
631 |
|
testdata.Append(":"); |
632 |
|
} |
633 |
|
} |
634 |
|
sIDnr = Splitter(testdata); |
635 |
|
sCommandID = Splitter(testdata); |
636 |
|
sInstallationsID = "0"; |
637 |
|
if (commandtest > 1) |
638 |
|
{ |
639 |
|
sInstallationsID = Splitter(testdata); |
640 |
|
iAll = 0; |
641 |
|
} |
642 |
|
|
643 |
|
if (sIDnr.GetLength() > 0) |
644 |
|
{ |
645 |
|
DBReadData(sIDnr,sCommandID,sInstallationsID); |
646 |
|
} |
647 |
|
Sleep(500); |
648 |
|
} |
649 |
|
} |
650 |
|
void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID) |
651 |
|
{ |
652 |
|
CString ServerTlfNr; |
653 |
|
int i = 0; |
654 |
|
ServerTlfNr = "29860132"; |
655 |
|
|
656 |
|
|
657 |
|
std::vector<Installation> inst; |
658 |
|
inst = DBReadPhone(InstallationsID); |
659 |
|
while (i < inst.size()) |
660 |
|
{ |
661 |
|
CString TlfNr, Imei, updaterate; |
662 |
|
TlfNr.Empty(); |
663 |
|
Imei.Empty(); |
664 |
|
updaterate.Empty(); |
665 |
|
|
666 |
|
CString dataen = inst[i].InstPhoneNr; |
667 |
|
TlfNr.Append(dataen); |
668 |
|
CString Imeidata = inst[i].Imei; |
669 |
|
Imei.Append(Imeidata); |
670 |
|
CString updaterat = inst[i].Updaterate; |
671 |
|
updaterate.Append(updaterat); |
672 |
|
i++; |
673 |
|
|
674 |
|
vector<unsigned char> tlfnr; |
675 |
|
for (int i=0; i<TlfNr.GetLength(); i++) |
676 |
|
{ |
677 |
|
tlfnr.push_back(TlfNr[i]); |
678 |
|
} |
679 |
|
|
680 |
|
int calcimei; |
681 |
|
__int64 buf; |
682 |
|
|
683 |
|
buf = atof(Imei); |
684 |
|
|
685 |
|
calcimei = tversum(buf); |
686 |
|
|
687 |
|
Imei.Format("%d",calcimei); |
688 |
|
|
689 |
|
SendSmsHead(tlfnr); |
690 |
|
Sleep(250); |
691 |
|
|
692 |
|
vector<unsigned char> smsdata; |
693 |
|
|
694 |
|
for (int i=0; i<Imei.GetLength(); i++) |
695 |
|
{ |
696 |
|
smsdata.push_back(Imei[i]); |
697 |
|
} |
698 |
|
smsdata.push_back(':'); |
699 |
|
for (int i=0; i<ServerTlfNr.GetLength(); i++) |
700 |
|
{ |
701 |
|
smsdata.push_back(ServerTlfNr[i]); |
702 |
|
} |
703 |
|
smsdata.push_back(':'); |
704 |
|
for (int i=0; i<updaterate.GetLength(); i++) |
705 |
|
{ |
706 |
|
smsdata.push_back(updaterate[i]); |
707 |
|
} |
708 |
|
|
709 |
|
SendSmsData(smsdata); |
710 |
|
Sleep(500); |
711 |
|
|
712 |
|
} |
713 |
|
CString SQL, Textwindow; |
714 |
|
SQL.Format("update command set executed=now() where id=%s",IDnr); |
715 |
|
db.ExecuteSQL(SQL); |
716 |
|
m_Textwindow.GetWindowText(Textwindow); |
717 |
|
Textwindow.Append("\r\n"); |
718 |
|
Textwindow.Append("Command executed"); |
719 |
|
m_Textwindow.SetWindowText(Textwindow); |
720 |
|
Sleep(150); |
721 |
|
|
722 |
|
} |
723 |
|
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
724 |
|
{ |
725 |
|
vector<Installation> buffer; |
726 |
|
|
727 |
|
CString SQL, phonenr, imei, updaterate; |
728 |
|
int installernull; |
729 |
|
Installation Myinst; |
730 |
|
installernull = atoi(sInstallationsID); |
731 |
|
if (installernull > 1) |
732 |
|
{ |
733 |
|
SQL.Format("select installationphonenr, imei, updaterate from installation WHERE id = %s", sInstallationsID); |
734 |
|
} |
735 |
|
else if(installernull < 2) |
736 |
|
{ |
737 |
|
SQL.Format("select installationphonenr, imei, updaterate from installation"); |
738 |
|
} |
739 |
|
CRecordset rs(&db); |
740 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
741 |
|
if (rs.GetRecordCount()>0) |
742 |
|
{ |
743 |
|
rs.MoveFirst(); |
744 |
|
while(!rs.IsEOF()) |
745 |
|
{ |
746 |
|
|
747 |
|
rs.GetFieldValue((short)0,phonenr); |
748 |
|
rs.GetFieldValue(1,imei); |
749 |
|
rs.GetFieldValue(2,updaterate); |
750 |
|
|
751 |
|
Myinst.InstPhoneNr = phonenr; |
752 |
|
Myinst.Imei = imei; |
753 |
|
Myinst.Updaterate = updaterate; |
754 |
|
|
755 |
|
buffer.push_back(Myinst); |
756 |
|
rs.MoveNext(); |
757 |
} |
} |
758 |
} |
} |
|
} |
|
759 |
|
rs.Close(); |
760 |
|
return buffer; |
761 |
|
} |
762 |
|
int CFlisServerDlg::tversum(__int64 input) |
763 |
|
{ |
764 |
|
int sum = 0; |
765 |
|
while (input > 0) |
766 |
|
{ |
767 |
|
sum += (input %10); |
768 |
|
input /= 10; |
769 |
|
} |
770 |
|
return sum; |
771 |
|
} |
772 |
|
void CFlisServerDlg::OnBnClickedStart() |
773 |
|
{ |
774 |
|
// TODO: Add your control notification handler code here |
775 |
|
|
776 |
|
continueThread = 1; |
777 |
|
|
778 |
|
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
779 |
|
} |