sharee.bike-App/LockIt.BusinessLogic/Model/Bikes/Bike/BluetoothLock/LockInfo.cs

125 lines
3.7 KiB
C#
Raw Permalink Normal View History

2022-09-20 13:51:55 +02:00
using System;
2021-05-13 17:07:16 +02:00
using System.Runtime.Serialization;
2022-08-30 15:42:25 +02:00
using Newtonsoft.Json;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Connector;
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
/// <summary> Locking states. </summary>
public enum LockingState
{
/// <summary> App is not connected to lock.</summary>
UnknownDisconnected,
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Lock might be open, closed or something in between.</summary>
/// <remarks>
/// Under certain circumstances lock reports/ is known to be in this state (genuine ILOCKIT locking state LockitLockingState.Unknown).
/// Example: If bold is blocked it might happen that opening or closing lock leads to state unknown, i.e. might be open, closed or inbetween.
/// </remarks>
UnknownFromHardwareError,
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Lock is closed. </summary>
Closed,
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Lock is open. </summary>
Open
}
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
[DataContract]
public class LockInfo : ILockInfo, IEquatable<LockInfo>
{
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
[DataMember]
public int Id { get; private set; } = TextToLockItTypeHelper.INVALIDLOCKID;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Identification GUID of bluetooth lock.</summary>
[DataMember]
public Guid Guid { get; private set; } = TextToLockItTypeHelper.INVALIDLOCKGUID;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
[DataMember]
public byte[] UserKey { get; private set; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
[DataMember]
public byte[] AdminKey { get; private set; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
[DataMember]
public byte[] Seed { get; private set; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Locking state of bluetooth lock. </summary>
[DataMember]
public LockingState State { get; private set; } = LockingState.UnknownDisconnected;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public bool IsIdValid => Id != TextToLockItTypeHelper.INVALIDLOCKID;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public bool IsGuidValid => Guid != TextToLockItTypeHelper.INVALIDLOCKGUID;
2021-05-13 17:07:16 +02:00
2022-09-20 13:51:55 +02:00
public override bool Equals(object obj)
=> Equals(obj as LockInfo);
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public bool Equals(LockInfo other)
{
2022-09-20 13:51:55 +02:00
if (ReferenceEquals(other, null)) return false;
if (ReferenceEquals(this, other)) return true;
if (GetType() != other.GetType()) return false;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
return ToString() == other.ToString();
}
2021-05-13 17:07:16 +02:00
2022-09-20 13:51:55 +02:00
public override int GetHashCode() => ToString().GetHashCode();
2021-05-13 17:07:16 +02:00
2022-09-20 13:51:55 +02:00
public override string ToString() => JsonConvert.SerializeObject(this);
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public static bool operator ==(LockInfo lhs, LockInfo rhs)
{
2022-09-20 13:51:55 +02:00
if (ReferenceEquals(lhs, null))
return ReferenceEquals(rhs, null) ? true /*null == null = true*/: false;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
return lhs.Equals(rhs);
}
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public static bool operator !=(LockInfo lhs, LockInfo rhs)
2022-09-20 13:51:55 +02:00
=> !(lhs == rhs);
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public class Builder
{
public Builder(LockInfo lockInfo = null)
{
if (lockInfo == null)
{
return;
}
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
LockInfo = JsonConvert.DeserializeObject<LockInfo>(JsonConvert.SerializeObject(lockInfo));
}
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
private readonly LockInfo LockInfo = new LockInfo();
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public byte[] UserKey { get => LockInfo.UserKey; set => LockInfo.UserKey = value; }
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
public byte[] AdminKey { get => LockInfo.AdminKey; set => LockInfo.AdminKey = value; }
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
public byte[] Seed { get => LockInfo.Seed; set => LockInfo.Seed = value; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public int Id { get => LockInfo.Id; set => LockInfo.Id = value; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public Guid Guid { get => LockInfo.Guid; set => LockInfo.Guid = value; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public LockingState State { get => LockInfo.State; set => LockInfo.State = value; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public LockInfo Build()
{
// Ensure consistency.
if ((UserKey?.Length > 0 || Seed?.Length > 0)
&& (UserKey?.Length == 0 || Seed?.Length == 0 || !LockInfo.IsIdValid))
throw new ArgumentException($"Can not build {typeof(LockInfo).Name}. Lock parameters must either be all know or all unknown.");
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
if (UserKey == null) UserKey = new byte[0];
if (AdminKey == null) AdminKey = new byte[0];
if (Seed == null) Seed = new byte[0];
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
return LockInfo;
}
}
}
2021-05-13 17:07:16 +02:00
}