Version 3.0.381

This commit is contained in:
Anja 2024-04-09 12:53:23 +02:00
parent f963c0a219
commit 3a363acf3a
1525 changed files with 60589 additions and 125098 deletions

View file

@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Serilog;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.Device;
using ShareeBike.Model.User.Account;
using ShareeBike.Repository;
using ShareeBike.Repository.Request;
using ShareeBike.Repository.Response;
namespace ShareeBike.Model.Connector
{
public class Command : Base, ICommand
{
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => CopriServer.IsConnected;
/// <summary> No user is logged in.</summary>
public string SessionCookie => null;
/// <summary> Is raised whenever login state has changed.</summary>
public event LoginStateChangedEventHandler LoginStateChanged;
/// <summary>Constructs a copri query object.</summary>
/// <param name="p_oCopriServer">Server which implements communication.</param>
public Command(
ICopriServerBase p_oCopriServer) : base(p_oCopriServer)
{
}
/// <summary>
/// Logs user in.
/// If log in succeeds either and session might be updated if it was no more valid (logged in by an different device).
/// If log in fails (password modified) session cookie is set to empty.
/// If communication fails an exception is thrown.
/// </summary>
public async Task<IAccount> DoLogin(
string mail,
string password,
string deviceId)
{
if (string.IsNullOrEmpty(mail))
{
throw new ArgumentNullException("Can not login user. Mail address must not be null or empty.");
}
if (string.IsNullOrEmpty(password))
{
throw new ArgumentNullException("Can not login user. Password must not be null or empty.");
}
if (string.IsNullOrEmpty(deviceId))
{
throw new ArgumentNullException("Can not login user. Device not be null or empty.");
}
AuthorizationResponse response;
try
{
response = (await CopriServer.DoAuthorizationAsync(mail, password, deviceId)).GetIsResponseOk(mail);
}
catch (Exception)
{
throw;
}
var l_oAccount = response.GetAccount(MerchantId, mail, password);
// Log in state changes. Notify parent object to update.
LoginStateChanged?.Invoke(this, new LoginStateChangedEventArgs(l_oAccount.SessionCookie, l_oAccount.Mail));
return l_oAccount;
}
/// <summary> Logs user out. </summary>
public async Task DoLogout()
{
Log.ForContext<Command>().Error("Unexpected log out request detected. No user logged in.");
await Task.CompletedTask;
}
/// <summary>
/// Request to reserve a bike.
/// </summary>
/// <param name="bike">Bike to book.</param>
public async Task DoReserve(
Bikes.BikeInfoNS.BC.IBikeInfoMutable bike)
{
Log.ForContext<Command>().Error("Unexpected booking request detected. No user logged in.");
await Task.CompletedTask;
}
/// <summary> Request to cancel a reservation.</summary>
/// <param name="p_oBike">Bike to book.</param>
public async Task DoCancelReservation(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike)
{
Log.ForContext<Command>().Error("Unexpected cancel reservation request detected. No user logged in.");
await Task.CompletedTask;
}
/// <summary> Get authentication keys.</summary>
/// <param name="bike">Bike to book.</param>
public async Task CalculateAuthKeys(Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable bike)
{
Log.ForContext<Command>().Error("Unexpected request to get authentication keys detected. No user logged in.");
await Task.CompletedTask;
}
/// <summary> Updates COPRI lock state for a booked bike. </summary>
/// <param name="bike">Bike to update locking state for.</param>
/// <param name="location">Location where lock was opened/ changed.</param>
/// <returns>Response on updating locking state.</returns>
public async Task StartReturningBike(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike)
{
Log.ForContext<Command>().Error("Unexpected request to notify about start of returning bike. No user logged in.");
await Task.CompletedTask;
}
/// <summary> Notifies COPRI about start of returning sequence. </summary>
/// <remarks> Operator specific call.</remarks>
/// <param name="bike">Bike to return.</param>
/// <returns>Response on notification about start of returning sequence.</returns>
public async Task UpdateLockingStateAsync(Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable bike, LocationDto location)
{
Log.ForContext<Command>().Error("Unexpected request to update locking state detected. No user logged in.");
await Task.CompletedTask;
}
public async Task DoBookAsync(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike, LockingAction? nextAction = null)
{
Log.ForContext<Command>().Error("Unexpected booking request detected. No user logged in.");
await Task.CompletedTask;
}
public async Task BookAndOpenAync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike)
{
Log.ForContext<Command>().Error("Unexpected request to book and open bike detected. No user logged in.");
await Task.CompletedTask;
}
public async Task<BookingFinishedModel> DoReturn(
Bikes.BikeInfoNS.BC.IBikeInfoMutable bike,
LocationDto location,
ISmartDevice smartDevice)
{
Log.ForContext<Command>().Error("Unexpected returning request detected. No user logged in.");
return await Task.FromResult(new BookingFinishedModel());
}
public async Task<BookingFinishedModel> ReturnAndCloseAsync(
Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike,
ISmartDevice smartDevice)
{
Log.ForContext<Command>().Error("Unexpected close lock and return request detected. No user logged in.");
return await Task.FromResult(new BookingFinishedModel());
}
/// <summary>
/// Submits feedback to copri server.
/// </summary>
/// <param name="userFeedback">Feedback to submit.</param>
#if USCSHARP9
public async Task DoSubmitFeedback(ICommand.IUserFeedback userFeedback, Uri opertorUri)
#else
public async Task DoSubmitFeedback(IUserFeedback userFeedback, Uri opertorUri)
#endif
{
Log.ForContext<Command>().Error("Unexpected submit feedback request detected. No user logged in.");
await Task.CompletedTask;
}
/// <summary> Submits mini survey to copri server. </summary>
/// <param name="answers">Collection of answers.</param>
public async Task DoSubmitMiniSurvey(IDictionary<string, string> answers)
{
Log.ForContext<Command>().Error("Unexpected submit mini survey request detected. No user logged in.");
await Task.CompletedTask;
}
public Task OpenLockAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike)
=> throw new NotImplementedException();
public Task CloseLockAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike)
=> throw new NotImplementedException();
}
}

View file

@ -0,0 +1,321 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.Device;
using ShareeBike.Model.User.Account;
using ShareeBike.Repository;
using ShareeBike.Repository.Exception;
using ShareeBike.Repository.Request;
using ShareeBike.Repository.Response;
using ShareeBike.Services.CopriApi;
namespace ShareeBike.Model.Connector
{
public class CommandLoggedIn : BaseLoggedIn, ICommand
{
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => CopriServer.IsConnected;
/// <summary> Is raised whenever login state has changed.</summary>
public event LoginStateChangedEventHandler LoginStateChanged;
/// <summary>Constructs a copri query object.</summary>
/// <param name="p_oCopriServer">Server which implements communication.</param>
public CommandLoggedIn(ICopriServerBase p_oCopriServer,
string sessionCookie,
string mail,
Func<DateTime> dateTimeProvider) : base(p_oCopriServer, sessionCookie, mail, dateTimeProvider)
{
}
/// <summary>
/// Logs user in.
/// If log in succeeds either and session might be updated if it was no more valid (logged in by an different device).
/// If log in fails (password modified) session cookie is set to empty.
/// If communication fails an ShareeBike.Repository.Exception is thrown.
/// </summary>
/// <param name="p_oAccount">Account to use for login.</param>
public Task<IAccount> DoLogin(string mail, string password, string deviceId)
{
if (string.IsNullOrEmpty(mail))
{
throw new ArgumentNullException("Can not login user. Mail address must not be null or empty.");
}
throw new Exception($"Fehler beim Anmelden von unter {mail}. Benutzer {Mail} ist bereits angemeldet.");
}
/// <summary> Logs user out. </summary>
public async Task DoLogout()
{
AuthorizationoutResponse l_oResponse = null;
try
{
l_oResponse = (await CopriServer.DoAuthoutAsync()).GetIsResponseOk();
}
catch (AuthcookieNotDefinedException)
{
// Cookie is no more defined, i.e. no need to logout user at copri because user is already logged out.
// Just ignore this error.
// User logged out, log in state changed. Notify parent object to update.
LoginStateChanged?.Invoke(this, new LoginStateChangedEventArgs());
return;
}
catch (Exception)
{
throw;
}
// User logged out, log in state changed. Notify parent object to update.
LoginStateChanged?.Invoke(this, new LoginStateChangedEventArgs());
}
/// <summary>
/// Request to reserve a bike.
/// </summary>
/// <param name="bike">Bike to book.</param>
public async Task DoReserve(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike)
{
if (bike == null)
{
throw new ArgumentNullException("Can not reserve bike. No bike object available.");
}
BikeInfoReservedOrBooked response;
try
{
response = (await CopriServer.DoReserveAsync(bike.Id, bike.OperatorUri)).GetIsReserveResponseOk(bike.Id);
}
catch (Exception)
{
// Exception was not expected or too many subsequent exceptions detected.
throw;
}
bike.Load(response, Mail, Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.None);
}
/// <summary> Request to cancel a reservation.</summary>
/// <param name="bike">Bike to cancel reservation.</param>
public async Task DoCancelReservation(
Bikes.BikeInfoNS.BC.IBikeInfoMutable bike)
{
if (bike == null)
{
throw new ArgumentNullException("Can not cancel reservation of bike. No bike object available.");
}
BookingActionResponse response;
try
{
response = (await CopriServer
.DoCancelReservationAsync(bike.Id, bike.OperatorUri))
.GetIsResponseOk(string.Format(MultilingualResources.AppResources.ErrorCancelReservationFailed, bike.Id));
}
catch (Exception)
{
// Exception was not expected or too many subsequent exceptions detected.
throw;
}
bike.Load(Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.None);
}
/// <summary> Get authentication keys.</summary>
/// <param name="bike">Bike to get new keys for.</param>
public async Task CalculateAuthKeys(Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable bike)
{
if (bike == null)
{
throw new ArgumentNullException("Can not calculate auth keys. No bike object available.");
}
switch (bike.State.Value)
{
case State.InUseStateEnum.Reserved:
case State.InUseStateEnum.Booked:
break;
default:
throw new ArgumentNullException($"Can not calculate auth keys. Unexpected bike state {bike.State.Value} detected.");
}
BikeInfoReservedOrBooked response;
Guid guid = (bike is BikeInfoMutable btBike) ? btBike.LockInfo.Guid : new Guid();
try
{
response = (await CopriServer.CalculateAuthKeysAsync(bike.Id, bike.OperatorUri)).GetIsBookingResponseOk(bike.Id);
}
catch (Exception)
{
// Exception was not expected or too many subsequent exceptions detected.
throw;
}
UpdaterJSON.Load(
bike,
response,
Mail,
Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.None);
}
/// <summary> Notifies COPRI about start of returning sequence. </summary>
/// <remarks> Operator specific call.</remarks>
/// <param name="bike">Bike to return.</param>
/// <returns>Response on notification about start of returning sequence.</returns>
public async Task StartReturningBike(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike)
{
if (bike == null)
{
throw new ArgumentNullException("Can not notify about start returning bike. No bike object available.");
}
try
{
(await CopriServer.StartReturningBike(
bike.Id,
bike.OperatorUri)).GetIsResponseOk("Start returning bike");
}
catch (Exception)
{
// Exception was not expected or too many subsequent exceptions detected.
throw;
}
}
/// <summary> Updates COPRI lock state for a booked or reserved bike. </summary>
/// <param name="bike">Bike to update locking state for.</param>
/// <param name="location">Location of the bike.</param>
/// <returns>Response on updating locking state.</returns>
public async Task UpdateLockingStateAsync(
IBikeInfoMutable bike,
LocationDto location)
{
if (bike == null)
{
throw new ArgumentNullException("Can not update locking state of bike. No bike object available.");
}
if (bike.State.Value != State.InUseStateEnum.Booked && bike.State.Value != State.InUseStateEnum.Reserved)
{
throw new ArgumentNullException($"Can not update locking state of bike. Unexpected booking state {bike.State} detected.");
}
lock_state? state = RequestBuilderHelper.GetLockState(bike.LockInfo.State);
if (!state.HasValue)
{
throw new ArgumentNullException($"Can not update locking state of bike. Unexpected locking state {bike.LockInfo.State} detected.");
}
try
{
(await CopriServer.UpdateLockingStateAsync(
bike.Id,
state.Value,
bike.OperatorUri,
location,
bike.LockInfo.BatteryPercentage,
bike.LockInfo.VersionInfo)).GetIsBookingResponseOk(bike.Id);
}
catch (Exception)
{
// Exception was not expected or too many subsequent exceptions detected.
throw;
}
}
/// <summary> Request to book a bike. </summary>
/// <param name="bike">Bike to book.</param>
/// <param name="nextAction">If not null next locking action which is performed after booking.</param>
public async Task DoBookAsync(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike, LockingAction? nextAction = null)
{
if (bike == null)
{
throw new ArgumentNullException(nameof(bike), "Can not book bike. No bike object available.");
}
BikeInfoReservedOrBooked response;
var btBike = bike as BikeInfoMutable;
Guid guid = btBike != null ? btBike.LockInfo.Guid : new Guid();
double batteryPercentage = btBike != null ? btBike.LockInfo.BatteryPercentage : double.NaN;
response = (await CopriServer.DoBookAsync(
bike.OperatorUri,
bike.Id,
guid,
batteryPercentage,
nextAction)).GetIsBookingResponseOk(bike.Id);
bike.Load(
response,
Mail,
Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.None);
}
/// <summary>
/// Books a bike and opens the lock.
/// </summary>
/// <param name="bike">Bike to book and open.</param>
public async Task BookAndOpenAync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike)
=> await Polling.BookAndOpenAync(CopriServer, bike, Mail);
/// <summary> Request to return a bike.</summary>
/// <param name="bike">Bike to return.</param>
/// <param name="locaton">Position of the bike for bluetooth locks.</param>
/// <param name="smartDevice">Provides info about hard and software.</param>
public async Task<BookingFinishedModel> DoReturn(
Bikes.BikeInfoNS.BC.IBikeInfoMutable bike,
LocationDto location = null,
ISmartDevice smartDevice = null)
{
if (bike == null)
{
throw new ArgumentNullException("Can not return bike. No bike object available.");
}
DoReturnResponse response
= (await CopriServer.DoReturn(bike.Id, location, bike.OperatorUri)).GetIsReturnBikeResponseOk(bike.Id);
bike.Load(
Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.None,
response.bike_returned.station ?? string.Empty);
return response?.Create() ?? new BookingFinishedModel();
}
/// <summary> Request to return bike and close the lock.</summary>
/// <param name="bike">Bike to return.</param>
/// <param name="smartDevice">Provides info about hard and software.</param>
public async Task<BookingFinishedModel> ReturnAndCloseAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike, ISmartDevice smartDevice = null)
=> await Polling.ReturnAndCloseAync(CopriServer, smartDevice, bike);
/// <summary>
/// Submits feedback to copri server.
/// </summary>
/// <param name="userFeedback">Feedback to submit.</param>
#if USCSHARP9
public async Task DoSubmitFeedback(ICommand.IUserFeedback userFeedback, Uri opertorUri)
=> await CopriServer.DoSubmitFeedback(userFeedback.BikeId, userFeedback.Message, userFeedback.IsBikeBroken, opertorUri);
#else
/// <summary> Submits feedback for a renting operation.</summary>
public async Task DoSubmitFeedback(
IUserFeedback userFeedback,
Uri opertorUri)
=> await CopriServer.DoSubmitFeedback(userFeedback.BikeId, userFeedback.CurrentChargeBars, userFeedback.Message, userFeedback.IsBikeBroken, opertorUri);
#endif
/// <summary> Submits mini survey to copri server. </summary>
/// <param name="answers">Collection of answers.</param>
public async Task DoSubmitMiniSurvey(IDictionary<string, string> answers)
=> await CopriServer.DoSubmitMiniSurvey(answers);
public async Task OpenLockAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike)
=> await CopriServer.OpenAync(bike);
public async Task CloseLockAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike)
=> await CopriServer.CloseAync(bike);
}
}

