/[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 70 by kevin, Tue Nov 27 15:26:42 2007 UTC revision 158 by kevin, Wed Dec 5 11:54:09 2007 UTC
# Line 4  Line 4 
4  #include "stdafx.h"  #include "stdafx.h"
5  #include "FlisServer.h"  #include "FlisServer.h"
6  #include "FlisServerDlg.h"  #include "FlisServerDlg.h"
 #include ".\flisserverdlg.h"  
7  #include <vector>  #include <vector>
8    #include ".\flisserverdlg.h"
9    
10  #ifdef _DEBUG  #ifdef _DEBUG
11  #define new DEBUG_NEW  #define new DEBUG_NEW
# Line 64  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo Line 64  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo
64          ON_WM_PAINT()          ON_WM_PAINT()
65          ON_WM_QUERYDRAGICON()          ON_WM_QUERYDRAGICON()
66          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
67          ON_BN_CLICKED(IDOK, OnBnClickedOk)          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
68          ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
69          ON_BN_CLICKED(IDC_test, OnBnClickedtest)          ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
70  END_MESSAGE_MAP()  END_MESSAGE_MAP()
71    
72    
# Line 101  BOOL CFlisServerDlg::OnInitDialog() Line 101  BOOL CFlisServerDlg::OnInitDialog()
101    
102          // TODO: Add extra initialization here          // TODO: Add extra initialization here
103          StartSerial();          StartSerial();
104          SetPin();          DBConnect();
105            ResetSms = 0;
106                    
107          return TRUE;  // return TRUE  unless you set the focus to a control          return TRUE;  // return TRUE  unless you set the focus to a control
108  }  }
# Line 156  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 176  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  }  }
 void CFlisServerDlg::OnBnClickedOk()  
 {  
         // TODO: Add your control notification handler code here  
         OnOK();  
         if( Serial.isOpen() ){  
                 Serial.close();  
         }  
 }  
   
 void CFlisServerDlg::OnBnClickedCancel()  
 {  
         // TODO: Add your control notification handler code here  
         OnCancel();  
         if( Serial.isOpen() ){  
                 Serial.close();  
         }  
 }  
   
 void CFlisServerDlg::OnBnClickedtest()  
 {  
         // TODO: Add your control notification handler code here  
         /*  
         std::vector<unsigned char> data;  
         data.push_back('a');  
         data.push_back('t');  
   
         writeFrame(data);  
213    
         if(Serial.getComstat().cbInQue > 0)  
         {  
         std::vector<unsigned char> answer = readFrame();  
         Sleep(50);  
         if (answer.size() == 0){  
                 m_Textwindow.SetWindowText("ØV");  
         }  
         else{  
                 CString ko;  
                 char test[150];  
                 int i;  
                 for (int i=0; i<answer.size(); i++)  
                 {  
                         test[i] = answer[i];  
                 }  
                 ko = test;  
                 m_Textwindow.SetWindowText(ko);  
         }  
         }  
         */  
         SetPin();  
 }  
