Initial version.

This commit is contained in:
Oliver Hauff 2021-05-13 20:03:07 +02:00
parent 193aaa1a56
commit b72c67a53e
228 changed files with 25924 additions and 0 deletions

View file

@ -0,0 +1,216 @@
using System;
using System.Collections.Generic;
using TINK.Model.Bikes.Bike;
using TINK.Model.State;
namespace TINK.Model.Bike.BC
{
public class BikeInfo : IBikeInfo
{
/// <summary> Default value of demo property. </summary>
public const bool DEFAULTVALUEISDEMO = false;
/// <summary> Holds the info about the bike state. </summary>
private readonly IStateInfo m_oStateInfo;
/// <summary>
/// Holds the bike object.
/// </summary>
private Bike Bike { get; }
/// <summary> Constructs a bike object.</summary>
protected BikeInfo(
IStateInfo stateInfo,
int id,
bool? isDemo = DEFAULTVALUEISDEMO,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null,
int? currentStationId = null,
Uri operatorUri = null,
TariffDescription tariffDescription = null)
{
Bike = new Bike(id, wheelType, typeOfBike, description);
m_oStateInfo = stateInfo;
IsDemo = isDemo ?? DEFAULTVALUEISDEMO;
Group = group ?? new List<string>();
CurrentStation = currentStationId;
OperatorUri = operatorUri;
TariffDescription = tariffDescription;
}
public BikeInfo(BikeInfo bikeInfo) : this(
bikeInfo?.State,
bikeInfo?.Id ?? throw new ArgumentException($"Can not copy-construct {typeof(BikeInfo).Name}-object. Source must not be null."),
bikeInfo.IsDemo,
bikeInfo.Group,
bikeInfo.WheelType,
bikeInfo.TypeOfBike,
bikeInfo.Description,
bikeInfo.CurrentStation,
bikeInfo.OperatorUri,
bikeInfo.TariffDescription) { }
/// <summary>
/// Constructs a bike info object for a available bike.
/// </summary>
/// <param name="id">Unique id of bike.</param>
/// <param name="currentStationId">Id of station where bike is located.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <param name="tariffDescription">Hold tariff description of bike.</param>
/// <param name="wheelType"></param>
public BikeInfo(
int id,
int? currentStationId,
Uri operatorUri = null,
TariffDescription tariffDescription = null,
bool? isDemo = DEFAULTVALUEISDEMO,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null) : this(
new StateInfo(),
id,
isDemo,
group,
wheelType,
typeOfBike,
description,
currentStationId,
operatorUri,
tariffDescription)
{
}
/// <summary>
/// Constructs a bike info object for a requested bike.
/// </summary>
/// <param name="dateTimeProvider">Provider for current date time to calculate remainig time on demand for state of type reserved.</param>
/// <param name="wheelType"></param>
/// <param name="id">Unique id of bike.</param>
/// <param name="stationId">Name of station where bike is located, null if bike is on the road.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <param name="tariffDescription">Hold tariff description of bike.</param>
/// <param name="requestedAt">Date time when bike was requested</param>
/// <param name="mailAddress">Mail address of user which requested bike.</param>
/// <param name="code">Booking code.</param>
/// <param name="p_oDateTimeNowProvider">Date time provider to calculate reaining time.</param>
public BikeInfo(
int id,
bool? isDemo,
IEnumerable<string> group,
WheelType? wheelType,
TypeOfBike? typeOfBike,
string description,
int? stationId,
Uri operatorUri,
TariffDescription tariffDescription,
DateTime requestedAt,
string mailAddress,
string code,
Func<DateTime> dateTimeProvider = null) : this(
new StateInfo(
dateTimeProvider,
requestedAt,
mailAddress,
code),
id,
isDemo,
group,
wheelType,
typeOfBike,
description,
stationId,
operatorUri,
tariffDescription)
{
}
/// <summary>
/// Constructs a bike info object for a booked bike.
/// </summary>
/// <param name="dateTimeProvider">Provider for current date time to calculate remainig time on demand for state of type reserved.</param>
/// <param name="wheelType"></param>
/// <param name="id">Unique id of bike.</param>
/// <param name="currentStationId">Name of station where bike is located, null if bike is on the road.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <param name="tariffDescription">Hold tariff description of bike.</param>
/// <param name="bookedAt">Date time when bike was booked</param>
/// <param name="mailAddress">Mail address of user which booked bike.</param>
/// <param name="code">Booking code.</param>
public BikeInfo(
int id,
bool? isDemo,
IEnumerable<string> group,
WheelType? wheelType,
TypeOfBike? typeOfBike,
string description,
int? currentStationId,
Uri operatorUri,
TariffDescription tariffDescription,
DateTime bookedAt,
string mailAddress,
string code) : this(
new StateInfo(
bookedAt,
mailAddress,
code),
id,
isDemo,
group,
wheelType,
typeOfBike,
description,
currentStationId,
operatorUri,
tariffDescription)
{
}
/// <summary> True if device is demo device, false otherwise. </summary>
public bool IsDemo { get; }
/// <summary> Returns the group (TINK, Konrad, ...). </summary>
public IEnumerable<string> Group { get; }
/// <summary>
/// Station a which bike is located, null otherwise.
/// </summary>
public int? CurrentStation { get; }
/// <summary> Holds description about the tarif. </summary>
public TariffDescription TariffDescription { get; }
/// Holds the rent state of the bike.
/// </summary>
public IStateInfo State
{
get { return m_oStateInfo; }
}
public int Id => Bike.Id;
public WheelType? WheelType => Bike.WheelType;
public TypeOfBike? TypeOfBike => Bike.TypeOfBike;
public string Description => Bike.Description;
/// <summary>
/// Uri of the operator or null, in case of single operator setup.
/// </summary>
public Uri OperatorUri { get; }
/// <summary>
/// Converts the instance to text.
/// </summary>
public new string ToString()
{
return $"Id={Bike.Id}{(Bike.WheelType != null ? $", wheel(s)={Bike.WheelType}" : string.Empty)}{(Bike.TypeOfBike != null ? $"type={Bike.TypeOfBike}" : "")}, state={State}, location={(CurrentStation.HasValue ? $"Station {CurrentStation}" : "On the road")}, is demo={IsDemo}.";
}
}
}