View file

@ -0,0 +1,177 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ShareeBike.Model.Device;
using ShareeBike.Model.User.Account;
using ShareeBike.Repository.Request;
namespace ShareeBike.Model.Connector
{
public interface ICommand
{
/// <summary> Is raised whenever login state has changed.</summary>
event LoginStateChangedEventHandler LoginStateChanged;
/// <summary>
/// Logs user in.
/// If log in succeeds either and session might be updated if it was no more valid (logged in by an different device).
/// If log in fails (password modified) session cookie is set to empty.
/// If communication fails an exception is thrown.
/// </summary>
Task<IAccount> DoLogin(string p_strMail, string p_strPassword, string p_strDeviceId);
/// <summary> Logs user out. </summary>
Task DoLogout();
/// <summary> Request to reserve a bike.</summary>
/// <param name="bike">Bike to book.</param>
Task DoReserve(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike);
/// <summary> Request to cancel a reservation.</summary>
/// <param name="bike">Bike to book.</param>
Task DoCancelReservation(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike);
/// <summary> Get authentication keys to connect to lock.</summary>
/// <param name="bike">Bike to book.</param>
Task CalculateAuthKeys(Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable bike);
/// <summary> Notifies COPRI about start of returning sequence. </summary>
/// <remarks> Operator specific call.</remarks>
/// <param name="bike">Bike to return.</param>
/// <returns>Response on notification about start of returning sequence.</returns>
Task StartReturningBike(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike);
/// <summary> Updates COPRI lock state for a booked bike. </summary>
/// <param name="bike">Bike to update locking state for.</param>
/// <param name="location">Geolocation of lock when returning bike.</param>
/// <returns>Response on updating locking state.</returns>
Task UpdateLockingStateAsync(Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable bike, LocationDto location = null);
/// <summary> Request to book a bike.</summary>
/// <param name="bike">Bike to book.</param>
/// <param name="nextAction">If not null next locking action which is performed after booking.</param>
Task DoBookAsync(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike, LockingAction? nextAction = null);
/// <summary> Request to book a bike and open its lock.</summary>
/// <param name="bike">Bike to book and to open lock for.</param>
Task BookAndOpenAync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike);
/// <summary> Request to open lock.</summary>
/// <param name="bike">Bike for which lock has to be opened.</param>
Task OpenLockAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike);
/// <summary> Request to close lock.</summary>
/// <param name="bike">Bike for which lock has to be closed.</param>
Task CloseLockAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike);
/// <summary> Request to return a bike.</summary>
/// <param name="bike">Bike to return.</param>
/// <param name="location">Geolocation of lock when returning bike.</param>
/// <param name="smartDevice">Provides info about hard and software.</param>
Task<BookingFinishedModel> DoReturn(Bikes.BikeInfoNS.BC.IBikeInfoMutable bike, LocationDto geolocation = null, ISmartDevice smartDevice = null);
/// <summary> Request to return bike and close the lock.</summary>
/// <param name="bike">Bike to return.</param>
/// <param name="smartDevice">Provides info about hard and software.</param>
Task<BookingFinishedModel> ReturnAndCloseAsync(Bikes.BikeInfoNS.CopriLock.IBikeInfoMutable bike, ISmartDevice smartDevice = null);
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
bool IsConnected { get; }
/// <summary> True if user is logged in false if not. </summary>
string SessionCookie { get; }
/// <summary> Submits feedback for a renting operation.</summary>
Task DoSubmitFeedback(
IUserFeedback userFeedback,
Uri opertorUri);
/// <summary> Submits mini survey to copri server. </summary>
/// <param name="answers">Collection of answers.</param>
Task DoSubmitMiniSurvey(IDictionary<string, string> answers);
#if USCSHARP9
/// <summary>
/// Feedback given by user when returning bike.
/// </summary>
public interface IUserFeedback
{
/// <summary> Id of the bike to which the feedback is related to.</summary>
string BikeId { get; }
/// <summary>
/// Holds whether bike is broken or not.
/// </summary>
bool IsBikeBroken { get; }
/// <summary>
/// Holds either
/// - general feedback
/// - error description of broken bike
/// or both.
/// </summary>
string Message { get; }
}
#endif
}
/// <summary>Defines delegate to be raised whenever login state changes.</summary>
/// <param name="eventArgs">Holds session cookie and mail address if user logged in successfully.</param>
public delegate void LoginStateChangedEventHandler(object sender, LoginStateChangedEventArgs eventArgs);
#if !USCSHARP9
/// <summary>
/// Feedback given by user when returning bike.
/// </summary>
public interface IUserFeedback
{
/// <summary> Id of the bike to which the feedback is related to.</summary>
string BikeId { get; }
/// <summary>
/// Holds the current charging level of the battery in bars, null if unknown.
/// </summary>
int? CurrentChargeBars { get; set; }
/// <summary>
/// Holds whether bike is broken or not.
/// </summary>
bool IsBikeBroken { get; }
/// <summary>
/// Holds either
/// - general feedback
/// - error description of broken bike
/// or both.
/// </summary>
string Message { get; }
}
#endif
/// <summary> Event arguments to notify about changes of logged in state.</summary>
public class LoginStateChangedEventArgs : EventArgs
{
public LoginStateChangedEventArgs() : this(string.Empty, string.Empty)
{ }
public LoginStateChangedEventArgs(string sessionCookie, string mail)
{
SessionCookie = sessionCookie;
Mail = mail;
}
public string SessionCookie { get; }
public string Mail { get; }
}
/// <summary>
/// Describes a action to be performed with an lock.
/// </summary>
public enum LockingAction
{
Close,
Open,
}
}

View file

@ -0,0 +1,32 @@
namespace ShareeBike.Model.Connector
{
#if USCSHARP9
public record UserFeedbackDto : ICommand.IUserFeedback
{
public string BikeId { get; init; }
public bool IsBikeBroken { get; init; }
public string Message { get; init; }
}
#else
#if USCSHARP9
public class UserFeedbackDto : ICommand.IUserFeedback
#else
public class UserFeedbackDto : IUserFeedback
#endif
{
public string BikeId { get; set; }
/// <summary>
/// Holds the current charging level of the battery in bars, null if unknown.
/// </summary>
public int? CurrentChargeBars { get; set; }
public bool IsBikeBroken { get; set; }
public string Message { get; set; }
}
#endif
}

View file

