/[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 132 by kevin, Mon Dec 3 11:46:03 2007 UTC revision 149 by torben, Tue Dec 4 17:32:44 2007 UTC
# Line 63  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo Line 63  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo
63          ON_WM_SYSCOMMAND()          ON_WM_SYSCOMMAND()
64          ON_WM_PAINT()          ON_WM_PAINT()
65          ON_WM_QUERYDRAGICON()          ON_WM_QUERYDRAGICON()
         ON_MESSAGE(UWM_MYMESSAGE, OnShowString)  
66          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
67          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
68          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
# Line 158  HCURSOR CFlisServerDlg::OnQueryDragIcon( Line 157  HCURSOR CFlisServerDlg::OnQueryDragIcon(
157  }  }
158  int CFlisServerDlg::StartSerial(void)  int CFlisServerDlg::StartSerial(void)
159  {  {
160            
161          int Baud;          int Baud;
         CString SerialPort = "COM3";  
162    
163            ConfigFile config;
164            try {
165                    config.ReadSettings();
166            } catch(...) {
167                    MessageBox("Could not open config file");
168                    OnOK();
169                    return true;
170            }
171            
172          Baud = 1200;          Baud = 1200;
173          if( Serial.isOpen() )          if( Serial.isOpen() )
174          {          {
175                  Serial.close();                  try
176                  Serial.open( SerialPort, Baud );                  {
177                            Serial.close();
178                            Serial.open( config.comport, Baud );
179                    }
180                    catch (exception* e)
181                    {
182                            MessageBox( "Serial.open() exception" );
183                            return 0;
184                    }
185          }          }
186          else          else
187          {          {
188                  Serial.open( SerialPort, Baud );                  try
189                    {
190                            Serial.open( config.comport, Baud );
191                    }
192                    catch (exception* e)
193                    {
194                            MessageBox( "Serial.open() exception 2" );
195                            return 0;
196                    }
197          }          }
198            
199                                    
200  return 0;  return 0;
201  }  }
# Line 232  void CFlisServerDlg::SendSmsData(std::ve Line 256  void CFlisServerDlg::SendSmsData(std::ve
256                  CString tekst;                  CString tekst;
257                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
258                  Sleep(50);                  Sleep(50);
259                          char array1[150];  
260                          int i;                  for (int i=0; i<answer.size(); i++)
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
                   
                         for (int i=0; i<answer.size(); i++)  
