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,20 @@
using System.Runtime.Serialization;
namespace TINK.Model.State
{
/// <summary>
/// Base type for serialization purposes.
/// </summary>
[DataContract]
[KnownType(typeof(StateAvailableInfo))]
[KnownType(typeof(StateRequestedInfo))]
[KnownType(typeof(StateOccupiedInfo))]
public abstract class BaseState
{
/// <summary> Constructor for Json serialization. </summary>
/// <param name="p_eValue">State value.</param>
protected BaseState(InUseStateEnum p_eValue) {}
public abstract InUseStateEnum Value { get; }
}
}

View file

@ -0,0 +1,11 @@

namespace TINK.Model.State
{
/// <summary>
/// Base state information.
/// </summary>
public interface IBaseState
{
InUseStateEnum Value { get; }
}
}

View file

@ -0,0 +1,14 @@
using System;
namespace TINK.Model.State
{
/// <summary>
/// State of bikes which are either reserved or booked.
/// </summary>
public interface INotAvailableState : IBaseState
{
DateTime From { get; }
string MailAddress { get; }
string Code { get; }
}
}

View file

@ -0,0 +1,16 @@
using System;
namespace TINK.Model.State
{
/// <summary>
/// Interface to access informations about bike information.
/// </summary>
public interface IStateInfo : IBaseState
{
string MailAddress { get; }
DateTime? From { get; }
string Code { get; }
}
}

View file

@ -0,0 +1,23 @@
using System;
namespace TINK.Model.State
{
public interface IStateInfoMutable
{
InUseStateEnum Value { get; }
/// <summary> Updates state from webserver. </summary>
/// <param name="p_oState">State of the bike.</param>
/// <param name="p_oFrom">Date time when bike was reserved/ booked.</param>
/// <param name="p_oDuration">Lenght of time span for which bike remains booked.</param>
/// <param name="p_strMailAddress">Mailaddress of the one which reserved/ booked.</param>
/// <param name="p_strCode">Booking code if bike is booked or reserved.</param>
/// <param name="notifyLevel">Controls whether notify property changed events are fired or not.</param>
void Load(
InUseStateEnum p_oState,
DateTime? p_oFrom = null,
string p_strMailAddress = null,
string p_strCode = null,
Bikes.Bike.BC.NotifyPropertyChangedLevel notifyLevel = Bikes.Bike.BC.NotifyPropertyChangedLevel.All);
}
}

View file

@ -0,0 +1,39 @@
using Newtonsoft.Json;
using System;
using System.Runtime.Serialization;
namespace TINK.Model.State
{
/// <summary>
/// Represents the state available.
/// </summary>
[DataContract]
public sealed class StateAvailableInfo : BaseState, IBaseState
{
/// <summary>
/// Constructs state info object representing state available.
/// </summary>
public StateAvailableInfo() : base(InUseStateEnum.Disposable)
{
}
/// <summary> Constructor for Json serialization. </summary>
/// <param name="p_eValue">Unused value.</param>
[JsonConstructor]
private StateAvailableInfo (InUseStateEnum p_eValue) : base(InUseStateEnum.Disposable)
{
}
/// <summary>
/// Gets the info that state is disposable.
/// Setter exists only for serialization purposes.
/// </summary>
public override InUseStateEnum Value
{
get
{
return InUseStateEnum.Disposable;
}
}
}
}

View file

