Search.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using System.IO;
  15. using System.Net;
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Linq;
  18. using System.Text.RegularExpressions;
  19. using System.Data.SqlClient;
  20. using System.Data;
  21. using System.Configuration;
  22. namespace kursach.Windows
  23. {
  24. /// <summary>
  25. /// Логика взаимодействия для Search.xaml
  26. /// </summary>
  27. public partial class Search : Window
  28. {
  29. string connectionString;
  30. SqlDataAdapter adapter = new SqlDataAdapter();
  31. DataTable usersTable = new DataTable();
  32. public Search()
  33. {
  34. InitializeComponent();
  35. try
  36. {
  37. string answer = Connect("https://opensky-network.org/api/states/all");
  38. planeGrid.ItemsSource = GetPlaneList(answer);
  39. }
  40. catch
  41. {
  42. MessageBox.Show("Проблемы с подключением...");
  43. }
  44. //получаем строку подключения из app.config
  45. connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  46. }
  47. //подключение к api
  48. public string Connect(string apiUrl)
  49. {
  50. WebRequest request = WebRequest.Create(apiUrl);
  51. request.Method = "GET";
  52. WebResponse response = request.GetResponse();
  53. string answer = string.Empty;
  54. using (Stream s = response.GetResponseStream())
  55. {
  56. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  57. {
  58. answer = reader.ReadToEnd();
  59. }
  60. }
  61. response.Close();
  62. return answer;
  63. }
  64. List<Plane> planeList = new List<Plane>();
  65. List<Plane> subPlaneList = new List<Plane>();
  66. //заполнение основного листа
  67. public List<Plane> GetPlaneList(string answer)
  68. {
  69. JObject answerJson = JObject.Parse(answer);
  70. IList<JToken> results = answerJson["states"].Children().ToList();
  71. foreach (JToken result in results)
  72. {
  73. List<string> resultString = result.ToString().TrimStart('[').TrimEnd(']').Split(',').ToList<string>();
  74. Plane plane = new Plane();
  75. plane.Icao24 = resultString[0].Trim().TrimStart('"').TrimEnd('"');
  76. plane.Callsign = resultString[1].Trim().TrimStart('"').TrimEnd('"');
  77. plane.Country = resultString[2].Trim().TrimStart('"').TrimEnd('"');
  78. plane.Longitude = resultString[5].Trim();
  79. plane.Latitude = resultString[6].Trim();
  80. plane.On_ground = resultString[8].Trim();
  81. plane.Velocity = resultString[9].Trim();
  82. plane.Altitude = resultString[13].Trim();
  83. planeList.Add(plane);
  84. }
  85. return planeList;
  86. }
  87. //возврат к предыдущему окну
  88. private void Back(object sender, RoutedEventArgs e)
  89. {
  90. //вернуться назад
  91. MainWindow main = new MainWindow();
  92. main.Show();
  93. Close();
  94. }
  95. //поиск
  96. private void Find(object sender, RoutedEventArgs e)
  97. {
  98. planeGrid.ItemsSource = new List<Plane>();
  99. subPlaneList.Clear();
  100. Regex regex1 = new Regex(@"(\w*)" + SearchCountry.Text + @"(\w*)");
  101. Regex regex2 = new Regex(@"(\w*)" + SearchCallsign.Text + @"(\w*)");
  102. foreach (Plane plane in planeList)
  103. {
  104. if (regex1.IsMatch(plane.Country) && SearchCallsign.Text == "")
  105. { subPlaneList.Add(plane); }
  106. if (regex2.IsMatch(plane.Callsign) && SearchCountry.Text == "")
  107. { subPlaneList.Add(plane); }
  108. if (regex1.IsMatch(plane.Country) && regex2.IsMatch(plane.Callsign))
  109. { subPlaneList.Add(plane); }
  110. }
  111. planeGrid.ItemsSource = subPlaneList;
  112. }
  113. //добавление в избранное
  114. private void Fav(object sender, RoutedEventArgs e)
  115. {
  116. if (Menu.UserId == 0)
  117. {
  118. MessageBox.Show("Эта функция доступна только авторизированным пользователям");
  119. }
  120. else
  121. {
  122. if(planeGrid.SelectedItem != null)
  123. {
  124. Plane plane = (Plane)planeGrid.SelectedItem;
  125. MessageBox.Show(plane.Icao24);
  126. SqlConnection connection = new SqlConnection(connectionString);
  127. connection.Open();
  128. SqlCommand command = new SqlCommand();
  129. command.CommandText = "INSERT INTO Favourites VALUES(" + Menu.UserId + ", '" + plane.Icao24 + "')";
  130. command.Connection = connection;
  131. adapter.SelectCommand = command;
  132. adapter.Fill(usersTable);
  133. connection.Close();
  134. }
  135. else
  136. {
  137. MessageBox.Show("Нет выделенной строки");
  138. }
  139. }
  140. }
  141. //обновление данных
  142. private void Upd(object sender, RoutedEventArgs e)
  143. {
  144. planeGrid.ItemsSource = null;
  145. planeList.Clear();
  146. try
  147. {
  148. string answer = Connect("https://opensky-network.org/api/states/all");
  149. planeGrid.ItemsSource = GetPlaneList(answer);
  150. }
  151. catch
  152. {
  153. MessageBox.Show("Проблемы с подключением...");
  154. }
  155. }
  156. }
  157. public class Plane
  158. {
  159. public string Icao24 { get; set; }
  160. public string Callsign { get; set; }
  161. public string Country { get; set; }
  162. public string Longitude { get; set; }
  163. public string Latitude { get; set; }
  164. public string Altitude { get; set; }
  165. public string Velocity { get; set; }
  166. public string On_ground { get; set; }
  167. }
  168. }