2021-05-13 17:07:16 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using TINK.Services.BluetoothLock.Tdo;
|
|
|
|
|
|
2022-08-30 15:42:25 +02:00
|
|
|
|
namespace TINK.Model.Bikes.BikeInfoNS.BluetoothLock
|
2021-05-13 17:07:16 +02:00
|
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
|
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>();
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
var state = lockInfoTdo.State.HasValue ? lockInfoTdo.State.Value.GetLockingState() : LockingState.UnknownDisconnected;
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
return locksInfoUpdated;
|
|
|
|
|
}
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
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.UnknownFromHardwareError;
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
case LockitLockingState.Open:
|
|
|
|
|
case LockitLockingState.CouldntCloseMoving:
|
|
|
|
|
case LockitLockingState.CouldntCloseBoldBlocked:
|
|
|
|
|
return LockingState.Open;
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
case LockitLockingState.Closed:
|
|
|
|
|
return LockingState.Closed;
|
|
|
|
|
}
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
throw new ArgumentException($"Can not convert LockIt specific locking state to logical locking state. Unknown state {lockingState} detected.");
|
|
|
|
|
}
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <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();
|
|
|
|
|
}
|
2021-05-13 17:07:16 +02:00
|
|
|
|
|
2022-08-30 15:42:25 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
}
|
2021-05-13 17:07:16 +02:00
|
|
|
|
}
|