sharee.bike-App/LockIt.BusinessLogic/Services/BluetoothLock/LockInfoHelper.cs

71 lines
2.3 KiB
C#
Raw Normal View History

2023-08-31 12:20:06 +02:00
using System;
2021-05-13 17:07:16 +02:00
using System.Collections.Generic;
using System.Linq;
2024-04-09 12:53:23 +02:00
using ShareeBike.Services.BluetoothLock.Tdo;
2021-05-13 17:07:16 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.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:
2023-08-31 12:20:06 +02:00
case LockitLockingState.CouldntOpenBoltBlocked: // Lock is closed in most cases, but this is not guaranteed according to haveltec.
2022-09-06 16:08:19 +02:00
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:
2023-08-31 12:20:06 +02:00
case LockitLockingState.CouldntCloseBoltBlocked:
2022-09-06 16:08:19 +02:00
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
}