sharee.bike-App/TINKLib/Repository/Request/RequestBuilderLoggedIn.cs

193 lines
9.8 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System;
using System.Globalization;
using System.Net;
2021-06-26 20:57:55 +02:00
using TINK.Model.Device;
using TINK.Repository.Exception;
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
namespace TINK.Repository.Request
2021-05-13 20:03:07 +02:00
{
/// <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>
2021-06-26 20:57:55 +02:00
public string DoReserve(string bikeId)
2021-05-13 20:03:07 +02:00
=> $"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>
2021-06-26 20:57:55 +02:00
public string DoCancelReservation(string p_iBikeId)
2021-05-13 20:03:07 +02:00
=> $"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>
2021-06-26 20:57:55 +02:00
public string CalculateAuthParameters(string bikeId)
2021-05-13 20:03:07 +02:00
=> $"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>
2021-06-26 20:57:55 +02:00
public string UpateLockingState(string bikeId, LocationDto geolocation, lock_state state, double batteryPercentage)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
return $"request=booking_update&bike={bikeId}{GetLocationParameters(geolocation)}&lock_state={state}{GetBatteryPercentageParameters(batteryPercentage)}&authcookie={SessionCookie}{MerchantId}";
2021-05-13 20:03:07 +02:00
}
/// <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>
2021-06-26 20:57:55 +02:00
public string DoBook(string bikeId, Guid guid, double batteryPercentage)
=> $"request=booking_update&bike={bikeId}&authcookie={SessionCookie}{MerchantId}&Ilockit_GUID={guid}&state=occupied&lock_state=unlocked{GetBatteryPercentageParameters(batteryPercentage)}";
2021-05-13 20:03:07 +02:00
/// <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>
2021-06-26 20:57:55 +02:00
public string DoReturn(string bikeId, LocationDto geolocation, ISmartDevice smartDevice)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
return $"request=booking_update" +
$"&bike={bikeId}" +
$"&authcookie={SessionCookie}{MerchantId}" +
$"&state=available" +
$"{GetLocationParameters(geolocation)}" +
$"&lock_state=locked" +
$"{GetSmartDeviceParameters(smartDevice)}";
2021-05-13 20:03:07 +02:00
}
/// <summary> Gets submit feedback request. </summary>
2021-06-26 20:57:55 +02:00
/// <param name="bikeId">Id of the bike to return.</param>
2021-05-13 20:03:07 +02:00
/// <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(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
string message = null,
bool isBikeBroken = false)
{
if (string.IsNullOrEmpty(message) && !isBikeBroken)
{
// User just acknoledged biked returned message.
2021-06-26 20:57:55 +02:00
return $"request=user_feedback&bike={bikeId}&authcookie={SessionCookie}{MerchantId}";
2021-05-13 20:03:07 +02:00
}
if (isBikeBroken == false)
{
// Bike is ok and user entered a feedback message.
2021-06-26 20:57:55 +02:00
return $"request=user_feedback&bike={bikeId}&message={WebUtility.UrlEncode(message)}&authcookie={SessionCookie}{MerchantId}";
2021-05-13 20:03:07 +02:00
}
if (string.IsNullOrEmpty(message))
{
// User just marked bike as broken without comment.
2021-06-26 20:57:55 +02:00
return $"request=user_feedback&bike={bikeId}&bike_broken=1&authcookie={SessionCookie}{MerchantId}";
2021-05-13 20:03:07 +02:00
}
// Bike is marked as broken and user added a comment.
2021-06-26 20:57:55 +02:00
return $"request=user_feedback&bike={bikeId}&bike_broken=1&message={WebUtility.UrlEncode(message)}&authcookie={SessionCookie}{MerchantId}";
2021-05-13 20:03:07 +02:00
}
2021-06-26 20:57:55 +02:00
private string GetBatteryPercentageParameters(double batteryPercentage) => !double.IsNaN(batteryPercentage)
2021-05-13 20:03:07 +02:00
? $"&voltage={batteryPercentage.ToString(CultureInfo.InvariantCulture)}"
: string.Empty;
2021-06-26 20:57:55 +02:00
/// <summary> Gets the geolocation parameter. </summary>
/// <param name="geolocation">Geolocation or null.</param>
private string GetLocationParameters(LocationDto geolocation)
2021-05-13 20:03:07 +02:00
{
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}";
}
2021-06-26 20:57:55 +02:00
/// <summary> Gets the geolocation parameter. </summary>
/// <param name="geolocation">Geolocation or null.</param>
private string GetSmartDeviceParameters(ISmartDevice smartDevice)
=> smartDevice != null
? $"{(!string.IsNullOrEmpty(smartDevice.Manufacturer) ? $"&user_device_manufaturer={smartDevice.Manufacturer})" : string.Empty)}" +
$"{(!string.IsNullOrEmpty(smartDevice.Model) ? $"&user_device_model={smartDevice.Model}" : string.Empty)}" +
$"{(!string.IsNullOrEmpty(smartDevice.PlatformText) ? $"&user_device_platform={smartDevice.PlatformText}" : string.Empty)}" +
$"{(!string.IsNullOrEmpty(smartDevice.VersionText) ? $"&user_device_version={smartDevice.VersionText}" : string.Empty)}" +
$"{(!string.IsNullOrEmpty(smartDevice.Identifier) ? $"&user_device_id={smartDevice.Identifier}" : string.Empty)}"
: string.Empty;
2021-05-13 20:03:07 +02:00
}
}