ConnectionTestUnit.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using InABox.Clients;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using System.Net;
  12. using InABox.Core;
  13. using Comal.Classes;
  14. using Email = Xamarin.Essentials.Email;
  15. namespace ConnectionTest
  16. {
  17. public delegate void RemoveURL(View view);
  18. [XamlCompilation(XamlCompilationOptions.Compile)]
  19. public partial class ConnectionTestUnit : ContentView
  20. {
  21. public event RemoveURL OnRemoveURL;
  22. string URL = "";
  23. string log = "";
  24. double interval = 1000;
  25. static RestClient<User> client;
  26. public ConnectionTestUnit(string url)
  27. {
  28. InitializeComponent();
  29. URL = url;
  30. urlLbl.Text = URL;
  31. Task.Run(() =>
  32. {
  33. Thread.Sleep(3000);
  34. StartTest();
  35. });
  36. Resolve();
  37. ResolveTimer();
  38. UpdateInterval();
  39. }
  40. private void ResolveTimer()
  41. {
  42. Task.Run(() =>
  43. {
  44. while (true)
  45. {
  46. int count = 59;
  47. while (count != 0)
  48. {
  49. Device.BeginInvokeOnMainThread(() =>
  50. {
  51. refreshLbl.Text = "(refresh in " + count + " s)";
  52. });
  53. Thread.Sleep(1000);
  54. count--;
  55. }
  56. Resolve();
  57. }
  58. });
  59. }
  60. private void Resolve()
  61. {
  62. try
  63. {
  64. var ip = Dns.GetHostEntry(URL.Substring(0, URL.Length - 5));
  65. Device.BeginInvokeOnMainThread(() =>
  66. {
  67. ipResolveLbl.Text = ip.AddressList.Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First().ToString();
  68. ipResolveLbl.BackgroundColor = Color.LightGreen;
  69. Task.Run(() =>
  70. {
  71. Thread.Sleep(1500);
  72. Device.BeginInvokeOnMainThread(() =>
  73. {
  74. ipResolveLbl.BackgroundColor = Color.Default;
  75. });
  76. });
  77. });
  78. }
  79. catch (Exception ex)
  80. {
  81. ipResolveLbl.Text = "Resolve error";
  82. }
  83. }
  84. private void EmailBtn_Clicked(object sender, EventArgs e)
  85. {
  86. Device.BeginInvokeOnMainThread(async () =>
  87. {
  88. var message = new EmailMessage
  89. {
  90. Subject = "Crash logs",
  91. Body = log,
  92. To = new List<string> { "support@prsdigital.com.au" }
  93. };
  94. await Email.ComposeAsync(message);
  95. });
  96. }
  97. private void StartTest()
  98. {
  99. int count = 0;
  100. int crashcount = 0;
  101. while (true)
  102. {
  103. try
  104. {
  105. Thread.Sleep((int)interval);
  106. if(client == null)
  107. client = new RestClient<User>(URL, false, true, BinarySerializationSettings.Latest);
  108. if(ClientFactory.ClientType == null)
  109. ClientFactory.SetClientType(typeof(RestClient<>), InABox.Core.Platform.TimeBench, "Connection Test app 2.0 - rest client", URL, true);
  110. var result = ClientFactory.Validate("TAN", "nictan");
  111. //var request = WebRequest.Create("http://" + URL);
  112. //var response = request.GetResponse();
  113. //var cmd = string.Format(
  114. // "{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}",
  115. // "validate",
  116. // "User",
  117. // "Json",
  118. // "Json",
  119. // "1.0"
  120. // );
  121. Device.BeginInvokeOnMainThread(() =>
  122. {
  123. count++;
  124. attemptNoLbl.Text = "Attempt: " + count;
  125. });
  126. //CoreTable table = new Client<Product>().Query();
  127. Device.BeginInvokeOnMainThread(() =>
  128. {
  129. count++;
  130. attemptNoLbl.Text = "Attempt: " + count;
  131. });
  132. }
  133. catch (Exception ex)
  134. {
  135. log = log + "Attempt number: " + count
  136. + Environment.NewLine
  137. + "Date/time: " + DateTime.Now.ToString("HH:mm dd MMM yy")
  138. + Environment.NewLine
  139. + Environment.NewLine
  140. + ex.Message + ex.StackTrace;
  141. crashcount++;
  142. count++;
  143. Device.BeginInvokeOnMainThread(() =>
  144. {
  145. attemptNoLbl.Text = "Attempt: " + count;
  146. crashNoLbl.Text = "Crash Count: " + crashcount;
  147. });
  148. }
  149. }
  150. }
  151. private void CloseButton_Clicked(object sender, EventArgs e)
  152. {
  153. OnRemoveURL?.Invoke(this);
  154. }
  155. void MinusBtn_Clicked(System.Object sender, System.EventArgs e)
  156. {
  157. if (interval > 500)
  158. {
  159. interval = interval - 500;
  160. UpdateInterval();
  161. }
  162. else if(interval == 500)
  163. {
  164. interval = 100;
  165. UpdateInterval();
  166. }
  167. }
  168. void PlusButton_Clicked(System.Object sender, System.EventArgs e)
  169. {
  170. if (interval >= 500)
  171. {
  172. interval = interval + 500;
  173. UpdateInterval();
  174. }
  175. else if(interval == 100)
  176. {
  177. interval = 500;
  178. UpdateInterval();
  179. }
  180. }
  181. private void UpdateInterval()
  182. {
  183. intervalLbl.Text = "Interval: " + (interval / 1000) + "s";
  184. }
  185. }
  186. }