|
@@ -18,16 +18,83 @@ using comal.timesheets.Tasks;
|
|
|
using System.IO;
|
|
|
using static comal.timesheets.CustomControls.JobShell;
|
|
|
using static Android.Graphics.Paint;
|
|
|
+using static InABox.Mobile.LocationServices;
|
|
|
|
|
|
namespace comal.timesheets
|
|
|
{
|
|
|
public delegate void MainPageNotificationsChanged();
|
|
|
+ public delegate void RefreshScreen();
|
|
|
public static class MainPageUtils
|
|
|
{
|
|
|
+ public static event MainPageNotificationsChanged OnMainPageNotificationsChanged;
|
|
|
+ public static event RefreshScreen OnRefreshScreen;
|
|
|
+
|
|
|
public static Assignment CurrentAssignment = null;
|
|
|
public static JobShell Job = new JobShell();
|
|
|
public static int NumberOfNotifications = 0;
|
|
|
- public static event MainPageNotificationsChanged OnMainPageNotificationsChanged;
|
|
|
+ public static string matchedDeviceName = "";
|
|
|
+ public static string deviceName = "";
|
|
|
+ public static bool bRecentlyUpdatedTiles = false;
|
|
|
+ public static TimeSheet _timesheet = null;
|
|
|
+ public static Employee _employee = null;
|
|
|
+ public static CoreTable _jobs = null;
|
|
|
+ public static bool firstLoad = true;
|
|
|
+ public static bool recentlyAskedToUpdate = true;
|
|
|
+ public static int updateCounter;
|
|
|
+
|
|
|
+ public static void Init()
|
|
|
+ {
|
|
|
+ InitEvents();
|
|
|
+ InitData();
|
|
|
+ InitTimers();
|
|
|
+ }
|
|
|
+ private static void InitEvents()
|
|
|
+ {
|
|
|
+ App.GPS.OnLocationFound += LocationFound;
|
|
|
+ App.GPS.OnLocationError += LocationError;
|
|
|
+ App.Bluetooth.OnScanFinished += ScanFinished;
|
|
|
+ App.Data.DataChanged += (s, t, e) => { OnRefreshScreen?.Invoke(); };
|
|
|
+ App.Data.DataRefreshed += () => { OnRefreshScreen?.Invoke(); };
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void InitData()
|
|
|
+ {
|
|
|
+ GlobalVariables.EmpID = GlobalVariables.GetEmployeeID();
|
|
|
+ GlobalVariables.EmpName = GlobalVariables.GetEmployeeName();
|
|
|
+
|
|
|
+ App.Data.Employee.ID = GlobalVariables.EmpID;
|
|
|
+ App.Data.Employee.Name = GlobalVariables.EmpName;
|
|
|
+
|
|
|
+ _timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
|
|
|
+ _employee = App.Data.Employee;
|
|
|
+ _jobs = App.Data.Jobs;
|
|
|
+
|
|
|
+ deviceName = MobileUtils.GetDeviceID();
|
|
|
+
|
|
|
+ firstLoad = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void InitTimers()
|
|
|
+ {
|
|
|
+ Timer t = new Timer(RecentlyAskedToUpdateTimer, null, 600000, 600000); //user is reminded to update when opening screen after timer of 10 minutes
|
|
|
+ updateCounter = 1; //user is forced to update after 3rd reminder
|
|
|
+
|
|
|
+ Timer t1 = new Timer(RecentlyUpdatedTilesTimer, null, 30000, 30000);
|
|
|
+ //bluetooth data is allowed to upload once every minute, notifications refreshing is piggybacked to this too
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void RecentlyAskedToUpdateTimer(object o)
|
|
|
+ {
|
|
|
+ recentlyAskedToUpdate = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void RecentlyUpdatedTilesTimer(object o)
|
|
|
+ {
|
|
|
+ bRecentlyUpdatedTiles = false;
|
|
|
+ App.Data.Refresh(true);
|
|
|
+ SearchForNewNotifications();
|
|
|
+ }
|
|
|
+
|
|
|
public static Assignment CheckCurrentAssignment()
|
|
|
{
|
|
|
Thread.Sleep(5000);
|
|
@@ -130,7 +197,7 @@ namespace comal.timesheets
|
|
|
return new AddEditTask(ID);
|
|
|
else if (type == "Comal.Classes.Delivery")
|
|
|
return new DeliveryDetails(ID);
|
|
|
- else
|
|
|
+ else
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -160,7 +227,7 @@ namespace comal.timesheets
|
|
|
else //new notifications or previous notifications have now been dismissed
|
|
|
{
|
|
|
NumberOfNotifications = table.Rows.Count();
|
|
|
- OnMainPageNotificationsChanged?.Invoke();
|
|
|
+ OnMainPageNotificationsChanged?.Invoke();
|
|
|
CheckNotificationsPushed(table);
|
|
|
}
|
|
|
}
|
|
@@ -242,5 +309,157 @@ namespace comal.timesheets
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
+
|
|
|
+ private static void LocationFound(LocationServices sender)
|
|
|
+ {
|
|
|
+ //if (bSharedDevice)
|
|
|
+ // return;
|
|
|
+ if (App.Bluetooth.RecentlyScanned)
|
|
|
+ UploadTiles();
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
|
|
|
+ if (timesheet != null)
|
|
|
+ {
|
|
|
+ if (timesheet.StartLocation.Latitude.Equals(0.0F) && timesheet.StartLocation.Longitude.Equals(0.0F))
|
|
|
+ {
|
|
|
+ timesheet.StartLocation.Latitude = sender.Latitude;
|
|
|
+ timesheet.StartLocation.Longitude = sender.Longitude;
|
|
|
+ timesheet.StartLocation.Timestamp = sender.TimeStamp;
|
|
|
+ timesheet.Address = sender.Address;
|
|
|
+ new Client<TimeSheet>().Save(timesheet, "Updating Timesheet with GPS Coordinates", (o, e) => { });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!string.IsNullOrWhiteSpace(matchedDeviceName))
|
|
|
+ {
|
|
|
+ InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
|
|
|
+ curlocation.Timestamp = DateTime.Now;
|
|
|
+
|
|
|
+ GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
|
|
|
+ gpsTrackerLocation.DeviceID = matchedDeviceName;
|
|
|
+ gpsTrackerLocation.Location.Timestamp = curlocation.Timestamp;
|
|
|
+ gpsTrackerLocation.Location = curlocation;
|
|
|
+
|
|
|
+ new Client<GPSTrackerLocation>().Save(gpsTrackerLocation, "Updated company device location from Timebench");
|
|
|
+ }
|
|
|
+
|
|
|
+ OnRefreshScreen?.Invoke();
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static async void UploadTiles()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
|
|
|
+ return;
|
|
|
+ if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
|
|
|
+ return;
|
|
|
+ if (bRecentlyUpdatedTiles)
|
|
|
+ return;
|
|
|
+ bRecentlyUpdatedTiles = true;
|
|
|
+ await Task.Run(() =>
|
|
|
+ {
|
|
|
+ InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
|
|
|
+ curlocation.Timestamp = DateTime.Now;
|
|
|
+ List<GPSTrackerLocation> trackersToUpdate = new List<GPSTrackerLocation>();
|
|
|
+
|
|
|
+ foreach (String id in App.Bluetooth.DetectedBlueToothMACAddresses)
|
|
|
+ {
|
|
|
+ GPSTracker tracker = GlobalVariables.GPSTrackerCache.Find(x => x.DeviceID.Equals(id));
|
|
|
+ bool stale = tracker.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 5, 0));
|
|
|
+ bool moved = tracker.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
|
|
|
+ if (stale || moved)
|
|
|
+ {
|
|
|
+ GlobalVariables.GPSTrackerCache.Remove(tracker);
|
|
|
+ tracker.Location = curlocation;
|
|
|
+ GlobalVariables.GPSTrackerCache.Add(tracker);
|
|
|
+ //cache is updated
|
|
|
+
|
|
|
+ GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
|
|
|
+ gpsTrackerLocation.DeviceID = tracker.DeviceID;
|
|
|
+ gpsTrackerLocation.Location.Timestamp = tracker.Location.Timestamp;
|
|
|
+ gpsTrackerLocation.Location = curlocation;
|
|
|
+ trackersToUpdate.Add(gpsTrackerLocation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (trackersToUpdate.Any())
|
|
|
+ {
|
|
|
+ if (ClientFactory.UserGuid != Guid.Empty)
|
|
|
+ new Client<GPSTrackerLocation>().Save(trackersToUpdate, "Updating Bluetooth Device Locations");
|
|
|
+ }
|
|
|
+ App.Bluetooth.DetectedBlueToothMACAddresses.Clear();
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //if ((master != null) && (master.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0))))
|
|
|
+ //{
|
|
|
+ // GPSTrackerLocation device = new GPSTrackerLocation();
|
|
|
+ // device.DeviceID = MobileUtils.GetDeviceID();
|
|
|
+ // device.Location.Latitude = App.GPS.Latitude;
|
|
|
+ // device.Location.Longitude = App.GPS.Longitude;
|
|
|
+ // device.Location.Timestamp = DateTime.Now;
|
|
|
+ // locations.Add(device);
|
|
|
+ // //device.BatteryLevel = ((double)CrossBattery.Current.RemainingChargePercent);
|
|
|
+ // //new Client<GPSTrackerLocation>().Save(device, "Updating Device Location"); //, SaveTrackerCallback);
|
|
|
+ //}
|
|
|
+
|
|
|
+ #region OLD
|
|
|
+ //for (int i = 0; i < App.Bluetooth.Devices.Length; i++)
|
|
|
+ //{
|
|
|
+ // String id = App.Bluetooth.Devices[i];
|
|
|
+ // int level = App.Bluetooth.BatteryLevels[i];
|
|
|
+ // var btmaster = trackers.FirstOrDefault(x => x.DeviceID.Equals(id));
|
|
|
+
|
|
|
+ // if ((btmaster != null) && (!locations.Any(x => x.DeviceID.Equals(btmaster.DeviceID))))
|
|
|
+ // {
|
|
|
+ // bool stale = btmaster.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0));
|
|
|
+ // bool moved = btmaster.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
|
|
|
+ // if (stale || moved)
|
|
|
+ // {
|
|
|
+ // GPSTrackerLocation location = new GPSTrackerLocation();
|
|
|
+ // location.DeviceID = id;
|
|
|
+ // location.Location.Latitude = App.GPS.Latitude;
|
|
|
+ // location.Location.Longitude = App.GPS.Longitude;
|
|
|
+ // location.Location.Timestamp = DateTime.Now;
|
|
|
+ // location.BatteryLevel = level;
|
|
|
+ // locations.Add(location);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // //new Client<GPSTrackerLocation>().Save(location, "Found Kontakt Device"); //, SaveTrackerCallback);
|
|
|
+ //}
|
|
|
+
|
|
|
+ //if (locations.Any())
|
|
|
+ // new Client<GPSTrackerLocation>().Save(locations, "Updating Bluetooth Device Locations", (o, e) => { });
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void LocationError(LocationServices sebder, Exception error)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void ScanFinished(Bluetooth sender)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ OnRefreshScreen?.Invoke();
|
|
|
+
|
|
|
+ if (App.GPS.RecentlyLocated)
|
|
|
+ UploadTiles();
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+ }
|
|
|
}
|
|
|
}
|