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 { /// Gets bikes available from copri server response. /// Response to create collection from. /// Specified the data source /// New collection of available bikes. 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); /// Gets bikes occupied from copri server response. /// Response to create bikes from. /// Specified the data source /// New collection of occupied bikes. public static BikeCollection GetBikesOccupied( this BikesReservedOccupiedResponse bikesOccupiedResponse, string mail, Func dateTimeProvider, DataSource dataSource) => GetBikesAll( new BikesAvailableResponse()?.bikes?.Values, bikesOccupiedResponse?.bikes_occupied?.Values, mail, dateTimeProvider, dataSource); /// Gets bikes occupied from copri server response. /// Response to create bikes available from. /// Response to create bikes occupied from. /// New collection of occupied bikes. public static BikeCollection GetBikesAll( IEnumerable bikesAvailable, IEnumerable bikesOccupied, string mail, Func dateTimeProvider, DataSource dataSource) { var bikesDictionary = new Dictionary(); var duplicates = new Dictionary(); // 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); } } }