sharee.bike-App/TINKLib/Model/State/StateRequestedInfo.cs
2021-05-13 20:03:07 +02:00

114 lines
3.9 KiB
C#

using Newtonsoft.Json;
using System;
using System.Runtime.Serialization;
namespace TINK.Model.State
{
/// <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
// Reference to date time provider.
private Func<DateTime> m_oDateTimeNowProvider;
/// <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.
m_oDateTimeNowProvider = () => DateTime.Now;
}
/// <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)
{
}
/// <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_oDateTimeNowProvider">
/// 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>
/// <param name="p_oRemainingTime">Time span which holds duration how long bike still will be reserved.</param>
public StateRequestedInfo(
Func<DateTime> p_oDateTimeNowProvider,
DateTime p_oFrom,
string p_strMailAddress,
string p_strCode) : base(InUseStateEnum.Reserved)
{
m_oDateTimeNowProvider = p_oDateTimeNowProvider ?? (() => DateTime.Now);
From = p_oFrom;
MailAddress = p_strMailAddress;
Code = p_strCode;
}
/// <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 = m_oDateTimeNowProvider().Subtract(From);
if (l_oTimeReserved > MaximumReserveTime)
{
// Reservation has elapsed. To not update remaining time.
p_oRemainingTime = null;
return false;
}
p_oRemainingTime = MaximumReserveTime - l_oTimeReserved;
return true;
}
/// <summary>
/// State reserved.
/// Setter exists only for serialization purposes.
/// </summary>
public override InUseStateEnum Value
{
get
{
return InUseStateEnum.Reserved;
}
}
/// <summary>
/// Date time when bike was reserved.
/// </summary>
[DataMember]
public DateTime From { get; }
/// <summary>
/// Mail address of user who reserved the bike.
/// </summary>
[DataMember]
public string MailAddress { get; }
/// <summary>
/// Booking code.
/// </summary>
[DataMember]
public string Code { get; }
}
}