add openhack files

This commit is contained in:
Ryan Peters
2022-11-03 16:41:13 -04:00
commit b2c9f7e29f
920 changed files with 118861 additions and 0 deletions

View File

@ -0,0 +1,18 @@
using System;
namespace Simulator.DataObjects
{
public interface IBaseDataObject
{
string Id { get; set; }
}
public class BaseDataObject : IBaseDataObject
{
public BaseDataObject() { Id = Guid.NewGuid().ToString(); }
public string Id { get ; set ; }
}
}

View File

@ -0,0 +1,39 @@
namespace Simulator.DataObjects
{
using Newtonsoft.Json;
using System;
public partial class Poi
{
[JsonProperty("tripId")]
public Guid TripId { get; set; }
[JsonProperty("latitude")]
public double Latitude { get; set; }
[JsonProperty("longitude")]
public double Longitude { get; set; }
[JsonProperty("poiType")]
public long PoiType { get; set; }
[JsonProperty("timestamp")]
public DateTime Timestamp { get; set; }
[JsonProperty("deleted")]
public bool Deleted { get; set; }
[JsonProperty("id")]
public Guid Id { get; set; }
}
public partial class Poi
{
public static Poi FromJson(string json) => JsonConvert.DeserializeObject<Poi>(json, Converter.Settings);
}
public static class PoiSerializer
{
public static string ToJson(this Poi self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
}

View File

@ -0,0 +1,63 @@
namespace Simulator.DataObjects
{
using Newtonsoft.Json;
using System;
public partial class Trip
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("UserId")]
public string UserId { get; set; }
[JsonProperty("RecordedTimeStamp")]
public DateTime RecordedTimeStamp { get; set; }
[JsonProperty("EndTimeStamp")]
public DateTime EndTimeStamp { get; set; }
[JsonProperty("Rating")]
public long Rating { get; set; }
[JsonProperty("IsComplete")]
public bool IsComplete { get; set; }
[JsonProperty("HasSimulatedOBDData")]
public bool HasSimulatedObdData { get; set; }
[JsonProperty("AverageSpeed")]
public long AverageSpeed { get; set; }
[JsonProperty("FuelUsed")]
public long FuelUsed { get; set; }
[JsonProperty("HardStops")]
public long HardStops { get; set; }
[JsonProperty("HardAccelerations")]
public long HardAccelerations { get; set; }
[JsonProperty("Distance")]
public double Distance { get; set; }
[JsonProperty("Created")]
public DateTime Created { get; set; }
[JsonProperty("UpdatedAt")]
public DateTime UpdatedAt { get; set; }
}
public partial class Trip
{
public static Trip FromJson(string json) => JsonConvert.DeserializeObject<Trip>(json, Converter.Settings);
}
public static class TripSerializer
{
public static string ToJson(this Trip self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
}

View File

@ -0,0 +1,95 @@
namespace Simulator.DataObjects
{
using Newtonsoft.Json;
using System;
public partial class TripPoint
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("TripId")]
public Guid TripId { get; set; }
[JsonProperty("Latitude")]
public double Latitude { get; set; }
[JsonProperty("Longitude")]
public double Longitude { get; set; }
[JsonProperty("Speed")]
public double Speed { get; set; }
[JsonProperty("RecordedTimeStamp")]
public DateTime RecordedTimeStamp { get; set; }
[JsonProperty("Sequence")]
public int Sequence { get; set; }
[JsonProperty("RPM")]
public double Rpm { get; set; }
[JsonProperty("ShortTermFuelBank")]
public double ShortTermFuelBank { get; set; }
[JsonProperty("LongTermFuelBank")]
public double LongTermFuelBank { get; set; }
[JsonProperty("ThrottlePosition")]
public double ThrottlePosition { get; set; }
[JsonProperty("RelativeThrottlePosition")]
public double RelativeThrottlePosition { get; set; }
[JsonProperty("Runtime")]
public double Runtime { get; set; }
[JsonProperty("DistanceWithMalfunctionLight")]
public double DistanceWithMalfunctionLight { get; set; }
[JsonProperty("EngineLoad")]
public double EngineLoad { get; set; }
[JsonProperty("EngineFuelRate")]
public double EngineFuelRate { get; set; }
[JsonProperty("VIN")]
public Vin Vin { get; set; }
[JsonProperty("CreatedAt")]
public DateTime CreatedAt { get; set; }
[JsonProperty("UpdatedAt")]
public DateTime UpdatedAt { get; set; }
}
public partial class Vin
{
[JsonProperty("String")]
public string String { get; set; }
[JsonProperty("Valid")]
public bool Valid { get; set; }
}
public partial class TripPoint
{
public static TripPoint FromJson(string json) => JsonConvert.DeserializeObject<TripPoint>(json, Converter.Settings);
}
public static class TripPointSerializer
{
public static string ToJson(this TripPoint self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
//DateParseHandling = DateParseHandling.None,
//Converters = {new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.None } },
};
}
}