@ -0,0 +1,161 @@
using System;
namespace TINK.Model.State
{
/// <summary>
/// Types of rent states
/// </summary>
public enum InUseStateEnum
{
/// <summary>
/// Bike is not in use. Corresponding COPRI state is "available".
/// </summary>
Disposable,
/// <summary>
/// Bike is reserved. Corresponding COPRI state is "requested".
/// </summary>
Reserved,
/// <summary>
/// Bike is booked. Corresponding COPRI statie is "occupied".
/// </summary>
Booked
}
/// <summary>
/// Manages the state of a bike.
/// </summary>
public class StateInfo : IStateInfo
{
// Holds the current disposable state value
private readonly BaseState m_oInUseState;
/// <summary>
/// Constructs a state info object when state is available.
/// </summary>
/// <param name="p_oDateTimeNowProvider">Provider for current date time to calculate remainig time on demand for state of type reserved.</param>
public StateInfo()
{
m_oInUseState = new StateAvailableInfo();
}
/// <summary>
/// Constructs a state info object when state is requested.
/// </summary>
/// <param name="p_oRequestedAt">Date time when bike was requested</param>
/// <param name="p_strMailAddress">Mail address of user which requested bike.</param>
/// <param name="p_strCode">Booking code.</param>
/// <param name="p_oDateTimeNowProvider">Date time provider to calculate reaining time.</param>
public StateInfo(
Func<DateTime> p_oDateTimeNowProvider,
DateTime p_oRequestedAt,
string p_strMailAddress,
string p_strCode)
{
// Todo: Handle p_oFrom == null here.
// Todo: Handle p_oDuration == null here.
m_oInUseState = new StateRequestedInfo(
p_oDateTimeNowProvider ?? (() => DateTime.Now),
p_oRequestedAt,
p_strMailAddress,
p_strCode);
}
/// <summary>
/// Constructs a state info object when state is booked.
/// </summary>
/// <param name="p_oBookedAt">Date time when bike was booked</param>
/// <param name="p_strMailAddress">Mail address of user which booked bike.</param>
/// <param name="p_strCode">Booking code.</param>
public StateInfo(
DateTime p_oBookedAt,
string p_strMailAddress,
string p_strCode)
{
// Todo: Handle p_oFrom == null here.
// Todo: Clearify question: What to do if code changes form one value to another? This should never happen.
// Todo: Clearify question: What to do if from time changes form one value to another? This should never happen.
m_oInUseState = new StateOccupiedInfo(
p_oBookedAt,
p_strMailAddress,
p_strCode);
}
/// <summary>
/// Gets the state value of object.
/// </summary>
public InUseStateEnum Value
{
get { return m_oInUseState.Value; }
}
/// <summary>
/// Member for serialization purposes.
/// </summary>
internal BaseState StateInfoObject
{
get { return m_oInUseState; }
}
/// Transforms object to string.
/// </summary>
/// <returns></returns>
public new string ToString()
{
return m_oInUseState.Value.ToString("g");
}
/// <summary>
/// Date of request/ bookeing action.
/// </summary>
public DateTime? From
{
get
{
var l_oNotDisposableInfo = m_oInUseState as INotAvailableState;
return l_oNotDisposableInfo != null ? l_oNotDisposableInfo.From : (DateTime?)null;
}
}
/// <summary>
/// Mail address.
/// </summary>
public string MailAddress
{
get
{
var l_oNotDisposableInfo = m_oInUseState as INotAvailableState;
return l_oNotDisposableInfo?.MailAddress;
}
}
/// <summary>
/// Reservation code.
/// </summary>
public string Code
{
get
{
var l_oNotDisposableInfo = m_oInUseState as INotAvailableState;
return l_oNotDisposableInfo?.Code;
}
}
/// <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_oReservedInfo = m_oInUseState as StateRequestedInfo;
if (l_oReservedInfo == null)
{
p_oRemainingTime = null;
return false;
}
return l_oReservedInfo.GetIsStillReserved(out p_oRemainingTime);
}
}
}

View file

