63 |
ON_WM_SYSCOMMAND() |
ON_WM_SYSCOMMAND() |
64 |
ON_WM_PAINT() |
ON_WM_PAINT() |
65 |
ON_WM_QUERYDRAGICON() |
ON_WM_QUERYDRAGICON() |
|
ON_MESSAGE(UWM_MYMESSAGE, OnShowString) |
|
66 |
//}}AFX_MSG_MAP |
//}}AFX_MSG_MAP |
67 |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
68 |
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) |
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) |
157 |
} |
} |
158 |
int CFlisServerDlg::StartSerial(void) |
int CFlisServerDlg::StartSerial(void) |
159 |
{ |
{ |
160 |
|
|
161 |
int Baud; |
int Baud; |
|
CString SerialPort = "COM3"; |
|
162 |
|
|
163 |
|
ConfigFile config; |
164 |
|
try { |
165 |
|
config.ReadSettings(); |
166 |
|
} catch(...) { |
167 |
|
MessageBox("Could not open config file"); |
168 |
|
OnOK(); |
169 |
|
return true; |
170 |
|
} |
171 |
|
|
172 |
Baud = 1200; |
Baud = 1200; |
173 |
if( Serial.isOpen() ){ |
if( Serial.isOpen() ) |
174 |
Serial.close(); |
{ |
175 |
Serial.open( SerialPort, Baud ); |
try |
176 |
|
{ |
177 |
|
Serial.close(); |
178 |
|
Serial.open( config.comport, Baud ); |
179 |
|
} |
180 |
|
catch (exception* e) |
181 |
|
{ |
182 |
|
MessageBox( "Serial.open() exception" ); |
183 |
|
return 0; |
184 |
|
} |
185 |
} |
} |
186 |
else { |
else |
187 |
Serial.open( SerialPort, Baud ); |
{ |
188 |
|
try |
189 |
|
{ |
190 |
|
Serial.open( config.comport, Baud ); |
191 |
|
} |
192 |
|
catch (exception* e) |
193 |
|
{ |
194 |
|
MessageBox( "Serial.open() exception 2" ); |
195 |
|
return 0; |
196 |
|
} |
197 |
} |
} |
198 |
|
|
199 |
|
|
200 |
return 0; |
return 0; |
201 |
} |
} |
204 |
std::vector<unsigned char> buf; |
std::vector<unsigned char> buf; |
205 |
while(Serial.getComstat().cbInQue > 0) |
while(Serial.getComstat().cbInQue > 0) |
206 |
{ |
{ |
207 |
unsigned char data = Serial.readByte(); |
unsigned char data = Serial.readByte(); |
208 |
|
|
209 |
buf.push_back(data); |
buf.push_back(data); |
210 |
} |
} |
211 |
return buf; |
return buf; |
212 |
} |
} |
240 |
data.push_back('5'); |
data.push_back('5'); |
241 |
|
|
242 |
writeFrame(data); |
writeFrame(data); |
|
/* Sleep(750); |
|
|
if(Serial.getComstat().cbInQue > 0) |
|
|
{ |
|
|
std::vector<unsigned char> answer = readFrame(); |
|
|
Sleep(50); |
|
|
char array1[25]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
|
|
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
|
|
{ |
|
|
tekst.AppendChar(array1[i]); |
|
|
} |
|
|
} |
|
|
m_Textwindow.SetWindowText(tekst); |
|
|
} |
|
|
*/ |
|
243 |
} |
} |
244 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
245 |
{ |
{ |
256 |
CString tekst; |
CString tekst; |
257 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
258 |
Sleep(50); |
Sleep(50); |
259 |
char array1[150]; |
|
260 |
int i; |
for (int i=0; i<answer.size(); i++) |
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
|
|
|
|
for (int i=0; i<answer.size(); i++) |
|
261 |
{ |
{ |
262 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
263 |
{ |
{ |
264 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
265 |
} |
} |
266 |
} |
} |
267 |
m_Textwindow.SetWindowText(tekst); |
AppendText(tekst); |
268 |
} |
} |
269 |
|
|
270 |
} |
} |
271 |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr) |
272 |
{ |
{ |
273 |
vector<unsigned char> atcommand; |
vector<unsigned char> atcommand; |
274 |
atcommand.push_back('a'); |
atcommand.push_back('a'); |
288 |
Sleep(5); |
Sleep(5); |
289 |
} |
} |
290 |
|
|
291 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<tlfnr.size(); i++) |
292 |
{ |
{ |
293 |
Serial.writeByte( data[i] ); |
Serial.writeByte( tlfnr[i] ); |
294 |
Sleep(5); |
Sleep(5); |
295 |
} |
} |
296 |
Serial.writeByte(atcommand[s]); |
Serial.writeByte(atcommand[s]); |
300 |
void CFlisServerDlg::DBConnect() |
void CFlisServerDlg::DBConnect() |
301 |
{ |
{ |
302 |
CString dsn; |
CString dsn; |
303 |
//dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //Torben |
ConfigFile config; |
304 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server |
try { |
305 |
|
config.ReadSettings(); |
306 |
|
} catch(...) { |
307 |
|
MessageBox("Could not open config file"); |
308 |
|
} |
309 |
|
|
310 |
|
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=%s; uid=%s;password=%s;database=%s;sslmode=prefer",config.host,config.username, config.password, config.database); |
311 |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
312 |
} |
} |
313 |
vector<Commands> CFlisServerDlg::DBRead(void) |
vector<Commands> CFlisServerDlg::DBReadCommands(void) |
314 |
{ |
{ |
315 |
vector<Commands> buffer; |
vector<Commands> buffer; |
316 |
|
|
317 |
CString SQL, IDnr, CommandID, InstallationsID; |
CString SQL, IDnr, CommandID, InstallationsID; |
318 |
SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL;"; |
SQL = "select id,date_trunc('second', created) as created,executed,commandid,installationid from command WHERE executed IS NULL ORDER BY created ASC LIMIT 1;"; |
319 |
CRecordset rs(&db); |
CRecordset rs(&db); |
320 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
321 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
322 |
{ |
{ |
323 |
rs.MoveFirst(); |
rs.MoveFirst(); |
|
while(!rs.IsEOF()) |
|
|
{ |
|
|
Commands Mycom; |
|
|
rs.GetFieldValue((short)0, IDnr); |
|
|
rs.GetFieldValue(3, CommandID); |
|
|
rs.GetFieldValue(4, InstallationsID); |
|
324 |
|
|
325 |
Mycom.IDnr = IDnr; |
Commands Mycom; |
326 |
Mycom.CommandID = CommandID; |
rs.GetFieldValue((short)0, IDnr); |
327 |
Mycom.InstallationsID = InstallationsID; |
rs.GetFieldValue(3, CommandID); |
328 |
|
rs.GetFieldValue(4, InstallationsID); |
329 |
|
|
330 |
|
Mycom.IDnr = IDnr; |
331 |
|
Mycom.CommandID = CommandID; |
332 |
|
Mycom.InstallationsID = InstallationsID; |
333 |
|
|
334 |
|
buffer.push_back(Mycom); |
335 |
|
rs.MoveNext(); |
336 |
|
|
|
buffer.push_back(Mycom); |
|
|
rs.MoveNext(); |
|
|
} |
|
337 |
} |
} |
338 |
rs.Close(); |
rs.Close(); |
339 |
return buffer; |
return buffer; |
341 |
void CFlisServerDlg::ReadSms() |
void CFlisServerDlg::ReadSms() |
342 |
{ |
{ |
343 |
CString tekst, oldtekst; |
CString tekst, oldtekst; |
344 |
Sleep(950); |
Sleep(950); //Holder en pause for at lade hele sms'en komme ind i serial køen. |
345 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
346 |
{ |
{ |
347 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
348 |
Sleep(50); |
Sleep(50); |
349 |
char array1[250]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
|
|
|
350 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
351 |
{ |
{ |
352 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
353 |
{ |
{ |
354 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
355 |
} |
} |
356 |
} |
} |
357 |
|
|
358 |
m_Textwindow.GetWindowText(oldtekst); |
AppendText(tekst); |
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
359 |
SmsSplit(tekst); |
SmsSplit(tekst); |
360 |
} |
} |
361 |
} |
} |
364 |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst; |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst; |
365 |
char CharData[150]; |
char CharData[150]; |
366 |
strcpy(CharData,data); |
strcpy(CharData,data); |
367 |
|
TlfNr = data.Mid(24,8); |
368 |
|
FyrData = data.Mid(57,data.GetLength()-59); |
369 |
|
FyrData.Append(":"); |
370 |
|
|
371 |
int s=24; |
if (FyrData.MakeLower() == "conf ok:") |
|
for (int i=0; i<=7; i++) |
|
|
{ |
|
|
TlfNr.AppendChar(CharData[s]); |
|
|
s++; |
|
|
} |
|
|
|
|
|
for (int s=57; s<=(data.GetLength()-3); s++) |
|
372 |
{ |
{ |
373 |
FyrData.AppendChar(CharData[s]); |
HandleAcknowledge(TlfNr); |
374 |
|
return; |
375 |
} |
} |
|
FyrData.Append(":"); |
|
376 |
|
|
377 |
SmsCount = Splitter(FyrData); |
SmsCount = Splitter(FyrData); |
378 |
Temper = Splitter(FyrData); |
Temper = Splitter(FyrData); |
380 |
Flis = Splitter(FyrData); |
Flis = Splitter(FyrData); |
381 |
FremFejl = Splitter(FyrData); |
FremFejl = Splitter(FyrData); |
382 |
PowerFail = Splitter(FyrData); |
PowerFail = Splitter(FyrData); |
383 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
384 |
///////////////////// Her skal sendes data til databasen ////NÅR HEDIN KAN SENDE SMS/////////////////// |
CString SQL, Textwindow, InstallNR; |
385 |
m_Textwindow.GetWindowText(oldtekst); |
SQL.Format("select ID from installation where installationphonenr=%s",TlfNr); |
386 |
oldtekst.Append("\r\n"); |
|
387 |
oldtekst.Append("På næste linie kommer SmsCount. \r\n"); |
CRecordset rs(&db); |
388 |
oldtekst.Append(SmsCount); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
389 |
oldtekst.Append("\r\n"); |
if (rs.GetRecordCount()>0) |
390 |
oldtekst.Append("På næste linie kommer Temper. \r\n"); |
{ |
391 |
oldtekst.Append(Temper); |
rs.MoveFirst(); |
392 |
oldtekst.Append("\r\n"); |
rs.GetFieldValue((short)0,InstallNR); |
393 |
oldtekst.Append("På næste linie kommer Flamme. \r\n"); |
} |
394 |
oldtekst.Append(Flamme); |
rs.Close(); |
395 |
oldtekst.Append("\r\n"); |
|
396 |
oldtekst.Append("På næste linie kommer Flis. \r\n"); |
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 |
oldtekst.Append(Flis); |
try |
398 |
oldtekst.Append("\r\n"); |
{ |
399 |
oldtekst.Append("På næste linie kommer FremFejl. \r\n"); |
db.ExecuteSQL(SQL); |
400 |
oldtekst.Append(FremFejl); |
} |
401 |
oldtekst.Append("\r\n"); |
catch(CDBException* e) |
402 |
oldtekst.Append("På næste linie kommer PowerFail. \r\n"); |
{ |
403 |
oldtekst.Append(PowerFail); |
MessageBox(e->m_strError); |
404 |
oldtekst.Append("\r\n"); |
} |
405 |
oldtekst.Append("På næste linie kommer TlfNR. \r\n"); |
AppendText("Sms added to Log"); |
406 |
oldtekst.Append(TlfNr); |
Sleep(150); |
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
|
407 |
} |
} |
408 |
CString CFlisServerDlg::Splitter(CString& fyrdata) |
CString CFlisServerDlg::Splitter(CString& fyrdata) |
409 |
{ |
{ |
421 |
{ |
{ |
422 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
423 |
continueThread = 0; |
continueThread = 0; |
424 |
DeleteSms(); //<--- crasher, men gider ikke bruge flere af mine penge på at teste det med sms'er, hele close laver crash, bare ikke når jeg stepper. :S |
DeleteSms(); |
425 |
|
|
426 |
Sleep(500); |
Sleep(500); |
427 |
if( Serial.isOpen() ) |
if( Serial.isOpen() ) |
431 |
|
|
432 |
if(db.IsOpen()) |
if(db.IsOpen()) |
433 |
{ |
{ |
434 |
db.Close(); |
db.Close(); |
435 |
} |
} |
436 |
|
|
437 |
OnOK(); |
OnOK(); |
441 |
void CFlisServerDlg::OnBnClickedGsmpin() |
void CFlisServerDlg::OnBnClickedGsmpin() |
442 |
{ |
{ |
443 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
444 |
|
bool ready = true; |
445 |
|
m_Textwindow.SetWindowText("Indsætter Pinkode, og venter på modem bliver klar"); |
446 |
|
UpdateWindow(); |
447 |
SetPin(); |
SetPin(); |
448 |
Sleep(35000); |
Sleep(500); |
449 |
|
|
450 |
|
while (ready == true) |
451 |
|
{ |
452 |
|
if (Serial.getComstat().cbInQue > 120) |
453 |
|
{ |
454 |
|
std::vector<unsigned char> answer = readFrame(); |
455 |
|
CString tekst; |
456 |
|
|
457 |
|
for (int i=0; i<answer.size(); i++) |
458 |
|
{ |
459 |
|
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
460 |
|
{ |
461 |
|
tekst.AppendChar(answer[i]); |
462 |
|
} |
463 |
|
} |
464 |
|
int n = tekst.Find("WIND: 11",0); |
465 |
|
if (n > -1) |
466 |
|
{ |
467 |
|
ready = false; |
468 |
|
} |
469 |
|
else |
470 |
|
{ |
471 |
|
AppendText(tekst); |
472 |
|
} |
473 |
|
} |
474 |
|
} |
475 |
|
Sleep(100); //Give the modem a chance to send the last data |
476 |
|
while(Serial.getComstat().cbInQue > 0) |
477 |
|
{ |
478 |
|
Serial.readByte(); //Flush the incoming queue |
479 |
|
Sleep(1); |
480 |
|
} |
481 |
OnBnClickedStart(); |
OnBnClickedStart(); |
482 |
} |
} |
483 |
void CFlisServerDlg::DeleteSms() |
void CFlisServerDlg::DeleteSms() |
498 |
writeFrame(atcommand); |
writeFrame(atcommand); |
499 |
Sleep(500); |
Sleep(500); |
500 |
} |
} |
501 |
LRESULT CFlisServerDlg::OnShowString(WPARAM wParam, LPARAM lParam) |
UINT threadWrapper(LPVOID thread) |
|
{ |
|
|
CString *s = (CString*) lParam; |
|
|
GetDlgItem(IDC_Textwindow)->SetWindowText(*s); |
|
|
|
|
|
delete s; |
|
|
return 0; |
|
|
|
|
|
}UINT threadWrapper(LPVOID thread) |
|
502 |
{ |
{ |
503 |
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
504 |
t->runthread(); |
t->runthread(); |
526 |
Sleep(500); |
Sleep(500); |
527 |
CString tekst, oldtekst; |
CString tekst, oldtekst; |
528 |
int lol; |
int lol; |
|
char array1[250]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
529 |
|
|
530 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
531 |
{ |
{ |
532 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
533 |
{ |
{ |
534 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
535 |
} |
} |
536 |
} |
} |
537 |
tekst.Append(":"); |
tekst.Append(":"); |
538 |
|
|
539 |
CString command; |
CString command; |
540 |
bool plus; |
bool plus = false; |
541 |
int pos = tekst.Find('+',0); |
int pos = tekst.Find('+',0); |
542 |
if (pos != -1) |
if (pos != -1) |
543 |
{ |
{ |
549 |
} |
} |
550 |
|
|
551 |
|
|
552 |
if(tekst == "OK") |
if(tekst.MakeLower() == "ok") |
553 |
{ |
{ |
554 |
m_Textwindow.GetWindowText(oldtekst); |
AppendText("OK tekst modtaget"); |
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append("OK tekst modtaget"); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
} |
|
|
else if (tekst == "error") |
|
|
{ |
|
|
m_Textwindow.GetWindowText(oldtekst); |
|
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append("error tekst"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
555 |
} |
} |
556 |
else if (plus = true) |
else if (tekst.MakeLower() == "error") |
557 |
{ |
{ |
558 |
if (command = "cmti") |
CString send; |
559 |
|
send.Append("error tekst"); |
560 |
|
send.Append("\r\n"); |
561 |
|
send.Append(tekst); |
562 |
|
AppendText(send); |
563 |
|
} |
564 |
|
else if (plus == true) |
565 |
|
{ |
566 |
|
if (command.MakeLower() == "cmti") |
567 |
{ |
{ |
568 |
CString smscount, oldteskst; |
CString smscount, oldteskst; |
569 |
int pos = tekst.Find(',',0); |
int pos = tekst.Find(',',0); |
588 |
{ |
{ |
589 |
data.push_back(smscount[i]); |
data.push_back(smscount[i]); |
590 |
} |
} |
591 |
|
m_Textwindow.GetWindowText(oldteskst); |
592 |
|
oldteskst.Append("\r\n"); |
593 |
for (int i=0; i<data.size();i++) |
for (int i=0; i<data.size();i++) |
594 |
{ |
{ |
595 |
oldteskst.AppendChar(data[i]); |
oldteskst.AppendChar(data[i]); |
596 |
} |
} |
597 |
m_Textwindow.SetWindowText(oldteskst); |
m_Textwindow.SetWindowText(oldteskst); |
598 |
writeFrame(data); |
writeFrame(data); |
601 |
ResetSms++; |
ResetSms++; |
602 |
|
|
603 |
} |
} |
604 |
else if(command = "wind") |
else if(command.MakeLower() == "wind") |
605 |
{ |
{ |
606 |
m_Textwindow.GetWindowText(oldtekst); |
|
607 |
oldtekst.Append("\r\n"); |
CString send; |
608 |
oldtekst.Append("Først lidt tekst på næste linie\r\n"); |
send.Append("Wind modtaget"); |
609 |
oldtekst.Append(tekst); |
send.Append("\r\n"); |
610 |
m_Textwindow.SetWindowText(oldtekst); |
send.Append(tekst); |
611 |
|
AppendText(send); |
612 |
} |
} |
613 |
} |
} |
614 |
if(ResetSms == 50) |
else |
615 |
|
{ |
616 |
|
AppendText(tekst); |
617 |
|
} |
618 |
|
if(ResetSms == 20) |
619 |
{ |
{ |
620 |
DeleteSms(); |
DeleteSms(); |
621 |
|
AppendText("Sms'er slettet da vi nåede grænsen"); |
622 |
} |
} |
623 |
|
|
624 |
} |
} |
625 |
if (continueThread = 1) |
if (continueThread == 1) |
626 |
{ |
{ |
627 |
|
|
628 |
CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei; |
CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei; |
629 |
int commandtest = 0; |
int commandtest = 0; |
630 |
int iAll = 1; |
int iAll = 1; |
631 |
std::vector<Commands> data; |
std::vector<Commands> data; |
632 |
data = DBRead(); |
data = DBReadCommands(); |
633 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<data.size(); i++) |
634 |
{ |
{ |
635 |
testdata.Append(data[i].IDnr); |
sIDnr = data[i].IDnr; |
636 |
testdata.Append(":"); |
sCommandID = data[i].CommandID; |
|
testdata.Append(data[i].CommandID); |
|
|
testdata.Append(":"); |
|
637 |
commandtest = atoi(data[i].CommandID); |
commandtest = atoi(data[i].CommandID); |
638 |
|
sInstallationsID = "0"; |
639 |
|
|
640 |
if(commandtest > 1) |
if(commandtest > 1) |
641 |
{ |
{ |
642 |
testdata.Append(data[i].InstallationsID); |
sInstallationsID = data[i].InstallationsID; |
|
testdata.Append(":"); |
|
643 |
} |
} |
644 |
} |
} |
645 |
sIDnr = Splitter(testdata); |
|
|
sCommandID = Splitter(testdata); |
|
|
sInstallationsID = "0"; |
|
646 |
if (commandtest > 1) |
if (commandtest > 1) |
647 |
{ |
{ |
648 |
sInstallationsID = Splitter(testdata); |
iAll = 0; |
|
iAll = 0; |
|
649 |
} |
} |
650 |
|
|
651 |
if (sIDnr.GetLength() > 0) |
if (sIDnr.GetLength() > 0) |
652 |
{ |
{ |
653 |
DBReadData(sIDnr,sCommandID,sInstallationsID); |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
654 |
} |
} |
655 |
Sleep(500); |
Sleep(500); |
656 |
|
|
657 |
|
CheckAcknowledges(); |
658 |
|
Sleep(500); |
659 |
|
} |
660 |
|
CTime now = CTime::GetCurrentTime(); |
661 |
|
|
662 |
|
CTimeSpan elapsed = now-Alive.tid; |
663 |
|
|
664 |
|
if (elapsed.GetTotalSeconds() >= 900) |
665 |
|
{ |
666 |
|
keepaliveandread(); |
667 |
} |
} |
668 |
} |
} |
669 |
void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID) |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
670 |
{ |
{ |
|
CString ServerTlfNr; |
|
|
int i = 0; |
|
|
ServerTlfNr = "29860132"; |
|
|
|
|
671 |
|
|
672 |
std::vector<Installation> inst; |
std::vector<Installation> inst; |
673 |
inst = DBReadPhone(InstallationsID); |
|
674 |
while (i < inst.size()) |
if (CommandID == "1") |
675 |
|
inst = DBReadPhone("0"); |
676 |
|
else |
677 |
|
inst = DBReadPhone(InstallationsID); |
678 |
|
|
679 |
|
for ( int j=0; j < inst.size(); j++) |
680 |
{ |
{ |
681 |
CString TlfNr, Imei, updaterate; |
CString TlfNr, Imei, updaterate; |
682 |
TlfNr.Empty(); |
TlfNr.Empty(); |
683 |
Imei.Empty(); |
Imei.Empty(); |
684 |
updaterate.Empty(); |
updaterate.Empty(); |
685 |
|
|
686 |
CString dataen = inst[i].InstPhoneNr; |
CString dataen = inst[j].InstPhoneNr; |
687 |
TlfNr.Append(dataen); |
TlfNr.Append(dataen); |
688 |
CString Imeidata = inst[i].Imei; |
CString Imeidata = inst[j].Imei; |
689 |
Imei.Append(Imeidata); |
Imei.Append(Imeidata); |
690 |
CString updaterat = inst[i].Updaterate; |
CString updaterat = inst[j].Updaterate; |
691 |
updaterate.Append(updaterat); |
updaterate.Append(updaterat); |
|
i++; |
|
|
|
|
|
vector<unsigned char> tlfnr; |
|
|
for (int i=0; i<TlfNr.GetLength(); i++) |
|
|
{ |
|
|
tlfnr.push_back(TlfNr[i]); |
|
|
} |
|
692 |
|
|
693 |
int calcimei; |
vector<unsigned char> tlfnr; |
694 |
__int64 buf; |
for (int i=0; i<TlfNr.GetLength(); i++) |
695 |
|
{ |
696 |
|
tlfnr.push_back(TlfNr[i]); |
697 |
|
} |
698 |
|
|
699 |
|
int calcimei; |
700 |
|
__int64 buf; |
701 |
|
|
702 |
buf = atof(Imei); |
buf = atof(Imei); |
703 |
|
|
704 |
calcimei = tversum(buf); |
calcimei = tversum(buf); |
705 |
|
|
706 |
Imei.Format("%d",calcimei); |
Imei.Format("%d",calcimei); |
707 |
|
|
708 |
SendSmsHead(tlfnr); |
SendSmsHead(tlfnr); |
709 |
Sleep(250); |
Sleep(250); |
710 |
|
|
711 |
vector<unsigned char> smsdata; |
vector<unsigned char> smsdata; |
712 |
|
|
713 |
for (int i=0; i<Imei.GetLength(); i++) |
for (int i=0; i<Imei.GetLength(); i++) |
714 |
{ |
{ |
715 |
smsdata.push_back(Imei[i]); |
smsdata.push_back(Imei[i]); |
716 |
} |
} |
717 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
718 |
for (int i=0; i<ServerTlfNr.GetLength(); i++) |
for (int i=0; i<Alive.Phonenr.GetLength(); i++) |
719 |
{ |
{ |
720 |
smsdata.push_back(ServerTlfNr[i]); |
smsdata.push_back(Alive.Phonenr[i]); |
721 |
} |
} |
722 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
723 |
for (int i=0; i<updaterate.GetLength(); i++) |
for (int i=0; i<updaterate.GetLength(); i++) |
724 |
{ |
{ |
725 |
smsdata.push_back(updaterate[i]); |
smsdata.push_back(updaterate[i]); |
726 |
} |
} |
727 |
|
|
728 |
|
SendSmsData(smsdata); |
729 |
|
Sleep(500); |
730 |
|
|
731 |
|
if (IDnr != "0") |
732 |
|
{ |
733 |
|
Acks.push_back( Acknowledge(inst[j].instID,TlfNr) ); |
734 |
|
} |
735 |
|
|
|
SendSmsData(smsdata); |
|
|
Sleep(500); |
|
|
|
|
736 |
} |
} |
737 |
|
|
738 |
|
if (IDnr != "0") |
739 |
|
{ |
740 |
CString SQL, Textwindow; |
CString SQL, Textwindow; |
741 |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
742 |
db.ExecuteSQL(SQL); |
db.ExecuteSQL(SQL); |
743 |
m_Textwindow.GetWindowText(Textwindow); |
AppendText("Command executed"); |
744 |
Textwindow.Append("\r\n"); |
} |
745 |
Textwindow.Append("Command executed"); |
Sleep(150); |
746 |
m_Textwindow.SetWindowText(Textwindow); |
|
747 |
Sleep(150); |
|
748 |
|
|
749 |
|
|
750 |
} |
} |
751 |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
752 |
{ |
{ |
753 |
vector<Installation> buffer; |
vector<Installation> buffer; |
754 |
|
|
755 |
CString SQL, phonenr, imei, updaterate; |
CString SQL, phonenr, imei, updaterate, id; |
|
int installernull; |
|
756 |
Installation Myinst; |
Installation Myinst; |
757 |
installernull = atoi(sInstallationsID); |
|
758 |
if (installernull > 1) |
if (sInstallationsID != "0") |
759 |
{ |
{ |
760 |
SQL.Format("select installationphonenr, imei, updaterate from installation WHERE id = %s", sInstallationsID); |
SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID); |
761 |
} |
} |
762 |
else if(installernull < 2) |
else |
763 |
{ |
{ |
764 |
SQL.Format("select installationphonenr, imei, updaterate from installation"); |
SQL.Format("select installationphonenr, imei, updaterate, id from installation"); |
765 |
} |
} |
766 |
|
|
767 |
CRecordset rs(&db); |
CRecordset rs(&db); |
768 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
769 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
775 |
rs.GetFieldValue((short)0,phonenr); |
rs.GetFieldValue((short)0,phonenr); |
776 |
rs.GetFieldValue(1,imei); |
rs.GetFieldValue(1,imei); |
777 |
rs.GetFieldValue(2,updaterate); |
rs.GetFieldValue(2,updaterate); |
778 |
|
rs.GetFieldValue(3,id); |
779 |
|
|
780 |
Myinst.InstPhoneNr = phonenr; |
Myinst.InstPhoneNr = phonenr; |
781 |
Myinst.Imei = imei; |
Myinst.Imei = imei; |
782 |
Myinst.Updaterate = updaterate; |
Myinst.Updaterate = updaterate; |
783 |
|
Myinst.instID = id; |
784 |
|
|
785 |
buffer.push_back(Myinst); |
buffer.push_back(Myinst); |
786 |
rs.MoveNext(); |
rs.MoveNext(); |
802 |
void CFlisServerDlg::OnBnClickedStart() |
void CFlisServerDlg::OnBnClickedStart() |
803 |
{ |
{ |
804 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
805 |
|
|
806 |
continueThread = 1; |
continueThread = 1; |
807 |
|
keepaliveandread(); |
808 |
|
Sleep(150); |
809 |
|
m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr ); |
810 |
|
AppendText("Started"); |
811 |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
812 |
} |
} |
813 |
|
void CFlisServerDlg::AppendText(CString s) |
814 |
|
{ |
815 |
|
CString Tekst; |
816 |
|
m_Textwindow.GetWindowText(Tekst); |
817 |
|
Tekst.Append("\r\n"); |
818 |
|
Tekst.Append(s); |
819 |
|
m_Textwindow.SetWindowText(Tekst); |
820 |
|
|
821 |
|
} |
822 |
|
void ConfigFile::ReadSettings() |
823 |
|
{ |
824 |
|
ifstream file("Server-Settings.ini"); |
825 |
|
if (!file.is_open()) |
826 |
|
throw("Could not open file"); |
827 |
|
|
828 |
|
char buf[200]; |
829 |
|
while (!file.eof() ) { |
830 |
|
file.getline(buf,200); |
831 |
|
CString tmp(buf); |
832 |
|
|
833 |
|
if (tmp.GetAt(0) == '#') |
834 |
|
continue; |
835 |
|
|
836 |
|
int pos = tmp.Find('='); |
837 |
|
if (pos>0) { |
838 |
|
CString key = tmp.Left(pos).Trim().MakeLower();; |
839 |
|
CString value = tmp.Right(tmp.GetLength()-pos-1).Trim(); |
840 |
|
|
841 |
|
if (key == "host") |
842 |
|
host = value; |
843 |
|
else if (key == "username") |
844 |
|
username = value; |
845 |
|
else if (key == "password") |
846 |
|
password = value; |
847 |
|
else if (key == "database") |
848 |
|
database = value; |
849 |
|
else if (key == "comport") |
850 |
|
comport = value; |
851 |
|
} |
852 |
|
|
853 |
|
} |
854 |
|
|
855 |
|
file.close(); |
856 |
|
} |
857 |
|
void CFlisServerDlg::HandleAcknowledge(CString tlfnr) |
858 |
|
{ |
859 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
860 |
|
{ |
861 |
|
Acknowledge& current = (*it); |
862 |
|
if (current.sTlfNr == tlfnr) |
863 |
|
{ |
864 |
|
CString SQL; |
865 |
|
SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID); |
866 |
|
db.ExecuteSQL(SQL); |
867 |
|
|
868 |
|
Acks.erase(it); |
869 |
|
|
870 |
|
return; |
871 |
|
} |
872 |
|
} |
873 |
|
} |
874 |
|
|
875 |
|
void CFlisServerDlg::CheckAcknowledges() |
876 |
|
{ |
877 |
|
CTime now = CTime::GetCurrentTime(); |
878 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
879 |
|
{ |
880 |
|
Acknowledge& current = (*it); |
881 |
|
|
882 |
|
CTimeSpan elapsed = now-current.cTime; |
883 |
|
|
884 |
|
if (elapsed.GetTotalSeconds() >= 300) |
885 |
|
{ |
886 |
|
DumpAckList(); |
887 |
|
if (current.iRetry >= 1) |
888 |
|
{ |
889 |
|
CString SQL; |
890 |
|
SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID); |
891 |
|
db.ExecuteSQL(SQL); |
892 |
|
Acks.erase(it); |
893 |
|
return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again |
894 |
|
} |
895 |
|
else |
896 |
|
{ |
897 |
|
SendConfig("0","2",current.sInstallationID); |
898 |
|
current.iRetry++; |
899 |
|
current.cTime = CTime::GetCurrentTime(); |
900 |
|
AppendText( CString("Retry send config to ") + current.sTlfNr ); |
901 |
|
} |
902 |
|
} |
903 |
|
} |
904 |
|
} |
905 |
|
|
906 |
|
void CFlisServerDlg::DumpAckList() |
907 |
|
{ |
908 |
|
CTime now = CTime::GetCurrentTime(); |
909 |
|
OutputDebugString("-------------------------------\n"); |
910 |
|
int count = 0; |
911 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
912 |
|
{ |
913 |
|
CString msg; |
914 |
|
msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr); |
915 |
|
OutputDebugString(msg); |
916 |
|
count++; |
917 |
|
} |
918 |
|
} |
919 |
|
vector<keepalive> CFlisServerDlg::keepaliveandread(void) |
920 |
|
{ |
921 |
|
vector<keepalive> buffer; |
922 |
|
|
923 |
|
CString SQL, name, value; |
924 |
|
SQL = "select name, value from config;"; |
925 |
|
CRecordset rs(&db); |
926 |
|
try |
927 |
|
{ |
928 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
929 |
|
if (rs.GetRecordCount()>0) |
930 |
|
{ |
931 |
|
rs.MoveFirst(); |
932 |
|
|
933 |
|
rs.GetFieldValue((short)0, name); |
934 |
|
rs.GetFieldValue(1,value); |
935 |
|
|
936 |
|
Alive.name = name; |
937 |
|
Alive.value = value; |
938 |
|
Alive.tid = CTime::GetCurrentTime(); |
939 |
|
if(Alive.name == "phonenr") |
940 |
|
{ |
941 |
|
Alive.Phonenr = value; |
942 |
|
} |
943 |
|
|
944 |
|
buffer.push_back(Alive); |
945 |
|
rs.MoveNext(); |
946 |
|
|
947 |
|
} |
948 |
|
rs.Close(); |
949 |
|
return buffer; |
950 |
|
} |
951 |
|
catch (char * str) |
952 |
|
{ |
953 |
|
AppendText("Keepalive failed, closing and opening the db connection again"); |
954 |
|
if(db.IsOpen()) |
955 |
|
{ |
956 |
|
db.Close(); |
957 |
|
AppendText("DB connection closed"); |
958 |
|
} |
959 |
|
if(!db.IsOpen()) |
960 |
|
{ |
961 |
|
DBConnect(); |
962 |
|
AppendText("DB connection started again"); |
963 |
|
} |
964 |
|
} |
965 |
|
|
966 |
|
|
967 |
|
} |