261                          {                          {
262                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
263                                  {                                  {
264                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
265                                  }                                  }
266                          }                          }
267                          m_Textwindow.SetWindowText(tekst);                          AppendText(tekst);
268          }          }
269    
270  }  }
271  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
272  {  {
273          vector<unsigned char> atcommand;          vector<unsigned char> atcommand;
274          atcommand.push_back('a');          atcommand.push_back('a');
# Line 270  void CFlisServerDlg::SendSmsHead(std::ve Line 288  void CFlisServerDlg::SendSmsHead(std::ve
288                  Sleep(5);                  Sleep(5);
289          }          }
290                    
291          for (int i=0; i<data.size(); i++)          for (int i=0; i<tlfnr.size(); i++)
292          {          {
293                  Serial.writeByte( data[i] );                  Serial.writeByte( tlfnr[i] );
294                  Sleep(5);                  Sleep(5);
295          }          }
296          Serial.writeByte(atcommand[s]);          Serial.writeByte(atcommand[s]);
# Line 282  void CFlisServerDlg::SendSmsHead(std::ve Line 300  void CFlisServerDlg::SendSmsHead(std::ve
300  void CFlisServerDlg::DBConnect()  void CFlisServerDlg::DBConnect()
301  {  {
302          CString dsn;          CString dsn;
303          //dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //Torben          ConfigFile config;
304          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //skole server          try {
305                    config.ReadSettings();
306            } catch(...) {
307                    MessageBox("Could not open config file");
308            }
309            
310            dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=%s; uid=%s;password=%s;database=%s;sslmode=prefer",config.host,config.username, config.password, config.database);
311          db.OpenEx(dsn, CDatabase::noOdbcDialog);          db.OpenEx(dsn, CDatabase::noOdbcDialog);
312  }  }
313  vector<Commands> CFlisServerDlg::DBRead(void)  vector<Commands> CFlisServerDlg::DBReadCommands(void)
314  {  {
315          vector<Commands> buffer;          vector<Commands> buffer;
316    
317          CString SQL, IDnr, CommandID, InstallationsID;          CString SQL, IDnr, CommandID, InstallationsID;
318          SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;";          SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL ORDER BY created ASC LIMIT 1;";
319          CRecordset rs(&db);          CRecordset rs(&db);
320          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
321          if (rs.GetRecordCount()>0)          if (rs.GetRecordCount()>0)
322          {          {
323                  rs.MoveFirst();                  rs.MoveFirst();
                 while(!rs.IsEOF())  
                 {  
                         Commands Mycom;  
                         rs.GetFieldValue((short)0, IDnr);  
                         rs.GetFieldValue(3, CommandID);  
                         rs.GetFieldValue(4, InstallationsID);  
324                                                    
325                          Mycom.IDnr = IDnr;                  Commands Mycom;
326                          Mycom.CommandID = CommandID;                  rs.GetFieldValue((short)0, IDnr);
327                          Mycom.InstallationsID = InstallationsID;                  rs.GetFieldValue(3, CommandID);
328                    rs.GetFieldValue(4, InstallationsID);
329                    
330                    Mycom.IDnr = IDnr;
331                    Mycom.CommandID = CommandID;
332                    Mycom.InstallationsID = InstallationsID;
333    
334                    buffer.push_back(Mycom);
335                    rs.MoveNext();
336    
                         buffer.push_back(Mycom);  
                         rs.MoveNext();  
                 }  
337          }          }
338          rs.Close();          rs.Close();
339          return buffer;          return buffer;
# Line 318  vector<Commands> CFlisServerDlg::DBRead( Line 341  vector<Commands> CFlisServerDlg::DBRead(
341  void CFlisServerDlg::ReadSms()  void CFlisServerDlg::ReadSms()
342  {  {
343          CString tekst, oldtekst;          CString tekst, oldtekst;
344          Sleep(950);          Sleep(950);             //Holder en pause for at lade hele sms'en komme ind i serial køen.
345          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
346          {          {
347                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
348                  Sleep(50);                  Sleep(50);
349                          char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
                   
350                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
351                          {                          {
352                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
353                                  {                                  {
354                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
355                                  }                                  }
356                          }                          }
357    
358                          m_Textwindow.GetWindowText(oldtekst);                          AppendText(tekst);
                         oldtekst.Append("\r\n");  
                         oldtekst.Append(tekst);  
                         m_Textwindow.SetWindowText(oldtekst);  
359                          SmsSplit(tekst);                          SmsSplit(tekst);
360          }          }
361  }  }
# Line 350  void CFlisServerDlg::SmsSplit(CString da Line 364  void CFlisServerDlg::SmsSplit(CString da
364          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
365          char CharData[150];          char CharData[150];
366          strcpy(CharData,data);          strcpy(CharData,data);
367            TlfNr = data.Mid(24,8);
368            FyrData = data.Mid(57,data.GetLength()-59);
369            FyrData.Append(":");
370    
371          int s=24;          if (FyrData.MakeLower() == "conf ok:")
         for (int i=0; i<=7; i++)  
         {  
                 TlfNr.AppendChar(CharData[s]);  
                 s++;  
         }  
   
         for (int s=57; s<=(data.GetLength()-3); s++)  
372          {          {
373                  FyrData.AppendChar(CharData[s]);                  HandleAcknowledge(TlfNr);
374                    return;
375          }          }
         FyrData.Append(":");  
376                    
377          SmsCount = Splitter(FyrData);          SmsCount = Splitter(FyrData);
378          Temper = Splitter(FyrData);          Temper = Splitter(FyrData);
# Line 384  void CFlisServerDlg::SmsSplit(CString da Line 394  void CFlisServerDlg::SmsSplit(CString da
394                  rs.Close();                  rs.Close();
395    
396                  SQL.Format("insert into logtable (logtime,temperature,flamedetector,solidfuelempty,conveyorerror,powerfailure,messagenr,installationnr) Values (now(),%s,'%s','%s','%s','%s',%s,%s)",Temper, Flamme, Flis, FremFejl, PowerFail, SmsCount,InstallNR);                  SQL.Format("insert into logtable (logtime,temperature,flamedetector,solidfuelempty,conveyorerror,powerfailure,messagenr,installationnr) Values (now(),%s,'%s','%s','%s','%s',%s,%s)",Temper, Flamme, Flis, FremFejl, PowerFail, SmsCount,InstallNR);
397                    try
398                    {
399                  db.ExecuteSQL(SQL);                  db.ExecuteSQL(SQL);
400                    }
401                  m_Textwindow.GetWindowText(Textwindow);                  catch(CDBException* e)
402                  Textwindow.Append("\r\n");                  {
403                  Textwindow.Append("Sms added to Log");                          MessageBox(e->m_strError);
404                  m_Textwindow.SetWindowText(Textwindow);                  }
405                    AppendText("Sms added to Log");
406                  Sleep(150);                  Sleep(150);
   
 ///////////////////////////////////////////////////////////////////////////////////////////////////////  
 ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  
407  }  }
408  CString CFlisServerDlg::Splitter(CString& fyrdata)  CString CFlisServerDlg::Splitter(CString& fyrdata)
409  {  {
# Line 431  void CFlisServerDlg::OnBnClickedClose() Line 441  void CFlisServerDlg::OnBnClickedClose()
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()
# Line 461  void CFlisServerDlg::DeleteSms() Line 499  void CFlisServerDlg::DeleteSms()
499          writeFrame(atcommand);          writeFrame(atcommand);
500          Sleep(500);          Sleep(500);
501  }  }
502  LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam)  UINT threadWrapper(LPVOID thread)
 {  
         CString *s = (CString*) lParam;  
         GetDlgItem(IDC_Textwindow)->SetWindowText(*s);  
   
         delete s;  
         return 0;  
   
 }UINT threadWrapper(LPVOID thread)  
