mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
39 lines
1.6 KiB
C#
39 lines
1.6 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace TINK.Model.Bike
|
|||
|
{
|
|||
|
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<BluetoothLock.LockInfo> locksInfo)
|
|||
|
{
|
|||
|
|
|||
|
var updatedBikesCollection = new Dictionary<int, BC.BikeInfo>();
|
|||
|
|
|||
|
foreach (var bikeInfo in bikes)
|
|||
|
{
|
|||
|
if (!(bikeInfo is BluetoothLock.BikeInfo bluetoothBikeInfo))
|
|||
|
{
|
|||
|
// No processing needed because bike is not a bluetooth bike
|
|||
|
updatedBikesCollection.Add(bikeInfo.Id, bikeInfo);
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
// 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.
|
|||
|
|
|||
|
updatedBikesCollection.Add(bluetoothBikeInfo.Id, new BluetoothLock.BikeInfo(bluetoothBikeInfo, currentLockInfo));
|
|||
|
}
|
|||
|
|
|||
|
return new BikeCollection(updatedBikesCollection);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|