214  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
215  {  {
216          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
# Line 243  void CFlisServerDlg::writeFrame(std::vec Line 222  void CFlisServerDlg::writeFrame(std::vec
222          Sleep(100);          Sleep(100);
223    
224  }  }
225  int CFlisServerDlg::SetPin(void)  void CFlisServerDlg::SetPin()
226  {  {
227          CString tekst, oldtekst;          CString tekst;
228          std::vector<unsigned char> data;          std::vector<unsigned char> data;
229          data.push_back('a');          data.push_back('a');
230          data.push_back('t');          data.push_back('t');
# Line 261  int CFlisServerDlg::SetPin(void) Line 240  int CFlisServerDlg::SetPin(void)
240          data.push_back('5');          data.push_back('5');
241    
242          writeFrame(data);          writeFrame(data);
243          Sleep(750);  }
244    void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
245    {      
246            
247            for (int i=0; i<data.size(); i++)
248            {
249                    Serial.writeByte( data[i] );
250                    Sleep(5);
251            }
252            Serial.writeByte(0x1A);
253            Sleep(3000);
254            if(Serial.getComstat().cbInQue > 0)
255            {
256                    CString tekst;
257                    std::vector<unsigned char> answer = readFrame();
258                    Sleep(50);
259    
260                    for (int i=0; i<answer.size(); i++)
261                            {
262                                    if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
263                                    {
264                                            tekst.AppendChar(answer[i]);
265                                    }
266                            }
267                            AppendText(tekst);
268            }
269    
270    }
271    void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
272    {
273            vector<unsigned char> atcommand;
274            atcommand.push_back('a');
275            atcommand.push_back('t');
276            atcommand.push_back('+');
277            atcommand.push_back('c');
278            atcommand.push_back('m');
279            atcommand.push_back('g');
280            atcommand.push_back('s');
281            atcommand.push_back('=');
282            atcommand.push_back('"');
283            int s = (atcommand.size() -1 );
284            
285            for (int i=0; i<(atcommand.size()); i++)
286            {
287                    Serial.writeByte( atcommand[i] );
288                    Sleep(5);
289            }
290            
291            for (int i=0; i<tlfnr.size(); i++)
292            {
293                    Serial.writeByte( tlfnr[i] );
294                    Sleep(5);
295            }
296            Serial.writeByte(atcommand[s]);
297            Serial.writeByte(0x0D);
298            Sleep(250);
299    }
300    void CFlisServerDlg::DBConnect()
301    {
302            CString dsn;
303            ConfigFile config;
304            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);
312    }
313    vector<Commands> CFlisServerDlg::DBReadCommands(void)
314    {
315            vector<Commands> buffer;
316    
317            CString SQL, IDnr, CommandID, InstallationsID;
318            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);
320            rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
321            if (rs.GetRecordCount()>0)
322            {
323                    rs.MoveFirst();
324                            
325                    Commands Mycom;
326                    rs.GetFieldValue((short)0, IDnr);
327                    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    
337            }
338            rs.Close();
339            return buffer;
340    }
341    void CFlisServerDlg::ReadSms()
342    {
343            CString tekst, oldtekst;
344            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[25];  
                         int i;  
350                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
351                          {                          {
352                                  array1[i] = answer[i];                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
353                                    {
354                                            tekst.AppendChar(answer[i]);
355                                    }
356                          }                          }
357    
358                            AppendText(tekst);
359                            SmsSplit(tekst);
360            }
361    }
362    void CFlisServerDlg::SmsSplit(CString data)
363    {
364            CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
365            char CharData[150];
366            strcpy(CharData,data);
367            TlfNr = data.Mid(24,8);
368            FyrData = data.Mid(57,data.GetLength()-59);
369            FyrData.Append(":");
370    
371            if (FyrData.MakeLower() == "conf ok:")
372            {
373                    HandleAcknowledge(TlfNr);
374                    return;
375            }
376            
377            SmsCount = Splitter(FyrData);
378            Temper = Splitter(FyrData);
379            Flamme = Splitter(FyrData);
380            Flis = Splitter(FyrData);
381            FremFejl = Splitter(FyrData);
382            PowerFail = Splitter(FyrData);
383                    
384                    CString SQL, Textwindow, InstallNR;
385                    SQL.Format("select ID from installation where installationphonenr=%s",TlfNr);
386            
387                    CRecordset rs(&db);
388                    rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
389                    if (rs.GetRecordCount()>0)
390                    {
391                            rs.MoveFirst();        
392                            rs.GetFieldValue((short)0,InstallNR);
393                    }
394                    rs.Close();
395    
396                    SQL.Format("insert into logtable (logtime,temperature,flamedetector,solidfuelempty,conveyorerror,powerfailure,messagenr,installationnr) Values (now(),%s,'%s','%s','%s','%s',%s,%s)",Temper, Flamme, Flis, FremFejl, PowerFail, SmsCount,InstallNR);
397                    try
398                    {
399                    db.ExecuteSQL(SQL);
400                    }
401                    catch(CDBException* e)
402                    {
403                            MessageBox(e->m_strError);
404                    }
405                    AppendText("Sms added to Log");
406                    Sleep(150);
407    }
408    CString CFlisServerDlg::Splitter(CString& fyrdata)
409    {
410            CString Output;
411    
412            int pos = fyrdata.Find(':',0);
413            if (pos != -1)
414                    {
415                            Output = fyrdata.Left(pos);
416                            fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
417                    }
418            return Output;
419    }
420    void CFlisServerDlg::OnBnClickedClose()
421    {
422            // TODO: Add your control notification handler code here
423            continueThread = 0;
424            DeleteSms();
425    
426            Sleep(500);
427            if( Serial.isOpen() )
428            {
429                    Serial.close();
430            }
431            
432            if(db.IsOpen())
433            {
434                    db.Close();
435            }
436            
437            OnOK();
438    
439    }
440    
441    void CFlisServerDlg::OnBnClickedGsmpin()
442    {
443            // 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();
448            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();
483    }
484    void CFlisServerDlg::DeleteSms()
485    {
486            vector<unsigned char> atcommand;
487            atcommand.push_back('a');
488            atcommand.push_back('t');
489            atcommand.push_back('+');
490            atcommand.push_back('c');
491            atcommand.push_back('m');
492            atcommand.push_back('g');
493            atcommand.push_back('d');
494            atcommand.push_back('=');
495            atcommand.push_back('1');
496            atcommand.push_back(',');
497            atcommand.push_back('3');
498    
499            writeFrame(atcommand);
500            Sleep(500);
501    }
502    UINT threadWrapper(LPVOID thread)
503    {
504            CFlisServerDlg *t = (CFlisServerDlg*) thread;
505            t->runthread();
506            return 0;
507    }
508    
509    void CFlisServerDlg::startthread()
510    {
511            AfxBeginThread(threadWrapper, (LPVOID) this);
512    }
513    
514    void CFlisServerDlg::runthread()
515    {
516            while (continueThread != 0)
517            {
518                    Reader();
519            }
520    }
521    void CFlisServerDlg::Reader()
522    {
523            if(Serial.getComstat().cbInQue > 0)
524                    {
525                            Sleep(250);
526                            std::vector<unsigned char> answer = readFrame();
527                            Sleep(500);
528                            CString tekst, oldtekst;
529                            int lol;
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(answer[i]);
536                                    }
537                            }
538                            tekst.Append(":");
539    
540                            CString command;
541                            bool plus = false;
542                                    int pos = tekst.Find('+',0);
543                                    if (pos != -1)
544                                    {
545                                            plus = true;
546                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
547                                            pos = tekst.Find(':');
548                                            command = tekst.Left(pos);
549                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
550                                    }
551                            
552                            
553                            if(tekst.MakeLower() == "ok")
554                            {      
555                                    AppendText("OK tekst modtaget");
556                            }
557                            else if (tekst.MakeLower() == "error")
558                            {
559                                    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;
570                                            int pos = tekst.Find(',',0);
571                                            if (pos != -1)
572                                            {
573                                                    smscount = tekst.Right( tekst.GetLength() - pos -1);
574                                                    smscount.Remove(':');
575                                                    ResetSms++;
576                                            }
577                                                    ////////////////Read sms ting//////////////////
578                                                    std::vector<unsigned char> data;
579                                                    data.push_back('a');
580                                                    data.push_back('t');
581                                                    data.push_back('+');
582                                                    data.push_back('c');
583                                                    data.push_back('m');
584                                                    data.push_back('g');
585                                                    data.push_back('r');
586                                                    data.push_back('=');
587                                                    
588                                                    for (int i=0; i< smscount.GetLength(); i++)
589                                                    {
590                                                            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++)
595                                                    {
596                                                            oldteskst.AppendChar(data[i]);
597                                                    }
598                                                    m_Textwindow.SetWindowText(oldteskst);
599                                                    writeFrame(data);
600                                                    Sleep(200);
601                                                    ReadSms();
602                                                    ResetSms++;
603    
604                                    }
605                                    else if(command.MakeLower() == "wind")
606                                  {                                  {
607                                          tekst.AppendChar(array1[i]);  
608                                            CString send;
609                                            send.Append("Wind modtaget");
610                                            send.Append("\r\n");
611                                            send.Append(tekst);
612                                            AppendText(send);
613                                  }                                  }
614                          }                          }
615                            else
616                            {
617                                    AppendText(tekst);
618                            }
619                            if(ResetSms == 20)
620                            {
621                                    DeleteSms();
622                                    AppendText("Sms'er slettet da vi nåede grænsen");
623                            }
624    
625                    }
626                    if (continueThread == 1)
627                    {
628                            
629                            CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
630                            int commandtest = 0;
631                            int iAll = 1;
632                            std::vector<Commands> data;
633                            data = DBReadCommands();
634                            for (int i=0; i<data.size(); i++)
635                            {
636                                    sIDnr = data[i].IDnr;
637                                    sCommandID = data[i].CommandID;
638                                    commandtest = atoi(data[i].CommandID);
639                                    sInstallationsID = "0";
640    
641                                    if(commandtest > 1)
642                                    {
643                                            sInstallationsID = data[i].InstallationsID;
644                                    }
645                            }
646                            
647                            if (commandtest > 1)
648                            {
649                                    iAll = 0;
650                            }
651    
652                            if (sIDnr.GetLength() > 0)
653                            {
654                                    SendConfig(sIDnr,sCommandID,sInstallationsID);
655                            }
656                            Sleep(500);
657    
658                            CheckAcknowledges();
659                            Sleep(500);
660                    }
661                    CTime now = CTime::GetCurrentTime();
662    
663                    CTimeSpan elapsed = now-Alive.tid;
664    
665                    if (elapsed.GetTotalSeconds() >= 900)
666                    {
667                            keepaliveandread();
668                    }
669    }
670    void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
671    {      
672            
673            std::vector<Installation> inst;
674    
675            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;
683                    TlfNr.Empty();
684                    Imei.Empty();
685                    updaterate.Empty();
686    
687                    CString dataen = inst[j].InstPhoneNr;
688                    TlfNr.Append(dataen);
689                    CString Imeidata = inst[j].Imei;
690                    Imei.Append(Imeidata);
691                    CString updaterat = inst[j].Updaterate;
692                    updaterate.Append(updaterat);
693            
694                    vector<unsigned char> tlfnr;
695                    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);
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    
                         m_Textwindow.GetWindowText(oldtekst);  
                         oldtekst.Append("\r\n");  
                         oldtekst.Append(tekst);  
                         m_Textwindow.SetWindowText(oldtekst);  
