/[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 121 by kevin, Sun Dec 2 12:03: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                  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[25];  
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");          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();
324                  while(!rs.IsEOF())                          
                 {  
325                  Commands Mycom;                  Commands Mycom;
326                  rs.GetFieldValue((short)0, IDnr);                  rs.GetFieldValue((short)0, IDnr);
327                  rs.GetFieldValue(3, CommandID);                  rs.GetFieldValue(3, CommandID);
# Line 330  vector<Commands> CFlisServerDlg::DBRead( Line 333  vector<Commands> CFlisServerDlg::DBRead(
333    
334                  buffer.push_back(Mycom);                  buffer.push_back(Mycom);
335                  rs.MoveNext();                  rs.MoveNext();
336                  }  
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(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
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");
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 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                                                    
                         ////////////////DBRead stuff//////////////////  
629                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
630                            int commandtest = 0;
631                            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;
638                                  testdata.Append(data[i].CommandID);                                  commandtest = atoi(data[i].CommandID);
639                                  testdata.Append(":");                                  sInstallationsID = "0";
640                                  testdata.Append(data[i].InstallationsID);  
641                                  testdata.Append(":");                                  if(commandtest > 1)
642                          }                                  {
643                          sIDnr = Splitter(testdata);                                          sInstallationsID = data[i].InstallationsID;
644                          sCommandID = Splitter(testdata);                                  }
645                          sInstallationsID = Splitter(testdata);                          }
646                            
647                            if (commandtest > 1)
648                            {
649                                    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                          ////////////////DBRead stuff//////////////////    
658                            CheckAcknowledges();
659                            Sleep(500);
660                  }                  }
661  }                  CTime now = CTime::GetCurrentTime();
 void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID)  
 {  
         CString TlfNr, Imei, ServerTlfNr, updaterate;  
662    
663          ServerTlfNr = "29860132";                  CTimeSpan elapsed = now-Alive.tid;
664                    
665          //kald dbreadphone som du kalder dbread                  if (elapsed.GetTotalSeconds() >= 900)
666                    {
667                            keepaliveandread();
668                    }
669    }
670    void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
671    {      
672            
673          std::vector<Installation> inst;          std::vector<Installation> inst;
674          inst = DBReadPhone(InstallationsID);  
675          for (int i=0; i<inst.size(); i++)          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 dataen = inst[i].InstPhoneNr;                  CString TlfNr, Imei, updaterate;
683                    TlfNr.Empty();
684                    Imei.Empty();
685                    updaterate.Empty();
686    
687                    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);
         }  
         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          double buf;                  for (int i=0; i<TlfNr.GetLength(); i++)
696          Imei.Remove('0');                  {
697                            tlfnr.push_back(TlfNr[i]);
698                    }
699                    
700                    int calcimei;
701                    __int64 buf;
702    
703                    buf = atof(Imei);
704                    
705                    calcimei = tversum(buf);
706                    
707                    Imei.Format("%d",calcimei);
708                    
709                    SendSmsHead(tlfnr);
710                    Sleep(250);
711                    
712                    vector<unsigned char> smsdata;
713                    
714                    for (int i=0; i<Imei.GetLength(); i++)
715                    {
716                            smsdata.push_back(Imei[i]);
717                    }
718                    smsdata.push_back(':');
719                    for (int i=0; i<Alive.Phonenr.GetLength(); i++)
720                    {
721                            smsdata.push_back(Alive.Phonenr[i]);
722                    }
723                    smsdata.push_back(':');
724                    for (int i=0; i<updaterate.GetLength(); i++)
725                    {
726                            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    
         buf = atof(Imei);  
           
         calcimei = tversum(buf);  
           
         Imei.Format("%d",calcimei);  
           
         SendSmsHead(tlfnr);  
         Sleep(500);  
         //35:29860132:75 <--- demo pakke;  
         vector<unsigned char> smsdata;  
           
         for (int i=0; i<Imei.GetLength(); i++)  
         {  
                 smsdata.push_back(Imei[i]);  
         }  
         smsdata.push_back(':');  
         for (int i=0; i<ServerTlfNr.GetLength(); i++)  
         {  
                 smsdata.push_back(ServerTlfNr[i]);  
737          }          }
738          smsdata.push_back(':');  
739          for (int i=0; i<updaterate.GetLength(); i++)          if (IDnr != "0")
740          {          {
741                  smsdata.push_back(updaterate[i]);                  CString SQL, Textwindow;
742                    SQL.Format("update command set executed=now() where id=%s",IDnr);
743                    db.ExecuteSQL(SQL);
744                    AppendText("Command executed");
745          }          }
746            Sleep(150);
747    
         SendSmsData(smsdata);  
         Sleep(250);  
748    
         CString SQL;  
749    
         SQL.Format("update command set executed=now() where id=%s",IDnr);  
         db.ExecuteSQL(SQL);  
         Sleep(150);  
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;
757          SQL.Format("select installationphonenr, imei, updaterate from installation WHERE id = %s",sInstallationsID);          Installation Myinst;
758    
759            if (sInstallationsID != "0")
760            {
761                    SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
762            }
763            else
764            {
765                    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 714  vector<Installation> CFlisServerDlg::DBR Line 772  vector<Installation> CFlisServerDlg::DBR
772                  rs.MoveFirst();                  rs.MoveFirst();
773                  while(!rs.IsEOF())                  while(!rs.IsEOF())
774                  {                  {
775                  Installation Myinst;                  
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                  Myinst.InstPhoneNr.Append(phonenr);  
781                  Myinst.Imei.Append(imei);                          Myinst.InstPhoneNr = phonenr;
782                  Myinst.Updaterate.Append(updaterate);                          Myinst.Imei = imei;
783                            Myinst.Updaterate = updaterate;
784                            Myinst.instID = id;
785    
786                  buffer.push_back(Myinst);                          buffer.push_back(Myinst);
787                  rs.MoveNext();                          rs.MoveNext();
788                  }                  }
789          }          }
790          rs.Close();          rs.Close();
791          return buffer;          return buffer;
792  }  }
793  int CFlisServerDlg::tversum(unsigned input)  int CFlisServerDlg::tversum(__int64 input)
794  {  {
795          int sum = 0;          int sum = 0;
796          while (input > 0)          while (input > 0)
# Line 743  int CFlisServerDlg::tversum(unsigned inp Line 803  int CFlisServerDlg::tversum(unsigned inp
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            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.121  
changed lines
  Added in v.149

  ViewVC Help
Powered by ViewVC 1.1.20