View File

@ -0,0 +1,104 @@
namespace Simulator.DataObjects
{
using Newtonsoft.Json;
using System;
public partial class User
{
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("firstName")]
public string FirstName { get; set; }
[JsonProperty("lastName")]
[JsonConverter(typeof(ParseStringConverter))]
public long LastName { get; set; }
[JsonProperty("userId")]
public string UserId { get; set; }
[JsonProperty("profilePictureUri")]
public string ProfilePictureUri { get; set; }
[JsonProperty("rating")]
public long Rating { get; set; }
[JsonProperty("ranking")]
public long Ranking { get; set; }
[JsonProperty("totalDistance")]
public double TotalDistance { get; set; }
[JsonProperty("totalTrips")]
public long TotalTrips { get; set; }
[JsonProperty("totalTime")]
public long TotalTime { get; set; }
[JsonProperty("hardStops")]
public long HardStops { get; set; }
[JsonProperty("hardAccelerations")]
public long HardAccelerations { get; set; }
[JsonProperty("fuelConsumption")]
public long FuelConsumption { get; set; }
[JsonProperty("maxSpeed")]
public long MaxSpeed { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
[JsonProperty("createdAt")]
public DateTime CreatedAt { get; set; }
[JsonProperty("updatedAt")]
public DateTime UpdatedAt { get; set; }
[JsonProperty("deleted")]
public bool Deleted { get; set; }
}
public partial class User
{
public static User FromJson(string json) => JsonConvert.DeserializeObject<User>(json, Converter.Settings);
}
public static class UserSerializer
{
public static string ToJson(this User self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
long l;
if (Int64.TryParse(value, out l))
{
return l;
}
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (long)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}
}

View File

@ -0,0 +1,11 @@
using System;
namespace TripViewer.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}

View File

