private void ShowProgress(StatusData msg) { Thread.BeginCriticalRegion(); gotmsg = msg; try { if (msg.status == -1) { AddToLogList(MsgLogType.CommErr, "", 0, "", 0, msg.carddata); bWGTInError = true; pup.SetStatus(PumpStatus.Stopped); return; } ActiveNozz = pup.GetNozz(); //check that we were in error and update log if (bWGTInError) { bWGTInError = false; AddToLogList(MsgLogType.WGTConnected, "", 0, "", 0, ""); } limit = 0.0; plate = ""; reason = ""; type = ""; cardtoauth = ""; statustouse = 0x30; if (ActiveNozz == 1) { cardtoauth = msg.carddata; statustouse = msg.status; } else if (ActiveNozz == 2) { cardtoauth = msg.carddata2; statustouse = msg.status2; } else //no nozzle is active { return; } //check if anything changed if (cardtoauth == laststatus.carddata && statustouse == laststatus.status) { return; //no change } laststatus.carddata = cardtoauth; laststatus.status = statustouse; switch (ps) { case PumpStatus.Idle: //try authorize card if (CheckAuth(cardtoauth, ActiveNozz, ref limit, ref type, ref plate, ref reason)) { ps = PumpStatus.Ready; pup.SetStatus(ps, limit, dd.type, dd.plate, conf.flowrate, -1); AddToLogList(MsgLogType.Authorized, reason, 0, plate, limit, msg.carddata); (MainPage.Document.GetElementById("Plate")).InnerHtml = "Welcome " + plate; if (type == "Money") { (MainPage.Document.GetElementById("Limit")).InnerHtml = limit.ToString("0.00") + " " + conf.LimitPerMoneyText; } else { (MainPage.Document.GetElementById("Limit")).InnerHtml = limit.ToString("0.00") + " " + conf.LimitPerVolText; } } else { if (statustouse == 0x30) { AddToLogList(MsgLogType.Disconnected, "", 0, "", 0, ""); return; } if (msg.status == -1) { AddToLogList(MsgLogType.CommErr, "", 0, plate, limit, msg.carddata); return; } AddToLogList(MsgLogType.CannotAuth, reason, 0, plate, 0, msg.carddata); } break; case PumpStatus.Call: if (cardtoauth != "") { MobileRes = "None"; CPassTry = 0; MsgID++; AddToLogList(MsgLogType.CheckAuth, "", 0, "", 0, cardtoauth); myTimer.Start(); } /* * if (CheckAuth(cardtoauth,ActiveNozz, ref limit, ref type, ref plate,ref reason)) * { * ps = PumpStatus.InUse; * pup.ResetVol(); * pup.SetStatus(ps, limit, dd.type, dd.plate, conf.flowrate, -1); * AddToLogList(MsgLogType.Authorized, reason, 0, plate, limit, msg.carddata); * (MainPage.Document.GetElementById("Plate")).InnerHtml = plate; * if (type == "Money") * (MainPage.Document.GetElementById("Limit")).InnerHtml = limit.ToString("0.00") + " " + conf.LimitPerMoneyText; * else * (MainPage.Document.GetElementById("Limit")).InnerHtml = limit.ToString("0.00") + " " + conf.LimitPerVolText; * } * else * { * if (statustouse != 0x30) * AddToLogList(MsgLogType.CannotAuth, reason, 0, plate, limit, msg.carddata); * } */ break; case PumpStatus.InUse: //check if we are 'out' if (statustouse == 0x30) { ps = PumpStatus.Stopped; pup.SetStatus(ps); //TODO ActivityList.Items.Insert(0, "Nozzle is out - suspending fuelling " + msg.carddata); AddToLogList(MsgLogType.Suspending, reason, 0, plate, limit, msg.carddata); break; } //check that we did not got other card if (cardtoauth != fuellingcardnumber) { //stop fuelling ps = PumpStatus.Stopped; pup.SetStatus(ps); //TODO ActivityList.Items.Insert(0, "Got another card - suspending fuelling " + msg.carddata); AddToLogList(MsgLogType.OtherCard, "", 0, plate, limit, msg.carddata); } break; case PumpStatus.Stopped: //check if we can resume if (cardtoauth == fuellingcardnumber) { ps = PumpStatus.InUse; pup.SetStatus(ps); AddToLogList(MsgLogType.Resuming, reason, 0, plate, limit, cardtoauth); } else if (cardtoauth != "") { // AddToLogList(MsgLogType.OtherCard, reason, 0, plate, limit, cardtoauth); } break; case PumpStatus.Ready: //if we lost the connection cancle the auth if (statustouse == 0x30) { ps = PumpStatus.Idle; pup.SetStatus(ps, 0); AddToLogList(MsgLogType.CancelAuth, reason, 0, plate, limit, cardtoauth); } //if we got a different card - need to recheck if (statustouse == 0x34 && fuellingcardnumber != cardtoauth) { if (CheckAuth(cardtoauth, ActiveNozz, ref limit, ref type, ref plate, ref reason)) { ps = PumpStatus.Ready; pup.SetStatus(ps, limit, dd.type, dd.plate, conf.flowrate, -1); AddToLogList(MsgLogType.NewCard, reason, 0, plate, limit, cardtoauth); (MainPage.Document.GetElementById("Plate")).InnerHtml = "Welcome " + plate; if (type == "Money") { (MainPage.Document.GetElementById("Limit")).InnerHtml = limit.ToString("0.00") + " " + conf.LimitPerMoneyText; } else { (MainPage.Document.GetElementById("Limit")).InnerHtml = limit.ToString("0.00") + " " + conf.LimitPerVolText; } } else { ps = PumpStatus.Idle; pup.SetStatus(ps, 0); AddToLogList(MsgLogType.CannotAuth, reason, 0, plate, limit, msg.carddata); } } break; } } catch (Exception ex) { // MessageBox.Show(ex.Message); } finally { Thread.EndCriticalRegion(); } }
public void Worker() { StatusData stat = new StatusData(); stat.status = -1; //error stat.carddata = ""; int laststatus1 = 0x30; //no data stat.status2 = -1; //error stat.carddata2 = ""; int laststatus2 = 0x30; //no data int count = 0; while (true) { if (tcpClient == null || !tcpClient.Connected) { try { tcpClient = new TcpClient(); //tcpClient.BeginConnect(_Host, _Port, new AsyncCallback(ConnectCallback1), tcpClient); tcpClient.Connect(_Host, _Port); //if (connectDone.WaitOne()) { /* * myLog.Log("network connection failed!"); * bConnected = false; * stat.carddata = ""; * stat.status = -1; * laststatus1 = -1; //no data * stat.carddata2 = ""; * stat.status2 = -1; * laststatus2 = -1; //no data * m_sender.BeginInvoke(m_senderDelegate, stat); * continue; */ bConnected = true; stat.status = 0x30; stat.status2 = 0x30; m_sender.BeginInvoke(m_senderDelegate, stat); } } catch (SocketException e) { Console.WriteLine(e.Message); tcpClient.Close(); tcpClient = null; bConnected = false; stat.status = -1; laststatus1 = -1; //no data stat.carddata = ""; stat.status2 = -1; laststatus2 = -1; //no data stat.carddata2 = ""; m_sender.BeginInvoke(m_senderDelegate, stat); Thread.Sleep(5000); continue; } catch (Exception exp) { myLog.Log(exp.Message); tcpClient.Close(); tcpClient = null; bConnected = false; laststatus1 = -1; //no data stat.status = -1; stat.carddata = ""; laststatus2 = -1; //no data stat.status2 = -1; stat.carddata2 = ""; m_sender.BeginInvoke(m_senderDelegate, stat); Thread.Sleep(5000); continue; } } //!bConnected try { Stream stream = tcpClient.GetStream(); stream.ReadTimeout = 5000; MakePollMessage(); myLog.Log("SEND: " + GetString(_Msg, 10)); stream.Write(_Msg, 0, 10); if (((count = Recv(tcpClient, _Msg)) == -1) || _Msg[3] != 0x01) { if (count == -1) { myLog.Log("RECV: Error in read: "); } else { laststatus1 = laststatus2 = -1; // 0x00; //reset myLog.Log("RECV: Error in read: " + GetString(_Msg, count)); } continue; } myLog.Log("RECV: " + GetString(_Msg, count)); int Chann2Stat = _Msg[8]; //check status of the 1st channel - we check on same data - will get on second poll if (((_Msg[7] == 0x35 || _Msg[7] == 0x34) && (laststatus1 == 0x30 || laststatus1 == -1)) || (_Msg[7] == 0x34 && laststatus1 == 0x35) || (_Msg[7] == 0x35 && laststatus1 == 0x34)) //only if we did not have any thing before { laststatus1 = _Msg[7]; //save it MakeGetDataMessage(1); myLog.Log("SEND: " + GetString(_Msg, 10)); stream.Write(_Msg, 0, 10); if (((count = Recv(tcpClient, _Msg)) == -1) || _Msg[3] != 0x22) { tcpClient.Close(); if (count == -1) { myLog.Log("Error in read"); } else { myLog.Log("RECV: Error in read: " + GetString(_Msg, count)); } continue; } myLog.Log("RECV: " + GetString(_Msg, count)); int i1 = System.Text.Encoding.Default.GetString(_Msg).IndexOf("?;"); int i2 = System.Text.Encoding.Default.GetString(_Msg).IndexOf("="); if (i2 <= i1) { if (i2 == 0xc) //other type? { stat.carddata = System.Text.Encoding.Default.GetString(_Msg).Substring(i2 + 1, 16); } else { myLog.Log("Bad card format = did not find track2 card number."); stat.carddata = ""; //no data } } else { stat.carddata = System.Text.Encoding.Default.GetString(_Msg).Substring(i1 + 2, i2 - i1 - 2); } stat.status = laststatus1; } else { // myLog.Log("What did we got"); } if ((laststatus1 == 0x35 || laststatus1 == 0x34) && _Msg[7] == 0x30) //no data after we had some { laststatus1 = 0x30; stat.status = laststatus1; stat.carddata = ""; stat.nozz = 1; } //second chan //check status of the 1st channel - we check on same data - will get on second poll if (((Chann2Stat == 0x35 || Chann2Stat == 0x34) && (laststatus2 == 0x30 || laststatus2 == -1)) || (Chann2Stat == 0x34 && laststatus2 == 0x35)) //only if we did not have any thing before { laststatus2 = Chann2Stat; //save it MakeGetDataMessage(2); myLog.Log("SEND: " + GetString(_Msg, 10)); stream.Write(_Msg, 0, 10); if (((count = Recv(tcpClient, _Msg)) == -1) || _Msg[3] != 0x22) { if (count == -1) { myLog.Log("Error in read"); } else { myLog.Log("RECV: Error in read: " + GetString(_Msg, count)); } continue; } myLog.Log("RECV: " + GetString(_Msg, count)); int i1 = System.Text.Encoding.Default.GetString(_Msg).IndexOf("?;"); int i2 = System.Text.Encoding.Default.GetString(_Msg).IndexOf("="); if (i2 <= i1) { myLog.Log("Bad card format = did not find track2 card number."); stat.carddata2 = ""; //no data } else { stat.carddata2 = System.Text.Encoding.Default.GetString(_Msg).Substring(i1 + 2, i2 - i1 - 2); } stat.status2 = laststatus2; } if ((laststatus2 == 0x35 || laststatus2 == 0x34) && Chann2Stat == 0x30) //no data after we had some { laststatus2 = 0x30; stat.status2 = laststatus2; stat.carddata2 = ""; stat.nozz2 = 2; } //second chan m_sender.BeginInvoke(m_senderDelegate, stat); Thread.Sleep(200); } catch (Exception exp) { myLog.Log(exp.Message); tcpClient.Close(); tcpClient = null; bConnected = false; laststatus1 = -1; //no data stat.status = -1; stat.carddata = ""; laststatus2 = -1; //no data stat.status2 = -1; stat.carddata2 = ""; m_sender.BeginInvoke(m_senderDelegate, stat); Thread.Sleep(5000); continue; } } }