/[H9]/trunk/FlisServer/FlisServerDlg.cpp
ViewVC logotype

Diff of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 149 by torben, Tue Dec 4 17:32:44 2007 UTC revision 191 by kevin, Sat Dec 8 09:45:58 2007 UTC
# Line 6  Line 6 
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
# Line 65  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo Line 66  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo
66          ON_WM_QUERYDRAGICON()          ON_WM_QUERYDRAGICON()
67          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
68          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
         ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)  
69          ON_BN_CLICKED(IDC_Start, OnBnClickedStart)          ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
70  END_MESSAGE_MAP()  END_MESSAGE_MAP()
71    
# Line 224  void CFlisServerDlg::writeFrame(std::vec Line 224  void CFlisServerDlg::writeFrame(std::vec
224  }  }
225  void CFlisServerDlg::SetPin()  void CFlisServerDlg::SetPin()
226  {  {
227            bool ready = true;
228          CString tekst;          CString tekst;
229          std::vector<unsigned char> data;          std::vector<unsigned char> data;
230    
231          data.push_back('a');          data.push_back('a');
232          data.push_back('t');          data.push_back('t');
233          data.push_back('+');          data.push_back('+');
# Line 233  void CFlisServerDlg::SetPin() Line 235  void CFlisServerDlg::SetPin()
235          data.push_back('p');          data.push_back('p');
236          data.push_back('i');          data.push_back('i');
237          data.push_back('n');          data.push_back('n');
238          data.push_back('=');          data.push_back('?');
         data.push_back('2');  
         data.push_back('5');  
         data.push_back('9');  
         data.push_back('5');  
239    
240          writeFrame(data);          writeFrame(data);
241            Sleep(100);
242            if(Serial.getComstat().cbInQue > 0)
243            {
244                    std::vector<unsigned char> answer = readFrame();
245                    Sleep(50);
246    
247                    for (int i=0; i<answer.size(); i++)
248                            {
249                                    if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
250                                    {
251                                            tekst.AppendChar(answer[i]);
252                                    }
253                            }
254                            AppendText(tekst);
255            }
256            Sleep(5);
257            tekst.MakeLower();
258            int pos = tekst.Find("+cpin: ",0);
259            if (pos != -1)
260            {
261                    tekst = tekst.Right( tekst.GetLength() - pos -1);
262            }
263    
264            if (tekst == "cpin: sim pin")
265            {
266                    CString pin;
267                    ConfigFile config;
268                    try {
269                            config.ReadSettings();
270                    } catch(...) {
271                            MessageBox("Could not open config file");
272                    }
273                    Sleep(50);
274    
275                    data.clear();
276                    data.push_back('a');
277                    data.push_back('t');
278                    data.push_back('+');
279                    data.push_back('c');
280                    data.push_back('p');
281                    data.push_back('i');
282                    data.push_back('n');
283                    data.push_back('=');
284                    for (int i = 0; i<config.pincode.GetLength(); i++)
285                    {
286                    data.push_back(config.pincode[i]);
287                    }
288    
289                    writeFrame(data);
290    
291                    Sleep(500);
292    
293                    while (ready == true)
294                    {
295                            if (Serial.getComstat().cbInQue > 120)
296                            {
297                                    std::vector<unsigned char> answer = readFrame();
298                                    CString tekst;
299                            
300                                    for (int i=0; i<answer.size(); i++)
301                                    {
302                                            if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
303                                            {
304                                                    tekst.AppendChar(answer[i]);
305                                            }
306                                    }
307                                    int n = tekst.Find("WIND: 11",0);
308                                    if (n > -1)
309                                    {
310                                            ready = false;
311                                    }
312                                    else
313                                    {
314                                            AppendText(tekst);
315                                    }
316                            }
317                            Sleep(10); //Small delay to avoid busy wait
318                    }
319            }
320            else if (tekst == "cpin: ready")
321            {
322                    AppendText("No pincode needed");
323            }
324            else
325            {
326                    AppendText("Error in set pin part of the code");
327            }
328  }  }
329  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
330  {        {      
# Line 340  vector<Commands> CFlisServerDlg::DBReadC Line 425  vector<Commands> CFlisServerDlg::DBReadC
425  }  }
426  void CFlisServerDlg::ReadSms()  void CFlisServerDlg::ReadSms()
427  {  {
428          CString tekst, oldtekst;          CString tekst;
429          Sleep(950);             //Holder en pause for at lade hele sms'en komme ind i serial køen.          Sleep(950);             //Holder en pause for at lade hele sms'en komme ind i serial køen.
430          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
431          {          {
# Line 361  void CFlisServerDlg::ReadSms() Line 446  void CFlisServerDlg::ReadSms()
446  }  }
447  void CFlisServerDlg::SmsSplit(CString data)  void CFlisServerDlg::SmsSplit(CString data)
448  {  {
449          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail;
450          char CharData[150];          char CharData[150];
451          strcpy(CharData,data);          strcpy(CharData,data);
452          TlfNr = data.Mid(24,8);          TlfNr = data.Mid(24,8);
# Line 373  void CFlisServerDlg::SmsSplit(CString da Line 458  void CFlisServerDlg::SmsSplit(CString da
458                  HandleAcknowledge(TlfNr);                  HandleAcknowledge(TlfNr);
459                  return;                  return;
460          }          }
461                    CAtlRegExp<> regex;
462            if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK)
463            {
464    
465                    CAtlREMatchContext<> pContext;
466    
467                    if (!regex.Match(FyrData, &pContext))
468                    {
469                            AppendText("Invalid SMS recieved");
470                            return;
471                    }
472            }
473            else
474            {
475                    AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske !
476            }
477    
478          SmsCount = Splitter(FyrData);          SmsCount = Splitter(FyrData);
479          Temper = Splitter(FyrData);          Temper = Splitter(FyrData);
480          Flamme = Splitter(FyrData);          Flamme = Splitter(FyrData);
# Line 421  void CFlisServerDlg::OnBnClickedClose() Line 522  void CFlisServerDlg::OnBnClickedClose()
522  {  {
523          // TODO: Add your control notification handler code here          // TODO: Add your control notification handler code here
524          continueThread = 0;          continueThread = 0;
525            Sleep(250);
526          DeleteSms();          DeleteSms();
527    
528          Sleep(500);          Sleep(500);
# Line 437  void CFlisServerDlg::OnBnClickedClose() Line 539  void CFlisServerDlg::OnBnClickedClose()
539          OnOK();          OnOK();
540    
541  }  }
   
 void CFlisServerDlg::OnBnClickedGsmpin()  
 {  
         // TODO: Add your control notification handler code here  
         bool ready = true;  
         m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar");  
         UpdateWindow();  
         SetPin();  
         Sleep(500);  
   
         while (ready == true)  
         {  
                 if (Serial.getComstat().cbInQue > 120)  
                 {  
                         std::vector<unsigned char> answer = readFrame();  
                         CString tekst;  
                   
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))  
                                 {  
                                         tekst.AppendChar(answer[i]);  
                                 }  
                         }  
                         int n = tekst.Find("WIND: 11",0);  
                         if (n > -1)  
                         {  
                                 ready = false;  
                         }  
                         else  
                         {  
                                 AppendText(tekst);  
                         }  
                 }  
                 Sleep(5); //Small delay to avoid busy wait  
         }  
         Sleep(100); //Give the modem a chance to send the last data  
         while(Serial.getComstat().cbInQue > 0)  
         {  
                 Serial.readByte(); //Flush the incoming queue  
                 Sleep(1);  
         }  
         OnBnClickedStart();  
 }  
