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

Annotation of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (hide annotations) (download)
Thu Nov 29 16:16:34 2007 UTC (16 years, 5 months ago) by kevin
File size: 11854 byte(s)
updated FlisServer software - now able to read/write sms, Read from the DB. next job data handling.
1 kevin 70 // FlisServerDlg.cpp : implementation file
2     //
3    
4     #include "stdafx.h"
5     #include "FlisServer.h"
6     #include "FlisServerDlg.h"
7     #include ".\flisserverdlg.h"
8     #include <vector>
9    
10     #ifdef _DEBUG
11     #define new DEBUG_NEW
12     #endif
13    
14    
15     // CAboutDlg dialog used for App About
16    
17     class CAboutDlg : public CDialog
18     {
19     public:
20     CAboutDlg();
21    
22     // Dialog Data
23     enum { IDD = IDD_ABOUTBOX };
24    
25     protected:
26     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
27    
28     // Implementation
29     protected:
30     DECLARE_MESSAGE_MAP()
31     };
32    
33     CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
34     {
35     }
36    
37     void CAboutDlg::DoDataExchange(CDataExchange* pDX)
38     {
39     CDialog::DoDataExchange(pDX);
40     }
41    
42     BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
43     END_MESSAGE_MAP()
44    
45    
46     // CFlisServerDlg dialog
47    
48    
49    
50     CFlisServerDlg::CFlisServerDlg(CWnd* pParent /*=NULL*/)
51     : CDialog(CFlisServerDlg::IDD, pParent)
52     {
53     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
54     }
55    
56     void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)
57     {
58     CDialog::DoDataExchange(pDX);
59     DDX_Control(pDX, IDC_Textwindow, m_Textwindow);
60     }
61    
62     BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialog)
63     ON_WM_SYSCOMMAND()
64     ON_WM_PAINT()
65     ON_WM_QUERYDRAGICON()
66     //}}AFX_MSG_MAP
67     ON_BN_CLICKED(IDC_test, OnBnClickedtest)
68 kevin 103 ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
69 kevin 70 END_MESSAGE_MAP()
70    
71    
72     // CFlisServerDlg message handlers
73    
74     BOOL CFlisServerDlg::OnInitDialog()
75     {
76     CDialog::OnInitDialog();
77    
78     // Add "About..." menu item to system menu.
79    
80     // IDM_ABOUTBOX must be in the system command range.
81     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
82     ASSERT(IDM_ABOUTBOX < 0xF000);
83    
84     CMenu* pSysMenu = GetSystemMenu(FALSE);
85     if (pSysMenu != NULL)
86     {
87     CString strAboutMenu;
88     strAboutMenu.LoadString(IDS_ABOUTBOX);
89     if (!strAboutMenu.IsEmpty())
90     {
91     pSysMenu->AppendMenu(MF_SEPARATOR);
92     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
93     }
94     }
95    
96     // Set the icon for this dialog. The framework does this automatically
97     // when the application's main window is not a dialog
98     SetIcon(m_hIcon, TRUE); // Set big icon
99     SetIcon(m_hIcon, FALSE); // Set small icon
100    
101     // TODO: Add extra initialization here
102     StartSerial();
103 kevin 81 //SetPin();
104     DBConnect();
105 kevin 70
106     return TRUE; // return TRUE unless you set the focus to a control
107     }
108    
109     void CFlisServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
110     {
111     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
112     {
113     CAboutDlg dlgAbout;
114     dlgAbout.DoModal();
115     }
116     else
117     {
118     CDialog::OnSysCommand(nID, lParam);
119     }
120     }
121    
122     // If you add a minimize button to your dialog, you will need the code below
123     // to draw the icon. For MFC applications using the document/view model,
124     // this is automatically done for you by the framework.
125    
126     void CFlisServerDlg::OnPaint()
127     {
128     if (IsIconic())
129     {
130     CPaintDC dc(this); // device context for painting
131    
132     SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
133    
134     // Center icon in client rectangle
135     int cxIcon = GetSystemMetrics(SM_CXICON);
136     int cyIcon = GetSystemMetrics(SM_CYICON);
137     CRect rect;
138     GetClientRect(&rect);
139     int x = (rect.Width() - cxIcon + 1) / 2;
140     int y = (rect.Height() - cyIcon + 1) / 2;
141    
142     // Draw the icon
143     dc.DrawIcon(x, y, m_hIcon);
144     }
145     else
146     {
147     CDialog::OnPaint();
148     }
149     }
150    
151     // The system calls this function to obtain the cursor to display while the user drags
152     // the minimized window.
153     HCURSOR CFlisServerDlg::OnQueryDragIcon()
154     {
155     return static_cast<HCURSOR>(m_hIcon);
156     }
157     int CFlisServerDlg::StartSerial(void)
158     {
159     int Baud;
160     CString SerialPort = "COM3";
161    
162     Baud = 1200;
163     if( Serial.isOpen() ){
164     Serial.close();
165     Serial.open( SerialPort, Baud );
166     }
167     else {
168     Serial.open( SerialPort, Baud );
169     }
170    
171    
172     return 0;
173     }
174     std::vector<unsigned char> CFlisServerDlg::readFrame()
175     {
176     std::vector<unsigned char> buf;
177     while(Serial.getComstat().cbInQue > 0)
178     {
179     unsigned char data = Serial.readByte();
180    
181     buf.push_back(data);
182     }
183     return buf;
184     }
185     void CFlisServerDlg::OnBnClickedtest()
186     {
187     // TODO: Add your control notification handler code here
188 kevin 103 /*
189     ////////////////Read sms ting//////////////////
190 kevin 81 CString tekst;
191 kevin 70 std::vector<unsigned char> data;
192     data.push_back('a');
193     data.push_back('t');
194 kevin 81 data.push_back('+');
195     data.push_back('c');
196     data.push_back('m');
197     data.push_back('g');
198     data.push_back('r');
199     data.push_back('=');
200     data.push_back('1');
201 kevin 70
202     writeFrame(data);
203 kevin 81 ReadSms();
204 kevin 103 ////////////////Read sms ting//////////////////
205     */
206     /*
207     ////////////////DBRead stuff//////////////////
208     CString testdata;
209     std::vector<Commands> data;
210     data = DBRead();
211     for (int i=0; i<data.size(); i++)
212     {
213     testdata.Append(data[i].IDnr);
214     testdata.Append(data[i].CommandID);
215     testdata.Append(data[i].InstallationsID);
216     }
217     m_Textwindow.SetWindowText(testdata);
218     ////////////////DBRead stuff//////////////////
219     */
220     /*
221     ///////////////Send sms stuff/////////////////
222     vector<unsigned char> tlfnr;
223     vector<unsigned char> smsdata;
224     tlfnr.push_back('2');
225     tlfnr.push_back('9');
226     tlfnr.push_back('7');
227     tlfnr.push_back('2');
228     tlfnr.push_back('2');
229     tlfnr.push_back('6');
230     tlfnr.push_back('0');
231     tlfnr.push_back('3');
232    
233     SendSmsHead(tlfnr);
234    
235     //35:29860132:75 <--- demo pakke;
236     smsdata.push_back('3');
237     smsdata.push_back('5');
238     smsdata.push_back(':');
239     smsdata.push_back('2');
240     smsdata.push_back('9');
241     smsdata.push_back('8');
242     smsdata.push_back('6');
243     smsdata.push_back('0');
244     smsdata.push_back('1');
245     smsdata.push_back('3');
246     smsdata.push_back('2');
247     smsdata.push_back(':');
248     smsdata.push_back('7');
249     smsdata.push_back('5');
250    
251     SendSmsData(smsdata);
252     Sleep(2000);
253     if(Serial.getComstat().cbInQue > 0)
254     {
255     CString tekst;
256     std::vector<unsigned char> answer = readFrame();
257     Sleep(50);
258     char array1[25];
259     int i;
260     for (int i=0; i<answer.size(); i++)
261     {
262     array1[i] = answer[i];
263     }
264    
265     for (int i=0; i<answer.size(); i++)
266     {
267     if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
268     {
269     tekst.AppendChar(array1[i]);
270     }
271     }
272     m_Textwindow.SetWindowText(tekst);
273     }
274     ///////////////Send sms stuff/////////////////
275     */
276 kevin 70 }
277     void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
278     {
279     for (int i=0; i<data.size(); i++)
280     {
281     Serial.writeByte( data[i] );
282     Sleep(5);
283     }
284     Serial.writeByte(0x0D);
285     Sleep(100);
286    
287     }
288     int CFlisServerDlg::SetPin(void)
289     {
290 kevin 81 CString tekst;
291 kevin 70 std::vector<unsigned char> data;
292     data.push_back('a');
293     data.push_back('t');
294     data.push_back('+');
295     data.push_back('c');
296     data.push_back('p');
297     data.push_back('i');
298     data.push_back('n');
299     data.push_back('=');
300     data.push_back('2');
301     data.push_back('5');
302     data.push_back('9');
303     data.push_back('5');
304    
305     writeFrame(data);
306     Sleep(750);
307     if(Serial.getComstat().cbInQue > 0)
308     {
309     std::vector<unsigned char> answer = readFrame();
310     Sleep(50);
311     char array1[25];
312     int i;
313     for (int i=0; i<answer.size(); i++)
314     {
315     array1[i] = answer[i];
316     }
317    
318     for (int i=0; i<answer.size(); i++)
319     {
320     if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
321     {
322     tekst.AppendChar(array1[i]);
323     }
324     }
325 kevin 81 m_Textwindow.SetWindowText(tekst);
326 kevin 70 }
327     return 0;
328     }
329     void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
330 kevin 103 {
331    
332 kevin 70 for (int i=0; i<data.size(); i++)
333     {
334     Serial.writeByte( data[i] );
335     Sleep(5);
336     }
337     Serial.writeByte(0x1A);
338 kevin 103 Sleep(500);
339 kevin 70
340     }
341     void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data)
342     {
343 kevin 103 vector<unsigned char> atcommand;
344     atcommand.push_back('a');
345     atcommand.push_back('t');
346     atcommand.push_back('+');
347     atcommand.push_back('c');
348     atcommand.push_back('m');
349     atcommand.push_back('g');
350     atcommand.push_back('s');
351     atcommand.push_back('=');
352     atcommand.push_back('"');
353     int s = (atcommand.size() -1 );
354    
355     for (int i=0; i<(atcommand.size()); i++)
356     {
357     Serial.writeByte( atcommand[i] );
358     Sleep(5);
359     }
360    
361 kevin 70 for (int i=0; i<data.size(); i++)
362     {
363     Serial.writeByte( data[i] );
364     Sleep(5);
365     }
366 kevin 103 Serial.writeByte(atcommand[s]);
367 kevin 70 Serial.writeByte(0x0D);
368 kevin 103 Sleep(250);
369 kevin 70 }
370 kevin 81 void CFlisServerDlg::DBConnect()
371 kevin 70 {
372     CString dsn;
373 kevin 103 dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer");
374     db.OpenEx(dsn, CDatabase::noOdbcDialog);
375 kevin 81 }
376 kevin 103 vector<Commands> CFlisServerDlg::DBRead(void)
377 kevin 81 {
378 kevin 103 vector<Commands> buffer;
379 kevin 81
380 kevin 103 CString SQL, IDnr, CommandID, InstallationsID;
381     SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;";
382     CRecordset rs(&db);
383     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
384     if (rs.GetRecordCount()>0)
385     {
386     rs.MoveFirst();
387     while(!rs.IsEOF())
388     {
389     Commands Mycom;
390     rs.GetFieldValue((short)0, IDnr);
391     rs.GetFieldValue(3, CommandID);
392     rs.GetFieldValue(4, InstallationsID);
393    
394     Mycom.IDnr = IDnr;
395     Mycom.CommandID = CommandID;
396     Mycom.InstallationsID = InstallationsID;
397    
398     buffer.push_back(Mycom);
399     rs.MoveNext();
400     }
401     }
402     rs.Close();
403     return buffer;
404 kevin 81 }
405     void CFlisServerDlg::ReadSms()
406     {
407     CString tekst, oldtekst;
408     Sleep(950);
409     if(Serial.getComstat().cbInQue > 0)
410     {
411     std::vector<unsigned char> answer = readFrame();
412     Sleep(50);
413     char array1[250];
414     int i;
415     for (int i=0; i<answer.size(); i++)
416     {
417     array1[i] = answer[i];
418     }
419    
420     for (int i=0; i<answer.size(); i++)
421     {
422     if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
423     {
424     tekst.AppendChar(array1[i]);
425     }
426     }
427    
428     m_Textwindow.GetWindowText(oldtekst);
429     oldtekst.Append("\r\n");
430     oldtekst.Append(tekst);
431     m_Textwindow.SetWindowText(oldtekst);
432     SmsSplit(tekst);
433     }
434     }
435     void CFlisServerDlg::SmsSplit(CString data)
436     {
437     CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
438     char CharData[150];
439     strcpy(CharData,data);
440    
441     int s=22;
442     for (int i=0; i<=7; i++)
443     {
444     TlfNr.AppendChar(CharData[s]);
445     s++;
446     }
447    
448     for (int s=55; s<=(data.GetLength()-3); s++)
449     {
450     FyrData.AppendChar(CharData[s]);
451     }
452     FyrData.Append(":");
453    
454     SmsCount = Splitter(FyrData);
455     Temper = Splitter(FyrData);
456     Flamme = Splitter(FyrData);
457     Flis = Splitter(FyrData);
458     FremFejl = Splitter(FyrData);
459     PowerFail = Splitter(FyrData);
460     ///////////////////////////////////////////////////////////////////////////////////////////////////////
461     ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
462     m_Textwindow.GetWindowText(oldtekst);
463     oldtekst.Append("\r\n");
464     oldtekst.Append("På næste linie kommer SmsCount. \r\n");
465     oldtekst.Append(SmsCount);
466     oldtekst.Append("\r\n");
467     oldtekst.Append("På næste linie kommer Temper. \r\n");
468     oldtekst.Append(Temper);
469     oldtekst.Append("\r\n");
470     oldtekst.Append("På næste linie kommer Flamme. \r\n");
471     oldtekst.Append(Flamme);
472     oldtekst.Append("\r\n");
473     oldtekst.Append("På næste linie kommer Flis. \r\n");
474     oldtekst.Append(Flis);
475     oldtekst.Append("\r\n");
476     oldtekst.Append("På næste linie kommer FremFejl. \r\n");
477     oldtekst.Append(FremFejl);
478     oldtekst.Append("\r\n");
479     oldtekst.Append("På næste linie kommer PowerFail. \r\n");
480     oldtekst.Append(PowerFail);
481     m_Textwindow.SetWindowText(oldtekst);
482     ///////////////////////////////////////////////////////////////////////////////////////////////////////
483     ///////////////////// Her skal sendes data til databasen //////////////////////////////////////////////
484     }
485     CString CFlisServerDlg::Splitter(CString& fyrdata)
486     {
487     CString Output;
488    
489     int pos = fyrdata.Find(':',0);
490     if (pos != -1)
491     {
492     Output = fyrdata.Left(pos);
493     fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
494     }
495     return Output;
496 kevin 103 }
497     void CFlisServerDlg::OnBnClickedClose()
498     {
499     // TODO: Add your control notification handler code here
500    
501     if( Serial.isOpen() )
502     {
503     Serial.close();
504     }
505    
506     if(db.IsOpen())
507     {
508     db.Close();
509     }
510     OnOK();
511     }

  ViewVC Help
Powered by ViewVC 1.1.20