/[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 130 by kevin, Mon Dec 3 09:22:26 2007 UTC revision 158 by kevin, Wed Dec 5 11:54:09 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                  Serial.close();          {
175                  Serial.open( SerialPort, Baud );                  try
176                    {
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                  Serial.open( SerialPort, Baud );          {
188                    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 178  std::vector<unsigned char> CFlisServerDl Line 204  std::vector<unsigned char> CFlisServerDl
204          std::vector<unsigned char> buf;          std::vector<unsigned char> buf;
205          while(Serial.getComstat().cbInQue > 0)          while(Serial.getComstat().cbInQue > 0)
206          {          {
207          unsigned char data = Serial.readByte();                  unsigned char data = Serial.readByte();
208    
209          buf.push_back(data);                  buf.push_back(data);
210          }          }
211          return buf;          return buf;
212  }  }
# Line 214  void CFlisServerDlg::SetPin() Line 240  void CFlisServerDlg::SetPin()
240          data.push_back('5');          data.push_back('5');
241    
242          writeFrame(data);          writeFrame(data);
 /*      Sleep(750);  
         if(Serial.getComstat().cbInQue > 0)  
         {  
                 std::vector<unsigned char> answer = readFrame();  
                 Sleep(50);  
                         char array1[25];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
                   
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 if ((array1[i] != 0x0A) && (array1[i] != 0x0D))  
                                 {  
                                         tekst.AppendChar(array1[i]);  
                                 }  
                         }  
                         m_Textwindow.SetWindowText(tekst);  
         }  
 */  
243  }  }
244  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
245  {        {      
# Line 252  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 290  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 302  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 338  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 370  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 390  void CFlisServerDlg::SmsSplit(CString da Line 380  void CFlisServerDlg::SmsSplit(CString da
380          Flis = Splitter(FyrData);          Flis = Splitter(FyrData);
381          FremFejl = Splitter(FyrData);          FremFejl = Splitter(FyrData);
382          PowerFail = Splitter(FyrData);          PowerFail = Splitter(FyrData);
383  ///////////////////////////////////////////////////////////////////////////////////////////////////////                  
384  ///////////////////// Her skal sendes data til databasen ////NÅR HEDIN KAN SENDE SMS///////////////////                  CString SQL, Textwindow, InstallNR;
385                  m_Textwindow.GetWindowText(oldtekst);                  SQL.Format("select ID from installation where installationphonenr=%s",TlfNr);
386                  oldtekst.Append("\r\n");          
387                  oldtekst.Append("På næste linie kommer SmsCount. \r\n");                  CRecordset rs(&db);
388                  oldtekst.Append(SmsCount);                  rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
389                  oldtekst.Append("\r\n");                  if (rs.GetRecordCount()>0)
390                  oldtekst.Append("På næste linie kommer Temper. \r\n");                  {
391                  oldtekst.Append(Temper);                          rs.MoveFirst();        
392                  oldtekst.Append("\r\n");                          rs.GetFieldValue((short)0,InstallNR);
393                  oldtekst.Append("På næste linie kommer Flamme. \r\n");                  }
394                  oldtekst.Append(Flamme);                  rs.Close();
395                  oldtekst.Append("\r\n");  
396                  oldtekst.Append("På næste linie kommer Flis. \r\n");                  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                  oldtekst.Append(Flis);                  try
398                  oldtekst.Append("\r\n");                  {
399                  oldtekst.Append("På næste linie kommer FremFejl. \r\n");                  db.ExecuteSQL(SQL);
400                  oldtekst.Append(FremFejl);                  }
401                  oldtekst.Append("\r\n");                  catch(CDBException* e)
402                  oldtekst.Append("På næste linie kommer PowerFail. \r\n");                  {
403                  oldtekst.Append(PowerFail);                          MessageBox(e->m_strError);
404                  oldtekst.Append("\r\n");                  }
405                  oldtekst.Append("På næste linie kommer TlfNR. \r\n");                  AppendText("Sms added to Log");
406                  oldtekst.Append(TlfNr);                  Sleep(150);
                 m_Textwindow.SetWindowText(oldtekst);  
 ///////////////////////////////////////////////////////////////////////////////////////////////////////  
 ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  
407  }  }
408  CString CFlisServerDlg::Splitter(CString& fyrdata)  CString CFlisServerDlg::Splitter(CString& fyrdata)
409  {  {
# Line 434  void CFlisServerDlg::OnBnClickedClose() Line 421  void CFlisServerDlg::OnBnClickedClose()
421  {  {
422          // TODO: Add your control notification handler code here          // TODO: Add your control notification handler code here
423          continueThread = 0;          continueThread = 0;
424          DeleteSms();   //<--- crasher, men gider ikke bruge flere af mine penge på at teste det med sms'er, hele close laver crash, bare ikke når jeg stepper. :S          DeleteSms();
425    
426          Sleep(500);          Sleep(500);
427          if( Serial.isOpen() )          if( Serial.isOpen() )
# Line 444  void CFlisServerDlg::OnBnClickedClose() Line 431  void CFlisServerDlg::OnBnClickedClose()
431                    
432          if(db.IsOpen())          if(db.IsOpen())
433          {          {
434          db.Close();                  db.Close();
435          }          }
436                    
437          OnOK();          OnOK();
# Line 454  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            bool ready = true;
445            m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar");
446            UpdateWindow();
447          SetPin();          SetPin();
448          Sleep(35000);          Sleep(500);
449    
450            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(10); //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
480                    Sleep(1);
481            }
482          OnBnClickedStart();          OnBnClickedStart();
483  }  }
484  void CFlisServerDlg::DeleteSms()  void CFlisServerDlg::DeleteSms()
# Line 476  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 512  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 541  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");
                                 oldtekst.Append("\r\n");  
                                 oldtekst.Append("OK tekst modtaget");  
                                 m_Textwindow.SetWindowText(oldtekst);  
                         }  
                         else if (tekst == "error")  
                         {  
                                 m_Textwindow.GetWindowText(oldtekst);  
                                 oldtekst.Append("\r\n");  
                                 oldtekst.Append("error tekst");  
                                 oldtekst.Append(tekst);  
                                 m_Textwindow.SetWindowText(oldtekst);  
556                          }                          }
557                          else if (plus = true)                          else if (tekst.MakeLower() == "error")
558                          {                          {
559                                  if (command = "cmti")                                  CString send;
560                                    send.Append("error tekst");
561                                    send.Append("\r\n");
562                                    send.Append(tekst);
563                                    AppendText(send);
564                            }
565                            else if (plus == true)
566                            {
567                                    if (command.MakeLower() == "cmti")
568                                  {                                  {
569                                          CString smscount, oldteskst;                                          CString smscount, oldteskst;
570                                          int pos = tekst.Find(',',0);                                          int pos = tekst.Find(',',0);
# Line 583  void CFlisServerDlg::Reader() Line 589  void CFlisServerDlg::Reader()
589                                                  {                                                  {
590                                                          data.push_back(smscount[i]);                                                          data.push_back(smscount[i]);
591                                                  }                                                  }
592                                                    m_Textwindow.GetWindowText(oldteskst);
593                                                    oldteskst.Append("\r\n");
594                                                  for (int i=0; i<data.size();i++)                                                  for (int i=0; i<data.size();i++)
595                                                  {                                                  {
596                                                  oldteskst.AppendChar(data[i]);                                                          oldteskst.AppendChar(data[i]);
597                                                  }                                                  }
598                                                  m_Textwindow.SetWindowText(oldteskst);                                                  m_Textwindow.SetWindowText(oldteskst);
599                                                  writeFrame(data);                                                  writeFrame(data);
# Line 594  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                          if(ResetSms == 50)                          else
616                            {
617                                    AppendText(tekst);
618                            }
619                            if(ResetSms == 20)
620                          {                          {
621                                  DeleteSms();                                  DeleteSms();
622                                    AppendText("Sms'er slettet da vi nåede grænsen");
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                          {                          {
649                          sInstallationsID = Splitter(testdata);                                  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);                  Sleep(10);
745                  Textwindow.Append("\r\n");                  AppendText("Command executed");
746                  Textwindow.Append("Command executed");          }
747                  m_Textwindow.SetWindowText(Textwindow);          Sleep(150);
748                  Sleep(150);  
749    
750    
751    
752  }  }
753  vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)  vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
754  {  {
755          vector<Installation> buffer;          vector<Installation> buffer;
756    
757          CString SQL, phonenr, imei, updaterate;          CString SQL, phonenr, imei, updaterate, id;
         int installernull;  
758          Installation Myinst;          Installation Myinst;
759          installernull = atoi(sInstallationsID);  
760          if (installernull > 1)          if (sInstallationsID != "0")
761          {          {
762                  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);
763          }          }
764          else if(installernull < 2)          else
765          {          {
766                  SQL.Format("select installationphonenr, imei, updaterate from installation");                  SQL.Format("select installationphonenr, imei, updaterate, id from installation");
767          }          }
768    
769          CRecordset rs(&db);          CRecordset rs(&db);
770          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
771          if (rs.GetRecordCount()>0)          if (rs.GetRecordCount()>0)
# Line 747  vector<Installation> CFlisServerDlg::DBR Line 777  vector<Installation> CFlisServerDlg::DBR
777                          rs.GetFieldValue((short)0,phonenr);                          rs.GetFieldValue((short)0,phonenr);
778                          rs.GetFieldValue(1,imei);                          rs.GetFieldValue(1,imei);
779                          rs.GetFieldValue(2,updaterate);                          rs.GetFieldValue(2,updaterate);
780                            rs.GetFieldValue(3,id);
781    
782                          Myinst.InstPhoneNr = phonenr;                          Myinst.InstPhoneNr = phonenr;
783                          Myinst.Imei = imei;                          Myinst.Imei = imei;
784                          Myinst.Updaterate = updaterate;                          Myinst.Updaterate = updaterate;
785                            Myinst.instID = id;
786    
787                          buffer.push_back(Myinst);                          buffer.push_back(Myinst);
788                          rs.MoveNext();                          rs.MoveNext();
# Line 772  int CFlisServerDlg::tversum(__int64 inpu Line 804  int CFlisServerDlg::tversum(__int64 inpu
804  void CFlisServerDlg::OnBnClickedStart()  void CFlisServerDlg::OnBnClickedStart()
805  {  {
806  // TODO: Add your control notification handler code here  // TODO: Add your control notification handler code here
807        
808          continueThread = 1;          continueThread = 1;
809            keepaliveandread();
810            Sleep(150);
811            m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr );
812            AppendText("Started");
813          AfxBeginThread(threadWrapper,AfxGetMainWnd());          AfxBeginThread(threadWrapper,AfxGetMainWnd());
814  }  }
815    void CFlisServerDlg::AppendText(CString s)
816    {
817            CString Tekst;
818            m_Textwindow.GetWindowText(Tekst);
819            Tekst.Append("\r\n");
820            Tekst.Append(s);
821            m_Textwindow.SetWindowText(Tekst);
822    
823    }
824    void ConfigFile::ReadSettings()
825    {
826            ifstream file("Server-Settings.ini");
827            if (!file.is_open())
828                    throw("Could not open file");
829    
830            char buf[200];
831            while (!file.eof() ) {
832                    file.getline(buf,200);
833                    CString tmp(buf);
834    
835                    if (tmp.GetAt(0) == '#')
836                            continue;
837    
838                    int pos = tmp.Find('=');
839                    if (pos>0) {
840                            CString key = tmp.Left(pos).Trim().MakeLower();;
841                            CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
842    
843                            if (key == "host")
844                                    host = value;
845                            else if (key == "username")
846                                    username = value;
847                            else if (key == "password")
848                                    password = value;
849                            else if (key == "database")
850                                    database = value;
851                            else if (key == "comport")
852                                    comport = value;
853                    }
854    
855            }
856    
857            file.close();
858    }
859    void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
860    {
861            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
862            {
863                    Acknowledge& current = (*it);
864                    if (current.sTlfNr == tlfnr)
865                    {
866                            CString SQL;
867                            SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID);
868                            db.ExecuteSQL(SQL);
869    
870                            Acks.erase(it);
871    
872                            return;
873                    }
874            }
875    }
876    
877    void CFlisServerDlg::CheckAcknowledges()
878    {
879            CTime now = CTime::GetCurrentTime();
880            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
881            {
882                    Acknowledge& current = (*it);
883    
884                    CTimeSpan elapsed = now-current.cTime;
885    
886                    if (elapsed.GetTotalSeconds() >= 300)
887                    {
888                            DumpAckList();
889                            if (current.iRetry >= 1)
890                            {
891                                    CString SQL;
892                                    SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID);
893                                    db.ExecuteSQL(SQL);
894                                    Acks.erase(it);
895                                    return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again
896                            }
897                            else
898                            {
899                                    SendConfig("0","2",current.sInstallationID);
900                                    current.iRetry++;
901                                    current.cTime = CTime::GetCurrentTime();
902                                    AppendText( CString("Retry send config to ") + current.sTlfNr );
903                            }
904                    }
905            }
906    }
907    
908    void CFlisServerDlg::DumpAckList()
909    {
910            CTime now = CTime::GetCurrentTime();
911            OutputDebugString("-------------------------------\n");
912            int count = 0;
913            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
914            {
915                    CString msg;
916                    msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr);
917                    OutputDebugString(msg);
918                    count++;
919            }
920    }
921    vector<keepalive> CFlisServerDlg::keepaliveandread(void)
922    {
923            vector<keepalive> buffer;
924    
925            CString SQL, name, value;
926            SQL = "select name, value from config;";
927            CRecordset rs(&db);
928            try
929            {
930                    rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
931                    if (rs.GetRecordCount()>0)
932                    {
933                            rs.MoveFirst();
934                            
935                            rs.GetFieldValue((short)0, name);
936                            rs.GetFieldValue(1,value);
937                    
938                            Alive.name = name;
939                            Alive.value = value;
940                            Alive.tid = CTime::GetCurrentTime();
941                            if(Alive.name == "phonenr")
942                            {
943                                    Alive.Phonenr = value;
944                            }
945    
946                            buffer.push_back(Alive);
947                            rs.MoveNext();
948    
949                    }
950                    rs.Close();
951                    return buffer;
952            }
953            catch (char * str)
954            {
955                    AppendText("Keepalive failed, closing and opening the db connection again");
956                    if(db.IsOpen())
957                    {
958                            db.Close();
959                            AppendText("DB connection closed");
960                    }
961                    if(!db.IsOpen())
962                    {
963                            DBConnect();
964                            AppendText("DB connection started again");
965                    }
966            }
967    
968            
969    }

Legend:
Removed from v.130  
changed lines
  Added in v.158

  ViewVC Help
Powered by ViewVC 1.1.20