/[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 113 by kevin, Sat Dec 1 14:18:33 2007 UTC revision 166 by kevin, Wed Dec 5 16:03:05 2007 UTC
# Line 5  Line 5 
5  #include "FlisServer.h"  #include "FlisServer.h"
6  #include "FlisServerDlg.h"  #include "FlisServerDlg.h"
7  #include <vector>  #include <vector>
8    #include ".\flisserverdlg.h"
9    #include <atlrx.h>
10    
11  #ifdef _DEBUG  #ifdef _DEBUG
12  #define new DEBUG_NEW  #define new DEBUG_NEW
# Line 62  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo Line 64  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo
64          ON_WM_SYSCOMMAND()          ON_WM_SYSCOMMAND()
65          ON_WM_PAINT()          ON_WM_PAINT()
66          ON_WM_QUERYDRAGICON()          ON_WM_QUERYDRAGICON()
         ON_MESSAGE(UWM_MYMESSAGE, OnShowString)  
67          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
         ON_BN_CLICKED(IDC_test, OnBnClickedtest)  
68          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
69          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
70            ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
71  END_MESSAGE_MAP()  END_MESSAGE_MAP()
72    
73    
# Line 102  BOOL CFlisServerDlg::OnInitDialog() Line 103  BOOL CFlisServerDlg::OnInitDialog()
103          // TODO: Add extra initialization here          // TODO: Add extra initialization here
104          StartSerial();          StartSerial();
105          DBConnect();          DBConnect();
         continueThread = 1;  
106          ResetSms = 0;          ResetSms = 0;
         /*  
 ////////////Start Tråd////////////////////////  
         MyThread *t = new MyThread(AfxGetMainWnd());  
         t->start();  
 ////////////Start Tråd////////////////////////  
 */  
         AfxBeginThread(threadWrapper,AfxGetMainWnd());  
107                    
108          return TRUE;  // return TRUE  unless you set the focus to a control          return TRUE;  // return TRUE  unless you set the focus to a control
109  }  }
# Line 165  HCURSOR CFlisServerDlg::OnQueryDragIcon( Line 158  HCURSOR CFlisServerDlg::OnQueryDragIcon(
158  }  }
159  int CFlisServerDlg::StartSerial(void)  int CFlisServerDlg::StartSerial(void)
160  {  {
161            
162          int Baud;          int Baud;
         CString SerialPort = "COM3";  
163    
164            ConfigFile config;
165            try {
166                    config.ReadSettings();
167            } catch(...) {
168                    MessageBox("Could not open config file");
169                    OnOK();
170                    return true;
171            }
172            
173          Baud = 1200;          Baud = 1200;
174          if( Serial.isOpen() ){          if( Serial.isOpen() )
175                  Serial.close();          {
176                  Serial.open( SerialPort, Baud );                  try
177                    {
178                            Serial.close();
179                            Serial.open( config.comport, Baud );
180                    }
181                    catch (exception* e)
182                    {
183                            MessageBox( "Serial.open() exception" );
184                            return 0;
185                    }
186          }          }
187          else {          else
188                  Serial.open( SerialPort, Baud );          {
189                    try
190                    {
191                            Serial.open( config.comport, Baud );
192                    }
193                    catch (exception* e)
194                    {
195                            MessageBox( "Serial.open() exception 2" );
196                            return 0;
197                    }
198          }          }
199            
200                                    
201  return 0;  return 0;
202  }  }
# Line 185  std::vector<unsigned char> CFlisServerDl Line 205  std::vector<unsigned char> CFlisServerDl
205          std::vector<unsigned char> buf;          std::vector<unsigned char> buf;
206          while(Serial.getComstat().cbInQue > 0)          while(Serial.getComstat().cbInQue > 0)
207          {          {
208          unsigned char data = Serial.readByte();                  unsigned char data = Serial.readByte();
209    
210          buf.push_back(data);                  buf.push_back(data);
211          }          }
212          return buf;          return buf;
213  }  }
 void CFlisServerDlg::OnBnClickedtest()  
 {  
         // TODO: Add your control notification handler code here  
214    
 /*        
 ///////////////Send sms stuff/////////////////  
         vector<unsigned char> tlfnr;  
         vector<unsigned char> smsdata;  
         tlfnr.push_back('2');  
         tlfnr.push_back('9');  
         tlfnr.push_back('7');  
         tlfnr.push_back('2');  
         tlfnr.push_back('2');  
         tlfnr.push_back('6');  
         tlfnr.push_back('0');  
         tlfnr.push_back('3');  
   
         SendSmsHead(tlfnr);  
   
          //35:29860132:75 <--- demo pakke;  
          vector<unsigned char> smsdata;  
         smsdata.push_back('3');  
         smsdata.push_back('5');  
         smsdata.push_back(':');  
         smsdata.push_back('2');  
         smsdata.push_back('9');  
         smsdata.push_back('8');  
         smsdata.push_back('6');  
         smsdata.push_back('0');  
         smsdata.push_back('1');  
         smsdata.push_back('3');  
         smsdata.push_back('2');  
         smsdata.push_back(':');  
         smsdata.push_back('7');  
         smsdata.push_back('5');  
   
         SendSmsData(smsdata);  
 ///////////////Send sms stuff/////////////////  
 */  
   
 }  
