sharee.bike-App/LockItShared/Services/BluetoothLock/LockInfoHelper.cs

71 lines
2.7 KiB
C#
Raw Normal View History

2021-05-13 17:07:16 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using TINK.Services.BluetoothLock.Tdo;
namespace TINK.Model.Bike.BluetoothLock
{
public static class LockInfoHelper
{
/// <summary> Update by id.</summary>
/// <param name="locksInfo"> Lock info objects to update by id.</param>
/// <param name="locksInfoTdo">Tdos holding data to updat from</param>
/// <returns></returns>
public static IEnumerable<LockInfo> UpdateById(
this IEnumerable<LockInfo> locksInfo,
IEnumerable<LockInfoTdo> locksInfoTdo)
{
var locksInfoUpdated = new List<LockInfo>();
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.");
}
/// <summary> Gets one type from another.</summary>
/// <param name="lockInfo"></param>
/// <returns></returns>
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();
}
}
}