mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-23 07:36:31 +01:00
129 lines
3.8 KiB
C#
129 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using TINK.Model.Bikes.BikeInfoNS.BC;
|
|
using TINK.Model.Bikes;
|
|
using TINK.Repository.Response;
|
|
using Serilog;
|
|
|
|
namespace TINK.Model.Connector.Updater
|
|
{
|
|
public static class BikeCollectionFactory
|
|
{
|
|
/// <summary> Gets bikes available from copri server response.</summary>
|
|
/// <param name="bikesAvailableResponse">Response to create collection from.</param>
|
|
/// <param name="dataSource">Specified the data source</param>
|
|
/// <returns>New collection of available bikes.</returns>
|
|
public static BikeCollection GetBikesAvailable(
|
|
this BikesAvailableResponse bikesAvailableResponse,
|
|
DataSource dataSource)
|
|
=> GetBikesAll(
|
|
bikesAvailableResponse?.bikes?.Values,
|
|
new BikesReservedOccupiedResponse()?.bikes_occupied?.Values, // There are no occupied bikes.
|
|
string.Empty,
|
|
() => DateTime.Now,
|
|
dataSource);
|
|
|
|
/// <summary> Gets bikes occupied from copri server response. </summary>
|
|
/// <param name="bikesOccupiedResponse">Response to create bikes from.</param>
|
|
/// <param name="dataSource">Specified the data source</param>
|
|
/// <returns>New collection of occupied bikes.</returns>
|
|
public static BikeCollection GetBikesOccupied(
|
|
this BikesReservedOccupiedResponse bikesOccupiedResponse,
|
|
string mail,
|
|
Func<DateTime> dateTimeProvider,
|
|
DataSource dataSource)
|
|
=> GetBikesAll(
|
|
new BikesAvailableResponse()?.bikes?.Values,
|
|
bikesOccupiedResponse?.bikes_occupied?.Values,
|
|
mail,
|
|
dateTimeProvider,
|
|
dataSource);
|
|
|
|
/// <summary> Gets bikes occupied from copri server response. </summary>
|
|
/// <param name="bikesAvailable">Response to create bikes available from.</param>
|
|
/// <param name="bikesOccupied">Response to create bikes occupied from.</param>
|
|
/// <returns>New collection of occupied bikes.</returns>
|
|
public static BikeCollection GetBikesAll(
|
|
IEnumerable<BikeInfoAvailable> bikesAvailable,
|
|
IEnumerable<BikeInfoReservedOrBooked> bikesOccupied,
|
|
string mail,
|
|
Func<DateTime> dateTimeProvider,
|
|
DataSource dataSource)
|
|
{
|
|
var bikesDictionary = new Dictionary<string, BikeInfo>();
|
|
var duplicates = new Dictionary<string, BikeInfo>();
|
|
|
|
// Get bikes from Copri/ file/ memory, ....
|
|
if (bikesAvailable != null)
|
|
{
|
|
foreach (var bikeInfoResponse in bikesAvailable)
|
|
{
|
|
var bikeInfo = BikeInfoFactory.Create(bikeInfoResponse, dataSource);
|
|
if (bikeInfo == null)
|
|
{
|
|
// Response is not valid.
|
|
continue;
|
|
}
|
|
|
|
if (bikesDictionary.ContainsKey(bikeInfo.Id))
|
|
{
|
|
// Duplicates are not allowed.
|
|
Log.Error($"Duplicate bike with id {bikeInfo.Id} detected evaluating bikes available. Bike status is {bikeInfo.State.Value}.");
|
|
|
|
if (!duplicates.ContainsKey(bikeInfo.Id))
|
|
{
|
|
duplicates.Add(bikeInfo.Id, bikeInfo);
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
bikesDictionary.Add(bikeInfo.Id, bikeInfo);
|
|
}
|
|
}
|
|
|
|
// Get bikes from Copri/ file/ memory, ....
|
|
if (bikesOccupied != null)
|
|
{
|
|
foreach (var bikeInfoResponse in bikesOccupied)
|
|
{
|
|
BikeInfo bikeInfo = BikeInfoFactory.Create(
|
|
bikeInfoResponse,
|
|
mail,
|
|
dateTimeProvider,
|
|
dataSource);
|
|
|
|
if (bikeInfo == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (bikesDictionary.ContainsKey(bikeInfo.Id))
|
|
{
|
|
// Duplicates are not allowed.
|
|
Log.Error($"Duplicate bike with id {bikeInfo.Id} detected evaluating bikes occupied. Bike status is {bikeInfo.State.Value}.");
|
|
if (!duplicates.ContainsKey(bikeInfo.Id))
|
|
{
|
|
duplicates.Add(bikeInfo.Id, bikeInfo);
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
|
|
bikesDictionary.Add(bikeInfo.Id, bikeInfo);
|
|
}
|
|
}
|
|
|
|
// Remove entries which are not unique.
|
|
foreach (var l_oDuplicate in duplicates)
|
|
{
|
|
bikesDictionary.Remove(l_oDuplicate.Key);
|
|
}
|
|
|
|
return new BikeCollection(bikesDictionary);
|
|
}
|
|
|
|
}
|
|
}
|