ConnectionTestUnit.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. namespace ConnectionTest
  13. {
  14. [XamlCompilation(XamlCompilationOptions.Compile)]
  15. public partial class ConnectionTestUnit : ContentView
  16. {
  17. string URL = "";
  18. string log = "";
  19. public ConnectionTestUnit(string url)
  20. {
  21. InitializeComponent();
  22. URL = url;
  23. urlLbl.Text = URL;
  24. Task.Run(() =>
  25. {
  26. Thread.Sleep(500);
  27. StartTest();
  28. });
  29. Resolve();
  30. ResolveTimer();
  31. }
  32. private void ResolveTimer()
  33. {
  34. Task.Run(() =>
  35. {
  36. while (true)
  37. {
  38. int count = 59;
  39. while (count != 0)
  40. {
  41. Device.BeginInvokeOnMainThread(() =>
  42. {
  43. refreshLbl.Text = "(refresh in " + count + " s)";
  44. });
  45. Thread.Sleep(1000);
  46. count--;
  47. }
  48. Resolve();
  49. }
  50. });
  51. }
  52. private void Resolve()
  53. {
  54. try
  55. {
  56. var ip = Dns.GetHostEntry(URL.Substring(0, URL.Length - 5));
  57. Device.BeginInvokeOnMainThread(() =>
  58. {
  59. ipResolveLbl.Text = ip.AddressList[0].ToString();
  60. ipResolveLbl.BackgroundColor = Color.LightGreen;
  61. Task.Run(() =>
  62. {
  63. Thread.Sleep(1500);
  64. Device.BeginInvokeOnMainThread(() =>
  65. {
  66. ipResolveLbl.BackgroundColor = Color.Default;
  67. });
  68. });
  69. });
  70. }
  71. catch (Exception ex)
  72. {
  73. ipResolveLbl.Text = "Resolve error";
  74. }
  75. }
  76. private void EmailBtn_Clicked(object sender, EventArgs e)
  77. {
  78. Device.BeginInvokeOnMainThread(async () =>
  79. {
  80. var message = new EmailMessage
  81. {
  82. Subject = "Crash logs",
  83. Body = log,
  84. To = new List<string> { "support@prsdigital.com.au" }
  85. };
  86. await Email.ComposeAsync(message);
  87. });
  88. }
  89. private void StartTest()
  90. {
  91. int count = 1;
  92. int crashcount = 0;
  93. ClientFactory.SetClientType(typeof(JsonClient<>), "Test app", "1.0", URL, true);
  94. while (true)
  95. {
  96. try
  97. {
  98. Thread.Sleep(500);
  99. var result = ClientFactory.Validate("TAN", "nictan");
  100. Device.BeginInvokeOnMainThread(() =>
  101. {
  102. attemptNoLbl.Text = "Attempt: " + count;
  103. count++;
  104. });
  105. }
  106. catch (Exception ex)
  107. {
  108. log = log + "Attempt number: " + count
  109. + Environment.NewLine
  110. + ex.Message + ex.StackTrace;
  111. crashcount++;
  112. count++;
  113. Device.BeginInvokeOnMainThread(() => { crashNoLbl.Text = "Crash Count: " + crashcount; });
  114. }
  115. }
  116. }
  117. }
  118. }