5 |
#include "FlisServer.h" |
#include "FlisServer.h" |
6 |
#include "FlisServerDlg.h" |
#include "FlisServerDlg.h" |
7 |
#include <vector> |
#include <vector> |
8 |
|
#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 |
|
ON_BN_CLICKED(IDC_test, OnBnClickedtest) |
|
68 |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
ON_BN_CLICKED(IDCLOSE, OnBnClickedClose) |
69 |
ON_BN_CLICKED(IDC_GSMPIN, OnBnClickedGsmpin) |
ON_BN_CLICKED(IDC_Start, OnBnClickedStart) |
70 |
END_MESSAGE_MAP() |
END_MESSAGE_MAP() |
71 |
|
|
72 |
|
|
102 |
// TODO: Add extra initialization here |
// TODO: Add extra initialization here |
103 |
StartSerial(); |
StartSerial(); |
104 |
DBConnect(); |
DBConnect(); |
105 |
continueThread = true; |
ResetSms = 0; |
|
//AfxBeginThread(MyMainThread,0,THREAD_PRIORITY_NORMAL,0,0,NULL); |
|
106 |
|
|
107 |
return TRUE; // return TRUE unless you set the focus to a control |
return TRUE; // return TRUE unless you set the focus to a control |
108 |
} |
} |
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 |
} |
} |
|
void CFlisServerDlg::OnBnClickedtest() |
|
|
{ |
|
|
// TODO: Add your control notification handler code here |
|
|
|
|
|
/* |
|
|
////////////////DBRead stuff////////////////// |
|
|
CString testdata; |
|
|
std::vector<Commands> data; |
|
|
data = DBRead(); |
|
|
for (int i=0; i<data.size(); i++) |
|
|
{ |
|
|
testdata.Append(data[i].IDnr); |
|
|
testdata.Append(data[i].CommandID); |
|
|
testdata.Append(data[i].InstallationsID); |
|
|
} |
|
|
m_Textwindow.SetWindowText(testdata); |
|
|
////////////////DBRead stuff////////////////// |
|
|
*/ |
|
|
/* |
|
|
///////////////Send sms stuff///////////////// |
|
|
vector<unsigned char> tlfnr; |
|
|
vector<unsigned char> smsdata; |
|
|
tlfnr.push_back('2'); |
|
|
tlfnr.push_back('9'); |
|
|
tlfnr.push_back('7'); |
|
|
tlfnr.push_back('2'); |
|
|
tlfnr.push_back('2'); |
|
|
tlfnr.push_back('6'); |
|
|
tlfnr.push_back('0'); |
|
|
tlfnr.push_back('3'); |
|
|
|
|
|
SendSmsHead(tlfnr); |
|
|
|
|
|
//35:29860132:75 <--- demo pakke; |
|
|
smsdata.push_back('3'); |
|
|
smsdata.push_back('5'); |
|
|
smsdata.push_back(':'); |
|
|
smsdata.push_back('2'); |
|
|
smsdata.push_back('9'); |
|
|
smsdata.push_back('8'); |
|
|
smsdata.push_back('6'); |
|
|
smsdata.push_back('0'); |
|
|
smsdata.push_back('1'); |
|
|
smsdata.push_back('3'); |
|
|
smsdata.push_back('2'); |
|
|
smsdata.push_back(':'); |
|
|
smsdata.push_back('7'); |
|
|
smsdata.push_back('5'); |
|
|
|
|
|
SendSmsData(smsdata); |
|
|
///////////////Send sms stuff///////////////// |
|
|
*/ |
|
|
/* |
|
|
////////////Start Tråd//////////////////////// |
|
|
MyThread *t = new MyThread(AfxGetMainWnd()); |
|
|
t->start(); |
|
|
////////////Start Tråd//////////////////////// |
|
|
*/ |
|
|
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
|
213 |
|
|
|
} |
|
214 |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
void CFlisServerDlg::writeFrame(std::vector<unsigned char> data) |
215 |
{ |
{ |
216 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<data.size(); i++) |
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 |
|
tekst.MakeLower(); |
258 |
|
int pos = tekst.Find("+cpin: ",0); |
259 |
|
if (pos != -1) |
260 |
|
{ |
261 |
|
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 |
|
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 |
|
for (int i = 0; i<config.pincode.GetLength(); i++) |
285 |
|
{ |
286 |
|
data.push_back(config.pincode[i]); |
287 |
|
} |
288 |
|
|
289 |
|
writeFrame(data); |
290 |
|
|
291 |
|
Sleep(500); |
292 |
|
|
293 |
|
while (ready == true) |
294 |
|
{ |
295 |
|
if (Serial.getComstat().cbInQue > 120) |
296 |
{ |
{ |
297 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
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 |
tekst.AppendChar(array1[i]); |
ready = false; |
311 |
|
} |
312 |
|
else |
313 |
|
{ |
314 |
|
AppendText(tekst); |
315 |
} |
} |
316 |
} |
} |
317 |
m_Textwindow.SetWindowText(tekst); |
Sleep(10); //Small delay to avoid busy wait |
318 |
|
} |
319 |
|
} |
320 |
|
else if (tekst == "cpin: ready") |
321 |
|
{ |
322 |
|
AppendText("No pincode needed"); |
323 |
|
} |
324 |
|
else |
325 |
|
{ |
326 |
|
AppendText("Error in set pin part of the code"); |
327 |
} |
} |
328 |
} |
} |
329 |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsData(std::vector<unsigned char> data) |
341 |
CString tekst; |
CString tekst; |
342 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
343 |
Sleep(50); |
Sleep(50); |
344 |
char array1[25]; |
|
345 |
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++) |
|
346 |
{ |
{ |
347 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
348 |
{ |
{ |
349 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
350 |
} |
} |
351 |
} |
} |
352 |
m_Textwindow.SetWindowText(tekst); |
AppendText(tekst); |
353 |
} |
} |
354 |
|
|
355 |
} |
} |
356 |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> data) |
void CFlisServerDlg::SendSmsHead(std::vector<unsigned char> tlfnr) |
357 |
{ |
{ |
358 |
vector<unsigned char> atcommand; |
vector<unsigned char> atcommand; |
359 |
atcommand.push_back('a'); |
atcommand.push_back('a'); |
373 |
Sleep(5); |
Sleep(5); |
374 |
} |
} |
375 |
|
|
376 |
for (int i=0; i<data.size(); i++) |
for (int i=0; i<tlfnr.size(); i++) |
377 |
{ |
{ |
378 |
Serial.writeByte( data[i] ); |
Serial.writeByte( tlfnr[i] ); |
379 |
Sleep(5); |
Sleep(5); |
380 |
} |
} |
381 |
Serial.writeByte(atcommand[s]); |
Serial.writeByte(atcommand[s]); |
385 |
void CFlisServerDlg::DBConnect() |
void CFlisServerDlg::DBConnect() |
386 |
{ |
{ |
387 |
CString dsn; |
CString dsn; |
388 |
dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=t-hoerup.dk; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); |
ConfigFile config; |
389 |
//dsn.Format("ODBC;Description=asd;DRIVER=PostgreSQL ANSI;SERVER=192.168.134.132; uid=serrenab;password=furnacemonitor;database=flisfyr;sslmode=prefer"); //skole server |
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 |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
db.OpenEx(dsn, CDatabase::noOdbcDialog); |
397 |
} |
} |
398 |
vector<Commands> CFlisServerDlg::DBRead(void) |
vector<Commands> CFlisServerDlg::DBReadCommands(void) |
399 |
{ |
{ |
400 |
vector<Commands> buffer; |
vector<Commands> buffer; |
401 |
|
|
402 |
CString SQL, IDnr, CommandID, InstallationsID; |
CString SQL, IDnr, CommandID, InstallationsID; |
403 |
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;"; |
404 |
CRecordset rs(&db); |
CRecordset rs(&db); |
405 |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
406 |
if (rs.GetRecordCount()>0) |
if (rs.GetRecordCount()>0) |
407 |
{ |
{ |
408 |
rs.MoveFirst(); |
rs.MoveFirst(); |
409 |
while(!rs.IsEOF()) |
|
|
{ |
|
410 |
Commands Mycom; |
Commands Mycom; |
411 |
rs.GetFieldValue((short)0, IDnr); |
rs.GetFieldValue((short)0, IDnr); |
412 |
rs.GetFieldValue(3, CommandID); |
rs.GetFieldValue(3, CommandID); |
418 |
|
|
419 |
buffer.push_back(Mycom); |
buffer.push_back(Mycom); |
420 |
rs.MoveNext(); |
rs.MoveNext(); |
421 |
} |
|
422 |
} |
} |
423 |
rs.Close(); |
rs.Close(); |
424 |
return buffer; |
return buffer; |
425 |
} |
} |
426 |
void CFlisServerDlg::ReadSms() |
void CFlisServerDlg::ReadSms() |
427 |
{ |
{ |
428 |
CString tekst, oldtekst; |
CString tekst; |
429 |
Sleep(950); |
Sleep(950); //Holder en pause for at lade hele sms'en komme ind i serial køen. |
430 |
if(Serial.getComstat().cbInQue > 0) |
if(Serial.getComstat().cbInQue > 0) |
431 |
{ |
{ |
432 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
433 |
Sleep(50); |
Sleep(50); |
434 |
char array1[250]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
|
|
|
435 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
436 |
{ |
{ |
437 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
438 |
{ |
{ |
439 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
440 |
} |
} |
441 |
} |
} |
442 |
|
|
443 |
m_Textwindow.GetWindowText(oldtekst); |
AppendText(tekst); |
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
444 |
SmsSplit(tekst); |
SmsSplit(tekst); |
445 |
} |
} |
446 |
} |
} |
447 |
void CFlisServerDlg::SmsSplit(CString data) |
void CFlisServerDlg::SmsSplit(CString data) |
448 |
{ |
{ |
449 |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail, oldtekst; |
CString FyrData, TlfNr, SmsCount, Temper, Flamme, Flis, FremFejl, PowerFail; |
450 |
char CharData[150]; |
TlfNr = data.Mid(24,8); |
451 |
strcpy(CharData,data); |
FyrData = data.Mid(57,data.GetLength()-59); |
452 |
|
FyrData.Append(":"); |
453 |
|
|
454 |
int s=22; |
if (FyrData.MakeLower() == "conf ok:") |
|
for (int i=0; i<=7; i++) |
|
455 |
{ |
{ |
456 |
TlfNr.AppendChar(CharData[s]); |
HandleAcknowledge(TlfNr); |
457 |
s++; |
return; |
458 |
} |
} |
459 |
|
CAtlRegExp<> regex; |
460 |
|
if (regex.Parse("^{[0-9]+}:{[0-9]+}:[0-1]:[0-1]:[0-1]:[0-1]:$") == REPARSE_ERROR_OK) |
461 |
|
{ |
462 |
|
|
463 |
for (int s=55; s<=(data.GetLength()-3); s++) |
CAtlREMatchContext<> pContext; |
464 |
|
|
465 |
|
if (!regex.Match(FyrData, &pContext)) |
466 |
|
{ |
467 |
|
AppendText("Invalid SMS recieved"); |
468 |
|
return; |
469 |
|
} |
470 |
|
} |
471 |
|
else |
472 |
{ |
{ |
473 |
FyrData.AppendChar(CharData[s]); |
AppendText("Regex PARSE error!!!"); //Burde aldrig kunne ske ! |
474 |
} |
} |
475 |
FyrData.Append(":"); |
|
|
|
|
476 |
SmsCount = Splitter(FyrData); |
SmsCount = Splitter(FyrData); |
477 |
Temper = Splitter(FyrData); |
Temper = Splitter(FyrData); |
478 |
Flamme = Splitter(FyrData); |
Flamme = Splitter(FyrData); |
479 |
Flis = Splitter(FyrData); |
Flis = Splitter(FyrData); |
480 |
FremFejl = Splitter(FyrData); |
FremFejl = Splitter(FyrData); |
481 |
PowerFail = Splitter(FyrData); |
PowerFail = Splitter(FyrData); |
482 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
483 |
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
CString SQL, Textwindow, InstallNR; |
484 |
m_Textwindow.GetWindowText(oldtekst); |
SQL.Format("select ID from installation where installationphonenr=%s",TlfNr); |
485 |
oldtekst.Append("\r\n"); |
|
486 |
oldtekst.Append("På næste linie kommer SmsCount. \r\n"); |
CRecordset rs(&db); |
487 |
oldtekst.Append(SmsCount); |
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
488 |
oldtekst.Append("\r\n"); |
if (rs.GetRecordCount()>0) |
489 |
oldtekst.Append("På næste linie kommer Temper. \r\n"); |
{ |
490 |
oldtekst.Append(Temper); |
rs.MoveFirst(); |
491 |
oldtekst.Append("\r\n"); |
rs.GetFieldValue((short)0,InstallNR); |
492 |
oldtekst.Append("På næste linie kommer Flamme. \r\n"); |
} |
493 |
oldtekst.Append(Flamme); |
rs.Close(); |
494 |
oldtekst.Append("\r\n"); |
|
495 |
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); |
496 |
oldtekst.Append(Flis); |
try |
497 |
oldtekst.Append("\r\n"); |
{ |
498 |
oldtekst.Append("På næste linie kommer FremFejl. \r\n"); |
db.ExecuteSQL(SQL); |
499 |
oldtekst.Append(FremFejl); |
} |
500 |
oldtekst.Append("\r\n"); |
catch(CDBException* e) |
501 |
oldtekst.Append("På næste linie kommer PowerFail. \r\n"); |
{ |
502 |
oldtekst.Append(PowerFail); |
MessageBox(e->m_strError); |
503 |
oldtekst.Append("\r\n"); |
} |
504 |
oldtekst.Append("På næste linie kommer TlfNR. \r\n"); |
AppendText("Sms added to Log"); |
505 |
oldtekst.Append(TlfNr); |
Sleep(150); |
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
///////////////////// Her skal sendes data til databasen ////////////////////////////////////////////// |
|
506 |
} |
} |
507 |
CString CFlisServerDlg::Splitter(CString& fyrdata) |
CString CFlisServerDlg::Splitter(CString& fyrdata) |
508 |
{ |
{ |
519 |
void CFlisServerDlg::OnBnClickedClose() |
void CFlisServerDlg::OnBnClickedClose() |
520 |
{ |
{ |
521 |
// TODO: Add your control notification handler code here |
// TODO: Add your control notification handler code here |
522 |
continueThread = false; |
continueThread = 0; |
523 |
|
Sleep(250); |
524 |
|
DeleteSms(); |
525 |
|
|
526 |
|
Sleep(500); |
527 |
if( Serial.isOpen() ) |
if( Serial.isOpen() ) |
528 |
{ |
{ |
529 |
Serial.close(); |
Serial.close(); |
531 |
|
|
532 |
if(db.IsOpen()) |
if(db.IsOpen()) |
533 |
{ |
{ |
534 |
db.Close(); |
db.Close(); |
535 |
} |
} |
536 |
DeleteSms(); |
|
537 |
OnOK(); |
OnOK(); |
|
} |
|
538 |
|
|
|
void CFlisServerDlg::OnBnClickedGsmpin() |
|
|
{ |
|
|
// TODO: Add your control notification handler code here |
|
|
SetPin(); |
|
|
Sleep(5000); |
|
539 |
} |
} |
540 |
void CFlisServerDlg::DeleteSms() |
void CFlisServerDlg::DeleteSms() |
541 |
{ |
{ |
554 |
|
|
555 |
writeFrame(atcommand); |
writeFrame(atcommand); |
556 |
Sleep(500); |
Sleep(500); |
|
if(Serial.getComstat().cbInQue > 0) |
|
|
{ |
|
|
std::vector<unsigned char> answer = readFrame(); |
|
|
Sleep(50); |
|
|
CString tekst, oldtekst; |
|
|
char array1[250]; |
|
|
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]); |
|
|
} |
|
|
} |
|
|
if(tekst == "OK") |
|
|
{ |
|
|
m_Textwindow.GetWindowText(oldtekst); |
|
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append("Old Sms messages deleted"); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
} |
|
|
else |
|
|
{ |
|
|
m_Textwindow.GetWindowText(oldtekst); |
|
|
oldtekst.Append("\r\n"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
|
} |
|
|
} |
|
557 |
} |
} |
558 |
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) |
|
559 |
{ |
{ |
560 |
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
CFlisServerDlg *t = (CFlisServerDlg*) thread; |
561 |
t->runthread(); |
t->runthread(); |
569 |
|
|
570 |
void CFlisServerDlg::runthread() |
void CFlisServerDlg::runthread() |
571 |
{ |
{ |
572 |
int count = 0; |
while (continueThread != 0) |
|
while (continueThread = true) |
|
573 |
{ |
{ |
574 |
Reader(); |
MyMainThread(); |
575 |
} |
} |
576 |
} |
} |
577 |
void CFlisServerDlg::Reader() |
void CFlisServerDlg::MyMainThread() |
578 |
{ |
{ |
579 |
int ResetSms = 0; |
if(Serial.getComstat().cbInQue > 0) |
|
while(1) |
|
|
{ |
|
|
if(Serial.getComstat().cbInQue > 0) |
|
580 |
{ |
{ |
581 |
|
Sleep(250); |
582 |
std::vector<unsigned char> answer = readFrame(); |
std::vector<unsigned char> answer = readFrame(); |
583 |
Sleep(500); |
Sleep(500); |
584 |
CString tekst, oldtekst; |
CString tekst; |
|
int lol; |
|
|
char array1[250]; |
|
|
int i; |
|
|
for (int i=0; i<answer.size(); i++) |
|
|
{ |
|
|
array1[i] = answer[i]; |
|
|
} |
|
585 |
|
|
586 |
for (int i=0; i<answer.size(); i++) |
for (int i=0; i<answer.size(); i++) |
587 |
{ |
{ |
588 |
if ((array1[i] != 0x0A) && (array1[i] != 0x0D)) |
if ((answer[i] != 0x0A) && (answer[i] != 0x0D)) |
589 |
{ |
{ |
590 |
tekst.AppendChar(array1[i]); |
tekst.AppendChar(answer[i]); |
591 |
} |
} |
592 |
} |
} |
593 |
tekst.Append(":"); |
tekst.Append(":"); |
594 |
|
|
595 |
CString command; |
CString command; |
596 |
bool plus; |
bool plus = false; |
597 |
int pos = tekst.Find('+',0); |
int pos = tekst.Find('+',0); |
598 |
if (pos != -1) |
if (pos != -1) |
599 |
{ |
{ |
605 |
} |
} |
606 |
|
|
607 |
|
|
608 |
if(tekst == "OK") |
if(tekst.MakeLower() == "ok") |
609 |
{ |
{ |
610 |
m_Textwindow.GetWindowText(oldtekst); |
AppendText("OK tekst modtaget"); |
611 |
oldtekst.Append("\r\n"); |
} |
612 |
oldtekst.Append("OK tekst modtaget"); |
else if (tekst.MakeLower() == "error") |
613 |
m_Textwindow.SetWindowText(oldtekst); |
{ |
614 |
} |
CString send; |
615 |
else if (tekst == "error") |
send.Append("error tekst"); |
616 |
{ |
send.Append("\r\n"); |
617 |
m_Textwindow.GetWindowText(oldtekst); |
send.Append(tekst); |
618 |
oldtekst.Append("\r\n"); |
AppendText(send); |
|
oldtekst.Append("error tekst"); |
|
|
oldtekst.Append(tekst); |
|
|
m_Textwindow.SetWindowText(oldtekst); |
|
619 |
} |
} |
620 |
else if (plus = true) |
else if (plus == true) |
621 |
{ |
{ |
622 |
if (command = "cmti") |
if (command.MakeLower() == "cmti") |
623 |
{ |
{ |
624 |
CString smscount; |
CString smscount; |
625 |
int pos = tekst.Find(',',0); |
int pos = tekst.Find(',',0); |
629 |
smscount.Remove(':'); |
smscount.Remove(':'); |
630 |
ResetSms++; |
ResetSms++; |
631 |
} |
} |
|
////////////////Read sms ting////////////////// |
|
|
std::vector<unsigned char> data; |
|
|
data.push_back('a'); |
|
|
data.push_back('t'); |
|
|
data.push_back('+'); |
|
|
data.push_back('c'); |
|
|
data.push_back('m'); |
|
|
data.push_back('g'); |
|
|
data.push_back('r'); |
|
|
data.push_back('='); |
|
|
|
|
|
for (int i=0; i< smscount.GetLength(); i++) |
|
|
{ |
|
|
data.push_back(smscount[i]); |
|
|
} |
|
632 |
|
|
633 |
writeFrame(data); |
std::vector<unsigned char> data; |
634 |
ReadSms(); |
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 |
|
AppendText(CString(&data[0])); |
648 |
|
writeFrame(data); |
649 |
|
Sleep(200); |
650 |
|
ReadSms(); |
651 |
|
|
652 |
|
} |
653 |
|
else if(command.MakeLower() == "wind") |
654 |
|
{ |
655 |
|
|
656 |
|
CString send; |
657 |
|
send.Append("Wind modtaget"); |
658 |
|
send.Append("\r\n"); |
659 |
|
send.Append(tekst); |
660 |
|
AppendText(send); |
661 |
} |
} |
662 |
} |
} |
663 |
if(ResetSms >= 50) |
else |
664 |
|
{ |
665 |
|
AppendText(tekst); |
666 |
|
} |
667 |
|
if(ResetSms == 20) |
668 |
{ |
{ |
669 |
DeleteSms(); |
DeleteSms(); |
670 |
|
AppendText("Sms'er slettet da vi nåede grænsen"); |
671 |
|
} |
672 |
|
|
673 |
|
} |
674 |
|
if (continueThread == 1) |
675 |
|
{ |
676 |
|
|
677 |
|
CString testdata, dataframe,testprint, sIDnr, sCommandID, sInstallationsID, sImei; |
678 |
|
int commandtest = 0; |
679 |
|
int iAll = 1; |
680 |
|
std::vector<Commands> data; |
681 |
|
data = DBReadCommands(); |
682 |
|
for (int i=0; i<data.size(); i++) |
683 |
|
{ |
684 |
|
sIDnr = data[i].IDnr; |
685 |
|
sCommandID = data[i].CommandID; |
686 |
|
commandtest = atoi(data[i].CommandID); |
687 |
|
sInstallationsID = "0"; |
688 |
|
|
689 |
|
if(commandtest > 1) |
690 |
|
{ |
691 |
|
sInstallationsID = data[i].InstallationsID; |
692 |
|
} |
693 |
} |
} |
694 |
|
|
695 |
|
if (commandtest > 1) |
696 |
|
{ |
697 |
|
iAll = 0; |
698 |
|
} |
699 |
|
|
700 |
|
if (sIDnr.GetLength() > 0) |
701 |
|
{ |
702 |
|
SendConfig(sIDnr,sCommandID,sInstallationsID); |
703 |
|
} |
704 |
|
Sleep(300); |
705 |
|
|
706 |
|
CheckAcknowledges(); |
707 |
|
Sleep(500); |
708 |
|
} |
709 |
|
CTime now = CTime::GetCurrentTime(); |
710 |
|
|
711 |
|
CTimeSpan elapsed = now-Alive.tid; |
712 |
|
|
713 |
|
if (elapsed.GetTotalSeconds() >= 900) |
714 |
|
{ |
715 |
|
keepaliveandread(); |
716 |
|
} |
717 |
|
} |
718 |
|
void CFlisServerDlg::SendConfig(CString IDnr,CString CommandID,CString InstallationsID) |
719 |
|
{ |
720 |
|
|
721 |
|
std::vector<Installation> inst; |
722 |
|
|
723 |
|
if (CommandID == "1") |
724 |
|
inst = DBReadPhone("0"); |
725 |
|
else |
726 |
|
inst = DBReadPhone(InstallationsID); |
727 |
|
|
728 |
|
for ( int j=0; j < inst.size(); j++) |
729 |
|
{ |
730 |
|
CString TlfNr, Imei, updaterate; |
731 |
|
TlfNr.Empty(); |
732 |
|
Imei.Empty(); |
733 |
|
updaterate.Empty(); |
734 |
|
|
735 |
|
CString dataen = inst[j].InstPhoneNr; |
736 |
|
TlfNr.Append(dataen); |
737 |
|
CString Imeidata = inst[j].Imei; |
738 |
|
Imei.Append(Imeidata); |
739 |
|
CString updaterat = inst[j].Updaterate; |
740 |
|
updaterate.Append(updaterat); |
741 |
|
|
742 |
|
vector<unsigned char> tlfnr; |
743 |
|
for (int i=0; i<TlfNr.GetLength(); i++) |
744 |
|
{ |
745 |
|
tlfnr.push_back(TlfNr[i]); |
746 |
|
} |
747 |
|
|
748 |
|
int calcimei; |
749 |
|
__int64 buf; |
750 |
|
|
751 |
|
buf = atof(Imei); |
752 |
|
|
753 |
|
calcimei = tversum(buf); |
754 |
|
|
755 |
|
Imei.Format("%d",calcimei); |
756 |
|
|
757 |
|
SendSmsHead(tlfnr); |
758 |
|
Sleep(250); |
759 |
|
|
760 |
|
vector<unsigned char> smsdata; |
761 |
|
|
762 |
|
for (int i=0; i<Imei.GetLength(); i++) |
763 |
|
{ |
764 |
|
smsdata.push_back(Imei[i]); |
765 |
|
} |
766 |
|
smsdata.push_back(':'); |
767 |
|
for (int i=0; i<Alive.Phonenr.GetLength(); i++) |
768 |
|
{ |
769 |
|
smsdata.push_back(Alive.Phonenr[i]); |
770 |
|
} |
771 |
|
smsdata.push_back(':'); |
772 |
|
for (int i=0; i<updaterate.GetLength(); i++) |
773 |
|
{ |
774 |
|
smsdata.push_back(updaterate[i]); |
775 |
|
} |
776 |
|
|
777 |
|
SendSmsData(smsdata); |
778 |
|
Sleep(500); |
779 |
|
|
780 |
|
if (IDnr != "0") |
781 |
|
{ |
782 |
|
Acks.push_back( Acknowledge(inst[j].instID,TlfNr) ); |
783 |
|
} |
784 |
|
|
785 |
|
} |
786 |
|
|
787 |
|
if (IDnr != "0") |
788 |
|
{ |
789 |
|
CString SQL, Textwindow; |
790 |
|
SQL.Format("update command set executed=now() where id=%s",IDnr); |
791 |
|
db.ExecuteSQL(SQL); |
792 |
|
Sleep(100); |
793 |
|
AppendText("Command executed"); |
794 |
|
} |
795 |
|
Sleep(150); |
796 |
|
|
797 |
|
|
798 |
|
|
799 |
|
|
800 |
|
} |
801 |
|
vector<Installation> CFlisServerDlg::DBReadPhone(CString sInstallationsID) |
802 |
|
{ |
803 |
|
vector<Installation> buffer; |
804 |
|
|
805 |
|
CString SQL, phonenr, imei, updaterate, id; |
806 |
|
Installation Myinst; |
807 |
|
|
808 |
|
if (sInstallationsID != "0") |
809 |
|
{ |
810 |
|
SQL.Format("select installationphonenr, imei, updaterate, id from installation WHERE id = %s", sInstallationsID); |
811 |
|
} |
812 |
|
else |
813 |
|
{ |
814 |
|
SQL.Format("select installationphonenr, imei, updaterate, id from installation"); |
815 |
|
} |
816 |
|
|
817 |
|
CRecordset rs(&db); |
818 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
819 |
|
if (rs.GetRecordCount()>0) |
820 |
|
{ |
821 |
|
rs.MoveFirst(); |
822 |
|
while(!rs.IsEOF()) |
823 |
|
{ |
824 |
|
|
825 |
|
rs.GetFieldValue((short)0,phonenr); |
826 |
|
rs.GetFieldValue(1,imei); |
827 |
|
rs.GetFieldValue(2,updaterate); |
828 |
|
rs.GetFieldValue(3,id); |
829 |
|
|
830 |
|
Myinst.InstPhoneNr = phonenr; |
831 |
|
Myinst.Imei = imei; |
832 |
|
Myinst.Updaterate = updaterate; |
833 |
|
Myinst.instID = id; |
834 |
|
|
835 |
|
buffer.push_back(Myinst); |
836 |
|
rs.MoveNext(); |
837 |
|
} |
838 |
|
} |
839 |
|
rs.Close(); |
840 |
|
return buffer; |
841 |
|
} |
842 |
|
int CFlisServerDlg::tversum(__int64 input) |
843 |
|
{ |
844 |
|
int sum = 0; |
845 |
|
while (input > 0) |
846 |
|
{ |
847 |
|
sum += (input %10); |
848 |
|
input /= 10; |
849 |
|
} |
850 |
|
return sum; |
851 |
|
} |
852 |
|
void CFlisServerDlg::OnBnClickedStart() |
853 |
|
{ |
854 |
|
// TODO: Add your control notification handler code here |
855 |
|
|
856 |
|
AppendText("Indsætter Pinkode, og venter på modem bliver klar, hvis pinkode er nødvendig"); |
857 |
|
UpdateWindow(); |
858 |
|
SetPin(); |
859 |
|
|
860 |
|
Sleep(100); //Give the modem a chance to send the last data |
861 |
|
while(Serial.getComstat().cbInQue > 0) |
862 |
|
{ |
863 |
|
Serial.readByte(); //Flush the incoming queue |
864 |
|
Sleep(1); |
865 |
|
} |
866 |
|
Sleep(5); |
867 |
|
|
868 |
|
continueThread = 1; |
869 |
|
keepaliveandread(); |
870 |
|
Sleep(150); |
871 |
|
AppendText(CString("Server phonenr read from db: ") + Alive.Phonenr ); |
872 |
|
AppendText("Started"); |
873 |
|
AfxBeginThread(threadWrapper,AfxGetMainWnd()); |
874 |
|
} |
875 |
|
void CFlisServerDlg::AppendText(CString s) |
876 |
|
{ |
877 |
|
CTime now = CTime::GetCurrentTime(); |
878 |
|
CString Tekst; |
879 |
|
m_Textwindow.GetWindowText(Tekst); |
880 |
|
Tekst.AppendFormat("%02d:%02d:%02d : ", now.GetHour(), now.GetMinute(), now.GetSecond()); |
881 |
|
Tekst.Append(s); |
882 |
|
Tekst.Append("\r\n"); |
883 |
|
|
884 |
|
m_Textwindow.SetWindowText(Tekst); |
885 |
|
Sleep(5); |
886 |
|
int g = m_Textwindow.GetScrollLimit(SB_VERT); |
887 |
|
if (g > 0) |
888 |
|
{ |
889 |
|
m_Textwindow.SetSel(m_Textwindow.GetWindowTextLength() -1,m_Textwindow.GetWindowTextLength(),false); |
890 |
|
} |
891 |
|
|
892 |
|
} |
893 |
|
void ConfigFile::ReadSettings() |
894 |
|
{ |
895 |
|
ifstream file("Server-Settings.ini"); |
896 |
|
if (!file.is_open()) |
897 |
|
throw("Could not open file"); |
898 |
|
|
899 |
|
char buf[200]; |
900 |
|
while (!file.eof() ) { |
901 |
|
file.getline(buf,200); |
902 |
|
CString tmp(buf); |
903 |
|
|
904 |
|
if (tmp.GetAt(0) == '#') |
905 |
|
continue; |
906 |
|
|
907 |
|
int pos = tmp.Find('='); |
908 |
|
if (pos>0) { |
909 |
|
CString key = tmp.Left(pos).Trim().MakeLower();; |
910 |
|
CString value = tmp.Right(tmp.GetLength()-pos-1).Trim(); |
911 |
|
|
912 |
|
if (key == "host") |
913 |
|
host = value; |
914 |
|
else if (key == "username") |
915 |
|
username = value; |
916 |
|
else if (key == "password") |
917 |
|
password = value; |
918 |
|
else if (key == "database") |
919 |
|
database = value; |
920 |
|
else if (key == "comport") |
921 |
|
comport = value; |
922 |
|
else if (key == "pincode") |
923 |
|
pincode = value; |
924 |
|
} |
925 |
|
|
926 |
|
} |
927 |
|
|
928 |
|
file.close(); |
929 |
|
} |
930 |
|
void CFlisServerDlg::HandleAcknowledge(CString tlfnr) |
931 |
|
{ |
932 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
933 |
|
{ |
934 |
|
Acknowledge& current = (*it); |
935 |
|
if (current.sTlfNr == tlfnr) |
936 |
|
{ |
937 |
|
CString SQL; |
938 |
|
SQL.Format("UPDATE installation SET commerror = false WHERE id = %s", current.sInstallationID); |
939 |
|
db.ExecuteSQL(SQL); |
940 |
|
|
941 |
|
Acks.erase(it); |
942 |
|
|
943 |
|
return; |
944 |
|
} |
945 |
|
} |
946 |
|
} |
947 |
|
|
948 |
|
void CFlisServerDlg::CheckAcknowledges() |
949 |
|
{ |
950 |
|
CTime now = CTime::GetCurrentTime(); |
951 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
952 |
|
{ |
953 |
|
Acknowledge& current = (*it); |
954 |
|
|
955 |
|
CTimeSpan elapsed = now-current.cTime; |
956 |
|
|
957 |
|
if (elapsed.GetTotalSeconds() >= 300) |
958 |
|
{ |
959 |
|
DumpAckList(); |
960 |
|
if (current.iRetry >= 1) |
961 |
|
{ |
962 |
|
CString SQL; |
963 |
|
SQL.Format("UPDATE installation SET commerror = true WHERE id = %s", current.sInstallationID); |
964 |
|
db.ExecuteSQL(SQL); |
965 |
|
Acks.erase(it); |
966 |
|
return; // the iterator is now invalidated, but the thread loop will make sure we are here soon again |
967 |
|
} |
968 |
|
else |
969 |
|
{ |
970 |
|
SendConfig("0","2",current.sInstallationID); |
971 |
|
current.iRetry++; |
972 |
|
current.cTime = CTime::GetCurrentTime(); |
973 |
|
AppendText( CString("Retry send config to ") + current.sTlfNr ); |
974 |
|
} |
975 |
|
} |
976 |
|
} |
977 |
|
} |
978 |
|
|
979 |
|
void CFlisServerDlg::DumpAckList() /* Debug tool */ |
980 |
|
{ |
981 |
|
|
982 |
|
CTime now = CTime::GetCurrentTime(); |
983 |
|
OutputDebugString("-------------------------------\n"); |
984 |
|
int count = 0; |
985 |
|
for (std::list<Acknowledge>::iterator it = Acks.begin(); it != Acks.end(); ++it) |
986 |
|
{ |
987 |
|
CString msg; |
988 |
|
msg.Format("%d:%d:%d> %4d : %s\n", now.GetHour(), now.GetMinute(), now.GetSecond(), count, (*it).sTlfNr); |
989 |
|
OutputDebugString(msg); |
990 |
|
count++; |
991 |
|
} |
992 |
|
} |
993 |
|
vector<keepalive> CFlisServerDlg::keepaliveandread(void) |
994 |
|
{ |
995 |
|
vector<keepalive> buffer; |
996 |
|
|
997 |
|
CString SQL, name, value; |
998 |
|
SQL = "select name, value from config;"; |
999 |
|
CRecordset rs(&db); |
1000 |
|
try |
1001 |
|
{ |
1002 |
|
rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL); |
1003 |
|
if (rs.GetRecordCount()>0) |
1004 |
|
{ |
1005 |
|
rs.MoveFirst(); |
1006 |
|
|
1007 |
|
rs.GetFieldValue((short)0, name); |
1008 |
|
rs.GetFieldValue(1,value); |
1009 |
|
|
1010 |
|
Alive.name = name; |
1011 |
|
Alive.value = value; |
1012 |
|
Alive.tid = CTime::GetCurrentTime(); |
1013 |
|
if(Alive.name == "phonenr") |
1014 |
|
{ |
1015 |
|
Alive.Phonenr = value; |
1016 |
|
} |
1017 |
|
|
1018 |
|
buffer.push_back(Alive); |
1019 |
|
rs.MoveNext(); |
1020 |
|
|
1021 |
} |
} |
1022 |
|
rs.Close(); |
1023 |
|
return buffer; |
1024 |
} |
} |
|
} |
|
1025 |
|
catch (char * str) |
1026 |
|
{ |
1027 |
|
AppendText("Keepalive failed, closing and opening the db connection again"); |
1028 |
|
if(db.IsOpen()) |
1029 |
|
{ |
1030 |
|
db.Close(); |
1031 |
|
AppendText("DB connection closed"); |
1032 |
|
} |
1033 |
|
if(!db.IsOpen()) |
1034 |
|
{ |
1035 |
|
DBConnect(); |
1036 |
|
AppendText("DB connection started again"); |
1037 |
|
} |
1038 |
|
return buffer; |
1039 |
|
} |
1040 |
|
|
1041 |
|
|
1042 |
|
} |