@ -0,0 +1,71 @@
using System;
using ShareeBike.Model.Device;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
namespace ShareeBike.Model.Connector
{
/// <summary>
/// Connects app to copri data by getting data from copri.
/// </summary>
public class Connector : IConnector
{
/// <summary>Constructs a copri connector object to connect to copri by https with cache fall back.</summary>
/// <param name="activeUri"> Uri to connect to.</param>
/// <param name="appContextInfo">Provides app related info (app name and version, merchant id) to pass to COPRI.</param>
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
/// <param name="sessionCookie"> Holds the session cookie.</param>
/// <param name="mail">Mail of user.</param>
/// <param name="smartDevice">Holds info about smart device.</param>
/// <param name="expiresAfter">Timespan which holds value after which cache expires.</param>
/// <param name="server"> Is null in production and might be a mock in testing context.</param>
public Connector(
Uri activeUri,
AppContextInfo appContextInfo,
string uiIsoLangugageName,
string sessionCookie,
string mail,
ISmartDevice smartDevice = null,
TimeSpan? expiresAfter = null,
ICachedCopriServer server = null)
{
var cachedServer = server ?? new CopriProviderHttps(
activeUri,
appContextInfo.MerchantId,
appContextInfo,
uiIsoLangugageName,
smartDevice,
sessionCookie,
expiresAfter);
Command = CreateCommand(
cachedServer,
sessionCookie,
mail);
Query = CreateQuery(
cachedServer,
sessionCookie,
mail);
}
/// <summary> Object for querying stations and bikes.</summary>
public ICommand Command { get; private set; }
/// <summary> Object for querying stations and bikes.</summary>
public IQuery Query { get; private set; }
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => Command.IsConnected;
/// <summary> Creates a command object to perform copri commands. </summary>
public static ICommand CreateCommand(ICopriServerBase copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
? new Command(copri)
: new CommandLoggedIn(copri, sessioncookie, mail, () => DateTime.Now) as ICommand;
/// <summary> Creates a command object to perform copri queries. </summary>
private static IQuery CreateQuery(ICachedCopriServer copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
? new CachedQuery(copri) as IQuery
: new CachedQueryLoggedIn(copri, sessioncookie, mail, () => DateTime.Now);
}
}

View file

@ -0,0 +1,53 @@
using System;
using ShareeBike.Model.Device;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
namespace ShareeBike.Model.Connector
{
/// <summary>
/// Connects app to copri data by getting data from cache.
/// </summary>
public class ConnectorCache : IConnector
{
/// <summary>Constructs a copri connector object to connect to cache.</summary>
/// <remarks>Used for offline scenario to ensure responsiveness of app by preventing hopeless tries to communicate with COPRI. </remarks>
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
/// <param name="sessionCookie"> Holds the session cookie.</param>
/// <param name="mail">Mail of user.</param>
/// <param name="smartDevice">Holds info about smart device.</param>
/// <param name="server"> Is null in production and might be a mock in testing context.</param>
public ConnectorCache(
AppContextInfo appContextInfo,
string uiIsoLangugageName,
string sessionCookie,
string mail,
ISmartDevice smartDevice = null,
ICopriServer server = null)
{
Command = Connector.CreateCommand(
server ?? new CopriProviderMonkeyStore(appContextInfo.MerchantId, uiIsoLangugageName, sessionCookie, smartDevice),
sessionCookie,
mail);
Query = GetQuery(
server ?? new CopriProviderMonkeyStore(appContextInfo.MerchantId, uiIsoLangugageName, sessionCookie, smartDevice),
sessionCookie,
mail);
}
/// <summary> Object for querying stations and bikes.</summary>
public ICommand Command { get; private set; }
/// <summary> Object for querying stations and bikes.</summary>
public IQuery Query { get; private set; }
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => Command.IsConnected;
/// <summary> Gets a command object to perform copri queries. </summary>
private static IQuery GetQuery(ICopriServer copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
? new Query(copri) as IQuery
: new QueryLoggedIn(copri, sessioncookie, mail, () => DateTime.Now);
}
}

View file

@ -0,0 +1,34 @@
using System;
using ShareeBike.Model.Device;
using ShareeBike.Repository;
namespace ShareeBike.Model.Connector
{
public class ConnectorFactory
{
/// <summary>
/// Gets a connector object depending on whether beein onlin or offline.
/// </summary>
/// <param name="isConnected">
/// True if online, false if offline.
/// If offline cache connector is returned to avoid performance penalty which would happen when trying to communicate with backend in offline scenario.
/// </param>
/// <param name="appContextInfo">Provides app related info (app name and version, merchantid) to pass to COPRI.</param>
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
/// <param name="smartDevice">Holds info about smart device.</param>
public static IConnector Create(
bool isConnected,
Uri activeUri,
AppContextInfo appContextInfo,
string uiIsoLangugageName,
string sessionCookie,
string mail,
ISmartDevice smartDevice = null,
TimeSpan? expiresAfter = null)
{
return isConnected
? new Connector(activeUri, appContextInfo, uiIsoLangugageName, sessionCookie, mail, smartDevice, expiresAfter: expiresAfter) as IConnector
: new ConnectorCache(appContextInfo, uiIsoLangugageName, sessionCookie, mail, smartDevice);
}
}
}

View file

@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Linq;
namespace ShareeBike.Model.Connector.Filter
{
public static class GroupFilterFactory
{
/// <summary>
/// Creates filter object.
/// </summary>
/// <param name="group">if value consists
/// - list of strings entries are used to filter (intersect) with or if value is
/// - null or an empty list null filter is applied, i.e. filtering is off.</param>
/// <returns>Filtering object.</returns>
/// <remarks>
/// Tread group values of null and empty lists as marker to turn filtering off to handle COPRI responses maximal flexible.
/// </remarks>
public static IGroupFilter Create(IEnumerable<string> group)
{
return group != null && group.Count() > 0
? (IGroupFilter)new IntersectGroupFilter(group) :
new NullGroupFilter();
}
}
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace ShareeBike.Model.Connector.Filter
{
public interface IGroupFilter
{
IEnumerable<string> DoFilter(IEnumerable<string> filter);
}
}

View file

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace ShareeBike.Model.Connector.Filter
{
/// <summary> Filters to enumerations of string by intersecting.</summary>
public class IntersectGroupFilter : IGroupFilter
{
private IEnumerable<string> Group { get; set; }
public IntersectGroupFilter(IEnumerable<string> group) => Group = group ?? new List<string>();
/// <summary> Applies filtering. </summary>
/// <param name="filter">Enumeration of filter values to filter with or null if no filtering has to be applied.</param>
/// <returns></returns>
public IEnumerable<string> DoFilter(IEnumerable<string> filter) => filter != null
? Group.IntersectByGoupId(filter)
: Group;
}
}

View file

@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ShareeBike.Model.Connector.Filter
{
public static class IntersectGroupFilterHelper
{
/// <summary>
/// Transforms a group (each element consists of an operator prefix and a numeric bike category) to bike category enumeration (numeric elements).
/// </summary>
/// <param name="group">Group to transform.</param>
/// <returns>Enumeration of numeric bike categories.</returns>
public static IEnumerable<string> ToBikeCategory(this IEnumerable<string> group)
=> group?.Select(x => x.GetBikeCategory())?.Where(x => !string.IsNullOrEmpty(x))
?? new List<string>();
/// <summary>
/// Extracts bike group category umber from a group string.
/// </summary>
/// <param name="group">Group to transform. Example KN_300101 (Stadtrad located in Konstanz), FR_300102 (Lastenrad located in Freiburg).</param>
/// <returns>Enumeration of numeric bike categories.</returns>
public static string GetBikeCategory(this string group)
=> Regex.Match(group, "[0-9]+")?.Value ?? string.Empty;
/// <summary>
/// Intersects two goups only taking into accout the numeric bike group category part.
/// </summary>
/// <param name="group">Group to filter.</param>
/// <param name="filter">Filter to apply</param>
public static IEnumerable<string> IntersectByGoupId(this IEnumerable<string> group, IEnumerable<string> filter)
=> group.Where(x => filter.ContainsGroupId(x));
/// <summary>
/// Gets if group contains a filter element.
/// </summary>
/// <param name="group"></param>
/// <param name="filterElement"></param>
/// <returns></returns>
public static bool ContainsGroupId(this IEnumerable<string> group, string filterElement)
=> group.ToBikeCategory().Contains(filterElement.GetBikeCategory());
}
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace ShareeBike.Model.Connector.Filter
{
public class NullGroupFilter : IGroupFilter
{
public IEnumerable<string> DoFilter(IEnumerable<string> filter) => filter;
}
}

View file

@ -0,0 +1,11 @@
namespace ShareeBike.Model.Connector
{
public static class FilterHelper
{
/// <summary> Holds the Citybike group (city bikes).</summary>
public const string CITYBIKE = "300103";
/// <summary> Holds the Cargo group (Lastenräder).</summary>
public const string CARGOBIKE = "300101";
}
}

View file

@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Connector.Filter;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Model.Stations;
using ShareeBike.Model.Stations.StationNS;
using ShareeBike.Model.Stations.StationNS.Operator;
using BikeInfo = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfo;
namespace ShareeBike.Model.Connector
{
/// <summary> Filters connector responses.</summary>
/// <remarks>Former name: Filter</remarks>
public class FilteredConnector : IFilteredConnector
{
/// <summary> Constructs a filter object. </summary>
/// <param name="group">Filter group.</param>
/// <param name="connector">Connector object.</param>
public FilteredConnector(
IEnumerable<string> group,
IConnector connector)
{
Connector = connector;
if (Connector == null)
{
throw new ArgumentException("Can not construct filter object. Connector- and command objects must not be null.");
}
Query = new QueryProvider(Connector.Query, GroupFilterFactory.Create(group));
}
/// <summary> Inner connector object.</summary>
public IConnector Connector { get; }
/// <summary> Command object. </summary>
public ICommand Command => Connector.Command;
/// <summary> Object to query information. </summary>
public IQuery Query { get; }
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => Connector.IsConnected;
/// <summary> Object to perform filtered queries.</summary>
private class QueryProvider : IQuery
{
/// <summary> Holds the filter. </summary>
private IGroupFilter Filter { get; }
/// <summary> Holds the reference to object which performs copri queries.</summary>
private IQuery m_oInnerQuery;
/// <summary> Constructs a query object.</summary>
/// <param name="innerQuerry"></param>
/// <param name="filter"></param>
public QueryProvider(IQuery innerQuerry, IGroupFilter filter)
{
m_oInnerQuery = innerQuerry;
Filter = filter;
}
/// <summary> Gets bikes either bikes available if no user is logged in or bikes available and bikes occupied if a user is logged in. </summary>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
public async Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
{
var result = await m_oInnerQuery.GetBikesAsync(operatorUri, stationId, bikeId);
if (bikeId == null)
{
// Do filter
return new Result<BikeCollection>(
result.Source,
new BikeCollection(DoFilter(result.Response, Filter)),
result.GeneralData,
result.Exception);
}
else
{
// Do NOT filter on SelectBikePage = use function from NullFilterConnector: https://dev.azure.com/TeilRad/sharee.bike%20Buchungsplattform/_workitems/edit/904
return new Result<BikeCollection>(
result.Source,
new BikeCollection(result.Response.ToDictionary(x => x.Id)),
result.GeneralData,
result.Exception);
}
}
/// <summary> Gets bikes occupied if a user is logged in. </summary>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
var result = await m_oInnerQuery.GetBikesOccupiedAsync();
return new Result<BikeCollection>(
result.Source,
new BikeCollection(result.Response.ToDictionary(x => x.Id)),
result.GeneralData,
result.Exception);
}
/// <summary> Gets all station applying filter rules. </summary>
/// <returns></returns>
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
// Bikes and stations from COPRI or cache
var providerBikesAndStations = await m_oInnerQuery.GetBikesAndStationsAsync();
// Do filtering.
var filteredStationsDictionary = new StationDictionary(
providerBikesAndStations.Response.StationsAll.CopriVersion,
DoFilter(providerBikesAndStations.Response.StationsAll, Filter));
var filteredBikesOccupiedDictionary = new BikeCollection(
DoFilter(providerBikesAndStations.Response.BikesOccupied, Filter));
var filteredBikesAndStations = new Result<StationsAndBikesContainer>(
providerBikesAndStations.Source,
new StationsAndBikesContainer(
filteredStationsDictionary,
filteredBikesOccupiedDictionary),
providerBikesAndStations.GeneralData,
providerBikesAndStations.Exception);
return filteredBikesAndStations;
}
/// <summary> Filter bikes by group. </summary>
/// <param name="bikes">Bikes to filter.</param>
/// <returns>Filtered bikes.</returns>
private static Dictionary<string, BikeInfo> DoFilter(BikeCollection bikes, IGroupFilter filter) =>
bikes
.Where(x => filter.DoFilter(x.Group).Count() > 0)
.ToDictionary(x => x.Id);
/// <summary> Filter stations by group and removes bike group collection entries which do not match group filter. </summary>
/// <returns>Matching stations.</returns>
private static Dictionary<string, IStation> DoFilter(StationDictionary stations, IGroupFilter filter) =>
stations
.Where(station => filter.DoFilter(station.Group).Count() > 0)
.Select(station => new Station(
station.Id,
station.Group,
station.Position,
station.StationName,
station.OperatorUri,
station.OperatorData,
new BikeGroupCol(station.BikeGroups
.Where(group => filter.DoFilter(new List<string> { group.Group }).Count() > 0))) as IStation)
.ToDictionary(x => x.Id);
}
}
}

View file

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace ShareeBike.Model.Connector
{
public static class FilteredConnectorFactory
{
/// <summary> Creates a filter object. </summary>
/// <param name="group">Filter to apply on stations and bikes.</param>
/// <param name="connector">Connector to connect to COPRI.</param>
public static IFilteredConnector Create(IEnumerable<string> group, IConnector connector)
{
return group != null
? (IFilteredConnector)new FilteredConnector(group, connector)
: new NullFilterConnector(connector); // Do not apply filtering.
}
}
}

View file

@ -0,0 +1,14 @@
namespace ShareeBike.Model.Connector
{
public interface IConnector
{
/// <summary> Object for querying stations and bikes.</summary>
ICommand Command { get; }
/// <summary> Object for querying stations and bikes.</summary>
IQuery Query { get; }
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
bool IsConnected { get; }
}
}

View file

@ -0,0 +1,7 @@
namespace ShareeBike.Model.Connector
{
public interface IFilteredConnector : IConnector
{
IConnector Connector { get; }
}
}

View file

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Model.Stations;
using ShareeBike.Model.Stations.StationNS;
using BikeInfo = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfo;
namespace ShareeBike.Model.Connector
{
/// <summary> Filters connector responses.</summary>
public class NullFilterConnector : IFilteredConnector
{
/// <summary> Constructs a filter object. </summary>
/// <param name="p_oGroup">Filter group.</param>
/// <param name="connector">Connector object.</param>
public NullFilterConnector(
IConnector connector)
{
Connector = connector;
if (Connector == null)
{
throw new ArgumentException("Can not construct filter object. Connector- and command objects must not be null.");
}
Query = new QueryProvider(Connector.Query);
}
/// <summary> Inner connector object.</summary>
public IConnector Connector { get; }
/// <summary> Command object. </summary>
public ICommand Command => Connector.Command;
/// <summary> Object to query information. </summary>
public IQuery Query { get; }
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => Connector.IsConnected;
/// <summary> Object to perform filtered queries.</summary>
private class QueryProvider : IQuery
{
/// <summary> Holds the reference to object which performs copri queries.</summary>
private IQuery m_oInnerQuery;
/// <summary> Constructs a query object.</summary>
/// <param name="p_oInnerQuery"></param>
/// <param name="p_oFilter"></param>
public QueryProvider(IQuery p_oInnerQuery)
{
m_oInnerQuery = p_oInnerQuery;
}
/// <summary> Gets bikes either bikes available if no user is logged in or bikes available and bikes occupied if a user is logged in. </summary>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
public async Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
{
var result = await m_oInnerQuery.GetBikesAsync(operatorUri, stationId, bikeId);
return new Result<BikeCollection>(
result.Source,
new BikeCollection(result.Response.ToDictionary(x => x.Id)),
result.GeneralData,
result.Exception);
}
/// <summary> Gets bikes occupied if a user is logged in. </summary>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
var result = await m_oInnerQuery.GetBikesOccupiedAsync();
return new Result<BikeCollection>(
result.Source,
new BikeCollection(result.Response.ToDictionary(x => x.Id)),
result.GeneralData,
result.Exception);
}
/// <summary> Gets all station applying filter rules. </summary>
/// <returns></returns>
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
var result = await m_oInnerQuery.GetBikesAndStationsAsync();
return new Result<StationsAndBikesContainer>(
result.Source,
new StationsAndBikesContainer(
new StationDictionary(
result.Response.StationsAll.CopriVersion,
result.Response.StationsAll.ToDictionary(x => x.Id)),
new BikeCollection(
result.Response.BikesOccupied?.ToDictionary(x => x.Id) ?? new Dictionary<string, BikeInfo>())),
result.GeneralData,
result.Exception);
}
/// <summary> Filter bikes by group. </summary>
/// <param name="bikes">Bikes to filter.</param>
/// <returns>Filtered bikes.</returns>
public static Dictionary<string, BikeInfo> DoFilter(BikeCollection bikes, IEnumerable<string> filter)
{
return bikes.Where(x => x.Group.Intersect(filter).Count() > 0).ToDictionary(x => x.Id);
}
/// <summary> Filter stations by group. </summary>
/// <returns></returns>
public static Dictionary<string, IStation> DoFilter(StationDictionary stations, IEnumerable<string> p_oFilter)
{
return stations.Where(x => x.Group.Intersect(p_oFilter).Count() > 0).ToDictionary(x => x.Id);
}
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using ShareeBike.Repository;
namespace ShareeBike.Model.Connector
{
/// <summary>
/// Provides information required for copri commands/ query operations.
/// </summary>
public class Base
{
/// <summary> Reference to object which provides access to copri server. </summary>
protected ICopriServerBase CopriServer { get; }
/// <summary> Gets the merchant id.</summary>
protected string MerchantId => CopriServer.MerchantId;
/// <summary> Constructs a query base object.</summary>
/// <param name="p_oCopriServer">Server which implements communication.</param>
/// <param name="p_oErrorStack">Object which hold communication objects.</param>
protected Base(
ICopriServerBase p_oCopriServer)
{
CopriServer = p_oCopriServer
?? throw new ArgumentException("Can not instantiate command/ query base- object. Copri server object must never be null or emtpy.");
}
}
}

View file

@ -0,0 +1,39 @@
using System;
using ShareeBike.Repository;
namespace ShareeBike.Model.Connector
{
/// <summary>Holds user infromation required for copri related commands/ query operations. </summary>
public class BaseLoggedIn : Base
{
/// <summary>Session cookie used to sign in to copri.</summary>
public string SessionCookie { get; }
/// <summary> Mail address of the user. </summary>
protected string Mail { get; }
/// <summary> Object which provides date time info. </summary>
protected readonly Func<DateTime> DateTimeProvider;
/// <summary>Constructs a copri query object.</summary>
/// <param name="copriServer">Server which implements communication.</param>
public BaseLoggedIn(ICopriServerBase copriServer,
string sessionCookie,
string mail,
Func<DateTime> p_oDateTimeProvider) : base(copriServer)
{
if (string.IsNullOrEmpty(sessionCookie))
throw new ArgumentException("Can not instantiate query object- object. Session cookie must never be null or emtpy.");
if (string.IsNullOrEmpty(mail))
throw new ArgumentException("Can not instantiate query object- object. Mail address must never be null or emtpy.");
DateTimeProvider = p_oDateTimeProvider
?? throw new ArgumentException("Can not instantiate connector- object. No date time provider object available.");
SessionCookie = sessionCookie;
Mail = mail;
}
}
}

View file

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Serilog;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
using ShareeBike.Services.CopriApi;
using BikeInfo = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfo;
namespace ShareeBike.Model.Connector
{
/// <summary> Provides query functionality for use without log in. </summary>
public class CachedQuery : Base, IQuery
{
/// <summary> Cached copri server (connection to copri backed up by cache). </summary>
private readonly ICachedCopriServer server;
/// <summary>Constructs a copri query object.</summary>
/// <param name="copriServer">Server which implements communication.</param>
public CachedQuery(
ICopriServerBase copriServer) : base(copriServer)
{
server = copriServer as ICachedCopriServer;
if (server == null)
{
throw new ArgumentException($"Copri server is not of expected type. Type detected is {copriServer.GetType()}.");
}
}
/// <summary> Gets all stations including positions and bikes.</summary>
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
var resultStations = await server.GetStations();
if (resultStations.Source == typeof(CopriCallsMonkeyStore))
{
// Communication with copri in order to get stations failed.
return new Result<StationsAndBikesContainer>(
resultStations.Source,
new StationsAndBikesContainer(
resultStations.Response.GetStationsAllMutable(),
new BikeCollection() /* There are no bikes occupied because user is not logged in. */),
resultStations.GeneralData,
resultStations.Exception);
}
// Communication with copri succeeded.
server.AddToCache(resultStations);
return new Result<StationsAndBikesContainer>(
resultStations.Source,
new StationsAndBikesContainer(
resultStations.Response.GetStationsAllMutable(),
new BikeCollection()),
resultStations.GeneralData);
}
/// <summary> Gets bikes occupied. </summary>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
Log.ForContext<CachedQuery>().Error("Unexpected call to get be bikes occupied detected. No user is logged in.");
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
await Task.FromResult(new BikeCollection(new Dictionary<string, BikeInfo>())),
new GeneralData(),
new Exception("Abfrage der reservierten/ gebuchten Räder nicht möglich. Kein Benutzer angemeldet."));
}
/// <summary> Gets bikes available. </summary>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
{
var result = await server.GetBikesAvailable(operatorUri: operatorUri, stationId: stationId, bikeId: bikeId);
if (result.Source != typeof(CopriCallsMonkeyStore))
{
server.AddToCache(result, operatorUri, stationId, bikeId);
}
return new Result<BikeCollection>(
result.Source,
result.Response.GetBikesAvailable(result.Source == typeof(CopriCallsMonkeyStore)
? Bikes.BikeInfoNS.BC.DataSource.Cache
: Bikes.BikeInfoNS.BC.DataSource.Copri),
result.GeneralData,
result.Exception);
}
}
}

View file

@ -0,0 +1,212 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Serilog;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
using ShareeBike.Repository.Response;
namespace ShareeBike.Model.Connector
{
/// <summary> Provides query functionality for a logged in user. </summary>
public class CachedQueryLoggedIn : BaseLoggedIn, IQuery
{
/// <summary> Cached copri server (connection to copri backed up by cache). </summary>
private ICachedCopriServer Server { get; }
/// <summary>Constructs a copri query object.</summary>
/// <param name="copriServer">Server which implements communication.</param>
public CachedQueryLoggedIn(ICopriServerBase copriServer,
string sessionCookie,
string mail,
Func<DateTime> dateTimeProvider) : base(copriServer, sessionCookie, mail, dateTimeProvider)
{
Server = copriServer as ICachedCopriServer;
if (Server == null)
{
throw new ArgumentException($"Copri server is not of expected type. Type detected is {copriServer.GetType()}.");
}
}
/// <summary> Gets all stations including positions.</summary>
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
BikeCollection GetBikeCollection(IEnumerable<BikeInfoReservedOrBooked> bikeInfoEnumerable, Bikes.BikeInfoNS.BC.DataSource dataSource) =>
BikeCollectionFactory.GetBikesAll(
null, // Bikes available are no more of interest because count of available bikes at each given station is was added to station object.
bikeInfoEnumerable ?? new Dictionary<string, BikeInfoReservedOrBooked>().Values,
Mail,
DateTimeProvider,
dataSource);
var stationsResponse = await Server.GetStations();
if (stationsResponse.Source == typeof(CopriCallsMonkeyStore)
|| stationsResponse.Exception != null)
{
// Stations were read from cache ==> get bikes available and occupied from cache as well to avoid inconsistencies
return new Result<StationsAndBikesContainer>(
stationsResponse.Source,
new StationsAndBikesContainer(
stationsResponse.Response.GetStationsAllMutable(),
GetBikeCollection(stationsResponse.Response.bikes_occupied?.Values, Bikes.BikeInfoNS.BC.DataSource.Cache)),
stationsResponse.GeneralData,
stationsResponse.Exception);
}
// Both types bikes could read from copri => update cache
Server.AddToCache(stationsResponse);
return new Result<StationsAndBikesContainer>(
stationsResponse.Source,
new StationsAndBikesContainer(
stationsResponse.Response.GetStationsAllMutable(),
GetBikeCollection(stationsResponse.Response.bikes_occupied?.Values, Bikes.BikeInfoNS.BC.DataSource.Copri)),
stationsResponse.GeneralData,
stationsResponse?.Exception);
}
/// <summary> Gets bikes occupied. </summary>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
var bikesAvailableResponse = await Server.GetBikesAvailable(false);
if (bikesAvailableResponse.Source == typeof(CopriCallsMonkeyStore)
|| bikesAvailableResponse.Exception != null)
{
// Bikes available were read from cache ==> get bikes occupied from cache as well to avoid inconsistencies.
Log.ForContext<CachedQueryLoggedIn>().Debug("Bikes available read from cache. Reading bikes occupied from cache as well.");
return new Result<BikeCollection>(
bikesAvailableResponse.Source,
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse.Response?.bikes?.Values?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
(await Server.GetBikesOccupied(true))?.Response?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesAvailableResponse.GeneralData,
bikesAvailableResponse.Exception);
}
var bikesOccupiedResponse = await Server.GetBikesOccupied(false);
if (bikesOccupiedResponse.Source == typeof(CopriCallsMonkeyStore)
|| bikesOccupiedResponse.Exception != null)
{
// Bikes occupied were read from cache ==> get bikes available from cache as well to avoid inconsistencies
Log.ForContext<CachedQueryLoggedIn>().Debug("Bikes occupied read from cache. Reread bikes available from cache as well.");
return new Result<BikeCollection>(
bikesOccupiedResponse.Source,
BikeCollectionFactory.GetBikesAll(
(await Server.GetBikesAvailable(true)).Response?.bikes?.Values?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
bikesOccupiedResponse.Response?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesOccupiedResponse.GeneralData,
bikesOccupiedResponse.Exception);
}
// Both types bikes could read from copri => update bikes occupied cache.
// // Do not add bikes available to cache because this might lead to conflicts calls GetBikesAsync() and bikes with FeedbackPending state are of no use offline.
Server.AddToCache(bikesOccupiedResponse);
return new Result<BikeCollection>(
bikesOccupiedResponse.Source,
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse?.Response.bikes?.Values?.Select(bike => bike)?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
bikesOccupiedResponse?.Response?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Copri),
bikesOccupiedResponse.GeneralData,
bikesOccupiedResponse.Exception);
}
/// <summary> Gets bikes available and bikes occupied. </summary>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
{
var bikesAvailableResponse = await Server.GetBikesAvailable(operatorUri: operatorUri, stationId: stationId, bikeId: bikeId);
if (bikesAvailableResponse.Source == typeof(CopriCallsMonkeyStore)
|| bikesAvailableResponse.Exception != null)
{
// Bikes were read from cache.
Log.ForContext<CachedQueryLoggedIn>().Debug("Bikes available and bikes occupied from cache invoking one single call.");
return new Result<BikeCollection>(
bikesAvailableResponse.Source,
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse.Response?.bikes?.Values,
operatorUri?.AbsoluteUri == null ?
(await Server.GetBikesOccupied(true)).Response?.bikes_occupied?.Values // Get bikes occupied from cache as well to avoid inconsistencies.
: bikesAvailableResponse.Response?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesAvailableResponse.GeneralData,
bikesAvailableResponse.Exception);
}
if (operatorUri?.AbsoluteUri != null)
{
// Both types bikes could read from copri successfully => update cache
Server.AddToCache(bikesAvailableResponse, operatorUri, stationId, bikeId);
Log.ForContext<CachedQueryLoggedIn>().Debug("Bikes available and occupied read successfully from server invoking one single request.");
return new Result<BikeCollection>(
bikesAvailableResponse.Source,
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse.Response?.bikes?.Values,
bikesAvailableResponse.Response?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Copri),
bikesAvailableResponse.GeneralData,
bikesAvailableResponse.Exception != null ? new AggregateException(new[] { bikesAvailableResponse.Exception }) : null);
}
/// Legacy implementation: GetBikesOccupied are not returned in <see cref="ICachedCopriServer.GetBikesAvailable"/> call.
/// A separate call <see cref="ICachedCopriServer.GetBikesOccupied"/> is required to retrieve all bikes.
var bikesOccupiedResponse = await Server.GetBikesOccupied(); /* Only query bikes occupied if operator uri is unknown. */
if (bikesOccupiedResponse.Source == typeof(CopriCallsMonkeyStore)
|| bikesOccupiedResponse.Exception != null)
{
// Bikes occupied were read from cache ==> get bikes available from cache as well to avoid inconsistencies
Log.ForContext<CachedQueryLoggedIn>().Debug("Bikes occupied read from cache. Reread bikes available from cache as well.");
return new Result<BikeCollection>(
bikesOccupiedResponse.Source,
BikeCollectionFactory.GetBikesAll(
(await Server.GetBikesAvailable(true, operatorUri, stationId, bikeId)).Response?.bikes?.Values,
bikesOccupiedResponse.Response?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesOccupiedResponse.GeneralData,
bikesOccupiedResponse.Exception);
}
// Both types bikes could read from copri => update cache
Server.AddToCache(bikesAvailableResponse, operatorUri, stationId, bikeId);
Server.AddToCache(bikesOccupiedResponse);
Log.ForContext<CachedQueryLoggedIn>().Debug("Bikes available and occupied read successfully from server.");
return new Result<BikeCollection>(
bikesAvailableResponse.Source,
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse.Response?.bikes?.Values,
bikesOccupiedResponse.Response?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Copri),
bikesAvailableResponse.GeneralData,
bikesAvailableResponse.Exception != null || bikesOccupiedResponse.Exception != null ? new AggregateException(new[] { bikesAvailableResponse.Exception, bikesOccupiedResponse.Exception }) : null);
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Services.CopriApi;
namespace ShareeBike.Model.Connector
{
public interface IQuery
{
/// <summary> Gets all stations including positions.</summary>
Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync();
/// <summary> Gets bikes occupied is a user is logged in. </summary>
/// <returns>Collection of bikes.</returns>
Task<Result<BikeCollection>> GetBikesOccupiedAsync();
/// <summary> Gets bikes either bikes available if no user is logged in or bikes available and bikes occupied if a user is logged in. </summary>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <returns>Collection of bikes.</returns>
Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null);
}
}

