Near Distance alert Update

main
unknown 6 months ago
parent 554dd75474
commit 38ca3b3d10
  1. 30
      Helpers/GeoHelper.cs
  2. 17
      Models/Authority.cs
  3. 35
      Services/AuthorityService.cs
  4. 59
      Views/IncidentReportPage.xaml.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;
}
}
}

@ -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; }
}
}

@ -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<Authority> _authorities = new List<Authority>
{
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<Authority> GetNearByAuthorities(double incidentLatitude, double incidentLongitude, double radiusKm = 10)
{
List<Authority> nearbyAuthorities = new List<Authority>();
foreach (var authority in _authorities)
{
double distance = GeoHelper.CalculateDistance(incidentLatitude, incidentLongitude, authority.Latitude, authority.Longitude);
if (distance <= radiusKm)
{
nearbyAuthorities.Add(authority);
}
}
return nearbyAuthorities;
}
}
}

@ -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);
}
*/
}
}

Loading…
Cancel
Save