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

Annotation of /trunk/FlisServer/FlisServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20