View file

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Serilog;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
using ShareeBike.Services.CopriApi;
using BikeInfo = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfo;
namespace ShareeBike.Model.Connector
{
/// <summary> Provides query functionality from cache without login. </summary>
public class Query : Base, IQuery
{
/// <summary> Cached copri server. </summary>
private readonly ICopriServer server;
/// <summary>Constructs a copri query object.</summary>
/// <param name="copriServer">Server which implements communication.</param>
public Query(ICopriServerBase copriServer) : base(copriServer)
{
server = copriServer as ICopriServer;
if (server == null)
{
throw new ArgumentException($"Copri server is not of expected type. Type detected is {copriServer.GetType()}.");
}
}
/// <summary> Gets all stations including positions.</summary>
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
var stationsAllResponse = await server.GetStationsAsync();
return new Result<StationsAndBikesContainer>(
typeof(CopriCallsMonkeyStore),
new StationsAndBikesContainer(
stationsAllResponse.GetStationsAllMutable(),
new BikeCollection() /* There are no bikes occupied because user is not logged in. */),
stationsAllResponse.GetGeneralData());
}
/// <summary> Gets bikes occupied. </summary>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
Log.ForContext<Query>().Error("Unexpected call to get be bikes occupied detected. No user is logged in.");
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
await Task.FromResult(new BikeCollection(new Dictionary<string, BikeInfo>())),
new GeneralData(),
new Exception("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen. Kein Benutzer angemeldet."));
}
/// <summary> Gets bikes occupied. </summary>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <returns> Collection of bikes. </returns>
public async Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
{
var bikesAvailableResponse = await server.GetBikesAvailableAsync(operatorUri, stationId, bikeId);
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
bikesAvailableResponse != null
? bikesAvailableResponse.GetBikesAvailable(Bikes.BikeInfoNS.BC.DataSource.Cache)
: await Task.FromResult(new BikeCollection(new Dictionary<string, BikeInfo>())),
bikesAvailableResponse?.GetGeneralData());
}
}
}

