/[H9]/trunk/FlisServer/FlisServerDlg.cpp
ViewVC logotype

Diff of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 70 by kevin, Tue Nov 27 15:26:42 2007 UTC revision 110 by kevin, Fri Nov 30 16:03:46 2007 UTC
# Line 4  Line 4 
4  #include "stdafx.h"  #include "stdafx.h"
5  #include "FlisServer.h"  #include "FlisServer.h"
6  #include "FlisServerDlg.h"  #include "FlisServerDlg.h"
 #include ".\flisserverdlg.h"  
7  #include <vector>  #include <vector>
8    
9  #ifdef _DEBUG  #ifdef _DEBUG
# Line 63  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo Line 62  BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialo
62          ON_WM_SYSCOMMAND()          ON_WM_SYSCOMMAND()
63          ON_WM_PAINT()          ON_WM_PAINT()
64          ON_WM_QUERYDRAGICON()          ON_WM_QUERYDRAGICON()
65            ON_MESSAGE(UWM_MYMESSAGE, OnShowString)
66          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
         ON_BN_CLICKED(IDOK, OnBnClickedOk)  
         ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)  
67          ON_BN_CLICKED(IDC_test, OnBnClickedtest)          ON_BN_CLICKED(IDC_test, OnBnClickedtest)
68            ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
69            ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
70  END_MESSAGE_MAP()  END_MESSAGE_MAP()
71    
72    
# Line 101  BOOL CFlisServerDlg::OnInitDialog() Line 101  BOOL CFlisServerDlg::OnInitDialog()
101    
102          // TODO: Add extra initialization here          // TODO: Add extra initialization here
103          StartSerial();          StartSerial();
104          SetPin();          DBConnect();
105            continueThread = true;
106            //AfxBeginThread(MyMainThread,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
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 182  std::vector<unsigned char> CFlisServerDl Line 184  std::vector<unsigned char> CFlisServerDl
184          }          }
185          return buf;          return buf;
186  }  }
 void CFlisServerDlg::OnBnClickedOk()  
 {  
         // TODO: Add your control notification handler code here  
         OnOK();  
         if( Serial.isOpen() ){  
                 Serial.close();  
         }  
 }  
   
 void CFlisServerDlg::OnBnClickedCancel()  
 {  
         // TODO: Add your control notification handler code here  
         OnCancel();  
         if( Serial.isOpen() ){  
                 Serial.close();  
         }  
 }  
   
187  void CFlisServerDlg::OnBnClickedtest()  void CFlisServerDlg::OnBnClickedtest()
188  {  {
189          // TODO: Add your control notification handler code here          // TODO: Add your control notification handler code here
         /*  
         std::vector<unsigned char> data;  
         data.push_back('a');  
         data.push_back('t');  
190    
191          writeFrame(data);  /*
192    ////////////////DBRead stuff//////////////////
193          if(Serial.getComstat().cbInQue > 0)          CString testdata;
194            std::vector<Commands> data;
195            data = DBRead();
196            for (int i=0; i<data.size(); i++)
197          {          {
198          std::vector<unsigned char> answer = readFrame();                  testdata.Append(data[i].IDnr);
199          Sleep(50);                  testdata.Append(data[i].CommandID);
200          if (answer.size() == 0){                  testdata.Append(data[i].InstallationsID);
201                  m_Textwindow.SetWindowText("ØV");          }
202          }          m_Textwindow.SetWindowText(testdata);
203          else{  ////////////////DBRead stuff//////////////////  
204                  CString ko;  */
205                  char test[150];  /*      
206                  int i;  ///////////////Send sms stuff/////////////////
207                  for (int i=0; i<answer.size(); i++)          vector<unsigned char> tlfnr;
208                  {          vector<unsigned char> smsdata;
209                          test[i] = answer[i];          tlfnr.push_back('2');
210                  }          tlfnr.push_back('9');
211                  ko = test;          tlfnr.push_back('7');
212                  m_Textwindow.SetWindowText(ko);          tlfnr.push_back('2');
213          }          tlfnr.push_back('2');
214          }          tlfnr.push_back('6');
215          */          tlfnr.push_back('0');
216          SetPin();          tlfnr.push_back('3');
217    
218            SendSmsHead(tlfnr);
219    
220             //35:29860132:75 <--- demo pakke;
221            smsdata.push_back('3');
222            smsdata.push_back('5');
223            smsdata.push_back(':');
224            smsdata.push_back('2');
225            smsdata.push_back('9');
226            smsdata.push_back('8');
227            smsdata.push_back('6');
228            smsdata.push_back('0');
229            smsdata.push_back('1');
230            smsdata.push_back('3');
231            smsdata.push_back('2');
232            smsdata.push_back(':');
233            smsdata.push_back('7');
234            smsdata.push_back('5');
235    
236            SendSmsData(smsdata);
237    ///////////////Send sms stuff/////////////////
238    */
239    /*
240    ////////////Start Tråd////////////////////////
241            MyThread *t = new MyThread(AfxGetMainWnd());
242            t->start();
243    ////////////Start Tråd////////////////////////
244    */
245            AfxBeginThread(threadWrapper,AfxGetMainWnd());
246    
247  }  }
248  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
249  {  {
# Line 243  void CFlisServerDlg::writeFrame(std::vec Line 256  void CFlisServerDlg::writeFrame(std::vec
256          Sleep(100);          Sleep(100);
257    
258  }  }
259  int CFlisServerDlg::SetPin(void)  void CFlisServerDlg::SetPin()
260  {  {
261          CString tekst, oldtekst;          CString tekst;
262          std::vector<unsigned char> data;          std::vector<unsigned char> data;
263          data.push_back('a');          data.push_back('a');
264          data.push_back('t');          data.push_back('t');
# Line 280  int CFlisServerDlg::SetPin(void) Line 293  int CFlisServerDlg::SetPin(void)
293                                          tekst.AppendChar(array1[i]);                                          tekst.AppendChar(array1[i]);
294                                  }                                  }
295                          }                          }
296                            m_Textwindow.SetWindowText(tekst);
                         m_Textwindow.GetWindowText(oldtekst);  
                         oldtekst.Append("\r\n");  
                         oldtekst.Append(tekst);  
                         m_Textwindow.SetWindowText(oldtekst);  
