/[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 130 by kevin, Mon Dec 3 09:22:26 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    #include ".\flisserverdlg.h"
9    
10  #ifdef _DEBUG  #ifdef _DEBUG
11  #define new DEBUG_NEW  #define new DEBUG_NEW
# Line 51  CFlisServerDlg::CFlisServerDlg(CWnd* pPa Line 51  CFlisServerDlg::CFlisServerDlg(CWnd* pPa
51          : CDialog(CFlisServerDlg::IDD, pParent)          : CDialog(CFlisServerDlg::IDD, pParent)
52  {  {
53          m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);          m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
         db = 0;  
54  }  }
55    
56  void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)  void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)
# Line 64  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()
66            ON_MESSAGE(UWM_MYMESSAGE, OnShowString)
67          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
68          ON_BN_CLICKED(IDOK, OnBnClickedOk)          ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
69          ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)          ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
70          ON_BN_CLICKED(IDC_test, OnBnClickedtest)          ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
71  END_MESSAGE_MAP()  END_MESSAGE_MAP()
72    
73    
# Line 102  BOOL CFlisServerDlg::OnInitDialog() Line 102  BOOL CFlisServerDlg::OnInitDialog()
102    
103          // TODO: Add extra initialization here          // TODO: Add extra initialization here
104          StartSerial();          StartSerial();
         //SetPin();  
105          DBConnect();          DBConnect();
106            ResetSms = 0;
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 184  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();  
         }  
   
         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;  
         }  
 }  
   
 void CFlisServerDlg::OnBnClickedtest()  
 {  
         // 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');  
187    
         writeFrame(data);  
         ReadSms();  
 }  