View file

@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using TINK.Model.Bikes.Bike;
using TINK.Model.Bikes.Bike.BC;
using TINK.Model.State;
namespace TINK.Model.Bike.BC
{
[DataContract]
public class BikeInfoMutable : IBikeInfoMutable, INotifyPropertyChanged
{
/// <summary> Holds the bike. </summary>
private readonly Bike m_oBike;
/// <summary> Holds the state info of the bike. </summary>
private readonly StateInfoMutable m_oStateInfo;
/// <summary>
/// Constructs a bike.
/// </summary>
/// <param name="id">Unique id of bike.</param>
/// <param name="isDemo">True if device is demo device, false otherwise.</param>
/// <param name="dateTimeProvider">Provider for current date time to calculate remainig time on demand for state of type reserved.</param>
/// <param name="wheelType"></param>
/// <param name="currentStationId">Name of station where bike is located, null if bike is on the road.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <param name="tariffDescription">Hold tariff description of bike.</param>
/// <param name="stateInfo">Bike state info.</param>
protected BikeInfoMutable(
int id,
bool isDemo = BikeInfo.DEFAULTVALUEISDEMO,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null,
int? currentStationId = null,
Uri operatorUri = null,
TariffDescription tariffDescription = null,
Func<DateTime> dateTimeProvider = null,
IStateInfo stateInfo = null)
{
IsDemo = isDemo;
Group = group;
m_oBike = new Bike(id, wheelType, typeOfBike, description);
m_oStateInfo = new StateInfoMutable(dateTimeProvider, stateInfo);
m_oStateInfo.PropertyChanged += (sender, eventargs) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(eventargs.PropertyName));
CurrentStation = currentStationId;
OperatorUri = operatorUri;
TariffDescription = tariffDescription;
}
/// <summary> Constructs a bike object from source. </summary>
public BikeInfoMutable(IBikeInfo p_oBike) : this(
p_oBike.Id,
p_oBike.IsDemo,
p_oBike.Group,
p_oBike.WheelType,
p_oBike.TypeOfBike,
p_oBike.Description,
p_oBike.CurrentStation,
p_oBike.OperatorUri,
p_oBike.TariffDescription,
null,
p_oBike.State)
{
}
/// <summary>
/// Station a which bike is located, null otherwise.
/// </summary>
[DataMember]
public int? CurrentStation { get; }
/// <summary> Holds description about the tarif. </summary>
[DataMember]
public TariffDescription TariffDescription { get; private set; }
/// <summary>
/// Holds the rent state of the bike.
/// </summary>
[DataMember]
public StateInfoMutable State
{
get { return m_oStateInfo; }
}
/// <summary>
/// Uri of the operator or null, in case of single operator setup.
/// </summary>
public Uri OperatorUri { get; }
/// <summary> Unused member. </summary>
IStateInfoMutable IBikeInfoMutable.State => m_oStateInfo;
public int Id => m_oBike.Id;
public bool IsDemo { get; }
/// <summary> Returns the group (TINK, Konrad, ...). </summary>
public IEnumerable<string> Group { get; }
public WheelType? WheelType => m_oBike.WheelType;
public TypeOfBike? TypeOfBike => m_oBike.TypeOfBike;
public string Description => m_oBike.Description;
/// <summary>
/// Fired whenever property of bike changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Converts the instance to text.
/// </summary>
/// <returns></returns>
public new string ToString()
{
return $"Id={Id}{(WheelType != null ? $", wheel(s)={WheelType}" : string.Empty)}{(TypeOfBike != null ? $", type={TypeOfBike}" : "")}, demo={IsDemo}, state={State.ToString()}, location={(CurrentStation.HasValue ? $"Station {CurrentStation}" : "On the road")}.";
}
}
}

