using System;
using System.Collections.Generic;
using System.Linq;
using ShareeBike.Services.BluetoothLock.Tdo;
namespace ShareeBike.Model.Bikes.BikeInfoNS.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.UnknownDisconnected;
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.CouldntOpenBoltBlocked: // Lock is closed in most cases, but this is not guaranteed according to haveltec.
return LockingState.UnknownFromHardwareError;
case LockitLockingState.Open:
case LockitLockingState.CouldntCloseMoving:
case LockitLockingState.CouldntCloseBoltBlocked:
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();
}
}
}