6 |
#include "FlisServerDlg.h" |
#include "FlisServerDlg.h" |
7 |
#include <vector> |
#include <vector> |
8 |
#include ".\flisserverdlg.h" |
#include ".\flisserverdlg.h" |
9 |
|
#include <atlrx.h> |
10 |
|
|
11 |
#ifdef _DEBUG |
#ifdef _DEBUG |
12 |
#define new DEBUG_NEW |
#define new DEBUG_NEW |
368 |
TlfNr = data.Mid(24,8); |
TlfNr = data.Mid(24,8); |
369 |
FyrData = data.Mid(57,data.GetLength()-59); |
FyrData = data.Mid(57,data.GetLength()-59); |
370 |
FyrData.Append(":"); |
FyrData.Append(":"); |
371 |
|
|
372 |
|
if (FyrData.MakeLower() == "conf ok:") |
373 |
|
{ |
374 |
|
HandleAcknowledge(TlfNr); |
375 |
|
return; |
376 |
|
} |
377 |
|
CAtlRegExp<> regex; |
378 |
|
if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK) |
379 |
|
{ |
380 |
|
|
381 |
|
CAtlREMatchContext<> pContext; |
382 |
|
|
383 |
|
if (!regex.Match(FyrData, &pContext)) |
384 |
|
{ |
385 |
|
AppendText("Invalid SMS recieved"); |
386 |
|
return; |
387 |
|
} |
388 |
|
} |
389 |
|
else |
390 |
|
{ |
391 |
|
AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske ! |
392 |
|
} |
393 |
|
|
394 |
SmsCount = Splitter(FyrData); |
SmsCount = Splitter(FyrData); |
395 |
Temper = Splitter(FyrData); |
Temper = Splitter(FyrData); |
396 |
Flamme = Splitter(FyrData); |
Flamme = Splitter(FyrData); |
438 |
{ |
{ |
439 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
440 |
continueThread = 0; |
continueThread = 0; |
441 |
|
Sleep(250); |
442 |
DeleteSms(); |
DeleteSms(); |
443 |
|
|
444 |
Sleep(500); |
Sleep(500); |
459 |
void CFlisServerDlg::OnBnClickedGsmpin() |
void CFlisServerDlg::OnBnClickedGsmpin() |
460 |
{ |
{ |
461 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
462 |
m_Textwindow.SetWindowText("Indsætter Pinkode, efterfuldt af 60sec pause"); |
bool ready = true; |
463 |
|
m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar"); |
464 |
UpdateWindow(); |
UpdateWindow(); |
465 |
SetPin(); |
SetPin(); |
466 |
Sleep(50000); |
Sleep(500); |
467 |
|
|
468 |
while (Serial.getComstat().cbInQue > 0) |
while (ready == true) |
469 |
|
{ |
470 |
|
if (Serial.getComstat().cbInQue > 120) |
471 |
|
{ |
472 |
|
std::vector<unsigned char> answer = readFrame(); |
473 |
|
CString tekst; |
474 |
|
|
475 |
|
for (int i=0; i<answer.size(); i++) |
476 |
|
{ |
477 |
|
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
478 |
|
{ |
479 |
|
tekst.AppendChar(answer[i]); |
480 |
|
} |
481 |
|
} |
482 |
|
int n = tekst.Find("WIND: 11",0); |
483 |
|
if (n > -1) |
484 |
|
{ |
485 |
|
ready = false; |
486 |
|
} |
487 |
|
else |
488 |
|
{ |
489 |
|
AppendText(tekst); |
490 |
|
} |
491 |
|
} |
492 |
|
Sleep(10); //Small delay to avoid busy wait |
493 |
|
} |
494 |
|
Sleep(100); //Give the modem a chance to send the last data |
495 |
|
while(Serial.getComstat().cbInQue > 0) |
496 |
{ |
{ |
497 |
Serial.readByte(); //Flush the incoming queue |
Serial.readByte(); //Flush the incoming queue |
498 |
|
Sleep(1); |
499 |
} |
} |
|
|
|
500 |
OnBnClickedStart(); |
OnBnClickedStart(); |
501 |
} |
} |
502 |
void CFlisServerDlg::DeleteSms() |
void CFlisServerDlg::DeleteSms() |
533 |
{ |
{ |
534 |
while (continueThread != 0) |
while (continueThread != 0) |
535 |
{ |
{ |
536 |
Reader(); |
MyMainThread(); |
537 |
} |
} |
538 |
} |
} |
539 |
void CFlisServerDlg::Reader() |
void CFlisServerDlg::MyMainThread() |
540 |
{ |
{ |
541 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
542 |
{ |
{ |
544 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
545 |
Sleep(500); |
Sleep(500); |
546 |
CString tekst, oldtekst; |
CString tekst, oldtekst; |
|
int lol; |
|
547 |
|
|
548 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
549 |
{ |
{ |
555 |
tekst.Append(":"); |
tekst.Append(":"); |
556 |
|
|
557 |
CString command; |
CString command; |
558 |
bool plus; |
bool plus = false; |
559 |
int pos = tekst.Find('+',0); |
int pos = tekst.Find('+',0); |
560 |
if (pos != -1) |
if (pos != -1) |
561 |
{ |
{ |
631 |
} |
} |
632 |
else |
else |
633 |
{ |
{ |
634 |
CString send; |
AppendText(tekst); |
|
send.Append("Anden tekst end forventet"); |
|
|
send.Append("\r\n"); |
|
|
send.Append(tekst); |
|
|
AppendText(send); |
|
635 |
} |
} |
636 |
if(ResetSms == 20) |
if(ResetSms == 20) |
637 |
{ |
{ |
671 |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
672 |
} |
} |
673 |
Sleep(500); |
Sleep(500); |
674 |
|
|
675 |
|
CheckAcknowledges(); |
676 |
|
Sleep(500); |
677 |
|
} |
678 |
|
CTime now = CTime::GetCurrentTime(); |
679 |
|
|
680 |
|
CTimeSpan elapsed = now-Alive.tid; |
681 |
|
|
682 |
|
if (elapsed.GetTotalSeconds() >= 900) |
683 |
|
{ |
684 |
|
keepaliveandread(); |
685 |
} |
} |
686 |
} |
} |
687 |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
688 |
{ |
{ |
|
CString ServerTlfNr; |
|
|
int i = 0; |
|
|
ServerTlfNr = "29860132"; |
|
|
|
|
689 |
|
|
690 |
std::vector<Installation> inst; |
std::vector<Installation> inst; |
691 |
inst = DBReadPhone(InstallationsID); |
|
692 |
while (i < inst.size()) |
if (CommandID == "1") |
693 |
|
inst = DBReadPhone("0"); |
694 |
|
else |
695 |
|
inst = DBReadPhone(InstallationsID); |
696 |
|
|
697 |
|
for ( int j=0; j < inst.size(); j++) |
698 |
{ |
{ |
699 |
CString TlfNr, Imei, updaterate; |
CString TlfNr, Imei, updaterate; |
700 |
TlfNr.Empty(); |
TlfNr.Empty(); |
701 |
Imei.Empty(); |
Imei.Empty(); |
702 |
updaterate.Empty(); |
updaterate.Empty(); |
703 |
|
|
704 |
CString dataen = inst[i].InstPhoneNr; |
CString dataen = inst[j].InstPhoneNr; |
705 |
TlfNr.Append(dataen); |
TlfNr.Append(dataen); |
706 |
CString Imeidata = inst[i].Imei; |
CString Imeidata = inst[j].Imei; |
707 |
Imei.Append(Imeidata); |
Imei.Append(Imeidata); |
708 |
CString updaterat = inst[i].Updaterate; |
CString updaterat = inst[j].Updaterate; |
709 |
updaterate.Append(updaterat); |
updaterate.Append(updaterat); |
|
i++; |
|
710 |
|
|
711 |
vector<unsigned char> tlfnr; |
vector<unsigned char> tlfnr; |
712 |
for (int i=0; i<TlfNr.GetLength(); i++) |
for (int i=0; i<TlfNr.GetLength(); i++) |
713 |
{ |
{ |
714 |
tlfnr.push_back(TlfNr[i]); |
tlfnr.push_back(TlfNr[i]); |
715 |
} |
} |
716 |
|
|
717 |
int calcimei; |
int calcimei; |
718 |
__int64 buf; |
__int64 buf; |
719 |
|
|
720 |
buf = atof(Imei); |
buf = atof(Imei); |
721 |
|
|
722 |
calcimei = tversum(buf); |
calcimei = tversum(buf); |
723 |
|
|
724 |
Imei.Format("%d",calcimei); |
Imei.Format("%d",calcimei); |
725 |
|
|
726 |
SendSmsHead(tlfnr); |
SendSmsHead(tlfnr); |
727 |
Sleep(250); |
Sleep(250); |
728 |
|
|
729 |
vector<unsigned char> smsdata; |
vector<unsigned char> smsdata; |
730 |
|
|
731 |
for (int i=0; i<Imei.GetLength(); i++) |
for (int i=0; i<Imei.GetLength(); i++) |
732 |
{ |
{ |
733 |
smsdata.push_back(Imei[i]); |
smsdata.push_back(Imei[i]); |
734 |
} |
} |
735 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
736 |
for (int i=0; i<ServerTlfNr.GetLength(); i++) |
for (int i=0; i<Alive.Phonenr.GetLength(); i++) |
737 |
{ |
{ |
738 |
smsdata.push_back(ServerTlfNr[i]); |
smsdata.push_back(Alive.Phonenr[i]); |
739 |
} |
} |
740 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
741 |
for (int i=0; i<updaterate.GetLength(); i++) |
for (int i=0; i<updaterate.GetLength(); i++) |
742 |
{ |
{ |
743 |
smsdata.push_back(updaterate[i]); |
smsdata.push_back(updaterate[i]); |
744 |
} |
} |
745 |
|
|
746 |
|
SendSmsData(smsdata); |
747 |
|
Sleep(500); |
748 |
|
|
749 |
|
if (IDnr != "0") |
750 |
|
{ |
751 |
|
Acks.push_back( Acknowledge(inst[j].instID,TlfNr) ); |
752 |
|
} |
753 |
|
|
|
SendSmsData(smsdata); |
|
|
Sleep(500); |
|
|
|
|
754 |
} |
} |
755 |
|
|
756 |
|
if (IDnr != "0") |
757 |
|
{ |
758 |
CString SQL, Textwindow; |
CString SQL, Textwindow; |
759 |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
760 |
db.ExecuteSQL(SQL); |
db.ExecuteSQL(SQL); |
761 |
|
Sleep(100); |
762 |
AppendText("Command executed"); |
AppendText("Command executed"); |
763 |
Sleep(150); |
} |
764 |
|
Sleep(150); |
765 |
|
|
766 |
|
|
767 |
|
|
768 |
|
|
769 |
} |
} |
770 |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
771 |
{ |
{ |
772 |
vector<Installation> buffer; |
vector<Installation> buffer; |
773 |
|
|
774 |
CString SQL, phonenr, imei, updaterate; |
CString SQL, phonenr, imei, updaterate, id; |
|
int installernull; |
|
775 |
Installation Myinst; |
Installation Myinst; |
776 |
installernull = atoi(sInstallationsID); |
|
777 |
if (installernull > 1) |
if (sInstallationsID != "0") |
778 |
{ |
{ |
779 |
SQL.Format("select installationphonenr, imei, updaterate from installation WHERE id = %s", sInstallationsID); |
SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID); |
780 |
} |
} |
781 |
else if(installernull < 2) |
else |
782 |
{ |
{ |
783 |
SQL.Format("select installationphonenr, imei, updaterate from installation"); |
SQL.Format("select installationphonenr, imei, updaterate, id from installation"); |
784 |
} |
} |
785 |
|
|
786 |
CRecordset rs(&db); |
CRecordset rs(&db); |
787 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
788 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
794 |
rs.GetFieldValue((short)0,phonenr); |
rs.GetFieldValue((short)0,phonenr); |
795 |
rs.GetFieldValue(1,imei); |
rs.GetFieldValue(1,imei); |
796 |
rs.GetFieldValue(2,updaterate); |
rs.GetFieldValue(2,updaterate); |
797 |
|
rs.GetFieldValue(3,id); |
798 |
|
|
799 |
Myinst.InstPhoneNr = phonenr; |
Myinst.InstPhoneNr = phonenr; |
800 |
Myinst.Imei = imei; |
Myinst.Imei = imei; |
801 |
Myinst.Updaterate = updaterate; |
Myinst.Updaterate = updaterate; |
802 |
|
Myinst.instID = id; |
803 |
|
|
804 |
buffer.push_back(Myinst); |
buffer.push_back(Myinst); |
805 |
rs.MoveNext(); |
rs.MoveNext(); |
821 |
void CFlisServerDlg::OnBnClickedStart() |
void CFlisServerDlg::OnBnClickedStart() |
822 |
{ |
{ |
823 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
824 |
|
|
825 |
continueThread = 1; |
continueThread = 1; |
826 |
m_Textwindow.SetWindowText("Started"); |
keepaliveandread(); |
827 |
|
Sleep(150); |
828 |
|
m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr ); |
829 |
|
AppendText("Started"); |
830 |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
831 |
} |
} |
832 |
void CFlisServerDlg::AppendText(CString s) |
void CFlisServerDlg::AppendText(CString s) |
836 |
Tekst.Append("\r\n"); |
Tekst.Append("\r\n"); |
837 |
Tekst.Append(s); |
Tekst.Append(s); |
838 |
m_Textwindow.SetWindowText(Tekst); |
m_Textwindow.SetWindowText(Tekst); |
839 |
|
int g = m_Textwindow.GetScrollLimit(SB_VERT); |
840 |
|
if (g > 0) |
841 |
|
{ |
842 |
|
m_Textwindow.SetScrollPos(g,1,false); |
843 |
|
} |
844 |
|
|
845 |
} |
} |
846 |
void ConfigFile::ReadSettings() |
void ConfigFile::ReadSettings() |
877 |
} |
} |
878 |
|
|
879 |
file.close(); |
file.close(); |
880 |
|
} |
881 |
|
void CFlisServerDlg::HandleAcknowledge(CString tlfnr) |
882 |
|
{ |
883 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
884 |
|
{ |
885 |
|
Acknowledge& current = (*it); |
886 |
|
if (current.sTlfNr == tlfnr) |
887 |
|
{ |
888 |
|
CString SQL; |
889 |
|
SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID); |
890 |
|
db.ExecuteSQL(SQL); |
891 |
|
|
892 |
|
Acks.erase(it); |
893 |
|
|
894 |
|
return; |
895 |
|
} |
896 |
|
} |
897 |
|
} |
898 |
|
|
899 |
|
void CFlisServerDlg::CheckAcknowledges() |
900 |
|
{ |
901 |
|
CTime now = CTime::GetCurrentTime(); |
902 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
903 |
|
{ |
904 |
|
Acknowledge& current = (*it); |
905 |
|
|
906 |
|
CTimeSpan elapsed = now-current.cTime; |
907 |
|
|
908 |
|
if (elapsed.GetTotalSeconds() >= 300) |
909 |
|
{ |
910 |
|
DumpAckList(); |
911 |
|
if (current.iRetry >= 1) |
912 |
|
{ |
913 |
|
CString SQL; |
914 |
|
SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID); |
915 |
|
db.ExecuteSQL(SQL); |
916 |
|
Acks.erase(it); |
917 |
|
return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again |
918 |
|
} |
919 |
|
else |
920 |
|
{ |
921 |
|
SendConfig("0","2",current.sInstallationID); |
922 |
|
current.iRetry++; |
923 |
|
current.cTime = CTime::GetCurrentTime(); |
924 |
|
AppendText( CString("Retry send config to ") + current.sTlfNr ); |
925 |
|
} |
926 |
|
} |
927 |
|
} |
928 |
|
} |
929 |
|
|
930 |
|
void CFlisServerDlg::DumpAckList() |
931 |
|
{ |
932 |
|
CTime now = CTime::GetCurrentTime(); |
933 |
|
OutputDebugString("-------------------------------\n"); |
934 |
|
int count = 0; |
935 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
936 |
|
{ |
937 |
|
CString msg; |
938 |
|
msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr); |
939 |
|
OutputDebugString(msg); |
940 |
|
count++; |
941 |
|
} |
942 |
|
} |
943 |
|
vector<keepalive> CFlisServerDlg::keepaliveandread(void) |
944 |
|
{ |
945 |
|
vector<keepalive> buffer; |
946 |
|
|
947 |
|
CString SQL, name, value; |
948 |
|
SQL = "select name, value from config;"; |
949 |
|
CRecordset rs(&db); |
950 |
|
try |
951 |
|
{ |
952 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
953 |
|
if (rs.GetRecordCount()>0) |
954 |
|
{ |
955 |
|
rs.MoveFirst(); |
956 |
|
|
957 |
|
rs.GetFieldValue((short)0, name); |
958 |
|
rs.GetFieldValue(1,value); |
959 |
|
|
960 |
|
Alive.name = name; |
961 |
|
Alive.value = value; |
962 |
|
Alive.tid = CTime::GetCurrentTime(); |
963 |
|
if(Alive.name == "phonenr") |
964 |
|
{ |
965 |
|
Alive.Phonenr = value; |
966 |
|
} |
967 |
|
|
968 |
|
buffer.push_back(Alive); |
969 |
|
rs.MoveNext(); |
970 |
|
|
971 |
|
} |
972 |
|
rs.Close(); |
973 |
|
return buffer; |
974 |
|
} |
975 |
|
catch (char * str) |
976 |
|
{ |
977 |
|
AppendText("Keepalive failed, closing and opening the db connection again"); |
978 |
|
if(db.IsOpen()) |
979 |
|
{ |
980 |
|
db.Close(); |
981 |
|
AppendText("DB connection closed"); |
982 |
|
} |
983 |
|
if(!db.IsOpen()) |
984 |
|
{ |
985 |
|
DBConnect(); |
986 |
|
AppendText("DB connection started again"); |
987 |
|
} |
988 |
|
return buffer; |
989 |
|
} |
990 |
|
|
991 |
|
|
992 |
} |
} |