Initial version.

This commit is contained in:
Oliver Hauff 2021-05-13 20:03:07 +02:00
parent 193aaa1a56
commit b72c67a53e
228 changed files with 25924 additions and 0 deletions

View file

@ -0,0 +1,122 @@
using System;
namespace TINK.Model.Repository.Request
{
/// <summary> Defines members to create requests.</summary>
public interface IRequestBuilder
{
/// <summary> Holds the id denoting the merchant (TINK 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">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>Requst which holds auth cookie <see cref="RequstBase.authcookie"/></remarks>
string DoAuthorization(
string mailAddress,
string password,
string deviceId);
/// <summary> Logs user out. </summary>
/// <param name="p_strMerchantId">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>
/// <returns>Request to query list of bikes available.</returns>
string GetBikesAvailable();
/// <summary> Gets a list of bikes reserved/ booked by acctive 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>Requst to reserve bike.</returns>
string DoReserve(int bikeId);
/// <summary> Gets request to cancel reservation. </summary>
/// <param name="bikeId">Id of the bike to cancel reservation for.</param>
/// <returns>Requst on cancel booking request.</returns>
string DoCancelReservation(int 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 CalculateAuthKeys(int 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="location">Geolocation of lock when state change occurred.</param>
/// <param name="state">New locking state.</param>
/// <returns>Request to update locking state.</returns>
string UpateLockingState(
int bikeId,
LocationDto location,
lock_state state,
double batteryPercentage);
/// <summary> Gets booking request 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>
/// <returns>Request to booking bike.</returns>
string DoBook(int bikeId, Guid guid, double batteryPercentage);
/// <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>Requst on returning request.</returns>
string DoReturn(int bikeId, LocationDto location);
/// <summary>
/// Gets request for submiting feedback to copri server.
/// </summary>
/// <param name="message">General purpose message or error description.</param>
/// <param name="isBikeBroken">True if bike is broken.</param>
string DoSubmitFeedback(string message = null, bool isBikeBroken = false);
}
/// <summary> Copri locking states</summary>
public enum lock_state
{
locked,
unlocked
}
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 };
}
}
}
}

View file

@ -0,0 +1,112 @@
using System;
using System.Net;
using TINK.Model.Repository.Exception;
namespace TINK.Model.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"></param>
public RequestBuilder(
string merchantId)
{
MerchantId = !string.IsNullOrEmpty(merchantId)
? merchantId
: throw new ArgumentException("Merchant id must not be null.", nameof(merchantId));
}
/// <summary> Holds the id denoting the merchant (TINK app). </summary>
public string MerchantId { get; }
/// <summary> Holds the session cookie if a user is logged in. </summary>
public string SessionCookie => string.Empty;
/// <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)
{
return string.Format(
"request=authorization&merchant_id={0}&user_id={1}&user_pw={2}&hw_id={3}",
MerchantId,
WebUtility.UrlEncode(mailAddress),
WebUtility.UrlEncode(password),
deviceId);
}
/// <summary> Logs user out. </summary>
public string DoAuthout()
{
throw new CallNotRequiredException();
}
/// <summary>Gets bikes available.</summary>
/// <returns>Request to query list of bikes available.</returns>
public string GetBikesAvailable()
{
return GetBikesAvailable(MerchantId);
}
/// <summary>Gets bikes available.</summary>
/// <returns>Request to query list of bikes available.</returns>
public static string GetBikesAvailable(string merchantId, string sessionCookie = null)
{
return $"request=bikes_available&system=all&authcookie={sessionCookie ?? string.Empty}{merchantId}";
}
/// <summary> Get list of stations from file. </summary>
/// <returns>Request to query list of station.</returns>
public string GetStations()
{
return GetStations(MerchantId);
}
/// <summary> Get list of stations from file. </summary>
/// <returns>Request to query list of station.</returns>
public static string GetStations(string merchantId, string sessionCookie = null)
{
return $"request=stations_available&authcookie={sessionCookie ?? string.Empty}{merchantId}";
}
/// <summary> Gets a list of bikes reserved/ booked by acctive 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="p_iBikeId">Id of the bike to book.</param>
/// <returns>Response on booking request.</returns>
public string DoReserve(int p_iBikeId) => 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(int 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 CalculateAuthKeys(int bikeId) => throw new NotSupportedException();
public string UpateLockingState(int bikeId, LocationDto geolocation, lock_state state, double batteryPercentage)
=> throw new NotSupportedException();
public string DoBook(int bikeId, Guid guid, double batteryPercentage) => throw new NotSupportedException();
public string DoReturn(int bikeId, LocationDto geolocation) => throw new NotSupportedException();
/// <summary> Gets submit feedback request. </summary>
/// <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 message = null,
bool isBikeBroken = false) => throw new NotSupportedException();
}
}

