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

Contents of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (show annotations) (download)
Fri Nov 30 16:03:46 2007 UTC (16 years, 5 months ago) by kevin
File size: 16028 byte(s)
updated Dagbog - kevin.doc and updated some of the server software
1 // FlisServerDlg.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "FlisServer.h"
6 #include "FlisServerDlg.h"
7 #include <vector>
8
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #endif
12
13
14 // CAboutDlg dialog used for App About
15
16 class CAboutDlg : public CDialog
17 {
18 public:
19 CAboutDlg();
20
21 // Dialog Data
22 enum { IDD = IDD_ABOUTBOX };
23
24 protected:
25 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
26
27 // Implementation
28 protected:
29 DECLARE_MESSAGE_MAP()
30 };
31
32 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
33 {
34 }
35
36 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
37 {
38 CDialog::DoDataExchange(pDX);
39 }
40
41 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
42 END_MESSAGE_MAP()
43
44
45 // CFlisServerDlg dialog
46
47
48
49 CFlisServerDlg::CFlisServerDlg(CWnd* pParent /*=NULL*/)
50 : CDialog(CFlisServerDlg::IDD, pParent)
51 {
52 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
53 }
54
55 void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)
56 {
57 CDialog::DoDataExchange(pDX);
58 DDX_Control(pDX, IDC_Textwindow, m_Textwindow);
59 }
60
61 BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialog)
62 ON_WM_SYSCOMMAND()
63 ON_WM_PAINT()
64 ON_WM_QUERYDRAGICON()
65 ON_MESSAGE(UWM_MYMESSAGE, OnShowString)
66 //}}AFX_MSG_MAP
67 ON_BN_CLICKED(IDC_test, OnBnClickedtest)
68 ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
69 ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
70 END_MESSAGE_MAP()
71
72
73 // CFlisServerDlg message handlers
74
75 BOOL CFlisServerDlg::OnInitDialog()
76 {
77 CDialog::OnInitDialog();
78
79 // Add "About..." menu item to system menu.
80
81 // IDM_ABOUTBOX must be in the system command range.
82 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
83 ASSERT(IDM_ABOUTBOX < 0xF000);
84
85 CMenu* pSysMenu = GetSystemMenu(FALSE);
86 if (pSysMenu != NULL)
87 {
88 CString strAboutMenu;
89 strAboutMenu.LoadString(IDS_ABOUTBOX);
90 if (!strAboutMenu.IsEmpty())
91 {
92 pSysMenu->AppendMenu(MF_SEPARATOR);
93 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
94 }
95 }
96
97 // Set the icon for this dialog. The framework does this automatically
98 // when the application's main window is not a dialog
99 SetIcon(m_hIcon, TRUE); // Set big icon
100 SetIcon(m_hIcon, FALSE); // Set small icon
101
102 // TODO: Add extra initialization here
103 StartSerial();
104 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
109 }
110
111 void CFlisServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
112 {
113 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
114 {
115 CAboutDlg dlgAbout;
116 dlgAbout.DoModal();
117 }
118 else
119 {
120 CDialog::OnSysCommand(nID, lParam);
121 }
122 }
123
124 // If you add a minimize button to your dialog, you will need the code below
125 // to draw the icon. For MFC applications using the document/view model,
126 // this is automatically done for you by the framework.
127
128 void CFlisServerDlg::OnPaint()
129 {
130 if (IsIconic())
131 {
132 CPaintDC dc(this); // device context for painting
133
134 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
135
136 // Center icon in client rectangle
137 int cxIcon = GetSystemMetrics(SM_CXICON);
138 int cyIcon = GetSystemMetrics(SM_CYICON);
139 CRect rect;
140 GetClientRect(&rect);
141 int x = (rect.Width() - cxIcon + 1) / 2;
142 int y = (rect.Height() - cyIcon + 1) / 2;
143
144 // Draw the icon
145 dc.DrawIcon(x, y, m_hIcon);
146 }
147 else
148 {
149 CDialog::OnPaint();
150 }
151 }
152
153 // The system calls this function to obtain the cursor to display while the user drags
154 // the minimized window.
155 HCURSOR CFlisServerDlg::OnQueryDragIcon()
156 {
157 return static_cast<HCURSOR>(m_hIcon);
158 }
159 int CFlisServerDlg::StartSerial(void)
160 {
161 int Baud;
162 CString SerialPort = "COM3";
163
164 Baud = 1200;
165 if( Serial.isOpen() ){
166 Serial.close();
167 Serial.open( SerialPort, Baud );
168 }
169 else {
170 Serial.open( SerialPort, Baud );
171 }
172
173
174 return 0;
175 }
176 std::vector<unsigned char> CFlisServerDlg::readFrame()
177 {
178 std::vector<unsigned char> buf;
179 while(Serial.getComstat().cbInQue > 0)
180 {
181 unsigned char data = Serial.readByte();
182
183 buf.push_back(data);
184 }
185 return buf;
186 }
187 void CFlisServerDlg::OnBnClickedtest()
188 {
189 // TODO: Add your control notification handler code here
190
191 /*
192 ////////////////DBRead stuff//////////////////
193 CString testdata;
194 std::vector<Commands> data;
195 data = DBRead();
196 for (int i=0; i<data.size(); i++)
197 {
198 testdata.Append(data[i].IDnr);
199 testdata.Append(data[i].CommandID);
200 testdata.Append(data[i].InstallationsID);
201 }
202 m_Textwindow.SetWindowText(testdata);
203 ////////////////DBRead stuff//////////////////
204 */
205 /*
206 ///////////////Send sms stuff/////////////////
207 vector<unsigned char> tlfnr;
208 vector<unsigned char> smsdata;
209 tlfnr.push_back('2');
210 tlfnr.push_back('9');
211 tlfnr.push_back('7');
212 tlfnr.push_back('2');
213 tlfnr.push_back('2');
214 tlfnr.push_back('6');
215 tlfnr.push_back('0');
216 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)
249 {
250 for (int i=0; i<data.size(); i++)
251 {
252 Serial.writeByte( data[i] );
253 Sleep(5);
254 }
255 Serial.writeByte(0x0D);
256 Sleep(100);
257
258 }
259 void CFlisServerDlg::SetPin()
260 {
261 CString tekst;
262 std::vector<unsigned char> data;
263 data.push_back('a');
264 data.push_back('t');
265 data.push_back('+');
266 data.push_back('c');
267 data.push_back('p');
268 data.push_back('i');
269 data.push_back('n');
270 data.push_back('=');
271 data.push_back('2');
272 data.push_back('5');
273 data.push_back('9');
274 data.push_back('5');
275
276 writeFrame(data);
277 Sleep(750);
278 if(Serial.getComstat().cbInQue > 0)
279 {
280 std::vector<unsigned char> answer = readFrame();
281 Sleep(50);
282 char array1[25];
283 int i;
284 for (int i=0; i<answer.size(); i++)
285 {
286 array1[i] = answer[i];
287 }
288
289 for (int i=0; i<answer.size(); i++)
290 {
291 if ((array1[i] != 0x0A) && (array1[i] != 0x0D))
292 {
293 tekst.AppendChar(array1[i]);
294 }
295 }
296 m_Textwindow.SetWindowText(tekst);
297 }
298 }
299 void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
300 {
301
302 for (int i=0; i<data.size(); i++)
303 {
304 Serial.writeByte( data[i] );
305 Sleep(5);
306 }
307 Serial.writeByte(0x1A);
308 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)
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++)
353 {
354 Serial.writeByte( data[i] );
355 Sleep(5);
356 }
357 Serial.writeByte(atcommand[s]);
358 Serial.writeByte(0x0D);
359 Sleep(250);
360 }
361 void CFlisServerDlg::DBConnect()
362 {
363 CString dsn;
364 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);
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 }

  ViewVC Help
Powered by ViewVC 1.1.20