@ -0,0 +1,299 @@
using System;
namespace TINK.Model.State
{
using System.ComponentModel;
using System.Runtime.Serialization;
/// <summary>
/// Manges the state of a bike.
/// </summary>
[DataContract]
public class StateInfoMutable : INotifyPropertyChanged, IStateInfoMutable
{
/// <summary>
/// Provider for current date time to calculate remainig time on demand for state of type reserved.
/// </summary>
private readonly Func<DateTime> m_oDateTimeNowProvider;
// Holds the current disposable state value
private StateInfo m_oStateInfo;
/// <summary>
/// Backs up remaining time of child object.
/// </summary>
private TimeSpan? m_oRemainingTime = null;
/// <summary> Notifies clients about state changes. </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Constructs a state object from source.
/// </summary>
/// <param name="p_oState">State info to load from.</param>
public StateInfoMutable(
Func<DateTime> p_oDateTimeNowProvider = null,
IStateInfo p_oState = null)
{
// Back up date time provider to be able to pass this to requested- object if state changes to requested.
m_oDateTimeNowProvider = p_oDateTimeNowProvider != null
? p_oDateTimeNowProvider
: () => DateTime.Now;
m_oStateInfo = Create(p_oState, p_oDateTimeNowProvider);
}
/// <summary>
/// Loads state from immutable source.
/// </summary>
/// <param name="p_oState">State to load from.</param>
public void Load(IStateInfo p_oState)
{
if (p_oState == null)
{
throw new ArgumentException("Can not load state info, object must not be null.");
}
// Back up last state value and remaining time value
// to be able to check whether an event has to be fired or not.
var l_oLastState = Value;
var l_oLastRemainingTime = m_oRemainingTime;
// Create new state info object from source.
m_oStateInfo = Create(p_oState, m_oDateTimeNowProvider);
// Update remaining time value.
m_oStateInfo.GetIsStillReserved(out m_oRemainingTime);
if (l_oLastState == m_oStateInfo.Value
&& l_oLastRemainingTime == m_oRemainingTime)
{
return;
}
// State has changed, notify clients.
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
}
/// <summary>
/// Creates a state info object.
/// </summary>
/// <param name="p_oState">State to load from.</param>
private static StateInfo Create(
IStateInfo p_oState,
Func<DateTime> p_oDateTimeNowProvider)
{
switch (p_oState != null ? p_oState.Value : InUseStateEnum.Disposable)
{
case InUseStateEnum.Disposable:
return new StateInfo();
case InUseStateEnum.Reserved:
// Todo: Handle p_oFrom == null here.
// Todo: Handle p_oDuration == null here.
return new StateInfo(
p_oDateTimeNowProvider,
p_oState.From.Value,
p_oState.MailAddress,
p_oState.Code);
case InUseStateEnum.Booked:
// Todo: Handle p_oFrom == null here.
// Todo: Clearify question: What to do if code changes form one value to another? This should never happen.
// Todo: Clearify question: What to do if from time changes form one value to another? This should never happen.
return new StateInfo(
p_oState.From.Value,
p_oState.MailAddress,
p_oState.Code);
default:
// Todo: New state Busy has to be defined.
throw new Exception(string.Format("Can not create new state info object. Unknown state {0} detected.", p_oState.Value));
}
}
/// <summary>
/// Gets the state value of object.
/// </summary>
public InUseStateEnum Value
{
get { return m_oStateInfo.Value; }
}
/// <summary>
/// Member for serialization purposes.
/// </summary>
[DataMember]
private BaseState StateInfoObject
{
get { return m_oStateInfo.StateInfoObject; }
set
{
var l_oStateOccupied = value as StateOccupiedInfo;
if (l_oStateOccupied != null)
{
m_oStateInfo = new StateInfo(l_oStateOccupied.From, l_oStateOccupied.MailAddress, l_oStateOccupied.Code);
return;
}
var l_oStateRequested = value as StateRequestedInfo;
if (l_oStateRequested != null)
{
m_oStateInfo = new StateInfo(m_oDateTimeNowProvider, l_oStateRequested.From, l_oStateRequested.MailAddress, l_oStateRequested.Code);
return;
}
m_oStateInfo = new StateInfo();
}
}
/// Transforms object to string.
/// </summary>
/// <returns></returns>
public new string ToString()
{
return m_oStateInfo.ToString();
}
/// <summary>
/// Checks and updates state if required.
/// </summary>
/// <returns>Value indicating wheter state has changed</returns>
public void UpdateOnTimeElapsed()
{
switch (m_oStateInfo.Value )
{
// State is disposable or booked. No need to update "OnTimeElapsed"
case InUseStateEnum.Disposable:
case InUseStateEnum.Booked:
return;
}
// Check if maximum reserved time has elapsed.
if (!m_oStateInfo.GetIsStillReserved(out m_oRemainingTime))
{
// Time has elapsed, switch state to disposable and notfiy client
m_oStateInfo = new StateInfo();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
return;
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(RemainingTime)));
}
/// <summary> Updates state from webserver. </summary>
/// <param name="p_oState">State of the bike.</param>
/// <param name="p_oFrom">Date time when bike was reserved/ booked.</param>
/// <param name="p_oDuration">Lenght of time span for which bike remains booked.</param>
/// <param name="p_strMailAddress">Mailaddress of the one which reserved/ booked.</param>
/// <param name="p_strCode">Booking code if bike is booked or reserved.</param>
/// <param name="supressNotifyPropertyChanged">Controls whether notify property changed events are fired or not.</param>
public void Load(
InUseStateEnum p_oState,
DateTime? p_oFrom = null,
string p_strMailAddress = null,
string p_strCode = null,
Bikes.Bike.BC.NotifyPropertyChangedLevel notifyLevel = Bikes.Bike.BC.NotifyPropertyChangedLevel.All)
{
var l_oLastState = m_oStateInfo.Value;
switch (p_oState)
{
case InUseStateEnum.Disposable:
m_oStateInfo = new StateInfo();
// Set value to null. Otherwise potentially obsolete value will be taken remaining time.
m_oRemainingTime = null;
break;
case InUseStateEnum.Reserved:
// Todo: Handle p_oFrom == null here.
// Todo: Handle p_oDuration == null here.
m_oStateInfo = new StateInfo(
m_oDateTimeNowProvider,
p_oFrom.Value,
p_strMailAddress,
p_strCode);
// Set value to null. Otherwise potentially obsolete value will be taken remaining time.
m_oRemainingTime = null;
break;
case InUseStateEnum.Booked:
// Todo: Handle p_oFrom == null here.
// Todo: Clearify question: What to do if code changes form one value to another? This should never happen.
// Todo: Clearify question: What to do if from time changes form one value to another? This should never happen.
m_oStateInfo = new StateInfo(
p_oFrom.Value,
p_strMailAddress,
p_strCode);
// Set value to null. Otherwise potentially obsolete value will be taken remaining time.
m_oRemainingTime = null;
break;
default:
// Todo: New state Busy has to be defined.
break;
}
if (l_oLastState != m_oStateInfo.Value
&& notifyLevel == Bikes.Bike.BC.NotifyPropertyChangedLevel.All)
{
// State has changed, notify clients.
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
}
}
/// <summary>
/// If bike is reserved time raimaining while bike stays reserved, null otherwise.
/// </summary>
public TimeSpan? RemainingTime
{
get
{
switch (m_oStateInfo.Value)
{
// State is either available or occupied.
case InUseStateEnum.Disposable:
case InUseStateEnum.Booked:
return null;
}
if (m_oRemainingTime.HasValue == false)
{
// Value was not yet querried.
// Do querry before returning object.
m_oStateInfo.GetIsStillReserved(out m_oRemainingTime);
}
return m_oRemainingTime;
}
}
public DateTime? From
{
get
{
return m_oStateInfo.From;
}
}
public string MailAddress
{
get
{
return m_oStateInfo.MailAddress;
}
}
public string Code
{
get
{
return m_oStateInfo.Code;
}
}
}
}

