/[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 176 by kevin, Thu Dec 6 14:12:31 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_Start, OnBnClickedStart)
70  END_MESSAGE_MAP()  END_MESSAGE_MAP()
71    
72    
# Line 102  BOOL CFlisServerDlg::OnInitDialog() Line 102  BOOL CFlisServerDlg::OnInitDialog()
102          // TODO: Add extra initialization here          // TODO: Add extra initialization here
103          StartSerial();          StartSerial();
104          DBConnect();          DBConnect();
         continueThread = 1;  
105          ResetSms = 0;          ResetSms = 0;
         /*  
 ////////////Start Tråd////////////////////////  
         MyThread *t = new MyThread(AfxGetMainWnd());  
         t->start();  
 ////////////Start Tråd////////////////////////  
 */  
         AfxBeginThread(threadWrapper,AfxGetMainWnd());  
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 165  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 185  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::OnBnClickedtest()  
 {  
         // TODO: Add your control notification handler code here  
213    
 /*        
 ///////////////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/////////////////  
 */  
   
 }  
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 245  void CFlisServerDlg::writeFrame(std::vec Line 224  void CFlisServerDlg::writeFrame(std::vec
224  }  }
225  void CFlisServerDlg::SetPin()  void CFlisServerDlg::SetPin()
226  {  {
227            bool ready = true;
228          CString tekst;          CString tekst;
229          std::vector<unsigned char> data;          std::vector<unsigned char> data;
230    
231          data.push_back('a');          data.push_back('a');
232          data.push_back('t');          data.push_back('t');
233          data.push_back('+');          data.push_back('+');
# Line 254  void CFlisServerDlg::SetPin() Line 235  void CFlisServerDlg::SetPin()
235          data.push_back('p');          data.push_back('p');
236          data.push_back('i');          data.push_back('i');
237          data.push_back('n');          data.push_back('n');
238          data.push_back('=');          data.push_back('?');
         data.push_back('2');  
         data.push_back('5');  
         data.push_back('9');  
         data.push_back('5');  
239    
240          writeFrame(data);          writeFrame(data);
241  /*      Sleep(750);          Sleep(100);
242          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
243          {          {
244                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
245                  Sleep(50);                  Sleep(50);
246                          char array1[25];  
247                          int i;                  for (int i=0; i<answer.size(); i++)
                         for (int i=0; i<answer.size(); i++)  
248                          {                          {
249                                  array1[i] = answer[i];                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
250                                    {
251                                            tekst.AppendChar(answer[i]);
252                                    }
253                          }                          }
254                                            AppendText(tekst);
255                          for (int i=0; i<answer.size(); i++)          }
256            Sleep(5);
257            tekst.MakeLower();
258            int pos = tekst.Find("+cpin: ",0);
259            if (pos != -1)
260            {
261                    tekst = tekst.Right( tekst.GetLength() - pos -1);
262            }
263    
264            if (tekst == "cpin: sim pin")
265            {
266                    CString pin;
267                    ConfigFile config;
268                    try {
269                            config.ReadSettings();
270                    } catch(...) {
271                            MessageBox("Could not open config file");
272                    }
273                    Sleep(50);
274    
275                    data.clear();
276                    data.push_back('a');
277                    data.push_back('t');
278                    data.push_back('+');
279                    data.push_back('c');
280                    data.push_back('p');
281                    data.push_back('i');
282                    data.push_back('n');
283                    data.push_back('=');
284                    for (int i = 0; i<config.pincode.GetLength(); i++)
285                    {
286                    data.push_back(config.pincode[i]);
287                    }
288    
289                    writeFrame(data);
290    
291                    Sleep(500);
292    
293                    while (ready == true)
294                    {
295                            if (Serial.getComstat().cbInQue > 120)
296                          {                          {
297                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  std::vector<unsigned char> answer = readFrame();
298                                    CString tekst;
299                            
300                                    for (int i=0; i<answer.size(); i++)
301                                  {                                  {
302                                          tekst.AppendChar(array1[i]);                                          if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
303                                            {
304                                                    tekst.AppendChar(answer[i]);
305                                            }
306                                    }
307                                    int n = tekst.Find("WIND: 11",0);
308                                    if (n > -1)
309                                    {
310                                            ready = false;
311                                    }
312                                    else
313                                    {
314                                            AppendText(tekst);
315                                  }                                  }
316                          }                          }
317                          m_Textwindow.SetWindowText(tekst);                          Sleep(10); //Small delay to avoid busy wait
318                    }
319            }
320            else if (tekst == "cpin: ready")
321            {
322                    AppendText("No pincode needed");
323            }
324            else
325            {
326                    AppendText("Error in set pin part of the code");
327          }          }
 */  
328  }  }
329  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
330  {        {      
# Line 299  void CFlisServerDlg::SendSmsData(std::ve Line 341  void CFlisServerDlg::SendSmsData(std::ve
341                  CString tekst;                  CString tekst;
342                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
343                  Sleep(50);                  Sleep(50);
344                          char array1[25];  
345                          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++)  
346                          {                          {
347                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
348                                  {                                  {
349                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
350                                  }                                  }
351                          }                          }
352                          m_Textwindow.SetWindowText(tekst);                          AppendText(tekst);
353          }          }
354    
355  }  }
356  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
357  {  {
358          vector<unsigned char> atcommand;          vector<unsigned char> atcommand;
359          atcommand.push_back('a');          atcommand.push_back('a');
# Line 337  void CFlisServerDlg::SendSmsHead(std::ve Line 373  void CFlisServerDlg::SendSmsHead(std::ve
373                  Sleep(5);                  Sleep(5);
374          }          }
375                    
376          for (int i=0; i<data.size(); i++)          for (int i=0; i<tlfnr.size(); i++)
377          {          {
378                  Serial.writeByte( data[i] );                  Serial.writeByte( tlfnr[i] );
379                  Sleep(5);                  Sleep(5);
380          }          }
381          Serial.writeByte(atcommand[s]);          Serial.writeByte(atcommand[s]);
# Line 349  void CFlisServerDlg::SendSmsHead(std::ve Line 385  void CFlisServerDlg::SendSmsHead(std::ve
385  void CFlisServerDlg::DBConnect()  void CFlisServerDlg::DBConnect()
386  {  {
387          CString dsn;          CString dsn;
388          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");          ConfigFile config;
389          //dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //skole server          try {
390                    config.ReadSettings();
391            } catch(...) {
392                    MessageBox("Could not open config file");
393            }
394            
395            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);
396          db.OpenEx(dsn, CDatabase::noOdbcDialog);          db.OpenEx(dsn, CDatabase::noOdbcDialog);
397  }  }
398  vector<Commands> CFlisServerDlg::DBRead(void)  vector<Commands> CFlisServerDlg::DBReadCommands(void)
399  {  {
400          vector<Commands> buffer;          vector<Commands> buffer;
401    
402          CString SQL, IDnr, CommandID, InstallationsID;          CString SQL, IDnr, CommandID, InstallationsID;
403          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;";
404          CRecordset rs(&db);          CRecordset rs(&db);
405          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
406          if (rs.GetRecordCount()>0)          if (rs.GetRecordCount()>0)
407          {          {
408                  rs.MoveFirst();                  rs.MoveFirst();
409                  while(!rs.IsEOF())                          
                 {  
410                  Commands Mycom;                  Commands Mycom;
411                  rs.GetFieldValue((short)0, IDnr);                  rs.GetFieldValue((short)0, IDnr);
412                  rs.GetFieldValue(3, CommandID);                  rs.GetFieldValue(3, CommandID);
# Line 377  vector<Commands> CFlisServerDlg::DBRead( Line 418  vector<Commands> CFlisServerDlg::DBRead(
418    
419                  buffer.push_back(Mycom);                  buffer.push_back(Mycom);
420                  rs.MoveNext();                  rs.MoveNext();
421                  }  
422          }          }
423          rs.Close();          rs.Close();
424          return buffer;          return buffer;
# Line 385  vector<Commands> CFlisServerDlg::DBRead( Line 426  vector<Commands> CFlisServerDlg::DBRead(
426  void CFlisServerDlg::ReadSms()  void CFlisServerDlg::ReadSms()
427  {  {
428          CString tekst, oldtekst;          CString tekst, oldtekst;
429          Sleep(950);          Sleep(950);             //Holder en pause for at lade hele sms'en komme ind i serial køen.
430          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
431          {          {
432                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
433                  Sleep(50);                  Sleep(50);
434                          char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
                   
435                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
436                          {                          {
437                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
438                                  {                                  {
439                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
440                                  }                                  }
441                          }                          }
442    
443                          m_Textwindow.GetWindowText(oldtekst);                          AppendText(tekst);
                         oldtekst.Append("\r\n");  
                         oldtekst.Append(tekst);  
                         m_Textwindow.SetWindowText(oldtekst);  
444                          SmsSplit(tekst);                          SmsSplit(tekst);
445          }          }
446  }  }
# Line 417  void CFlisServerDlg::SmsSplit(CString da Line 449  void CFlisServerDlg::SmsSplit(CString da
449          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
450          char CharData[150];          char CharData[150];
451          strcpy(CharData,data);          strcpy(CharData,data);
452            TlfNr = data.Mid(24,8);
453            FyrData = data.Mid(57,data.GetLength()-59);
454            FyrData.Append(":");
455    
456          int s=24;          if (FyrData.MakeLower() == "conf ok:")
         for (int i=0; i<=7; i++)  
457          {          {
458                  TlfNr.AppendChar(CharData[s]);                  HandleAcknowledge(TlfNr);
459                  s++;                  return;
460          }          }
461            CAtlRegExp<> regex;
462            if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK)
463            {
464    
465                    CAtlREMatchContext<> pContext;
466    
467          for (int s=57; s<=(data.GetLength()-3); s++)                  if (!regex.Match(FyrData, &pContext))
468                    {
469                            AppendText("Invalid SMS recieved");
470                            return;
471                    }
472            }
473            else
474          {          {
475                  FyrData.AppendChar(CharData[s]);                  AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske !
476          }          }
477          FyrData.Append(":");  
           
478          SmsCount = Splitter(FyrData);          SmsCount = Splitter(FyrData);
479          Temper = Splitter(FyrData);          Temper = Splitter(FyrData);
480          Flamme = Splitter(FyrData);          Flamme = Splitter(FyrData);
481          Flis = Splitter(FyrData);          Flis = Splitter(FyrData);
482          FremFejl = Splitter(FyrData);          FremFejl = Splitter(FyrData);
483          PowerFail = Splitter(FyrData);          PowerFail = Splitter(FyrData);
484  ///////////////////////////////////////////////////////////////////////////////////////////////////////                  
485  ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////                  CString SQL, Textwindow, InstallNR;
486                  m_Textwindow.GetWindowText(oldtekst);                  SQL.Format("select ID from installation where installationphonenr=%s",TlfNr);
487                  oldtekst.Append("\r\n");          
488                  oldtekst.Append("På næste linie kommer SmsCount. \r\n");                  CRecordset rs(&db);
489                  oldtekst.Append(SmsCount);                  rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
490                  oldtekst.Append("\r\n");                  if (rs.GetRecordCount()>0)
491                  oldtekst.Append("På næste linie kommer Temper. \r\n");                  {
492                  oldtekst.Append(Temper);                          rs.MoveFirst();        
493                  oldtekst.Append("\r\n");                          rs.GetFieldValue((short)0,InstallNR);
494                  oldtekst.Append("På næste linie kommer Flamme. \r\n");                  }
495                  oldtekst.Append(Flamme);                  rs.Close();
496                  oldtekst.Append("\r\n");  
497                  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);
498                  oldtekst.Append(Flis);                  try
499                  oldtekst.Append("\r\n");                  {
500                  oldtekst.Append("På næste linie kommer FremFejl. \r\n");                  db.ExecuteSQL(SQL);
501                  oldtekst.Append(FremFejl);                  }
502                  oldtekst.Append("\r\n");                  catch(CDBException* e)
503                  oldtekst.Append("På næste linie kommer PowerFail. \r\n");                  {
504                  oldtekst.Append(PowerFail);                          MessageBox(e->m_strError);
505                  oldtekst.Append("\r\n");                  }
506                  oldtekst.Append("På næste linie kommer TlfNR. \r\n");                  AppendText("Sms added to Log");
507                  oldtekst.Append(TlfNr);                  Sleep(150);
                 m_Textwindow.SetWindowText(oldtekst);  
 ///////////////////////////////////////////////////////////////////////////////////////////////////////  
 ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  