@ -0,0 +1,42 @@
namespace Simulator.DataStore.Stores
{
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Collections.Generic;
public class BaseStore
//<T> : IBaseStore<T> where T : class, IBaseDataObject, new()
{
public string EndPoint { get; set; }
public HttpClient Client { get; set; }
public DateTime DateTime { get; set; }
private readonly IHttpClientFactory _clientFactory;
public BaseStore(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public Task InitializeStore(string EndPoint)
{
Client = _clientFactory.CreateClient();
Client.BaseAddress = new Uri(EndPoint);
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
DateTime = DateTime.UtcNow;
return Task.CompletedTask;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Simulator.DataStore.Stores
{
public interface IBaseStore<T>
{
Task InitializeStoreAsync();
Task<T> GetItemAsync(string id);
Task<IEnumerable<T>> GetItemsAsync();
Task<bool> CreateItemAsync(T item);
Task<bool> UpdateItemAsync(T item);
Task<bool> DeleteItemAsync(T item);
}
}

View File

@ -0,0 +1,71 @@
namespace Simulator.DataStore.Stores
{
using Simulator.DataObjects;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
public class PoiStore : BaseStore, IBaseStore<Poi>
{
public PoiStore(string EndPoint)
{
base.InitializeStore(EndPoint);
}
public async Task<Poi> GetItemAsync(string id)
{
Poi poi = null;
HttpResponseMessage response = await Client.GetAsync($"/api/poi/{id}");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
poi = await response.Content.ReadAsAsync<Poi>();
}
return poi;
}
public async Task<List<Poi>> GetItemsAsync()
{
List<Poi> trips = null;
HttpResponseMessage response = await Client.GetAsync("api/poi/");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
trips = await response.Content.ReadAsAsync<List<Poi>>();
}
return trips;
}
public async Task<Poi> CreateItemAsync(Poi item)
{
HttpResponseMessage response = await Client.PostAsJsonAsync<Poi>("api/poi", item);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
item = await response.Content.ReadAsAsync<Poi>();
}
return item;
}
public async Task<bool> UpdateItemAsync(Poi item)
{
HttpResponseMessage response = await Client.PatchAsJsonAsync($"api/poi/{item.Id}", item);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
response.Content.Headers.ContentType.MediaType = "application/json";
return true;
}
public async Task<bool> DeleteItemAsync(Poi item)
{
HttpResponseMessage response = await Client.DeleteAsync($"api/poi/{item.Id}");
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
response.Content.Headers.ContentType.MediaType = "application/json";
return true;
}
}
}

View File

@ -0,0 +1,81 @@
namespace Simulator.DataStore.Stores
{
using Simulator.DataObjects;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
public class TripPointStore : BaseStore//, IBaseStore<TripPoint>
{
public TripPointStore(IHttpClientFactory clientFactory, string EndPoint) : base(clientFactory)
{
base.InitializeStore(EndPoint);
}
public async Task<TripPoint> GetItemAsync(string id)
{
//Deviating implemetnation because of complxity of the nested API
TripPoint tripPoint = null;
HttpResponseMessage response = await Client.GetAsync($"/api/trips/{id}/trippoints/{id}");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
tripPoint = await response.Content.ReadAsAsync<TripPoint>();
}
return tripPoint;
}
public async Task<TripPoint> GetItemAsync(TripPoint item)
{
//Deviating implemetnation because of complxity of the nested API
TripPoint tripPoint = null;
HttpResponseMessage response = await Client.GetAsync($"/api/trips/{item.TripId}/trippoints/{item.Id}");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
tripPoint = await response.Content.ReadAsAsync<TripPoint>();
}
return tripPoint;
}
public Task<List<TripPoint>> GetItemsAsync()
{
throw new NotImplementedException();
}
public async Task<List<TripPoint>> GetItemsAsync(Trip item)
{
List<TripPoint> tripPoints = null;
HttpResponseMessage response = await Client.GetAsync($"api/trips/{item.Id}/trippoints");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
tripPoints = await response.Content.ReadAsAsync<List<TripPoint>>();
}
return tripPoints;
}
public async Task<TripPoint> CreateItemAsync(TripPoint item)
{
string apiPath = $"api/trips/{item.TripId.ToString()}/trippoints";
HttpResponseMessage response = await Client.PostAsJsonAsync<TripPoint>(apiPath, item);
response.EnsureSuccessStatusCode();
response.Content.Headers.ContentType.MediaType = "application/json";
item = await response.Content.ReadAsAsync<TripPoint>();
return item;
}
public Task<bool> UpdateItemAsync(TripPoint item)
{
throw new NotImplementedException();
}
public Task<bool> DeleteItemAsync(TripPoint item)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,44 @@
namespace Simulator.DataStore.Stores
{
using Simulator.DataObjects;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
public class TripStore : BaseStore//, IBaseStore<Trip>
{
public TripStore(IHttpClientFactory clientFactory, string EndPoint) : base(clientFactory)
{
base.InitializeStore(EndPoint);
}
public async Task<Trip> GetItemAsync(string id)
{
Trip trip = null;
HttpResponseMessage response = await Client.GetAsync($"/api/trips/{id}");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
trip = await response.Content.ReadAsAsync<Trip>();
}
return trip;
}
public async Task<List<Trip>> GetItemsAsync()
{
List<Trip> trips = null;
HttpResponseMessage response = await Client.GetAsync("api/trips/");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
trips = await response.Content.ReadAsAsync<List<Trip>>();
}
return trips;
}
}
}

View File

@ -0,0 +1,56 @@
namespace Simulator.DataStore.Stores
{
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Simulator.DataObjects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
public class UserStore : BaseStore//, IBaseStore<User>
{
private readonly IConfiguration Configuration;
public UserStore(IHttpClientFactory clientFactory, string EndPoint, IConfiguration configuration) : base(clientFactory)
{
base.InitializeStore(EndPoint);
Configuration = configuration;
}
public async Task<User> GetItemAsync(string id)
{
User user = null;
// var baseUrl = Configuration.GetValue<string>("USER_ROOT_URL");
HttpResponseMessage response = await Client.GetAsync($"/api/user/{id}");
if (response.IsSuccessStatusCode)
{
response.Content.Headers.ContentType.MediaType = "application/json";
user = await response.Content.ReadAsAsync<User>();
}
return user;
}
public async Task<List<User>> GetItemsAsync()
{
List<User> users = null;
HttpResponseMessage response = await Client.GetAsync("/api/user/");
if (response.IsSuccessStatusCode)
{
var contents = await response.Content.ReadAsStreamAsync();
var serializer = new JsonSerializer();
using (var sr = new StreamReader(contents))
using (var jsonTextReader = new JsonTextReader(sr))
{
users = serializer.Deserialize<List<User>>(jsonTextReader);
}
}
return users;
}
}
}