View file

@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
using ShareeBike.Repository.Response;
namespace ShareeBike.Model.Connector
{
/// <summary> Provides query functionality from cache for a logged in user. </summary>
public class QueryLoggedIn : BaseLoggedIn, IQuery
{
/// <summary> Copri server. </summary>
private readonly ICopriServer server;
/// <summary>Constructs a copri query object.</summary>
/// <param name="copriServer">Server which implements communication.</param>
public QueryLoggedIn(ICopriServerBase copriServer,
string sessionCookie,
string mail,
Func<DateTime> dateTimeProvider) : base(copriServer, sessionCookie, mail, dateTimeProvider)
{
server = copriServer as ICopriServer;
if (server == null)
{
throw new ArgumentException($"Copri server is not of expected type. Type detected is {copriServer.GetType()}.");
}
server = copriServer as ICopriServer;
}
/// <summary> Gets all stations including positions.</summary>
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
var stationResponse = await server.GetStationsAsync();
return new Result<StationsAndBikesContainer>(
typeof(CopriCallsMonkeyStore),
new StationsAndBikesContainer(
stationResponse.GetStationsAllMutable(),
BikeCollectionFactory.GetBikesAll(
null, // Bikes available are no more of interest because count of available bikes at each given station is was added to station object.
stationResponse.bikes_occupied?.Values ?? new Dictionary<string, BikeInfoReservedOrBooked>().Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache)),
stationResponse.GetGeneralData());
}
/// <summary> Gets bikes occupied and bikes for which feedback is required. </summary>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
var bikesFeedbackRequired = await server.GetBikesAvailableAsync();
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
BikeCollectionFactory.GetBikesAll(
bikesFeedbackRequired.bikes?.Values?.Select(bike => bike)?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesOccupiedResponse.GetGeneralData());
}
/// <summary> Gets bikes available and bikes occupied. </summary>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
{
var bikesAvailableResponse = await server.GetBikesAvailableAsync(operatorUri, stationId, bikeId);
if (operatorUri?.AbsoluteUri != null)
{
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesAvailableResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesAvailableResponse?.GetGeneralData());
}
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesAvailableResponse?.GetGeneralData());
}
}
}

View file