508  }  }
509  CString CFlisServerDlg::Splitter(CString& fyrdata)  CString CFlisServerDlg::Splitter(CString& fyrdata)
510  {  {
# Line 481  void CFlisServerDlg::OnBnClickedClose() Line 522  void CFlisServerDlg::OnBnClickedClose()
522  {  {
523          // TODO: Add your control notification handler code here          // TODO: Add your control notification handler code here
524          continueThread = 0;          continueThread = 0;
525          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);
526            DeleteSms();
527    
528          Sleep(500);          Sleep(500);
529          if( Serial.isOpen() )          if( Serial.isOpen() )
# Line 491  void CFlisServerDlg::OnBnClickedClose() Line 533  void CFlisServerDlg::OnBnClickedClose()
533                    
534          if(db.IsOpen())          if(db.IsOpen())
535          {          {
536          db.Close();                  db.Close();
537          }          }
538                    
539          OnOK();          OnOK();
540    
541  }  }
   
 void CFlisServerDlg::OnBnClickedGsmpin()  
 {  
         // TODO: Add your control notification handler code here  
         SetPin();  
         //Sleep(5000);  
 }  
542  void CFlisServerDlg::DeleteSms()  void CFlisServerDlg::DeleteSms()
543  {  {
544          vector<unsigned char> atcommand;          vector<unsigned char> atcommand;
# Line 522  void CFlisServerDlg::DeleteSms() Line 557  void CFlisServerDlg::DeleteSms()
557          writeFrame(atcommand);          writeFrame(atcommand);
558          Sleep(500);          Sleep(500);
559  }  }
560  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)  
561  {  {
562          CFlisServerDlg *t = (CFlisServerDlg*) thread;          CFlisServerDlg *t = (CFlisServerDlg*) thread;
563          t->runthread();          t->runthread();
# Line 546  void CFlisServerDlg::runthread() Line 573  void CFlisServerDlg::runthread()
573  {  {
574          while (continueThread != 0)          while (continueThread != 0)
575          {          {
576                  Reader();                  MyMainThread();
577          }          }
578  }  }
579  void CFlisServerDlg::Reader()  void CFlisServerDlg::MyMainThread()
580  {  {
581          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
582                  {                  {
# Line 557  void CFlisServerDlg::Reader() Line 584  void CFlisServerDlg::Reader()
584                          std::vector<unsigned char> answer = readFrame();                          std::vector<unsigned char> answer = readFrame();
585                          Sleep(500);                          Sleep(500);
586                          CString tekst, oldtekst;                          CString tekst, oldtekst;
                         int lol;  
                         char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
587                                    
588                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
589                          {                          {
590                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
591                                  {                                  {
592                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
593                                  }                                  }
594                          }                          }
595                          tekst.Append(":");                          tekst.Append(":");
596    
597                          CString command;                          CString command;
598                          bool plus;                          bool plus = false;
599                                  int pos = tekst.Find('+',0);                                  int pos = tekst.Find('+',0);
600                                  if (pos != -1)                                  if (pos != -1)
601                                  {                                  {
# Line 587  void CFlisServerDlg::Reader() Line 607  void CFlisServerDlg::Reader()
607                                  }                                  }
608                                                    
609                                                    
610                          if(tekst == "OK")                          if(tekst.MakeLower() == "ok")
611                          {                                {      
612                                  m_Textwindow.GetWindowText(oldtekst);                                  AppendText("OK tekst modtaget");
613                                  oldtekst.Append("\r\n");                          }
614                                  oldtekst.Append("OK tekst modtaget");                          else if (tekst.MakeLower() == "error")
615                                  m_Textwindow.SetWindowText(oldtekst);                          {
616                          }                                  CString send;
617                          else if (tekst == "error")                                  send.Append("error tekst");
618                          {                                  send.Append("\r\n");
619                                  m_Textwindow.GetWindowText(oldtekst);                                  send.Append(tekst);
620                                  oldtekst.Append("\r\n");                                  AppendText(send);
                                 oldtekst.Append("error tekst");  
                                 oldtekst.Append(tekst);  
                                 m_Textwindow.SetWindowText(oldtekst);  
621                          }                          }
622                          else if (plus = true)                          else if (plus == true)
623                          {                          {
624                                  if (command = "cmti")                                  if (command.MakeLower() == "cmti")
625                                  {                                  {
626                                          CString smscount, oldteskst;                                          CString smscount, oldteskst;
627                                          int pos = tekst.Find(',',0);                                          int pos = tekst.Find(',',0);
# Line 629  void CFlisServerDlg::Reader() Line 646  void CFlisServerDlg::Reader()
646                                                  {                                                  {
647                                                          data.push_back(smscount[i]);                                                          data.push_back(smscount[i]);
648                                                  }                                                  }
649                                                  for (int i=0; i<data.size();i++)                                                  AppendText(CString(&data[0]));
                                                 {  
                                                 oldteskst.AppendChar(data[i]);  
                                                 }  
                                                 m_Textwindow.SetWindowText(oldteskst);  
650                                                  writeFrame(data);                                                  writeFrame(data);
651                                                  Sleep(200);                                                  Sleep(200);
652                                                  ReadSms();                                                  ReadSms();
653                                                    ResetSms++;
654    
655                                  }                                  }
656                                  else if(command = "wind")                                  else if(command.MakeLower() == "wind")
657                                  {                                  {
658                                          m_Textwindow.GetWindowText(oldtekst);  
659                                          oldtekst.Append("\r\n");                                          CString send;
660                                          oldtekst.Append("Først lidt tekst på næste linie\r\n");                                          send.Append("Wind modtaget");
661                                          oldtekst.Append(tekst);                                          send.Append("\r\n");
662                                          m_Textwindow.SetWindowText(oldtekst);                                          send.Append(tekst);
663                                            AppendText(send);
664                                  }                                  }
665                          }                          }
666                          if(ResetSms == 50)                          else
667                            {
668                                    AppendText(tekst);
669                            }
670                            if(ResetSms == 20)
671                          {                          {
672                                  DeleteSms();                                  DeleteSms();
673                                    AppendText("Sms'er slettet da vi nåede grænsen");
674                          }                          }
675    
676                  }                  }
677                  if (continueThread = 1)                  if (continueThread == 1)
678                  {                  {
679                                                    
680                          ////////////////DBRead stuff//////////////////                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
681                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID;                          int commandtest = 0;
682                            int iAll = 1;
683                          std::vector<Commands> data;                          std::vector<Commands> data;
684                          data = DBRead();                          data = DBReadCommands();
685                          for (int i=0; i<data.size(); i++)                          for (int i=0; i<data.size(); i++)
686                          {                          {
687                                  testdata.Append(data[i].IDnr);                                  sIDnr = data[i].IDnr;
688                                  testdata.Append(":");                                  sCommandID = data[i].CommandID;
689                                  testdata.Append(data[i].CommandID);                                  commandtest = atoi(data[i].CommandID);
690                                  testdata.Append(":");                                  sInstallationsID = "0";
691                                  testdata.Append(data[i].InstallationsID);  
692                                  testdata.Append(":");                                  if(commandtest > 1)
693                          }                                  {
694                          sIDnr = Splitter(testdata);                                          sInstallationsID = data[i].InstallationsID;
695                          sCommandID = Splitter(testdata);                                  }
696                          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);  
                           
697                                                    
698                            if (commandtest > 1)
699                            {
700                                    iAll = 0;
701                            }
702    
703                          if (sIDnr.GetLength() > 0)                          if (sIDnr.GetLength() > 0)
704                          {                          {
705                                  DBReadData(sIDnr,sCommandID,sInstallationsID);                                  SendConfig(sIDnr,sCommandID,sInstallationsID);
706                          }                          }
707                            Sleep(300);
708    
709                            CheckAcknowledges();
710                          Sleep(500);                          Sleep(500);
711                          ////////////////DBRead stuff//////////////////                    }
712                    CTime now = CTime::GetCurrentTime();
713    
714                    CTimeSpan elapsed = now-Alive.tid;
715    
716                    if (elapsed.GetTotalSeconds() >= 900)
717                    {
718                            keepaliveandread();
719                  }                  }
720  }  }
721  void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID)  void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
722  {  {      
723          CString TlfNr;          
                   
         //kald dbreadphone som du kalder dbread  
724          std::vector<Installation> inst;          std::vector<Installation> inst;
725          inst = DBReadPhone(InstallationsID);  
726          for (int i=0; i<inst.size(); i++)          if (CommandID == "1")
727                    inst = DBReadPhone("0");
728            else
729                    inst = DBReadPhone(InstallationsID);
730    
731            for ( int j=0; j < inst.size(); j++)
732          {          {
733                  CString dataen = inst[i].InstPhoneNr;                  CString TlfNr, Imei, updaterate;
734                    TlfNr.Empty();
735                    Imei.Empty();
736                    updaterate.Empty();
737    
738                    CString dataen = inst[j].InstPhoneNr;
739                  TlfNr.Append(dataen);                  TlfNr.Append(dataen);
740                    CString Imeidata = inst[j].Imei;
741                    Imei.Append(Imeidata);
742                    CString updaterat = inst[j].Updaterate;
743                    updaterate.Append(updaterat);
744            
745                    vector<unsigned char> tlfnr;
746                    for (int i=0; i<TlfNr.GetLength(); i++)
747                    {
748                            tlfnr.push_back(TlfNr[i]);
749                    }
750                    
751                    int calcimei;
752                    __int64 buf;
753    
754                    buf = atof(Imei);
755                    
756                    calcimei = tversum(buf);
757                    
758                    Imei.Format("%d",calcimei);
759                    
760                    SendSmsHead(tlfnr);
761                    Sleep(250);
762                    
763                    vector<unsigned char> smsdata;
764                    
765                    for (int i=0; i<Imei.GetLength(); i++)
766                    {
767                            smsdata.push_back(Imei[i]);
768                    }
769                    smsdata.push_back(':');
770                    for (int i=0; i<Alive.Phonenr.GetLength(); i++)
771                    {
772                            smsdata.push_back(Alive.Phonenr[i]);
773                    }
774                    smsdata.push_back(':');
775                    for (int i=0; i<updaterate.GetLength(); i++)
776                    {
777                            smsdata.push_back(updaterate[i]);
778                    }
779    
780                    SendSmsData(smsdata);
781                    Sleep(500);
782    
783                    if (IDnr != "0")
784                    {
785                            Acks.push_back( Acknowledge(inst[j].instID,TlfNr) );
786                    }
787    
788          }          }
789          vector<unsigned char> tlfnr;  
790          for (int i=0; i<TlfNr.GetLength(); i++)          if (IDnr != "0")
791          {          {
792                  tlfnr.push_back(TlfNr[i]);                  CString SQL, Textwindow;
793                    SQL.Format("update command set executed=now() where id=%s",IDnr);
794                    db.ExecuteSQL(SQL);
795                    Sleep(100);
796                    AppendText("Command executed");
797          }          }
798            Sleep(150);
799    
         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');  
800    
         SendSmsData(smsdata);  
         Sleep(250);  
801    
         CString SQL;  
802    
         SQL.Format("update command set executed=now() where id=%s",IDnr);  
         db.ExecuteSQL(SQL);  
         Sleep(150);  
803  }  }
804  vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)  vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
805  {  {
806          vector<Installation> buffer;          vector<Installation> buffer;
807    
808          CString SQL, phonenr;          CString SQL, phonenr, imei, updaterate, id;
809          SQL.Format("select installationphonenr from installation WHERE id = %s",sInstallationsID);          Installation Myinst;
810    
811            if (sInstallationsID != "0")
812            {
813                    SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
814            }
815            else
816            {
817                    SQL.Format("select installationphonenr, imei, updaterate, id from installation");
818            }
819    
820          CRecordset rs(&db);          CRecordset rs(&db);
821          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
822          if (rs.GetRecordCount()>0)          if (rs.GetRecordCount()>0)
# Line 752  vector<Installation> CFlisServerDlg::DBR Line 824  vector<Installation> CFlisServerDlg::DBR
824                  rs.MoveFirst();                  rs.MoveFirst();
825                  while(!rs.IsEOF())                  while(!rs.IsEOF())
826                  {                  {
827                  Installation Myinst;                  
828                  rs.GetFieldValue((short)0,phonenr);                          rs.GetFieldValue((short)0,phonenr);
829                            rs.GetFieldValue(1,imei);
830                  Myinst.InstPhoneNr.Append(phonenr);                          rs.GetFieldValue(2,updaterate);
831                            rs.GetFieldValue(3,id);
832    
833                            Myinst.InstPhoneNr = phonenr;
834                            Myinst.Imei = imei;
835                            Myinst.Updaterate = updaterate;
836                            Myinst.instID = id;
837    
838                  buffer.push_back(Myinst);                          buffer.push_back(Myinst);
839                  rs.MoveNext();                          rs.MoveNext();
840                  }                  }
841          }          }
842          rs.Close();          rs.Close();
843          return buffer;          return buffer;
 }  
