App.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.Mobile;
  9. using InABox.Rpc;
  10. using Xamarin.Essentials;
  11. using Xamarin.Forms;
  12. using Xamarin.Forms.Xaml;
  13. [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
  14. namespace PRS.Mobile
  15. {
  16. public enum TransportStatus
  17. {
  18. BadConfiguration,
  19. NoConnection,
  20. OK,
  21. None
  22. }
  23. public partial class App : Application
  24. {
  25. public static string LaunchParameters { get; set; }
  26. public static IRpcClientTransport Transport { get; set; }
  27. public static TransportStatus ConnectTransport(string[] urls)
  28. {
  29. if (Transport?.IsConnected() == true)
  30. Transport.Disconnect();
  31. Transport = null;
  32. if (!urls.Any())
  33. return TransportStatus.BadConfiguration;
  34. var transport = new RpcClientSocketTransport(urls);
  35. transport.Connect();
  36. if (!transport.IsConnected())
  37. return TransportStatus.NoConnection;
  38. Transport = transport;
  39. ClientFactory.SetClientType(typeof(RpcClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, Transport);
  40. var info = new Client<User>().Info();
  41. var resturl = $"{transport.ServerName()}:{info.RestPort}";
  42. ClientFactory.SetClientType(typeof(RestClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, resturl);
  43. return TransportStatus.OK;
  44. }
  45. public static LocationServices GPS { get; } = new LocationServices() { ScanDelay = new TimeSpan(0, 1, 0) };
  46. public static Bluetooth Bluetooth { get; } = new Bluetooth() { ScanDelay = new TimeSpan(0, 1, 0) };
  47. public static DataModel Data { get; } = new DataModel();
  48. public static string DeviceString
  49. {
  50. get
  51. {
  52. var result = Device.RuntimePlatform.Substring(0, 1).ToUpper();
  53. if (Device.Idiom == TargetIdiom.Phone)
  54. result = result.ToLower();
  55. return result;
  56. }
  57. }
  58. public static double GetXamarinWidth()
  59. {
  60. var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
  61. var width = mainDisplayInfo.Width;
  62. return width / mainDisplayInfo.Density;
  63. }
  64. public static bool IsInForeground { get; set; } = false;
  65. // this is set by PINLoginPage if DatabaseSettings.UserID is blank
  66. // Effective, this will enable the "back" (-> logout) button on the Main page
  67. // if the userid is not set.
  68. public static bool IsSharedDevice { get; set; }
  69. public const string MessageOnStart = "OnStart";
  70. public const string MessageOnSleep = "OnSleep";
  71. public const string MessageOnResume = "OnResume";
  72. public App()
  73. {
  74. InitializeComponent();
  75. // https://github.com/Baseflow/XF-Material-Library
  76. XF.Material.Forms.Material.Init(this);
  77. XF.Material.Forms.Material.Use("Material.Configuration");
  78. MobileUtils.Init();
  79. CoreUtils.RegisterClasses();
  80. ComalUtils.RegisterClasses();
  81. StartMonitoringGPS();
  82. StartMonitoringBlueTooth();
  83. // PINLoginPage is going to be the root navigation page.
  84. // If shown, it will rerun the validation process
  85. // So we should be able to get to this page from
  86. // 1. App Startup => Autologin
  87. // 2. SettingsPage.Save() => AutoLogin
  88. // 3. Main Window => Always manual login?
  89. var login = new PinLoginPage();
  90. var navigation = new NavigationPage(login);
  91. MainPage = navigation;
  92. }
  93. protected override void OnStart()
  94. {
  95. // Handle when your app starts
  96. }
  97. protected override void OnSleep()
  98. {
  99. // Handle when your app sleeps
  100. }
  101. protected override void OnResume()
  102. {
  103. // Handle when your app resumes
  104. }
  105. private void StartMonitoringGPS()
  106. {
  107. Task.Run(() =>
  108. {
  109. while (true)
  110. {
  111. if (App.IsInForeground)
  112. {
  113. try
  114. {
  115. GPS.GetLocation();
  116. }
  117. catch (Exception e)
  118. {
  119. // Need to Log this
  120. }
  121. }
  122. Thread.Sleep(TimeSpan.FromSeconds(30));
  123. }
  124. });
  125. }
  126. private void StartMonitoringBlueTooth()
  127. {
  128. Task.Run(() =>
  129. {
  130. while (true)
  131. {
  132. if (App.IsInForeground)
  133. {
  134. try
  135. {
  136. Bluetooth.ScanForDevices();
  137. }
  138. catch (Exception e)
  139. {
  140. // Need to Log this
  141. }
  142. }
  143. Thread.Sleep(TimeSpan.FromSeconds(30));
  144. }
  145. });
  146. }
  147. }
  148. }