367 |
TlfNr = data.Mid(24,8); |
TlfNr = data.Mid(24,8); |
368 |
FyrData = data.Mid(57,data.GetLength()-59); |
FyrData = data.Mid(57,data.GetLength()-59); |
369 |
FyrData.Append(":"); |
FyrData.Append(":"); |
370 |
|
|
371 |
|
if (FyrData.MakeLower() == "conf ok:") |
372 |
|
{ |
373 |
|
HandleAcknowledge(TlfNr); |
374 |
|
return; |
375 |
|
} |
376 |
|
|
377 |
SmsCount = Splitter(FyrData); |
SmsCount = Splitter(FyrData); |
378 |
Temper = Splitter(FyrData); |
Temper = Splitter(FyrData); |
441 |
void CFlisServerDlg::OnBnClickedGsmpin() |
void CFlisServerDlg::OnBnClickedGsmpin() |
442 |
{ |
{ |
443 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
444 |
m_Textwindow.SetWindowText("Indsætter Pinkode, efterfuldt af 60sec pause"); |
bool ready = true; |
445 |
|
m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar"); |
446 |
UpdateWindow(); |
UpdateWindow(); |
447 |
SetPin(); |
SetPin(); |
448 |
Sleep(50000); |
Sleep(500); |
449 |
|
|
450 |
while (Serial.getComstat().cbInQue > 0) |
while (ready == true) |
451 |
|
{ |
452 |
|
if (Serial.getComstat().cbInQue > 120) |
453 |
|
{ |
454 |
|
std::vector<unsigned char> answer = readFrame(); |
455 |
|
CString tekst; |
456 |
|
|
457 |
|
for (int i=0; i<answer.size(); i++) |
458 |
|
{ |
459 |
|
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
460 |
|
{ |
461 |
|
tekst.AppendChar(answer[i]); |
462 |
|
} |
463 |
|
} |
464 |
|
int n = tekst.Find("WIND: 11",0); |
465 |
|
if (n > -1) |
466 |
|
{ |
467 |
|
ready = false; |
468 |
|
} |
469 |
|
else |
470 |
|
{ |
471 |
|
AppendText(tekst); |
472 |
|
} |
473 |
|
} |
474 |
|
Sleep(5); //Small delay to avoid busy wait |
475 |
|
} |
476 |
|
Sleep(100); //Give the modem a chance to send the last data |
477 |
|
while(Serial.getComstat().cbInQue > 0) |
478 |
{ |
{ |
479 |
Serial.readByte(); //Flush the incoming queue |
Serial.readByte(); //Flush the incoming queue |
480 |
|
Sleep(1); |
481 |
} |
} |
|
|
|
482 |
OnBnClickedStart(); |
OnBnClickedStart(); |
483 |
} |
} |
484 |
void CFlisServerDlg::DeleteSms() |
void CFlisServerDlg::DeleteSms() |
538 |
tekst.Append(":"); |
tekst.Append(":"); |
539 |
|
|
540 |
CString command; |
CString command; |
541 |
bool plus; |
bool plus = false; |
542 |
int pos = tekst.Find('+',0); |
int pos = tekst.Find('+',0); |
543 |
if (pos != -1) |
if (pos != -1) |
544 |
{ |
{ |
614 |
} |
} |
615 |
else |
else |
616 |
{ |
{ |
617 |
CString send; |
AppendText(tekst); |
|
send.Append("Anden tekst end forventet"); |
|
|
send.Append("\r\n"); |
|
|
send.Append(tekst); |
|
|
AppendText(send); |
|
618 |
} |
} |
619 |
if(ResetSms == 20) |
if(ResetSms == 20) |
620 |
{ |
{ |
654 |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
655 |
} |
} |
656 |
Sleep(500); |
Sleep(500); |
657 |
|
|
658 |
|
CheckAcknowledges(); |
659 |
|
Sleep(500); |
660 |
|
} |
661 |
|
CTime now = CTime::GetCurrentTime(); |
662 |
|
|
663 |
|
CTimeSpan elapsed = now-Alive.tid; |
664 |
|
|
665 |
|
if (elapsed.GetTotalSeconds() >= 900) |
666 |
|
{ |
667 |
|
keepaliveandread(); |
668 |
} |
} |
669 |
} |
} |
670 |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
671 |
{ |
{ |
|
CString ServerTlfNr; |
|
|
int i = 0; |
|
|
ServerTlfNr = "29860132"; |
|
|
|
|
672 |
|
|
673 |
std::vector<Installation> inst; |
std::vector<Installation> inst; |
674 |
inst = DBReadPhone(InstallationsID); |
|
675 |
while (i < inst.size()) |
if (CommandID == "1") |
676 |
|
inst = DBReadPhone("0"); |
677 |
|
else |
678 |
|
inst = DBReadPhone(InstallationsID); |
679 |
|
|
680 |
|
for ( int j=0; j < inst.size(); j++) |
681 |
{ |
{ |
682 |
CString TlfNr, Imei, updaterate; |
CString TlfNr, Imei, updaterate; |
683 |
TlfNr.Empty(); |
TlfNr.Empty(); |
684 |
Imei.Empty(); |
Imei.Empty(); |
685 |
updaterate.Empty(); |
updaterate.Empty(); |
686 |
|
|
687 |
CString dataen = inst[i].InstPhoneNr; |
CString dataen = inst[j].InstPhoneNr; |
688 |
TlfNr.Append(dataen); |
TlfNr.Append(dataen); |
689 |
CString Imeidata = inst[i].Imei; |
CString Imeidata = inst[j].Imei; |
690 |
Imei.Append(Imeidata); |
Imei.Append(Imeidata); |
691 |
CString updaterat = inst[i].Updaterate; |
CString updaterat = inst[j].Updaterate; |
692 |
updaterate.Append(updaterat); |
updaterate.Append(updaterat); |
|
i++; |
|
693 |
|
|
694 |
vector<unsigned char> tlfnr; |
vector<unsigned char> tlfnr; |
695 |
for (int i=0; i<TlfNr.GetLength(); i++) |
for (int i=0; i<TlfNr.GetLength(); i++) |
696 |
{ |
{ |
697 |
tlfnr.push_back(TlfNr[i]); |
tlfnr.push_back(TlfNr[i]); |
698 |
} |
} |
699 |
|
|
700 |
int calcimei; |
int calcimei; |
701 |
__int64 buf; |
__int64 buf; |
702 |
|
|
703 |
buf = atof(Imei); |
buf = atof(Imei); |
704 |
|
|
705 |
calcimei = tversum(buf); |
calcimei = tversum(buf); |
706 |
|
|
707 |
Imei.Format("%d",calcimei); |
Imei.Format("%d",calcimei); |
708 |
|
|
709 |
SendSmsHead(tlfnr); |
SendSmsHead(tlfnr); |
710 |
Sleep(250); |
Sleep(250); |
711 |
|
|
712 |
vector<unsigned char> smsdata; |
vector<unsigned char> smsdata; |
713 |
|
|
714 |
for (int i=0; i<Imei.GetLength(); i++) |
for (int i=0; i<Imei.GetLength(); i++) |
715 |
{ |
{ |
716 |
smsdata.push_back(Imei[i]); |
smsdata.push_back(Imei[i]); |
717 |
} |
} |
718 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
719 |
for (int i=0; i<ServerTlfNr.GetLength(); i++) |
for (int i=0; i<Alive.Phonenr.GetLength(); i++) |
720 |
{ |
{ |
721 |
smsdata.push_back(ServerTlfNr[i]); |
smsdata.push_back(Alive.Phonenr[i]); |
722 |
} |
} |
723 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
724 |
for (int i=0; i<updaterate.GetLength(); i++) |
for (int i=0; i<updaterate.GetLength(); i++) |
725 |
{ |
{ |
726 |
smsdata.push_back(updaterate[i]); |
smsdata.push_back(updaterate[i]); |
727 |
} |
} |
728 |
|
|
729 |
|
SendSmsData(smsdata); |
730 |
|
Sleep(500); |
731 |
|
|
732 |
|
if (IDnr != "0") |
733 |
|
{ |
734 |
|
Acks.push_back( Acknowledge(inst[j].instID,TlfNr) ); |
735 |
|
} |
736 |
|
|
|
SendSmsData(smsdata); |
|
|
Sleep(500); |
|
|
|
|
737 |
} |
} |
738 |
|
|
739 |
|
if (IDnr != "0") |
740 |
|
{ |
741 |
CString SQL, Textwindow; |
CString SQL, Textwindow; |
742 |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
743 |
db.ExecuteSQL(SQL); |
db.ExecuteSQL(SQL); |
744 |
AppendText("Command executed"); |
AppendText("Command executed"); |
745 |
Sleep(150); |
} |
746 |
|
Sleep(150); |
747 |
|
|
748 |
|
|
749 |
|
|
750 |
|
|
751 |
} |
} |
752 |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
753 |
{ |
{ |
754 |
vector<Installation> buffer; |
vector<Installation> buffer; |
755 |
|
|
756 |
CString SQL, phonenr, imei, updaterate; |
CString SQL, phonenr, imei, updaterate, id; |
|
int installernull; |
|
757 |
Installation Myinst; |
Installation Myinst; |
758 |
installernull = atoi(sInstallationsID); |
|
759 |
if (installernull > 1) |
if (sInstallationsID != "0") |
760 |
{ |
{ |
761 |
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); |
762 |
} |
} |
763 |
else if(installernull < 2) |
else |
764 |
{ |
{ |
765 |
SQL.Format("select installationphonenr, imei, updaterate from installation"); |
SQL.Format("select installationphonenr, imei, updaterate, id from installation"); |
766 |
} |
} |
767 |
|
|
768 |
CRecordset rs(&db); |
CRecordset rs(&db); |
769 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
770 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
776 |
rs.GetFieldValue((short)0,phonenr); |
rs.GetFieldValue((short)0,phonenr); |
777 |
rs.GetFieldValue(1,imei); |
rs.GetFieldValue(1,imei); |
778 |
rs.GetFieldValue(2,updaterate); |
rs.GetFieldValue(2,updaterate); |
779 |
|
rs.GetFieldValue(3,id); |
780 |
|
|
781 |
Myinst.InstPhoneNr = phonenr; |
Myinst.InstPhoneNr = phonenr; |
782 |
Myinst.Imei = imei; |
Myinst.Imei = imei; |
783 |
Myinst.Updaterate = updaterate; |
Myinst.Updaterate = updaterate; |
784 |
|
Myinst.instID = id; |
785 |
|
|
786 |
buffer.push_back(Myinst); |
buffer.push_back(Myinst); |
787 |
rs.MoveNext(); |
rs.MoveNext(); |
803 |
void CFlisServerDlg::OnBnClickedStart() |
void CFlisServerDlg::OnBnClickedStart() |
804 |
{ |
{ |
805 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
806 |
|
|
807 |
continueThread = 1; |
continueThread = 1; |
808 |
m_Textwindow.SetWindowText("Started"); |
keepaliveandread(); |
809 |
|
Sleep(150); |
810 |
|
m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr ); |
811 |
|
AppendText("Started"); |
812 |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
813 |
} |
} |
814 |
void CFlisServerDlg::AppendText(CString s) |
void CFlisServerDlg::AppendText(CString s) |
854 |
} |
} |
855 |
|
|
856 |
file.close(); |
file.close(); |
857 |
|
} |
858 |
|
void CFlisServerDlg::HandleAcknowledge(CString tlfnr) |
859 |
|
{ |
860 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
861 |
|
{ |
862 |
|
Acknowledge& current = (*it); |
863 |
|
if (current.sTlfNr == tlfnr) |
864 |
|
{ |
865 |
|
CString SQL; |
866 |
|
SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID); |
867 |
|
db.ExecuteSQL(SQL); |
868 |
|
|
869 |
|
Acks.erase(it); |
870 |
|
|
871 |
|
return; |
872 |
|
} |
873 |
|
} |
874 |
|
} |
875 |
|
|
876 |
|
void CFlisServerDlg::CheckAcknowledges() |
877 |
|
{ |
878 |
|
CTime now = CTime::GetCurrentTime(); |
879 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
880 |
|
{ |
881 |
|
Acknowledge& current = (*it); |
882 |
|
|
883 |
|
CTimeSpan elapsed = now-current.cTime; |
884 |
|
|
885 |
|
if (elapsed.GetTotalSeconds() >= 300) |
886 |
|
{ |
887 |
|
DumpAckList(); |
888 |
|
if (current.iRetry >= 1) |
889 |
|
{ |
890 |
|
CString SQL; |
891 |
|
SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID); |
892 |
|
db.ExecuteSQL(SQL); |
893 |
|
Acks.erase(it); |
894 |
|
return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again |
895 |
|
} |
896 |
|
else |
897 |
|
{ |
898 |
|
SendConfig("0","2",current.sInstallationID); |
899 |
|
current.iRetry++; |
900 |
|
current.cTime = CTime::GetCurrentTime(); |
901 |
|
AppendText( CString("Retry send config to ") + current.sTlfNr ); |
902 |
|
} |
903 |
|
} |
904 |
|
} |
905 |
|
} |
906 |
|
|
907 |
|
void CFlisServerDlg::DumpAckList() |
908 |
|
{ |
909 |
|
CTime now = CTime::GetCurrentTime(); |
910 |
|
OutputDebugString("-------------------------------\n"); |
911 |
|
int count = 0; |
912 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
913 |
|
{ |
914 |
|
CString msg; |
915 |
|
msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr); |
916 |
|
OutputDebugString(msg); |
917 |
|
count++; |
918 |
|
} |
919 |
|
} |
920 |
|
vector<keepalive> CFlisServerDlg::keepaliveandread(void) |
921 |
|
{ |
922 |
|
vector<keepalive> buffer; |
923 |
|
|
924 |
|
CString SQL, name, value; |
925 |
|
SQL = "select name, value from config;"; |
926 |
|
CRecordset rs(&db); |
927 |
|
try |
928 |
|
{ |
929 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
930 |
|
if (rs.GetRecordCount()>0) |
931 |
|
{ |
932 |
|
rs.MoveFirst(); |
933 |
|
|
934 |
|
rs.GetFieldValue((short)0, name); |
935 |
|
rs.GetFieldValue(1,value); |
936 |
|
|
937 |
|
Alive.name = name; |
938 |
|
Alive.value = value; |
939 |
|
Alive.tid = CTime::GetCurrentTime(); |
940 |
|
if(Alive.name == "phonenr") |
941 |
|
{ |
942 |
|
Alive.Phonenr = value; |
943 |
|
} |
944 |
|
|
945 |
|
buffer.push_back(Alive); |
946 |
|
rs.MoveNext(); |
947 |
|
|
948 |
|
} |
949 |
|
rs.Close(); |
950 |
|
return buffer; |
951 |
|
} |
952 |
|
catch (char * str) |
953 |
|
{ |
954 |
|
AppendText("Keepalive failed, closing and opening the db connection again"); |
955 |
|
if(db.IsOpen()) |
956 |
|
{ |
957 |
|
db.Close(); |
958 |
|
AppendText("DB connection closed"); |
959 |
|
} |
960 |
|
if(!db.IsOpen()) |
961 |
|
{ |
962 |
|
DBConnect(); |
963 |
|
AppendText("DB connection started again"); |
964 |
|
} |
965 |
|
} |
966 |
|
|
967 |
|
|
968 |
} |
} |