RefuelingPage.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using System.Windows.Threading;
  20. namespace FillingColumn
  21. {
  22. /// <summary>
  23. /// Логика взаимодействия для RefuelingPage.xaml
  24. /// </summary>
  25. public partial class RefuelingPage : Page
  26. {
  27. public RefuelingPage()
  28. {
  29. InitializeComponent();
  30. var timer = new DispatcherTimer();
  31. timer.Interval = new TimeSpan(0, 0, 5);
  32. timer.IsEnabled = true;
  33. timer.Tick += Timer_Tick;
  34. timer.Start();
  35. AddTypeOFRefueling();
  36. CmbTypeOfFuel.ItemsSource = Helper.CurrentGasStation.FuelInCurrentGasStation;
  37. Title = $"Работа с колонкой на АЗС {Helper.CurrentGasStation.GasStationID}";
  38. }
  39. //Событие таймера
  40. private async void Timer_Tick(object sender, EventArgs e)
  41. {
  42. try
  43. {
  44. HttpClient client = new HttpClient();
  45. var response = await client.GetAsync("http://127.0.0.1:7070/getDataFromCamera/");
  46. var Body = await response.Content.ReadAsStringAsync();
  47. DataFromCamera dataFromCamera = JsonConvert.DeserializeObject<DataFromCamera>(Body);
  48. TxtDataOfCamera.Text = "Гос. номер распознан: " + dataFromCamera.CarNumber;
  49. FindCar(Body);
  50. }
  51. catch (Exception)
  52. {
  53. TxtDataOfCamera.Text = "Нет ответа от камеры";
  54. Helper.FindCar = null;
  55. }
  56. }
  57. public async void FindCar(string JsonDataOfCamera)
  58. {
  59. string IDGasStation = Helper.CurrentGasStation.GasStationID.ToString();
  60. if (IDGasStation.Length == 1)
  61. {
  62. IDGasStation = IDGasStation.Insert(0, "0");
  63. }
  64. string port = "102" + IDGasStation;
  65. IPEndPoint IpPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32(port));
  66. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  67. socket.Connect(IpPoint);
  68. //Отправка данных с камеры на сервер АЗС
  69. string request = "POST data of camera=" + JsonDataOfCamera;
  70. byte[] data = Encoding.UTF8.GetBytes(request);
  71. socket.Send(data);
  72. //Получение ответа от сервера АЗС
  73. data = new byte[256]; // буфер для ответа
  74. StringBuilder builder = new StringBuilder();
  75. int bytes = 0; // количество полученных байт
  76. do
  77. {
  78. bytes = socket.Receive(data, data.Length, 0);
  79. builder.Append(Encoding.UTF8.GetString(data, 0, bytes));
  80. }
  81. while (socket.Available > 0);
  82. //Десериализация данных о машине
  83. Helper.FindCar = JsonConvert.DeserializeObject<RootDataOfCar>(builder.ToString());
  84. AddTypeOFRefueling();
  85. // закрываем сокет
  86. socket.Shutdown(SocketShutdown.Both);
  87. socket.Close();
  88. }
  89. public void AddTypeOFRefueling()
  90. {
  91. List<string> TypeOfRefueling = new List<string>();
  92. TypeOfRefueling.Add("Фиксированный объем");
  93. TypeOfRefueling.Add("Фиксированная цена");
  94. List<string> TypeOfPayment = new List<string>();
  95. TypeOfPayment.Add("Банковской картой");
  96. if (Helper.FindCar != null)
  97. {
  98. if (Helper.FindCar.VolumeTank != null)
  99. {
  100. TypeOfRefueling.Add("До полного бака с ограничением по объему");
  101. }
  102. if (Helper.FindCar.CardOfCars.FirstOrDefault(x=>x.IDCardType == 3) != null)
  103. {
  104. TypeOfPayment.Add("Накопительной картой");
  105. }
  106. if (Helper.FindCar.CardOfCars.FirstOrDefault(x => x.IDCardType == 2) != null)
  107. {
  108. TypeOfPayment.Add("Кредитной картой");
  109. }
  110. }
  111. CmbTypeOfPayment.ItemsSource = TypeOfPayment;
  112. CmbTypeOfRefueling.ItemsSource = TypeOfRefueling;
  113. }
  114. private void BtnEnterDataOfCard_Click(object sender, RoutedEventArgs e)
  115. {
  116. AddDataOfCardWindow addDataOfCardWindow = new AddDataOfCardWindow();
  117. addDataOfCardWindow.ShowDialog();
  118. }
  119. private void BtnPayment_Click(object sender, RoutedEventArgs e)
  120. {
  121. }
  122. private void CmbTypeOfRefueling_SelectionChanged(object sender, SelectionChangedEventArgs e)
  123. {
  124. if (CmbTypeOfRefueling.SelectedIndex == 0)
  125. {
  126. TbAmount.Visibility = Visibility.Visible;
  127. TxtAmount.Visibility = Visibility.Hidden;
  128. TbCost.Visibility = Visibility.Hidden;
  129. TxtCost.Visibility = Visibility.Visible;
  130. }
  131. if (CmbTypeOfRefueling.SelectedIndex == 1)
  132. {
  133. TbAmount.Visibility = Visibility.Hidden;
  134. TxtAmount.Visibility = Visibility.Visible;
  135. TbCost.Visibility = Visibility.Visible;
  136. TxtCost.Visibility = Visibility.Hidden;
  137. }
  138. }
  139. private void TbAmount_TextChanged(object sender, TextChangedEventArgs e)
  140. {
  141. if (TbAmount.Text.Length == 0)
  142. {
  143. TxtErroreMessage.Visibility = Visibility.Hidden;
  144. TxtCost.Text = string.Empty;
  145. return;
  146. }
  147. if (TbAmount.Text.All(char.IsDigit) == true)
  148. {
  149. TxtErroreMessage.Visibility = Visibility.Hidden;
  150. var CurrentFuel = CmbTypeOfFuel.SelectedItem as FuelInCurrentGasStation;
  151. double cost = Convert.ToInt32(TbAmount.Text) * CurrentFuel.Price;
  152. TxtCost.Text = cost.ToString("F2") + " руб.";
  153. }
  154. else
  155. {
  156. TxtErroreMessage.Text = "Не корректное количество!";
  157. TxtErroreMessage.Visibility = Visibility.Visible;
  158. }
  159. }
  160. private void TbCost_TextChanged(object sender, TextChangedEventArgs e)
  161. {
  162. if (TbCost.Text.Length == 0)
  163. {
  164. TxtErroreMessage.Visibility = Visibility.Hidden;
  165. TxtAmount.Text = string.Empty;
  166. return;
  167. }
  168. if (TbCost.Text.All(char.IsDigit) == true)
  169. {
  170. TxtErroreMessage.Visibility = Visibility.Hidden;
  171. var CurrentFuel = CmbTypeOfFuel.SelectedItem as FuelInCurrentGasStation;
  172. double Amount = Convert.ToDouble(TbCost.Text) / CurrentFuel.Price;
  173. TxtAmount.Text = Amount.ToString("F2") + " л.";
  174. }
  175. else
  176. {
  177. TxtErroreMessage.Text = "Не корректная сумма!";
  178. TxtErroreMessage.Visibility = Visibility.Visible;
  179. }
  180. }
  181. private void CmbTypeOfPayment_SelectionChanged(object sender, SelectionChangedEventArgs e)
  182. {
  183. if (CmbTypeOfPayment.SelectedValue.ToString() == "Банковской картой")
  184. {
  185. BtnEnterDataOfCard.Visibility = Visibility.Visible;
  186. }
  187. else
  188. {
  189. BtnEnterDataOfCard.Visibility = Visibility.Hidden;
  190. }
  191. }
  192. private void CmbTypeOfFuel_SelectionChanged(object sender, SelectionChangedEventArgs e)
  193. {
  194. TxtAmount.Text = "";
  195. TbAmount.Text = "";
  196. TxtCost.Text = "";
  197. TbCost.Text = "";
  198. }
  199. }
  200. }