503  {  {
504          CFlisServerDlg *t = (CFlisServerDlg*) thread;          CFlisServerDlg *t = (CFlisServerDlg*) thread;
505          t->runthread();          t->runthread();
# Line 497  void CFlisServerDlg::Reader() Line 527  void CFlisServerDlg::Reader()
527                          Sleep(500);                          Sleep(500);
528                          CString tekst, oldtekst;                          CString tekst, oldtekst;
529                          int lol;                          int lol;
                         char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
530                                    
531                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
532                          {                          {
533                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
534                                  {                                  {
535                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
536                                  }                                  }
537                          }                          }
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                                  {                                  {
# Line 526  void CFlisServerDlg::Reader() Line 550  void CFlisServerDlg::Reader()
550                                  }                                  }
551                                                    
552                                                    
553                          if(tekst == "OK")                          if(tekst.MakeLower() == "ok")
554                          {                                {      
555                                  m_Textwindow.GetWindowText(oldtekst);                                  AppendText("OK tekst modtaget");
556                                  oldtekst.Append("\r\n");                          }
557                                  oldtekst.Append("OK tekst modtaget");                          else if (tekst.MakeLower() == "error")
558                                  m_Textwindow.SetWindowText(oldtekst);                          {
559                          }                                  CString send;
560                          else if (tekst == "error")                                  send.Append("error tekst");
561                          {                                  send.Append("\r\n");
562                                  m_Textwindow.GetWindowText(oldtekst);                                  send.Append(tekst);
563                                  oldtekst.Append("\r\n");                                  AppendText(send);
                                 oldtekst.Append("error tekst");  
                                 oldtekst.Append(tekst);  
                                 m_Textwindow.SetWindowText(oldtekst);  
564                          }                          }
565                          else if (plus = true)                          else if (plus == true)
566                          {                          {
567                                  if (command = "cmti")                                  if (command.MakeLower() == "cmti")
568                                  {                                  {
569                                          CString smscount, oldteskst;                                          CString smscount, oldteskst;
570                                          int pos = tekst.Find(',',0);                                          int pos = tekst.Find(',',0);
# Line 581  void CFlisServerDlg::Reader() Line 602  void CFlisServerDlg::Reader()
602                                                  ResetSms++;                                                  ResetSms++;
603    
604                                  }                                  }
605                                  else if(command = "wind")                                  else if(command.MakeLower() == "wind")
606                                  {                                  {
607                                          m_Textwindow.GetWindowText(oldtekst);  
608                                          oldtekst.Append("\r\n");                                          CString send;
609                                          oldtekst.Append("Først lidt tekst på næste linie\r\n");                                          send.Append("Wind modtaget");
610                                          oldtekst.Append(tekst);                                          send.Append("\r\n");
611                                          m_Textwindow.SetWindowText(oldtekst);                                          send.Append(tekst);
612                                            AppendText(send);
613                                  }                                  }
614                          }                          }
615                          else                          else
616                          {                          {
617                                  m_Textwindow.GetWindowText(oldtekst);                                  AppendText(tekst);
                                 oldtekst.Append("\r\n");  
                                 oldtekst.Append("Anden tekst end forventet");  
                                 oldtekst.Append("\r\n");  
                                 oldtekst.Append(tekst);  
                                 m_Textwindow.SetWindowText(oldtekst);  
618                          }                          }
619                          if(ResetSms == 20)                          if(ResetSms == 20)
620                          {                          {
621                                  DeleteSms();                                  DeleteSms();
622                                  m_Textwindow.GetWindowText(oldtekst);                                  AppendText("Sms'er slettet da vi nåede grænsen");
                                 oldtekst.Append("\r\n");  
                                 oldtekst.Append("Slettet Sendte og læste beskeder da vi nåede grænsen.");  
                                 m_Textwindow.SetWindowText(oldtekst);  
623                          }                          }
624    
625                  }                  }
626                  if (continueThread = 1)                  if (continueThread == 1)
627                  {                  {
628                                                    
629                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
630                          int commandtest = 0;                          int commandtest = 0;
631                          int iAll = 1;                          int iAll = 1;
632                          std::vector<Commands> data;                          std::vector<Commands> data;
633                          data = DBRead();                          data = DBReadCommands();
634                          for (int i=0; i<data.size(); i++)                          for (int i=0; i<data.size(); i++)
635                          {                          {
636                                  testdata.Append(data[i].IDnr);                                  sIDnr = data[i].IDnr;
637                                  testdata.Append(":");                                  sCommandID = data[i].CommandID;
                                 testdata.Append(data[i].CommandID);  
                                 testdata.Append(":");  
638                                  commandtest = atoi(data[i].CommandID);                                  commandtest = atoi(data[i].CommandID);
639                                    sInstallationsID = "0";
640    
641                                  if(commandtest > 1)                                  if(commandtest > 1)
642                                  {                                  {
643                                          testdata.Append(data[i].InstallationsID);                                          sInstallationsID = data[i].InstallationsID;
                                         testdata.Append(":");  
644                                  }                                  }
645                          }                          }
646                          sIDnr = Splitter(testdata);                          
                         sCommandID = Splitter(testdata);  
                         sInstallationsID = "0";  
647                          if (commandtest > 1)                          if (commandtest > 1)
648                          {                          {
                                 sInstallationsID = Splitter(testdata);  
649                                  iAll = 0;                                  iAll = 0;
650                          }                          }
651    
652                          if (sIDnr.GetLength() > 0)                          if (sIDnr.GetLength() > 0)
653                          {                          {
654                                  DBReadData(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::DBReadData(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++;  
           
         vector<unsigned char> tlfnr;  
         for (int i=0; i<TlfNr.GetLength(); i++)  
         {  
                 tlfnr.push_back(TlfNr[i]);  
         }  
693                    
694          int calcimei;                  vector<unsigned char> tlfnr;
695          __int64 buf;                  for (int i=0; i<TlfNr.GetLength(); i++)
696                    {
697                            tlfnr.push_back(TlfNr[i]);
698                    }
699                    
700                    int calcimei;
701                    __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                  m_Textwindow.GetWindowText(Textwindow);                  AppendText("Command executed");
745                  Textwindow.Append("\r\n");          }
746                  Textwindow.Append("Command executed");          Sleep(150);
747                  m_Textwindow.SetWindowText(Textwindow);  
748                  Sleep(150);  
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)
# Line 747  vector<Installation> CFlisServerDlg::DBR Line 776  vector<Installation> CFlisServerDlg::DBR
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();
# Line 772  int CFlisServerDlg::tversum(__int64 inpu Line 803  int CFlisServerDlg::tversum(__int64 inpu
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)
815    {
816            CString Tekst;
817            m_Textwindow.GetWindowText(Tekst);
818            Tekst.Append("\r\n");
819            Tekst.Append(s);
820            m_Textwindow.SetWindowText(Tekst);
821    
822    }
823    void ConfigFile::ReadSettings()
824    {
825            ifstream file("Server-Settings.ini");
826            if (!file.is_open())
827                    throw("Could not open file");
828    
829            char buf[200];
830            while (!file.eof() ) {
831                    file.getline(buf,200);
832                    CString tmp(buf);
833    
834                    if (tmp.GetAt(0) == '#')
835                            continue;
836    
837                    int pos = tmp.Find('=');
838                    if (pos>0) {
839                            CString key = tmp.Left(pos).Trim().MakeLower();;
840                            CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
841    
842                            if (key == "host")
843                                    host = value;
844                            else if (key == "username")
845                                    username = value;
846                            else if (key == "password")
847                                    password = value;
848                            else if (key == "database")
849                                    database = value;
850                            else if (key == "comport")
851                                    comport = value;
852                    }
853    
854            }
855    
856            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    }

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

  ViewVC Help
Powered by ViewVC 1.1.20