sharee.bike-App/TINKLib/Model/Bikes/BikeInfoNS/BluetoothLock/LockInfoMutable.cs
2023-08-31 12:20:06 +02:00

89 lines
2.7 KiB
C#

using System;
using TINK.Services.Geolocation;
namespace TINK.Model.Bikes.BikeInfoNS.BluetoothLock
{
public class LockInfoMutable : ILockInfoMutable
{
/// <summary> Lock info object. </summary>
private LockInfo LockInfo { get; set; }
/// <summary>
/// Delegate to create time stamp.
/// </summary>
private Func<DateTime> _nowDelegate;
/// <summary> Constructs a bluetooth lock info object. </summary>
/// <param name="id">Id of lock must always been known when constructing an lock info object.</param>
/// <param name="nowDelegate">Delegate to create time stamp if null DateTime.Now is used.</param>
public LockInfoMutable(
int id,
Guid guid,
byte[] userKey,
byte[] adminKey,
byte[] seed,
LockingState state,
Func<DateTime> nowDelegate = null)
{
_nowDelegate = nowDelegate ?? (() => DateTime.Now);
LockInfo = new LockInfo.Builder() { Id = id, Guid = guid, UserKey = userKey, AdminKey = adminKey, Seed = seed, State = state }.Build();
}
public int Id => LockInfo.Id;
/// <summary> Changes during runtime: Can be unknown when set from copri and chang to a valid value when set from lock.</summary>
public Guid Guid
{
get => LockInfo.Guid;
set => LockInfo = new LockInfo.Builder(LockInfo) { Guid = value }.Build();
}
public byte[] Seed => LockInfo.Seed;
public byte[] UserKey => LockInfo.UserKey;
public byte[] AdminKey => LockInfo.AdminKey;
/// <summary>
/// Gets or sets the locking state.
/// </summary>
public LockingState State
{
get => LockInfo.State;
set
{
if (LockInfo.State == value)
{
// State does not change, nothing to do.
return;
}
Location = null; // Invalidate location.
LastLockingStateChange = _nowDelegate(); // Get time stamp when state change happened.
LockInfo = new LockInfo.Builder(LockInfo) { State = value }.Build();
}
}
/// <summary> Gets the time stamp of the last locking state change.</summary>
public DateTime? LastLockingStateChange { get; private set; }
/// <summary>
/// Gets or sets the current location of the bike, null if location is unknown.
/// </summary>
public IGeolocation Location { get; set; }
/// <summary> Holds the percentage of lock battery.</summary>
public double BatteryPercentage { get; set; } = double.NaN;
/// <summary>
/// Gets the version info of the lock.
/// </summary>
public IVersionInfo VersionInfo { get; set; } = new VersionInfo.Builder().Build();
/// <summary> Loads lock info object from values. </summary>
public void Load(int id, Guid guid, byte[] seed, byte[] userKey, byte[] adminKey)
{
LockInfo = new LockInfo.Builder(LockInfo) { Id = id, Guid = guid, Seed = seed, UserKey = userKey, AdminKey = adminKey }.Build();
}
}
}