215  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
216  {  {
217          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
# Line 261  void CFlisServerDlg::SetPin() Line 241  void CFlisServerDlg::SetPin()
241          data.push_back('5');          data.push_back('5');
242    
243          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);  
         }  
 */  
244  }  }
245  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
246  {        {      
# Line 299  void CFlisServerDlg::SendSmsData(std::ve Line 257  void CFlisServerDlg::SendSmsData(std::ve
257                  CString tekst;                  CString tekst;
258                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
259                  Sleep(50);                  Sleep(50);
260                          char array1[25];  
261                          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++)  
262                          {                          {
263                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
264                                  {                                  {
265                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
266                                  }                                  }
267                          }                          }
268                          m_Textwindow.SetWindowText(tekst);                          AppendText(tekst);
269          }          }
270    
271  }  }
272  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
273  {  {
274          vector<unsigned char> atcommand;          vector<unsigned char> atcommand;
275          atcommand.push_back('a');          atcommand.push_back('a');
# Line 337  void CFlisServerDlg::SendSmsHead(std::ve Line 289  void CFlisServerDlg::SendSmsHead(std::ve
289                  Sleep(5);                  Sleep(5);
290          }          }
291                    
292          for (int i=0; i<data.size(); i++)          for (int i=0; i<tlfnr.size(); i++)
293          {          {
294                  Serial.writeByte( data[i] );                  Serial.writeByte( tlfnr[i] );
295                  Sleep(5);                  Sleep(5);
296          }          }
297          Serial.writeByte(atcommand[s]);          Serial.writeByte(atcommand[s]);
# Line 349  void CFlisServerDlg::SendSmsHead(std::ve Line 301  void CFlisServerDlg::SendSmsHead(std::ve
301  void CFlisServerDlg::DBConnect()  void CFlisServerDlg::DBConnect()
302  {  {
303          CString dsn;          CString dsn;
304          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");          ConfigFile config;
305          //dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //skole server          try {
306                    config.ReadSettings();
307            } catch(...) {
308                    MessageBox("Could not open config file");
309            }
310            
311            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);
312          db.OpenEx(dsn, CDatabase::noOdbcDialog);          db.OpenEx(dsn, CDatabase::noOdbcDialog);
313  }  }
314  vector<Commands> CFlisServerDlg::DBRead(void)  vector<Commands> CFlisServerDlg::DBReadCommands(void)
315  {  {
316          vector<Commands> buffer;          vector<Commands> buffer;
317    
318          CString SQL, IDnr, CommandID, InstallationsID;          CString SQL, IDnr, CommandID, InstallationsID;
319          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;";
320          CRecordset rs(&db);          CRecordset rs(&db);
321          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
322          if (rs.GetRecordCount()>0)          if (rs.GetRecordCount()>0)
323          {          {
324                  rs.MoveFirst();                  rs.MoveFirst();
325                  while(!rs.IsEOF())                          
                 {  
326                  Commands Mycom;                  Commands Mycom;
327                  rs.GetFieldValue((short)0, IDnr);                  rs.GetFieldValue((short)0, IDnr);
328                  rs.GetFieldValue(3, CommandID);                  rs.GetFieldValue(3, CommandID);
# Line 377  vector<Commands> CFlisServerDlg::DBRead( Line 334  vector<Commands> CFlisServerDlg::DBRead(
334    
335                  buffer.push_back(Mycom);                  buffer.push_back(Mycom);
336                  rs.MoveNext();                  rs.MoveNext();
337                  }  
338          }          }
339          rs.Close();          rs.Close();
340          return buffer;          return buffer;
# Line 385  vector<Commands> CFlisServerDlg::DBRead( Line 342  vector<Commands> CFlisServerDlg::DBRead(
342  void CFlisServerDlg::ReadSms()  void CFlisServerDlg::ReadSms()
343  {  {
344          CString tekst, oldtekst;          CString tekst, oldtekst;
345          Sleep(950);          Sleep(950);             //Holder en pause for at lade hele sms'en komme ind i serial køen.
346          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
347          {          {
348                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
349                  Sleep(50);                  Sleep(50);
350                          char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
                   
351                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
352                          {                          {
353                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
354                                  {                                  {
355                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
356                                  }                                  }
357                          }                          }
358    
359                          m_Textwindow.GetWindowText(oldtekst);                          AppendText(tekst);
                         oldtekst.Append("\r\n");  
                         oldtekst.Append(tekst);  
                         m_Textwindow.SetWindowText(oldtekst);  
360                          SmsSplit(tekst);                          SmsSplit(tekst);
361          }          }
362  }  }
# Line 417  void CFlisServerDlg::SmsSplit(CString da Line 365  void CFlisServerDlg::SmsSplit(CString da
365          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
366          char CharData[150];          char CharData[150];
367          strcpy(CharData,data);          strcpy(CharData,data);
368            TlfNr = data.Mid(24,8);
369            FyrData = data.Mid(57,data.GetLength()-59);
370            FyrData.Append(":");
371    
372          int s=24;          if (FyrData.MakeLower() == "conf ok:")
         for (int i=0; i<=7; i++)  
373          {          {
374                  TlfNr.AppendChar(CharData[s]);                  HandleAcknowledge(TlfNr);
375                  s++;                  return;
376          }          }
377            CAtlRegExp<> regex;
378            if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK)
379            {
380    
381          for (int s=57; s<=(data.GetLength()-3); s++)                  CAtlREMatchContext<> pContext;
382    
383                    if (!regex.Match(FyrData, &pContext))
384                    {
385                            AppendText("Invalid SMS recieved");
386                            return;
387                    }
388            }
389            else
390          {          {
391                  FyrData.AppendChar(CharData[s]);                  AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske !
392          }          }
393          FyrData.Append(":");  
           
394          SmsCount = Splitter(FyrData);          SmsCount = Splitter(FyrData);
395          Temper = Splitter(FyrData);          Temper = Splitter(FyrData);
396          Flamme = Splitter(FyrData);          Flamme = Splitter(FyrData);
397          Flis = Splitter(FyrData);          Flis = Splitter(FyrData);
398          FremFejl = Splitter(FyrData);          FremFejl = Splitter(FyrData);
399          PowerFail = Splitter(FyrData);          PowerFail = Splitter(FyrData);
400  ///////////////////////////////////////////////////////////////////////////////////////////////////////                  
401  ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////                  CString SQL, Textwindow, InstallNR;
402                  m_Textwindow.GetWindowText(oldtekst);                  SQL.Format("select ID from installation where installationphonenr=%s",TlfNr);
403                  oldtekst.Append("\r\n");          
404                  oldtekst.Append("På næste linie kommer SmsCount. \r\n");                  CRecordset rs(&db);
405                  oldtekst.Append(SmsCount);                  rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
406                  oldtekst.Append("\r\n");                  if (rs.GetRecordCount()>0)
407                  oldtekst.Append("På næste linie kommer Temper. \r\n");                  {
408                  oldtekst.Append(Temper);                          rs.MoveFirst();        
409                  oldtekst.Append("\r\n");                          rs.GetFieldValue((short)0,InstallNR);
410                  oldtekst.Append("På næste linie kommer Flamme. \r\n");                  }
411                  oldtekst.Append(Flamme);                  rs.Close();
412                  oldtekst.Append("\r\n");  
413                  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);
414                  oldtekst.Append(Flis);                  try
415                  oldtekst.Append("\r\n");                  {
416                  oldtekst.Append("På næste linie kommer FremFejl. \r\n");                  db.ExecuteSQL(SQL);
417                  oldtekst.Append(FremFejl);                  }
418                  oldtekst.Append("\r\n");                  catch(CDBException* e)
419                  oldtekst.Append("På næste linie kommer PowerFail. \r\n");                  {
420                  oldtekst.Append(PowerFail);                          MessageBox(e->m_strError);
421                  oldtekst.Append("\r\n");                  }
422                  oldtekst.Append("På næste linie kommer TlfNR. \r\n");                  AppendText("Sms added to Log");
423                  oldtekst.Append(TlfNr);                  Sleep(150);
                 m_Textwindow.SetWindowText(oldtekst);  
 ///////////////////////////////////////////////////////////////////////////////////////////////////////  
 ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  
