6 |
#include "FlisServerDlg.h" |
#include "FlisServerDlg.h" |
7 |
#include <vector> |
#include <vector> |
8 |
#include ".\flisserverdlg.h" |
#include ".\flisserverdlg.h" |
9 |
|
#include <atlrx.h> |
10 |
|
|
11 |
#ifdef _DEBUG |
#ifdef _DEBUG |
12 |
#define new DEBUG_NEW |
#define new DEBUG_NEW |
64 |
ON_WM_SYSCOMMAND() |
ON_WM_SYSCOMMAND() |
65 |
ON_WM_PAINT() |
ON_WM_PAINT() |
66 |
ON_WM_QUERYDRAGICON() |
ON_WM_QUERYDRAGICON() |
|
ON_MESSAGE(UWM_MYMESSAGE, OnShowString) |
|
67 |
//}}AFX_MSG_MAP |
//}}AFX_MSG_MAP |
68 |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
|
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) |
|
69 |
ON_BN_CLICKED(IDC_Start, OnBnClickedStart) |
ON_BN_CLICKED(IDC_Start, OnBnClickedStart) |
70 |
END_MESSAGE_MAP() |
END_MESSAGE_MAP() |
71 |
|
|
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 |
} |
} |
224 |
} |
} |
225 |
void CFlisServerDlg::SetPin() |
void CFlisServerDlg::SetPin() |
226 |
{ |
{ |
227 |
|
bool ready = true; |
228 |
CString tekst; |
CString tekst; |
229 |
std::vector<unsigned char> data; |
std::vector<unsigned char> data; |
230 |
|
|
231 |
data.push_back('a'); |
data.push_back('a'); |
232 |
data.push_back('t'); |
data.push_back('t'); |
233 |
data.push_back('+'); |
data.push_back('+'); |
235 |
data.push_back('p'); |
data.push_back('p'); |
236 |
data.push_back('i'); |
data.push_back('i'); |
237 |
data.push_back('n'); |
data.push_back('n'); |
238 |
data.push_back('='); |
data.push_back('?'); |
|
data.push_back('2'); |
|
|
data.push_back('5'); |
|
|
data.push_back('9'); |
|
|
data.push_back('5'); |
|
239 |
|
|
240 |
writeFrame(data); |
writeFrame(data); |
241 |
/* Sleep(750); |
Sleep(100); |
242 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
243 |
{ |
{ |
244 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
245 |
Sleep(50); |
Sleep(50); |
246 |
char array1[25]; |
|
247 |
int i; |
for (int i=0; i<answer.size(); i++) |
|
for (int i=0; i<answer.size(); i++) |
|
248 |
{ |
{ |
249 |
array1[i] = answer[i]; |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
250 |
|
{ |
251 |
|
tekst.AppendChar(answer[i]); |
252 |
|
} |
253 |
} |
} |
254 |
|
AppendText(tekst); |
255 |
for (int i=0; i<answer.size(); i++) |
} |
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 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
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 |
tekst.AppendChar(array1[i]); |
AppendText(tekst); |
299 |
} |
} |
300 |
} |
} |
301 |
m_Textwindow.SetWindowText(tekst); |
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 |
} |
} |
313 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
314 |
{ |
{ |
325 |
CString tekst; |
CString tekst; |
326 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
327 |
Sleep(50); |
Sleep(50); |
328 |
char array1[150]; |
|
329 |
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++) |
|
330 |
{ |
{ |
331 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
332 |
{ |
{ |
333 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
334 |
} |
} |
335 |
} |
} |
336 |
m_Textwindow.SetWindowText(tekst); |
AppendText(tekst); |
337 |
} |
} |
338 |
|
|
339 |
} |
} |
340 |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr) |
341 |
{ |
{ |
342 |
vector<unsigned char> atcommand; |
vector<unsigned char> atcommand; |
343 |
atcommand.push_back('a'); |
atcommand.push_back('a'); |
357 |
Sleep(5); |
Sleep(5); |
358 |
} |
} |
359 |
|
|
360 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<tlfnr.size(); i++) |
361 |
{ |
{ |
362 |
Serial.writeByte( data[i] ); |
Serial.writeByte( tlfnr[i] ); |
363 |
Sleep(5); |
Sleep(5); |
364 |
} |
} |
365 |
Serial.writeByte(atcommand[s]); |
Serial.writeByte(atcommand[s]); |
369 |
void CFlisServerDlg::DBConnect() |
void CFlisServerDlg::DBConnect() |
370 |
{ |
{ |
371 |
CString dsn; |
CString dsn; |
372 |
//dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //Torben |
ConfigFile config; |
373 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server |
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 |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
381 |
} |
} |
382 |
vector<Commands> CFlisServerDlg::DBRead(void) |
vector<Commands> CFlisServerDlg::DBReadCommands(void) |
383 |
{ |
{ |
384 |
vector<Commands> buffer; |
vector<Commands> buffer; |
385 |
|
|
386 |
CString SQL, IDnr, CommandID, InstallationsID; |
CString SQL, IDnr, CommandID, InstallationsID; |
387 |
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;"; |
388 |
CRecordset rs(&db); |
CRecordset rs(&db); |
389 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
390 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
391 |
{ |
{ |
392 |
rs.MoveFirst(); |
rs.MoveFirst(); |
|
while(!rs.IsEOF()) |
|
|
{ |
|
|
Commands Mycom; |
|
|
rs.GetFieldValue((short)0, IDnr); |
|
|
rs.GetFieldValue(3, CommandID); |
|
|
rs.GetFieldValue(4, InstallationsID); |
|
393 |
|
|
394 |
Mycom.IDnr = IDnr; |
Commands Mycom; |
395 |
Mycom.CommandID = CommandID; |
rs.GetFieldValue((short)0, IDnr); |
396 |
Mycom.InstallationsID = InstallationsID; |
rs.GetFieldValue(3, CommandID); |
397 |
|
rs.GetFieldValue(4, InstallationsID); |
398 |
|
|
399 |
|
Mycom.IDnr = IDnr; |
400 |
|
Mycom.CommandID = CommandID; |
401 |
|
Mycom.InstallationsID = InstallationsID; |
402 |
|
|
403 |
|
buffer.push_back(Mycom); |
404 |
|
rs.MoveNext(); |
405 |
|
|
|
buffer.push_back(Mycom); |
|
|
rs.MoveNext(); |
|
|
} |
|
406 |
} |
} |
407 |
rs.Close(); |
rs.Close(); |
408 |
return buffer; |
return buffer; |
410 |
void CFlisServerDlg::ReadSms() |
void CFlisServerDlg::ReadSms() |
411 |
{ |
{ |
412 |
CString tekst, oldtekst; |
CString tekst, oldtekst; |
413 |
Sleep(950); |
Sleep(950); //Holder en pause for at lade hele sms'en komme ind i serial køen. |
414 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
415 |
{ |
{ |
416 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
417 |
Sleep(50); |
Sleep(50); |
418 |
char array1[250]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
|
|
|
419 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
420 |
{ |
{ |
421 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
422 |
{ |
{ |
423 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
424 |
} |
} |
425 |
} |
} |
426 |
|
|
427 |
m_Textwindow.GetWindowText(oldtekst); |
AppendText(tekst); |
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
428 |
SmsSplit(tekst); |
SmsSplit(tekst); |
429 |
} |
} |
430 |
} |
} |
433 |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst; |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst; |
434 |
char CharData[150]; |
char CharData[150]; |
435 |
strcpy(CharData,data); |
strcpy(CharData,data); |
436 |
|
TlfNr = data.Mid(24,8); |
437 |
|
FyrData = data.Mid(57,data.GetLength()-59); |
438 |
|
FyrData.Append(":"); |
439 |
|
|
440 |
int s=24; |
if (FyrData.MakeLower() == "conf ok:") |
|
for (int i=0; i<=7; i++) |
|
441 |
{ |
{ |
442 |
TlfNr.AppendChar(CharData[s]); |
HandleAcknowledge(TlfNr); |
443 |
s++; |
return; |
444 |
} |
} |
445 |
|
CAtlRegExp<> regex; |
446 |
|
if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK) |
447 |
|
{ |
448 |
|
|
449 |
for (int s=57; s<=(data.GetLength()-3); s++) |
CAtlREMatchContext<> pContext; |
450 |
|
|
451 |
|
if (!regex.Match(FyrData, &pContext)) |
452 |
|
{ |
453 |
|
AppendText("Invalid SMS recieved"); |
454 |
|
return; |
455 |
|
} |
456 |
|
} |
457 |
|
else |
458 |
{ |
{ |
459 |
FyrData.AppendChar(CharData[s]); |
AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske ! |
460 |
} |
} |
461 |
FyrData.Append(":"); |
|
|
|
|
462 |
SmsCount = Splitter(FyrData); |
SmsCount = Splitter(FyrData); |
463 |
Temper = Splitter(FyrData); |
Temper = Splitter(FyrData); |
464 |
Flamme = Splitter(FyrData); |
Flamme = Splitter(FyrData); |
465 |
Flis = Splitter(FyrData); |
Flis = Splitter(FyrData); |
466 |
FremFejl = Splitter(FyrData); |
FremFejl = Splitter(FyrData); |
467 |
PowerFail = Splitter(FyrData); |
PowerFail = Splitter(FyrData); |
468 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
469 |
///////////////////// Her skal sendes data til databasen ////NÅR HEDIN KAN SENDE SMS/////////////////// |
CString SQL, Textwindow, InstallNR; |
470 |
m_Textwindow.GetWindowText(oldtekst); |
SQL.Format("select ID from installation where installationphonenr=%s",TlfNr); |
471 |
oldtekst.Append("\r\n"); |
|
472 |
oldtekst.Append("På næste linie kommer SmsCount. \r\n"); |
CRecordset rs(&db); |
473 |
oldtekst.Append(SmsCount); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
474 |
oldtekst.Append("\r\n"); |
if (rs.GetRecordCount()>0) |
475 |
oldtekst.Append("På næste linie kommer Temper. \r\n"); |
{ |
476 |
oldtekst.Append(Temper); |
rs.MoveFirst(); |
477 |
oldtekst.Append("\r\n"); |
rs.GetFieldValue((short)0,InstallNR); |
478 |
oldtekst.Append("På næste linie kommer Flamme. \r\n"); |
} |
479 |
oldtekst.Append(Flamme); |
rs.Close(); |
480 |
oldtekst.Append("\r\n"); |
|
481 |
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); |
482 |
oldtekst.Append(Flis); |
try |
483 |
oldtekst.Append("\r\n"); |
{ |
484 |
oldtekst.Append("På næste linie kommer FremFejl. \r\n"); |
db.ExecuteSQL(SQL); |
485 |
oldtekst.Append(FremFejl); |
} |
486 |
oldtekst.Append("\r\n"); |
catch(CDBException* e) |
487 |
oldtekst.Append("På næste linie kommer PowerFail. \r\n"); |
{ |
488 |
oldtekst.Append(PowerFail); |
MessageBox(e->m_strError); |
489 |
oldtekst.Append("\r\n"); |
} |
490 |
oldtekst.Append("På næste linie kommer TlfNR. \r\n"); |
AppendText("Sms added to Log"); |
491 |
oldtekst.Append(TlfNr); |
Sleep(150); |
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
|
492 |
} |
} |
493 |
CString CFlisServerDlg::Splitter(CString& fyrdata) |
CString CFlisServerDlg::Splitter(CString& fyrdata) |
494 |
{ |
{ |
506 |
{ |
{ |
507 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
508 |
continueThread = 0; |
continueThread = 0; |
509 |
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 |
Sleep(250); |
510 |
|
DeleteSms(); |
511 |
|
|
512 |
Sleep(500); |
Sleep(500); |
513 |
if( Serial.isOpen() ) |
if( Serial.isOpen() ) |
517 |
|
|
518 |
if(db.IsOpen()) |
if(db.IsOpen()) |
519 |
{ |
{ |
520 |
db.Close(); |
db.Close(); |
521 |
} |
} |
522 |
|
|
523 |
OnOK(); |
OnOK(); |
524 |
|
|
525 |
} |
} |
|
|
|
|
void CFlisServerDlg::OnBnClickedGsmpin() |
|
|
{ |
|
|
// TODO: Add your control notification handler code here |
|
|
SetPin(); |
|
|
Sleep(35000); |
|
|
OnBnClickedStart(); |
|
|
} |
|
526 |
void CFlisServerDlg::DeleteSms() |
void CFlisServerDlg::DeleteSms() |
527 |
{ |
{ |
528 |
vector<unsigned char> atcommand; |
vector<unsigned char> atcommand; |
541 |
writeFrame(atcommand); |
writeFrame(atcommand); |
542 |
Sleep(500); |
Sleep(500); |
543 |
} |
} |
544 |
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) |
|
545 |
{ |
{ |
546 |
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
547 |
t->runthread(); |
t->runthread(); |
557 |
{ |
{ |
558 |
while (continueThread != 0) |
while (continueThread != 0) |
559 |
{ |
{ |
560 |
Reader(); |
MyMainThread(); |
561 |
} |
} |
562 |
} |
} |
563 |
void CFlisServerDlg::Reader() |
void CFlisServerDlg::MyMainThread() |
564 |
{ |
{ |
565 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
566 |
{ |
{ |
568 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
569 |
Sleep(500); |
Sleep(500); |
570 |
CString tekst, oldtekst; |
CString tekst, oldtekst; |
|
int lol; |
|
|
char array1[250]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
571 |
|
|
572 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
573 |
{ |
{ |
574 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
575 |
{ |
{ |
576 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
577 |
} |
} |
578 |
} |
} |
579 |
tekst.Append(":"); |
tekst.Append(":"); |
580 |
|
|
581 |
CString command; |
CString command; |
582 |
bool plus; |
bool plus = false; |
583 |
int pos = tekst.Find('+',0); |
int pos = tekst.Find('+',0); |
584 |
if (pos != -1) |
if (pos != -1) |
585 |
{ |
{ |
591 |
} |
} |
592 |
|
|
593 |
|
|
594 |
if(tekst == "OK") |
if(tekst.MakeLower() == "ok") |
595 |
{ |
{ |
596 |
m_Textwindow.GetWindowText(oldtekst); |
AppendText("OK tekst modtaget"); |
597 |
oldtekst.Append("\r\n"); |
} |
598 |
oldtekst.Append("OK tekst modtaget"); |
else if (tekst.MakeLower() == "error") |
599 |
m_Textwindow.SetWindowText(oldtekst); |
{ |
600 |
} |
CString send; |
601 |
else if (tekst == "error") |
send.Append("error tekst"); |
602 |
{ |
send.Append("\r\n"); |
603 |
m_Textwindow.GetWindowText(oldtekst); |
send.Append(tekst); |
604 |
oldtekst.Append("\r\n"); |
AppendText(send); |
|
oldtekst.Append("error tekst"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
605 |
} |
} |
606 |
else if (plus = true) |
else if (plus == true) |
607 |
{ |
{ |
608 |
if (command = "cmti") |
if (command.MakeLower() == "cmti") |
609 |
{ |
{ |
610 |
CString smscount, oldteskst; |
CString smscount, oldteskst; |
611 |
int pos = tekst.Find(',',0); |
int pos = tekst.Find(',',0); |
630 |
{ |
{ |
631 |
data.push_back(smscount[i]); |
data.push_back(smscount[i]); |
632 |
} |
} |
633 |
|
m_Textwindow.GetWindowText(oldteskst); |
634 |
|
oldteskst.Append("\r\n"); |
635 |
for (int i=0; i<data.size();i++) |
for (int i=0; i<data.size();i++) |
636 |
{ |
{ |
637 |
oldteskst.AppendChar(data[i]); |
oldteskst.AppendChar(data[i]); |
638 |
} |
} |
639 |
m_Textwindow.SetWindowText(oldteskst); |
m_Textwindow.SetWindowText(oldteskst); |
640 |
writeFrame(data); |
writeFrame(data); |
643 |
ResetSms++; |
ResetSms++; |
644 |
|
|
645 |
} |
} |
646 |
else if(command = "wind") |
else if(command.MakeLower() == "wind") |
647 |
{ |
{ |
648 |
m_Textwindow.GetWindowText(oldtekst); |
|
649 |
oldtekst.Append("\r\n"); |
CString send; |
650 |
oldtekst.Append("Først lidt tekst på næste linie\r\n"); |
send.Append("Wind modtaget"); |
651 |
oldtekst.Append(tekst); |
send.Append("\r\n"); |
652 |
m_Textwindow.SetWindowText(oldtekst); |
send.Append(tekst); |
653 |
|
AppendText(send); |
654 |
} |
} |
655 |
} |
} |
656 |
if(ResetSms == 50) |
else |
657 |
|
{ |
658 |
|
AppendText(tekst); |
659 |
|
} |
660 |
|
if(ResetSms == 20) |
661 |
{ |
{ |
662 |
DeleteSms(); |
DeleteSms(); |
663 |
|
AppendText("Sms'er slettet da vi nåede grænsen"); |
664 |
} |
} |
665 |
|
|
666 |
} |
} |
667 |
if (continueThread = 1) |
if (continueThread == 1) |
668 |
{ |
{ |
669 |
|
|
670 |
CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei; |
CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei; |
671 |
int commandtest = 0; |
int commandtest = 0; |
672 |
int iAll = 1; |
int iAll = 1; |
673 |
std::vector<Commands> data; |
std::vector<Commands> data; |
674 |
data = DBRead(); |
data = DBReadCommands(); |
675 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<data.size(); i++) |
676 |
{ |
{ |
677 |
testdata.Append(data[i].IDnr); |
sIDnr = data[i].IDnr; |
678 |
testdata.Append(":"); |
sCommandID = data[i].CommandID; |
|
testdata.Append(data[i].CommandID); |
|
|
testdata.Append(":"); |
|
679 |
commandtest = atoi(data[i].CommandID); |
commandtest = atoi(data[i].CommandID); |
680 |
|
sInstallationsID = "0"; |
681 |
|
|
682 |
if(commandtest > 1) |
if(commandtest > 1) |
683 |
{ |
{ |
684 |
testdata.Append(data[i].InstallationsID); |
sInstallationsID = data[i].InstallationsID; |
|
testdata.Append(":"); |
|
685 |
} |
} |
686 |
} |
} |
687 |
sIDnr = Splitter(testdata); |
|
|
sCommandID = Splitter(testdata); |
|
|
sInstallationsID = "0"; |
|
688 |
if (commandtest > 1) |
if (commandtest > 1) |
689 |
{ |
{ |
690 |
sInstallationsID = Splitter(testdata); |
iAll = 0; |
|
iAll = 0; |
|
691 |
} |
} |
692 |
|
|
693 |
if (sIDnr.GetLength() > 0) |
if (sIDnr.GetLength() > 0) |
694 |
{ |
{ |
695 |
DBReadData(sIDnr,sCommandID,sInstallationsID); |
SendConfig(sIDnr,sCommandID,sInstallationsID); |
696 |
} |
} |
697 |
|
Sleep(300); |
698 |
|
|
699 |
|
CheckAcknowledges(); |
700 |
Sleep(500); |
Sleep(500); |
701 |
} |
} |
702 |
|
CTime now = CTime::GetCurrentTime(); |
703 |
|
|
704 |
|
CTimeSpan elapsed = now-Alive.tid; |
705 |
|
|
706 |
|
if (elapsed.GetTotalSeconds() >= 900) |
707 |
|
{ |
708 |
|
keepaliveandread(); |
709 |
|
} |
710 |
} |
} |
711 |
void CFlisServerDlg::DBReadData(CString IDnr,CString CommandID,CString InstallationsID) |
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
712 |
{ |
{ |
|
CString ServerTlfNr; |
|
|
int i = 0; |
|
|
ServerTlfNr = "29860132"; |
|
|
|
|
713 |
|
|
714 |
std::vector<Installation> inst; |
std::vector<Installation> inst; |
715 |
inst = DBReadPhone(InstallationsID); |
|
716 |
while (i < inst.size()) |
if (CommandID == "1") |
717 |
|
inst = DBReadPhone("0"); |
718 |
|
else |
719 |
|
inst = DBReadPhone(InstallationsID); |
720 |
|
|
721 |
|
for ( int j=0; j < inst.size(); j++) |
722 |
{ |
{ |
723 |
CString TlfNr, Imei, updaterate; |
CString TlfNr, Imei, updaterate; |
724 |
TlfNr.Empty(); |
TlfNr.Empty(); |
725 |
Imei.Empty(); |
Imei.Empty(); |
726 |
updaterate.Empty(); |
updaterate.Empty(); |
727 |
|
|
728 |
CString dataen = inst[i].InstPhoneNr; |
CString dataen = inst[j].InstPhoneNr; |
729 |
TlfNr.Append(dataen); |
TlfNr.Append(dataen); |
730 |
CString Imeidata = inst[i].Imei; |
CString Imeidata = inst[j].Imei; |
731 |
Imei.Append(Imeidata); |
Imei.Append(Imeidata); |
732 |
CString updaterat = inst[i].Updaterate; |
CString updaterat = inst[j].Updaterate; |
733 |
updaterate.Append(updaterat); |
updaterate.Append(updaterat); |
|
i++; |
|
734 |
|
|
735 |
vector<unsigned char> tlfnr; |
vector<unsigned char> tlfnr; |
736 |
for (int i=0; i<TlfNr.GetLength(); i++) |
for (int i=0; i<TlfNr.GetLength(); i++) |
737 |
{ |
{ |
738 |
tlfnr.push_back(TlfNr[i]); |
tlfnr.push_back(TlfNr[i]); |
739 |
} |
} |
740 |
|
|
741 |
int calcimei; |
int calcimei; |
742 |
__int64 buf; |
__int64 buf; |
743 |
|
|
744 |
buf = atof(Imei); |
buf = atof(Imei); |
745 |
|
|
746 |
calcimei = tversum(buf); |
calcimei = tversum(buf); |
747 |
|
|
748 |
Imei.Format("%d",calcimei); |
Imei.Format("%d",calcimei); |
749 |
|
|
750 |
SendSmsHead(tlfnr); |
SendSmsHead(tlfnr); |
751 |
Sleep(250); |
Sleep(250); |
752 |
|
|
753 |
vector<unsigned char> smsdata; |
vector<unsigned char> smsdata; |
754 |
|
|
755 |
for (int i=0; i<Imei.GetLength(); i++) |
for (int i=0; i<Imei.GetLength(); i++) |
756 |
{ |
{ |
757 |
smsdata.push_back(Imei[i]); |
smsdata.push_back(Imei[i]); |
758 |
} |
} |
759 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
760 |
for (int i=0; i<ServerTlfNr.GetLength(); i++) |
for (int i=0; i<Alive.Phonenr.GetLength(); i++) |
761 |
{ |
{ |
762 |
smsdata.push_back(ServerTlfNr[i]); |
smsdata.push_back(Alive.Phonenr[i]); |
763 |
} |
} |
764 |
smsdata.push_back(':'); |
smsdata.push_back(':'); |
765 |
for (int i=0; i<updaterate.GetLength(); i++) |
for (int i=0; i<updaterate.GetLength(); i++) |
766 |
{ |
{ |
767 |
smsdata.push_back(updaterate[i]); |
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 |
|
|
|
SendSmsData(smsdata); |
|
|
Sleep(500); |
|
|
|
|
778 |
} |
} |
779 |
|
|
780 |
|
if (IDnr != "0") |
781 |
|
{ |
782 |
CString SQL, Textwindow; |
CString SQL, Textwindow; |
783 |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
SQL.Format("update command set executed=now() where id=%s",IDnr); |
784 |
db.ExecuteSQL(SQL); |
db.ExecuteSQL(SQL); |
785 |
m_Textwindow.GetWindowText(Textwindow); |
Sleep(100); |
786 |
Textwindow.Append("\r\n"); |
AppendText("Command executed"); |
787 |
Textwindow.Append("Command executed"); |
} |
788 |
m_Textwindow.SetWindowText(Textwindow); |
Sleep(150); |
789 |
Sleep(150); |
|
790 |
|
|
791 |
|
|
792 |
|
|
793 |
} |
} |
794 |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
795 |
{ |
{ |
796 |
vector<Installation> buffer; |
vector<Installation> buffer; |
797 |
|
|
798 |
CString SQL, phonenr, imei, updaterate; |
CString SQL, phonenr, imei, updaterate, id; |
|
int installernull; |
|
799 |
Installation Myinst; |
Installation Myinst; |
800 |
installernull = atoi(sInstallationsID); |
|
801 |
if (installernull > 1) |
if (sInstallationsID != "0") |
802 |
{ |
{ |
803 |
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); |
804 |
} |
} |
805 |
else if(installernull < 2) |
else |
806 |
{ |
{ |
807 |
SQL.Format("select installationphonenr, imei, updaterate from installation"); |
SQL.Format("select installationphonenr, imei, updaterate, id from installation"); |
808 |
} |
} |
809 |
|
|
810 |
CRecordset rs(&db); |
CRecordset rs(&db); |
811 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
812 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
818 |
rs.GetFieldValue((short)0,phonenr); |
rs.GetFieldValue((short)0,phonenr); |
819 |
rs.GetFieldValue(1,imei); |
rs.GetFieldValue(1,imei); |
820 |
rs.GetFieldValue(2,updaterate); |
rs.GetFieldValue(2,updaterate); |
821 |
|
rs.GetFieldValue(3,id); |
822 |
|
|
823 |
Myinst.InstPhoneNr = phonenr; |
Myinst.InstPhoneNr = phonenr; |
824 |
Myinst.Imei = imei; |
Myinst.Imei = imei; |
825 |
Myinst.Updaterate = updaterate; |
Myinst.Updaterate = updaterate; |
826 |
|
Myinst.instID = id; |
827 |
|
|
828 |
buffer.push_back(Myinst); |
buffer.push_back(Myinst); |
829 |
rs.MoveNext(); |
rs.MoveNext(); |
846 |
{ |
{ |
847 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
848 |
|
|
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 |
|
|
860 |
continueThread = 1; |
continueThread = 1; |
861 |
|
keepaliveandread(); |
862 |
|
Sleep(150); |
863 |
|
m_Textwindow.SetWindowText(CString("Server phonenr read from db: ") + Alive.Phonenr ); |
864 |
|
AppendText("Started"); |
865 |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
866 |
} |
} |
867 |
|
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 |
|
Sleep(5); |
875 |
|
int g = m_Textwindow.GetScrollLimit(SB_VERT); |
876 |
|
if (g > 0) |
877 |
|
{ |
878 |
|
m_Textwindow.SetSel(m_Textwindow.GetWindowTextLength() -1,m_Textwindow.GetWindowTextLength(),false); |
879 |
|
} |
880 |
|
|
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 |
|
} |
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 |
|
void CFlisServerDlg::DumpAckList() /* Debug tool */ |
967 |
|
{ |
968 |
|
|
969 |
|
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 |
|
try |
988 |
|
{ |
989 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
990 |
|
if (rs.GetRecordCount()>0) |
991 |
|
{ |
992 |
|
rs.MoveFirst(); |
993 |
|
|
994 |
|
rs.GetFieldValue((short)0, name); |
995 |
|
rs.GetFieldValue(1,value); |
996 |
|
|
997 |
|
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 |
|
{ |
1017 |
|
db.Close(); |
1018 |
|
AppendText("DB connection closed"); |
1019 |
|
} |
1020 |
|
if(!db.IsOpen()) |
1021 |
|
{ |
1022 |
|
DBConnect(); |
1023 |
|
AppendText("DB connection started again"); |
1024 |
|
} |
1025 |
|
return buffer; |
1026 |
|
} |
1027 |
|
|
1028 |
|
|
1029 |
|
} |