sharee.bike-App/TINKLib/Model/Bikes/BikeInfoNS/BluetoothLock/LockInfoMutable.cs

90 lines
2.7 KiB
C#
Raw Normal View History

2022-09-20 13:51:55 +02:00
using System;
2023-04-05 15:02:10 +02:00
using TINK.Services.Geolocation;
2021-05-13 20:03:07 +02:00
2022-08-30 15:42:25 +02:00
namespace TINK.Model.Bikes.BikeInfoNS.BluetoothLock
2021-05-13 20:03:07 +02:00
{
2022-09-20 13:51:55 +02:00
public class LockInfoMutable : ILockInfoMutable
2022-09-06 16:08:19 +02:00
{
/// <summary> Lock info object. </summary>
2022-09-20 13:51:55 +02:00
private LockInfo LockInfo { get; set; }
2022-09-06 16:08:19 +02:00
2023-04-05 15:02:10 +02:00
/// <summary>
/// Delegate to create time stamp.
/// </summary>
private Func<DateTime> _nowDelegate;
2022-09-06 16:08:19 +02:00
/// <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>
2023-04-05 15:02:10 +02:00
/// <param name="nowDelegate">Delegate to create time stamp if null DateTime.Now is used.</param>
2022-09-06 16:08:19 +02:00
public LockInfoMutable(
int id,
Guid guid,
byte[] userKey,
byte[] adminKey,
byte[] seed,
2023-04-05 15:02:10 +02:00
LockingState state,
Func<DateTime> nowDelegate = null)
2022-09-06 16:08:19 +02:00
{
2023-04-05 15:02:10 +02:00
_nowDelegate = nowDelegate ?? (() => DateTime.Now);
2022-09-20 13:51:55 +02:00
LockInfo = new LockInfo.Builder() { Id = id, Guid = guid, UserKey = userKey, AdminKey = adminKey, Seed = seed, State = state }.Build();
2022-09-06 16:08:19 +02:00
}
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;
2022-09-20 13:51:55 +02:00
set => LockInfo = new LockInfo.Builder(LockInfo) { Guid = value }.Build();
2022-09-06 16:08:19 +02:00
}
public byte[] Seed => LockInfo.Seed;
public byte[] UserKey => LockInfo.UserKey;
public byte[] AdminKey => LockInfo.AdminKey;
2023-04-05 15:02:10 +02:00
/// <summary>
/// Gets or sets the locking state.
/// </summary>
2022-09-20 13:51:55 +02:00
public LockingState State
2022-09-06 16:08:19 +02:00
{
get => LockInfo.State;
2023-04-05 15:02:10 +02:00
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();
}
2022-09-06 16:08:19 +02:00
}
2023-08-31 12:20:06 +02:00
/// <summary> Gets the time stamp of the last locking state change.</summary>
2023-04-05 15:02:10 +02:00
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; }
2022-09-06 16:08:19 +02:00
/// <summary> Holds the percentage of lock battery.</summary>
public double BatteryPercentage { get; set; } = double.NaN;
2022-09-20 13:51:55 +02:00
/// <summary>
/// Gets the version info of the lock.
/// </summary>
public IVersionInfo VersionInfo { get; set; } = new VersionInfo.Builder().Build();
2022-09-06 16:08:19 +02:00
/// <summary> Loads lock info object from values. </summary>
public void Load(int id, Guid guid, byte[] seed, byte[] userKey, byte[] adminKey)
{
2022-09-20 13:51:55 +02:00
LockInfo = new LockInfo.Builder(LockInfo) { Id = id, Guid = guid, Seed = seed, UserKey = userKey, AdminKey = adminKey }.Build();
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 20:03:07 +02:00
}