View file

@ -0,0 +1,170 @@
using System;
using System.Globalization;
using System.Net;
using TINK.Model.Repository.Exception;
namespace TINK.Model.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"></param>
public RequestBuilderLoggedIn(
string merchantId,
string sessionCookie)
{
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));
}
/// <summary> Holds the id denoting the merchant (TINK app). </summary>
public string MerchantId { get; }
/// <summary> Holds the session cookie if a user is logged in. </summary>
public string SessionCookie { 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)
{
throw new CallNotRequiredException();
}
/// <summary> Logs user out. </summary>
public string DoAuthout()
{
return $"request=authout&authcookie={SessionCookie}{MerchantId}";
}
/// <summary>Gets bikes available.</summary>
/// <returns>Request to query list of bikes available.</returns>
public string GetBikesAvailable()
{
return RequestBuilder.GetBikesAvailable(MerchantId, SessionCookie);
}
/// <summary> Gets a list of bikes reserved/ booked by acctive user from Copri.</summary>
/// <returns>Request to query list of bikes occupied.</returns>
public string GetBikesOccupied()
{
return !string.IsNullOrEmpty(SessionCookie)
? $"request=user_bikes_occupied&system=all&genkey=1&authcookie={SessionCookie}{MerchantId}"
: "request=bikes_available";
}
/// <summary> Get list of stations from file. </summary>
/// <returns>Request to query list of station.</returns>
public string GetStations()
{
return $"request=stations_available&authcookie={SessionCookie ?? string.Empty}{MerchantId}";
}
/// <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>Requst to reserve bike.</returns>
public string DoReserve(int bikeId)
=> $"request=booking_request&bike={bikeId}&authcookie={SessionCookie}{MerchantId}";
/// <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>Requst on cancel booking request.</returns>
public string DoCancelReservation(int p_iBikeId)
=> $"request=booking_cancel&bike={p_iBikeId}&authcookie={SessionCookie}{MerchantId}";
/// <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 CalculateAuthKeys(int bikeId)
=> $"request=booking_update&bike={bikeId}&authcookie={SessionCookie}{MerchantId}&genkey=1";
/// <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>
/// <returns>Request to update locking state.</returns>
public string UpateLockingState(int bikeId, LocationDto geolocation, lock_state state, double batteryPercentage)
{
return $"request=booking_update&bike={bikeId}{GetLocationKey(geolocation)}&lock_state={state}{GetBatteryPercentageKey(batteryPercentage)}&authcookie={SessionCookie}{MerchantId}";
}
/// <summary> Gets booking request 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>
/// <returns>Request to booking bike.</returns>
public string DoBook(int bikeId, Guid guid, double batteryPercentage)
=> $"request=booking_update&bike={bikeId}&authcookie={SessionCookie}{MerchantId}&Ilockit_GUID={guid}&state=occupied&lock_state=unlocked{GetBatteryPercentageKey(batteryPercentage)}";
/// <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>Requst on returning request.</returns>
public string DoReturn(int bikeId, LocationDto geolocation)
{
return $"request=booking_update&bike={bikeId}&authcookie={SessionCookie}{MerchantId}&state=available{GetLocationKey(geolocation)}&lock_state=locked";
}
/// <summary> Gets submit feedback request. </summary>
/// <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 message = null,
bool isBikeBroken = false)
{
if (string.IsNullOrEmpty(message) && !isBikeBroken)
{
// User just acknoledged biked returned message.
return "request=user_feedback";
}
if (isBikeBroken == false)
{
// Bike is ok and user entered a feedback message.
return $"request=user_feedback&message={WebUtility.UrlEncode(message)}";
}
if (string.IsNullOrEmpty(message))
{
// User just marked bike as broken without comment.
return $"request=user_feedback&bike_broken=1";
}
// Bike is marked as broken and user added a comment.
return $"request=user_feedback&bike_broken=1&message={WebUtility.UrlEncode(message)}";
}
private string GetBatteryPercentageKey(double batteryPercentage) => !double.IsNaN(batteryPercentage)
? $"&voltage={batteryPercentage.ToString(CultureInfo.InvariantCulture)}"
: string.Empty;
private string GetLocationKey(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}";
}
}
}