/[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 81 by kevin, Wed Nov 28 16:24:04 2007 UTC revision 113 by kevin, Sat Dec 1 14:18:33 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 51  CFlisServerDlg::CFlisServerDlg(CWnd* pPa Line 50  CFlisServerDlg::CFlisServerDlg(CWnd* pPa
50          : CDialog(CFlisServerDlg::IDD, pParent)          : CDialog(CFlisServerDlg::IDD, pParent)
51  {  {
52          m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);          m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
         db = 0;  
53  }  }
54    
55  void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)  void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)
# Line 64  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 102  BOOL CFlisServerDlg::OnInitDialog() Line 101  BOOL CFlisServerDlg::OnInitDialog()
101    
102          // TODO: Add extra initialization here          // TODO: Add extra initialization here
103          StartSerial();          StartSerial();
         //SetPin();  
104          DBConnect();          DBConnect();
105            continueThread = 1;
106            ResetSms = 0;
107            /*
108    ////////////Start Tråd////////////////////////
109            MyThread *t = new MyThread(AfxGetMainWnd());
110            t->start();
111    ////////////Start Tråd////////////////////////
112    */
113            AfxBeginThread(threadWrapper,AfxGetMainWnd());
114                    
115          return TRUE;  // return TRUE  unless you set the focus to a control          return TRUE;  // return TRUE  unless you set the focus to a control
116  }  }
# Line 184  std::vector<unsigned char> CFlisServerDl Line 191  std::vector<unsigned char> CFlisServerDl
191          }          }
192          return buf;          return buf;
193  }  }
 void CFlisServerDlg::OnBnClickedOk()  
 {  
         // TODO: Add your control notification handler code here  
         OnOK();  
         if( Serial.isOpen() ){  
                 Serial.close();  
         }  
   
         if (db != 0)  
         {  
                 //db->Close();  
                 delete db;  
                 db=0;  
         }  
 }  
   
 void CFlisServerDlg::OnBnClickedCancel()  
 {  
         // TODO: Add your control notification handler code here  
         OnCancel();  
         if( Serial.isOpen() ){  
                 Serial.close();  
         }  
   
         if (db != 0)  
         {  
                 db->Close();  
                 delete db;  
                 db = 0;  
         }  
 }  
   
