/[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 132 by kevin, Mon Dec 3 11:46:03 2007 UTC revision 138 by kevin, Mon Dec 3 14:40:44 2007 UTC
# Line 63  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo Line 63  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo
63          ON_WM_SYSCOMMAND()          ON_WM_SYSCOMMAND()
64          ON_WM_PAINT()          ON_WM_PAINT()
65          ON_WM_QUERYDRAGICON()          ON_WM_QUERYDRAGICON()
         ON_MESSAGE(UWM_MYMESSAGE, OnShowString)  
66          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
67          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
68          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
# Line 158  HCURSOR CFlisServerDlg::OnQueryDragIcon( Line 157  HCURSOR CFlisServerDlg::OnQueryDragIcon(
157  }  }
158  int CFlisServerDlg::StartSerial(void)  int CFlisServerDlg::StartSerial(void)
159  {  {
160            
161          int Baud;          int Baud;
         CString SerialPort = "COM3";  
162    
163            ConfigFile config;
164            try {
165                    config.ReadSettings();
166            } catch(...) {
167                    MessageBox("Could not open config file");
168                    OnOK();
169                    return true;
170            }
171            
172          Baud = 1200;          Baud = 1200;
173          if( Serial.isOpen() )          if( Serial.isOpen() )
174          {          {
175                  Serial.close();                  try
176                  Serial.open( SerialPort, Baud );                  {
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          {          {
188                  Serial.open( SerialPort, Baud );                  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 232  void CFlisServerDlg::SendSmsData(std::ve Line 256  void CFlisServerDlg::SendSmsData(std::ve
256                  CString tekst;                  CString tekst;
257                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
258                  Sleep(50);                  Sleep(50);
259                          char array1[150];  
260                          int i;                  for (int i=0; i<answer.size(); i++)
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
                   
                         for (int i=0; i<answer.size(); i++)  
261                          {                          {
262                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
263                                  {                                  {
264                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
265                                  }                                  }
266                          }                          }
267                          m_Textwindow.SetWindowText(tekst);                          AppendText(tekst);
268          }          }
269    
270  }  }
271  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
272  {  {
273          vector<unsigned char> atcommand;          vector<unsigned char> atcommand;
274          atcommand.push_back('a');          atcommand.push_back('a');
# Line 270  void CFlisServerDlg::SendSmsHead(std::ve Line 288  void CFlisServerDlg::SendSmsHead(std::ve
288                  Sleep(5);                  Sleep(5);
289          }          }
290                    
291          for (int i=0; i<data.size(); i++)          for (int i=0; i<tlfnr.size(); i++)
292          {          {
293                  Serial.writeByte( data[i] );                  Serial.writeByte( tlfnr[i] );
294                  Sleep(5);                  Sleep(5);
295          }          }
296          Serial.writeByte(atcommand[s]);          Serial.writeByte(atcommand[s]);
# Line 282  void CFlisServerDlg::SendSmsHead(std::ve Line 300  void CFlisServerDlg::SendSmsHead(std::ve
300  void CFlisServerDlg::DBConnect()  void CFlisServerDlg::DBConnect()
301  {  {
302          CString dsn;          CString dsn;
303          //dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //Torben          ConfigFile config;
304          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //skole server          try {
305                    config.ReadSettings();
306            } catch(...) {
307                    MessageBox("Could not open config file");
308            }
309            
310            dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=%s; uid=%s;password=%s;database=%s;sslmode=prefer",config.host,config.username, config.password, config.database);
311          db.OpenEx(dsn, CDatabase::noOdbcDialog);          db.OpenEx(dsn, CDatabase::noOdbcDialog);
312  }  }
313  vector<Commands> CFlisServerDlg::DBRead(void)  vector<Commands> CFlisServerDlg::DBReadCommands(void)
314  {  {
315          vector<Commands> buffer;          vector<Commands> buffer;
316    
317          CString SQL, IDnr, CommandID, InstallationsID;          CString SQL, IDnr, CommandID, InstallationsID;
318          SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;";          SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL ORDER BY created ASC LIMIT 1;";
319          CRecordset rs(&db);          CRecordset rs(&db);
320          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);          rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
321          if (rs.GetRecordCount()>0)          if (rs.GetRecordCount()>0)
322          {          {
323                  rs.MoveFirst();                  rs.MoveFirst();
                 while(!rs.IsEOF())  
                 {  
                         Commands Mycom;  
                         rs.GetFieldValue((short)0, IDnr);  
                         rs.GetFieldValue(3, CommandID);  
                         rs.GetFieldValue(4, InstallationsID);  
324                                                    
325                          Mycom.IDnr = IDnr;                  Commands Mycom;
326                          Mycom.CommandID = CommandID;                  rs.GetFieldValue((short)0, IDnr);
327                          Mycom.InstallationsID = InstallationsID;                  rs.GetFieldValue(3, CommandID);
328                    rs.GetFieldValue(4, InstallationsID);
329                    
330                    Mycom.IDnr = IDnr;
331                    Mycom.CommandID = CommandID;
332                    Mycom.InstallationsID = InstallationsID;
333    
334                    buffer.push_back(Mycom);
335                    rs.MoveNext();
336    
                         buffer.push_back(Mycom);  
                         rs.MoveNext();  
                 }  
337          }          }
338          rs.Close();          rs.Close();
339          return buffer;          return buffer;
# Line 318  vector<Commands> CFlisServerDlg::DBRead( Line 341  vector<Commands> CFlisServerDlg::DBRead(
341  void CFlisServerDlg::ReadSms()  void CFlisServerDlg::ReadSms()
342  {  {
343          CString tekst, oldtekst;          CString tekst, oldtekst;
344          Sleep(950);          Sleep(950);             //Holder en pause for at lade hele sms'en komme ind i serial køen.
345          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
346          {          {
347                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
348                  Sleep(50);                  Sleep(50);
349                          char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
                   
350                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
351                          {                          {
352                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
353                                  {                                  {
354                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
355                                  }                                  }
356                          }                          }
357    
358                          m_Textwindow.GetWindowText(oldtekst);                          AppendText(tekst);
                         oldtekst.Append("\r\n");  
                         oldtekst.Append(tekst);  
                         m_Textwindow.SetWindowText(oldtekst);  
359                          SmsSplit(tekst);                          SmsSplit(tekst);
360          }          }
361  }  }
# Line 350  void CFlisServerDlg::SmsSplit(CString da Line 364  void CFlisServerDlg::SmsSplit(CString da
364          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;          CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
365          char CharData[150];          char CharData[150];
366          strcpy(CharData,data);          strcpy(CharData,data);
367            TlfNr = data.Mid(24,8);
368          int s=24;          FyrData = data.Mid(57,data.GetLength()-59);
         for (int i=0; i<=7; i++)  
         {  
                 TlfNr.AppendChar(CharData[s]);  
                 s++;  
         }  
   
         for (int s=57; s<=(data.GetLength()-3); s++)  
         {  
                 FyrData.AppendChar(CharData[s]);  
         }  
369          FyrData.Append(":");          FyrData.Append(":");
370                    
371          SmsCount = Splitter(FyrData);          SmsCount = Splitter(FyrData);
# Line 384  void CFlisServerDlg::SmsSplit(CString da Line 388  void CFlisServerDlg::SmsSplit(CString da
388                  rs.Close();                  rs.Close();
389    
390                  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);                  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);
391                    try
392                    {
393                  db.ExecuteSQL(SQL);                  db.ExecuteSQL(SQL);
394                    }
395                  m_Textwindow.GetWindowText(Textwindow);                  catch(CDBException* e)
396                  Textwindow.Append("\r\n");                  {
397                  Textwindow.Append("Sms added to Log");                          MessageBox(e->m_strError);
398                  m_Textwindow.SetWindowText(Textwindow);                  }
399                    AppendText("Sms added to Log");
400                  Sleep(150);                  Sleep(150);
   
 ///////////////////////////////////////////////////////////////////////////////////////////////////////  
 ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  
401  }  }
402  CString CFlisServerDlg::Splitter(CString& fyrdata)  CString CFlisServerDlg::Splitter(CString& fyrdata)
403  {  {
# Line 461  void CFlisServerDlg::DeleteSms() Line 465  void CFlisServerDlg::DeleteSms()
465          writeFrame(atcommand);          writeFrame(atcommand);
466          Sleep(500);          Sleep(500);
467  }  }
468  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)  
469  {  {
470          CFlisServerDlg *t = (CFlisServerDlg*) thread;          CFlisServerDlg *t = (CFlisServerDlg*) thread;
471          t->runthread();          t->runthread();
# Line 497  void CFlisServerDlg::Reader() Line 493  void CFlisServerDlg::Reader()
493                          Sleep(500);                          Sleep(500);
494                          CString tekst, oldtekst;                          CString tekst, oldtekst;
495                          int lol;                          int lol;
                         char array1[250];  
                         int i;  
                         for (int i=0; i<answer.size(); i++)  
                         {  
                                 array1[i] = answer[i];  
                         }  
496                                    
497                          for (int i=0; i<answer.size(); i++)                          for (int i=0; i<answer.size(); i++)
498                          {                          {
499                                  if ((array1[i] != 0x0A) && (array1[i] != 0x0D))                                  if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
500                                  {                                  {
501                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(answer[i]);
502                                  }                                  }
503                          }                          }
504                          tekst.Append(":");                          tekst.Append(":");
# Line 526  void CFlisServerDlg::Reader() Line 516  void CFlisServerDlg::Reader()
516                                  }                                  }
517                                                    
518                                                    
519                          if(tekst == "OK")                          if(tekst.MakeLower() == "ok")
520                          {                                {      
521                                  m_Textwindow.GetWindowText(oldtekst);                                  AppendText("OK tekst modtaget");
                                 oldtekst.Append("\r\n");  
                                 oldtekst.Append("OK tekst modtaget");  
                                 m_Textwindow.SetWindowText(oldtekst);  
522                          }                          }
523                          else if (tekst == "error")                          else if (tekst.MakeLower() == "error")
524                          {                          {
525                                  m_Textwindow.GetWindowText(oldtekst);                                  CString send;
526                                  oldtekst.Append("\r\n");                                  send.Append("error tekst");
527                                  oldtekst.Append("error tekst");                                  send.Append("\r\n");
528                                  oldtekst.Append(tekst);                                  send.Append(tekst);
529                                  m_Textwindow.SetWindowText(oldtekst);                                  AppendText(send);
530                          }                          }
531                          else if (plus = true)                          else if (plus == true)
532                          {                          {
533                                  if (command = "cmti")                                  if (command.MakeLower() == "cmti")
534                                  {                                  {
535                                          CString smscount, oldteskst;                                          CString smscount, oldteskst;
536                                          int pos = tekst.Find(',',0);                                          int pos = tekst.Find(',',0);
# Line 581  void CFlisServerDlg::Reader() Line 568  void CFlisServerDlg::Reader()
568                                                  ResetSms++;                                                  ResetSms++;
569    
570                                  }                                  }
571                                  else if(command = "wind")                                  else if(command.MakeLower() == "wind")
572                                  {                                  {
573                                          m_Textwindow.GetWindowText(oldtekst);  
574                                          oldtekst.Append("\r\n");                                          CString send;
575                                          oldtekst.Append("Først lidt tekst på næste linie\r\n");                                          send.Append("Wind modtaget");
576                                          oldtekst.Append(tekst);                                          send.Append("\r\n");
577                                          m_Textwindow.SetWindowText(oldtekst);                                          send.Append(tekst);
578                                            AppendText(send);
579                                  }                                  }
580                          }                          }
581                          else                          else
582                          {                          {
583                                  m_Textwindow.GetWindowText(oldtekst);                                  CString send;
584                                  oldtekst.Append("\r\n");                                  send.Append("Anden tekst end forventet");
585                                  oldtekst.Append("Anden tekst end forventet");                                  send.Append("\r\n");
586                                  oldtekst.Append("\r\n");                                  send.Append(tekst);
587                                  oldtekst.Append(tekst);                                  AppendText(send);
                                 m_Textwindow.SetWindowText(oldtekst);  
588                          }                          }
589                          if(ResetSms == 20)                          if(ResetSms == 20)
590                          {                          {
591                                  DeleteSms();                                  DeleteSms();
592                                  m_Textwindow.GetWindowText(oldtekst);                                  AppendText("Sms'er slettet da vi nåede grænsen");
                                 oldtekst.Append("\r\n");  
                                 oldtekst.Append("Slettet Sendte og læste beskeder da vi nåede grænsen.");  
                                 m_Textwindow.SetWindowText(oldtekst);  
593                          }                          }
594    
595                  }                  }
596                  if (continueThread = 1)                  if (continueThread == 1)
597                  {                  {
598                                                    
599                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;                          CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
600                          int commandtest = 0;                          int commandtest = 0;
601                          int iAll = 1;                          int iAll = 1;
602                          std::vector<Commands> data;                          std::vector<Commands> data;
603                          data = DBRead();                          data = DBReadCommands();
604                          for (int i=0; i<data.size(); i++)                          for (int i=0; i<data.size(); i++)
605                          {                          {
606                                  testdata.Append(data[i].IDnr);                                  sIDnr = data[i].IDnr;
607                                  testdata.Append(":");                                  sCommandID = data[i].CommandID;
                                 testdata.Append(data[i].CommandID);  
                                 testdata.Append(":");  
608                                  commandtest = atoi(data[i].CommandID);                                  commandtest = atoi(data[i].CommandID);
609                                    sInstallationsID = "0";
610    
611                                  if(commandtest > 1)                                  if(commandtest > 1)
612                                  {                                  {
613                                          testdata.Append(data[i].InstallationsID);                                          sInstallationsID = data[i].InstallationsID;
                                         testdata.Append(":");  
614                                  }                                  }
615                          }                          }
616                          sIDnr = Splitter(testdata);                          
                         sCommandID = Splitter(testdata);  
                         sInstallationsID = "0";  
617                          if (commandtest > 1)                          if (commandtest > 1)
618                          {                          {
                                 sInstallationsID = Splitter(testdata);  
619                                  iAll = 0;                                  iAll = 0;
620                          }                          }
621    
622                          if (sIDnr.GetLength() > 0)                          if (sIDnr.GetLength() > 0)
623                          {                          {
624                                  DBReadData(sIDnr,sCommandID,sInstallationsID);                                  SendConfig(sIDnr,sCommandID,sInstallationsID);
625                          }                          }
626                          Sleep(500);                          Sleep(500);
627                  }                  }
628  }  }
629  void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID)  void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
630  {  {
631          CString ServerTlfNr;          CString ServerTlfNr;
632          int i = 0;          int i = 0;
# Line 713  void CFlisServerDlg::DBReadData(CString Line 692  void CFlisServerDlg::DBReadData(CString
692                  CString SQL, Textwindow;                  CString SQL, Textwindow;
693                  SQL.Format("update command set executed=now() where id=%s",IDnr);                  SQL.Format("update command set executed=now() where id=%s",IDnr);
694                  db.ExecuteSQL(SQL);                  db.ExecuteSQL(SQL);
695                  m_Textwindow.GetWindowText(Textwindow);                  AppendText("Command executed");
                 Textwindow.Append("\r\n");  
                 Textwindow.Append("Command executed");  
                 m_Textwindow.SetWindowText(Textwindow);  
696                  Sleep(150);                  Sleep(150);
697    
698  }  }
# Line 777  void CFlisServerDlg::OnBnClickedStart() Line 753  void CFlisServerDlg::OnBnClickedStart()
753          m_Textwindow.SetWindowText("Started");          m_Textwindow.SetWindowText("Started");
754          AfxBeginThread(threadWrapper,AfxGetMainWnd());          AfxBeginThread(threadWrapper,AfxGetMainWnd());
755  }  }
756    void CFlisServerDlg::AppendText(CString s)
757    {
758            CString Tekst;
759            m_Textwindow.GetWindowText(Tekst);
760            Tekst.Append("\r\n");
761            Tekst.Append(s);
762            m_Textwindow.SetWindowText(Tekst);
763    
764    }
765    void ConfigFile::ReadSettings()
766    {
767            ifstream file("Server-Settings.ini");
768            if (!file.is_open())
769                    throw("Could not open file");
770    
771            char buf[200];
772            while (!file.eof() ) {
773                    file.getline(buf,200);
774                    CString tmp(buf);
775    
776                    if (tmp.GetAt(0) == '#')
777                            continue;
778    
779                    int pos = tmp.Find('=');
780                    if (pos>0) {
781                            CString key = tmp.Left(pos).Trim().MakeLower();;
782                            CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
783    
784                            if (key == "host")
785                                    host = value;
786                            else if (key == "username")
787                                    username = value;
788                            else if (key == "password")
789                                    password = value;
790                            else if (key == "database")
791                                    database = value;
792                            else if (key == "comport")
793                                    comport = value;
794                    }
795    
796            }
797    
798            file.close();
799    }

Legend:
Removed from v.132  
changed lines
  Added in v.138

  ViewVC Help
Powered by ViewVC 1.1.20