App.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 = new RpcClientSocketTransport(urls);
  32. if (!urls.Any())
  33. return TransportStatus.BadConfiguration;
  34. Transport.Connect();
  35. if (!Transport.IsConnected())
  36. return TransportStatus.NoConnection;
  37. ClientFactory.SetClientType(typeof(RpcClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, Transport);
  38. // Workaround for bug in RPCSocketServer where lrge uploads (ie photos)
  39. // actually crash the server. So to get something working, we'll
  40. // fall back to a Rest Client while still maintaining the RPC Transport stuff
  41. // var info = new Client<User>().Info();
  42. // if (info.RestPort != 0)
  43. // {
  44. // var resturl = $"{transport.ServerName()}:{info.RestPort}";
  45. // ClientFactory.SetClientType(typeof(RestClient<>), InABox.Core.Platform.TimeBench,
  46. // MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, resturl);
  47. // }
  48. return TransportStatus.OK;
  49. }
  50. public static Bluetooth Bluetooth { get; } = new Bluetooth() { ScanDelay = new TimeSpan(0, 1, 0) };
  51. public static DataModel Data { get; } = new DataModel();
  52. public static string DeviceString
  53. {
  54. get
  55. {
  56. var result = Device.RuntimePlatform.Substring(0, 1).ToUpper();
  57. if (Device.Idiom == TargetIdiom.Phone)
  58. result = result.ToLower();
  59. return result;
  60. }
  61. }
  62. public static double GetXamarinWidth()
  63. {
  64. var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
  65. var width = mainDisplayInfo.Width;
  66. return width / mainDisplayInfo.Density;
  67. }
  68. public static bool IsInForeground { get; set; } = false;
  69. // this is set by PINLoginPage if DatabaseSettings.UserID is blank
  70. // Effective, this will enable the "back" (-> logout) button on the Main page
  71. // if the userid is not set.
  72. public static bool IsSharedDevice { get; set; }
  73. public const string MessageOnStart = "OnStart";
  74. public const string MessageOnSleep = "OnSleep";
  75. public const string MessageOnResume = "OnResume";
  76. public App()
  77. {
  78. InitializeComponent();
  79. MobilePage.Host = Data;
  80. // https://github.com/Baseflow/XF-Material-Library
  81. XF.Material.Forms.Material.Init(this);
  82. XF.Material.Forms.Material.Use("Material.Configuration");
  83. MobileUtils.Init();
  84. CoreUtils.RegisterClasses();
  85. ComalUtils.RegisterClasses();
  86. // PINLoginPage is going to be the root navigation page.
  87. // If shown, it will rerun the validation process
  88. // So we should be able to get to this page from
  89. // 1. App Startup => Autologin
  90. // 2. SettingsPage.Save() => AutoLogin
  91. // 3. Main Window => Always manual login?
  92. MainPage = new NavigationPage(new PinLoginPage());
  93. //MainPage = new NavigationPage(new TestRadioList());
  94. }
  95. protected override void OnStart()
  96. {
  97. // Handle when your app starts
  98. }
  99. protected override void OnSleep()
  100. {
  101. // Handle when your app sleeps
  102. }
  103. protected override void OnResume()
  104. {
  105. // Handle when your app resumes
  106. }
  107. public static LocationServices GPS { get; } = new LocationServices() { ScanDelay = new TimeSpan(0, 1, 0) };
  108. public static void StartMonitoringGPS()
  109. {
  110. Task.Run(() =>
  111. {
  112. while (true)
  113. {
  114. if (IsInForeground)
  115. {
  116. try
  117. {
  118. GPS.GetLocation();
  119. }
  120. catch (Exception e)
  121. {
  122. InABox.Mobile.MobileLogging.Log(e,"GPS");
  123. }
  124. }
  125. Thread.Sleep(TimeSpan.FromSeconds(30));
  126. }
  127. });
  128. }
  129. public static void StartMonitoringBlueTooth()
  130. {
  131. Task.Run(() =>
  132. {
  133. while (true)
  134. {
  135. if (IsInForeground)
  136. {
  137. try
  138. {
  139. Bluetooth.ScanForDevices();
  140. }
  141. catch (Exception e)
  142. {
  143. InABox.Mobile.MobileLogging.Log(e,"BT");
  144. }
  145. }
  146. Thread.Sleep(TimeSpan.FromSeconds(30));
  147. }
  148. });
  149. }
  150. }
  151. }