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

Contents of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 144 - (show annotations) (download)
Tue Dec 4 13:26:00 2007 UTC (16 years, 5 months ago) by kevin
File size: 20594 byte(s)
updated diary for kevin, and modified the server software to match the kravspec.
1 // FlisServerDlg.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "FlisServer.h"
6 #include "FlisServerDlg.h"
7 #include <vector>
8 #include ".\flisserverdlg.h"
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(IDCLOSE, OnBnClickedClose)
68 ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
69 ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
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 ResetSms = 0;
106
107 return TRUE; // return TRUE unless you set the focus to a control
108 }
109
110 void CFlisServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
111 {
112 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
113 {
114 CAboutDlg dlgAbout;
115 dlgAbout.DoModal();
116 }
117 else
118 {
119 CDialog::OnSysCommand(nID, lParam);
120 }
121 }
122
123 // If you add a minimize button to your dialog, you will need the code below
124 // to draw the icon. For MFC applications using the document/view model,
125 // this is automatically done for you by the framework.
126
127 void CFlisServerDlg::OnPaint()
128 {
129 if (IsIconic())
130 {
131 CPaintDC dc(this); // device context for painting
132
133 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
134
135 // Center icon in client rectangle
136 int cxIcon = GetSystemMetrics(SM_CXICON);
137 int cyIcon = GetSystemMetrics(SM_CYICON);
138 CRect rect;
139 GetClientRect(&rect);
140 int x = (rect.Width() - cxIcon + 1) / 2;
141 int y = (rect.Height() - cyIcon + 1) / 2;
142
143 // Draw the icon
144 dc.DrawIcon(x, y, m_hIcon);
145 }
146 else
147 {
148 CDialog::OnPaint();
149 }
150 }
151
152 // The system calls this function to obtain the cursor to display while the user drags
153 // the minimized window.
154 HCURSOR CFlisServerDlg::OnQueryDragIcon()
155 {
156 return static_cast<HCURSOR>(m_hIcon);
157 }
158 int CFlisServerDlg::StartSerial(void)
159 {
160
161 int Baud;
162
163 ConfigFile config;
164 try {
165 config.ReadSettings();
166 } catch(...) {
167 MessageBox("Could not open config file");
168 OnOK();
169 return true;
170 }
171
172 Baud = 1200;
173 if( Serial.isOpen() )
174 {
175 try
176 {
177 Serial.close();
178 Serial.open( config.comport, Baud );
179 }
180 catch (exception* e)
181 {
182 MessageBox( "Serial.open() exception" );
183 return 0;
184 }
185 }
186 else
187 {
188 try
189 {
190 Serial.open( config.comport, Baud );
191 }
192 catch (exception* e)
193 {
194 MessageBox( "Serial.open() exception 2" );
195 return 0;
196 }
197 }
198
199
200 return 0;
201 }
202 std::vector<unsigned char> CFlisServerDlg::readFrame()
203 {
204 std::vector<unsigned char> buf;
205 while(Serial.getComstat().cbInQue > 0)
206 {
207 unsigned char data = Serial.readByte();
208
209 buf.push_back(data);
210 }
211 return buf;
212 }
213
214 void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
215 {
216 for (int i=0; i<data.size(); i++)
217 {
218 Serial.writeByte( data[i] );
219 Sleep(5);
220 }
221 Serial.writeByte(0x0D);
222 Sleep(100);
223
224 }
225 void CFlisServerDlg::SetPin()
226 {
227 CString tekst;
228 std::vector<unsigned char> data;
229 data.push_back('a');
230 data.push_back('t');
231 data.push_back('+');
232 data.push_back('c');
233 data.push_back('p');
234 data.push_back('i');
235 data.push_back('n');
236 data.push_back('=');
237 data.push_back('2');
238 data.push_back('5');
239 data.push_back('9');
240 data.push_back('5');
241
242 writeFrame(data);
243 }
244 void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
245 {
246
247 for (int i=0; i<data.size(); i++)
248 {
249 Serial.writeByte( data[i] );
250 Sleep(5);
251 }
252 Serial.writeByte(0x1A);
253 Sleep(3000);
254 if(Serial.getComstat().cbInQue > 0)
255 {
256 CString tekst;
257 std::vector<unsigned char> answer = readFrame();
258 Sleep(50);
259
260 for (int i=0; i<answer.size(); i++)
261 {
262 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
263 {
264 tekst.AppendChar(answer[i]);
265 }
266 }
267 AppendText(tekst);
268 }
269
270 }
271 void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
272 {
273 vector<unsigned char> atcommand;
274 atcommand.push_back('a');
275 atcommand.push_back('t');
276 atcommand.push_back('+');
277 atcommand.push_back('c');
278 atcommand.push_back('m');
279 atcommand.push_back('g');
280 atcommand.push_back('s');
281 atcommand.push_back('=');
282 atcommand.push_back('"');
283 int s = (atcommand.size() -1 );
284
285 for (int i=0; i<(atcommand.size()); i++)
286 {
287 Serial.writeByte( atcommand[i] );
288 Sleep(5);
289 }
290
291 for (int i=0; i<tlfnr.size(); i++)
292 {
293 Serial.writeByte( tlfnr[i] );
294 Sleep(5);
295 }
296 Serial.writeByte(atcommand[s]);
297 Serial.writeByte(0x0D);
298 Sleep(250);
299 }
300 void CFlisServerDlg::DBConnect()
301 {
302 CString dsn;
303 ConfigFile config;
304 try {
305 config.ReadSettings();
306 } catch(...) {
307 MessageBox("Could not open config file");
308 }
309
310 dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=%s; uid=%s;password=%s;database=%s;sslmode=prefer",config.host,config.username, config.password, config.database);
311 db.OpenEx(dsn, CDatabase::noOdbcDialog);
312 }
313 vector<Commands> CFlisServerDlg::DBReadCommands(void)
314 {
315 vector<Commands> buffer;
316
317 CString SQL, IDnr, CommandID, InstallationsID;
318 SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL ORDER BY created ASC LIMIT 1;";
319 CRecordset rs(&db);
320 rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
321 if (rs.GetRecordCount()>0)
322 {
323 rs.MoveFirst();
324
325 Commands Mycom;
326 rs.GetFieldValue((short)0, IDnr);
327 rs.GetFieldValue(3, CommandID);
328 rs.GetFieldValue(4, InstallationsID);
329
330 Mycom.IDnr = IDnr;
331 Mycom.CommandID = CommandID;
332 Mycom.InstallationsID = InstallationsID;
333
334 buffer.push_back(Mycom);
335 rs.MoveNext();
336
337 }
338 rs.Close();
339 return buffer;
340 }
341 void CFlisServerDlg::ReadSms()
342 {
343 CString tekst, oldtekst;
344 Sleep(950); //Holder en pause for at lade hele sms'en komme ind i serial køen.
345 if(Serial.getComstat().cbInQue > 0)
346 {
347 std::vector<unsigned char> answer = readFrame();
348 Sleep(50);
349
350 for (int i=0; i<answer.size(); i++)
351 {
352 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
353 {
354 tekst.AppendChar(answer[i]);
355 }
356 }
357
358 AppendText(tekst);
359 SmsSplit(tekst);
360 }
361 }
362 void CFlisServerDlg::SmsSplit(CString data)
363 {
364 CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
365 char CharData[150];
366 strcpy(CharData,data);
367 TlfNr = data.Mid(24,8);
368 FyrData = data.Mid(57,data.GetLength()-59);
369 FyrData.Append(":");
370
371 if (FyrData.MakeLower() == "conf ok:")
372 {
373 HandleAcknowledge(TlfNr);
374 return;
375 }
376
377 SmsCount = Splitter(FyrData);
378 Temper = Splitter(FyrData);
379 Flamme = Splitter(FyrData);
380 Flis = Splitter(FyrData);
381 FremFejl = Splitter(FyrData);
382 PowerFail = Splitter(FyrData);
383
384 CString SQL, Textwindow, InstallNR;
385 SQL.Format("select ID from installation where installationphonenr=%s",TlfNr);
386
387 CRecordset rs(&db);
388 rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
389 if (rs.GetRecordCount()>0)
390 {
391 rs.MoveFirst();
392 rs.GetFieldValue((short)0,InstallNR);
393 }
394 rs.Close();
395
396 SQL.Format("insert into logtable (logtime,temperature,flamedetector,solidfuelempty,conveyorerror,powerfailure,messagenr,installationnr) Values (now(),%s,'%s','%s','%s','%s',%s,%s)",Temper, Flamme, Flis, FremFejl, PowerFail, SmsCount,InstallNR);
397 try
398 {
399 db.ExecuteSQL(SQL);
400 }
401 catch(CDBException* e)
402 {
403 MessageBox(e->m_strError);
404 }
405 AppendText("Sms added to Log");
406 Sleep(150);
407 }
408 CString CFlisServerDlg::Splitter(CString& fyrdata)
409 {
410 CString Output;
411
412 int pos = fyrdata.Find(':',0);
413 if (pos != -1)
414 {
415 Output = fyrdata.Left(pos);
416 fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
417 }
418 return Output;
419 }
420 void CFlisServerDlg::OnBnClickedClose()
421 {
422 // TODO: Add your control notification handler code here
423 continueThread = 0;
424 DeleteSms();
425
426 Sleep(500);
427 if( Serial.isOpen() )
428 {
429 Serial.close();
430 }
431
432 if(db.IsOpen())
433 {
434 db.Close();
435 }
436
437 OnOK();
438
439 }
440
441 void CFlisServerDlg::OnBnClickedGsmpin()
442 {
443 // TODO: Add your control notification handler code here
444 bool ready = true;
445 m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar");
446 UpdateWindow();
447 SetPin();
448 Sleep(500);
449
450 while (ready == true)
451 {
452 if (Serial.getComstat().cbInQue > 120)
453 {
454 std::vector<unsigned char> answer = readFrame();
455 CString tekst;
456
457 for (int i=0; i<answer.size(); i++)
458 {
459 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
460 {
461 tekst.AppendChar(answer[i]);
462 }
463 }
464 int n = tekst.Find("WIND: 11",0);
465 if (n > -1)
466 {
467 ready = false;
468 }
469 else
470 {
471 AppendText(tekst);
472 }
473 }
474 }
475 Sleep(100); //Give the modem a chance to send the last data
476 while(Serial.getComstat().cbInQue > 0)
477 {
478 Serial.readByte(); //Flush the incoming queue
479 Sleep(1);
480 }
481 OnBnClickedStart();
482 }
483 void CFlisServerDlg::DeleteSms()
484 {
485 vector<unsigned char> atcommand;
486 atcommand.push_back('a');
487 atcommand.push_back('t');
488 atcommand.push_back('+');
489 atcommand.push_back('c');
490 atcommand.push_back('m');
491 atcommand.push_back('g');
492 atcommand.push_back('d');
493 atcommand.push_back('=');
494 atcommand.push_back('1');
495 atcommand.push_back(',');
496 atcommand.push_back('3');
497
498 writeFrame(atcommand);
499 Sleep(500);
500 }
501 UINT threadWrapper(LPVOID thread)
502 {
503 CFlisServerDlg *t = (CFlisServerDlg*) thread;
504 t->runthread();
505 return 0;
506 }
507
508 void CFlisServerDlg::startthread()
509 {
510 AfxBeginThread(threadWrapper, (LPVOID) this);
511 }
512
513 void CFlisServerDlg::runthread()
514 {
515 while (continueThread != 0)
516 {
517 Reader();
518 }
519 }
520 void CFlisServerDlg::Reader()
521 {
522 if(Serial.getComstat().cbInQue > 0)
523 {
524 Sleep(250);
525 std::vector<unsigned char> answer = readFrame();
526 Sleep(500);
527 CString tekst, oldtekst;
528 int lol;
529
530 for (int i=0; i<answer.size(); i++)
531 {
532 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
533 {
534 tekst.AppendChar(answer[i]);
535 }
536 }
537 tekst.Append(":");
538
539 CString command;
540 bool plus = false;
541 int pos = tekst.Find('+',0);
542 if (pos != -1)
543 {
544 plus = true;
545 tekst = tekst.Right( tekst.GetLength() - pos -1);
546 pos = tekst.Find(':');
547 command = tekst.Left(pos);
548 tekst = tekst.Right( tekst.GetLength() - pos -1);
549 }
550
551
552 if(tekst.MakeLower() == "ok")
553 {
554 AppendText("OK tekst modtaget");
555 }
556 else if (tekst.MakeLower() == "error")
557 {
558 CString send;
559 send.Append("error tekst");
560 send.Append("\r\n");
561 send.Append(tekst);
562 AppendText(send);
563 }
564 else if (plus == true)
565 {
566 if (command.MakeLower() == "cmti")
567 {
568 CString smscount, oldteskst;
569 int pos = tekst.Find(',',0);
570 if (pos != -1)
571 {
572 smscount = tekst.Right( tekst.GetLength() - pos -1);
573 smscount.Remove(':');
574 ResetSms++;
575 }
576 ////////////////Read sms ting//////////////////
577 std::vector<unsigned char> data;
578 data.push_back('a');
579 data.push_back('t');
580 data.push_back('+');
581 data.push_back('c');
582 data.push_back('m');
583 data.push_back('g');
584 data.push_back('r');
585 data.push_back('=');
586
587 for (int i=0; i< smscount.GetLength(); i++)
588 {
589 data.push_back(smscount[i]);
590 }
591 m_Textwindow.GetWindowText(oldteskst);
592 oldteskst.Append("\r\n");
593 for (int i=0; i<data.size();i++)
594 {
595 oldteskst.AppendChar(data[i]);
596 }
597 m_Textwindow.SetWindowText(oldteskst);
598 writeFrame(data);
599 Sleep(200);
600 ReadSms();
601 ResetSms++;
602
603 }
604 else if(command.MakeLower() == "wind")
605 {
606
607 CString send;
608 send.Append("Wind modtaget");
609 send.Append("\r\n");
610 send.Append(tekst);
611 AppendText(send);
612 }
613 }
614 else
615 {
616 AppendText(tekst);
617 }
618 if(ResetSms == 20)
619 {
620 DeleteSms();
621 AppendText("Sms'er slettet da vi nåede grænsen");
622 }
623
624 }
625 if (continueThread == 1)
626 {
627
628 CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
629 int commandtest = 0;
630 int iAll = 1;
631 std::vector<Commands> data;
632 data = DBReadCommands();
633 for (int i=0; i<data.size(); i++)
634 {
635 sIDnr = data[i].IDnr;
636 sCommandID = data[i].CommandID;
637 commandtest = atoi(data[i].CommandID);
638 sInstallationsID = "0";
639
640 if(commandtest > 1)
641 {
642 sInstallationsID = data[i].InstallationsID;
643 }
644 }
645
646 if (commandtest > 1)
647 {
648 iAll = 0;
649 }
650
651 if (sIDnr.GetLength() > 0)
652 {
653 SendConfig(sIDnr,sCommandID,sInstallationsID);
654 }
655 Sleep(500);
656
657 CheckAcknowledges();
658 Sleep(500);
659 }
660 CTime now = CTime::GetCurrentTime();
661
662 CTimeSpan elapsed = now-Alive.tid;
663
664 if (elapsed.GetTotalSeconds() >= 900)
665 {
666 keepaliveandread();
667 }
668 }
669 void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
670 {
671
672 std::vector<Installation> inst;
673
674 if (CommandID == "1")
675 inst = DBReadPhone("0");
676 else
677 inst = DBReadPhone(InstallationsID);
678
679 for ( int j=0; j < inst.size(); j++)
680 {
681 CString TlfNr, Imei, updaterate;
682 TlfNr.Empty();
683 Imei.Empty();
684 updaterate.Empty();
685
686 CString dataen = inst[j].InstPhoneNr;
687 TlfNr.Append(dataen);
688 CString Imeidata = inst[j].Imei;
689 Imei.Append(Imeidata);
690 CString updaterat = inst[j].Updaterate;
691 updaterate.Append(updaterat);
692
693 vector<unsigned char> tlfnr;
694 for (int i=0; i<TlfNr.GetLength(); i++)
695 {
696 tlfnr.push_back(TlfNr[i]);
697 }
698
699 int calcimei;
700 __int64 buf;
701
702 buf = atof(Imei);
703
704 calcimei = tversum(buf);
705
706 Imei.Format("%d",calcimei);
707
708 SendSmsHead(tlfnr);
709 Sleep(250);
710
711 vector<unsigned char> smsdata;
712
713 for (int i=0; i<Imei.GetLength(); i++)
714 {
715 smsdata.push_back(Imei[i]);
716 }
717 smsdata.push_back(':');
718 for (int i=0; i<Alive.Phonenr.GetLength(); i++)
719 {
720 smsdata.push_back(Alive.Phonenr[i]);
721 }
722 smsdata.push_back(':');
723 for (int i=0; i<updaterate.GetLength(); i++)
724 {
725 smsdata.push_back(updaterate[i]);
726 }
727
728 SendSmsData(smsdata);
729 Sleep(500);
730
731 if (IDnr != "0")
732 {
733 Acks.push_back( Acknowledge(inst[j].instID,TlfNr) );
734 }
735
736 }
737
738 if (IDnr != "0")
739 {
740 CString SQL, Textwindow;
741 SQL.Format("update command set executed=now() where id=%s",IDnr);
742 db.ExecuteSQL(SQL);
743 AppendText("Command executed");
744 }
745 Sleep(150);
746
747
748
749
750 }
751 vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
752 {
753 vector<Installation> buffer;
754
755 CString SQL, phonenr, imei, updaterate, id;
756 Installation Myinst;
757
758 if (sInstallationsID != "0")
759 {
760 SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
761 }
762 else
763 {
764 SQL.Format("select installationphonenr, imei, updaterate, id from installation");
765 }
766
767 CRecordset rs(&db);
768 rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
769 if (rs.GetRecordCount()>0)
770 {
771 rs.MoveFirst();
772 while(!rs.IsEOF())
773 {
774
775 rs.GetFieldValue((short)0,phonenr);
776 rs.GetFieldValue(1,imei);
777 rs.GetFieldValue(2,updaterate);
778 rs.GetFieldValue(3,id);
779
780 Myinst.InstPhoneNr = phonenr;
781 Myinst.Imei = imei;
782 Myinst.Updaterate = updaterate;
783 Myinst.instID = id;
784
785 buffer.push_back(Myinst);
786 rs.MoveNext();
787 }
788 }
789 rs.Close();
790 return buffer;
791 }
792 int CFlisServerDlg::tversum(__int64 input)
793 {
794 int sum = 0;
795 while (input > 0)
796 {
797 sum += (input %10);
798 input /= 10;
799 }
800 return sum;
801 }
802 void CFlisServerDlg::OnBnClickedStart()
803 {
804 // TODO: Add your control notification handler code here
805
806 continueThread = 1;
807 keepaliveandread();
808 Sleep(150);
809 m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr );
810 AppendText("Started");
811 AfxBeginThread(threadWrapper,AfxGetMainWnd());
812 }
813 void CFlisServerDlg::AppendText(CString s)
814 {
815 CString Tekst;
816 m_Textwindow.GetWindowText(Tekst);
817 Tekst.Append("\r\n");
818 Tekst.Append(s);
819 m_Textwindow.SetWindowText(Tekst);
820
821 }
822 void ConfigFile::ReadSettings()
823 {
824 ifstream file("Server-Settings.ini");
825 if (!file.is_open())
826 throw("Could not open file");
827
828 char buf[200];
829 while (!file.eof() ) {
830 file.getline(buf,200);
831 CString tmp(buf);
832
833 if (tmp.GetAt(0) == '#')
834 continue;
835
836 int pos = tmp.Find('=');
837 if (pos>0) {
838 CString key = tmp.Left(pos).Trim().MakeLower();;
839 CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
840
841 if (key == "host")
842 host = value;
843 else if (key == "username")
844 username = value;
845 else if (key == "password")
846 password = value;
847 else if (key == "database")
848 database = value;
849 else if (key == "comport")
850 comport = value;
851 }
852
853 }
854
855 file.close();
856 }
857 void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
858 {
859 for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
860 {
861 Acknowledge& current = (*it);
862 if (current.sTlfNr == tlfnr)
863 {
864 CString SQL;
865 SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID);
866 db.ExecuteSQL(SQL);
867
868 Acks.erase(it);
869
870 return;
871 }
872 }
873 }
874
875 void CFlisServerDlg::CheckAcknowledges()
876 {
877 CTime now = CTime::GetCurrentTime();
878 for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
879 {
880 Acknowledge& current = (*it);
881
882 CTimeSpan elapsed = now-current.cTime;
883
884 if (elapsed.GetTotalSeconds() >= 300)
885 {
886 DumpAckList();
887 if (current.iRetry >= 1)
888 {
889 CString SQL;
890 SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID);
891 db.ExecuteSQL(SQL);
892 Acks.erase(it);
893 return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again
894 }
895 else
896 {
897 SendConfig("0","2",current.sInstallationID);
898 current.iRetry++;
899 current.cTime = CTime::GetCurrentTime();
900 AppendText( CString("Retry send config to ") + current.sTlfNr );
901 }
902 }
903 }
904 }
905
906 void CFlisServerDlg::DumpAckList()
907 {
908 CTime now = CTime::GetCurrentTime();
909 OutputDebugString("-------------------------------\n");
910 int count = 0;
911 for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
912 {
913 CString msg;
914 msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr);
915 OutputDebugString(msg);
916 count++;
917 }
918 }
919 vector<keepalive> CFlisServerDlg::keepaliveandread(void)
920 {
921 vector<keepalive> buffer;
922
923 CString SQL, name, value;
924 SQL = "select name, value from config;";
925 CRecordset rs(&db);
926 rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
927 if (rs.GetRecordCount()>0)
928 {
929 rs.MoveFirst();
930
931 rs.GetFieldValue((short)0, name);
932 rs.GetFieldValue(1,value);
933
934 Alive.name = name;
935 Alive.value = value;
936 Alive.tid = CTime::GetCurrentTime();
937 if(Alive.name == "phonenr")
938 {
939 Alive.Phonenr = value;
940 }
941
942 buffer.push_back(Alive);
943 rs.MoveNext();
944
945 }
946 rs.Close();
947 return buffer;
948
949 }

  ViewVC Help
Powered by ViewVC 1.1.20