424  }  }
425  CString CFlisServerDlg::Splitter(CString& fyrdata)  CString CFlisServerDlg::Splitter(CString& fyrdata)
426  {  {
# Line 481  void CFlisServerDlg::OnBnClickedClose() Line 438  void CFlisServerDlg::OnBnClickedClose()
438  {  {
439          // TODO: Add your control notification handler code here          // TODO: Add your control notification handler code here
440          continueThread = 0;          continueThread = 0;
441          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          Sleep(250);
442            DeleteSms();
443    
444          Sleep(500);          Sleep(500);
445          if( Serial.isOpen() )          if( Serial.isOpen() )
# Line 491  void CFlisServerDlg::OnBnClickedClose() Line 449  void CFlisServerDlg::OnBnClickedClose()
449                    
450          if(db.IsOpen())          if(db.IsOpen())
451          {          {
452          db.Close();                  db.Close();
453          }          }
454                    
455          OnOK();          OnOK();
# Line 501  void CFlisServerDlg::OnBnClickedClose() Line 459  void CFlisServerDlg::OnBnClickedClose()
459  void CFlisServerDlg::OnBnClickedGsmpin()  void CFlisServerDlg::OnBnClickedGsmpin()
460  {  {
461          // TODO: Add your control notification handler code here          // TODO: Add your control notification handler code here
462            bool ready = true;
463            m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar");
464            UpdateWindow();
465          SetPin();          SetPin();
466          //Sleep(5000);          Sleep(500);
467    
468            while (ready == true)
469            {
470                    if (Serial.getComstat().cbInQue > 120)
471                    {
472                            std::vector<unsigned char> answer = readFrame();
473                            CString tekst;
474                    
475                            for (int i=0; i<answer.size(); i++)
476                            {
477                                    if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
478                                    {
479                                            tekst.AppendChar(answer[i]);
480                                    }
481                            }
482                            int n = tekst.Find("WIND: 11",0);
483                            if (n > -1)
484                            {
485                                    ready = false;
486                            }
487                            else
488                            {
489                                    AppendText(tekst);
490                            }
491                    }
492                    Sleep(10); //Small delay to avoid busy wait
493            }
494            Sleep(100); //Give the modem a chance to send the last data
495            while(Serial.getComstat().cbInQue > 0)
496            {
497                    Serial.readByte(); //Flush the incoming queue
498                    Sleep(1);
499            }
500            OnBnClickedStart();
501  }  }
502  void CFlisServerDlg::DeleteSms()  void CFlisServerDlg::DeleteSms()
503  {  {
# Line 522  void CFlisServerDlg::DeleteSms() Line 517  void CFlisServerDlg::DeleteSms()
517          writeFrame(atcommand);          writeFrame(atcommand);
518          Sleep(500);          Sleep(500);
519  }  }
520  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)  
521  {  {
522          CFlisServerDlg *t = (CFlisServerDlg*) thread;          CFlisServerDlg *t = (CFlisServerDlg*) thread;
523          t->runthread();          t->runthread();
# Line 558  void CFlisServerDlg::Reader() Line 545  void CFlisServerDlg::Reader()
545                          Sleep(500);                          Sleep(500);
546                          CString tekst, oldtekst;                          CString tekst, oldtekst;
547                          int lol;                          int lol;
                         char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
548                                    
549                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
550                          {                          {
551                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
552                                  {                                  {
553                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
554                                  }                                  }
555                          }                          }
556                          tekst.Append(":");                          tekst.Append(":");
557    
558                          CString command;                          CString command;
559                          bool plus;                          bool plus = false;
560                                  int pos = tekst.Find('+',0);                                  int pos = tekst.Find('+',0);
561                                  if (pos != -1)                                  if (pos != -1)
562                                  {                                  {
# Line 587  void CFlisServerDlg::Reader() Line 568  void CFlisServerDlg::Reader()
568                                  }                                  }
569                                                    
570                                                    
571                          if(tekst == "OK")                          if(tekst.MakeLower() == "ok")
572                          {                                {      
573                                  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);  
574                          }                          }
575                          else if (plus = true)                          else if (tekst.MakeLower() == "error")
576                          {                          {
577                                  if (command = "cmti")                                  CString send;
578                                    send.Append("error tekst");
579                                    send.Append("\r\n");
580                                    send.Append(tekst);
581                                    AppendText(send);
582                            }
583                            else if (plus == true)
584                            {
585                                    if (command.MakeLower() == "cmti")
586                                  {                                  {
587                                          CString smscount, oldteskst;                                          CString smscount, oldteskst;
588                                          int pos = tekst.Find(',',0);                                          int pos = tekst.Find(',',0);
# Line 629  void CFlisServerDlg::Reader() Line 607  void CFlisServerDlg::Reader()
607                                                  {                                                  {
608                                                          data.push_back(smscount[i]);                                                          data.push_back(smscount[i]);
609                                                  }                                                  }
610                                                    m_Textwindow.GetWindowText(oldteskst);
611                                                    oldteskst.Append("\r\n");
612                                                  for (int i=0; i<data.size();i++)                                                  for (int i=0; i<data.size();i++)
613                                                  {                                                  {
614                                                  oldteskst.AppendChar(data[i]);                                                          oldteskst.AppendChar(data[i]);
615                                                  }                                                  }
616                                                  m_Textwindow.SetWindowText(oldteskst);                                                  m_Textwindow.SetWindowText(oldteskst);
617                                                  writeFrame(data);                                                  writeFrame(data);
618                                                  Sleep(200);                                                  Sleep(200);
619                                                  ReadSms();                                                  ReadSms();
620                                                    ResetSms++;
621    
622                                  }                                  }
623                                  else if(command = "wind")                                  else if(command.MakeLower() == "wind")
624                                  {                                  {
625                                          m_Textwindow.GetWindowText(oldtekst);  
626                                          oldtekst.Append("\r\n");                                          CString send;
627                                          oldtekst.Append("Først lidt tekst på næste linie\r\n");                                          send.Append("Wind modtaget");
628                                          oldtekst.Append(tekst);                                          send.Append("\r\n");
629                                          m_Textwindow.SetWindowText(oldtekst);                                          send.Append(tekst);
630                                            AppendText(send);
631                                  }                                  }
632                          }                          }
633                          if(ResetSms == 50)                          else
634                            {
635                                    AppendText(tekst);
636                            }
637                            if(ResetSms == 20)
638                          {                          {
639                                  DeleteSms();                                  DeleteSms();
640                                    AppendText("Sms'er slettet da vi nåede grænsen");
641                          }                          }
642    
643                  }                  }
644                  if (continueThread = 1)                  if (continueThread == 1)
645                  {                  {
646                                                    
647                          ////////////////DBRead stuff//////////////////                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
648                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID;                          int commandtest = 0;
649                            int iAll = 1;
650                          std::vector<Commands> data;                          std::vector<Commands> data;
651                          data = DBRead();                          data = DBReadCommands();
652                          for (int i=0; i<data.size(); i++)                          for (int i=0; i<data.size(); i++)
653                          {                          {
654                                  testdata.Append(data[i].IDnr);                                  sIDnr = data[i].IDnr;
655                                  testdata.Append(":");                                  sCommandID = data[i].CommandID;
656                                  testdata.Append(data[i].CommandID);                                  commandtest = atoi(data[i].CommandID);
657                                  testdata.Append(":");                                  sInstallationsID = "0";
658                                  testdata.Append(data[i].InstallationsID);  
659                                  testdata.Append(":");                                  if(commandtest > 1)
660                          }                                  {
661                          sIDnr = Splitter(testdata);                                          sInstallationsID = data[i].InstallationsID;
662                          sCommandID = Splitter(testdata);                                  }
663                          sInstallationsID = Splitter(testdata);                          }
   
                         testprint.Append("sIDnr på næste linie\r\n");  
                         testprint.Append(sIDnr);  
                         testprint.Append("\r\n");  
                         testprint.Append("sCommandID på næste linie\r\n");  
                         testprint.Append(sCommandID);  
                         testprint.Append("\r\n");  
                         testprint.Append("sInstallationsID på næste linie\r\n");  
                         testprint.Append(sInstallationsID);  
                         m_Textwindow.SetWindowText(testprint);  
                           
664                                                    
665                            if (commandtest > 1)
666                            {
667                                    iAll = 0;
668                            }
669    
670                          if (sIDnr.GetLength() > 0)                          if (sIDnr.GetLength() > 0)
671                          {                          {
672                                  DBReadData(sIDnr,sCommandID,sInstallationsID);                                  SendConfig(sIDnr,sCommandID,sInstallationsID);
673                          }                          }
674                          Sleep(500);                          Sleep(500);
675                          ////////////////DBRead stuff//////////////////    
676                            CheckAcknowledges();
677                            Sleep(500);
678                    }
679                    CTime now = CTime::GetCurrentTime();
680    
681                    CTimeSpan elapsed = now-Alive.tid;
682    
683                    if (elapsed.GetTotalSeconds() >= 900)
684                    {
685                            keepaliveandread();
686                  }                  }
687  }  }
688  void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID)  void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
689  {  {      
690          CString TlfNr;          
                   
         //kald dbreadphone som du kalder dbread  
691          std::vector<Installation> inst;          std::vector<Installation> inst;
692          inst = DBReadPhone(InstallationsID);  
693          for (int i=0; i<inst.size(); i++)          if (CommandID == "1")
694                    inst = DBReadPhone("0");
695            else
696                    inst = DBReadPhone(InstallationsID);
697    
698            for ( int j=0; j < inst.size(); j++)
699          {          {
700                  CString dataen = inst[i].InstPhoneNr;                  CString TlfNr, Imei, updaterate;
701                    TlfNr.Empty();
702                    Imei.Empty();
703                    updaterate.Empty();
704    
705                    CString dataen = inst[j].InstPhoneNr;
706                  TlfNr.Append(dataen);                  TlfNr.Append(dataen);
707                    CString Imeidata = inst[j].Imei;
708                    Imei.Append(Imeidata);
709                    CString updaterat = inst[j].Updaterate;
710                    updaterate.Append(updaterat);
711            
712                    vector<unsigned char> tlfnr;
713                    for (int i=0; i<TlfNr.GetLength(); i++)
714                    {
715                            tlfnr.push_back(TlfNr[i]);
716                    }
717                    
718                    int calcimei;
719                    __int64 buf;
720    
721                    buf = atof(Imei);
722                    
723                    calcimei = tversum(buf);
724                    
725                    Imei.Format("%d",calcimei);
726                    
727                    SendSmsHead(tlfnr);
728                    Sleep(250);
729                    
730                    vector<unsigned char> smsdata;
731                    
732                    for (int i=0; i<Imei.GetLength(); i++)
733                    {
734                            smsdata.push_back(Imei[i]);
735                    }
736                    smsdata.push_back(':');
737                    for (int i=0; i<Alive.Phonenr.GetLength(); i++)
738                    {
739                            smsdata.push_back(Alive.Phonenr[i]);
740                    }
741                    smsdata.push_back(':');
742                    for (int i=0; i<updaterate.GetLength(); i++)
743                    {
744                            smsdata.push_back(updaterate[i]);
745                    }
746    
747                    SendSmsData(smsdata);
748                    Sleep(500);
749    
750                    if (IDnr != "0")
751                    {
752                            Acks.push_back( Acknowledge(inst[j].instID,TlfNr) );
753                    }
754    
755          }          }
756          vector<unsigned char> tlfnr;  
757          for (int i=0; i<TlfNr.GetLength(); i++)          if (IDnr != "0")
758          {          {
759                  tlfnr.push_back(TlfNr[i]);                  CString SQL, Textwindow;
760                    SQL.Format("update command set executed=now() where id=%s",IDnr);
761                    db.ExecuteSQL(SQL);
762                    Sleep(100);
763                    AppendText("Command executed");
764          }          }
765            Sleep(150);
766    
         SendSmsHead(tlfnr);  
         Sleep(500);  
         //35:29860132:75 <--- demo pakke;  
         vector<unsigned char> smsdata;  
         smsdata.push_back('3');  
         smsdata.push_back('5');  
         smsdata.push_back(':');  
         smsdata.push_back('2');  
         smsdata.push_back('9');  
         smsdata.push_back('8');  
         smsdata.push_back('6');  
         smsdata.push_back('0');  
         smsdata.push_back('1');  
         smsdata.push_back('3');  
         smsdata.push_back('2');  
         smsdata.push_back(':');  
         smsdata.push_back('7');  
         smsdata.push_back('5');  
767    
         SendSmsData(smsdata);  
         Sleep(250);  
768    
         CString SQL;  
769    
         SQL.Format("update command set executed=now() where id=%s",IDnr);  
         db.ExecuteSQL(SQL);  
         Sleep(150);  
770  }  }
771  vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)  vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
772  {  {
773          vector<Installation> buffer;          vector<Installation> buffer;
774    
775          CString SQL, phonenr;          CString SQL, phonenr, imei, updaterate, id;
776          SQL.Format("select installationphonenr from installation WHERE id = %s",sInstallationsID);          Installation Myinst;
777    
778            if (sInstallationsID != "0")
779            {
780                    SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
781            }
782            else
783            {
784                    SQL.Format("select installationphonenr, imei, updaterate, id from installation");
785            }
786    
787          CRecordset rs(&db);          CRecordset rs(&db);
788          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
789          if (rs.GetRecordCount()>0)          if (rs.GetRecordCount()>0)
# Line 752  vector<Installation> CFlisServerDlg::DBR Line 791  vector<Installation> CFlisServerDlg::DBR
791                  rs.MoveFirst();                  rs.MoveFirst();
792                  while(!rs.IsEOF())                  while(!rs.IsEOF())
793                  {                  {
794                  Installation Myinst;                  
795                  rs.GetFieldValue((short)0,phonenr);                          rs.GetFieldValue((short)0,phonenr);
796                            rs.GetFieldValue(1,imei);
797                  Myinst.InstPhoneNr.Append(phonenr);                          rs.GetFieldValue(2,updaterate);
798                            rs.GetFieldValue(3,id);
799    
800                            Myinst.InstPhoneNr = phonenr;
801                            Myinst.Imei = imei;
802                            Myinst.Updaterate = updaterate;
803                            Myinst.instID = id;
804    
805                  buffer.push_back(Myinst);                          buffer.push_back(Myinst);
806                  rs.MoveNext();                          rs.MoveNext();
807                  }                  }
808          }          }
809          rs.Close();          rs.Close();
810          return buffer;          return buffer;
 }  
