From 38ca3b3d107f4634dd0d5db506bded70af000f70 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 12 Jan 2025 08:38:59 +0545 Subject: [PATCH] Near Distance alert Update --- Helpers/GeoHelper.cs | 30 ++++++++++++++++ Models/Authority.cs | 17 +++++++++ Services/AuthorityService.cs | 35 +++++++++++++++++++ Views/IncidentReportPage.xaml.cs | 59 ++++++++++++++++++++++++++++++-- 4 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 Helpers/GeoHelper.cs create mode 100644 Models/Authority.cs create mode 100644 Services/AuthorityService.cs diff --git a/Helpers/GeoHelper.cs b/Helpers/GeoHelper.cs new file mode 100644 index 0000000..8c828c7 --- /dev/null +++ b/Helpers/GeoHelper.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Justice.Helpers +{ + public class GeoHelper + { + public static double CalculateDistance(double latitude1, double longitude1, double latitude2, double longitude2) + { + double radianLat1 = Math.PI * latitude1 / 100; + double radianLat2 = Math.PI * latitude2 / 100; + double radianLong1 = Math.PI * longitude1 / 100; + double radianLong2 = Math.PI * longitude2 / 100; + + double dLong = radianLong2 - radianLong1; + double dLat = radianLat2 - radianLat1; + double a = Math.Pow(Math.Sin(dLat / 2), 2) + + Math.Cos(radianLat1) * Math.Cos(radianLat2) * + Math.Pow(Math.Sin(dLong / 2),2); + double c = 2 * Math.Asin(Math.Sqrt(a)); + const double radius = 6371; //radius of earth in kilometer + double distance = radius * c; + + return distance; + } + } +} diff --git a/Models/Authority.cs b/Models/Authority.cs new file mode 100644 index 0000000..a8a4d33 --- /dev/null +++ b/Models/Authority.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Justice.Models +{ + public class Authority + { + public string Name { get; set; } + public string Email { get; set; } + public double Latitude { get; set; } + public double Longitude { get; set; } + public string AuthorityType { get; set; } + } +} diff --git a/Services/AuthorityService.cs b/Services/AuthorityService.cs new file mode 100644 index 0000000..e9922c4 --- /dev/null +++ b/Services/AuthorityService.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Justice.Models; +using Justice.Services; +using Justice.Helpers; + +namespace Justice.Services +{ + public class AuthorityService + { + private List _authorities = new List + { + new Authority { Name = "Police", Email = "amritsyangtan1@gmail.com", Latitude = 47.7008, Longitude = 60.3000, AuthorityType = "Accident" }, + new Authority { Name = "Police", Email = "amreitsyanf@gmail.com", Latitude = 27.7008, Longitude = 85.3000, AuthorityType = "Accident" }, + new Authority { Name = "Fire Department", Email = "amAritsyangtan1@gmail.com", Latitude = 27.7110, Longitude = 85.2915, AuthorityType = "Fire" }, + new Authority { Name = "Ambulance", Email = "ambulance@example.com", Latitude = 27.7052, Longitude = 85.3092, AuthorityType = "Ambulance" } + }; + public List GetNearByAuthorities(double incidentLatitude, double incidentLongitude, double radiusKm = 10) + { + List nearbyAuthorities = new List(); + foreach (var authority in _authorities) + { + double distance = GeoHelper.CalculateDistance(incidentLatitude, incidentLongitude, authority.Latitude, authority.Longitude); + if (distance <= radiusKm) + { + nearbyAuthorities.Add(authority); + } + } + return nearbyAuthorities; + } + } +} diff --git a/Views/IncidentReportPage.xaml.cs b/Views/IncidentReportPage.xaml.cs index 555a0ba..1b4123e 100644 --- a/Views/IncidentReportPage.xaml.cs +++ b/Views/IncidentReportPage.xaml.cs @@ -101,10 +101,62 @@ namespace Justice.Views await DisplayAlert("Error", "Please fill in all required fields.", "OK"); return; } + await SetLoadingStateAsync(true); //show loader + try + { + var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync(); + var report = new IncidentReport + { + ReporterName = ReporterNameEntry.Text.Trim(), + IncidentType = IncidentTypePicker.SelectedItem.ToString(), + Description = DescriptionEditor.Text.Trim(), + Latitude = latitude, + Longitude = longitude, + Address = address, + DateTime = DateTime.Now, + AttachmentPath = _selectedAttachmentPath + }; + //save report to database + await _databaseHelper.InsertAsync(report); + // get nearby authorities based on incident location + var authorityService = new AuthorityService(); + var nearbyAuthorities = authorityService.GetNearByAuthorities(report.Latitude, report.Longitude); + // Prepare the email content + string emailBody = $"Incident Report:\n\n" + + $"Name: {report.ReporterName}\n" + + $"Type: {report.IncidentType}\n" + + $"Description: {report.Description}\n" + + $"Latitude: {report.Latitude}\n" + + $"Longitude: {report.Longitude}\n" + + $"Location: {report.Address}\n" + + $"Date/Time: {report.DateTime}"; + + // Send email to each nearby authority + foreach (var authority in nearbyAuthorities) + { + bool emailSent = await _emailService.SendEmailWithAttachmentAsync(authority.Email, "Incident Report", emailBody, _selectedAttachmentPath); - var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync(); + if (emailSent) + { + Console.WriteLine($"Email sent to: {authority.Name}"); + } + } - var report = new IncidentReport + await DisplayAlert("Success", "Incident report submitted and email alert sent to nearby authorities.", "OK"); + } + catch (Exception ex) + { + await DisplayAlert("Error", $"Failed to submit report:{ex.Message}", "OK"); + } + finally + { + await SetLoadingStateAsync(false); //hide loader + } + + + // var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync(); + + /* var report = new IncidentReport { ReporterName = ReporterNameEntry.Text.Trim(), IncidentType = IncidentTypePicker.SelectedItem.ToString(), @@ -119,7 +171,7 @@ namespace Justice.Views try { // Save the report to the database - await SetLoadingStateAsync(true); + await _databaseHelper.InsertAsync(report); // Get the recipient email based on the incident type @@ -155,6 +207,7 @@ namespace Justice.Views { await SetLoadingStateAsync(false); } + */ } }