mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-06-21 21:46:27 +02:00
Version 3.0.381
This commit is contained in:
parent
f963c0a219
commit
3a363acf3a
1525 changed files with 60589 additions and 125098 deletions
177
SharedBusinessLogic/Repository/Request/IRequestBuilder.cs
Normal file
177
SharedBusinessLogic/Repository/Request/IRequestBuilder.cs
Normal file
|
@ -0,0 +1,177 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using ShareeBike.Model.Connector;
|
||||
|
||||
namespace ShareeBike.Repository.Request
|
||||
{
|
||||
/// <summary> Defines members to create requests.</summary>
|
||||
public interface IRequestBuilder
|
||||
{
|
||||
/// <summary> Holds the id denoting the merchant (ShareeBike app). </summary>
|
||||
string MerchantId { get; }
|
||||
|
||||
/// <summary> Gets the session cookie if user is logged in, an empty string otherwise. </summary>
|
||||
string SessionCookie { get; }
|
||||
|
||||
/// <summary> Gets request to log user in. </summary>
|
||||
/// <param name="mailAddress">Mail address of user to log in.</param>
|
||||
/// <param name="password">Password to log in.</param>
|
||||
/// <param name="deviceId">Id specifying user and hardware.</param>
|
||||
/// <remarks>Request which holds auth cookie <see cref="RequstBase.authcookie"/></remarks>
|
||||
string DoAuthorization(
|
||||
string mailAddress,
|
||||
string password,
|
||||
string deviceId);
|
||||
|
||||
/// <summary> Logs user out. </summary>
|
||||
/// <param name="merchantId">Id of the merchant.</param>
|
||||
/// <param name="p_strSessionCookie"> Cookie which identifies user.</param>
|
||||
string DoAuthout();
|
||||
|
||||
/// <summary> Get list of stations from file. </summary>
|
||||
/// <returns>Request to query list of station.</returns>
|
||||
string GetStations();
|
||||
|
||||
/// <summary>Gets bikes available.</summary>
|
||||
/// <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 to get.</param>
|
||||
/// <returns>Request to query list of bikes available.</returns>
|
||||
string GetBikesAvailable(string stationId = null, string bikeId = null);
|
||||
|
||||
/// <summary> Gets a list of bikes reserved/ booked by active user from Copri.</summary>
|
||||
/// <returns>Request to query list of bikes occupied.</returns>
|
||||
string GetBikesOccupied();
|
||||
|
||||
/// <summary> Gets reservation request (synonym: reservation == request == reservieren). </summary>
|
||||
/// <param name="bikeId">Id of the bike to reserve.</param>
|
||||
/// <returns>Request to reserve bike.</returns>
|
||||
string DoReserve(string bikeId);
|
||||
|
||||
/// <summary> Gets request to cancel reservation. </summary>
|
||||
/// <param name="bikeId">Id of the bike to cancel reservation for.</param>
|
||||
/// <returns>Request on cancel booking request.</returns>
|
||||
string DoCancelReservation(string bikeId);
|
||||
|
||||
/// <summary> Request to get keys. </summary>
|
||||
/// <param name="bikeId">Id of the bike to get keys for.</param>
|
||||
/// <returns>Request to get keys.</returns>
|
||||
string CalculateAuthParameters(string bikeId);
|
||||
|
||||
/// <summary> Gets the request for notifying about start of returning sequence. </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <returns>Request to notify about start of returning sequence.</returns>
|
||||
string StartReturningBike(string bikeId);
|
||||
|
||||
/// <summary> Gets the request for updating lock state for a booked bike. </summary>
|
||||
/// <param name="bikeId">Id of the bike to update locking state for.</param>
|
||||
/// <param name="state">New locking state.</param>
|
||||
/// <param name="location">Geolocation of lock when state change occurred.</param>
|
||||
/// <param name="versionInfo">Information about lock (firmware version, hardware version, ...).</param>
|
||||
/// <returns>Request to update locking state.</returns>
|
||||
string UpdateLockingState(
|
||||
string bikeId,
|
||||
lock_state state,
|
||||
LocationDto location = null,
|
||||
double batteryPercentage = double.NaN,
|
||||
IVersionInfo versionInfo = null);
|
||||
|
||||
/// <summary> Gets the booking request (synonym: booking == renting == mieten). </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <param name="guid">Used to publish GUID from app to copri. Used for initial setup of bike in copri.</param>
|
||||
/// <param name="batteryPercentage">Holds the filling level percentage of the battery.</param>
|
||||
/// <param name="nextAction">If not null next locking action which is performed after booking.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
string DoBook(string bikeId, Guid guid, double batteryPercentage, LockingAction? nextAction = null);
|
||||
|
||||
/// <summary> Gets the request to book and start opening the bike (synonym: booking == renting == mieten). </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
string BookAvailableAndStartOpening(string bikeId);
|
||||
|
||||
/// <summary> Gets the request to book and start opening the bike (synonym: booking == renting == mieten). </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
string BookReservedAndStartOpening(string bikeId);
|
||||
|
||||
/// <summary> Gets request for returning the bike. </summary>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <param name="location">Geolocation of lock when returning bike.</param>
|
||||
/// <returns>Request on returning request.</returns>
|
||||
string DoReturn(string bikeId, LocationDto location);
|
||||
|
||||
/// <summary> Returns a bike and starts closing. </summary>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <param name="smartDevice">Provides info about hard and software.</param>
|
||||
/// <returns>Response to send to copri.</returns>
|
||||
string ReturnAndStartClosing(string bikeId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets request for submitting feedback to copri server.
|
||||
/// </summary>
|
||||
/// <param name="bikeId">Id of the bike to which the feedback is related to.</param>
|
||||
/// <param name="currentChargeBars">Null if bike has no engine or charge is unknown. Otherwise the charge filling level of the drive battery.</param>
|
||||
/// <param name="message">General purpose message or error description.</param>
|
||||
/// <param name="isBikeBroken">True if bike is broken.</param>
|
||||
string DoSubmitFeedback(
|
||||
string bikeId,
|
||||
int? currentChargeBars,
|
||||
string message = null,
|
||||
bool isBikeBroken = false);
|
||||
|
||||
/// <summary>
|
||||
/// Gets request for submitting mini survey to copri server.
|
||||
/// </summary>
|
||||
/// <param name="answers">Collection of answers.</param>
|
||||
string DoSubmitMiniSurvey(IDictionary<string, string> answers);
|
||||
}
|
||||
|
||||
/// <summary> Copri locking states</summary>
|
||||
public enum lock_state
|
||||
{
|
||||
/// <summary> Request to backend to close lock in context of pausing ride.</summary>
|
||||
locking,
|
||||
|
||||
/// <summary> Lock is closed.</summary>
|
||||
locked,
|
||||
|
||||
/// <summary> Request to backend to close lock either in context of resuming ride or starting a rental.</summary>
|
||||
unlocking,
|
||||
|
||||
/// <summary> Lock is open.</summary>
|
||||
unlocked,
|
||||
|
||||
/// <summary> Lock is unknown state.</summary>
|
||||
unspecific,
|
||||
}
|
||||
|
||||
/// <summary> Holds location info.</summary>
|
||||
public class LocationDto
|
||||
{
|
||||
public double Latitude { get; private set; }
|
||||
|
||||
public double Longitude { get; private set; }
|
||||
|
||||
/// <summary> Accuracy of location in meters.</summary>
|
||||
public double? Accuracy { get; private set; }
|
||||
|
||||
public TimeSpan Age { get; private set; }
|
||||
|
||||
public class Builder
|
||||
{
|
||||
public double Latitude { get; set; }
|
||||
|
||||
public double Longitude { get; set; }
|
||||
|
||||
public double? Accuracy { get; set; }
|
||||
|
||||
public TimeSpan Age { get; set; }
|
||||
|
||||
public LocationDto Build()
|
||||
{
|
||||
return new LocationDto { Latitude = Latitude, Longitude = Longitude, Accuracy = Accuracy, Age = Age };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
28
SharedBusinessLogic/Repository/Request/QueryBuilderHelper.cs
Normal file
28
SharedBusinessLogic/Repository/Request/QueryBuilderHelper.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
|
||||
namespace ShareeBike.Repository.Request
|
||||
{
|
||||
public static class QueryBuilderHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the htlm query string element for session id.
|
||||
/// </summary>
|
||||
/// <returns>Query string.</returns>
|
||||
public static string GetSessionIdQueryElement(
|
||||
string op,
|
||||
string merchantId,
|
||||
string sessionCookie = null)
|
||||
=> !string.IsNullOrEmpty(merchantId) || !string.IsNullOrEmpty(sessionCookie)
|
||||
? $"{op}sessionid={(!string.IsNullOrEmpty(sessionCookie) ? sessionCookie : string.Empty)}{(!string.IsNullOrEmpty(merchantId) ? merchantId : string.Empty)}"
|
||||
: string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the htlm query string element for the language.
|
||||
/// </summary>
|
||||
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
|
||||
/// <returns>Query string.</returns>
|
||||
public static string GetLanguageQueryElement(string op, string uiIsoLangugageName)
|
||||
=> !string.IsNullOrEmpty(uiIsoLangugageName)
|
||||
? $"{op}lang={uiIsoLangugageName}"
|
||||
: string.Empty;
|
||||
}
|
||||
}
|
165
SharedBusinessLogic/Repository/Request/RequestBuilder.cs
Normal file
165
SharedBusinessLogic/Repository/Request/RequestBuilder.cs
Normal file
|
@ -0,0 +1,165 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
using ShareeBike.Repository.Exception;
|
||||
|
||||
namespace ShareeBike.Repository.Request
|
||||
{
|
||||
/// <summary> Creates requests if no user is logged in.</summary>
|
||||
public class RequestBuilder : IRequestBuilder
|
||||
{
|
||||
/// <summary> Constructs a object for building requests. </summary>
|
||||
/// <param name="merchantId">Holds the id denoting the merchant.</param>
|
||||
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
|
||||
/// <param name="smartDevice">Holds info about smart device.</param>
|
||||
public RequestBuilder(
|
||||
string merchantId,
|
||||
string uiIsoLangugageName,
|
||||
ISmartDevice smartDevice = null)
|
||||
{
|
||||
MerchantId = !string.IsNullOrEmpty(merchantId)
|
||||
? merchantId
|
||||
: throw new ArgumentException("Merchant id must not be null.", nameof(merchantId));
|
||||
|
||||
UiIsoLanguageNameParameter = RequestBuilderHelper.GetLanguageParameter(WebUtility.UrlEncode(uiIsoLangugageName));
|
||||
|
||||
AuthCookieParameter = $"&authcookie={WebUtility.UrlEncode(MerchantId)}";
|
||||
|
||||
SmartDevice = smartDevice;
|
||||
}
|
||||
|
||||
/// <summary>Holds the id denoting the merchant.</summary>
|
||||
public string MerchantId { get; }
|
||||
|
||||
/// <summary> Holds the session cookie if a user is logged in. </summary>
|
||||
public string SessionCookie => string.Empty;
|
||||
|
||||
/// <summary> Holds the current ui two letter ISO language name. </summary>
|
||||
private string UiIsoLanguageNameParameter { get; }
|
||||
|
||||
/// <summary> Auth cookie parameter. </summary>
|
||||
private string AuthCookieParameter { get; }
|
||||
|
||||
/// <summary>Holds info about smart device.</summary>
|
||||
private ISmartDevice SmartDevice { get; }
|
||||
|
||||
/// <summary> Gets request to log user in. </summary>
|
||||
/// <param name="mailAddress">Mailaddress of user to log in.</param>
|
||||
/// <param name="password">Password to log in.</param>
|
||||
/// <param name="deviceId">Id specifying user and hardware.</param>
|
||||
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
|
||||
public string DoAuthorization(
|
||||
string mailAddress,
|
||||
string password,
|
||||
string deviceId)
|
||||
=> "request=authorization" +
|
||||
$"&merchant_id={MerchantId}" +
|
||||
$"&user_id={WebUtility.UrlEncode(mailAddress)}" +
|
||||
$"&user_pw={WebUtility.UrlEncode(password)}" +
|
||||
$"&hw_id={WebUtility.UrlEncode(deviceId)}" +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Logs user out. </summary>
|
||||
public string DoAuthout()
|
||||
=> throw new CallNotRequiredException();
|
||||
|
||||
/// <summary>Gets bikes available.</summary>
|
||||
/// <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 to get.</param>
|
||||
/// <returns>Request to query list of bikes available.</returns>
|
||||
public string GetBikesAvailable(string stationId = null, string bikeId = null)
|
||||
=> "request=bikes_available&system=all" +
|
||||
stationId.GetStationId() +
|
||||
bikeId.GetBikeId() +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Get list of stations from file. </summary>
|
||||
/// <returns>Request to query list of station.</returns>
|
||||
public string GetStations()
|
||||
=> "request=stations_available" +
|
||||
AuthCookieParameter +
|
||||
SmartDevice.GetSmartDeviceParameters() +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets a list of bikes reserved/ booked by active user from Copri.</summary>
|
||||
/// <returns>Request to query list of bikes occupied.</returns>
|
||||
public string GetBikesOccupied()
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Gets booking request response. </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <returns>Response on booking request.</returns>
|
||||
public string DoReserve(string bikeId)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Gets cancel booking request response. </summary>
|
||||
/// <param name="p_iBikeId">Id of the bike to book.</param>
|
||||
/// <returns>Response on cancel booking request.</returns>
|
||||
public string DoCancelReservation(string p_iBikeId)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Request to calculate authentication keys. </summary>
|
||||
/// <param name="bikeId">Id of the bike to get keys for.</param>
|
||||
/// <returns>Response on request.</returns>
|
||||
public string CalculateAuthParameters(string bikeId)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Gets the request for notifying about start of returning sequence. </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <returns>Request to notify about start of returning sequence.</returns>
|
||||
public string StartReturningBike(string bikeId)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary>
|
||||
/// Not supported if user is not logged in. Lock state is only updated after open/ close which is only possible if user is logged in.
|
||||
/// </summary>
|
||||
/// <exception cref="NotSupportedException"></exception>
|
||||
public string UpdateLockingState(string bikeId, lock_state state, LocationDto geolocation, double batteryPercentage, IVersionInfo versionInfo)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
public string DoBook(string bikeId, Guid guid, double batteryPercentage, LockingAction? nextAction = null)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Gets the request to book and start opening the bike (synonym: booking == renting == mieten). </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
public string BookAvailableAndStartOpening(string bikeId)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Gets the request to book and start opening the bike (synonym: booking == renting == mieten). </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
public string BookReservedAndStartOpening(string bikeId)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
public string DoReturn(string bikeId, LocationDto geolocation)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Returns a bike and starts closing. </summary>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <param name="smartDevice">Provides info about hard and software.</param>
|
||||
/// <returns>Response to send to copri.</returns>
|
||||
public string ReturnAndStartClosing(string bikeId)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary> Gets submit feedback request. </summary>
|
||||
/// <param name="bikeId">Id of the bike to which the feedback is related to.</param>
|
||||
/// <param name="message">General purpose message or error description.</param>
|
||||
/// <param name="isBikeBroken">True if bike is broken.</param>
|
||||
/// <returns>Submit feedback request.</returns>
|
||||
public string DoSubmitFeedback(string bikeId, int? currentChargeBars, string message = null, bool isBikeBroken = false)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <summary>
|
||||
/// Gets request for submitting mini survey to copri server.
|
||||
/// </summary>
|
||||
/// <param name="answers">Collection of answers.</param>
|
||||
public string DoSubmitMiniSurvey(IDictionary<string, string> answers) =>
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
|
||||
using System.Net;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
|
||||
namespace ShareeBike.Repository.Request
|
||||
{
|
||||
public static class RequestBuilderHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the REST language parameter.
|
||||
/// </summary>
|
||||
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
|
||||
/// <returns></returns>
|
||||
public static string GetLanguageParameter(string uiIsoLangugageName)
|
||||
=> !string.IsNullOrEmpty(uiIsoLangugageName)
|
||||
? $"&lang={uiIsoLangugageName}"
|
||||
: string.Empty;
|
||||
|
||||
public static lock_state? GetLockState(this LockingAction? action)
|
||||
{
|
||||
switch (action)
|
||||
{
|
||||
case LockingAction.Open:
|
||||
return lock_state.unlocking;
|
||||
|
||||
case LockingAction.Close:
|
||||
return lock_state.locking;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Gets the smart device parameters. </summary>
|
||||
/// <returns>in a format which is url encode invariant.</returns>
|
||||
public static string GetSmartDeviceParameters(this ISmartDevice smartDevice)
|
||||
=> smartDevice != null
|
||||
? $"{(!string.IsNullOrEmpty(smartDevice.Manufacturer) ? $"&user_device_manufacturer={WebUtility.UrlEncode(smartDevice.Manufacturer)}" : string.Empty)}" +
|
||||
$"{(!string.IsNullOrEmpty(smartDevice.Model) ? $"&user_device_model={WebUtility.UrlEncode(smartDevice.Model)}" : string.Empty)}" +
|
||||
$"{(!string.IsNullOrEmpty(smartDevice.Platform.ToString()) ? $"&user_device_platform={WebUtility.UrlEncode(smartDevice.Platform.ToString())}" : string.Empty)}" +
|
||||
$"{(!string.IsNullOrEmpty(smartDevice.VersionText) ? $"&user_device_version={WebUtility.UrlEncode(smartDevice.VersionText)}" : string.Empty)}" +
|
||||
$"{(!string.IsNullOrEmpty(smartDevice.Identifier) ? $"&user_device_id={WebUtility.UrlEncode(smartDevice.Identifier)}" : string.Empty)}"
|
||||
: string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Converts from one locking state enum to another.
|
||||
/// </summary>
|
||||
/// <param name="state">Locking state to convert.</param>
|
||||
/// <returns>Target state.</returns>
|
||||
public static lock_state? GetLockState(this Model.Bikes.BikeInfoNS.BluetoothLock.LockingState state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case Model.Bikes.BikeInfoNS.BluetoothLock.LockingState.Open:
|
||||
return lock_state.unlocked;
|
||||
|
||||
case Model.Bikes.BikeInfoNS.BluetoothLock.LockingState.Closed:
|
||||
return lock_state.locked;
|
||||
|
||||
case Model.Bikes.BikeInfoNS.BluetoothLock.LockingState.UnknownFromHardwareError:
|
||||
return lock_state.unspecific;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Gets the station id filter. </summary>
|
||||
/// <returns>Station id filter.</returns>
|
||||
public static string GetStationId(this string stationId)
|
||||
=> !string.IsNullOrEmpty(stationId)
|
||||
? $"&station={WebUtility.UrlEncode(stationId)}"
|
||||
: string.Empty;
|
||||
|
||||
/// <summary> Gets the bike id filter. </summary>
|
||||
/// <returns>Bike id filter.</returns>
|
||||
public static string GetBikeId(this string bikeId)
|
||||
=> !string.IsNullOrEmpty(bikeId)
|
||||
? $"&bike={WebUtility.UrlEncode(bikeId)}"
|
||||
: string.Empty;
|
||||
}
|
||||
}
|
344
SharedBusinessLogic/Repository/Request/RequestBuilderLoggedIn.cs
Normal file
344
SharedBusinessLogic/Repository/Request/RequestBuilderLoggedIn.cs
Normal file
|
@ -0,0 +1,344 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
using ShareeBike.Repository.Exception;
|
||||
using ShareeBike.Services.Logging;
|
||||
|
||||
namespace ShareeBike.Repository.Request
|
||||
{
|
||||
/// <summary> Creates requests if a user is logged in.</summary>
|
||||
public class RequestBuilderLoggedIn : IRequestBuilder
|
||||
{
|
||||
/// <summary> Constructs a object for building requests. </summary>
|
||||
/// <param name="merchantId">Holds the id denoting the merchant.</param>
|
||||
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
|
||||
/// <param name="smartDevice">Holds info about smart device.</param>
|
||||
public RequestBuilderLoggedIn(
|
||||
string merchantId,
|
||||
string uiIsoLangugageName,
|
||||
string sessionCookie,
|
||||
ISmartDevice smartDevice = null)
|
||||
{
|
||||
MerchantId = !string.IsNullOrEmpty(merchantId)
|
||||
? merchantId
|
||||
: throw new ArgumentException("Merchant id must not be null.", nameof(merchantId));
|
||||
|
||||
SessionCookie = !string.IsNullOrEmpty(sessionCookie)
|
||||
? sessionCookie
|
||||
: throw new ArgumentException("Session cookie must not be null.", nameof(sessionCookie));
|
||||
|
||||
UiIsoLanguageNameParameter = RequestBuilderHelper.GetLanguageParameter(WebUtility.UrlEncode(uiIsoLangugageName));
|
||||
|
||||
AuthCookieParameter = $"&authcookie={WebUtility.UrlEncode(SessionCookie)}{WebUtility.UrlEncode(MerchantId)}";
|
||||
|
||||
SmartDevice = smartDevice;
|
||||
}
|
||||
|
||||
/// <summary> Holds the id denoting the merchant. </summary>
|
||||
public string MerchantId { get; }
|
||||
|
||||
/// <summary> Holds the session cookie if a user is logged in. </summary>
|
||||
public string SessionCookie { get; }
|
||||
|
||||
/// <summary> Holds the current ui two letter ISO language name. </summary>
|
||||
public string UiIsoLanguageNameParameter { get; }
|
||||
|
||||
/// <summary> Auth cookie parameter. </summary>
|
||||
private string AuthCookieParameter { get; }
|
||||
|
||||
/// <summary>Holds info about smart device.</summary>
|
||||
private ISmartDevice SmartDevice { get; }
|
||||
|
||||
/// <summary> Gets request to log user in. </summary>
|
||||
/// <param name="mailAddress">Mail address of user to log in.</param>
|
||||
/// <param name="password">Password to log in.</param>
|
||||
/// <param name="deviceId">Id specifying user and hardware.</param>
|
||||
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
|
||||
public string DoAuthorization(
|
||||
string mailAddress,
|
||||
string password,
|
||||
string deviceId)
|
||||
=> throw new CallNotRequiredException();
|
||||
|
||||
/// <summary> Logs user out. </summary>
|
||||
public string DoAuthout()
|
||||
=> "request=authout" +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary>Gets bikes available.</summary>
|
||||
/// <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 to get.</param>
|
||||
/// <returns>Request to query list of bikes available.</returns>
|
||||
public string GetBikesAvailable(string stationId = null, string bikeId = null)
|
||||
=> "request=bikes_available&system=all" +
|
||||
stationId.GetStationId() +
|
||||
bikeId.GetBikeId() +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets a list of bikes reserved/ booked by active user from Copri.</summary>
|
||||
/// <returns>Request to query list of bikes occupied.</returns>
|
||||
public string GetBikesOccupied()
|
||||
=> "request=user_bikes_occupied&system=all&genkey=1" +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Get list of stations from file. </summary>
|
||||
/// <returns>Request to query list of station.</returns>
|
||||
public string GetStations()
|
||||
=> "request=stations_available" +
|
||||
AuthCookieParameter +
|
||||
SmartDevice.GetSmartDeviceParameters() +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets reservation request (synonym: reservation == request == reservieren). </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to reserve.</param>
|
||||
/// <returns>Request to reserve bike.</returns>
|
||||
public string DoReserve(string bikeId)
|
||||
=> "request=booking_request" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
SmartDevice.GetSmartDeviceParameters() +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets request to cancel reservation. </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to cancel reservation for.</param>
|
||||
/// <returns>Request on cancel booking request.</returns>
|
||||
public string DoCancelReservation(string bikeId)
|
||||
=> "request=booking_cancel" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Request to get keys. </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to get keys for.</param>
|
||||
/// <returns>Request to get keys.</returns>
|
||||
public string CalculateAuthParameters(string bikeId)
|
||||
=> "request=booking_update" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
"&genkey=1" +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets the request for notifying about start of returning sequence. </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <returns>Request to notify about start of returning sequence.</returns>
|
||||
public string StartReturningBike(string bikeId)
|
||||
=> "request=booking_update" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
GetLockStateParameter(lock_state.locking) +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets the request for updating lock state for a booked bike. </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to update locking state for.</param>
|
||||
/// <param name="state">New locking state.</param>
|
||||
/// <param name="versionInfo">Information about lock (firmware version, hardware version, ...).</param>
|
||||
/// <returns>Request to update locking state.</returns>
|
||||
public string UpdateLockingState(
|
||||
string bikeId,
|
||||
lock_state state,
|
||||
LocationDto geolocation,
|
||||
double batteryPercentage,
|
||||
IVersionInfo versionInfo)
|
||||
=> "request=booking_update" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
GetLocationParameters(geolocation) +
|
||||
GetLockStateParameter(state) +
|
||||
GetBatteryPercentageParameters(batteryPercentage) +
|
||||
GetVersionInfoParameter(versionInfo) +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
|
||||
/// <summary> Gets booking request (synonym: booking == renting == mieten). </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <param name="guid">Used to publish GUID from app to copri. Used for initial setup of bike in copri.</param>
|
||||
/// <param name="batteryPercentage">Holds the filling level percentage of the battery.</param>
|
||||
/// <param name="nextAction">If not null next locking action which is performed after booking.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
public string DoBook(string bikeId, Guid guid, double batteryPercentage, LockingAction? nextAction = null)
|
||||
=> "request=booking_update" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
$"&Ilockit_GUID={guid}" +
|
||||
"&state=occupied" +
|
||||
GetLockStateParameter(nextAction.GetLockState()) +
|
||||
GetBatteryPercentageParameters(batteryPercentage) +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets the request to book and start opening the bike (synonym: booking == renting == mieten). </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
public string BookAvailableAndStartOpening(string bikeId)
|
||||
=> "request=booking_request" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
"&state=occupied" +
|
||||
GetLockStateParameter(lock_state.unlocking) +
|
||||
SmartDevice.GetSmartDeviceParameters() +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets the request to book and start opening the bike (synonym: booking == renting == mieten). </summary>
|
||||
/// <param name="bikeId">Id of the bike to book.</param>
|
||||
/// <returns>Request to booking bike.</returns>
|
||||
public string BookReservedAndStartOpening(string bikeId)
|
||||
=> "request=booking_update" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
"&state=occupied" +
|
||||
GetLockStateParameter(lock_state.unlocking) +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets request for returning the bike. </summary>
|
||||
/// <remarks> Operator specific call.</remarks>
|
||||
/// <param name="bikeId">Id of bike to return.</param>
|
||||
/// <param name="geolocation">Geolocation of lock when returning bike.</param>
|
||||
/// <returns>Request on returning request.</returns>
|
||||
public string DoReturn(string bikeId, LocationDto geolocation)
|
||||
=> "request=booking_update" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
"&state=available" +
|
||||
GetLocationParameters(geolocation) +
|
||||
GetLockStateParameter(lock_state.locked) +
|
||||
SmartDevice.GetSmartDeviceParameters() +
|
||||
GetLog() +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
|
||||
/// <summary> Returns a bike and starts closing. </summary>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <param name="smartDevice">Provides info about hard and software.</param>
|
||||
/// <returns>Response to send to copri.</returns>
|
||||
public string ReturnAndStartClosing(string bikeId)
|
||||
=> "request=booking_update" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
AuthCookieParameter +
|
||||
"&state=available" +
|
||||
GetLockStateParameter(lock_state.locking) +
|
||||
SmartDevice.GetSmartDeviceParameters() +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
/// <summary> Gets submit feedback request. </summary>
|
||||
/// <param name="bikeId">Id of the bike to return.</param>
|
||||
/// <param name="message">General purpose message or error description.</param>
|
||||
/// <param name="isBikeBroken">True if bike is broken.</param>
|
||||
/// <returns>Submit feedback request.</returns>
|
||||
public string DoSubmitFeedback(
|
||||
string bikeId,
|
||||
int? currentChargeBars = null,
|
||||
string message = null,
|
||||
bool isBikeBroken = false)
|
||||
{
|
||||
string GetIsBikeBroken()
|
||||
=> isBikeBroken ? "&bike_broken=1" : string.Empty;
|
||||
|
||||
string GetMessage()
|
||||
=> !string.IsNullOrEmpty(message) ? $"&message={WebUtility.UrlEncode(message)}" : string.Empty;
|
||||
|
||||
string GetCurrentChargeBars()
|
||||
=> currentChargeBars != null ? $"&charge_current_bars={currentChargeBars.Value}" : string.Empty;
|
||||
|
||||
return "request=user_feedback" +
|
||||
GetBikeIdParameter(bikeId) +
|
||||
GetCurrentChargeBars() +
|
||||
GetIsBikeBroken() +
|
||||
GetMessage() +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets request for submitting mini survey to copri server.
|
||||
/// </summary>
|
||||
/// <param name="answers">Collection of answers.</param>
|
||||
public string DoSubmitMiniSurvey(IDictionary<string, string> answers)
|
||||
{
|
||||
// Remove entires which invalid keys or values.
|
||||
var validAnsers = answers?.Where(x => !string.IsNullOrEmpty(x.Key?.Trim()) && !string.IsNullOrEmpty(x.Value?.Trim()));
|
||||
|
||||
// Create quersy
|
||||
if (validAnsers == null || validAnsers.Count() <= 0)
|
||||
return "request=user_minianswer" +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
|
||||
return "request=user_minianswer" +
|
||||
$"&{string.Join("&", validAnsers.Select(x => $"{x.Key}={WebUtility.UrlEncode(x.Value)}"))}" +
|
||||
AuthCookieParameter +
|
||||
UiIsoLanguageNameParameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets bike id parameter.
|
||||
/// </summary>
|
||||
/// <param name="bikeId">Id of bike.</param>
|
||||
/// <returns>bike id parameter in a format which is urlencode invariant.</returns>
|
||||
private static string GetBikeIdParameter(string bikeId)
|
||||
=> $"&bike={WebUtility.UrlEncode(bikeId)}";
|
||||
|
||||
/// <summary>
|
||||
/// Gets parameter holding lock state or null if state is unknown.
|
||||
/// </summary>
|
||||
/// <param name="lockState">Null or state of lock.</param>
|
||||
/// <returns>Lock state in a format which is urlencode invariant.</returns>
|
||||
private static string GetLockStateParameter(lock_state? lockState)
|
||||
=> lockState.HasValue ? $"&lock_state={lockState}" : string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the battery level percentage parameter if percentage is not NaN an empty string otherwise.
|
||||
/// </summary>
|
||||
/// <returns>Gets battery percentage parameters in a format which is urlencode invariant or an empty string if percentage percentge is NaN.</returns>
|
||||
private static string GetBatteryPercentageParameters(double batteryPercentage) => !double.IsNaN(batteryPercentage)
|
||||
? $"&voltage={batteryPercentage.ToString(CultureInfo.InvariantCulture)}"
|
||||
: string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the version info parameter or an empty string if version info is not available.
|
||||
/// </summary>
|
||||
/// <param name="versionInfo">Lock version info.</param>
|
||||
/// <returns>Version info in a format which is urlencode invariant or empty. </returns>
|
||||
private static string GetVersionInfoParameter(IVersionInfo versionInfo) => versionInfo?.FirmwareVersion > 0 || versionInfo?.HardwareVersion > 0 || versionInfo?.LockVersion > 0
|
||||
? $"&firmware=HW%20{versionInfo.HardwareVersion}%3BFW%20{versionInfo.FirmwareVersion}%3BLock%20{versionInfo.LockVersion}"
|
||||
: string.Empty;
|
||||
|
||||
/// <summary> Gets the geolocation parameter. </summary>
|
||||
/// <param name="geolocation">Geolocation or null.</param>
|
||||
/// <returns>Empty string if geolocation is null otherwise parameter including latitude, longitude and age in a format which is urlencode invariant.</returns>
|
||||
private static string GetLocationParameters(LocationDto geolocation)
|
||||
{
|
||||
if (geolocation == null)
|
||||
return string.Empty;
|
||||
|
||||
if (geolocation.Accuracy == null)
|
||||
return $"&gps={geolocation.Latitude.ToString(CultureInfo.InvariantCulture)},{geolocation.Longitude.ToString(CultureInfo.InvariantCulture)}&gps_age={geolocation.Age.TotalSeconds}";
|
||||
|
||||
return $"&gps={geolocation.Latitude.ToString(CultureInfo.InvariantCulture)},{geolocation.Longitude.ToString(CultureInfo.InvariantCulture)}&gps_accuracy={geolocation.Accuracy.Value.ToString(CultureInfo.InvariantCulture)}&gps_age={geolocation.Age.TotalSeconds}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets logging entries from serilog.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static string GetLog()
|
||||
{
|
||||
var messages = string.Join("\n", MemoryStackSink.PopAllMessages());
|
||||
return !string.IsNullOrEmpty(messages)
|
||||
? "&app_debug=" + WebUtility.UrlEncode(string.Join("\n", messages))
|
||||
: string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue