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

Annotation of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 174 - (hide annotations) (download)
Thu Dec 6 12:53:06 2007 UTC (16 years, 5 months ago) by kevin
File size: 22213 byte(s)
updated the serversoftware with some fancy stuff torben found
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 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 174 bool ready = true;
228 kevin 81 CString tekst;
229 kevin 70 std::vector<unsigned char> data;
230 kevin 174
231 kevin 70 data.push_back('a');
232     data.push_back('t');
233     data.push_back('+');
234     data.push_back('c');
235     data.push_back('p');
236     data.push_back('i');
237     data.push_back('n');
238 kevin 174 data.push_back('?');
239 kevin 70
240     writeFrame(data);
241 kevin 174 Sleep(100);
242     if(Serial.getComstat().cbInQue > 0)
243     {
244     std::vector<unsigned char> answer = readFrame();
245     Sleep(50);
246    
247     for (int i=0; i<answer.size(); i++)
248     {
249     if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
250     {
251     tekst.AppendChar(answer[i]);
252     }
253     }
254     AppendText(tekst);
255     }
256     Sleep(5);
257     if (tekst.MakeLower() == "+cpin: sim pin")
258     {
259     data.clear();
260     data.push_back('a');
261     data.push_back('t');
262     data.push_back('+');
263     data.push_back('c');
264     data.push_back('p');
265     data.push_back('i');
266     data.push_back('n');
267     data.push_back('=');
268     data.push_back('2');
269     data.push_back('5');
270     data.push_back('9');
271     data.push_back('5');
272    
273     writeFrame(data);
274    
275     Sleep(500);
276    
277     while (ready == true)
278     {
279     if (Serial.getComstat().cbInQue > 120)
280     {
281     std::vector<unsigned char> answer = readFrame();
282     CString tekst;
283    
284     for (int i=0; i<answer.size(); i++)
285     {
286     if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
287     {
288     tekst.AppendChar(answer[i]);
289     }
290     }
291     int n = tekst.Find("WIND: 11",0);
292     if (n > -1)
293     {
294     ready = false;
295     }
296     else
297     {
298     AppendText(tekst);
299     }
300     }
301     Sleep(10); //Small delay to avoid busy wait
302     }
303     }
304     else if (tekst.MakeLower() == "+cpin: ready")
305     {
306     AppendText("No pincode needed");
307     }
308     else
309     {
310     AppendText("Error in pincode message");
311     }
312 kevin 70 }
313     void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data)
314 kevin 103 {
315    
316 kevin 70 for (int i=0; i<data.size(); i++)
317     {
318     Serial.writeByte( data[i] );
319     Sleep(5);
320     }
321     Serial.writeByte(0x1A);
322 kevin 105 Sleep(3000);
323     if(Serial.getComstat().cbInQue > 0)
324     {
325     CString tekst;
326     std::vector<unsigned char> answer = readFrame();
327     Sleep(50);
328 kevin 136
329     for (int i=0; i<answer.size(); i++)
330 kevin 105 {
331 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
332 kevin 105 {
333 kevin 136 tekst.AppendChar(answer[i]);
334 kevin 105 }
335     }
336 kevin 136 AppendText(tekst);
337 kevin 105 }
338 kevin 70
339     }
340 kevin 136 void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr)
341 kevin 70 {
342 kevin 103 vector<unsigned char> atcommand;
343     atcommand.push_back('a');
344     atcommand.push_back('t');
345     atcommand.push_back('+');
346     atcommand.push_back('c');
347     atcommand.push_back('m');
348     atcommand.push_back('g');
349     atcommand.push_back('s');
350     atcommand.push_back('=');
351     atcommand.push_back('"');
352     int s = (atcommand.size() -1 );
353    
354     for (int i=0; i<(atcommand.size()); i++)
355     {
356     Serial.writeByte( atcommand[i] );
357     Sleep(5);
358     }
359    
360 kevin 136 for (int i=0; i<tlfnr.size(); i++)
361 kevin 70 {
362 kevin 136 Serial.writeByte( tlfnr[i] );
363 kevin 70 Sleep(5);
364     }
365 kevin 103 Serial.writeByte(atcommand[s]);
366 kevin 70 Serial.writeByte(0x0D);
367 kevin 103 Sleep(250);
368 kevin 70 }
369 kevin 81 void CFlisServerDlg::DBConnect()
370 kevin 70 {
371     CString dsn;
372 kevin 136 ConfigFile config;
373     try {
374     config.ReadSettings();
375     } catch(...) {
376     MessageBox("Could not open config file");
377     }
378    
379     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);
380 kevin 103 db.OpenEx(dsn, CDatabase::noOdbcDialog);
381 kevin 81 }
382 kevin 136 vector<Commands> CFlisServerDlg::DBReadCommands(void)
383 kevin 81 {
384 kevin 103 vector<Commands> buffer;
385 kevin 81
386 kevin 103 CString SQL, IDnr, CommandID, InstallationsID;
387 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;";
388 kevin 103 CRecordset rs(&db);
389     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
390     if (rs.GetRecordCount()>0)
391     {
392     rs.MoveFirst();
393 kevin 130
394 kevin 136 Commands Mycom;
395     rs.GetFieldValue((short)0, IDnr);
396     rs.GetFieldValue(3, CommandID);
397     rs.GetFieldValue(4, InstallationsID);
398    
399     Mycom.IDnr = IDnr;
400     Mycom.CommandID = CommandID;
401     Mycom.InstallationsID = InstallationsID;
402 kevin 103
403 kevin 136 buffer.push_back(Mycom);
404     rs.MoveNext();
405    
406 kevin 103 }
407     rs.Close();
408     return buffer;
409 kevin 81 }
410     void CFlisServerDlg::ReadSms()
411     {
412     CString tekst, oldtekst;
413 kevin 136 Sleep(950); //Holder en pause for at lade hele sms'en komme ind i serial køen.
414 kevin 81 if(Serial.getComstat().cbInQue > 0)
415     {
416     std::vector<unsigned char> answer = readFrame();
417 kevin 138 Sleep(50);
418 kevin 136
419 kevin 81 for (int i=0; i<answer.size(); i++)
420     {
421 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
422 kevin 81 {
423 kevin 136 tekst.AppendChar(answer[i]);
424 kevin 81 }
425     }
426    
427 kevin 136 AppendText(tekst);
428 kevin 81 SmsSplit(tekst);
429     }
430     }
431     void CFlisServerDlg::SmsSplit(CString data)
432     {
433     CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst;
434     char CharData[150];
435     strcpy(CharData,data);
436 kevin 136 TlfNr = data.Mid(24,8);
437 kevin 138 FyrData = data.Mid(57,data.GetLength()-59);
438 kevin 81 FyrData.Append(":");
439 kevin 144
440     if (FyrData.MakeLower() == "conf ok:")
441     {
442     HandleAcknowledge(TlfNr);
443     return;
444     }
445 kevin 166 CAtlRegExp<> regex;
446     if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK)
447     {
448    
449     CAtlREMatchContext<> pContext;
450    
451     if (!regex.Match(FyrData, &pContext))
452     {
453     AppendText("Invalid SMS recieved");
454     return;
455     }
456     }
457     else
458     {
459     AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske !
460     }
461    
462 kevin 81 SmsCount = Splitter(FyrData);
463     Temper = Splitter(FyrData);
464     Flamme = Splitter(FyrData);
465     Flis = Splitter(FyrData);
466     FremFejl = Splitter(FyrData);
467     PowerFail = Splitter(FyrData);
468 kevin 132
469     CString SQL, Textwindow, InstallNR;
470     SQL.Format("select ID from installation where installationphonenr=%s",TlfNr);
471    
472     CRecordset rs(&db);
473     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
474     if (rs.GetRecordCount()>0)
475     {
476     rs.MoveFirst();
477     rs.GetFieldValue((short)0,InstallNR);
478     }
479     rs.Close();
480    
481     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);
482 kevin 138 try
483     {
484 kevin 132 db.ExecuteSQL(SQL);
485 kevin 138 }
486     catch(CDBException* e)
487     {
488     MessageBox(e->m_strError);
489     }
490 kevin 136 AppendText("Sms added to Log");
491 kevin 132 Sleep(150);
492 kevin 81 }
493     CString CFlisServerDlg::Splitter(CString& fyrdata)
494     {
495     CString Output;
496    
497     int pos = fyrdata.Find(':',0);
498     if (pos != -1)
499     {
500     Output = fyrdata.Left(pos);
501     fyrdata = fyrdata.Right( fyrdata.GetLength() - pos -1);
502     }
503     return Output;
504 kevin 103 }
505     void CFlisServerDlg::OnBnClickedClose()
506     {
507     // TODO: Add your control notification handler code here
508 kevin 113 continueThread = 0;
509 kevin 166 Sleep(250);
510 kevin 132 DeleteSms();
511 kevin 113
512     Sleep(500);
513 kevin 103 if( Serial.isOpen() )
514     {
515     Serial.close();
516     }
517    
518     if(db.IsOpen())
519     {
520 kevin 132 db.Close();
521 kevin 103 }
522 kevin 113
523 kevin 103 OnOK();
524 kevin 113
525 kevin 103 }
526 kevin 110 void CFlisServerDlg::DeleteSms()
527     {
528     vector<unsigned char> atcommand;
529     atcommand.push_back('a');
530     atcommand.push_back('t');
531     atcommand.push_back('+');
532     atcommand.push_back('c');
533     atcommand.push_back('m');
534     atcommand.push_back('g');
535     atcommand.push_back('d');
536     atcommand.push_back('=');
537     atcommand.push_back('1');
538     atcommand.push_back(',');
539     atcommand.push_back('3');
540    
541     writeFrame(atcommand);
542     Sleep(500);
543     }
544 kevin 136 UINT threadWrapper(LPVOID thread)
545 kevin 110 {
546     CFlisServerDlg *t = (CFlisServerDlg*) thread;
547     t->runthread();
548     return 0;
549     }
550    
551     void CFlisServerDlg::startthread()
552     {
553     AfxBeginThread(threadWrapper, (LPVOID) this);
554     }
555    
556     void CFlisServerDlg::runthread()
557     {
558 kevin 113 while (continueThread != 0)
559 kevin 110 {
560 kevin 168 MyMainThread();
561 kevin 110 }
562     }
563 kevin 168 void CFlisServerDlg::MyMainThread()
564 kevin 110 {
565 kevin 113 if(Serial.getComstat().cbInQue > 0)
566 kevin 110 {
567 kevin 113 Sleep(250);
568 kevin 110 std::vector<unsigned char> answer = readFrame();
569     Sleep(500);
570     CString tekst, oldtekst;
571    
572     for (int i=0; i<answer.size(); i++)
573     {
574 kevin 136 if ((answer[i] != 0x0A) && (answer[i] != 0x0D))
575 kevin 110 {
576 kevin 136 tekst.AppendChar(answer[i]);
577 kevin 110 }
578     }
579     tekst.Append(":");
580    
581     CString command;
582 kevin 144 bool plus = false;
583 kevin 110 int pos = tekst.Find('+',0);
584     if (pos != -1)
585     {
586     plus = true;
587     tekst = tekst.Right( tekst.GetLength() - pos -1);
588     pos = tekst.Find(':');
589     command = tekst.Left(pos);
590     tekst = tekst.Right( tekst.GetLength() - pos -1);
591     }
592    
593    
594 kevin 138 if(tekst.MakeLower() == "ok")
595 kevin 110 {
596 kevin 136 AppendText("OK tekst modtaget");
597 kevin 110 }
598 kevin 138 else if (tekst.MakeLower() == "error")
599 kevin 110 {
600 kevin 136 CString send;
601     send.Append("error tekst");
602     send.Append("\r\n");
603     send.Append(tekst);
604     AppendText(send);
605 kevin 110 }
606 kevin 136 else if (plus == true)
607 kevin 110 {
608 kevin 138 if (command.MakeLower() == "cmti")
609 kevin 110 {
610 kevin 113 CString smscount, oldteskst;
611 kevin 110 int pos = tekst.Find(',',0);
612     if (pos != -1)
613     {
614     smscount = tekst.Right( tekst.GetLength() - pos -1);
615     smscount.Remove(':');
616     ResetSms++;
617     }
618     ////////////////Read sms ting//////////////////
619     std::vector<unsigned char> data;
620     data.push_back('a');
621     data.push_back('t');
622     data.push_back('+');
623     data.push_back('c');
624     data.push_back('m');
625     data.push_back('g');
626     data.push_back('r');
627     data.push_back('=');
628    
629     for (int i=0; i< smscount.GetLength(); i++)
630     {
631     data.push_back(smscount[i]);
632     }
633 kevin 132 m_Textwindow.GetWindowText(oldteskst);
634     oldteskst.Append("\r\n");
635 kevin 113 for (int i=0; i<data.size();i++)
636     {
637 kevin 132 oldteskst.AppendChar(data[i]);
638 kevin 113 }
639     m_Textwindow.SetWindowText(oldteskst);
640 kevin 110 writeFrame(data);
641 kevin 113 Sleep(200);
642 kevin 110 ReadSms();
643 kevin 121 ResetSms++;
644 kevin 110
645     }
646 kevin 138 else if(command.MakeLower() == "wind")
647 kevin 113 {
648 kevin 136
649     CString send;
650     send.Append("Wind modtaget");
651     send.Append("\r\n");
652     send.Append(tekst);
653     AppendText(send);
654 kevin 113 }
655 kevin 110 }
656 kevin 132 else
657 kevin 110 {
658 kevin 144 AppendText(tekst);
659 kevin 132 }
660     if(ResetSms == 20)
661     {
662 kevin 110 DeleteSms();
663 kevin 136 AppendText("Sms'er slettet da vi nåede grænsen");
664 kevin 110 }
665 kevin 113
666     }
667 kevin 136 if (continueThread == 1)
668 kevin 113 {
669 kevin 110
670 kevin 121 CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei;
671 kevin 130 int commandtest = 0;
672     int iAll = 1;
673 kevin 113 std::vector<Commands> data;
674 kevin 136 data = DBReadCommands();
675 kevin 113 for (int i=0; i<data.size(); i++)
676     {
677 kevin 136 sIDnr = data[i].IDnr;
678     sCommandID = data[i].CommandID;
679 kevin 130 commandtest = atoi(data[i].CommandID);
680 kevin 138 sInstallationsID = "0";
681 kevin 130
682     if(commandtest > 1)
683     {
684 kevin 136 sInstallationsID = data[i].InstallationsID;
685 kevin 130 }
686 kevin 113 }
687 kevin 138
688 kevin 130 if (commandtest > 1)
689     {
690 kevin 132 iAll = 0;
691 kevin 130 }
692 kevin 113
693     if (sIDnr.GetLength() > 0)
694     {
695 kevin 136 SendConfig(sIDnr,sCommandID,sInstallationsID);
696 kevin 113 }
697 kevin 174 Sleep(300);
698 kevin 144
699     CheckAcknowledges();
700     Sleep(500);
701 kevin 110 }
702 kevin 144 CTime now = CTime::GetCurrentTime();
703    
704     CTimeSpan elapsed = now-Alive.tid;
705    
706     if (elapsed.GetTotalSeconds() >= 900)
707     {
708     keepaliveandread();
709     }
710 kevin 113 }
711 kevin 136 void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID)
712 kevin 144 {
713 kevin 130
714 kevin 113 std::vector<Installation> inst;
715 kevin 144
716     if (CommandID == "1")
717     inst = DBReadPhone("0");
718     else
719     inst = DBReadPhone(InstallationsID);
720    
721     for ( int j=0; j < inst.size(); j++)
722 kevin 113 {
723 kevin 130 CString TlfNr, Imei, updaterate;
724     TlfNr.Empty();
725     Imei.Empty();
726     updaterate.Empty();
727    
728 kevin 144 CString dataen = inst[j].InstPhoneNr;
729 kevin 113 TlfNr.Append(dataen);
730 kevin 144 CString Imeidata = inst[j].Imei;
731 kevin 121 Imei.Append(Imeidata);
732 kevin 144 CString updaterat = inst[j].Updaterate;
733 kevin 121 updaterate.Append(updaterat);
734 kevin 130
735 kevin 144 vector<unsigned char> tlfnr;
736     for (int i=0; i<TlfNr.GetLength(); i++)
737     {
738     tlfnr.push_back(TlfNr[i]);
739     }
740    
741     int calcimei;
742     __int64 buf;
743    
744     buf = atof(Imei);
745    
746     calcimei = tversum(buf);
747    
748     Imei.Format("%d",calcimei);
749    
750     SendSmsHead(tlfnr);
751     Sleep(250);
752    
753     vector<unsigned char> smsdata;
754    
755     for (int i=0; i<Imei.GetLength(); i++)
756     {
757     smsdata.push_back(Imei[i]);
758     }
759     smsdata.push_back(':');
760     for (int i=0; i<Alive.Phonenr.GetLength(); i++)
761     {
762     smsdata.push_back(Alive.Phonenr[i]);
763     }
764     smsdata.push_back(':');
765     for (int i=0; i<updaterate.GetLength(); i++)
766     {
767     smsdata.push_back(updaterate[i]);
768     }
769    
770     SendSmsData(smsdata);
771     Sleep(500);
772    
773     if (IDnr != "0")
774     {
775     Acks.push_back( Acknowledge(inst[j].instID,TlfNr) );
776     }
777    
778 kevin 113 }
779    
780 kevin 144 if (IDnr != "0")
781 kevin 121 {
782 kevin 130 CString SQL, Textwindow;
783     SQL.Format("update command set executed=now() where id=%s",IDnr);
784     db.ExecuteSQL(SQL);
785 kevin 166 Sleep(100);
786 kevin 136 AppendText("Command executed");
787 kevin 144 }
788     Sleep(150);
789 kevin 113
790 kevin 144
791    
792    
793 kevin 113 }
794     vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID)
795     {
796     vector<Installation> buffer;
797    
798 kevin 144 CString SQL, phonenr, imei, updaterate, id;
799 kevin 130 Installation Myinst;
800 kevin 144
801     if (sInstallationsID != "0")
802 kevin 130 {
803 kevin 144 SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID);
804 kevin 130 }
805 kevin 144 else
806 kevin 130 {
807 kevin 144 SQL.Format("select installationphonenr, imei, updaterate, id from installation");
808 kevin 130 }
809 kevin 144
810 kevin 113 CRecordset rs(&db);
811     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
812     if (rs.GetRecordCount()>0)
813     {
814     rs.MoveFirst();
815     while(!rs.IsEOF())
816     {
817 kevin 130
818     rs.GetFieldValue((short)0,phonenr);
819     rs.GetFieldValue(1,imei);
820     rs.GetFieldValue(2,updaterate);
821 kevin 144 rs.GetFieldValue(3,id);
822 kevin 113
823 kevin 130 Myinst.InstPhoneNr = phonenr;
824     Myinst.Imei = imei;
825     Myinst.Updaterate = updaterate;
826 kevin 144 Myinst.instID = id;
827 kevin 113
828 kevin 130 buffer.push_back(Myinst);
829     rs.MoveNext();
830 kevin 113 }
831     }
832     rs.Close();
833     return buffer;
834 kevin 121 }
835 kevin 130 int CFlisServerDlg::tversum(__int64 input)
836 kevin 121 {
837     int sum = 0;
838     while (input > 0)
839     {
840     sum += (input %10);
841     input /= 10;
842     }
843     return sum;
844     }
845     void CFlisServerDlg::OnBnClickedStart()
846     {
847     // TODO: Add your control notification handler code here
848 kevin 174
849     m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar, hvis pinkode er nødvendig");
850     UpdateWindow();
851     SetPin();
852    
853     Sleep(100); //Give the modem a chance to send the last data
854     while(Serial.getComstat().cbInQue > 0)
855     {
856     Serial.readByte(); //Flush the incoming queue
857     Sleep(1);
858     }
859 kevin 144
860 kevin 121 continueThread = 1;
861 kevin 144 keepaliveandread();
862     Sleep(150);
863     m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr );
864     AppendText("Started");
865 kevin 121 AfxBeginThread(threadWrapper,AfxGetMainWnd());
866     }
867 kevin 136 void CFlisServerDlg::AppendText(CString s)
868     {
869     CString Tekst;
870     m_Textwindow.GetWindowText(Tekst);
871     Tekst.Append("\r\n");
872     Tekst.Append(s);
873     m_Textwindow.SetWindowText(Tekst);
874 kevin 174 Sleep(5);
875 kevin 168 int g = m_Textwindow.GetScrollLimit(SB_VERT);
876     if (g > 0)
877     {
878 kevin 174 m_Textwindow.SetSel(m_Textwindow.GetWindowTextLength() -1,m_Textwindow.GetWindowTextLength(),false);
879 kevin 168 }
880 kevin 136
881     }
882     void ConfigFile::ReadSettings()
883     {
884     ifstream file("Server-Settings.ini");
885     if (!file.is_open())
886     throw("Could not open file");
887    
888     char buf[200];
889     while (!file.eof() ) {
890     file.getline(buf,200);
891     CString tmp(buf);
892    
893     if (tmp.GetAt(0) == '#')
894     continue;
895    
896     int pos = tmp.Find('=');
897     if (pos>0) {
898     CString key = tmp.Left(pos).Trim().MakeLower();;
899     CString value = tmp.Right(tmp.GetLength()-pos-1).Trim();
900    
901     if (key == "host")
902     host = value;
903     else if (key == "username")
904     username = value;
905     else if (key == "password")
906     password = value;
907     else if (key == "database")
908     database = value;
909     else if (key == "comport")
910     comport = value;
911     }
912    
913     }
914    
915     file.close();
916 kevin 144 }
917     void CFlisServerDlg::HandleAcknowledge(CString tlfnr)
918     {
919     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
920     {
921     Acknowledge& current = (*it);
922     if (current.sTlfNr == tlfnr)
923     {
924     CString SQL;
925     SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID);
926     db.ExecuteSQL(SQL);
927    
928     Acks.erase(it);
929    
930     return;
931     }
932     }
933     }
934    
935     void CFlisServerDlg::CheckAcknowledges()
936     {
937     CTime now = CTime::GetCurrentTime();
938     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
939     {
940     Acknowledge& current = (*it);
941    
942     CTimeSpan elapsed = now-current.cTime;
943    
944     if (elapsed.GetTotalSeconds() >= 300)
945     {
946     DumpAckList();
947     if (current.iRetry >= 1)
948     {
949     CString SQL;
950     SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID);
951     db.ExecuteSQL(SQL);
952     Acks.erase(it);
953     return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again
954     }
955     else
956     {
957     SendConfig("0","2",current.sInstallationID);
958     current.iRetry++;
959     current.cTime = CTime::GetCurrentTime();
960     AppendText( CString("Retry send config to ") + current.sTlfNr );
961     }
962     }
963     }
964     }
965    
966 kevin 174 void CFlisServerDlg::DumpAckList() /* Debug tool */
967 kevin 144 {
968 kevin 174
969 kevin 144 CTime now = CTime::GetCurrentTime();
970     OutputDebugString("-------------------------------\n");
971     int count = 0;
972     for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it)
973     {
974     CString msg;
975     msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr);
976     OutputDebugString(msg);
977     count++;
978     }
979     }
980     vector<keepalive> CFlisServerDlg::keepaliveandread(void)
981     {
982     vector<keepalive> buffer;
983    
984     CString SQL, name, value;
985     SQL = "select name, value from config;";
986     CRecordset rs(&db);
987 kevin 146 try
988 kevin 144 {
989 kevin 146 rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
990     if (rs.GetRecordCount()>0)
991     {
992     rs.MoveFirst();
993 kevin 144
994 kevin 146 rs.GetFieldValue((short)0, name);
995     rs.GetFieldValue(1,value);
996 kevin 144
997 kevin 146 Alive.name = name;
998     Alive.value = value;
999     Alive.tid = CTime::GetCurrentTime();
1000     if(Alive.name == "phonenr")
1001     {
1002     Alive.Phonenr = value;
1003     }
1004    
1005     buffer.push_back(Alive);
1006     rs.MoveNext();
1007    
1008     }
1009     rs.Close();
1010     return buffer;
1011     }
1012     catch (char * str)
1013     {
1014     AppendText("Keepalive failed, closing and opening the db connection again");
1015     if(db.IsOpen())
1016 kevin 144 {
1017 kevin 146 db.Close();
1018     AppendText("DB connection closed");
1019 kevin 144 }
1020 kevin 146 if(!db.IsOpen())
1021     {
1022     DBConnect();
1023     AppendText("DB connection started again");
1024     }
1025 kevin 168 return buffer;
1026 kevin 146 }
1027 kevin 144
1028    
1029 kevin 174 }

  ViewVC Help
Powered by ViewVC 1.1.20