737          }          }
738  return 0;  
739            if (IDnr != "0")
740            {
741                    CString SQL, Textwindow;
742                    SQL.Format("update command set executed=now() where id=%s",IDnr);
743                    db.ExecuteSQL(SQL);
744                    Sleep(10);
745                    AppendText("Command executed");
746            }
747            Sleep(150);
748    
749    
750    
751    
752  }  }
753  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
754  {  {
755          for (int i=0; i<data.size(); i++)          vector<Installation> buffer;
756    
757            CString SQL, phonenr, imei, updaterate, id;
758            Installation Myinst;
759    
760            if (sInstallationsID != "0")
761          {          {
762                  Serial.writeByte( data[i] );                  SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
763                  Sleep(5);          }
764            else
765            {
766                    SQL.Format("select installationphonenr, imei, updaterate, id from installation");
767          }          }
         Serial.writeByte(0x1A);  
         Sleep(100);  
768    
769            CRecordset rs(&db);
770            rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
771            if (rs.GetRecordCount()>0)
772            {
773                    rs.MoveFirst();
774                    while(!rs.IsEOF())
775                    {
776                    
777                            rs.GetFieldValue((short)0,phonenr);
778                            rs.GetFieldValue(1,imei);
779                            rs.GetFieldValue(2,updaterate);
780                            rs.GetFieldValue(3,id);
781    
782                            Myinst.InstPhoneNr = phonenr;
783                            Myinst.Imei = imei;
784                            Myinst.Updaterate = updaterate;
785                            Myinst.instID = id;
786    
787                            buffer.push_back(Myinst);
788                            rs.MoveNext();
789                    }
790            }
791            rs.Close();
792            return buffer;
793  }  }
794  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  int CFlisServerDlg::tversum(__int64 input)
795  {  {
796          for (int i=0; i<data.size(); i++)          int sum = 0;
797            while (input > 0)
798          {          {
799                  Serial.writeByte( data[i] );                  sum += (input %10);
800                  Sleep(5);                  input /= 10;
801            }
802            return sum;
803    }
804    void CFlisServerDlg::OnBnClickedStart()
805    {
806    // TODO: Add your control notification handler code here
807        
808            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());
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          }          }
         Serial.writeByte(0x0D);  
         Sleep(100);  
856    
857            file.close();
858  }  }
859  void CFlisServerDlg::DBconnect()  void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
860  {  {
861          CString dsn;          for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
862          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=%s; uid=%s;password=%s;database=%s",config.host, config.username, config.password, config.database);          {
863          db.OpenEx(dsn, CDatabase::noOdbcDialog);                  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.70  
changed lines
  Added in v.158

  ViewVC Help
Powered by ViewVC 1.1.20