View file

@ -0,0 +1,80 @@
using Newtonsoft.Json;
using System;
using System.Runtime.Serialization;
namespace TINK.Model.State
{
/// <summary>
/// Manages state booked.
/// </summary>
[DataContract]
public sealed class StateOccupiedInfo : BaseState, IBaseState, INotAvailableState
{
/// <summary>
/// Prevents an invalid instance to be created.
/// </summary>
private StateOccupiedInfo() : base(InUseStateEnum.Booked)
{
}
/// <summary>
/// Constructs an object holding booked state info.
/// </summary>
/// <param name="p_oFrom">Date time when bike was booked</param>
/// <param name="p_strMailAddress"></param>
/// <param name="p_strCode"></param>
public StateOccupiedInfo(
DateTime p_oFrom,
string p_strMailAddress,
string p_strCode) : base(InUseStateEnum.Booked)
{
From = p_oFrom;
MailAddress = p_strMailAddress;
Code = p_strCode;
}
/// <summary> Constructor for Json serialization. </summary>
/// <param name="Value">Unused value.</param>
/// <param name="From">Date time when bike was booked</param>
/// <param name="MailAddress"></param>
/// <param name="Code"></param>
[JsonConstructor]
private StateOccupiedInfo(
InUseStateEnum Value,
DateTime From,
string MailAddress,
string Code) : this(From, MailAddress, Code)
{
}
/// <summary>
/// Gets the info that state is reserved.
/// Setter exists only for serialization purposes.
/// </summary>
public override InUseStateEnum Value
{
get
{
return InUseStateEnum.Booked;
}
}
/// <summary>
/// Prevents an invalid instance to be created.
/// </summary>
[DataMember]
public DateTime From { get; }
/// <summary>
/// Mail address of user who bookec the bike.
/// </summary>
[DataMember]
public string MailAddress { get; }
/// <summary>
/// Booking code.
/// </summary>
[DataMember]
public string Code { get; }
}
}

View file

@ -0,0 +1,114 @@
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; }
}
}