188  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)  void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
189  {  {
190          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
# Line 245  void CFlisServerDlg::writeFrame(std::vec Line 196  void CFlisServerDlg::writeFrame(std::vec
196          Sleep(100);          Sleep(100);
197    
198  }  }
199  int CFlisServerDlg::SetPin(void)  void CFlisServerDlg::SetPin()
200  {  {
201          CString tekst;          CString tekst;
202          std::vector<unsigned char> data;          std::vector<unsigned char> data;
# Line 263  int CFlisServerDlg::SetPin(void) Line 214  int CFlisServerDlg::SetPin(void)
214          data.push_back('5');          data.push_back('5');
215    
216          writeFrame(data);          writeFrame(data);
217          Sleep(750);  /*      Sleep(750);
218          if(Serial.getComstat().cbInQue > 0)          if(Serial.getComstat().cbInQue > 0)
219          {          {
220                  std::vector<unsigned char> answer = readFrame();                  std::vector<unsigned char> answer = readFrame();
# Line 284  int CFlisServerDlg::SetPin(void) Line 235  int CFlisServerDlg::SetPin(void)
235                          }                          }
236                          m_Textwindow.SetWindowText(tekst);                          m_Textwindow.SetWindowText(tekst);
237          }          }
238  return 0;  */
239  }  }
240  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
241  {  {      
242            
243          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
244          {          {
245                  Serial.writeByte( data[i] );                  Serial.writeByte( data[i] );
246                  Sleep(5);                  Sleep(5);
247          }          }
248          Serial.writeByte(0x1A);          Serial.writeByte(0x1A);
249          Sleep(100);          Sleep(3000);
250            if(Serial.getComstat().cbInQue > 0)
251            {
252                    CString tekst;
253                    std::vector<unsigned char> answer = readFrame();
254                    Sleep(50);
255                            char array1[150];
256                            int i;
257                            for (int i=0; i<answer.size(); i++)
258                            {
259                                    array1[i] = answer[i];
260                            }
261                    
262                            for (int i=0; i<answer.size(); i++)
263                            {
264                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
265                                    {
266                                            tekst.AppendChar(array1[i]);
267                                    }
268                            }
269                            m_Textwindow.SetWindowText(tekst);
270            }
271    
272  }  }
273  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)  void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)
274  {  {
275            vector<unsigned char> atcommand;
276            atcommand.push_back('a');
277            atcommand.push_back('t');
278            atcommand.push_back('+');
279            atcommand.push_back('c');
280            atcommand.push_back('m');
281            atcommand.push_back('g');
282            atcommand.push_back('s');
283            atcommand.push_back('=');
284            atcommand.push_back('"');
285            int s = (atcommand.size() -1 );
286            
287            for (int i=0; i<(atcommand.size()); i++)
288            {
289                    Serial.writeByte( atcommand[i] );
290                    Sleep(5);
291            }
292            
293          for (int i=0; i<data.size(); i++)          for (int i=0; i<data.size(); i++)
294          {          {
295                  Serial.writeByte( data[i] );                  Serial.writeByte( data[i] );
296                  Sleep(5);                  Sleep(5);
297          }          }
298            Serial.writeByte(atcommand[s]);
299          Serial.writeByte(0x0D);          Serial.writeByte(0x0D);
300          Sleep(100);          Sleep(250);
   
301  }  }
302  void CFlisServerDlg::DBConnect()  void CFlisServerDlg::DBConnect()
303  {  {
         db = new CDatabase();  
   
304          CString dsn;          CString dsn;
305          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");   //Torben
306          //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
307                    db.OpenEx(dsn, CDatabase::noOdbcDialog);
308  }  }
309  void CFlisServerDlg::DBRead()  vector<Commands> CFlisServerDlg::DBRead(void)
310  {  {
311            vector<Commands> buffer;
312    
313            CString SQL, IDnr, CommandID, InstallationsID;
314            SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;";
315            CRecordset rs(&db);
316            rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
317            if (rs.GetRecordCount()>0)
318            {
319                    rs.MoveFirst();
320                    while(!rs.IsEOF())
321                    {
322                            Commands Mycom;
323                            rs.GetFieldValue((short)0, IDnr);
324                            rs.GetFieldValue(3, CommandID);
325                            rs.GetFieldValue(4, InstallationsID);
326                            
327                            Mycom.IDnr = IDnr;
328                            Mycom.CommandID = CommandID;
329                            Mycom.InstallationsID = InstallationsID;
330    
331                            buffer.push_back(Mycom);
332                            rs.MoveNext();
333                    }
334            }
335            rs.Close();
336            return buffer;
337  }  }
338  void CFlisServerDlg::ReadSms()  void CFlisServerDlg::ReadSms()
339  {  {
# Line 357  void CFlisServerDlg::SmsSplit(CString da Line 371  void CFlisServerDlg::SmsSplit(CString da
371          char CharData[150];          char CharData[150];
372          strcpy(CharData,data);          strcpy(CharData,data);
373    
374          int s=22;          int s=24;
375          for (int i=0; i<=7; i++)          for (int i=0; i<=7; i++)
376          {          {
377                  TlfNr.AppendChar(CharData[s]);                  TlfNr.AppendChar(CharData[s]);
378                  s++;                  s++;
379          }          }
380    
381          for (int s=55; s<=(data.GetLength()-3); s++)          for (int s=57; s<=(data.GetLength()-3); s++)
382          {          {
383                  FyrData.AppendChar(CharData[s]);                  FyrData.AppendChar(CharData[s]);
384          }          }
# Line 377  void CFlisServerDlg::SmsSplit(CString da Line 391  void CFlisServerDlg::SmsSplit(CString da
391          FremFejl = Splitter(FyrData);          FremFejl = Splitter(FyrData);
392          PowerFail = Splitter(FyrData);          PowerFail = Splitter(FyrData);
393  ///////////////////////////////////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////////////////////////////////
394  ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  ///////////////////// Her skal sendes data til databasen ////NÅR HEDIN KAN SENDE SMS///////////////////
395                  m_Textwindow.GetWindowText(oldtekst);                  m_Textwindow.GetWindowText(oldtekst);
396                  oldtekst.Append("\r\n");                  oldtekst.Append("\r\n");
397                  oldtekst.Append("På næste linie kommer SmsCount. \r\n");                  oldtekst.Append("På næste linie kommer SmsCount. \r\n");
# Line 397  void CFlisServerDlg::SmsSplit(CString da Line 411  void CFlisServerDlg::SmsSplit(CString da
411                  oldtekst.Append("\r\n");                  oldtekst.Append("\r\n");
412                  oldtekst.Append("På næste linie kommer PowerFail. \r\n");                  oldtekst.Append("På næste linie kommer PowerFail. \r\n");
413                  oldtekst.Append(PowerFail);                  oldtekst.Append(PowerFail);
414                    oldtekst.Append("\r\n");
415                    oldtekst.Append("På næste linie kommer TlfNR. \r\n");
416                    oldtekst.Append(TlfNr);
417                  m_Textwindow.SetWindowText(oldtekst);                  m_Textwindow.SetWindowText(oldtekst);
418  ///////////////////////////////////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////////////////////////////////
419  ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////  ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
# Line 412  CString CFlisServerDlg::Splitter(CString Line 429  CString CFlisServerDlg::Splitter(CString
429                          fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);                          fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
430                  }                  }
431          return Output;          return Output;
 }  
