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() |
556 |
tekst.Append(":"); |
tekst.Append(":"); |
557 |
|
|
558 |
CString command; |
CString command; |
559 |
bool plus; |
bool plus = false; |
560 |
int pos = tekst.Find('+',0); |
int pos = tekst.Find('+',0); |
561 |
if (pos != -1) |
if (pos != -1) |
562 |
{ |
{ |
632 |
} |
} |
633 |
else |
else |
634 |
{ |
{ |
635 |
CString send; |
AppendText(tekst); |
|
send.Append("Anden tekst end forventet"); |
|
|
send.Append("\r\n"); |
|
|
send.Append(tekst); |
|
|
AppendText(send); |
|
636 |
} |
} |
637 |
if(ResetSms == 20) |
if(ResetSms == 20) |
638 |
{ |
{ |
672 |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
673 |
} |
} |
674 |
Sleep(500); |
Sleep(500); |
675 |
|
|
676 |
|
CheckAcknowledges(); |
677 |
|
Sleep(500); |
678 |
|
} |
679 |
|
CTime now = CTime::GetCurrentTime(); |
680 |
|
|
681 |
|
CTimeSpan elapsed = now-Alive.tid; |
682 |
|
|
683 |
|
if (elapsed.GetTotalSeconds() >= 900) |
684 |
|
{ |
685 |
|
keepaliveandread(); |
686 |
} |
} |
687 |
} |
} |
688 |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
689 |
{ |
{ |
|
CString ServerTlfNr; |
|
|
int i = 0; |
|
|
ServerTlfNr = "29860132"; |
|
|
|
|
690 |
|
|
691 |
std::vector<Installation> inst; |
std::vector<Installation> inst; |
692 |
inst = DBReadPhone(InstallationsID); |
|
693 |
while (i < inst.size()) |
if (CommandID == "1") |
694 |
|
inst = DBReadPhone("0"); |
695 |
|
else |
696 |
|
inst = DBReadPhone(InstallationsID); |
697 |
|
|
698 |
|
for ( int j=0; j < inst.size(); j++) |
699 |
{ |
{ |
700 |
CString TlfNr, Imei, updaterate; |
CString TlfNr, Imei, updaterate; |
701 |
TlfNr.Empty(); |
TlfNr.Empty(); |
702 |
Imei.Empty(); |
Imei.Empty(); |
703 |
updaterate.Empty(); |
updaterate.Empty(); |
704 |
|
|
705 |
CString dataen = inst[i].InstPhoneNr; |
CString dataen = inst[j].InstPhoneNr; |
706 |
TlfNr.Append(dataen); |
TlfNr.Append(dataen); |
707 |
CString Imeidata = inst[i].Imei; |
CString Imeidata = inst[j].Imei; |
708 |
Imei.Append(Imeidata); |
Imei.Append(Imeidata); |
709 |
CString updaterat = inst[i].Updaterate; |
CString updaterat = inst[j].Updaterate; |
710 |
updaterate.Append(updaterat); |
updaterate.Append(updaterat); |
|
i++; |
|
711 |
|
|
712 |
vector<unsigned char> tlfnr; |
vector<unsigned char> tlfnr; |
713 |
for (int i=0; i<TlfNr.GetLength(); i++) |
for (int i=0; i<TlfNr.GetLength(); i++) |
714 |
{ |
{ |
715 |
tlfnr.push_back(TlfNr[i]); |
tlfnr.push_back(TlfNr[i]); |
716 |
} |
} |
717 |
|
|
718 |
int calcimei; |
int calcimei; |
719 |
__int64 buf; |
__int64 buf; |
720 |
|
|
721 |
buf = atof(Imei); |
buf = atof(Imei); |
722 |
|
|
723 |
calcimei = tversum(buf); |
calcimei = tversum(buf); |
724 |
|
|
725 |
Imei.Format("%d",calcimei); |
Imei.Format("%d",calcimei); |
726 |
|
|
727 |
SendSmsHead(tlfnr); |
SendSmsHead(tlfnr); |
728 |
Sleep(250); |
Sleep(250); |
729 |
|
|
730 |
vector<unsigned char> smsdata; |
vector<unsigned char> smsdata; |
731 |
|
|
732 |
for (int i=0; i<Imei.GetLength(); i++) |
for (int i=0; i<Imei.GetLength(); i++) |
733 |
{ |
{ |
734 |
smsdata.push_back(Imei[i]); |
smsdata.push_back(Imei[i]); |
735 |
} |
} |
736 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
737 |
for (int i=0; i<ServerTlfNr.GetLength(); i++) |
for (int i=0; i<Alive.Phonenr.GetLength(); i++) |
738 |
{ |
{ |
739 |
smsdata.push_back(ServerTlfNr[i]); |
smsdata.push_back(Alive.Phonenr[i]); |
740 |
} |
} |
741 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
742 |
for (int i=0; i<updaterate.GetLength(); i++) |
for (int i=0; i<updaterate.GetLength(); i++) |
743 |
{ |
{ |
744 |
smsdata.push_back(updaterate[i]); |
smsdata.push_back(updaterate[i]); |
745 |
} |
} |
746 |
|
|
747 |
|
SendSmsData(smsdata); |
748 |
|
Sleep(500); |
749 |
|
|
750 |
|
if (IDnr != "0") |
751 |
|
{ |
752 |
|
Acks.push_back( Acknowledge(inst[j].instID,TlfNr) ); |
753 |
|
} |
754 |
|
|
|
SendSmsData(smsdata); |
|
|
Sleep(500); |
|
|
|
|
755 |
} |
} |
756 |
|
|
757 |
|
if (IDnr != "0") |
758 |
|
{ |
759 |
CString SQL, Textwindow; |
CString SQL, Textwindow; |
760 |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
761 |
db.ExecuteSQL(SQL); |
db.ExecuteSQL(SQL); |
762 |
|
Sleep(100); |
763 |
AppendText("Command executed"); |
AppendText("Command executed"); |
764 |
Sleep(150); |
} |
765 |
|
Sleep(150); |
766 |
|
|
767 |
|
|
768 |
|
|
769 |
|
|
770 |
} |
} |
771 |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
772 |
{ |
{ |
773 |
vector<Installation> buffer; |
vector<Installation> buffer; |
774 |
|
|
775 |
CString SQL, phonenr, imei, updaterate; |
CString SQL, phonenr, imei, updaterate, id; |
|
int installernull; |
|
776 |
Installation Myinst; |
Installation Myinst; |
777 |
installernull = atoi(sInstallationsID); |
|
778 |
if (installernull > 1) |
if (sInstallationsID != "0") |
779 |
{ |
{ |
780 |
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); |
781 |
} |
} |
782 |
else if(installernull < 2) |
else |
783 |
{ |
{ |
784 |
SQL.Format("select installationphonenr, imei, updaterate from installation"); |
SQL.Format("select installationphonenr, imei, updaterate, id from installation"); |
785 |
} |
} |
786 |
|
|
787 |
CRecordset rs(&db); |
CRecordset rs(&db); |
788 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
789 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
795 |
rs.GetFieldValue((short)0,phonenr); |
rs.GetFieldValue((short)0,phonenr); |
796 |
rs.GetFieldValue(1,imei); |
rs.GetFieldValue(1,imei); |
797 |
rs.GetFieldValue(2,updaterate); |
rs.GetFieldValue(2,updaterate); |
798 |
|
rs.GetFieldValue(3,id); |
799 |
|
|
800 |
Myinst.InstPhoneNr = phonenr; |
Myinst.InstPhoneNr = phonenr; |
801 |
Myinst.Imei = imei; |
Myinst.Imei = imei; |
802 |
Myinst.Updaterate = updaterate; |
Myinst.Updaterate = updaterate; |
803 |
|
Myinst.instID = id; |
804 |
|
|
805 |
buffer.push_back(Myinst); |
buffer.push_back(Myinst); |
806 |
rs.MoveNext(); |
rs.MoveNext(); |
822 |
void CFlisServerDlg::OnBnClickedStart() |
void CFlisServerDlg::OnBnClickedStart() |
823 |
{ |
{ |
824 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
825 |
|
|
826 |
continueThread = 1; |
continueThread = 1; |
827 |
m_Textwindow.SetWindowText("Started"); |
keepaliveandread(); |
828 |
|
Sleep(150); |
829 |
|
m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr ); |
830 |
|
AppendText("Started"); |
831 |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
832 |
} |
} |
833 |
void CFlisServerDlg::AppendText(CString s) |
void CFlisServerDlg::AppendText(CString s) |
873 |
} |
} |
874 |
|
|
875 |
file.close(); |
file.close(); |
|
} |
|
876 |
|
} |
877 |
|
void CFlisServerDlg::HandleAcknowledge(CString tlfnr) |
878 |
|
{ |
879 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
880 |
|
{ |
881 |
|
Acknowledge& current = (*it); |
882 |
|
if (current.sTlfNr == tlfnr) |
883 |
|
{ |
884 |
|
CString SQL; |
885 |
|
SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID); |
886 |
|
db.ExecuteSQL(SQL); |
887 |
|
|
888 |
|
Acks.erase(it); |
889 |
|
|
890 |
|
return; |
891 |
|
} |
892 |
|
} |
893 |
|
} |
894 |
|
|
895 |
|
void CFlisServerDlg::CheckAcknowledges() |
896 |
|
{ |
897 |
|
CTime now = CTime::GetCurrentTime(); |
898 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
899 |
|
{ |
900 |
|
Acknowledge& current = (*it); |
901 |
|
|
902 |
|
CTimeSpan elapsed = now-current.cTime; |
903 |
|
|
904 |
|
if (elapsed.GetTotalSeconds() >= 300) |
905 |
|
{ |
906 |
|
DumpAckList(); |
907 |
|
if (current.iRetry >= 1) |
908 |
|
{ |
909 |
|
CString SQL; |
910 |
|
SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID); |
911 |
|
db.ExecuteSQL(SQL); |
912 |
|
Acks.erase(it); |
913 |
|
return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again |
914 |
|
} |
915 |
|
else |
916 |
|
{ |
917 |
|
SendConfig("0","2",current.sInstallationID); |
918 |
|
current.iRetry++; |
919 |
|
current.cTime = CTime::GetCurrentTime(); |
920 |
|
AppendText( CString("Retry send config to ") + current.sTlfNr ); |
921 |
|
} |
922 |
|
} |
923 |
|
} |
924 |
|
} |
925 |
|
|
926 |
|
void CFlisServerDlg::DumpAckList() |
927 |
|
{ |
928 |
|
CTime now = CTime::GetCurrentTime(); |
929 |
|
OutputDebugString("-------------------------------\n"); |
930 |
|
int count = 0; |
931 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
932 |
|
{ |
933 |
|
CString msg; |
934 |
|
msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr); |
935 |
|
OutputDebugString(msg); |
936 |
|
count++; |
937 |
|
} |
938 |
|
} |
939 |
|
vector<keepalive> CFlisServerDlg::keepaliveandread(void) |
940 |
|
{ |
941 |
|
vector<keepalive> buffer; |
942 |
|
|
943 |
|
CString SQL, name, value; |
944 |
|
SQL = "select name, value from config;"; |
945 |
|
CRecordset rs(&db); |
946 |
|
try |
947 |
|
{ |
948 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
949 |
|
if (rs.GetRecordCount()>0) |
950 |
|
{ |
951 |
|
rs.MoveFirst(); |
952 |
|
|
953 |
|
rs.GetFieldValue((short)0, name); |
954 |
|
rs.GetFieldValue(1,value); |
955 |
|
|
956 |
|
Alive.name = name; |
957 |
|
Alive.value = value; |
958 |
|
Alive.tid = CTime::GetCurrentTime(); |
959 |
|
if(Alive.name == "phonenr") |
960 |
|
{ |
961 |
|
Alive.Phonenr = value; |
962 |
|
} |
963 |
|
|
964 |
|
buffer.push_back(Alive); |
965 |
|
rs.MoveNext(); |
966 |
|
|
967 |
|
} |
968 |
|
rs.Close(); |
969 |
|
return buffer; |
970 |
|
} |
971 |
|
catch (char * str) |
972 |
|
{ |
973 |
|
AppendText("Keepalive failed, closing and opening the db connection again"); |
974 |
|
if(db.IsOpen()) |
975 |
|
{ |
976 |
|
db.Close(); |
977 |
|
AppendText("DB connection closed"); |
978 |
|
} |
979 |
|
if(!db.IsOpen()) |
980 |
|
{ |
981 |
|
DBConnect(); |
982 |
|
AppendText("DB connection started again"); |
983 |
|
} |
984 |
|
} |
985 |
|
|
986 |
|
|
987 |
|
} |