@ -0,0 +1,489 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Serilog;
using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS;
using ShareeBike.Model.Services.CopriApi.ServerUris;
using ShareeBike.Model.State;
using ShareeBike.Model.Stations.StationNS;
using ShareeBike.Model.Stations.StationNS.Operator;
using ShareeBike.Repository.Exception;
using ShareeBike.Repository.Response;
using ShareeBike.Repository.Response.Stations.Station;
using Xamarin.Forms;
namespace ShareeBike.Model.Connector
{
/// <summary>
/// Converts weak typed JSON data (mostly string) to strong typed c# data (base types, enumerations, objects, ...).
/// JSON is received from COPRI and deserialized using Json.NET.
/// </summary>
public static class TextToTypeHelper
{
/// <summary> Holds the text for demo bikes. </summary>
private const string DEMOBIKEMARKER = "DEMO";
/// <summary>
/// Gets the position from StationInfo object.
/// </summary>
/// <param name="stationInfo">Object to get information from.</param>
/// <returns>Position information.</returns>
public static IPosition GetPosition(this StationInfo stationInfo)
=> GetPosition(stationInfo.gps);
/// <summary> Gets the position from StationInfo object. </summary>
/// <param name="authorizationResponse">Object to get information from.</param>
/// <returns>Position information.</returns>
public static IEnumerable<string> GetGroup(this AuthorizationResponse authorizationResponse)
{
try
{
return authorizationResponse.user_group.GetGroup();
}
catch (Exception l_oException)
{
throw new Exception($"Can not get group of user from text \"{authorizationResponse.user_group}\".", l_oException);
}
}
/// <summary> Gets the position from StationInfo object. </summary>
/// <param name="group">Object to get information from.</param>
/// <returns>Position information.</returns>
public static IEnumerable<string> GetGroup(this string[] group)
{
if (group == null || group.Length == 0)
{
// If not logged in stations groups are empty form COPRI version v4.1.
Log.Debug("Can not get group form string. Group text can not be null.");
return new List<string>();
}
return new HashSet<string>(group).ToList();
}
/// <summary> Gets if user acknowledged AGBs or not. </summary>
/// <param name="authorizationResponse">Object to get information from.</param>
/// <returns>Position information.</returns>
public static bool GetIsAgbAcknowledged(this AuthorizationResponse authorizationResponse)
=> int.TryParse(authorizationResponse?.agb_checked, out int result)
&& result != 0;
/// <summary> Gets the position from StationInfo object. </summary>
/// <param name="group">Object to get information from.</param>
/// <returns>Position information.</returns>
public static string GetGroup(this IEnumerable<string> group)
{
return string.Join(",", group);
}
/// <summary> Gets the position from StationInfo object. </summary>
/// <param name="stationInfo">Object to get information from.</param>
/// <returns>Position information.</returns>
public static IEnumerable<string> GetGroup(this StationInfo stationInfo)
{
try
{
return stationInfo.station_group.GetGroup();
}
catch (Exception l_oException)
{
throw new Exception($"Can not get group of stations from text \"{stationInfo.station_group}\".", l_oException);
}
}
/// <summary>
/// Gets the position from BikeInfoBase object.
/// </summary>
/// <param name="bikeInfo">Object to get information from.</param>
/// <returns>Rental state.</returns>
public static InUseStateEnum GetState(this BikeInfoBase bikeInfo)
{
var stateText = bikeInfo.state?.ToLower()?.Trim();
if (string.IsNullOrEmpty(stateText))
{
throw new InvalidResponseException<BikeInfoBase>(
"Bike state must not be empty.",
bikeInfo);
}
if (Enum.TryParse(stateText, out InUseStateEnum state))
return state;
if (stateText == "available")
{
return InUseStateEnum.Disposable;
}
else if (stateText == "reserved" ||
stateText == "requested")
{
return InUseStateEnum.Reserved;
}
else if (stateText == "booked" ||
stateText == "occupied")
{
return InUseStateEnum.Booked;
}
throw new CommunicationException(string.Format("Unknown bike state detected. State is {0}.", stateText));
}
/// <summary>
/// Gets the position from BikeInfoAvailable object.
/// </summary>
/// <param name="bikeInfo">Object to get information from.</param>
/// <returns>Rental state.</returns>
public static InUseStateEnum GetState(this BikeInfoAvailable bikeInfo)
{
var state = GetState((BikeInfoBase)bikeInfo);
if (state != InUseStateEnum.Disposable)
return state;
return bikeInfo.GetIsFeedbackPending()
? InUseStateEnum.FeedbackPending
: InUseStateEnum.Disposable;
}
/// <summary>
/// Gets the from date information from JSON.
/// </summary>
/// <param name="bikeInfo">JSON to get information from..</param>
/// <returns>From information if bike hold this information 0001-01-01 (DateTime.MinValue) otherwise.</returns>
public static DateTime GetFrom(this BikeInfoReservedOrBooked bikeInfo)
=> DateTime.TryParse(bikeInfo?.start_time, out DateTime dateFrom) ? dateFrom : DateTime.MinValue;
/// <summary>
/// Gets whether the bike is a trike or not.
/// </summary>
/// <param name="bikeInfo">JSON to get information from..</param>
/// <returns>From information.</returns>
public static bool? GetIsDemo(this BikeInfoBase bikeInfo)
{
return bikeInfo?.description != null
? bikeInfo.description.ToUpper().Contains(DEMOBIKEMARKER)
: (bool?)null;
}
/// <summary>
/// Gets whether the bike is a trike or not.
/// </summary>
/// <param name="bikeInfo">JSON to get information from.</param>
/// <returns>From information.</returns>
public static IEnumerable<string> GetGroup(this BikeInfoBase bikeInfo)
{
try
{
return bikeInfo?.bike_group?.GetGroup()?.ToList() ?? new List<string>();
}
catch (Exception l_oException)
{
throw new Exception($"Can not get group of user from text \"{bikeInfo.bike_group}\".", l_oException);
}
}
/// <summary> Gets whether the bike has a bord computer or not. </summary>
/// <param name="bikeInfo">JSON to get information from.</param>
/// <returns>From information.</returns>
public static bool GetIsManualLockBike(this BikeInfoBase bikeInfo)
{
return !string.IsNullOrEmpty(bikeInfo.system)
&& bikeInfo.system.ToUpper().StartsWith("LOCK");
}
/// <summary> Gets whether the bike has a bord computer or not. </summary>
/// <param name="bikeInfo">JSON to get information from..</param>
/// <returns>From information.</returns>
public static bool GetIsBluetoothLockBike(this BikeInfoBase bikeInfo)
{
return !string.IsNullOrEmpty(bikeInfo.system)
&& bikeInfo.system.ToUpper().StartsWith("ILOCKIT");
}
/// <summary> Gets whether the bike Is a Sigo bike or not. </summary>
/// <param name="bikeInfo">JSON to get information from..</param>
/// <returns>True if bike is a Sigo.</returns>
public static bool GetIsSigoBike(this BikeInfoBase bikeInfo)
{
return !string.IsNullOrEmpty(bikeInfo.system)
&& bikeInfo.system.ToUpper().StartsWith("SIGO");
}
public static LockModel? GetLockModel(this BikeInfoBase bikeInfo)
{
if (GetIsBluetoothLockBike(bikeInfo))
return LockModel.ILockIt;
if (GetIsManualLockBike(bikeInfo))
return LockModel.BordComputer;
if (GetIsSigoBike(bikeInfo))
return LockModel.Sigo;
return null;
}
/// <summary> Gets whether the bike has a bord computer or not. </summary>
/// <param name="bikeInfo">JSON to get information from..</param>
/// <returns>From information.</returns>
public static int GetBluetoothLockId(this BikeInfoBase bikeInfo)
{
return TextToLockItTypeHelper.GetBluetoothLockId(bikeInfo?.Ilockit_ID);
}
/// <summary> Gets whether the bike has a bord computer or not. </summary>
/// <param name="bikeInfo">JSON to get information from..</param>
/// <returns>From information.</returns>
public static Guid GetBluetoothLockGuid(this BikeInfoBase bikeInfo)
{
// return new Guid("00000000-0000-0000-0000-e57e6b9aee16");
return Guid.TryParse(bikeInfo?.Ilockit_GUID, out Guid lockGuid)
? lockGuid
: TextToLockItTypeHelper.INVALIDLOCKGUID;
}
public static byte[] GetUserKey(this BikeInfoReservedOrBooked bikeInfo)
{
return GetKey(bikeInfo.K_u);
}
public static byte[] GetAdminKey(this BikeInfoReservedOrBooked bikeInfo)
{
return GetKey(bikeInfo.K_a);
}
public static byte[] GetSeed(this BikeInfoReservedOrBooked bikeInfo)
{
return GetKey(bikeInfo.K_seed);
}
/// <summary>
/// Get array of keys from string of format "[12, -9, 5]"
/// </summary>
/// <param name="keyArrayText"></param>
/// <returns></returns>
private static byte[] GetKey(string keyArrayText)
{
try
{
if (string.IsNullOrEmpty(keyArrayText))
return new byte[0];
return Regex.Replace(keyArrayText, @"\[(.*)\]", "$1").Split(',').Select(x => (byte)sbyte.Parse(x)).ToArray();
}
catch (Exception exception)
{
Log.Error("Can not extract K_u/ K_a/ or K_seed. Key {ArrayText} does not is not of expected format. {Exception}", keyArrayText, exception);
return new byte[0];
}
}
/// <summary>
/// Gets whether the bike is a trike or not.
/// </summary>
/// <param name="bikeInfo">JSON to get information from..</param>
/// <returns>From information.</returns>
public static WheelType? GetWheelType(this BikeInfoBase bikeInfo)
=> Enum.TryParse(bikeInfo?.bike_type?.wheels, true, out WheelType wheelType)
? wheelType
: (WheelType?)null;
/// <summary>
/// Gets the type of bike.
/// </summary>
/// <param name="bikeInfo">Object to get bike type from.</param>
/// <returns>Type of bike.</returns>
public static TypeOfBike? GetTypeOfBike(this BikeInfoBase bikeInfo)
=> Enum.TryParse(bikeInfo?.bike_type?.category, true, out TypeOfBike typeOfBike)
? typeOfBike
: (TypeOfBike?)null;
/// <summary>
/// Gets whether bike is a AA bike (bike must be always returned a the same station) or AB bike (start and end stations can be different stations).
/// </summary>
/// <param name="bikeInfo">Object to get AA info from.</param>
/// <returns>AA info.</returns>
public static AaRideType? GetAaRideType(this BikeInfoBase bikeInfo)
=> Enum.TryParse(bikeInfo?.aa_ride, true, out AaRideType aaRide)
? aaRide
: (AaRideType?)null;
/// <summary> Get position from a ,- separated string. </summary>
/// <param name="gps">Text to extract position from.</param>
/// <returns>Position object.</returns>
public static IPosition GetPosition(Repository.Response.Position gps)
=> PositionFactory.Create(
double.TryParse(gps?.latitude, NumberStyles.Float, CultureInfo.InvariantCulture, out double latitude) ? latitude : double.NaN,
double.TryParse(gps?.longitude, NumberStyles.Float, CultureInfo.InvariantCulture, out double longitude) ? longitude : double.NaN);
/// <summary> Get position from a ,- separated string. </summary>
/// <param name="gps">Text to extract position from.</param>
/// <returns>Position object.</returns>
public static Map.IMapSpan GetMapSpan(this MapSpan mapSpan)
=> Map.MapSpanFactory.Create(
GetPosition(mapSpan?.center),
double.TryParse(mapSpan?.radius, NumberStyles.Float, CultureInfo.InvariantCulture, out double radius) ? radius : double.NaN);
/// <summary>
/// Gets the locking state from response.
/// </summary>
/// <param name="bikeInfo"> Response locking state from.</param>
/// <returns>Locking state</returns>
public static Bikes.BikeInfoNS.CopriLock.LockingState GetCopriLockingState(this BikeInfoBase bikeInfo)
{
if (string.IsNullOrEmpty(bikeInfo?.lock_state))
return Bikes.BikeInfoNS.CopriLock.LockingState.UnknownDisconnected;
if (bikeInfo.lock_state.ToUpper().Trim() == "locked".ToUpper())
return Bikes.BikeInfoNS.CopriLock.LockingState.Closed;
if (bikeInfo.lock_state.ToUpper().Trim() == "locking".ToUpper())
return Bikes.BikeInfoNS.CopriLock.LockingState.Closing;
if (bikeInfo.lock_state.ToUpper().Trim() == "unlocked".ToUpper())
return Bikes.BikeInfoNS.CopriLock.LockingState.Open;
if (bikeInfo.lock_state.ToUpper().Trim() == "unlocking".ToUpper())
return Bikes.BikeInfoNS.CopriLock.LockingState.Opening;
return Bikes.BikeInfoNS.CopriLock.LockingState.UnknownDisconnected;
}
/// <summary>
/// Gets the operator Uri from response.
/// </summary>
/// <param name="bikeInfo"> Response to get uri from.</param>
/// <returns>Operator Uri</returns>
public static Uri GetOperatorUri(this BikeInfoBase bikeInfo)
{
if (Uri.TryCreate(bikeInfo?.uri_operator, UriKind.Absolute, out var operatorUri))
{
// Valid uri detected.
return new Uri($"{operatorUri.AbsoluteUri}/{CopriServerUriList.REST_RESOURCE_ROOT}");
}
Log.Error(!string.IsNullOrEmpty(bikeInfo?.uri_operator)
? $"Operator uri can not be extracted from bike info base object {bikeInfo.uri_operator}. Uri is not valid."
: "Operator uri can not be extracted from bike info base object. Entry is null or empty.");
return null;
}
/// <summary>
/// Gets the operator Uri from response.
/// </summary>
/// <param name="stationInfo"> Response to get uri from.</param>
/// <returns>Operator Uri</returns>
public static Uri GetOperatorUri(this StationInfo stationInfo)
{
if (Uri.TryCreate(stationInfo?.uri_operator, UriKind.Absolute, out var operatorUri))
{
// Valid uri detected.
return new Uri($"{operatorUri.AbsoluteUri}/{CopriServerUriList.REST_RESOURCE_ROOT}");
}
Log.Error(!string.IsNullOrEmpty(stationInfo?.uri_operator)
? $"Operator uri can not be extracted from station object {stationInfo.uri_operator}. Uri is not valid."
: "Operator uri can not be extracted from station object. Entry is null or empty.");
return null;
}
/// <summary> Tries to get the copri version from response.</summary>
/// <param name="response">Response to get version info from.</param>
/// <returns>COPRI version</returns>
public static bool TryGetCopriVersion(this CopriVersion response, out Version copriVersion)
{
copriVersion = new Version(0, 0);
return response != null
&& !string.IsNullOrEmpty(response.copri_version)
&& Version.TryParse(response.copri_version, out copriVersion);
}
/// <summary> Gets the copri version from.</summary>
/// <param name="response">Response to get version info from.</param>
/// <returns>COPRI version</returns>
public static Version GetCopriVersion(this CopriVersion response)
=> response.TryGetCopriVersion(out Version copriVersion)
? copriVersion
: throw new InvalidResponseException($"Can not get version info from copri response {response?.copri_version}.");
/// <summary>
/// Gets bike advanced bike state. If entry Co2Saving exists feedback is required.
/// </summary>
/// <param name="bike">Bike get to state from.</param>
public static bool GetIsFeedbackPending(this BikeInfoAvailable bike)
=> bike.co2saving != null;
/// <summary>
/// Gets the count of bikes available at station.
/// </summary>
/// <param name="stationInfo">Object to get information from.</param>
/// <returns>Count of bikes available or null if information is unknown.</returns>
public static int? GetBikesAvailableCount(this StationInfo stationInfo)
=> TryGetBikesAvailableCount(stationInfo, out int bikeCount)
? bikeCount
: (int?)null;
public static bool TryGetBikesAvailableCount(this StationInfo stationInfo, out int bikesCount)
=> int.TryParse(stationInfo?.bike_count, out bikesCount);
public static void SetBikesAvailableCount(this StationInfo stationInfo, int bikesCount)
=> stationInfo.bike_count = bikesCount.ToString(CultureInfo.InvariantCulture);
/// <summary>
/// Gets station object from response object.
/// </summary>
/// <param name="station">Response object to get station object from.</param>
/// <returns>Station object.</returns>
public static Station GetStation(this StationInfo station) =>
new Station(
station.station,
station.GetGroup(),
station.GetPosition(),
station.description,
station.GetOperatorUri(),
new Data(station.operator_data?.operator_name,
station.operator_data?.operator_phone,
station.operator_data?.operator_hours,
station.operator_data?.operator_email,
!string.IsNullOrEmpty(station.operator_data?.operator_color)
? Color.FromHex(station.operator_data?.operator_color)
: (Color?)null),
new BikeGroupCol(station.station_type?.Select(x => new BikeGroupCol.Entry(
x.Key,
int.TryParse(x.Value.bike_count, out int count) ? count : 0,
x.Value.bike_group)) ?? new List<BikeGroupCol.Entry>()
));
/// <summary>
/// Gets the bike group object from response object.
/// </summary>
/// <param name="bikeGroup">Response object to get station from.</param>
/// <param name="name">Name of the bike group.</param>
/// <returns></returns>
public static BikeGroupCol.Entry GetBikeGroup(this BikeGroup bikeGroup, string name) =>
new BikeGroupCol.Entry(
name,
int.TryParse(bikeGroup?.bike_count ?? "0", out var countCity) ? countCity : 0,
bikeGroup?.bike_group ?? string.Empty);
/// <summary>
/// Default value for reserve_timerange.
/// </summary>
private static int DEFAULTMAXRESERVATIONTIMESPAN = 15;
/// <summary>
/// Gets the reservation time span from response.
/// </summary>
/// <param name="description">Response to get time span from.</param>
/// <returns>Time span.</returns>
public static TimeSpan GetMaxReservationTimeSpan(this RentalDescription description) =>
TimeSpan.FromMinutes(int.TryParse(description?.reserve_timerange, out int minutes)
? minutes
: DEFAULTMAXRESERVATIONTIMESPAN );
}
}

