sharee.bike-App/TINKLib/Model/Bikes/BikeCollectionUpdater.cs

39 lines
1.5 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.Linq;
2022-08-30 15:42:25 +02:00
namespace TINK.Model.Bikes
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
public static class BikeCollectionUpdater
{
/// <summary> Updates bikes lock info with the latest lock info from bluetooth service.</summary>
/// <param name="bikes">bikes to be updated.</param>
/// <param name="locksInfo">locks info to be used for updating bikes.</param>
/// <returns></returns>
public static BikeCollection UpdateLockInfo(
this BikeCollection bikes,
IEnumerable<Bikes.BikeInfoNS.BluetoothLock.LockInfo> locksInfo)
{
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
var updatedBikesCollection = new Dictionary<string, Bikes.BikeInfoNS.BC.BikeInfo>();
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
foreach (var bikeInfo in bikes)
{
if (!(bikeInfo is BikeInfoNS.BluetoothLock.BikeInfo bluetoothBikeInfo))
{
// No processing needed because bike is not a bluetooth bike
updatedBikesCollection.Add(bikeInfo.Id, bikeInfo);
continue;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
// Check if to update current bike lock info using state from bluetooth service or not.
var currentLockInfo = locksInfo.FirstOrDefault(x => x.Id == bluetoothBikeInfo.LockInfo.Id) // Update bike info with latest info from bluethooth service if available
?? bluetoothBikeInfo.LockInfo; // Use lock info state object from copri which holds a lock id and a state of value unknown.
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
updatedBikesCollection.Add(bluetoothBikeInfo.Id, new BikeInfoNS.BluetoothLock.BikeInfo(bluetoothBikeInfo, currentLockInfo));
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return new BikeCollection(updatedBikesCollection);
}
}
2021-05-13 20:03:07 +02:00
}