View file

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using TINK.Model.Bikes.Bike;
using TINK.Model.State;
namespace TINK.Model.Bike.BC
{
/// <summary>
/// Allows to access bike info.
/// </summary>
public interface IBikeInfo
{
/// <summary>
/// Holds the unique id of the bike;
/// </summary>
int Id { get; }
/// <summary> True if bike is a demo bike. </summary>
bool IsDemo { get; }
/// <summary> Returns the group (TINK, Konrad, ...). </summary>
IEnumerable<string> Group { get; }
/// <summary>
/// Holds the count of wheels.
/// </summary>
WheelType? WheelType { get; }
/// <summary>
/// Holds the type of bike.
/// </summary>
TypeOfBike? TypeOfBike { get; }
/// <summary> Holds the description of the bike. </summary>
string Description { get; }
/// <summary>
/// Station a which bike is located, null otherwise.
/// </summary>
int? CurrentStation { get; }
/// <summary>
/// Uri of the operator or null, in case of single operator setup.
/// </summary>
Uri OperatorUri { get; }
/// <summary> Holds description about the tarif. </summary>
TariffDescription TariffDescription { get; }
/// <summary>
/// Holds the rent state of the bike.
/// </summary>
IStateInfo State { get; }
}
}

View file

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using TINK.Model.Bike;
using TINK.Model.State;
namespace TINK.Model.Bikes.Bike.BC
{
public interface IBikeInfoMutable
{
/// <summary>
/// Holds the unique id of the bike;
/// </summary>
int Id { get; }
/// <summary> True if bike is a demo bike. </summary>
bool IsDemo { get; }
/// <summary> Returns the group (TINK, Konrad, ...). </summary>
IEnumerable<string> Group { get; }
/// <summary>
/// Holds the count of wheels.
/// </summary>
WheelType? WheelType { get; }
/// <summary>
/// Holds the type of bike.
/// </summary>
TypeOfBike? TypeOfBike { get; }
/// <summary> Holds the description of the bike. </summary>
string Description { get; }
/// <summary>
/// Station a which bike is located, null otherwise.
/// </summary>
int? CurrentStation { get; }
/// <summary>
/// Holds the rent state of the bike.
/// </summary>
IStateInfoMutable State { get; }
/// <summary>
/// Uri of the operator or null, in case of single operator setup.
/// </summary>
Uri OperatorUri { get; }
event PropertyChangedEventHandler PropertyChanged;
}
public enum NotifyPropertyChangedLevel
{
/// <summary> Notify about all property changes.</summary>
All,
/// <summary> Notify about no property changes.</summary>
None
}
}

View file