811    }
812    int CFlisServerDlg::tversum(__int64 input)
813    {
814            int sum = 0;
815            while (input > 0)
816            {
817                    sum += (input %10);
818                    input /= 10;
819            }
820            return sum;
821    }
822    void CFlisServerDlg::OnBnClickedStart()
823    {
824    // TODO: Add your control notification handler code here
825        
826            continueThread = 1;
827            keepaliveandread();
828            Sleep(150);
829            m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr );
830            AppendText("Started");
831            AfxBeginThread(threadWrapper,AfxGetMainWnd());
832    }
833    void CFlisServerDlg::AppendText(CString s)
834    {
835            CString Tekst;
836            m_Textwindow.GetWindowText(Tekst);
837            Tekst.Append("\r\n");
838            Tekst.Append(s);
839            m_Textwindow.SetWindowText(Tekst);
840    
841    }
842    void ConfigFile::ReadSettings()
843    {
844            ifstream file("Server-Settings.ini");
845            if (!file.is_open())
846                    throw("Could not open file");
847    
848            char buf[200];
849            while (!file.eof() ) {
850                    file.getline(buf,200);
851                    CString tmp(buf);
852    
853                    if (tmp.GetAt(0) == '#')
854                            continue;
855    
856                    int pos = tmp.Find('=');
857                    if (pos>0) {
858                            CString key = tmp.Left(pos).Trim().MakeLower();;
859                            CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
860    
861                            if (key == "host")
862                                    host = value;
863                            else if (key == "username")
864                                    username = value;
865                            else if (key == "password")
866                                    password = value;
867                            else if (key == "database")
868                                    database = value;
869                            else if (key == "comport")
870                                    comport = value;
871                    }
872    
873            }
874    
875            file.close();
876    }
877    void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
878    {
879            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
880            {
881                    Acknowledge& current = (*it);
882                    if (current.sTlfNr == tlfnr)
883                    {
884                            CString SQL;
885                            SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID);
886                            db.ExecuteSQL(SQL);
887    
888                            Acks.erase(it);
889    
890                            return;
891                    }
892            }
893    }
894    
895    void CFlisServerDlg::CheckAcknowledges()
896    {
897            CTime now = CTime::GetCurrentTime();
898            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
899            {
900                    Acknowledge& current = (*it);
901    
902                    CTimeSpan elapsed = now-current.cTime;
903    
904                    if (elapsed.GetTotalSeconds() >= 300)
905                    {
906                            DumpAckList();
907                            if (current.iRetry >= 1)
908                            {
909                                    CString SQL;
910                                    SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID);
911                                    db.ExecuteSQL(SQL);
912                                    Acks.erase(it);
913                                    return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again
914                            }
915                            else
916                            {
917                                    SendConfig("0","2",current.sInstallationID);
918                                    current.iRetry++;
919                                    current.cTime = CTime::GetCurrentTime();
920                                    AppendText( CString("Retry send config to ") + current.sTlfNr );
921                            }
922                    }
923            }
924    }
925    
926    void CFlisServerDlg::DumpAckList()
927    {
928            CTime now = CTime::GetCurrentTime();
929            OutputDebugString("-------------------------------\n");
930            int count = 0;
931            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
932            {
933                    CString msg;
934                    msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr);
935                    OutputDebugString(msg);
936                    count++;
937            }
938    }
939    vector<keepalive> CFlisServerDlg::keepaliveandread(void)
940    {
941            vector<keepalive> buffer;
942    
943            CString SQL, name, value;
944            SQL = "select name, value from config;";
945            CRecordset rs(&db);
946            try
947            {
948                    rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
949                    if (rs.GetRecordCount()>0)
950                    {
951                            rs.MoveFirst();
952                            
953                            rs.GetFieldValue((short)0, name);
954                            rs.GetFieldValue(1,value);
955                    
956                            Alive.name = name;
957                            Alive.value = value;
958                            Alive.tid = CTime::GetCurrentTime();
959                            if(Alive.name == "phonenr")
960                            {
961                                    Alive.Phonenr = value;
962                            }
963    
964                            buffer.push_back(Alive);
965                            rs.MoveNext();
966    
967                    }
968                    rs.Close();
969                    return buffer;
970            }
971            catch (char * str)
972            {
973                    AppendText("Keepalive failed, closing and opening the db connection again");
974                    if(db.IsOpen())
975                    {
976                            db.Close();
977                            AppendText("DB connection closed");
978                    }
979                    if(!db.IsOpen())
980                    {
981                            DBConnect();
982                            AppendText("DB connection started again");
983                    }
984            }
985    
986            
987    }

Legend:
Removed from v.113  
changed lines
  Added in v.166

  ViewVC Help
Powered by ViewVC 1.1.20