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

Annotation of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 149 - (hide annotations) (download)
Tue Dec 4 17:32:44 2007 UTC (16 years, 5 months ago) by torben
File size: 20962 byte(s)
Do not make this kind of loops without delay !!!
1 kevin 70 // FlisServerDlg.cpp : implementation file
2     //
3    
4     #include "stdafx.h"
5     #include "FlisServer.h"
6     #include "FlisServerDlg.h"
7     #include <vector>
8 kevin 121 #include ".\flisserverdlg.h"
9 kevin 70
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 kevin 103 ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
68 kevin 105 ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
69 kevin 121 ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
70 kevin 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 kevin 81 DBConnect();
105 kevin 113 ResetSms = 0;
106 kevin 70
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 kevin 136
161 kevin 70 int Baud;
162    
163 kevin 136 ConfigFile config;
164     try {
165     config.ReadSettings();
166     } catch(...) {
167     MessageBox("Could not open config file");
168     OnOK();
169     return true;
170     }
171    
172 kevin 70 Baud = 1200;
173 kevin 132 if( Serial.isOpen() )
174     {
175 kevin 136 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 kevin 70 }
186 kevin 136 else
187 kevin 132 {
188 kevin 136 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 kevin 70 }
198 kevin 136
199 kevin 70
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 kevin 132 unsigned char data = Serial.readByte();
208 kevin 70
209 kevin 132 buf.push_back(data);
210 kevin 70 }
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 kevin 110 void CFlisServerDlg::SetPin()
226 kevin 70 {
227 kevin 81 CString tekst;
228 kevin 70 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 kevin 103 {
246    
247 kevin 70 for (int i=0; i<data.size(); i++)
248     {
249     Serial.writeByte( data[i] );
250     Sleep(5);
251     }
252     Serial.writeByte(0x1A);
253 kevin 105 Sleep(3000);
254     if(Serial.getComstat().cbInQue > 0)
255     {
256     CString tekst;
257     std::vector<unsigned char> answer = readFrame();
258     Sleep(50);
259 kevin 136
260     for (int i=0; i<answer.size(); i++)
261 kevin 105 {
262 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
263 kevin 105 {
264 kevin 136 tekst.AppendChar(answer[i]);
265 kevin 105 }
266     }
267 kevin 136 AppendText(tekst);
268 kevin 105 }
269 kevin 70
270     }
271 kevin 136 void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
272 kevin 70 {
273 kevin 103 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 kevin 136 for (int i=0; i<tlfnr.size(); i++)
292 kevin 70 {
293 kevin 136 Serial.writeByte( tlfnr[i] );
294 kevin 70 Sleep(5);
295     }
296 kevin 103 Serial.writeByte(atcommand[s]);
297 kevin 70 Serial.writeByte(0x0D);
298 kevin 103 Sleep(250);
299 kevin 70 }
300 kevin 81 void CFlisServerDlg::DBConnect()
301 kevin 70 {
302     CString dsn;
303 kevin 136 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 kevin 103 db.OpenEx(dsn, CDatabase::noOdbcDialog);
312 kevin 81 }
313 kevin 136 vector<Commands> CFlisServerDlg::DBReadCommands(void)
314 kevin 81 {
315 kevin 103 vector<Commands> buffer;
316 kevin 81
317 kevin 103 CString SQL, IDnr, CommandID, InstallationsID;
318 kevin 136 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 kevin 103 CRecordset rs(&db);
320     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
321     if (rs.GetRecordCount()>0)
322     {
323     rs.MoveFirst();
324 kevin 130
325 kevin 136 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 kevin 103
334 kevin 136 buffer.push_back(Mycom);
335     rs.MoveNext();
336    
337 kevin 103 }
338     rs.Close();
339     return buffer;
340 kevin 81 }
341     void CFlisServerDlg::ReadSms()
342     {
343     CString tekst, oldtekst;
344 kevin 136 Sleep(950); //Holder en pause for at lade hele sms'en komme ind i serial køen.
345 kevin 81 if(Serial.getComstat().cbInQue > 0)
346     {
347     std::vector<unsigned char> answer = readFrame();
348 kevin 138 Sleep(50);
349 kevin 136
350 kevin 81 for (int i=0; i<answer.size(); i++)
351     {
352 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
353 kevin 81 {
354 kevin 136 tekst.AppendChar(answer[i]);
355 kevin 81 }
356     }
357    
358 kevin 136 AppendText(tekst);
359 kevin 81 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 kevin 136 TlfNr = data.Mid(24,8);
368 kevin 138 FyrData = data.Mid(57,data.GetLength()-59);
369 kevin 81 FyrData.Append(":");
370 kevin 144
371     if (FyrData.MakeLower() == "conf ok:")
372     {
373     HandleAcknowledge(TlfNr);
374     return;
375     }
376 kevin 81
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 kevin 132
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 kevin 138 try
398     {
399 kevin 132 db.ExecuteSQL(SQL);
400 kevin 138 }
401     catch(CDBException* e)
402     {
403     MessageBox(e->m_strError);
404     }
405 kevin 136 AppendText("Sms added to Log");
406 kevin 132 Sleep(150);
407 kevin 81 }
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 kevin 103 }
420     void CFlisServerDlg::OnBnClickedClose()
421     {
422     // TODO: Add your control notification handler code here
423 kevin 113 continueThread = 0;
424 kevin 132 DeleteSms();
425 kevin 113
426     Sleep(500);
427 kevin 103 if( Serial.isOpen() )
428     {
429     Serial.close();
430     }
431    
432     if(db.IsOpen())
433     {
434 kevin 132 db.Close();
435 kevin 103 }
436 kevin 113
437 kevin 103 OnOK();
438 kevin 113
439 kevin 103 }
440 kevin 105
441     void CFlisServerDlg::OnBnClickedGsmpin()
442     {
443     // TODO: Add your control notification handler code here
444 kevin 144 bool ready = true;
445     m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar");
446 kevin 132 UpdateWindow();
447 kevin 105 SetPin();
448 kevin 144 Sleep(500);
449 kevin 132
450 kevin 144 while (ready == true)
451 kevin 132 {
452 kevin 144 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 torben 149 Sleep(5); //Small delay to avoid busy wait
475 kevin 144 }
476     Sleep(100); //Give the modem a chance to send the last data
477     while(Serial.getComstat().cbInQue > 0)
478     {
479 kevin 132 Serial.readByte(); //Flush the incoming queue
480 kevin 144 Sleep(1);
481 kevin 132 }
482 kevin 121 OnBnClickedStart();
483 kevin 105 }
484 kevin 110 void CFlisServerDlg::DeleteSms()
485     {
486     vector<unsigned char> atcommand;
487     atcommand.push_back('a');
488     atcommand.push_back('t');
489     atcommand.push_back('+');
490     atcommand.push_back('c');
491     atcommand.push_back('m');
492     atcommand.push_back('g');
493     atcommand.push_back('d');
494     atcommand.push_back('=');
495     atcommand.push_back('1');
496     atcommand.push_back(',');
497     atcommand.push_back('3');
498    
499     writeFrame(atcommand);
500     Sleep(500);
501     }
502 kevin 136 UINT threadWrapper(LPVOID thread)
503 kevin 110 {
504     CFlisServerDlg *t = (CFlisServerDlg*) thread;
505     t->runthread();
506     return 0;
507     }
508    
509     void CFlisServerDlg::startthread()
510     {
511     AfxBeginThread(threadWrapper, (LPVOID) this);
512     }
513    
514     void CFlisServerDlg::runthread()
515     {
516 kevin 113 while (continueThread != 0)
517 kevin 110 {
518     Reader();
519     }
520     }
521     void CFlisServerDlg::Reader()
522     {
523 kevin 113 if(Serial.getComstat().cbInQue > 0)
524 kevin 110 {
525 kevin 113 Sleep(250);
526 kevin 110 std::vector<unsigned char> answer = readFrame();
527     Sleep(500);
528     CString tekst, oldtekst;
529     int lol;
530    
531     for (int i=0; i<answer.size(); i++)
532     {
533 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
534 kevin 110 {
535 kevin 136 tekst.AppendChar(answer[i]);
536 kevin 110 }
537     }
538     tekst.Append(":");
539    
540     CString command;
541 kevin 144 bool plus = false;
542 kevin 110 int pos = tekst.Find('+',0);
543     if (pos != -1)
544     {
545     plus = true;
546     tekst = tekst.Right( tekst.GetLength() - pos -1);
547     pos = tekst.Find(':');
548     command = tekst.Left(pos);
549     tekst = tekst.Right( tekst.GetLength() - pos -1);
550     }
551    
552    
553 kevin 138 if(tekst.MakeLower() == "ok")
554 kevin 110 {
555 kevin 136 AppendText("OK tekst modtaget");
556 kevin 110 }
557 kevin 138 else if (tekst.MakeLower() == "error")
558 kevin 110 {
559 kevin 136 CString send;
560     send.Append("error tekst");
561     send.Append("\r\n");
562     send.Append(tekst);
563     AppendText(send);
564 kevin 110 }
565 kevin 136 else if (plus == true)
566 kevin 110 {
567 kevin 138 if (command.MakeLower() == "cmti")
568 kevin 110 {
569 kevin 113 CString smscount, oldteskst;
570 kevin 110 int pos = tekst.Find(',',0);
571     if (pos != -1)
572     {
573     smscount = tekst.Right( tekst.GetLength() - pos -1);
574     smscount.Remove(':');
575     ResetSms++;
576     }
577     ////////////////Read sms ting//////////////////
578     std::vector<unsigned char> data;
579     data.push_back('a');
580     data.push_back('t');
581     data.push_back('+');
582     data.push_back('c');
583     data.push_back('m');
584     data.push_back('g');
585     data.push_back('r');
586     data.push_back('=');
587    
588     for (int i=0; i< smscount.GetLength(); i++)
589     {
590     data.push_back(smscount[i]);
591     }
592 kevin 132 m_Textwindow.GetWindowText(oldteskst);
593     oldteskst.Append("\r\n");
594 kevin 113 for (int i=0; i<data.size();i++)
595     {
596 kevin 132 oldteskst.AppendChar(data[i]);
597 kevin 113 }
598     m_Textwindow.SetWindowText(oldteskst);
599 kevin 110 writeFrame(data);
600 kevin 113 Sleep(200);
601 kevin 110 ReadSms();
602 kevin 121 ResetSms++;
603 kevin 110
604     }
605 kevin 138 else if(command.MakeLower() == "wind")
606 kevin 113 {
607 kevin 136
608     CString send;
609     send.Append("Wind modtaget");
610     send.Append("\r\n");
611     send.Append(tekst);
612     AppendText(send);
613 kevin 113 }
614 kevin 110 }
615 kevin 132 else
616 kevin 110 {
617 kevin 144 AppendText(tekst);
618 kevin 132 }
619     if(ResetSms == 20)
620     {
621 kevin 110 DeleteSms();
622 kevin 136 AppendText("Sms'er slettet da vi nåede grænsen");
623 kevin 110 }
624 kevin 113
625     }
626 kevin 136 if (continueThread == 1)
627 kevin 113 {
628 kevin 110
629 kevin 121 CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
630 kevin 130 int commandtest = 0;
631     int iAll = 1;
632 kevin 113 std::vector<Commands> data;
633 kevin 136 data = DBReadCommands();
634 kevin 113 for (int i=0; i<data.size(); i++)
635     {
636 kevin 136 sIDnr = data[i].IDnr;
637     sCommandID = data[i].CommandID;
638 kevin 130 commandtest = atoi(data[i].CommandID);
639 kevin 138 sInstallationsID = "0";
640 kevin 130
641     if(commandtest > 1)
642     {
643 kevin 136 sInstallationsID = data[i].InstallationsID;
644 kevin 130 }
645 kevin 113 }
646 kevin 138
647 kevin 130 if (commandtest > 1)
648     {
649 kevin 132 iAll = 0;
650 kevin 130 }
651 kevin 113
652     if (sIDnr.GetLength() > 0)
653     {
654 kevin 136 SendConfig(sIDnr,sCommandID,sInstallationsID);
655 kevin 113 }
656     Sleep(500);
657 kevin 144
658     CheckAcknowledges();
659     Sleep(500);
660 kevin 110 }
661 kevin 144 CTime now = CTime::GetCurrentTime();
662    
663     CTimeSpan elapsed = now-Alive.tid;
664    
665     if (elapsed.GetTotalSeconds() >= 900)
666     {
667     keepaliveandread();
668     }
669 kevin 113 }
670 kevin 136 void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
671 kevin 144 {
672 kevin 130
673 kevin 113 std::vector<Installation> inst;
674 kevin 144
675     if (CommandID == "1")
676     inst = DBReadPhone("0");
677     else
678     inst = DBReadPhone(InstallationsID);
679    
680     for ( int j=0; j < inst.size(); j++)
681 kevin 113 {
682 kevin 130 CString TlfNr, Imei, updaterate;
683     TlfNr.Empty();
684     Imei.Empty();
685     updaterate.Empty();
686    
687 kevin 144 CString dataen = inst[j].InstPhoneNr;
688 kevin 113 TlfNr.Append(dataen);
689 kevin 144 CString Imeidata = inst[j].Imei;
690 kevin 121 Imei.Append(Imeidata);
691 kevin 144 CString updaterat = inst[j].Updaterate;
692 kevin 121 updaterate.Append(updaterat);
693 kevin 130
694 kevin 144 vector<unsigned char> tlfnr;
695     for (int i=0; i<TlfNr.GetLength(); i++)
696     {
697     tlfnr.push_back(TlfNr[i]);
698     }
699    
700     int calcimei;
701     __int64 buf;
702    
703     buf = atof(Imei);
704    
705     calcimei = tversum(buf);
706    
707     Imei.Format("%d",calcimei);
708    
709     SendSmsHead(tlfnr);
710     Sleep(250);
711    
712     vector<unsigned char> smsdata;
713    
714     for (int i=0; i<Imei.GetLength(); i++)
715     {
716     smsdata.push_back(Imei[i]);
717     }
718     smsdata.push_back(':');
719     for (int i=0; i<Alive.Phonenr.GetLength(); i++)
720     {
721     smsdata.push_back(Alive.Phonenr[i]);
722     }
723     smsdata.push_back(':');
724     for (int i=0; i<updaterate.GetLength(); i++)
725     {
726     smsdata.push_back(updaterate[i]);
727     }
728    
729     SendSmsData(smsdata);
730     Sleep(500);
731    
732     if (IDnr != "0")
733     {
734     Acks.push_back( Acknowledge(inst[j].instID,TlfNr) );
735     }
736    
737 kevin 113 }
738    
739 kevin 144 if (IDnr != "0")
740 kevin 121 {
741 kevin 130 CString SQL, Textwindow;
742     SQL.Format("update command set executed=now() where id=%s",IDnr);
743     db.ExecuteSQL(SQL);
744 kevin 136 AppendText("Command executed");
745 kevin 144 }
746     Sleep(150);
747 kevin 113
748 kevin 144
749    
750    
751 kevin 113 }
752     vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
753     {
754     vector<Installation> buffer;
755    
756 kevin 144 CString SQL, phonenr, imei, updaterate, id;
757 kevin 130 Installation Myinst;
758 kevin 144
759     if (sInstallationsID != "0")
760 kevin 130 {
761 kevin 144 SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
762 kevin 130 }
763 kevin 144 else
764 kevin 130 {
765 kevin 144 SQL.Format("select installationphonenr, imei, updaterate, id from installation");
766 kevin 130 }
767 kevin 144
768 kevin 113 CRecordset rs(&db);
769     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
770     if (rs.GetRecordCount()>0)
771     {
772     rs.MoveFirst();
773     while(!rs.IsEOF())
774     {
775 kevin 130
776     rs.GetFieldValue((short)0,phonenr);
777     rs.GetFieldValue(1,imei);
778     rs.GetFieldValue(2,updaterate);
779 kevin 144 rs.GetFieldValue(3,id);
780 kevin 113
781 kevin 130 Myinst.InstPhoneNr = phonenr;
782     Myinst.Imei = imei;
783     Myinst.Updaterate = updaterate;
784 kevin 144 Myinst.instID = id;
785 kevin 113
786 kevin 130 buffer.push_back(Myinst);
787     rs.MoveNext();
788 kevin 113 }
789     }
790     rs.Close();
791     return buffer;
792 kevin 121 }
793 kevin 130 int CFlisServerDlg::tversum(__int64 input)
794 kevin 121 {
795     int sum = 0;
796     while (input > 0)
797     {
798     sum += (input %10);
799     input /= 10;
800     }
801     return sum;
802     }
803     void CFlisServerDlg::OnBnClickedStart()
804     {
805     // TODO: Add your control notification handler code here
806 kevin 144
807 kevin 121 continueThread = 1;
808 kevin 144 keepaliveandread();
809     Sleep(150);
810     m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr );
811     AppendText("Started");
812 kevin 121 AfxBeginThread(threadWrapper,AfxGetMainWnd());
813     }
814 kevin 136 void CFlisServerDlg::AppendText(CString s)
815     {
816     CString Tekst;
817     m_Textwindow.GetWindowText(Tekst);
818     Tekst.Append("\r\n");
819     Tekst.Append(s);
820     m_Textwindow.SetWindowText(Tekst);
821    
822     }
823     void ConfigFile::ReadSettings()
824     {
825     ifstream file("Server-Settings.ini");
826     if (!file.is_open())
827     throw("Could not open file");
828    
829     char buf[200];
830     while (!file.eof() ) {
831     file.getline(buf,200);
832     CString tmp(buf);
833    
834     if (tmp.GetAt(0) == '#')
835     continue;
836    
837     int pos = tmp.Find('=');
838     if (pos>0) {
839     CString key = tmp.Left(pos).Trim().MakeLower();;
840     CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
841    
842     if (key == "host")
843     host = value;
844     else if (key == "username")
845     username = value;
846     else if (key == "password")
847     password = value;
848     else if (key == "database")
849     database = value;
850     else if (key == "comport")
851     comport = value;
852     }
853    
854     }
855    
856     file.close();
857 kevin 144 }
858     void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
859     {
860     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
861     {
862     Acknowledge& current = (*it);
863     if (current.sTlfNr == tlfnr)
864     {
865     CString SQL;
866     SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID);
867     db.ExecuteSQL(SQL);
868    
869     Acks.erase(it);
870    
871     return;
872     }
873     }
874     }
875    
876     void CFlisServerDlg::CheckAcknowledges()
877     {
878     CTime now = CTime::GetCurrentTime();
879     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
880     {
881     Acknowledge& current = (*it);
882    
883     CTimeSpan elapsed = now-current.cTime;
884    
885     if (elapsed.GetTotalSeconds() >= 300)
886     {
887     DumpAckList();
888     if (current.iRetry >= 1)
889     {
890     CString SQL;
891     SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID);
892     db.ExecuteSQL(SQL);
893     Acks.erase(it);
894     return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again
895     }
896     else
897     {
898     SendConfig("0","2",current.sInstallationID);
899     current.iRetry++;
900     current.cTime = CTime::GetCurrentTime();
901     AppendText( CString("Retry send config to ") + current.sTlfNr );
902     }
903     }
904     }
905     }
906    
907     void CFlisServerDlg::DumpAckList()
908     {
909     CTime now = CTime::GetCurrentTime();
910     OutputDebugString("-------------------------------\n");
911     int count = 0;
912     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
913     {
914     CString msg;
915     msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr);
916     OutputDebugString(msg);
917     count++;
918     }
919     }
920     vector<keepalive> CFlisServerDlg::keepaliveandread(void)
921     {
922     vector<keepalive> buffer;
923    
924     CString SQL, name, value;
925     SQL = "select name, value from config;";
926     CRecordset rs(&db);
927 kevin 146 try
928 kevin 144 {
929 kevin 146 rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
930     if (rs.GetRecordCount()>0)
931     {
932     rs.MoveFirst();
933 kevin 144
934 kevin 146 rs.GetFieldValue((short)0, name);
935     rs.GetFieldValue(1,value);
936 kevin 144
937 kevin 146 Alive.name = name;
938     Alive.value = value;
939     Alive.tid = CTime::GetCurrentTime();
940     if(Alive.name == "phonenr")
941     {
942     Alive.Phonenr = value;
943     }
944    
945     buffer.push_back(Alive);
946     rs.MoveNext();
947    
948     }
949     rs.Close();
950     return buffer;
951     }
952     catch (char * str)
953     {
954     AppendText("Keepalive failed, closing and opening the db connection again");
955     if(db.IsOpen())
956 kevin 144 {
957 kevin 146 db.Close();
958     AppendText("DB connection closed");
959 kevin 144 }
960 kevin 146 if(!db.IsOpen())
961     {
962     DBConnect();
963     AppendText("DB connection started again");
964     }
965     }
966 kevin 144
967    
968 kevin 136 }

  ViewVC Help
Powered by ViewVC 1.1.20