@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
namespace TINK.Model.Bike
{
/// <summary> Count of wheels. </summary>
public enum WheelType
{
Mono = 0,
Two = 1,
Trike = 2,
}
/// <summary> Type of bike. </summary>
public enum TypeOfBike
{
Allround = 0,
Cargo = 1,
Citybike = 2,
}
public class Bike : IEquatable<Bike>
{
/// <summary>
/// Constructs a bike.
/// </summary>
/// <param name="dateTimeProvider">Provider for current date time to calculate remainig time on demand for state of type reserved.</param>
/// <param name="wheelType"></param>
/// <param name="p_iId">Unique id of bike.</param>
/// <param name="p_strCurrentStationName">Name of station where bike is located, null if bike is on the road.</param>
public Bike(
int p_iId,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null)
{
WheelType = wheelType;
TypeOfBike = typeOfBike;
Id = p_iId;
Description = description;
}
/// <summary>
/// Holds the unique id of the bike;
/// </summary>
public int Id { get; }
/// <summary>
/// Holds the count of wheels.
/// </summary>
public WheelType? WheelType { get; }
/// <summary>
/// Holds the type of bike.
/// </summary>
public TypeOfBike? TypeOfBike { get; }
/// <summary> Holds the description of the bike. </summary>
public string Description { get; }
/// <summary> Compares two bike object.</summary>
/// <param name="obj">Object to compare with.</param>
/// <returns>True if bikes are equal.</returns>
public override bool Equals(object obj)
{
var l_oBike = obj as Bike;
if (l_oBike == null)
{
return false;
}
return Equals(l_oBike);
}
/// <summary> Converts the instance to text.</summary>
public new string ToString()
{
return WheelType == null || TypeOfBike == null
? $"Id={Id}{(!string.IsNullOrEmpty(Description) ? $", {Description}" : "")}"
: $"Id={Id}{(WheelType != null ? $", wheel(s)={WheelType}" : string.Empty)}{(TypeOfBike != null ? $"type={TypeOfBike}" : "")}.";
}
/// <summary> Compares two bike object.</summary>
/// <param name="obj">Object to compare with.</param>
/// <returns>True if bikes are equal.</returns>
public bool Equals(Bike other)
{
return other != null &&
Id == other.Id &&
WheelType == other.WheelType &&
TypeOfBike == other.TypeOfBike
&& Description == other.Description;
}
/// <summary> Compares two bike object.</summary>
/// <param name="obj">Object to compare with.</param>
/// <returns>True if bikes are equal.</returns>
public static bool operator ==(Bike bike1, Bike bike2)
{
return EqualityComparer<Bike>.Default.Equals(bike1, bike2);
}
/// <summary> Compares two bike object.</summary>
/// <param name="obj">Object to compare with.</param>
/// <returns>True if bikes are equal.</returns>
public static bool operator !=(Bike bike1, Bike bike2)
{
return !(bike1 == bike2);
}
/// <summary>
/// Generates hash code for bike object.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
var hashCode = -390870100;
hashCode = hashCode * -1521134295 + Id.GetHashCode();
hashCode = hashCode * -1521134295 + WheelType?.GetHashCode() ?? 0;
hashCode = hashCode * -1521134295 + TypeOfBike?.GetHashCode() ?? 0;
hashCode = hashCode * -1521134295 + Description?.GetHashCode() ?? 0;
return hashCode;
}
}
}

View file