View file

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Text;
using ShareeBike.Model.Bikes.BikeInfoNS.BC;
using ShareeBike.Model.Bikes;
using ShareeBike.Repository.Response;
using Serilog;
namespace ShareeBike.Model.Connector.Updater
{
public static class BikeCollectionFactory
{
/// <summary> Gets bikes available from copri server response.</summary>
/// <param name="bikesAvailableResponse">Response to create collection from.</param>
/// <param name="dataSource">Specified the data source</param>
/// <returns>New collection of available bikes.</returns>
public static BikeCollection GetBikesAvailable(
this BikesAvailableResponse bikesAvailableResponse,
DataSource dataSource)
=> GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesAvailableResponse?.bikes_occupied?.Values,
string.Empty,
() => DateTime.Now,
dataSource);
/// <summary> Gets bikes occupied from copri server response. </summary>
/// <param name="bikesOccupiedResponse">Response to create bikes from.</param>
/// <param name="dataSource">Specified the data source</param>
/// <returns>New collection of occupied bikes.</returns>
public static BikeCollection GetBikesOccupied(
this BikesReservedOccupiedResponse bikesOccupiedResponse,
string mail,
Func<DateTime> dateTimeProvider,
DataSource dataSource)
=> GetBikesAll(
new BikesAvailableResponse()?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
mail,
dateTimeProvider,
dataSource);
/// <summary> Gets bikes occupied from copri server response. </summary>
/// <param name="bikesAvailable">Response to create bikes available from.</param>
/// <param name="bikesOccupied">Response to create bikes occupied from.</param>
/// <returns>New collection of occupied bikes.</returns>
public static BikeCollection GetBikesAll(
IEnumerable<BikeInfoAvailable> bikesAvailable,
IEnumerable<BikeInfoReservedOrBooked> bikesOccupied,
string mail,
Func<DateTime> dateTimeProvider,
DataSource dataSource)
{
var bikesDictionary = new Dictionary<string, BikeInfo>();
var duplicates = new Dictionary<string, BikeInfo>();
// Get bikes from Copri/ file/ memory, ....
if (bikesAvailable != null)
{
foreach (var bikeInfoResponse in bikesAvailable)
{
var bikeInfo = BikeInfoFactory.Create(bikeInfoResponse, dataSource);
if (bikeInfo == null)
{
// Response is not valid.
continue;
}
if (bikesDictionary.ContainsKey(bikeInfo.Id))
{
// Duplicates are not allowed.
Log.Error($"Duplicate bike with id {bikeInfo.Id} detected evaluating bikes available. Bike status is {bikeInfo.State.Value}.");
if (!duplicates.ContainsKey(bikeInfo.Id))
{
duplicates.Add(bikeInfo.Id, bikeInfo);
}
continue;
}
bikesDictionary.Add(bikeInfo.Id, bikeInfo);
}
}
// Get bikes from Copri/ file/ memory, ....
if (bikesOccupied != null)
{
foreach (var bikeInfoResponse in bikesOccupied)
{
BikeInfo bikeInfo = BikeInfoFactory.Create(
bikeInfoResponse,
mail,
dateTimeProvider,
dataSource);
if (bikeInfo == null)
{
continue;
}
if (bikesDictionary.ContainsKey(bikeInfo.Id))
{
// Duplicates are not allowed.
Log.Error($"Duplicate bike with id {bikeInfo.Id} detected evaluating bikes occupied. Bike status is {bikeInfo.State.Value}.");
if (!duplicates.ContainsKey(bikeInfo.Id))
{
duplicates.Add(bikeInfo.Id, bikeInfo);
}
continue;
}
bikesDictionary.Add(bikeInfo.Id, bikeInfo);
}
}
// Remove entries which are not unique.
foreach (var l_oDuplicate in duplicates)
{
bikesDictionary.Remove(l_oDuplicate.Key);
}
return new BikeCollection(bikesDictionary);
}
}
}

View file

@ -0,0 +1,303 @@
using System;
using System.Collections.Generic;
using Serilog;
using ShareeBike.Model.Bikes.BikeInfoNS.BC;
using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS;
using ShareeBike.Model.MiniSurvey;
using ShareeBike.Model.State;
using ShareeBike.Repository.Response;
using BikeExtension = ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.BikeExtension;
namespace ShareeBike.Model.Connector.Updater
{
/// <summary>
/// Constructs bike info instances/ bike info derived instances.
/// </summary>
public static class BikeInfoFactory
{
/// <summary> Set default lock type to . </summary>
public static LockModel DEFAULTLOCKMODEL = LockModel.Sigo;
/// <summary> Creates a bike info object from copri response. </summary>
/// <param name="bikeInfo">Copri response for a disposable bike. </param>
/// <param name="dataSource">Specifies the data source.</param>
public static BikeInfo Create(
BikeInfoAvailable bikeInfo,
DataSource dataSource)
{
if (bikeInfo == null) throw new ArgumentNullException(nameof(bikeInfo));
var lockModel = bikeInfo.GetLockModel();
if (lockModel.HasValue
&& lockModel.Value == LockModel.BordComputer)
{
// Manual lock bikes are no more supported.
Log.Error(
$"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoAvailable)} argument. " +
"Manual lock bikes are no more supported." +
$"Bike number: {bikeInfo.bike}{(bikeInfo.station != null ? $"station number {bikeInfo.station}" : string.Empty)}."
);
return null;
}
switch (bikeInfo.GetState())
{
case InUseStateEnum.Disposable:
case InUseStateEnum.FeedbackPending:
break;
default:
Log.Error($"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoAvailable)} argument. Unexpected state {bikeInfo.GetState()} detected.");
return null;
}
if (string.IsNullOrEmpty(bikeInfo.station))
{
// Bike available must always have a station id because bikes can only be returned at a station.
Log.Error($"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoAvailable)} argument. No station info set.");
return null;
}
var lockType = lockModel.HasValue
? BikeExtension.GetLockType(lockModel.Value)
: BikeExtension.GetLockType(DEFAULTLOCKMODEL); // Map bikes without "system"- entry in response to back end- locks.
try
{
switch (lockType)
{
case LockType.Backend:
return new Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike(
bikeInfo.bike,
LockModel.Sigo,
bikeInfo.GetWheelType(),
bikeInfo.GetTypeOfBike(),
bikeInfo.GetAaRideType(),
bikeInfo.description),
DriveFactory.Create(bikeInfo?.bike_type),
dataSource,
bikeInfo.station,
new Bikes.BikeInfoNS.CopriLock.LockInfo.Builder { State = bikeInfo.GetCopriLockingState() }.Build(),
bikeInfo.GetState() == InUseStateEnum.FeedbackPending,
bikeInfo.GetOperatorUri(),
bikeInfo.rental_description != null
? RentalDescriptionFactory.Create(bikeInfo.rental_description)
: TariffDescriptionFactory.Create(bikeInfo.tariff_description),
bikeInfo.GetIsDemo(),
bikeInfo.GetGroup(),
miniSurvey: bikeInfo.user_miniquery != null
? new MiniSurveyModel(new Dictionary<string, IQuestionModel> {
{ "q1", new QuestionModel()} // Add a dummy query. Queries are not yet read from COPRI but compiled into the app.
})
: new MiniSurveyModel(),
co2Saving: bikeInfo.co2saving);
case LockType.Bluethooth:
return new Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new Bike(
bikeInfo.bike,
LockModel.ILockIt,
bikeInfo.GetWheelType(),
bikeInfo.GetTypeOfBike(),
bikeInfo.GetAaRideType(),
bikeInfo.description),
DriveFactory.Create(bikeInfo?.bike_type),
dataSource,
bikeInfo.GetBluetoothLockId(),
bikeInfo.GetBluetoothLockGuid(),
bikeInfo.station,
bikeInfo.GetOperatorUri(),
bikeInfo.rental_description != null
? RentalDescriptionFactory.Create(bikeInfo.rental_description)
: TariffDescriptionFactory.Create(bikeInfo.tariff_description),
bikeInfo.GetIsDemo(),
bikeInfo.GetGroup());
default:
throw new ArgumentException($"Unsupported lock type {lockType} detected.");
}
}
catch (ArgumentException ex)
{
// Constructor reported invalid arguments (missing lock id, ....).
Log.Error($"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoAvailable)} argument. Invalid response detected. Available bike with id {bikeInfo.bike} skipped. {ex.Message}");
return null;
}
}
/// <summary> Creates a bike info object from copri response. </summary>
/// <param name="bikeInfo">Copri response. </param>
/// <param name="mailAddress">Mail address of user.</param>
/// <param name="dateTimeProvider">Date and time provider function.</param>
/// <param name="dataSource">Specified the source of the data.</param>
public static BikeInfo Create(
BikeInfoReservedOrBooked bikeInfo,
string mailAddress,
Func<DateTime> dateTimeProvider,
DataSource dataSource)
{
if (bikeInfo == null) throw new ArgumentNullException(nameof(bikeInfo));
var lockModel = bikeInfo.GetLockModel();
if (lockModel.HasValue
&& lockModel.Value == LockModel.BordComputer)
{
// Manual lock bikes are no more supported.
Log.Error(
$"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoAvailable)} argument. " +
"Manual lock bikes are no more supported." +
$"Bike number: {bikeInfo.bike}{(bikeInfo.station != null ? $", station number {bikeInfo.station}" : string.Empty)}."
);
return null;
}
var lockType = lockModel.HasValue
? BikeExtension.GetLockType(lockModel.Value)
: BikeExtension.GetLockType(DEFAULTLOCKMODEL); // Map bikes without "system"- entry in response to backend- locks.
// Check if bike is a bluetooth lock bike.
int lockSerial = bikeInfo.GetBluetoothLockId();
Guid lockGuid = bikeInfo.GetBluetoothLockGuid();
switch (bikeInfo.GetState())
{
case InUseStateEnum.Reserved:
try
{
switch (lockType)
{
case LockType.Bluethooth:
return new Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new Bike(
bikeInfo.bike,
LockModel.ILockIt,
bikeInfo.GetWheelType(),
bikeInfo.GetTypeOfBike(),
bikeInfo.GetAaRideType(),
bikeInfo.description),
DriveFactory.Create(bikeInfo?.bike_type),
dataSource,
lockSerial,
lockGuid,
bikeInfo.GetUserKey(),
bikeInfo.GetAdminKey(),
bikeInfo.GetSeed(),
bikeInfo.GetFrom(),
mailAddress,
bikeInfo.station,
bikeInfo.GetOperatorUri(),
bikeInfo.rental_description != null
? RentalDescriptionFactory.Create(bikeInfo.rental_description)
: TariffDescriptionFactory.Create(bikeInfo.tariff_description),
dateTimeProvider,
bikeInfo.GetIsDemo(),
bikeInfo.GetGroup());
case LockType.Backend:
return new Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike(
bikeInfo.bike,
LockModel.Sigo,
bikeInfo.GetWheelType(),
bikeInfo.GetTypeOfBike(),
bikeInfo.GetAaRideType(),
bikeInfo.description),
DriveFactory.Create(bikeInfo?.bike_type),
dataSource,
bikeInfo.GetFrom(),
mailAddress,
bikeInfo.station,
new Bikes.BikeInfoNS.CopriLock.LockInfo.Builder { State = bikeInfo.GetCopriLockingState() }.Build(),
bikeInfo.GetOperatorUri(),
bikeInfo.rental_description != null
? RentalDescriptionFactory.Create(bikeInfo.rental_description)
: TariffDescriptionFactory.Create(bikeInfo.tariff_description),
dateTimeProvider,
bikeInfo.GetIsDemo(),
bikeInfo.GetGroup());
default:
throw new ArgumentException($"Unsupported lock type {lockType} detected.");
}
}
catch (ArgumentException ex)
{
// Constructor reported invalid arguments (missing lock id, ....).
Log.Error($"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoReservedOrBooked)} argument. Invalid response detected. Reserved bike with id {bikeInfo.bike} skipped. {ex.Message}");
return null;
}
case InUseStateEnum.Booked:
try
{
switch (lockModel)
{
case LockModel.ILockIt:
return new Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new Bike(
bikeInfo.bike,
LockModel.ILockIt,
bikeInfo.GetWheelType(),
bikeInfo.GetTypeOfBike(),
bikeInfo.GetAaRideType(),
bikeInfo.description),
DriveFactory.Create(bikeInfo?.bike_type),
dataSource,
lockSerial,
bikeInfo.GetBluetoothLockGuid(),
bikeInfo.GetUserKey(),
bikeInfo.GetAdminKey(),
bikeInfo.GetSeed(),
bikeInfo.GetFrom(),
mailAddress,
bikeInfo.station,
bikeInfo.GetOperatorUri(),
bikeInfo.rental_description != null
? RentalDescriptionFactory.Create(bikeInfo.rental_description)
: TariffDescriptionFactory.Create(bikeInfo.tariff_description),
bikeInfo.GetIsDemo(),
bikeInfo.GetGroup());
case LockModel.BordComputer:
throw new NotSupportedException($"Bikes with lock model of type {lockModel} are no more supported.");
default:
return new Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike(
bikeInfo.bike,
LockModel.Sigo,
bikeInfo.GetWheelType(),
bikeInfo.GetTypeOfBike(),
bikeInfo.GetAaRideType(),
bikeInfo.description),
DriveFactory.Create(bikeInfo?.bike_type),
DataSource.Copri,
bikeInfo.GetFrom(),
mailAddress,
bikeInfo.station,
new Bikes.BikeInfoNS.CopriLock.LockInfo.Builder { State = bikeInfo.GetCopriLockingState() }.Build(),
bikeInfo.GetOperatorUri(),
bikeInfo.rental_description != null
? RentalDescriptionFactory.Create(bikeInfo.rental_description)
: TariffDescriptionFactory.Create(bikeInfo.tariff_description),
bikeInfo.GetIsDemo(),
bikeInfo.GetGroup());
}
}
catch (ArgumentException ex)
{
// Constructor reported invalid arguments (missing lock id, ....).
Log.Error($"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoReservedOrBooked)} argument. Invalid response detected. Booked bike with id {bikeInfo.bike} skipped. {ex.Message}");
return null;
}
default:
Log.Error($"Can not create new {nameof(BikeInfo)}-object from {nameof(BikeInfoAvailable)} argument. Unexpected state {bikeInfo.GetState()} detected.");
return null;
}
}
}
}

View file

