using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace FillingColumn { /// /// Логика взаимодействия для RefuelingPage.xaml /// public partial class RefuelingPage : Page { public RefuelingPage() { InitializeComponent(); var timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 5); timer.IsEnabled = true; timer.Tick += Timer_Tick; timer.Start(); AddTypeOFRefueling(); CmbTypeOfFuel.ItemsSource = Helper.CurrentGasStation.FuelInCurrentGasStation; Title = $"Работа с колонкой на АЗС {Helper.CurrentGasStation.GasStationID}"; } //Событие таймера private async void Timer_Tick(object sender, EventArgs e) { try { HttpClient client = new HttpClient(); var response = await client.GetAsync("http://127.0.0.1:7070/getDataFromCamera/"); var Body = await response.Content.ReadAsStringAsync(); DataFromCamera dataFromCamera = JsonConvert.DeserializeObject(Body); TxtDataOfCamera.Text = "Гос. номер распознан: " + dataFromCamera.CarNumber; FindCar(Body); } catch (Exception) { TxtDataOfCamera.Text = "Нет ответа от камеры"; Helper.FindCar = null; } } public async void FindCar(string JsonDataOfCamera) { string IDGasStation = Helper.CurrentGasStation.GasStationID.ToString(); if (IDGasStation.Length == 1) { IDGasStation = IDGasStation.Insert(0, "0"); } string port = "102" + IDGasStation; IPEndPoint IpPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32(port)); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(IpPoint); //Отправка данных с камеры на сервер АЗС string request = "POST data of camera=" + JsonDataOfCamera; byte[] data = Encoding.UTF8.GetBytes(request); socket.Send(data); //Получение ответа от сервера АЗС data = new byte[256]; // буфер для ответа StringBuilder builder = new StringBuilder(); int bytes = 0; // количество полученных байт do { bytes = socket.Receive(data, data.Length, 0); builder.Append(Encoding.UTF8.GetString(data, 0, bytes)); } while (socket.Available > 0); //Десериализация данных о машине Helper.FindCar = JsonConvert.DeserializeObject(builder.ToString()); AddTypeOFRefueling(); // закрываем сокет socket.Shutdown(SocketShutdown.Both); socket.Close(); } public void AddTypeOFRefueling() { List TypeOfRefueling = new List(); TypeOfRefueling.Add("Фиксированный объем"); TypeOfRefueling.Add("Фиксированная цена"); List TypeOfPayment = new List(); TypeOfPayment.Add("Банковской картой"); if (Helper.FindCar != null) { if (Helper.FindCar.VolumeTank != null) { TypeOfRefueling.Add("До полного бака с ограничением по объему"); } if (Helper.FindCar.CardOfCars.FirstOrDefault(x=>x.IDCardType == 3) != null) { TypeOfPayment.Add("Накопительной картой"); } if (Helper.FindCar.CardOfCars.FirstOrDefault(x => x.IDCardType == 2) != null) { TypeOfPayment.Add("Кредитной картой"); } } CmbTypeOfPayment.ItemsSource = TypeOfPayment; CmbTypeOfRefueling.ItemsSource = TypeOfRefueling; } private void BtnEnterDataOfCard_Click(object sender, RoutedEventArgs e) { AddDataOfCardWindow addDataOfCardWindow = new AddDataOfCardWindow(); addDataOfCardWindow.ShowDialog(); } private void BtnPayment_Click(object sender, RoutedEventArgs e) { } private void CmbTypeOfRefueling_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (CmbTypeOfRefueling.SelectedIndex == 0) { TbAmount.Visibility = Visibility.Visible; TxtAmount.Visibility = Visibility.Hidden; TbCost.Visibility = Visibility.Hidden; TxtCost.Visibility = Visibility.Visible; } if (CmbTypeOfRefueling.SelectedIndex == 1) { TbAmount.Visibility = Visibility.Hidden; TxtAmount.Visibility = Visibility.Visible; TbCost.Visibility = Visibility.Visible; TxtCost.Visibility = Visibility.Hidden; } } private void TbAmount_TextChanged(object sender, TextChangedEventArgs e) { if (TbAmount.Text.Length == 0) { TxtErroreMessage.Visibility = Visibility.Hidden; TxtCost.Text = string.Empty; return; } if (TbAmount.Text.All(char.IsDigit) == true) { TxtErroreMessage.Visibility = Visibility.Hidden; var CurrentFuel = CmbTypeOfFuel.SelectedItem as FuelInCurrentGasStation; double cost = Convert.ToInt32(TbAmount.Text) * CurrentFuel.Price; TxtCost.Text = cost.ToString("F2") + " руб."; } else { TxtErroreMessage.Text = "Не корректное количество!"; TxtErroreMessage.Visibility = Visibility.Visible; } } private void TbCost_TextChanged(object sender, TextChangedEventArgs e) { if (TbCost.Text.Length == 0) { TxtErroreMessage.Visibility = Visibility.Hidden; TxtAmount.Text = string.Empty; return; } if (TbCost.Text.All(char.IsDigit) == true) { TxtErroreMessage.Visibility = Visibility.Hidden; var CurrentFuel = CmbTypeOfFuel.SelectedItem as FuelInCurrentGasStation; double Amount = Convert.ToDouble(TbCost.Text) / CurrentFuel.Price; TxtAmount.Text = Amount.ToString("F2") + " л."; } else { TxtErroreMessage.Text = "Не корректная сумма!"; TxtErroreMessage.Visibility = Visibility.Visible; } } private void CmbTypeOfPayment_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (CmbTypeOfPayment.SelectedValue.ToString() == "Банковской картой") { BtnEnterDataOfCard.Visibility = Visibility.Visible; } else { BtnEnterDataOfCard.Visibility = Visibility.Hidden; } } private void CmbTypeOfFuel_SelectionChanged(object sender, SelectionChangedEventArgs e) { TxtAmount.Text = ""; TbAmount.Text = ""; TxtCost.Text = ""; TbCost.Text = ""; } } }