@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using TINK.Model.Bikes.Bike;
using TINK.Model.State;
namespace TINK.Model.Bike.BluetoothLock
{
public class BikeInfo : BC.BikeInfo, IBikeInfo
{
/// <summary>
/// Constructs a bike info object for a available bike.
/// </summary>
/// <param name="bikeId">Unique id of bike.</param>
/// <param name="lockId">Id of the lock.</param>
/// <param name="lockGuid">GUID specifying the lock.</param>
/// <param name="currentStationId">Id of station where bike is located.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <param name="tariffDescription">Hold tariff description of bike.</param>
/// <param name="wheelType">Trike, two wheels, mono, ....</param>
public BikeInfo(
int bikeId,
int lockId,
Guid lockGuid,
int? currentStationId,
Uri operatorUri = null,
TariffDescription tariffDescription = null,
bool? isDemo = DEFAULTVALUEISDEMO,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null) : base(
new StateInfo(),
bikeId,
isDemo,
group,
wheelType,
typeOfBike,
description,
currentStationId,
operatorUri,
tariffDescription)
{
LockInfo = new LockInfo.Builder { Id = lockId, Guid = lockGuid }.Build();
}
/// <summary>
/// Constructs a bike info object for a requested bike.
/// </summary>
/// <param name="dateTimeProvider">Provider for current date time to calculate remainig time on demand for state of type reserved.</param>
/// <param name="id">Unique id of bike.</param>
/// <param name="lockId">Id of the lock.</param>
/// <param name="lockGuid">GUID specifying the lock.</param>
/// <param name="requestedAt">Date time when bike was requested</param>
/// <param name="mailAddress">Mail address of user which requested bike.</param>
/// <param name="currentStationId">Name of station where bike is located, null if bike is on the road.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <param name="tariffDescription">Hold tariff description of bike.</param>
/// <param name="p_oDateTimeNowProvider">Date time provider to calculate reaining time.</param>
/// <param name="wheelType"></param>
public BikeInfo(
int id,
int lockId,
Guid lockGuid,
byte[] userKey,
byte[] adminKey,
byte[] seed,
DateTime requestedAt,
string mailAddress,
int? currentStationId,
Uri operatorUri,
TariffDescription tariffDescription,
Func<DateTime> dateTimeProvider,
bool? isDemo = DEFAULTVALUEISDEMO,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null) : base(
new StateInfo(
dateTimeProvider,
requestedAt,
mailAddress,
""),
id,
isDemo,
group,
wheelType,
typeOfBike,
description,
currentStationId,
operatorUri,
tariffDescription)
{
LockInfo = new LockInfo.Builder { Id = lockId, Guid = lockGuid, UserKey = userKey, AdminKey = adminKey, Seed = seed }.Build();
}
/// <summary>
/// Constructs a bike info object for a booked bike.
/// </summary>
/// <param name="id">Unique id of bike.</param>
/// <param name="lockId">Id of the lock.</param>
/// <param name="lockGuid">GUID specifying the lock.</param>
/// <param name="bookedAt">Date time when bike was booked</param>
/// <param name="mailAddress">Mail address of user which booked bike.</param>
/// <param name="currentStationId">Name of station where bike is located, null if bike is on the road.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <param name="tariffDescription">Hold tariff description of bike.</param>
/// <param name="wheelType"></param>
public BikeInfo(
int id,
int lockId,
Guid lockGuid,
byte[] userKey,
byte[] adminKey,
byte[] seed,
DateTime bookedAt,
string mailAddress,
int? currentStationId,
Uri operatorUri,
TariffDescription tariffDescription = null,
bool? isDemo = DEFAULTVALUEISDEMO,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null) : base(
new StateInfo(
bookedAt,
mailAddress,
""),
id,
isDemo,
group,
wheelType,
typeOfBike,
description,
currentStationId,
operatorUri,
tariffDescription)
{
LockInfo = new LockInfo.Builder { Id = lockId, Guid = lockGuid, UserKey = userKey, AdminKey = adminKey, Seed = seed }.Build();
}
public BikeInfo(BC.BikeInfo bikeInfo, LockInfo lockInfo) : base(
bikeInfo ?? throw new ArgumentException($"Can not copy-construct {typeof(BikeInfo).Name}-object. Source bike info must not be null."))
{
LockInfo = lockInfo
?? throw new ArgumentException($"Can not copy-construct {typeof(BikeInfo).Name}-object. Source lock object must not be null.");
}
public LockInfo LockInfo { get; private set; }
}
}

View file

@ -0,0 +1,36 @@
using System;
using TINK.Model.Bikes.Bike;
using TINK.Model.Bikes.Bike.BluetoothLock;
namespace TINK.Model.Bike.BluetoothLock
{
public class BikeInfoMutable : BC.BikeInfoMutable, IBikeInfoMutable
{
/// <summary> Constructs a bike object from source. </summary>
public BikeInfoMutable(BikeInfo bike) : base(
bike.Id,
bike.IsDemo,
bike.Group,
bike.WheelType,
bike.TypeOfBike,
bike.Description,
bike.CurrentStation,
bike.OperatorUri,
bike.TariffDescription,
() => DateTime.Now,
bike.State)
{
LockInfo = new LockInfoMutable(
bike.LockInfo.Id,
bike.LockInfo.Guid,
bike.LockInfo.UserKey,
bike.LockInfo.AdminKey,
bike.LockInfo.Seed,
bike.LockInfo.State);
}
public LockInfoMutable LockInfo { get; }
ILockInfoMutable IBikeInfoMutable.LockInfo => LockInfo;
}
}