@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using ShareeBike.Model.MiniSurvey;
using ShareeBike.Repository.Response;
namespace ShareeBike.Model.Connector.Updater
{
public static class BookingFinishedModelFactory
{
/// <summary> Creates a booking finished object from response.</summary>
/// <param name="response">Response to create survey object from.</param>
public static BookingFinishedModel Create(this DoReturnResponse response)
{
var bookingFinished = new BookingFinishedModel
{
Co2Saving = response?.bike_returned.co2saving,
RentalCosts = response?.bike_returned.total_price,
Duration = response?.bike_returned.real_clock,
Distance = response?.bike_returned.distance,
};
if (response?.user_miniquery == null)
{
return bookingFinished;
}
var miniquery = response.user_miniquery;
bookingFinished.MiniSurvey = new MiniSurveyModel
{
Title = miniquery.title,
Subtitle = miniquery.subtitle,
Footer = miniquery.footer
};
foreach (var question in miniquery?.questions?.OrderBy(x => x.Key) ?? new Dictionary<string, MiniSurveyResponse.Question>().OrderBy(x => x.Key))
{
if (string.IsNullOrEmpty(question.Key.Trim())
|| question.Value.query == null)
{
// Skip invalid entries.
continue;
}
bookingFinished.MiniSurvey.Questions.Add(
question.Key,
new QuestionModel());
}
return bookingFinished;
}
}
}

View file

@ -0,0 +1,45 @@
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS;
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS.BatteryNS;
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS.EngineNS;
using ShareeBike.Repository.Response;
namespace ShareeBike.Model.Connector.Updater
{
public static class DriveFactory
{
public static DriveMutable Create(this BikeType bikeType)
{
if (string.IsNullOrEmpty(bikeType?.engine?.manufacturer))
{
// Bike is has no engine
return new DriveMutable();
}
// Bike is a pedelec.
return new DriveMutable(
new Engine(bikeType?.engine?.manufacturer),
new Battery.Builder
{
CurrentChargePercent = double.TryParse(bikeType?.battery?.charge_current_percent, out double currentChargePercent)
? currentChargePercent
: double.NaN,
CurrentChargeBars = int.TryParse(bikeType?.battery?.charge_current_bars, out int currentChargeBars)
? (int?)currentChargeBars
: null,
MaxChargeBars = int.TryParse(bikeType?.battery?.charge_max_bars, out int maxChargeBars)
? (int?)maxChargeBars
: null,
IsBackendAccessible = bikeType?.battery?.backend_accessible != null && int.TryParse(bikeType.battery.backend_accessible, out int accessible)
? (bool?)(accessible > 0)
: null,
IsHidden = bikeType?.battery?.hidden != null && int.TryParse(bikeType.battery.hidden, out int hidden)
? (bool?)(hidden > 0)
: null
}.Build());
}
}
}

View file

@ -0,0 +1,61 @@
using System.Collections.Generic;
using System.Linq;
using ShareeBike.Model.Bikes.BikeInfoNS;
namespace ShareeBike.Model.Connector.Updater
{
public static class RentalDescriptionFactory
{
/// <summary>
/// Creates rental description object from JSON- tariff description object.
/// </summary>
/// <param name="rentalDesciption">Source JSON object.</param>
/// <returns>Tariff description object.</returns>
public static RentalDescription Create(this Repository.Response.RentalDescription rentalDesciption)
{
RentalDescription.TariffElement CreateTarifEntry(string[] elementValue) =>
new RentalDescription.TariffElement
{
Description = elementValue != null && elementValue.Length > 0 ? elementValue[0] : string.Empty,
Value = elementValue != null && elementValue.Length > 1 ? elementValue[1] : string.Empty,
};
RentalDescription.InfoElement CreateInfoElement(string[] elementValue) =>
new RentalDescription.InfoElement
{
Key = elementValue != null && elementValue.Length > 0 ? elementValue[0] : string.Empty,
Value = elementValue != null && elementValue.Length > 1 ? elementValue[1] : string.Empty,
};
// Read tariff elements.
var tarifEntries = rentalDesciption?.tarif_elements != null
? rentalDesciption.tarif_elements.Select(x => new
{
x.Key,
Value = CreateTarifEntry(x.Value)
}).ToLookup(x => x.Key, x => x.Value).ToDictionary(x => x.Key, x => x.First())
: new Dictionary<string, RentalDescription.TariffElement>();
// Read info elements.
var InfoEntries = rentalDesciption?.rental_info != null
? rentalDesciption.rental_info.Select(x => new
{
x.Key,
Value = CreateInfoElement(x.Value)
}).ToLookup(x => x.Key, x => x.Value).ToDictionary(x => x.Key, x => x.First())
: new Dictionary<string, RentalDescription.InfoElement>();
var bike = new RentalDescription
{
Name = rentalDesciption?.name ?? string.Empty,
Id = int.TryParse(rentalDesciption?.id ?? string.Empty, out int number) ? number : (int?)null,
MaxReservationTimeSpan = rentalDesciption.GetMaxReservationTimeSpan(),
TariffEntries = tarifEntries,
InfoEntries = InfoEntries
};
return bike;
}
}
}

View file

@ -0,0 +1,74 @@
using System.Globalization;
using ShareeBike.MultilingualResources;
using ShareeBike.Repository.Response;
namespace ShareeBike.Model.Connector.Updater
{
public static class TariffDescriptionFactory
{
/// <summary>
/// Creates rental description object from JSON- tarif description object.
/// </summary>
/// <param name="tariffDesciption">Source JSON object.</param>
/// <returns>Tariff description object.</returns>
public static Bikes.BikeInfoNS.RentalDescription Create(this TariffDescription tariffDesciption)
{
var bike = new Bikes.BikeInfoNS.RentalDescription
{
Name = tariffDesciption?.name,
#if USCSHARP9
Number = int.TryParse(tariffDesciption?.number, out int number) ? number : null,
#else
Id = int.TryParse(tariffDesciption?.number, out int number) ? number : (int?)null,
#endif
};
if (!string.IsNullOrEmpty(tariffDesciption?.free_hours)
&& double.TryParse(tariffDesciption?.free_hours, NumberStyles.Any, CultureInfo.InvariantCulture, out double freeHours))
{
// Free time. Unit hours,format floating point number.
bike.TariffEntries.Add("1", new Bikes.BikeInfoNS.RentalDescription.TariffElement
{
Description = AppResources.MessageBikesManagementTariffDescriptionFreeTimePerSession,
Value = string.Format("{0} {1}", freeHours.ToString("0.00"), AppResources.MessageBikesManagementTariffDescriptionHour)
});
}
if (!string.IsNullOrEmpty(tariffDesciption?.eur_per_hour)
&& double.TryParse(tariffDesciption?.eur_per_hour, NumberStyles.Any, CultureInfo.InvariantCulture, out double euroPerHour))
{
// Euro per hour. Format floating point.
bike.TariffEntries.Add("2", new Bikes.BikeInfoNS.RentalDescription.TariffElement
{
Description = AppResources.MessageBikesManagementTariffDescriptionFeeEuroPerHour,
Value = string.Format("{0} {1}", euroPerHour.ToString("0.00"), AppResources.MessageBikesManagementTariffDescriptionEuroPerHour)
});
}
if (!string.IsNullOrEmpty(tariffDesciption?.max_eur_per_day)
&& double.TryParse(tariffDesciption.max_eur_per_day, NumberStyles.Any, CultureInfo.InvariantCulture, out double maxEuroPerDay))
{
// Max euro per day. Format floating point.
bike.TariffEntries.Add("3", new Bikes.BikeInfoNS.RentalDescription.TariffElement
{
Description = AppResources.MessageBikesManagementTariffDescriptionMaxFeeEuroPerDay,
Value = string.Format("{0} {1}", maxEuroPerDay.ToString("0.00"), AppResources.MessageBikesManagementMaxFeeEuroPerDay)
});
}
if (!string.IsNullOrEmpty(tariffDesciption?.abo_eur_per_month)
&& double.TryParse(tariffDesciption.abo_eur_per_month, NumberStyles.Any, CultureInfo.InvariantCulture, out double aboEuroPerMonth))
{
// Abo per month
bike.TariffEntries.Add("4", new Bikes.BikeInfoNS.RentalDescription.TariffElement
{
Description = AppResources.MessageBikesManagementTariffDescriptionAboEuroPerMonth,
Value = string.Format("{0} {1}", aboEuroPerMonth.ToString("0.00"), AppResources.MessageBikesManagementTariffDescriptionEuroPerMonth)
});
}
return bike;
}
}
}

View file

@ -0,0 +1,149 @@
using System;
using ShareeBike.Model.Bikes.BikeInfoNS.BC;
using ShareeBike.Model.State;
using ShareeBike.Model.Stations;
using ShareeBike.Model.User.Account;
using ShareeBike.Repository.Exception;
using ShareeBike.Repository.Response;
using ShareeBike.Repository.Response.Stations;
using ShareeBike.Services.CopriApi;
using IBikeInfoMutable = ShareeBike.Model.Bikes.BikeInfoNS.BC.IBikeInfoMutable;
namespace ShareeBike.Model.Connector.Updater
{
/// <summary>
/// Connects ShareeBike app to copri using JSON as input data format.
/// </summary>
/// <todo>Rename to UpdateFromCopri.</todo>
public static class UpdaterJSON
{
/// <summary>
/// Gets all station for station provider and add them into station list.
/// </summary>
/// <param name="stationsAllResponse">List of stations to update.</param>
public static StationDictionary GetStationsAllMutable(this StationsAvailableResponse stationsAllResponse)
{
// Get stations from Copri/ file/ memory, ....
if (stationsAllResponse == null
|| stationsAllResponse.stations == null)
{
// Latest list of stations could not be retrieved from provider.
return new StationDictionary();
}
Version.TryParse(stationsAllResponse.copri_version, out Version copriVersion);
var stations = new StationDictionary(version: copriVersion);
foreach (var station in stationsAllResponse.stations)
{
if (stations.GetById(station.Value.station) != null)
{
// Can not add station to list of station. Id is not unique.
throw new InvalidResponseException<StationsAvailableResponse>(
string.Format("Station id {0} is not unique.", station.Value.station), stationsAllResponse);
}
stations.Add(station.Value.GetStation());
}
return stations;
}
/// <summary>
/// Gets general data from COPRI response.
/// </summary>
/// <param name="response">Response to get data from.</param>
/// <returns>General data object initialized form COPRI response.</returns>
public static GeneralData GetGeneralData(this ResponseBase response)
=> new GeneralData(
response.init_map.GetMapSpan(),
response.merchant_message,
response.TryGetCopriVersion(out Version copriVersion)
? new Version(0, 0)
: copriVersion,
new ResourceUrls(response.tariff_info_html, response.bike_info_html, response.agb_html, response.privacy_html, response.impress_html));
/// <summary> Gets account object from login response.</summary>
/// <param name="merchantId">Needed to extract cookie from authorization response.</param>
/// <param name="loginResponse">Response to get session cookie and debug level from.</param>
/// <param name="mail">Mail address needed to construct a complete account object (is not part of response).</param>
/// <param name="password">Password needed to construct a complete account object (is not part of response).</param>
public static IAccount GetAccount(
this AuthorizationResponse loginResponse,
string merchantId,
string mail,
string password)
{
if (loginResponse == null)
{
throw new ArgumentNullException(nameof(loginResponse));
}
return new Account(
mail,
password,
loginResponse.GetIsAgbAcknowledged(),
loginResponse.authcookie?.Replace(merchantId, ""),
loginResponse.GetGroup(),
loginResponse.debuglevel == 1
? Permissions.All :
(Permissions)loginResponse.debuglevel);
}
/// <summary> Load bike object from booking response. </summary>
/// <param name="bike">Bike object to load from response.</param>
/// <param name="bikeInfo">Booking response.</param>
/// <param name="mailAddress">Mail address of user which books bike.</param>
/// <param name="notifyLevel">Controls whether notify property changed events are fired or not.</param>
public static void Load(
this IBikeInfoMutable bike,
BikeInfoReservedOrBooked bikeInfo,
string mailAddress,
NotifyPropertyChangedLevel notifyLevel = NotifyPropertyChangedLevel.All)
{
if (bike is Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable btBikeInfo)
{
btBikeInfo.LockInfo.Load(
bikeInfo.GetBluetoothLockId(),
bikeInfo.GetBluetoothLockGuid(),
bikeInfo.GetSeed(),
bikeInfo.GetUserKey(),
bikeInfo.GetAdminKey());
}
var l_oState = bikeInfo.GetState();
switch (l_oState)
{
case InUseStateEnum.Disposable:
bike.State.Load(
InUseStateEnum.Disposable,
notifyLevel: notifyLevel);
break;
case InUseStateEnum.Reserved:
bike.State.Load(
InUseStateEnum.Reserved,
bikeInfo.GetFrom(),
bikeInfo.rental_description.GetMaxReservationTimeSpan(),
mailAddress,
bikeInfo.timeCode,
notifyLevel);
break;
case InUseStateEnum.Booked:
bike.State.Load(
InUseStateEnum.Booked,
bikeInfo.GetFrom(),
mailAddress: mailAddress,
code: bikeInfo.timeCode,
notifyLevel: notifyLevel);
break;
default:
throw new Exception(string.Format("Unexpected bike state detected. state is {0}.", l_oState));
}
}
}
}