2022-08-30 15:42:25 +02:00
|
|
|
|
using System;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
using System.Runtime.Serialization;
|
2022-08-30 15:42:25 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
|
|
|
|
namespace TINK.Model.State
|
|
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manages state reserved.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataContract]
|
|
|
|
|
public sealed class StateRequestedInfo : BaseState, IBaseState, INotAvailableState
|
|
|
|
|
{
|
|
|
|
|
// Maximum time while reserving request is kept.
|
|
|
|
|
public static readonly TimeSpan MaximumReserveTime = new TimeSpan(0, 15, 0); // 15 mins
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
// Reference to date time provider.
|
|
|
|
|
private Func<DateTime> _DateTimeNowProvider;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prevents an invalid instance to be created.
|
|
|
|
|
/// Used by serializer only.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private StateRequestedInfo() : base(InUseStateEnum.Reserved)
|
|
|
|
|
{
|
|
|
|
|
// Is called in context of JSON deserialization.
|
|
|
|
|
_DateTimeNowProvider = () => DateTime.Now;
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reservation performed with other device/ before start of app.
|
|
|
|
|
/// Date time info when bike was reserved has been received from webserver.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p_oRemainingTime">Time span which holds duration how long bike still will be reserved.</param>
|
|
|
|
|
[JsonConstructor]
|
|
|
|
|
private StateRequestedInfo(
|
|
|
|
|
InUseStateEnum Value,
|
|
|
|
|
DateTime From,
|
|
|
|
|
string MailAddress,
|
|
|
|
|
string Code) : this(() => DateTime.Now, From, MailAddress, Code)
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reservation performed with other device/ before start of app.
|
|
|
|
|
/// Date time info when bike was reserved has been received from webserver.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dateTimeNowProvider">
|
|
|
|
|
/// Used to to provide current date time information for potential calls of <seealso cref="GetIsStillReserved"/>.
|
|
|
|
|
/// Not used to calculate remaining time because this duration whould always be shorter as the one received from webserver.
|
|
|
|
|
/// </param>
|
|
|
|
|
public StateRequestedInfo(
|
|
|
|
|
Func<DateTime> dateTimeNowProvider,
|
|
|
|
|
DateTime from,
|
|
|
|
|
string mailAddress,
|
|
|
|
|
string code) : base(InUseStateEnum.Reserved)
|
|
|
|
|
{
|
|
|
|
|
_DateTimeNowProvider = dateTimeNowProvider ?? (() => DateTime.Now);
|
|
|
|
|
From = from;
|
|
|
|
|
MailAddress = mailAddress;
|
|
|
|
|
Code = code;
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries update
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if reservation span has not exeeded and state remains reserved, false otherwise.</returns>
|
|
|
|
|
/// <todo>Implement logging of time stamps.</todo>
|
|
|
|
|
public bool GetIsStillReserved(out TimeSpan? p_oRemainingTime)
|
|
|
|
|
{
|
|
|
|
|
var l_oTimeReserved = _DateTimeNowProvider().Subtract(From);
|
|
|
|
|
if (l_oTimeReserved > MaximumReserveTime)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
{
|
|
|
|
|
// Reservation has elapsed. To not update remaining time.
|
|
|
|
|
p_oRemainingTime = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
p_oRemainingTime = MaximumReserveTime - l_oTimeReserved;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// State reserved.
|
|
|
|
|
/// Setter exists only for serialization purposes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override InUseStateEnum Value
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return InUseStateEnum.Reserved;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Date time when bike was reserved.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataMember]
|
|
|
|
|
public DateTime From { get; }
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Mail address of user who reserved the bike.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataMember]
|
|
|
|
|
public string MailAddress { get; }
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Booking code.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataMember]
|
|
|
|
|
public string Code { get; }
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|