View file

@ -0,0 +1,6 @@
namespace TINK.Model.Bike.BluetoothLock
{
public interface IBikeInfo : BC.IBikeInfo
{
}
}

View file

@ -0,0 +1,7 @@
namespace TINK.Model.Bikes.Bike.BluetoothLock
{
public interface IBikeInfoMutable : BC.IBikeInfoMutable
{
ILockInfoMutable LockInfo { get; }
}
}

View file

@ -0,0 +1,24 @@
using System;
using TINK.Model.Bike.BluetoothLock;
namespace TINK.Model.Bikes.Bike.BluetoothLock
{
public interface ILockInfoMutable
{
/// <summary> Identification number of bluetooth lock.</summary>
int Id { get; }
/// <summary> Gets the user key.</summary>
byte[] UserKey { get; }
LockingState State { get; set; }
/// <summary> Holds the percentage of lock battery.</summary>
double BatteryPercentage { get; set; }
/// <summary> Changes during runtime: Can be unknown when set from copri and chang to a valid value when set from lock.</summary>
Guid Guid { get; set; }
byte[] Seed { get; }
}
}

View file

@ -0,0 +1,54 @@
using System;
using TINK.Model.Bikes.Bike.BluetoothLock;
namespace TINK.Model.Bike.BluetoothLock
{
public class LockInfoMutable : ILockInfoMutable
{
/// <summary> Lock info object. </summary>
private LockInfo LockInfo { get; set; }
/// <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>
public LockInfoMutable(
int id,
Guid guid,
byte[] userKey,
byte[] adminKey,
byte[] seed,
LockingState state)
{
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;
public LockingState State
{
get => LockInfo.State;
set => LockInfo = new LockInfo.Builder(LockInfo) { State = value }.Build();
}
/// <summary> Holds the percentage of lock battery.</summary>
public double BatteryPercentage { get; set; } = double.NaN;
/// <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();
}
}
}

View file

@ -0,0 +1,40 @@
using System;
namespace TINK.Model.Bikes.Bike
{
/// <summary>
/// Holds tariff info for a single bike.
/// </summary>
public record TariffDescription
{
/// <summary>
/// Name of the tariff.
/// </summary>
public string Name { get; init; }
/// <summary>
/// Number of the tariff.
/// </summary>
public int? Number { get; init; }
/// <summary>
/// Costs per hour in euro.
/// </summary>
public double FeeEuroPerHour { get; init; }
/// <summary>
/// Costs of the abo per month.
/// </summary>
public double AboEuroPerMonth { get; init; }
/// <summary>
/// Costs per hour in euro.
/// </summary>
public TimeSpan FreeTimePerSession { get; init; }
/// <summary>
/// Max. costs per day in euro.
/// </summary>
public double MaxFeeEuroPerDay { get; init; }
}
}

View file