844    }
845    int CFlisServerDlg::tversum(__int64 input)
846    {
847            int sum = 0;
848            while (input > 0)
849            {
850                    sum += (input %10);
851                    input /= 10;
852            }
853            return sum;
854    }
855    void CFlisServerDlg::OnBnClickedStart()
856    {
857    // TODO: Add your control notification handler code here
858    
859            AppendText("Indsætter Pinkode, og venter på modem bliver klar, hvis pinkode er nødvendig");
860            UpdateWindow();
861            SetPin();
862            
863            Sleep(100); //Give the modem a chance to send the last data
864            while(Serial.getComstat().cbInQue > 0)
865            {
866                    Serial.readByte(); //Flush the incoming queue
867                    Sleep(1);
868            }
869        Sleep(5);
870    
871            continueThread = 1;
872            keepaliveandread();
873            Sleep(150);
874            AppendText(CString("Server phonenr read from db: ") + Alive.Phonenr );
875            AppendText("Started");
876            AfxBeginThread(threadWrapper,AfxGetMainWnd());
877    }
878    void CFlisServerDlg::AppendText(CString s)
879    {
880            CTime now = CTime::GetCurrentTime();
881            CString Tekst;
882            m_Textwindow.GetWindowText(Tekst);
883            Tekst.AppendFormat("%02d:%02d:%02d : ", now.GetHour(), now.GetMinute(), now.GetSecond());
884            Tekst.Append(s);
885            Tekst.Append("\r\n");
886    
887            m_Textwindow.SetWindowText(Tekst);
888            Sleep(5);
889            int g = m_Textwindow.GetScrollLimit(SB_VERT);
890            if (g > 0)
891            {
892            m_Textwindow.SetSel(m_Textwindow.GetWindowTextLength() -1,m_Textwindow.GetWindowTextLength(),false);
893            }
894    
895    }
896    void ConfigFile::ReadSettings()
897    {
898            ifstream file("Server-Settings.ini");
899            if (!file.is_open())
900                    throw("Could not open file");
901    
902            char buf[200];
903            while (!file.eof() ) {
904                    file.getline(buf,200);
905                    CString tmp(buf);
906    
907                    if (tmp.GetAt(0) == '#')
908                            continue;
909    
910                    int pos = tmp.Find('=');
911                    if (pos>0) {
912                            CString key = tmp.Left(pos).Trim().MakeLower();;
913                            CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
914    
915                            if (key == "host")
916                                    host = value;
917                            else if (key == "username")
918                                    username = value;
919                            else if (key == "password")
920                                    password = value;
921                            else if (key == "database")
922                                    database = value;
923                            else if (key == "comport")
924                                    comport = value;
925                            else if (key == "pincode")
926                                    pincode = value;
927                    }
928    
929            }
930    
931            file.close();
932    }
933    void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
934    {
935            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
936            {
937                    Acknowledge& current = (*it);
938                    if (current.sTlfNr == tlfnr)
939                    {
940                            CString SQL;
941                            SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID);
942                            db.ExecuteSQL(SQL);
943    
944                            Acks.erase(it);
945    
946                            return;
947                    }
948            }
949    }
950    
951    void CFlisServerDlg::CheckAcknowledges()
952    {
953            CTime now = CTime::GetCurrentTime();
954            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
955            {
956                    Acknowledge& current = (*it);
957    
958                    CTimeSpan elapsed = now-current.cTime;
959    
960                    if (elapsed.GetTotalSeconds() >= 300)
961                    {
962                            DumpAckList();
963                            if (current.iRetry >= 1)
964                            {
965                                    CString SQL;
966                                    SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID);
967                                    db.ExecuteSQL(SQL);
968                                    Acks.erase(it);
969                                    return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again
970                            }
971                            else
972                            {
973                                    SendConfig("0","2",current.sInstallationID);
974                                    current.iRetry++;
975                                    current.cTime = CTime::GetCurrentTime();
976                                    AppendText( CString("Retry send config to ") + current.sTlfNr );
977                            }
978                    }
979            }
980    }
981    
982    void CFlisServerDlg::DumpAckList()      /* Debug tool */
983    {
984            
985            CTime now = CTime::GetCurrentTime();
986            OutputDebugString("-------------------------------\n");
987            int count = 0;
988            for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
989            {
990                    CString msg;
991                    msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr);
992                    OutputDebugString(msg);
993                    count++;
994            }
995    }
996    vector<keepalive> CFlisServerDlg::keepaliveandread(void)
997    {
998            vector<keepalive> buffer;
999    
1000            CString SQL, name, value;
1001            SQL = "select name, value from config;";
1002            CRecordset rs(&db);
1003            try
1004            {
1005                    rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
1006                    if (rs.GetRecordCount()>0)
1007                    {
1008                            rs.MoveFirst();
1009                            
1010                            rs.GetFieldValue((short)0, name);
1011                            rs.GetFieldValue(1,value);
1012                    
1013                            Alive.name = name;
1014                            Alive.value = value;
1015                            Alive.tid = CTime::GetCurrentTime();
1016                            if(Alive.name == "phonenr")
1017                            {
1018                                    Alive.Phonenr = value;
1019                            }
1020    
1021                            buffer.push_back(Alive);
1022                            rs.MoveNext();
1023    
1024                    }
1025                    rs.Close();
1026                    return buffer;
1027            }
1028            catch (char * str)
1029            {
1030                    AppendText("Keepalive failed, closing and opening the db connection again");
1031                    if(db.IsOpen())
1032                    {
1033                            db.Close();
1034                            AppendText("DB connection closed");
1035                    }
1036                    if(!db.IsOpen())
1037                    {
1038                            DBConnect();
1039                            AppendText("DB connection started again");
1040                    }
1041                    return buffer;
1042            }
1043    
1044            
1045    }

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

  ViewVC Help
Powered by ViewVC 1.1.20