542  void CFlisServerDlg::DeleteSms()  void CFlisServerDlg::DeleteSms()
543  {  {
544          vector<unsigned char> atcommand;          vector<unsigned char> atcommand;
# Line 515  void CFlisServerDlg::runthread() Line 573  void CFlisServerDlg::runthread()
573  {  {
574          while (continueThread != 0)          while (continueThread != 0)
575          {          {
576                  Reader();                  MyMainThread();
577          }          }
578  }  }
579  void CFlisServerDlg::Reader()  void CFlisServerDlg::MyMainThread()
580  {  {
581          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
582                  {                  {
583                          Sleep(250);                          Sleep(250);
584                          std::vector<unsigned char> answer = readFrame();                          std::vector<unsigned char> answer = readFrame();
585                          Sleep(500);                          Sleep(500);
586                          CString tekst, oldtekst;                          CString tekst;
                         int lol;  
587                                    
588                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
589                          {                          {
# Line 589  void CFlisServerDlg::Reader() Line 646  void CFlisServerDlg::Reader()
646                                                  {                                                  {
647                                                          data.push_back(smscount[i]);                                                          data.push_back(smscount[i]);
648                                                  }                                                  }
649                                                  m_Textwindow.GetWindowText(oldteskst);                                                  AppendText(CString(&data[0]));
                                                 oldteskst.Append("\r\n");  
                                                 for (int i=0; i<data.size();i++)  
                                                 {  
                                                         oldteskst.AppendChar(data[i]);  
                                                 }  
                                                 m_Textwindow.SetWindowText(oldteskst);  
650                                                  writeFrame(data);                                                  writeFrame(data);
651                                                  Sleep(200);                                                  Sleep(200);
652                                                  ReadSms();                                                  ReadSms();
# Line 653  void CFlisServerDlg::Reader() Line 704  void CFlisServerDlg::Reader()
704                          {                          {
705                                  SendConfig(sIDnr,sCommandID,sInstallationsID);                                  SendConfig(sIDnr,sCommandID,sInstallationsID);
706                          }                          }
707                          Sleep(500);                          Sleep(300);
708    
709                          CheckAcknowledges();                          CheckAcknowledges();
710                          Sleep(500);                          Sleep(500);
# Line 741  void CFlisServerDlg::SendConfig(CString Line 792  void CFlisServerDlg::SendConfig(CString
792                  CString SQL, Textwindow;                  CString SQL, Textwindow;
793                  SQL.Format("update command set executed=now() where id=%s",IDnr);                  SQL.Format("update command set executed=now() where id=%s",IDnr);
794                  db.ExecuteSQL(SQL);                  db.ExecuteSQL(SQL);
795                    Sleep(100);
796                  AppendText("Command executed");                  AppendText("Command executed");
797          }          }
798          Sleep(150);          Sleep(150);
# Line 803  int CFlisServerDlg::tversum(__int64 inpu Line 855  int CFlisServerDlg::tversum(__int64 inpu
855  void CFlisServerDlg::OnBnClickedStart()  void CFlisServerDlg::OnBnClickedStart()
856  {  {
857  // TODO: Add your control notification handler code here  // TODO: Add your control notification handler code here
858        
859            AppendText("Indsætter Pinkode, og venter på modem bliver klar, hvis pinkode er nødvendig");
860            UpdateWindow();
861            SetPin();
862            
863            Sleep(100); //Give the modem a chance to send the last data
864            while(Serial.getComstat().cbInQue > 0)
865            {
866                    Serial.readByte(); //Flush the incoming queue
867                    Sleep(1);
868            }
869        Sleep(5);
870    
871          continueThread = 1;          continueThread = 1;
872          keepaliveandread();          keepaliveandread();
873          Sleep(150);          Sleep(150);
874          m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr );          AppendText(CString("Server phonenr read from db: ") + Alive.Phonenr );
875          AppendText("Started");          AppendText("Started");
876          AfxBeginThread(threadWrapper,AfxGetMainWnd());          AfxBeginThread(threadWrapper,AfxGetMainWnd());
877  }  }
878  void CFlisServerDlg::AppendText(CString s)  void CFlisServerDlg::AppendText(CString s)
879  {  {
880            CTime now = CTime::GetCurrentTime();
881          CString Tekst;          CString Tekst;
882          m_Textwindow.GetWindowText(Tekst);          m_Textwindow.GetWindowText(Tekst);
883          Tekst.Append("\r\n");          Tekst.AppendFormat("%02d:%02d:%02d : ", now.GetHour(), now.GetMinute(), now.GetSecond());
884          Tekst.Append(s);          Tekst.Append(s);
885            Tekst.Append("\r\n");
886    
887          m_Textwindow.SetWindowText(Tekst);          m_Textwindow.SetWindowText(Tekst);
888            Sleep(5);
889            int g = m_Textwindow.GetScrollLimit(SB_VERT);
890            if (g > 0)
891            {
892            m_Textwindow.SetSel(m_Textwindow.GetWindowTextLength() -1,m_Textwindow.GetWindowTextLength(),false);
893            }
894    
895  }  }
896  void ConfigFile::ReadSettings()  void ConfigFile::ReadSettings()
# Line 849  void ConfigFile::ReadSettings() Line 922  void ConfigFile::ReadSettings()
922                                  database = value;                                  database = value;
923                          else if (key == "comport")                          else if (key == "comport")
924                                  comport = value;                                  comport = value;
925                            else if (key == "pincode")
926                                    pincode = value;
927                  }                  }
928    
929          }          }
# Line 904  void CFlisServerDlg::CheckAcknowledges() Line 979  void CFlisServerDlg::CheckAcknowledges()
979          }          }
980  }  }
981    
982  void CFlisServerDlg::DumpAckList()  void CFlisServerDlg::DumpAckList()      /* Debug tool */
983  {  {
984            
985          CTime now = CTime::GetCurrentTime();          CTime now = CTime::GetCurrentTime();
986          OutputDebugString("-------------------------------\n");          OutputDebugString("-------------------------------\n");
987          int count = 0;          int count = 0;
# Line 962  vector<keepalive> CFlisServerDlg::keepal Line 1038  vector<keepalive> CFlisServerDlg::keepal
1038                          DBConnect();                          DBConnect();
1039                          AppendText("DB connection started again");                          AppendText("DB connection started again");
1040                  }                  }
1041                    return buffer;
1042          }          }
1043    
1044                    
 }  
1045    }

Legend:
Removed from v.149  
changed lines
  Added in v.191

  ViewVC Help
Powered by ViewVC 1.1.20