@ -0,0 +1,54 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BikeInfo = TINK.Model.Bike.BC.BikeInfo;
namespace TINK.Model.Bike
{
public class BikeCollection : IBikeDictionary<BikeInfo>
{
/// <summary> Holds the bike dictionary object.</summary>
private Dictionary<int, BikeInfo> BikeDictionary { get; }
/// <summary>Constructs an empty bike info dictionary object.</summary>
public BikeCollection()
{
BikeDictionary = new Dictionary<int, BikeInfo>();
}
/// <summary> Constructs a bike collection object.</summary>
/// <param name="bikeDictionary"></param>
public BikeCollection(Dictionary<int, BikeInfo> bikeDictionary)
{
BikeDictionary = bikeDictionary ??
throw new ArgumentNullException(nameof(bikeDictionary), "Can not construct BikeCollection object.");
}
/// <summary> Gets a bike by its id.</summary>
/// <param name="p_iId">Id of the bike to get.</param>
/// <returns></returns>
public BikeInfo GetById(int p_iId)
{
return BikeDictionary.FirstOrDefault(x => x.Key == p_iId).Value;
}
/// <summary> Gets the count of bikes. </summary>
public int Count => BikeDictionary.Count;
/// <summary> Gets if a bike with given id exists.</summary>
/// <param name="p_iId">Id of bike.</param>
/// <returns>True if bike is contained, false otherwise.</returns>
public bool ContainsKey(int p_iId) => BikeDictionary.Keys.Contains(p_iId);
/// <summary> Gets the enumerator. </summary>
/// <returns>Enumerator object.</returns>
public IEnumerator<BikeInfo> GetEnumerator() => BikeDictionary.Values.GetEnumerator();
/// <summary> Gets the enumerator. </summary>
/// <returns>Enumerator object.</returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
using TINK.Model.Bike;
using BikeInfo = TINK.Model.Bike.BC.BikeInfo;
namespace TINK.Model
{
public static class BikeCollectionFilter
{
/// <summary> Filters bikes by station. </summary>
/// <param name="bikesAtAnyStation">Bikes available, requested and/ or occupied bikes to filter.</param>
/// <param name="selectedStation">Id of station, might be null</param>
/// <returns>BikeCollection holding bikes at given station or empty BikeCollection, if there are no bikes.</returns>
public static BikeCollection GetAtStation(
this BikeCollection bikesAtAnyStation,
int? selectedStation)
{
return new BikeCollection(bikesAtAnyStation?
.Where(bike => selectedStation.HasValue && bike.CurrentStation == selectedStation.Value)
.ToDictionary(x => x.Id) ?? new Dictionary<int, BikeInfo>());
}
/// <summary> Filters bikes by bike type. </summary>
/// <param name="bcAndLockItBikes">Bikes available, requested and/ or occupied bikes to filter.</param>
/// <returns>BikeCollection holding LockIt-bikes empty BikeCollection, if there are no LockIt-bikes.</returns>
public static BikeCollection GetLockIt(this BikeCollection bcAndLockItBikes)
{
return new BikeCollection(bcAndLockItBikes?
.Where(bike => bike is Bike.BluetoothLock.BikeInfo)
.ToDictionary(x => x.Id) ?? new Dictionary<int, BikeInfo>());
}
}
}

View file

@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using BikeInfo = TINK.Model.Bike.BC.BikeInfo;
using BikeInfoMutable = TINK.Model.Bike.BC.BikeInfoMutable;
namespace TINK.Model.Bike
{
/// <summary> Holds entity of bikes. </summary>
public class BikeCollectionMutable : ObservableCollection<BikeInfoMutable>, IBikeDictionaryMutable<BikeInfoMutable>
{
/// <summary> Constructs a mutable bike collection object. </summary>
public BikeCollectionMutable()
{
SelectedBike = null;
}
/// <summary>
/// Updates bikes dictionary from bikes response, i.e.
/// - removes bikes which are no more contained in bikes response
/// - updates state of all bikes from state contained in bikes response
/// </summary>
/// <param name="bikesAll"> Object holding bikes info from copri to update from.</param>
/// <param name="p_oDateTimeProvider">Provices date time information.</param>
public void Update(
IEnumerable<BikeInfo> bikesAll)
{
// Get list of current bikes by state(s) to update.
// Needed to remove bikes which switched state and have to be removed from collection.
var bikesToBeRemoved = this.Select(x => x.Id).ToList();
foreach (var bikeInfo in (bikesAll ?? new List<BikeInfo>()))
{
/// Check if bike has to be added to list of existing station.
if (ContainsKey(bikeInfo.Id) == false)
{
// Bike does not yet exist in list of bikes.
Add(BikeInfoMutableFactory.Create(bikeInfo));
continue;
}
// Update bike.
GetById(bikeInfo.Id).State.Load(bikeInfo.State);
if (bikesToBeRemoved.Contains<int>(bikeInfo.Id))
{
// Remove list from obsolete list.
bikesToBeRemoved.Remove(bikeInfo.Id);
}
}
// Remove obsolete bikes.
foreach (var l_oId in bikesToBeRemoved)
{
RemoveById(l_oId);
}
}
/// <summary>
/// Adds a new bike to collecion of bike.
/// </summary>
/// <param name="p_oNewBike">New bike to add.</param>
/// <exception cref="Exception">Thrown if bike is not unique.</exception>
public new void Add(BikeInfoMutable p_oNewBike)
{
// Ensure that bike id of new bike is is unique
foreach (BikeInfoMutable l_oBike in Items)
{
if (l_oBike.Id == p_oNewBike.Id)
{
throw new Exception(string.Format("Can not add bike with {0} to collection ob bike. Id is not unnique.", p_oNewBike));
}
}
base.Add(p_oNewBike);
}
/// <summary>
/// Bike selected by user for regerving or cancel reservation.
/// </summary>
public BikeInfoMutable SelectedBike
{
get;
private set;
}
public void SetSelectedBike(int p_intId)
{
SelectedBike = GetById(p_intId);
}
/// <summary>
/// Gets a bike by its id.
/// </summary>
/// <param name="p_iId"></param>
/// <returns></returns>
public BikeInfoMutable GetById(int p_iId)
{
{
return this.FirstOrDefault(bike => bike.Id == p_iId);
}
}
/// <summary>
/// Deteermines whether a bike by given key exists.
/// </summary>
/// <param name="p_strKey">Key to check.</param>
/// <returns>True if bike exists.</returns>
public bool ContainsKey(int p_iId)
{
return GetById(p_iId) != null;
}
/// <summary>
/// Removes a bike by its id.
/// </summary>
/// <param name="p_iId">Id of bike to be removed.</param>
public void RemoveById(int p_iId)
{
var l_oBike = GetById(p_iId);
if (l_oBike == null)
{
// Nothing to do if bike does not exists.
return;
}
Remove(l_oBike);
}
/// <summary>
/// Create mutable objects from immutable objects.
/// </summary>
private static class BikeInfoMutableFactory
{
public static BikeInfoMutable Create(BikeInfo bikeInfo)
{
return (bikeInfo is BluetoothLock.BikeInfo bluetoothLockBikeInfo)
? new BluetoothLock.BikeInfoMutable(bluetoothLockBikeInfo)
: new BikeInfoMutable(bikeInfo);
}
}
}
}

View file

@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;
namespace TINK.Model.Bike
{
public static class BikeCollectionUpdater
{
/// <summary> Updates bikes lock info with the latest lock info from bluetooth service.</summary>
/// <param name="bikes">bikes to be updated.</param>
/// <param name="locksInfo">locks info to be used for updating bikes.</param>
/// <returns></returns>
public static BikeCollection UpdateLockInfo(
this BikeCollection bikes,
IEnumerable<BluetoothLock.LockInfo> locksInfo)
{
var updatedBikesCollection = new Dictionary<int, BC.BikeInfo>();
foreach (var bikeInfo in bikes)
{
if (!(bikeInfo is BluetoothLock.BikeInfo bluetoothBikeInfo))
{
// No processing needed because bike is not a bluetooth bike
updatedBikesCollection.Add(bikeInfo.Id, bikeInfo);
continue;
}
// Check if to update current bike lock info using state from bluetooth service or not.
var currentLockInfo = locksInfo.FirstOrDefault(x => x.Id == bluetoothBikeInfo.LockInfo.Id) // Update bike info with latest info from bluethooth service if available
?? bluetoothBikeInfo.LockInfo; // Use lock info state object from copri which holds a lock id and a state of value unknown.
updatedBikesCollection.Add(bluetoothBikeInfo.Id, new BluetoothLock.BikeInfo(bluetoothBikeInfo, currentLockInfo));
}
return new BikeCollection(updatedBikesCollection);
}
}
}

View file

@ -0,0 +1,35 @@
using System.Collections.Generic;
namespace TINK.Model.Bike
{
public interface IBikeDictionary<T> : IReadOnlyCollection<T>
{
/// <summary>
/// Gets a bike by its id.
/// </summary>
/// <param name="p_iId"></param>
/// <returns></returns>
T GetById(int p_iId);
/// <summary>
/// Deteermines whether a bike by given key exists.
/// </summary>
/// <param name="p_strKey">Key to check.</param>
/// <returns>True if bike exists.</returns>
bool ContainsKey(int p_iId);
}
public interface IBikeDictionaryMutable<T> : IBikeDictionary<T>
{
/// <summary>
/// Removes a bike by its id.
/// </summary>
/// <param name="p_iId">Id of bike to be removed.</param>
void RemoveById(int p_iId);
/// <summary>
/// Adds a new element to dictinary.
/// </summary>
/// <param name="p_oNewElement">New element to add.</param>
void Add(T p_oNewElement);
}
}