using System; using System.Collections.Generic; using System.Linq; using TINK.Services.BluetoothLock.Tdo; namespace TINK.Model.Bike.BluetoothLock { public static class LockInfoHelper { /// Update by id. /// Lock info objects to update by id. /// Tdos holding data to updat from /// public static IEnumerable UpdateById( this IEnumerable locksInfo, IEnumerable locksInfoTdo) { var locksInfoUpdated = new List(); foreach (var lockInfo in locksInfo) { var lockInfoTdo = locksInfoTdo.FirstOrDefault(x => x.Id == lockInfo.Id); if (lockInfoTdo == null) { // No object to update from found. locksInfoUpdated.Add(lockInfo); continue; } var state = lockInfoTdo.State.HasValue ? lockInfoTdo.State.Value.GetLockingState() : LockingState.Disconnected; locksInfoUpdated.Add(state != lockInfo.State || lockInfoTdo.Guid != lockInfo.Guid ? new LockInfo.Builder(lockInfo) { Guid = lockInfoTdo.Guid, State = state}.Build() // State has changed, update required. : lockInfo); } return locksInfoUpdated; } public static LockingState GetLockingState(this LockitLockingState lockingState) { switch (lockingState) { case LockitLockingState.Unknown: case LockitLockingState.CouldntOpenBoldBlocked: // Lock is closed in most cases, but this is not guarnteed according to haveltec. return LockingState.Unknown; case LockitLockingState.Open: case LockitLockingState.CouldntCloseMoving: case LockitLockingState.CouldntCloseBoldBlocked: return LockingState.Open; case LockitLockingState.Closed: return LockingState.Closed; } throw new ArgumentException($"Can not convert LockIt specific locking state to logical locking state. Unknown state {lockingState} detected."); } /// Gets one type from another. /// /// public static LockInfoAuthTdo ToLockInfoTdo(this LockInfo lockInfo) { return new LockInfoAuthTdo.Builder() { Id = lockInfo.Id, Guid = lockInfo.Guid, K_u = lockInfo.UserKey, K_a = lockInfo.AdminKey, K_seed = lockInfo.Seed }.Build(); } } }