297          }          }
 return 0;  
298  }  }
299  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
300  {  {      
301            
302          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
303          {          {
304                  Serial.writeByte( data[i] );                  Serial.writeByte( data[i] );
305                  Sleep(5);                  Sleep(5);
306          }          }
307          Serial.writeByte(0x1A);          Serial.writeByte(0x1A);
308          Sleep(100);          Sleep(3000);
309            if(Serial.getComstat().cbInQue > 0)
310            {
311                    CString tekst;
312                    std::vector<unsigned char> answer = readFrame();
313                    Sleep(50);
314                            char array1[25];
315                            int i;
316                            for (int i=0; i<answer.size(); i++)
317                            {
318                                    array1[i] = answer[i];
319                            }
320                    
321                            for (int i=0; i<answer.size(); i++)
322                            {
323                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
324                                    {
325                                            tekst.AppendChar(array1[i]);
326                                    }
327                            }
328                            m_Textwindow.SetWindowText(tekst);
329            }
330    
331  }  }
332  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)
333  {  {
334            vector<unsigned char> atcommand;
335            atcommand.push_back('a');
336            atcommand.push_back('t');
337            atcommand.push_back('+');
338            atcommand.push_back('c');
339            atcommand.push_back('m');
340            atcommand.push_back('g');
341            atcommand.push_back('s');
342            atcommand.push_back('=');
343            atcommand.push_back('"');
344            int s = (atcommand.size() -1 );
345            
346            for (int i=0; i<(atcommand.size()); i++)
347            {
348                    Serial.writeByte( atcommand[i] );
349                    Sleep(5);
350            }
351            
352          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
353          {          {
354                  Serial.writeByte( data[i] );                  Serial.writeByte( data[i] );
355                  Sleep(5);                  Sleep(5);
356          }          }
357            Serial.writeByte(atcommand[s]);
358          Serial.writeByte(0x0D);          Serial.writeByte(0x0D);
359          Sleep(100);          Sleep(250);
   
360  }  }
361  void CFlisServerDlg::DBconnect()  void CFlisServerDlg::DBConnect()
362  {  {
363          CString dsn;          CString dsn;
364          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=%s; uid=%s;password=%s;database=%s",config.host, config.username, config.password, config.database);          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");
365            //dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //skole server
366          db.OpenEx(dsn, CDatabase::noOdbcDialog);          db.OpenEx(dsn, CDatabase::noOdbcDialog);
367    }
368    vector<Commands> CFlisServerDlg::DBRead(void)
369    {
370            vector<Commands> buffer;
371    
372            CString SQL, IDnr, CommandID, InstallationsID;
373            SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;";
374            CRecordset rs(&db);
375            rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
376            if (rs.GetRecordCount()>0)
377            {
378                    rs.MoveFirst();
379                    while(!rs.IsEOF())
380                    {
381                    Commands Mycom;
382                    rs.GetFieldValue((short)0, IDnr);
383                    rs.GetFieldValue(3, CommandID);
384                    rs.GetFieldValue(4, InstallationsID);
385                    
386                    Mycom.IDnr = IDnr;
387                    Mycom.CommandID = CommandID;
388                    Mycom.InstallationsID = InstallationsID;
389    
390                    buffer.push_back(Mycom);
391                    rs.MoveNext();
392                    }
393            }
394            rs.Close();
395            return buffer;
396    }
397    void CFlisServerDlg::ReadSms()
398    {
399            CString tekst, oldtekst;
400            Sleep(950);
401            if(Serial.getComstat().cbInQue > 0)
402            {
403                    std::vector<unsigned char> answer = readFrame();
404                    Sleep(50);
405                            char array1[250];
406                            int i;
407                            for (int i=0; i<answer.size(); i++)
408                            {
409                                    array1[i] = answer[i];
410                            }
411                    
412                            for (int i=0; i<answer.size(); i++)
413                            {
414                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
415                                    {
416                                            tekst.AppendChar(array1[i]);
417                                    }
418                            }
419    
420                            m_Textwindow.GetWindowText(oldtekst);
421                            oldtekst.Append("\r\n");
422                            oldtekst.Append(tekst);
423                            m_Textwindow.SetWindowText(oldtekst);
424                            SmsSplit(tekst);
425            }
426    }
427    void CFlisServerDlg::SmsSplit(CString data)
428    {
429            CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
430            char CharData[150];
431            strcpy(CharData,data);
432    
433            int s=22;
434            for (int i=0; i<=7; i++)
435            {
436                    TlfNr.AppendChar(CharData[s]);
437                    s++;
438            }
439    
440            for (int s=55; s<=(data.GetLength()-3); s++)
441            {
442                    FyrData.AppendChar(CharData[s]);
443            }
444            FyrData.Append(":");
445            
446            SmsCount = Splitter(FyrData);
447            Temper = Splitter(FyrData);
448            Flamme = Splitter(FyrData);
449            Flis = Splitter(FyrData);
450            FremFejl = Splitter(FyrData);
451            PowerFail = Splitter(FyrData);
452    ///////////////////////////////////////////////////////////////////////////////////////////////////////
453    ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
454                    m_Textwindow.GetWindowText(oldtekst);
455                    oldtekst.Append("\r\n");
456                    oldtekst.Append("På næste linie kommer SmsCount. \r\n");
457                    oldtekst.Append(SmsCount);
458                    oldtekst.Append("\r\n");
459                    oldtekst.Append("På næste linie kommer Temper. \r\n");
460                    oldtekst.Append(Temper);
461                    oldtekst.Append("\r\n");
462                    oldtekst.Append("På næste linie kommer Flamme. \r\n");
463                    oldtekst.Append(Flamme);
464                    oldtekst.Append("\r\n");
465                    oldtekst.Append("På næste linie kommer Flis. \r\n");
466                    oldtekst.Append(Flis);
467                    oldtekst.Append("\r\n");
468                    oldtekst.Append("På næste linie kommer FremFejl. \r\n");
469                    oldtekst.Append(FremFejl);
470                    oldtekst.Append("\r\n");
471                    oldtekst.Append("På næste linie kommer PowerFail. \r\n");
472                    oldtekst.Append(PowerFail);
473                    oldtekst.Append("\r\n");
474                    oldtekst.Append("På næste linie kommer TlfNR. \r\n");
475                    oldtekst.Append(TlfNr);
476                    m_Textwindow.SetWindowText(oldtekst);
477    ///////////////////////////////////////////////////////////////////////////////////////////////////////
478    ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
479    }
480    CString CFlisServerDlg::Splitter(CString& fyrdata)
481    {
482            CString Output;
483    
484            int pos = fyrdata.Find(':',0);
485            if (pos != -1)
486                    {
487                            Output = fyrdata.Left(pos);
488                            fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
489                    }
490            return Output;
491    }
492    void CFlisServerDlg::OnBnClickedClose()
493    {
494            // TODO: Add your control notification handler code here
495            continueThread = false;
496            if( Serial.isOpen() )
497            {
498                    Serial.close();
499            }
500            
501            if(db.IsOpen())
502            {
503            db.Close();
504            }
505            DeleteSms();
506            OnOK();
507    }
508    
509    void CFlisServerDlg::OnBnClickedGsmpin()
510    {
511            // TODO: Add your control notification handler code here
512            SetPin();
513            Sleep(5000);
514    }
515    void CFlisServerDlg::DeleteSms()
516    {
517            vector<unsigned char> atcommand;
518            atcommand.push_back('a');
519            atcommand.push_back('t');
520            atcommand.push_back('+');
521            atcommand.push_back('c');
522            atcommand.push_back('m');
523            atcommand.push_back('g');
524            atcommand.push_back('d');
525            atcommand.push_back('=');
526            atcommand.push_back('1');
527            atcommand.push_back(',');
528            atcommand.push_back('3');
529    
530            writeFrame(atcommand);
531            Sleep(500);
532            if(Serial.getComstat().cbInQue > 0)
533            {
534                    std::vector<unsigned char> answer = readFrame();
535                    Sleep(50);
536                            CString tekst, oldtekst;
537                            char array1[250];
538                            int i;
539                            for (int i=0; i<answer.size(); i++)
540                            {
541                                    array1[i] = answer[i];
542                            }
543                    
544                            for (int i=0; i<answer.size(); i++)
545                            {
546                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
547                                    {
548                                            tekst.AppendChar(array1[i]);
549                                    }
550                            }
551                            if(tekst == "OK")
552                            {      
553                                    m_Textwindow.GetWindowText(oldtekst);
554                                    oldtekst.Append("\r\n");
555                                    oldtekst.Append("Old Sms messages deleted");
556                                    m_Textwindow.SetWindowText(oldtekst);
557                            }
558                            else
559                            {
560                                    m_Textwindow.GetWindowText(oldtekst);
561                                    oldtekst.Append("\r\n");
562                                    oldtekst.Append(tekst);
563                                    m_Textwindow.SetWindowText(oldtekst);
564                            }
565            }
566    }
567    LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam)
568    {
569            CString *s = (CString*) lParam;
570            GetDlgItem(IDC_Textwindow)->SetWindowText(*s);
571    
572            delete s;
573            return 0;
574    
575    }UINT threadWrapper(LPVOID thread)
576    {
577            CFlisServerDlg *t = (CFlisServerDlg*) thread;
578            t->runthread();
579            return 0;
580    }
581    
582    void CFlisServerDlg::startthread()
583    {
584            AfxBeginThread(threadWrapper, (LPVOID) this);
585    }
586    
587    void CFlisServerDlg::runthread()
588    {
589            int count = 0;
590            while (continueThread = true)
591            {
592                    Reader();
593            }
594    }
595    void CFlisServerDlg::Reader()
596    {
597            int ResetSms = 0;
598            while(1)
599            {
600                    if(Serial.getComstat().cbInQue > 0)
601                    {
602                            std::vector<unsigned char> answer = readFrame();
603                            Sleep(500);
604                            CString tekst, oldtekst;
605                            int lol;
606                            char array1[250];
607                            int i;
608                            for (int i=0; i<answer.size(); i++)
609                            {
610                                    array1[i] = answer[i];
611                            }
612                    
613                            for (int i=0; i<answer.size(); i++)
614                            {
615                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
616                                    {
617                                            tekst.AppendChar(array1[i]);
618                                    }
619                            }
620                            tekst.Append(":");
621    
622                            CString command;
623                            bool plus;
624                                    int pos = tekst.Find('+',0);
625                                    if (pos != -1)
626                                    {
627                                            plus = true;
628                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
629                                            pos = tekst.Find(':');
630                                            command = tekst.Left(pos);
631                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
632                                    }
633                            
634                            
635                            if(tekst == "OK")
636                            {      
637                                    m_Textwindow.GetWindowText(oldtekst);
638                                    oldtekst.Append("\r\n");
639                                    oldtekst.Append("OK tekst modtaget");
640                                    m_Textwindow.SetWindowText(oldtekst);
641                            }
642                            else if (tekst == "error")
643                            {
644                                    m_Textwindow.GetWindowText(oldtekst);
645                                    oldtekst.Append("\r\n");
646                                    oldtekst.Append("error tekst");
647                                    oldtekst.Append(tekst);
648                                    m_Textwindow.SetWindowText(oldtekst);
649                            }
650                            else if (plus = true)
651                            {
652                                    if (command = "cmti")
653                                    {
654                                            CString smscount;
655                                            int pos = tekst.Find(',',0);
656                                            if (pos != -1)
657                                            {
658                                                    smscount = tekst.Right( tekst.GetLength() - pos -1);
659                                                    smscount.Remove(':');
660                                                    ResetSms++;
661                                            }
662                                                    ////////////////Read sms ting//////////////////
663                                                    std::vector<unsigned char> data;
664                                                    data.push_back('a');
665                                                    data.push_back('t');
666                                                    data.push_back('+');
667                                                    data.push_back('c');
668                                                    data.push_back('m');
669                                                    data.push_back('g');
670                                                    data.push_back('r');
671                                                    data.push_back('=');
672                                                    
673                                                    for (int i=0; i< smscount.GetLength(); i++)
674                                                    {
675                                                            data.push_back(smscount[i]);
676                                                    }
677    
678                                                    writeFrame(data);
679                                                    ReadSms();
680    
681                                    }
682                            }
683                            if(ResetSms >= 50)
684                            {
685                                    DeleteSms();
686                            }
687                            
688                            
689                    }
690            }
691  }  }

Legend:
Removed from v.70  
changed lines
  Added in v.110

  ViewVC Help
Powered by ViewVC 1.1.20