Compare commits

...

1 Commits

Author SHA1 Message Date
DESKTOP-T0O5CDB\DESK-555BD
7bc47363fd backend for torque integration. 2024-01-24 14:15:40 -07:00
5 changed files with 161 additions and 8 deletions

View File

@@ -22,10 +22,12 @@ namespace CarCareTracker.Controllers
private readonly IReminderRecordDataAccess _reminderRecordDataAccess;
private readonly IUpgradeRecordDataAccess _upgradeRecordDataAccess;
private readonly IOdometerRecordDataAccess _odometerRecordDataAccess;
private readonly ITorqueRecordDataAccess _torqueRecordDataAccess;
private readonly IReminderHelper _reminderHelper;
private readonly IGasHelper _gasHelper;
private readonly IUserLogic _userLogic;
private readonly IFileHelper _fileHelper;
private readonly IConfigHelper _configHelper;
public APIController(IVehicleDataAccess dataAccess,
IGasHelper gasHelper,
IReminderHelper reminderHelper,
@@ -37,6 +39,8 @@ namespace CarCareTracker.Controllers
IReminderRecordDataAccess reminderRecordDataAccess,
IUpgradeRecordDataAccess upgradeRecordDataAccess,
IOdometerRecordDataAccess odometerRecordDataAccess,
ITorqueRecordDataAccess torqueRecordDataAccess,
IConfigHelper configHelper,
IFileHelper fileHelper,
IUserLogic userLogic)
{
@@ -49,7 +53,9 @@ namespace CarCareTracker.Controllers
_reminderRecordDataAccess = reminderRecordDataAccess;
_upgradeRecordDataAccess = upgradeRecordDataAccess;
_odometerRecordDataAccess = odometerRecordDataAccess;
_torqueRecordDataAccess = torqueRecordDataAccess;
_gasHelper = gasHelper;
_configHelper = configHelper;
_reminderHelper = reminderHelper;
_userLogic = userLogic;
_fileHelper = fileHelper;
@@ -337,7 +343,8 @@ namespace CarCareTracker.Controllers
response.Success = true;
response.Message = "Odometer Record Added";
return Json(response);
} catch (Exception ex)
}
catch (Exception ex)
{
response.Success = false;
response.Message = ex.Message;
@@ -352,7 +359,8 @@ namespace CarCareTracker.Controllers
{
var vehicleRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
var result = _gasHelper.GetGasRecordViewModels(vehicleRecords, useMPG, useUKMPG)
.Select(x => new GasRecordExportModel {
.Select(x => new GasRecordExportModel
{
Date = x.Date,
Odometer = x.Mileage.ToString(),
Cost = x.Cost.ToString(),
@@ -423,7 +431,7 @@ namespace CarCareTracker.Controllers
{
var currentMileage = GetMaxMileage(vehicleId);
var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId);
var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).Select(x=> new ReminderExportModel { Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes});
var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).Select(x => new ReminderExportModel { Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes });
return Json(results);
}
[Authorize(Roles = nameof(UserData.IsRootUser))]
@@ -434,6 +442,62 @@ namespace CarCareTracker.Controllers
var result = _fileHelper.MakeBackup();
return Json(result);
}
[Route("/api/obdii/vehicle/{vehicleId}")]
[AllowAnonymous]
public IActionResult OBDII(int vehicleId, TorqueRecord record)
{
if (record.kff1005 != default && record.kff1006 != default && vehicleId != default)
{
//check if there is an existing session.
try
{
var existingRecord = _torqueRecordDataAccess.GetTorqueRecordById(record.Session);
if (existingRecord != null)
{
//calculate difference between last coordinates.
var distance = GetDistance(existingRecord.LastLongitude, existingRecord.LastLatitude, record.kff1005, record.kff1006);
var useMPG = _configHelper.GetUserConfig(User).UseMPG;
if (useMPG)
{
distance /= 1609; //get miles.
}
else
{
distance /= 1000;
}
existingRecord.DistanceTraveled += distance;
existingRecord.LastLongitude = record.kff1005;
existingRecord.LastLatitude = record.kff1006;
_torqueRecordDataAccess.SaveTorqueRecord(existingRecord);
}
else
{
//new record.
record.InitialLongitude = record.kff1005;
record.InitialLatitude = record.kff1006;
record.LastLongitude = record.kff1005;
record.LastLatitude = record.kff1006;
_torqueRecordDataAccess.SaveTorqueRecord(record);
}
return Json(true);
}
catch (Exception ex)
{
return Json(false);
}
}
return Json(false);
}
private double GetDistance(double longitude, double latitude, double otherLongitude, double otherLatitude)
{
var d1 = latitude * (Math.PI / 180.0);
var num1 = longitude * (Math.PI / 180.0);
var d2 = otherLatitude * (Math.PI / 180.0);
var num2 = otherLongitude * (Math.PI / 180.0) - num1;
var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));
}
private int GetMaxMileage(int vehicleId)
{
var numbersArray = new List<int>();

View File

@@ -0,0 +1,39 @@
using CarCareTracker.External.Interfaces;
using CarCareTracker.Helper;
using CarCareTracker.Models;
using LiteDB;
namespace CarCareTracker.External.Implementations
{
public class TorqueRecordDataAccess: ITorqueRecordDataAccess
{
private static string dbName = StaticHelper.DbName;
private static string tableName = "torquerecords";
public TorqueRecord GetTorqueRecordById(string torqueRecordId)
{
using (var db = new LiteDatabase(dbName))
{
var table = db.GetCollection<TorqueRecord>(tableName);
return table.FindById(torqueRecordId);
};
}
public bool DeleteTorqueRecordById(int torqueRecordId)
{
using (var db = new LiteDatabase(dbName))
{
var table = db.GetCollection<TorqueRecord>(tableName);
table.Delete(torqueRecordId);
return true;
};
}
public bool SaveTorqueRecord(TorqueRecord torqueRecord)
{
using (var db = new LiteDatabase(dbName))
{
var table = db.GetCollection<TorqueRecord>(tableName);
table.Upsert(torqueRecord);
return true;
};
}
}
}

View File

@@ -0,0 +1,11 @@
using CarCareTracker.Models;
namespace CarCareTracker.External.Interfaces
{
public interface ITorqueRecordDataAccess
{
public TorqueRecord GetTorqueRecordById(string torqueRecordId);
public bool DeleteTorqueRecordById(int torqueRecordId);
public bool SaveTorqueRecord(TorqueRecord torqueRecord);
}
}

View File

@@ -0,0 +1,38 @@
using LiteDB;
namespace CarCareTracker.Models
{
public class TorqueRecord
{
/// <summary>
/// Session Id provided by Torque
/// </summary>
[BsonId]
public string Session { get; set; }
/// <summary>
/// VehicleId
/// </summary>
public int VehicleId { get; set; }
/// <summary>
/// Email Address
/// </summary>
public string Eml { get; set; }
/// <summary>
/// longitude
/// </summary>
public double kff1005 { get; set; }
/// <summary>
/// latitude
/// </summary>
public double kff1006 { get; set; }
/// <summary>
/// Calculated fields.
/// </summary>
public double InitialLongitude { get; set; }
public double InitialLatitude { get; set; }
public double LastLongitude { get; set; }
public double LastLatitude { get; set; }
public double DistanceTraveled { get; set; }
}
}

View File

@@ -25,6 +25,7 @@ builder.Services.AddSingleton<IUserConfigDataAccess, UserConfigDataAccess>();
builder.Services.AddSingleton<ISupplyRecordDataAccess, SupplyRecordDataAccess>();
builder.Services.AddSingleton<IPlanRecordDataAccess, PlanRecordDataAccess>();
builder.Services.AddSingleton<IOdometerRecordDataAccess, OdometerRecordDataAccess>();
builder.Services.AddSingleton<ITorqueRecordDataAccess, TorqueRecordDataAccess>();
//configure helpers
builder.Services.AddSingleton<IFileHelper, FileHelper>();