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

Annotation of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 168 - (hide annotations) (download)
Thu Dec 6 07:14:17 2007 UTC (16 years, 5 months ago) by kevin
File size: 21476 byte(s)
updated the serversoftware to fix some minor bugs
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 166 #include <atlrx.h>
10 kevin 70
11     #ifdef _DEBUG
12     #define new DEBUG_NEW
13     #endif
14    
15    
16     // CAboutDlg dialog used for App About
17    
18     class CAboutDlg : public CDialog
19     {
20     public:
21     CAboutDlg();
22    
23     // Dialog Data
24     enum { IDD = IDD_ABOUTBOX };
25    
26     protected:
27     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
28    
29     // Implementation
30     protected:
31     DECLARE_MESSAGE_MAP()
32     };
33    
34     CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
35     {
36     }
37    
38     void CAboutDlg::DoDataExchange(CDataExchange* pDX)
39     {
40     CDialog::DoDataExchange(pDX);
41     }
42    
43     BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
44     END_MESSAGE_MAP()
45    
46    
47     // CFlisServerDlg dialog
48    
49    
50    
51     CFlisServerDlg::CFlisServerDlg(CWnd* pParent /*=NULL*/)
52     : CDialog(CFlisServerDlg::IDD, pParent)
53     {
54     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
55     }
56    
57     void CFlisServerDlg::DoDataExchange(CDataExchange* pDX)
58     {
59     CDialog::DoDataExchange(pDX);
60     DDX_Control(pDX, IDC_Textwindow, m_Textwindow);
61     }
62    
63     BEGIN_MESSAGE_MAP(CFlisServerDlg, CDialog)
64     ON_WM_SYSCOMMAND()
65     ON_WM_PAINT()
66     ON_WM_QUERYDRAGICON()
67     //}}AFX_MSG_MAP
68 kevin 103 ON_BN_CLICKED(IDCLOSE, OnBnClickedClose)
69 kevin 105 ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin)
70 kevin 121 ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
71 kevin 70 END_MESSAGE_MAP()
72    
73    
74     // CFlisServerDlg message handlers
75    
76     BOOL CFlisServerDlg::OnInitDialog()
77     {
78     CDialog::OnInitDialog();
79    
80     // Add "About..." menu item to system menu.
81    
82     // IDM_ABOUTBOX must be in the system command range.
83     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
84     ASSERT(IDM_ABOUTBOX < 0xF000);
85    
86     CMenu* pSysMenu = GetSystemMenu(FALSE);
87     if (pSysMenu != NULL)
88     {
89     CString strAboutMenu;
90     strAboutMenu.LoadString(IDS_ABOUTBOX);
91     if (!strAboutMenu.IsEmpty())
92     {
93     pSysMenu->AppendMenu(MF_SEPARATOR);
94     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
95     }
96     }
97    
98     // Set the icon for this dialog. The framework does this automatically
99     // when the application's main window is not a dialog
100     SetIcon(m_hIcon, TRUE); // Set big icon
101     SetIcon(m_hIcon, FALSE); // Set small icon
102    
103     // TODO: Add extra initialization here
104     StartSerial();
105 kevin 81 DBConnect();
106 kevin 113 ResetSms = 0;
107 kevin 70
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 kevin 136
162 kevin 70 int Baud;
163    
164 kevin 136 ConfigFile config;
165     try {
166     config.ReadSettings();
167     } catch(...) {
168     MessageBox("Could not open config file");
169     OnOK();
170     return true;
171     }
172    
173 kevin 70 Baud = 1200;
174 kevin 132 if( Serial.isOpen() )
175     {
176 kevin 136 try
177     {
178     Serial.close();
179     Serial.open( config.comport, Baud );
180     }
181     catch (exception* e)
182     {
183     MessageBox( "Serial.open() exception" );
184     return 0;
185     }
186 kevin 70 }
187 kevin 136 else
188 kevin 132 {
189 kevin 136 try
190     {
191     Serial.open( config.comport, Baud );
192     }
193     catch (exception* e)
194     {
195     MessageBox( "Serial.open() exception 2" );
196     return 0;
197     }
198 kevin 70 }
199 kevin 136
200 kevin 70
201     return 0;
202     }
203     std::vector<unsigned char> CFlisServerDlg::readFrame()
204     {
205     std::vector<unsigned char> buf;
206     while(Serial.getComstat().cbInQue > 0)
207     {
208 kevin 132 unsigned char data = Serial.readByte();
209 kevin 70
210 kevin 132 buf.push_back(data);
211 kevin 70 }
212     return buf;
213     }
214    
215     void CFlisServerDlg::writeFrame(std::vector<unsigned char> data)
216     {
217     for (int i=0; i<data.size(); i++)
218     {
219     Serial.writeByte( data[i] );
220     Sleep(5);
221     }
222     Serial.writeByte(0x0D);
223     Sleep(100);
224    
225     }
226 kevin 110 void CFlisServerDlg::SetPin()
227 kevin 70 {
228 kevin 81 CString tekst;
229 kevin 70 std::vector<unsigned char> data;
230     data.push_back('a');
231     data.push_back('t');
232     data.push_back('+');
233     data.push_back('c');
234     data.push_back('p');
235     data.push_back('i');
236     data.push_back('n');
237     data.push_back('=');
238     data.push_back('2');
239     data.push_back('5');
240     data.push_back('9');
241     data.push_back('5');
242    
243     writeFrame(data);
244     }
245     void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
246 kevin 103 {
247    
248 kevin 70 for (int i=0; i<data.size(); i++)
249     {
250     Serial.writeByte( data[i] );
251     Sleep(5);
252     }
253     Serial.writeByte(0x1A);
254 kevin 105 Sleep(3000);
255     if(Serial.getComstat().cbInQue > 0)
256     {
257     CString tekst;
258     std::vector<unsigned char> answer = readFrame();
259     Sleep(50);
260 kevin 136
261     for (int i=0; i<answer.size(); i++)
262 kevin 105 {
263 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
264 kevin 105 {
265 kevin 136 tekst.AppendChar(answer[i]);
266 kevin 105 }
267     }
268 kevin 136 AppendText(tekst);
269 kevin 105 }
270 kevin 70
271     }
272 kevin 136 void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
273 kevin 70 {
274 kevin 103 vector<unsigned char> atcommand;
275     atcommand.push_back('a');
276     atcommand.push_back('t');
277     atcommand.push_back('+');
278     atcommand.push_back('c');
279     atcommand.push_back('m');
280     atcommand.push_back('g');
281     atcommand.push_back('s');
282     atcommand.push_back('=');
283     atcommand.push_back('"');
284     int s = (atcommand.size() -1 );
285    
286     for (int i=0; i<(atcommand.size()); i++)
287     {
288     Serial.writeByte( atcommand[i] );
289     Sleep(5);
290     }
291    
292 kevin 136 for (int i=0; i<tlfnr.size(); i++)
293 kevin 70 {
294 kevin 136 Serial.writeByte( tlfnr[i] );
295 kevin 70 Sleep(5);
296     }
297 kevin 103 Serial.writeByte(atcommand[s]);
298 kevin 70 Serial.writeByte(0x0D);
299 kevin 103 Sleep(250);
300 kevin 70 }
301 kevin 81 void CFlisServerDlg::DBConnect()
302 kevin 70 {
303     CString dsn;
304 kevin 136 ConfigFile config;
305     try {
306     config.ReadSettings();
307     } catch(...) {
308     MessageBox("Could not open config file");
309     }
310    
311     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);
312 kevin 103 db.OpenEx(dsn, CDatabase::noOdbcDialog);
313 kevin 81 }
314 kevin 136 vector<Commands> CFlisServerDlg::DBReadCommands(void)
315 kevin 81 {
316 kevin 103 vector<Commands> buffer;
317 kevin 81
318 kevin 103 CString SQL, IDnr, CommandID, InstallationsID;
319 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;";
320 kevin 103 CRecordset rs(&db);
321     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
322     if (rs.GetRecordCount()>0)
323     {
324     rs.MoveFirst();
325 kevin 130
326 kevin 136 Commands Mycom;
327     rs.GetFieldValue((short)0, IDnr);
328     rs.GetFieldValue(3, CommandID);
329     rs.GetFieldValue(4, InstallationsID);
330    
331     Mycom.IDnr = IDnr;
332     Mycom.CommandID = CommandID;
333     Mycom.InstallationsID = InstallationsID;
334 kevin 103
335 kevin 136 buffer.push_back(Mycom);
336     rs.MoveNext();
337    
338 kevin 103 }
339     rs.Close();
340     return buffer;
341 kevin 81 }
342     void CFlisServerDlg::ReadSms()
343     {
344     CString tekst, oldtekst;
345 kevin 136 Sleep(950); //Holder en pause for at lade hele sms'en komme ind i serial køen.
346 kevin 81 if(Serial.getComstat().cbInQue > 0)
347     {
348     std::vector<unsigned char> answer = readFrame();
349 kevin 138 Sleep(50);
350 kevin 136
351 kevin 81 for (int i=0; i<answer.size(); i++)
352     {
353 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
354 kevin 81 {
355 kevin 136 tekst.AppendChar(answer[i]);
356 kevin 81 }
357     }
358    
359 kevin 136 AppendText(tekst);
360 kevin 81 SmsSplit(tekst);
361     }
362     }
363     void CFlisServerDlg::SmsSplit(CString data)
364     {
365     CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
366     char CharData[150];
367     strcpy(CharData,data);
368 kevin 136 TlfNr = data.Mid(24,8);
369 kevin 138 FyrData = data.Mid(57,data.GetLength()-59);
370 kevin 81 FyrData.Append(":");
371 kevin 144
372     if (FyrData.MakeLower() == "conf ok:")
373     {
374     HandleAcknowledge(TlfNr);
375     return;
376     }
377 kevin 166 CAtlRegExp<> regex;
378     if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK)
379     {
380    
381     CAtlREMatchContext<> pContext;
382    
383     if (!regex.Match(FyrData, &pContext))
384     {
385     AppendText("Invalid SMS recieved");
386     return;
387     }
388     }
389     else
390     {
391     AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske !
392     }
393    
394 kevin 81 SmsCount = Splitter(FyrData);
395     Temper = Splitter(FyrData);
396     Flamme = Splitter(FyrData);
397     Flis = Splitter(FyrData);
398     FremFejl = Splitter(FyrData);
399     PowerFail = Splitter(FyrData);
400 kevin 132
401     CString SQL, Textwindow, InstallNR;
402     SQL.Format("select ID from installation where installationphonenr=%s",TlfNr);
403    
404     CRecordset rs(&db);
405     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
406     if (rs.GetRecordCount()>0)
407     {
408     rs.MoveFirst();
409     rs.GetFieldValue((short)0,InstallNR);
410     }
411     rs.Close();
412    
413     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);
414 kevin 138 try
415     {
416 kevin 132 db.ExecuteSQL(SQL);
417 kevin 138 }
418     catch(CDBException* e)
419     {
420     MessageBox(e->m_strError);
421     }
422 kevin 136 AppendText("Sms added to Log");
423 kevin 132 Sleep(150);
424 kevin 81 }
425     CString CFlisServerDlg::Splitter(CString& fyrdata)
426     {
427     CString Output;
428    
429     int pos = fyrdata.Find(':',0);
430     if (pos != -1)
431     {
432     Output = fyrdata.Left(pos);
433     fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
434     }
435     return Output;
436 kevin 103 }
437     void CFlisServerDlg::OnBnClickedClose()
438     {
439     // TODO: Add your control notification handler code here
440 kevin 113 continueThread = 0;
441 kevin 166 Sleep(250);
442 kevin 132 DeleteSms();
443 kevin 113
444     Sleep(500);
445 kevin 103 if( Serial.isOpen() )
446     {
447     Serial.close();
448     }
449    
450     if(db.IsOpen())
451     {
452 kevin 132 db.Close();
453 kevin 103 }
454 kevin 113
455 kevin 103 OnOK();
456 kevin 113
457 kevin 103 }
458 kevin 105
459     void CFlisServerDlg::OnBnClickedGsmpin()
460     {
461     // TODO: Add your control notification handler code here
462 kevin 144 bool ready = true;
463     m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar");
464 kevin 132 UpdateWindow();
465 kevin 105 SetPin();
466 kevin 144 Sleep(500);
467 kevin 132
468 kevin 144 while (ready == true)
469 kevin 132 {
470 kevin 144 if (Serial.getComstat().cbInQue > 120)
471     {
472     std::vector<unsigned char> answer = readFrame();
473     CString tekst;
474    
475     for (int i=0; i<answer.size(); i++)
476     {
477     if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
478     {
479     tekst.AppendChar(answer[i]);
480     }
481     }
482     int n = tekst.Find("WIND: 11",0);
483     if (n > -1)
484     {
485     ready = false;
486     }
487     else
488     {
489     AppendText(tekst);
490     }
491     }
492 kevin 153 Sleep(10); //Small delay to avoid busy wait
493 kevin 144 }
494     Sleep(100); //Give the modem a chance to send the last data
495     while(Serial.getComstat().cbInQue > 0)
496     {
497 kevin 132 Serial.readByte(); //Flush the incoming queue
498 kevin 144 Sleep(1);
499 kevin 132 }
500 kevin 121 OnBnClickedStart();
501 kevin 105 }
502 kevin 110 void CFlisServerDlg::DeleteSms()
503     {
504     vector<unsigned char> atcommand;
505     atcommand.push_back('a');
506     atcommand.push_back('t');
507     atcommand.push_back('+');
508     atcommand.push_back('c');
509     atcommand.push_back('m');
510     atcommand.push_back('g');
511     atcommand.push_back('d');
512     atcommand.push_back('=');
513     atcommand.push_back('1');
514     atcommand.push_back(',');
515     atcommand.push_back('3');
516    
517     writeFrame(atcommand);
518     Sleep(500);
519     }
520 kevin 136 UINT threadWrapper(LPVOID thread)
521 kevin 110 {
522     CFlisServerDlg *t = (CFlisServerDlg*) thread;
523     t->runthread();
524     return 0;
525     }
526    
527     void CFlisServerDlg::startthread()
528     {
529     AfxBeginThread(threadWrapper, (LPVOID) this);
530     }
531    
532     void CFlisServerDlg::runthread()
533     {
534 kevin 113 while (continueThread != 0)
535 kevin 110 {
536 kevin 168 MyMainThread();
537 kevin 110 }
538     }
539 kevin 168 void CFlisServerDlg::MyMainThread()
540 kevin 110 {
541 kevin 113 if(Serial.getComstat().cbInQue > 0)
542 kevin 110 {
543 kevin 113 Sleep(250);
544 kevin 110 std::vector<unsigned char> answer = readFrame();
545     Sleep(500);
546     CString tekst, oldtekst;
547    
548     for (int i=0; i<answer.size(); i++)
549     {
550 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
551 kevin 110 {
552 kevin 136 tekst.AppendChar(answer[i]);
553 kevin 110 }
554     }
555     tekst.Append(":");
556    
557     CString command;
558 kevin 144 bool plus = false;
559 kevin 110 int pos = tekst.Find('+',0);
560     if (pos != -1)
561     {
562     plus = true;
563     tekst = tekst.Right( tekst.GetLength() - pos -1);
564     pos = tekst.Find(':');
565     command = tekst.Left(pos);
566     tekst = tekst.Right( tekst.GetLength() - pos -1);
567     }
568    
569    
570 kevin 138 if(tekst.MakeLower() == "ok")
571 kevin 110 {
572 kevin 136 AppendText("OK tekst modtaget");
573 kevin 110 }
574 kevin 138 else if (tekst.MakeLower() == "error")
575 kevin 110 {
576 kevin 136 CString send;
577     send.Append("error tekst");
578     send.Append("\r\n");
579     send.Append(tekst);
580     AppendText(send);
581 kevin 110 }
582 kevin 136 else if (plus == true)
583 kevin 110 {
584 kevin 138 if (command.MakeLower() == "cmti")
585 kevin 110 {
586 kevin 113 CString smscount, oldteskst;
587 kevin 110 int pos = tekst.Find(',',0);
588     if (pos != -1)
589     {
590     smscount = tekst.Right( tekst.GetLength() - pos -1);
591     smscount.Remove(':');
592     ResetSms++;
593     }
594     ////////////////Read sms ting//////////////////
595     std::vector<unsigned char> data;
596     data.push_back('a');
597     data.push_back('t');
598     data.push_back('+');
599     data.push_back('c');
600     data.push_back('m');
601     data.push_back('g');
602     data.push_back('r');
603     data.push_back('=');
604    
605     for (int i=0; i< smscount.GetLength(); i++)
606     {
607     data.push_back(smscount[i]);
608     }
609 kevin 132 m_Textwindow.GetWindowText(oldteskst);
610     oldteskst.Append("\r\n");
611 kevin 113 for (int i=0; i<data.size();i++)
612     {
613 kevin 132 oldteskst.AppendChar(data[i]);
614 kevin 113 }
615     m_Textwindow.SetWindowText(oldteskst);
616 kevin 110 writeFrame(data);
617 kevin 113 Sleep(200);
618 kevin 110 ReadSms();
619 kevin 121 ResetSms++;
620 kevin 110
621     }
622 kevin 138 else if(command.MakeLower() == "wind")
623 kevin 113 {
624 kevin 136
625     CString send;
626     send.Append("Wind modtaget");
627     send.Append("\r\n");
628     send.Append(tekst);
629     AppendText(send);
630 kevin 113 }
631 kevin 110 }
632 kevin 132 else
633 kevin 110 {
634 kevin 144 AppendText(tekst);
635 kevin 132 }
636     if(ResetSms == 20)
637     {
638 kevin 110 DeleteSms();
639 kevin 136 AppendText("Sms'er slettet da vi nåede grænsen");
640 kevin 110 }
641 kevin 113
642     }
643 kevin 136 if (continueThread == 1)
644 kevin 113 {
645 kevin 110
646 kevin 121 CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
647 kevin 130 int commandtest = 0;
648     int iAll = 1;
649 kevin 113 std::vector<Commands> data;
650 kevin 136 data = DBReadCommands();
651 kevin 113 for (int i=0; i<data.size(); i++)
652     {
653 kevin 136 sIDnr = data[i].IDnr;
654     sCommandID = data[i].CommandID;
655 kevin 130 commandtest = atoi(data[i].CommandID);
656 kevin 138 sInstallationsID = "0";
657 kevin 130
658     if(commandtest > 1)
659     {
660 kevin 136 sInstallationsID = data[i].InstallationsID;
661 kevin 130 }
662 kevin 113 }
663 kevin 138
664 kevin 130 if (commandtest > 1)
665     {
666 kevin 132 iAll = 0;
667 kevin 130 }
668 kevin 113
669     if (sIDnr.GetLength() > 0)
670     {
671 kevin 136 SendConfig(sIDnr,sCommandID,sInstallationsID);
672 kevin 113 }
673     Sleep(500);
674 kevin 144
675     CheckAcknowledges();
676     Sleep(500);
677 kevin 110 }
678 kevin 144 CTime now = CTime::GetCurrentTime();
679    
680     CTimeSpan elapsed = now-Alive.tid;
681    
682     if (elapsed.GetTotalSeconds() >= 900)
683     {
684     keepaliveandread();
685     }
686 kevin 113 }
687 kevin 136 void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
688 kevin 144 {
689 kevin 130
690 kevin 113 std::vector<Installation> inst;
691 kevin 144
692     if (CommandID == "1")
693     inst = DBReadPhone("0");
694     else
695     inst = DBReadPhone(InstallationsID);
696    
697     for ( int j=0; j < inst.size(); j++)
698 kevin 113 {
699 kevin 130 CString TlfNr, Imei, updaterate;
700     TlfNr.Empty();
701     Imei.Empty();
702     updaterate.Empty();
703    
704 kevin 144 CString dataen = inst[j].InstPhoneNr;
705 kevin 113 TlfNr.Append(dataen);
706 kevin 144 CString Imeidata = inst[j].Imei;
707 kevin 121 Imei.Append(Imeidata);
708 kevin 144 CString updaterat = inst[j].Updaterate;
709 kevin 121 updaterate.Append(updaterat);
710 kevin 130
711 kevin 144 vector<unsigned char> tlfnr;
712     for (int i=0; i<TlfNr.GetLength(); i++)
713     {
714     tlfnr.push_back(TlfNr[i]);
715     }
716    
717     int calcimei;
718     __int64 buf;
719    
720     buf = atof(Imei);
721    
722     calcimei = tversum(buf);
723    
724     Imei.Format("%d",calcimei);
725    
726     SendSmsHead(tlfnr);
727     Sleep(250);
728    
729     vector<unsigned char> smsdata;
730    
731     for (int i=0; i<Imei.GetLength(); i++)
732     {
733     smsdata.push_back(Imei[i]);
734     }
735     smsdata.push_back(':');
736     for (int i=0; i<Alive.Phonenr.GetLength(); i++)
737     {
738     smsdata.push_back(Alive.Phonenr[i]);
739     }
740     smsdata.push_back(':');
741     for (int i=0; i<updaterate.GetLength(); i++)
742     {
743     smsdata.push_back(updaterate[i]);
744     }
745    
746     SendSmsData(smsdata);
747     Sleep(500);
748    
749     if (IDnr != "0")
750     {
751     Acks.push_back( Acknowledge(inst[j].instID,TlfNr) );
752     }
753    
754 kevin 113 }
755    
756 kevin 144 if (IDnr != "0")
757 kevin 121 {
758 kevin 130 CString SQL, Textwindow;
759     SQL.Format("update command set executed=now() where id=%s",IDnr);
760     db.ExecuteSQL(SQL);
761 kevin 166 Sleep(100);
762 kevin 136 AppendText("Command executed");
763 kevin 144 }
764     Sleep(150);
765 kevin 113
766 kevin 144
767    
768    
769 kevin 113 }
770     vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
771     {
772     vector<Installation> buffer;
773    
774 kevin 144 CString SQL, phonenr, imei, updaterate, id;
775 kevin 130 Installation Myinst;
776 kevin 144
777     if (sInstallationsID != "0")
778 kevin 130 {
779 kevin 144 SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
780 kevin 130 }
781 kevin 144 else
782 kevin 130 {
783 kevin 144 SQL.Format("select installationphonenr, imei, updaterate, id from installation");
784 kevin 130 }
785 kevin 144
786 kevin 113 CRecordset rs(&db);
787     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
788     if (rs.GetRecordCount()>0)
789     {
790     rs.MoveFirst();
791     while(!rs.IsEOF())
792     {
793 kevin 130
794     rs.GetFieldValue((short)0,phonenr);
795     rs.GetFieldValue(1,imei);
796     rs.GetFieldValue(2,updaterate);
797 kevin 144 rs.GetFieldValue(3,id);
798 kevin 113
799 kevin 130 Myinst.InstPhoneNr = phonenr;
800     Myinst.Imei = imei;
801     Myinst.Updaterate = updaterate;
802 kevin 144 Myinst.instID = id;
803 kevin 113
804 kevin 130 buffer.push_back(Myinst);
805     rs.MoveNext();
806 kevin 113 }
807     }
808     rs.Close();
809     return buffer;
810 kevin 121 }
811 kevin 130 int CFlisServerDlg::tversum(__int64 input)
812 kevin 121 {
813     int sum = 0;
814     while (input > 0)
815     {
816     sum += (input %10);
817     input /= 10;
818     }
819     return sum;
820     }
821     void CFlisServerDlg::OnBnClickedStart()
822     {
823     // TODO: Add your control notification handler code here
824 kevin 144
825 kevin 121 continueThread = 1;
826 kevin 144 keepaliveandread();
827     Sleep(150);
828     m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr );
829     AppendText("Started");
830 kevin 121 AfxBeginThread(threadWrapper,AfxGetMainWnd());
831     }
832 kevin 136 void CFlisServerDlg::AppendText(CString s)
833     {
834     CString Tekst;
835     m_Textwindow.GetWindowText(Tekst);
836     Tekst.Append("\r\n");
837     Tekst.Append(s);
838     m_Textwindow.SetWindowText(Tekst);
839 kevin 168 int g = m_Textwindow.GetScrollLimit(SB_VERT);
840     if (g > 0)
841     {
842     m_Textwindow.SetScrollPos(g,1,false);
843     }
844 kevin 136
845     }
846     void ConfigFile::ReadSettings()
847     {
848     ifstream file("Server-Settings.ini");
849     if (!file.is_open())
850     throw("Could not open file");
851    
852     char buf[200];
853     while (!file.eof() ) {
854     file.getline(buf,200);
855     CString tmp(buf);
856    
857     if (tmp.GetAt(0) == '#')
858     continue;
859    
860     int pos = tmp.Find('=');
861     if (pos>0) {
862     CString key = tmp.Left(pos).Trim().MakeLower();;
863     CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
864    
865     if (key == "host")
866     host = value;
867     else if (key == "username")
868     username = value;
869     else if (key == "password")
870     password = value;
871     else if (key == "database")
872     database = value;
873     else if (key == "comport")
874     comport = value;
875     }
876    
877     }
878    
879     file.close();
880 kevin 144 }
881     void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
882     {
883     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
884     {
885     Acknowledge& current = (*it);
886     if (current.sTlfNr == tlfnr)
887     {
888     CString SQL;
889     SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID);
890     db.ExecuteSQL(SQL);
891    
892     Acks.erase(it);
893    
894     return;
895     }
896     }
897     }
898    
899     void CFlisServerDlg::CheckAcknowledges()
900     {
901     CTime now = CTime::GetCurrentTime();
902     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
903     {
904     Acknowledge& current = (*it);
905    
906     CTimeSpan elapsed = now-current.cTime;
907    
908     if (elapsed.GetTotalSeconds() >= 300)
909     {
910     DumpAckList();
911     if (current.iRetry >= 1)
912     {
913     CString SQL;
914     SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID);
915     db.ExecuteSQL(SQL);
916     Acks.erase(it);
917     return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again
918     }
919     else
920     {
921     SendConfig("0","2",current.sInstallationID);
922     current.iRetry++;
923     current.cTime = CTime::GetCurrentTime();
924     AppendText( CString("Retry send config to ") + current.sTlfNr );
925     }
926     }
927     }
928     }
929    
930     void CFlisServerDlg::DumpAckList()
931     {
932     CTime now = CTime::GetCurrentTime();
933     OutputDebugString("-------------------------------\n");
934     int count = 0;
935     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
936     {
937     CString msg;
938     msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr);
939     OutputDebugString(msg);
940     count++;
941     }
942     }
943     vector<keepalive> CFlisServerDlg::keepaliveandread(void)
944     {
945     vector<keepalive> buffer;
946    
947     CString SQL, name, value;
948     SQL = "select name, value from config;";
949     CRecordset rs(&db);
950 kevin 146 try
951 kevin 144 {
952 kevin 146 rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
953     if (rs.GetRecordCount()>0)
954     {
955     rs.MoveFirst();
956 kevin 144
957 kevin 146 rs.GetFieldValue((short)0, name);
958     rs.GetFieldValue(1,value);
959 kevin 144
960 kevin 146 Alive.name = name;
961     Alive.value = value;
962     Alive.tid = CTime::GetCurrentTime();
963     if(Alive.name == "phonenr")
964     {
965     Alive.Phonenr = value;
966     }
967    
968     buffer.push_back(Alive);
969     rs.MoveNext();
970    
971     }
972     rs.Close();
973     return buffer;
974     }
975     catch (char * str)
976     {
977     AppendText("Keepalive failed, closing and opening the db connection again");
978     if(db.IsOpen())
979 kevin 144 {
980 kevin 146 db.Close();
981     AppendText("DB connection closed");
982 kevin 144 }
983 kevin 146 if(!db.IsOpen())
984     {
985     DBConnect();
986     AppendText("DB connection started again");
987     }
988 kevin 168 return buffer;
989 kevin 146 }
990 kevin 144
991    
992 kevin 168 }

  ViewVC Help
Powered by ViewVC 1.1.20