194  void CFlisServerDlg::OnBnClickedtest()  void CFlisServerDlg::OnBnClickedtest()
195  {  {
196          // TODO: Add your control notification handler code here          // TODO: Add your control notification handler code here
         CString tekst;  
         std::vector<unsigned char> data;  
         data.push_back('a');  
         data.push_back('t');  
         data.push_back('+');  
         data.push_back('c');  
         data.push_back('m');  
         data.push_back('g');  
         data.push_back('r');  
         data.push_back('=');  
         data.push_back('1');  
197    
198          writeFrame(data);  /*      
199          ReadSms();  ///////////////Send sms stuff/////////////////
200            vector<unsigned char> tlfnr;
201            vector<unsigned char> smsdata;
202            tlfnr.push_back('2');
203            tlfnr.push_back('9');
204            tlfnr.push_back('7');
205            tlfnr.push_back('2');
206            tlfnr.push_back('2');
207            tlfnr.push_back('6');
208            tlfnr.push_back('0');
209            tlfnr.push_back('3');
210    
211            SendSmsHead(tlfnr);
212    
213             //35:29860132:75 <--- demo pakke;
214             vector<unsigned char> smsdata;
215            smsdata.push_back('3');
216            smsdata.push_back('5');
217            smsdata.push_back(':');
218            smsdata.push_back('2');
219            smsdata.push_back('9');
220            smsdata.push_back('8');
221            smsdata.push_back('6');
222            smsdata.push_back('0');
223            smsdata.push_back('1');
224            smsdata.push_back('3');
225            smsdata.push_back('2');
226            smsdata.push_back(':');
227            smsdata.push_back('7');
228            smsdata.push_back('5');
229    
230            SendSmsData(smsdata);
231    ///////////////Send sms stuff/////////////////
232    */
233    
234  }  }
235  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
236  {  {
# Line 245  void CFlisServerDlg::writeFrame(std::vec Line 243  void CFlisServerDlg::writeFrame(std::vec
243          Sleep(100);          Sleep(100);
244    
245  }  }
246  int CFlisServerDlg::SetPin(void)  void CFlisServerDlg::SetPin()
247  {  {
248          CString tekst;          CString tekst;
249          std::vector<unsigned char> data;          std::vector<unsigned char> data;
# Line 263  int CFlisServerDlg::SetPin(void) Line 261  int CFlisServerDlg::SetPin(void)
261          data.push_back('5');          data.push_back('5');
262    
263          writeFrame(data);          writeFrame(data);
264          Sleep(750);  /*      Sleep(750);
265          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
266          {          {
267                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
# Line 284  int CFlisServerDlg::SetPin(void) Line 282  int CFlisServerDlg::SetPin(void)
282                          }                          }
283                          m_Textwindow.SetWindowText(tekst);                          m_Textwindow.SetWindowText(tekst);
284          }          }
285  return 0;  */
286  }  }
287  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
288  {  {      
289            
290          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
291          {          {
292                  Serial.writeByte( data[i] );                  Serial.writeByte( data[i] );
293                  Sleep(5);                  Sleep(5);
294          }          }
295          Serial.writeByte(0x1A);          Serial.writeByte(0x1A);
296          Sleep(100);          Sleep(3000);
297            if(Serial.getComstat().cbInQue > 0)
298            {
299                    CString tekst;
300                    std::vector<unsigned char> answer = readFrame();
301                    Sleep(50);
302                            char array1[25];
303                            int i;
304                            for (int i=0; i<answer.size(); i++)
305                            {
306                                    array1[i] = answer[i];
307                            }
308                    
309                            for (int i=0; i<answer.size(); i++)
310                            {
311                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
312                                    {
313                                            tekst.AppendChar(array1[i]);
314                                    }
315                            }
316                            m_Textwindow.SetWindowText(tekst);
317            }
318    
319  }  }
320  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)
321  {  {
322            vector<unsigned char> atcommand;
323            atcommand.push_back('a');
324            atcommand.push_back('t');
325            atcommand.push_back('+');
326            atcommand.push_back('c');
327            atcommand.push_back('m');
328            atcommand.push_back('g');
329            atcommand.push_back('s');
330            atcommand.push_back('=');
331            atcommand.push_back('"');
332            int s = (atcommand.size() -1 );
333            
334            for (int i=0; i<(atcommand.size()); i++)
335            {
336                    Serial.writeByte( atcommand[i] );
337                    Sleep(5);
338            }
339            
340          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
341          {          {
342                  Serial.writeByte( data[i] );                  Serial.writeByte( data[i] );
343                  Sleep(5);                  Sleep(5);
344          }          }
345            Serial.writeByte(atcommand[s]);
346          Serial.writeByte(0x0D);          Serial.writeByte(0x0D);
347          Sleep(100);          Sleep(250);
   
348  }  }
349  void CFlisServerDlg::DBConnect()  void CFlisServerDlg::DBConnect()
350  {  {
         db = new CDatabase();  
   
351          CString dsn;          CString dsn;
352          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr");          dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");
353          //db->OpenEx(dsn, CDatabase::noOdbcDialog);          //dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");   //skole server
354                    db.OpenEx(dsn, CDatabase::noOdbcDialog);
355  }  }
356  void CFlisServerDlg::DBRead()  vector<Commands> CFlisServerDlg::DBRead(void)
357  {  {
358            vector<Commands> buffer;
359    
360            CString SQL, IDnr, CommandID, InstallationsID;
361            SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;";
362            CRecordset rs(&db);
363            rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
364            if (rs.GetRecordCount()>0)
365            {
366                    rs.MoveFirst();
367                    while(!rs.IsEOF())
368                    {
369                    Commands Mycom;
370                    rs.GetFieldValue((short)0, IDnr);
371                    rs.GetFieldValue(3, CommandID);
372                    rs.GetFieldValue(4, InstallationsID);
373                    
374                    Mycom.IDnr = IDnr;
375                    Mycom.CommandID = CommandID;
376                    Mycom.InstallationsID = InstallationsID;
377    
378                    buffer.push_back(Mycom);
379                    rs.MoveNext();
380                    }
381            }
382            rs.Close();
383            return buffer;
384  }  }
385  void CFlisServerDlg::ReadSms()  void CFlisServerDlg::ReadSms()
386  {  {
# Line 357  void CFlisServerDlg::SmsSplit(CString da Line 418  void CFlisServerDlg::SmsSplit(CString da
418          char CharData[150];          char CharData[150];
419          strcpy(CharData,data);          strcpy(CharData,data);
420    
421          int s=22;          int s=24;
422          for (int i=0; i<=7; i++)          for (int i=0; i<=7; i++)
423          {          {
424                  TlfNr.AppendChar(CharData[s]);                  TlfNr.AppendChar(CharData[s]);
425                  s++;                  s++;
426          }          }
427    
428          for (int s=55; s<=(data.GetLength()-3); s++)          for (int s=57; s<=(data.GetLength()-3); s++)
429          {          {
430                  FyrData.AppendChar(CharData[s]);                  FyrData.AppendChar(CharData[s]);
431          }          }
# Line 397  void CFlisServerDlg::SmsSplit(CString da Line 458  void CFlisServerDlg::SmsSplit(CString da
458                  oldtekst.Append("\r\n");                  oldtekst.Append("\r\n");
459                  oldtekst.Append("På næste linie kommer PowerFail. \r\n");                  oldtekst.Append("På næste linie kommer PowerFail. \r\n");
460                  oldtekst.Append(PowerFail);                  oldtekst.Append(PowerFail);
461                    oldtekst.Append("\r\n");
462                    oldtekst.Append("På næste linie kommer TlfNR. \r\n");
463                    oldtekst.Append(TlfNr);
464                  m_Textwindow.SetWindowText(oldtekst);                  m_Textwindow.SetWindowText(oldtekst);
465  ///////////////////////////////////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////////////////////////////////
466  ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
# Line 412  CString CFlisServerDlg::Splitter(CString Line 476  CString CFlisServerDlg::Splitter(CString
476                          fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);                          fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
477                  }                  }
478          return Output;          return Output;
479    }
480    void CFlisServerDlg::OnBnClickedClose()
481    {
482            // TODO: Add your control notification handler code here
483            continueThread = 0;
484            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
485    
486            Sleep(500);
487            if( Serial.isOpen() )
488            {
489                    Serial.close();
490            }
491            
492            if(db.IsOpen())
493            {
494            db.Close();
495            }
496            
497            OnOK();
498    
499    }
500    
501    void CFlisServerDlg::OnBnClickedGsmpin()
502    {
503            // TODO: Add your control notification handler code here
504            SetPin();
505            //Sleep(5000);
506    }
507    void CFlisServerDlg::DeleteSms()
508    {
509            vector<unsigned char> atcommand;
510            atcommand.push_back('a');
511            atcommand.push_back('t');
512            atcommand.push_back('+');
513            atcommand.push_back('c');
514            atcommand.push_back('m');
515            atcommand.push_back('g');
516            atcommand.push_back('d');
517            atcommand.push_back('=');
518            atcommand.push_back('1');
519            atcommand.push_back(',');
520            atcommand.push_back('3');
521    
522            writeFrame(atcommand);
523            Sleep(500);
524    }
525    LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam)
526    {
527            CString *s = (CString*) lParam;
528            GetDlgItem(IDC_Textwindow)->SetWindowText(*s);
529    
530            delete s;
531            return 0;
532    
533    }UINT threadWrapper(LPVOID thread)
534    {
535            CFlisServerDlg *t = (CFlisServerDlg*) thread;
536            t->runthread();
537            return 0;
538    }
539    
540    void CFlisServerDlg::startthread()
541    {
542            AfxBeginThread(threadWrapper, (LPVOID) this);
543    }
544    
545    void CFlisServerDlg::runthread()
546    {
547            while (continueThread != 0)
548            {
549                    Reader();
550            }
551    }
552    void CFlisServerDlg::Reader()
553    {
554            if(Serial.getComstat().cbInQue > 0)
555                    {
556                            Sleep(250);
557                            std::vector<unsigned char> answer = readFrame();
558                            Sleep(500);
559                            CString tekst, oldtekst;
560                            int lol;
561                            char array1[250];
562                            int i;
563                            for (int i=0; i<answer.size(); i++)
564                            {
565                                    array1[i] = answer[i];
566                            }
567                    
568                            for (int i=0; i<answer.size(); i++)
569                            {
570                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
571                                    {
572                                            tekst.AppendChar(array1[i]);
573                                    }
574                            }
575                            tekst.Append(":");
576    
577                            CString command;
578                            bool plus;
579                                    int pos = tekst.Find('+',0);
580                                    if (pos != -1)
581                                    {
582                                            plus = true;
583                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
584                                            pos = tekst.Find(':');
585                                            command = tekst.Left(pos);
586                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
587                                    }
588                            
589                            
590                            if(tekst == "OK")
591                            {      
592                                    m_Textwindow.GetWindowText(oldtekst);
593                                    oldtekst.Append("\r\n");
594                                    oldtekst.Append("OK tekst modtaget");
595                                    m_Textwindow.SetWindowText(oldtekst);
596                            }
597                            else if (tekst == "error")
598                            {
599                                    m_Textwindow.GetWindowText(oldtekst);
600                                    oldtekst.Append("\r\n");
601                                    oldtekst.Append("error tekst");
602                                    oldtekst.Append(tekst);
603                                    m_Textwindow.SetWindowText(oldtekst);
604                            }
605                            else if (plus = true)
606                            {
607                                    if (command = "cmti")
608                                    {
609                                            CString smscount, oldteskst;
610                                            int pos = tekst.Find(',',0);
611                                            if (pos != -1)
612                                            {
613                                                    smscount = tekst.Right( tekst.GetLength() - pos -1);
614                                                    smscount.Remove(':');
615                                                    ResetSms++;
616                                            }
617                                                    ////////////////Read sms ting//////////////////
618                                                    std::vector<unsigned char> data;
619                                                    data.push_back('a');
620                                                    data.push_back('t');
621                                                    data.push_back('+');
622                                                    data.push_back('c');
623                                                    data.push_back('m');
624                                                    data.push_back('g');
625                                                    data.push_back('r');
626                                                    data.push_back('=');
627                                                    
628                                                    for (int i=0; i< smscount.GetLength(); i++)
629                                                    {
630                                                            data.push_back(smscount[i]);
631                                                    }
632                                                    for (int i=0; i<data.size();i++)
633                                                    {
634                                                    oldteskst.AppendChar(data[i]);
635                                                    }
636                                                    m_Textwindow.SetWindowText(oldteskst);
637                                                    writeFrame(data);
638                                                    Sleep(200);
639                                                    ReadSms();
640    
641                                    }
642                                    else if(command = "wind")
643                                    {
644                                            m_Textwindow.GetWindowText(oldtekst);
645                                            oldtekst.Append("\r\n");
646                                            oldtekst.Append("Først lidt tekst på næste linie\r\n");
647                                            oldtekst.Append(tekst);
648                                            m_Textwindow.SetWindowText(oldtekst);
649                                    }
650                            }
651                            if(ResetSms == 50)
652                            {
653                                    DeleteSms();
654                            }
655    
656                    }
657                    if (continueThread = 1)
658                    {
659                            
660                            ////////////////DBRead stuff//////////////////
661                            CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID;
662                            std::vector<Commands> data;
663                            data = DBRead();
664                            for (int i=0; i<data.size(); i++)
665                            {
666                                    testdata.Append(data[i].IDnr);
667                                    testdata.Append(":");
668                                    testdata.Append(data[i].CommandID);
669                                    testdata.Append(":");
670                                    testdata.Append(data[i].InstallationsID);
671                                    testdata.Append(":");
672                            }
673                            sIDnr = Splitter(testdata);
674                            sCommandID = Splitter(testdata);
675                            sInstallationsID = Splitter(testdata);
676    
677                            testprint.Append("sIDnr på næste linie\r\n");
678                            testprint.Append(sIDnr);
679                            testprint.Append("\r\n");
680                            testprint.Append("sCommandID på næste linie\r\n");
681                            testprint.Append(sCommandID);
682                            testprint.Append("\r\n");
683                            testprint.Append("sInstallationsID på næste linie\r\n");
684                            testprint.Append(sInstallationsID);
685                            m_Textwindow.SetWindowText(testprint);
686                            
687                            
688                            if (sIDnr.GetLength() > 0)
689                            {
690                                    DBReadData(sIDnr,sCommandID,sInstallationsID);
691                            }
692                            Sleep(500);
693                            ////////////////DBRead stuff//////////////////  
694                    }
695    }
696    void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID)
697    {
698            CString TlfNr;
699                    
700            //kald dbreadphone som du kalder dbread
701            std::vector<Installation> inst;
702            inst = DBReadPhone(InstallationsID);
703            for (int i=0; i<inst.size(); i++)
704            {
705                    CString dataen = inst[i].InstPhoneNr;
706                    TlfNr.Append(dataen);
707            }
708            vector<unsigned char> tlfnr;
709            for (int i=0; i<TlfNr.GetLength(); i++)
710            {
711                    tlfnr.push_back(TlfNr[i]);
712            }
713    
714            SendSmsHead(tlfnr);
715            Sleep(500);
716            //35:29860132:75 <--- demo pakke;
717            vector<unsigned char> smsdata;
718            smsdata.push_back('3');
719            smsdata.push_back('5');
720            smsdata.push_back(':');
721            smsdata.push_back('2');
722            smsdata.push_back('9');
723            smsdata.push_back('8');
724            smsdata.push_back('6');
725            smsdata.push_back('0');
726            smsdata.push_back('1');
727            smsdata.push_back('3');
728            smsdata.push_back('2');
729            smsdata.push_back(':');
730            smsdata.push_back('7');
731            smsdata.push_back('5');
732    
733            SendSmsData(smsdata);
734            Sleep(250);
735    
736            CString SQL;
737    
738            SQL.Format("update command set executed=now() where id=%s",IDnr);
739            db.ExecuteSQL(SQL);
740            Sleep(150);
741    }
742    vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
743    {
744            vector<Installation> buffer;
745    
746            CString SQL, phonenr;
747            SQL.Format("select installationphonenr from installation WHERE id = %s",sInstallationsID);
748            CRecordset rs(&db);
749            rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
750            if (rs.GetRecordCount()>0)
751            {
752                    rs.MoveFirst();
753                    while(!rs.IsEOF())
754                    {
755                    Installation Myinst;
756                    rs.GetFieldValue((short)0,phonenr);
757    
758                    Myinst.InstPhoneNr.Append(phonenr);
759    
760                    buffer.push_back(Myinst);
761                    rs.MoveNext();
762                    }
763            }
764            rs.Close();
765            return buffer;
766  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20