432    }
433    void CFlisServerDlg::OnBnClickedClose()
434    {
435            // TODO: Add your control notification handler code here
436            continueThread = 0;
437            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
438    
439            Sleep(500);
440            if( Serial.isOpen() )
441            {
442                    Serial.close();
443            }
444            
445            if(db.IsOpen())
446            {
447            db.Close();
448            }
449            
450            OnOK();
451    
452    }
453    
454    void CFlisServerDlg::OnBnClickedGsmpin()
455    {
456            // TODO: Add your control notification handler code here
457            SetPin();
458            Sleep(35000);
459            OnBnClickedStart();
460    }
461    void CFlisServerDlg::DeleteSms()
462    {
463            vector<unsigned char> atcommand;
464            atcommand.push_back('a');
465            atcommand.push_back('t');
466            atcommand.push_back('+');
467            atcommand.push_back('c');
468            atcommand.push_back('m');
469            atcommand.push_back('g');
470            atcommand.push_back('d');
471            atcommand.push_back('=');
472            atcommand.push_back('1');
473            atcommand.push_back(',');
474            atcommand.push_back('3');
475    
476            writeFrame(atcommand);
477            Sleep(500);
478    }
479    LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam)
480    {
481            CString *s = (CString*) lParam;
482            GetDlgItem(IDC_Textwindow)->SetWindowText(*s);
483    
484            delete s;
485            return 0;
486    
487    }UINT threadWrapper(LPVOID thread)
488    {
489            CFlisServerDlg *t = (CFlisServerDlg*) thread;
490            t->runthread();
491            return 0;
492    }
493    
494    void CFlisServerDlg::startthread()
495    {
496            AfxBeginThread(threadWrapper, (LPVOID) this);
497    }
498    
499    void CFlisServerDlg::runthread()
500    {
501            while (continueThread != 0)
502            {
503                    Reader();
504            }
505    }
506    void CFlisServerDlg::Reader()
507    {
508            if(Serial.getComstat().cbInQue > 0)
509                    {
510                            Sleep(250);
511                            std::vector<unsigned char> answer = readFrame();
512                            Sleep(500);
513                            CString tekst, oldtekst;
514                            int lol;
515                            char array1[250];
516                            int i;
517                            for (int i=0; i<answer.size(); i++)
518                            {
519                                    array1[i] = answer[i];
520                            }
521                    
522                            for (int i=0; i<answer.size(); i++)
523                            {
524                                    if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
525                                    {
526                                            tekst.AppendChar(array1[i]);
527                                    }
528                            }
529                            tekst.Append(":");
530    
531                            CString command;
532                            bool plus;
533                                    int pos = tekst.Find('+',0);
534                                    if (pos != -1)
535                                    {
536                                            plus = true;
537                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
538                                            pos = tekst.Find(':');
539                                            command = tekst.Left(pos);
540                                            tekst = tekst.Right( tekst.GetLength() - pos -1);
541                                    }
542                            
543                            
544                            if(tekst == "OK")
545                            {      
546                                    m_Textwindow.GetWindowText(oldtekst);
547                                    oldtekst.Append("\r\n");
548                                    oldtekst.Append("OK tekst modtaget");
549                                    m_Textwindow.SetWindowText(oldtekst);
550                            }
551                            else if (tekst == "error")
552                            {
553                                    m_Textwindow.GetWindowText(oldtekst);
554                                    oldtekst.Append("\r\n");
555                                    oldtekst.Append("error tekst");
556                                    oldtekst.Append(tekst);
557                                    m_Textwindow.SetWindowText(oldtekst);
558                            }
559                            else if (plus = true)
560                            {
561                                    if (command = "cmti")
562                                    {
563                                            CString smscount, oldteskst;
564                                            int pos = tekst.Find(',',0);
565                                            if (pos != -1)
566                                            {
567                                                    smscount = tekst.Right( tekst.GetLength() - pos -1);
568                                                    smscount.Remove(':');
569                                                    ResetSms++;
570                                            }
571                                                    ////////////////Read sms ting//////////////////
572                                                    std::vector<unsigned char> data;
573                                                    data.push_back('a');
574                                                    data.push_back('t');
575                                                    data.push_back('+');
576                                                    data.push_back('c');
577                                                    data.push_back('m');
578                                                    data.push_back('g');
579                                                    data.push_back('r');
580                                                    data.push_back('=');
581                                                    
582                                                    for (int i=0; i< smscount.GetLength(); i++)
583                                                    {
584                                                            data.push_back(smscount[i]);
585                                                    }
586                                                    for (int i=0; i<data.size();i++)
587                                                    {
588                                                    oldteskst.AppendChar(data[i]);
589                                                    }
590                                                    m_Textwindow.SetWindowText(oldteskst);
591                                                    writeFrame(data);
592                                                    Sleep(200);
593                                                    ReadSms();
594                                                    ResetSms++;
595    
596                                    }
597                                    else if(command = "wind")
598                                    {
599                                            m_Textwindow.GetWindowText(oldtekst);
600                                            oldtekst.Append("\r\n");
601                                            oldtekst.Append("Først lidt tekst på næste linie\r\n");
602                                            oldtekst.Append(tekst);
603                                            m_Textwindow.SetWindowText(oldtekst);
604                                    }
605                            }
606                            if(ResetSms == 50)
607                            {
608                                    DeleteSms();
609                            }
610    
611                    }
612                    if (continueThread = 1)
613                    {
614                            
615                            CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
616                            int commandtest = 0;
617                            int iAll = 1;
618                            std::vector<Commands> data;
619                            data = DBRead();
620                            for (int i=0; i<data.size(); i++)
621                            {
622                                    testdata.Append(data[i].IDnr);
623                                    testdata.Append(":");
624                                    testdata.Append(data[i].CommandID);
625                                    testdata.Append(":");
626                                    commandtest = atoi(data[i].CommandID);
627    
628                                    if(commandtest > 1)
629                                    {
630                                    testdata.Append(data[i].InstallationsID);
631                                    testdata.Append(":");
632                                    }
633                            }
634                            sIDnr = Splitter(testdata);
635                            sCommandID = Splitter(testdata);
636                            sInstallationsID = "0";
637                            if (commandtest > 1)
638                            {
639                            sInstallationsID = Splitter(testdata);
640                            iAll = 0;
641                            }
642    
643                            if (sIDnr.GetLength() > 0)
644                            {
645                                    DBReadData(sIDnr,sCommandID,sInstallationsID);
646                            }
647                            Sleep(500);
648                    }
649    }
650    void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID)
651    {
652            CString ServerTlfNr;
653            int i = 0;
654            ServerTlfNr = "29860132";
655                    
656            
657            std::vector<Installation> inst;
658            inst = DBReadPhone(InstallationsID);
659            while (i < inst.size())
660            {
661                    CString TlfNr, Imei, updaterate;
662                    TlfNr.Empty();
663                    Imei.Empty();
664                    updaterate.Empty();
665    
666                    CString dataen = inst[i].InstPhoneNr;
667                    TlfNr.Append(dataen);
668                    CString Imeidata = inst[i].Imei;
669                    Imei.Append(Imeidata);
670                    CString updaterat = inst[i].Updaterate;
671                    updaterate.Append(updaterat);
672                    i++;
673            
674            vector<unsigned char> tlfnr;
675            for (int i=0; i<TlfNr.GetLength(); i++)
676            {
677                    tlfnr.push_back(TlfNr[i]);
678            }
679            
680            int calcimei;
681            __int64 buf;
682    
683            buf = atof(Imei);
684            
685            calcimei = tversum(buf);
686            
687            Imei.Format("%d",calcimei);
688            
689            SendSmsHead(tlfnr);
690            Sleep(250);
691            
692            vector<unsigned char> smsdata;
693            
694            for (int i=0; i<Imei.GetLength(); i++)
695            {
696                    smsdata.push_back(Imei[i]);
697            }
698            smsdata.push_back(':');
699            for (int i=0; i<ServerTlfNr.GetLength(); i++)
700            {
701                    smsdata.push_back(ServerTlfNr[i]);
702            }
703            smsdata.push_back(':');
704            for (int i=0; i<updaterate.GetLength(); i++)
705            {
706                    smsdata.push_back(updaterate[i]);
707            }
708    
709            SendSmsData(smsdata);
710            Sleep(500);
711            
712            }
713                    CString SQL, Textwindow;
714                    SQL.Format("update command set executed=now() where id=%s",IDnr);
715                    db.ExecuteSQL(SQL);
716                    m_Textwindow.GetWindowText(Textwindow);
717                    Textwindow.Append("\r\n");
718                    Textwindow.Append("Command executed");
719                    m_Textwindow.SetWindowText(Textwindow);
720                    Sleep(150);
721    
722    }
723    vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
724    {
725            vector<Installation> buffer;
726    
727            CString SQL, phonenr, imei, updaterate;
728            int installernull;
729            Installation Myinst;
730            installernull = atoi(sInstallationsID);
731            if (installernull > 1)
732            {
733                    SQL.Format("select installationphonenr, imei, updaterate from installation WHERE id = %s", sInstallationsID);
734            }
735            else if(installernull < 2)
736            {
737                    SQL.Format("select installationphonenr, imei, updaterate from installation");
738            }
739            CRecordset rs(&db);
740            rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
741            if (rs.GetRecordCount()>0)
742            {
743                    rs.MoveFirst();
744                    while(!rs.IsEOF())
745                    {
746                    
747                            rs.GetFieldValue((short)0,phonenr);
748                            rs.GetFieldValue(1,imei);
749                            rs.GetFieldValue(2,updaterate);
750    
751                            Myinst.InstPhoneNr = phonenr;
752                            Myinst.Imei = imei;
753                            Myinst.Updaterate = updaterate;
754    
755                            buffer.push_back(Myinst);
756                            rs.MoveNext();
757                    }
758            }
759            rs.Close();
760            return buffer;
761    }
762    int CFlisServerDlg::tversum(__int64 input)
763    {
764            int sum = 0;
765            while (input > 0)
766            {
767                    sum += (input %10);
768                    input /= 10;
769            }
770            return sum;
771    }
772    void CFlisServerDlg::OnBnClickedStart()
773    {
774    // TODO: Add your control notification handler code here
775    
776            continueThread = 1;
777    
778            AfxBeginThread(threadWrapper,AfxGetMainWnd());
779    }

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

  ViewVC Help
Powered by ViewVC 1.1.20