App.xaml.cs 6.0 KB

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