--- trunk/FlisServer/FlisServerDlg.cpp 2007/11/28 16:24:04 81 +++ trunk/FlisServer/FlisServerDlg.cpp 2007/12/01 14:18:33 113 @@ -4,7 +4,6 @@ #include "stdafx.h" #include "FlisServer.h" #include "FlisServerDlg.h" -#include ".\flisserverdlg.h" #include #ifdef _DEBUG @@ -51,7 +50,6 @@ : CDialog(CFlisServerDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); - db = 0; } void CFlisServerDlg::DoDataExchange(CDataExchange* pDX) @@ -64,10 +62,11 @@ ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() + ON_MESSAGE(UWM_MYMESSAGE, OnShowString) //}}AFX_MSG_MAP - ON_BN_CLICKED(IDOK, OnBnClickedOk) - ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel) ON_BN_CLICKED(IDC_test, OnBnClickedtest) + ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) + ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) END_MESSAGE_MAP() @@ -102,8 +101,16 @@ // TODO: Add extra initialization here StartSerial(); - //SetPin(); DBConnect(); + continueThread = 1; + ResetSms = 0; + /* +////////////Start Tråd//////////////////////// + MyThread *t = new MyThread(AfxGetMainWnd()); + t->start(); +////////////Start Tråd//////////////////////// +*/ + AfxBeginThread(threadWrapper,AfxGetMainWnd()); return TRUE; // return TRUE unless you set the focus to a control } @@ -184,55 +191,46 @@ } return buf; } -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 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'); - writeFrame(data); - ReadSms(); +/* +///////////////Send sms stuff///////////////// + vector tlfnr; + vector smsdata; + tlfnr.push_back('2'); + tlfnr.push_back('9'); + tlfnr.push_back('7'); + tlfnr.push_back('2'); + tlfnr.push_back('2'); + tlfnr.push_back('6'); + tlfnr.push_back('0'); + tlfnr.push_back('3'); + + SendSmsHead(tlfnr); + + //35:29860132:75 <--- demo pakke; + vector smsdata; + smsdata.push_back('3'); + smsdata.push_back('5'); + smsdata.push_back(':'); + smsdata.push_back('2'); + smsdata.push_back('9'); + smsdata.push_back('8'); + smsdata.push_back('6'); + smsdata.push_back('0'); + smsdata.push_back('1'); + smsdata.push_back('3'); + smsdata.push_back('2'); + smsdata.push_back(':'); + smsdata.push_back('7'); + smsdata.push_back('5'); + + SendSmsData(smsdata); +///////////////Send sms stuff///////////////// +*/ + } void CFlisServerDlg::writeFrame(std::vector data) { @@ -245,7 +243,7 @@ Sleep(100); } -int CFlisServerDlg::SetPin(void) +void CFlisServerDlg::SetPin() { CString tekst; std::vector data; @@ -263,7 +261,7 @@ data.push_back('5'); writeFrame(data); - Sleep(750); +/* Sleep(750); if(Serial.getComstat().cbInQue > 0) { std::vector answer = readFrame(); @@ -284,42 +282,105 @@ } m_Textwindow.SetWindowText(tekst); } -return 0; +*/ } void CFlisServerDlg::SendSmsData(std::vector data) -{ +{ + for (int i=0; i 0) + { + CString tekst; + std::vector answer = readFrame(); + Sleep(50); + char array1[25]; + int i; + for (int i=0; i data) { + vector atcommand; + atcommand.push_back('a'); + atcommand.push_back('t'); + atcommand.push_back('+'); + atcommand.push_back('c'); + atcommand.push_back('m'); + atcommand.push_back('g'); + atcommand.push_back('s'); + atcommand.push_back('='); + atcommand.push_back('"'); + int s = (atcommand.size() -1 ); + + for (int i=0; i<(atcommand.size()); i++) + { + Serial.writeByte( atcommand[i] ); + Sleep(5); + } + for (int i=0; iOpenEx(dsn, CDatabase::noOdbcDialog); - -} -void CFlisServerDlg::DBRead() -{ + dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); + //dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server + db.OpenEx(dsn, CDatabase::noOdbcDialog); +} +vector CFlisServerDlg::DBRead(void) +{ + vector buffer; + + CString SQL, IDnr, CommandID, InstallationsID; + SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;"; + CRecordset rs(&db); + rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); + if (rs.GetRecordCount()>0) + { + rs.MoveFirst(); + while(!rs.IsEOF()) + { + Commands Mycom; + rs.GetFieldValue((short)0, IDnr); + rs.GetFieldValue(3, CommandID); + rs.GetFieldValue(4, InstallationsID); + + Mycom.IDnr = IDnr; + Mycom.CommandID = CommandID; + Mycom.InstallationsID = InstallationsID; + buffer.push_back(Mycom); + rs.MoveNext(); + } + } + rs.Close(); + return buffer; } void CFlisServerDlg::ReadSms() { @@ -357,14 +418,14 @@ char CharData[150]; strcpy(CharData,data); - int s=22; + int s=24; for (int i=0; i<=7; i++) { TlfNr.AppendChar(CharData[s]); s++; } - for (int s=55; s<=(data.GetLength()-3); s++) + for (int s=57; s<=(data.GetLength()-3); s++) { FyrData.AppendChar(CharData[s]); } @@ -397,6 +458,9 @@ oldtekst.Append("\r\n"); oldtekst.Append("På næste linie kommer PowerFail. \r\n"); oldtekst.Append(PowerFail); + oldtekst.Append("\r\n"); + oldtekst.Append("På næste linie kommer TlfNR. \r\n"); + oldtekst.Append(TlfNr); m_Textwindow.SetWindowText(oldtekst); /////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// @@ -412,4 +476,291 @@ fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1); } return Output; +} +void CFlisServerDlg::OnBnClickedClose() +{ + // TODO: Add your control notification handler code here + continueThread = 0; + DeleteSms(); //<--- crasher, men gider ikke bruge flere af mine penge på at teste det med sms'er, hele close laver crash, bare ikke når jeg stepper. :S + + Sleep(500); + if( Serial.isOpen() ) + { + Serial.close(); + } + + if(db.IsOpen()) + { + db.Close(); + } + + OnOK(); + +} + +void CFlisServerDlg::OnBnClickedGsmpin() +{ + // TODO: Add your control notification handler code here + SetPin(); + //Sleep(5000); +} +void CFlisServerDlg::DeleteSms() +{ + vector atcommand; + atcommand.push_back('a'); + atcommand.push_back('t'); + atcommand.push_back('+'); + atcommand.push_back('c'); + atcommand.push_back('m'); + atcommand.push_back('g'); + atcommand.push_back('d'); + atcommand.push_back('='); + atcommand.push_back('1'); + atcommand.push_back(','); + atcommand.push_back('3'); + + writeFrame(atcommand); + Sleep(500); +} +LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam) +{ + CString *s = (CString*) lParam; + GetDlgItem(IDC_Textwindow)->SetWindowText(*s); + + delete s; + return 0; + +}UINT threadWrapper(LPVOID thread) +{ + CFlisServerDlg *t = (CFlisServerDlg*) thread; + t->runthread(); + return 0; +} + +void CFlisServerDlg::startthread() +{ + AfxBeginThread(threadWrapper, (LPVOID) this); +} + +void CFlisServerDlg::runthread() +{ + while (continueThread != 0) + { + Reader(); + } +} +void CFlisServerDlg::Reader() +{ + if(Serial.getComstat().cbInQue > 0) + { + Sleep(250); + std::vector answer = readFrame(); + Sleep(500); + CString tekst, oldtekst; + int lol; + char array1[250]; + int i; + for (int i=0; i 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('='); + + for (int i=0; i< smscount.GetLength(); i++) + { + data.push_back(smscount[i]); + } + for (int i=0; i data; + data = DBRead(); + for (int i=0; i 0) + { + DBReadData(sIDnr,sCommandID,sInstallationsID); + } + Sleep(500); + ////////////////DBRead stuff////////////////// + } +} +void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID) +{ + CString TlfNr; + + //kald dbreadphone som du kalder dbread + std::vector inst; + inst = DBReadPhone(InstallationsID); + for (int i=0; i tlfnr; + for (int i=0; i smsdata; + smsdata.push_back('3'); + smsdata.push_back('5'); + smsdata.push_back(':'); + smsdata.push_back('2'); + smsdata.push_back('9'); + smsdata.push_back('8'); + smsdata.push_back('6'); + smsdata.push_back('0'); + smsdata.push_back('1'); + smsdata.push_back('3'); + smsdata.push_back('2'); + smsdata.push_back(':'); + smsdata.push_back('7'); + smsdata.push_back('5'); + + SendSmsData(smsdata); + Sleep(250); + + CString SQL; + + SQL.Format("update command set executed=now() where id=%s",IDnr); + db.ExecuteSQL(SQL); + Sleep(150); +} +vector CFlisServerDlg::DBReadPhone(CString sInstallationsID) +{ + vector buffer; + + CString SQL, phonenr; + SQL.Format("select installationphonenr from installation WHERE id = %s",sInstallationsID); + CRecordset rs(&db); + rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); + if (rs.GetRecordCount()>0) + { + rs.MoveFirst(); + while(!rs.IsEOF()) + { + Installation Myinst; + rs.GetFieldValue((short)0,phonenr); + + Myinst.InstPhoneNr.Append(phonenr); + + buffer.push_back(Myinst); + rs.MoveNext(); + } + } + rs.Close(); + return buffer; } \ No newline at end of file