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,15 @@
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
[DataContract]
public class AuthorizationResponse : ResponseBase
{
[DataMember]
public int debuglevel { get; private set; }
/// <summary> Holds the group of the bike (TINK, Konrad, ...).</summary>
[DataMember]
public string user_group { get; private set; }
}
}

View file

@ -0,0 +1,9 @@
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
[DataContract]
public class AuthorizationoutResponse : ResponseBase
{
}
}

View file

@ -0,0 +1,22 @@
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
[DataContract]
public class BikeInfoAvailable : BikeInfoBase
{
/// <summary>
/// Position of the bike.
/// </summary>
[DataMember]
public string gps { get; private set; }
[DataMember]
/// <summary> Full advertisement name.</summary>
public string Ilockit_ID { get; private set; }
[DataMember]
/// <summary> Full advertisement name.</summary>
public string Ilockit_GUID { get; private set; }
}
}

View file

@ -0,0 +1,76 @@
using System.Runtime.Serialization;
using TINK.Repository.Response;
namespace TINK.Model.Repository.Response
{
/// <summary>
/// Holds info about a single bike.
/// </summary>
[DataContract]
public class BikeInfoBase
{
/// <summary>
/// Id of the bike.
/// </summary>
[DataMember]
public int bike { get; private set; }
/// <summary>
/// Id of the station.
/// </summary>
[DataMember]
public int? station { get; private set; }
/// <summary>
/// Holds the localized (german) description of the bike.
/// </summary>
[DataMember]
public string description { get; private set; }
/// <summary> Holds the group of the bike.</summary>
/// <remarks>
/// Copri returns values "TINK", "Konrad".
/// </remarks>
[DataMember]
public string bike_group { get; private set; }
/// <summary>
/// Rental state.
/// </summary>
[DataMember]
public string state { get; private set; }
/// <summary>
/// Holds the uri where to reserve/ rent the bike.
/// </summary>
[DataMember]
public string uri_operator { get; private set; }
/// <summary> Holds whether bike is equipped with computer or if bike is a lock bike.</summary>
/// <remarks>
/// <table>
/// <tr><th>Value </th><th>Type of bike </th><th>Member to extract info.</th></tr>
/// <tr><td>LOCK </td><td>Bike with manual lock. </td><td>TextToTypeHelper.GetIsNonBikeComputerBike</td></tr>
/// <tr><td> </td><td>Bike with a bord computer. </td><td></td></tr>
/// <tr><td>? </td><td>Bike with a bluetooth lock.</td><td></td></tr>
/// </table>
/// </remarks>
[DataMember]
public string system { get; private set; }
#if !NOTARIFFDESCRIPTION
/// <summary> Holds the tariff information for a bike. </summary>
[DataMember]
public TariffDescription tariff_description { get; private set; }
#endif
/// <summary>
/// Textual description of response.
/// </summary>
/// <returns>Object as text.</returns>
public new string ToString()
{
return $"Bike {bike}{(station != null ? $", at station {station}" : string.Empty)}{(!string.IsNullOrEmpty(description) ? $", {description}" : string.Empty)}{(!string.IsNullOrEmpty(state) ? $", status={state}" : string.Empty)}.";
}
}
}

View file

@ -0,0 +1,32 @@
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
[DataContract]
public class BikeInfoReservedOrBooked : BikeInfoAvailable
{
/// <summary>
/// Date from when bike was reserved from/ booked from.
/// Format: 2017-11-28 11:01:51.637747+01
/// </summary>
[DataMember]
public string start_time { get; private set; }
/// <summary> Booking code if bike is BC-bike.</summary>
[DataMember]
public string timeCode { get; private set; }
[DataMember]
/// <summary> Seed used to generate key for connecting to bluetooth lock.</summary>
public string K_seed { get; private set; }
[DataMember]
/// <summary> Key for connect to bluetooth lock as user.</summary>
public string K_u { get; private set; }
[DataMember]
/// <summary> Key for connect to bluetooth lock as admin.</summary>
public string K_a { get; private set;}
}
}

View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
/// <summary>
/// Holds the information about all bikes and is used for deserialization of copri answer.
/// </summary>
[DataContract]
public class BikesAvailableResponse : ResponseBase
{
/// <summary>
/// Dictionary of bikes.
/// </summary>
[DataMember]
public Dictionary<int, BikeInfoAvailable> bikes { get; private set; }
}
}

View file

@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
public class BikesReservedOccupiedResponse : ResponseBase
{
/// <summary>
/// Dictionary of bikes.
/// </summary>
[DataMember]
public Dictionary<int, BikeInfoReservedOrBooked> bikes_occupied { get; private set; }
}
}

View file

@ -0,0 +1,27 @@
using TINK.Repository.Exception;
namespace TINK.Repository.Response
{
public static class JsonConvert
{
/// <summary>
/// Deserializes COPRI responses in a consitent way for entire app.
/// </summary>
/// <typeparam name="T">Type of object to serialize to.</typeparam>
/// <param name="response">JSON to deserialize.</param>
/// <returns>Deserialized object.</returns>
public static T DeserializeObject<T>(string response)
{
try
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(response);
}
catch (System.Exception ex)
{
throw new DeserializationException(ex);
}
}
public static string SerializeObject(object value) => Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}

View file

@ -0,0 +1,15 @@
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
/// <summary>
/// Holds the information about a booking request and is used for deserialization of copri answer.
/// </summary>
[DataContract]
public class ReservationBookingResponse : BikesReservedOccupiedResponse
{
/// <summary> Booking code for BC- bikes. </summary>
[DataMember]
public string timeCode { get; private set; }
}
}

View file

@ -0,0 +1,10 @@

namespace TINK.Model.Repository.Response
{
/// <summary>
/// Holds the information about a cancel booking request and is used for deserialization of copri answer.
/// </summary>
public class ReservationCancelReturnResponse : BikesReservedOccupiedResponse
{
}
}

View file

@ -0,0 +1,32 @@
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
[DataContract]
public class ResponseBase
{
[DataMember]
public string response_state { get; private set; }
[DataMember]
public string response { get; private set; }
[DataMember]
public string response_text { get; private set; }
[DataMember]
public string authcookie { get; private set; }
[DataMember]
public string copri_version { get; private set; }
/// <summary> Textual description of response. </summary>
public new string ToString()
{
return $"Response state is \"{response_state ?? string.Empty}\", " +
$"auth cookie is \"{authcookie ?? string.Empty}\" and response is \"{response_text ?? string.Empty}\", " +
$"code \"{response ?? string.Empty}\"" +
$"response text \"{response_text ?? string.Empty}\".";
}
}
}

View file

@ -0,0 +1,25 @@
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
[DataContract]
public class ResponseContainer<T>
{
[DataMember]
public T tinkjson { get; private set; }
/// <summary>
/// Serializes object to string.
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (tinkjson == null)
{
return "Response container does not hold no entry.";
}
return tinkjson.ToString();
}
}
}

View file

@ -0,0 +1,240 @@
using System.Linq;
using TINK.Model.Repository.Exception;
using TINK.MultilingualResources;
using TINK.Repository.Exception;
namespace TINK.Model.Repository.Response
{
public static class ResponseHelper
{
public const string RESPONSE_OK = "OK";
/// <summary> Holds the description of the action logout. </summary>
public const string BIKES_LOGOUT_ACTIONTEXT = "Abmeldung fehlgeschlagen.";
/// <summary> Holds the description of the action get stations available. </summary>
public const string STATIONS_AVAILABLE_ACTIONTEXT = "Abfrage der verfügbaren Stationen fehlgeschlagen.";
/// <summary> Holds the description of the action get bikes available. </summary>
public const string BIKES_AVAILABLE_ACTIONTEXT = "Abfrage der verfügbaren Fahrräder fehlgeschlagen.";
/// <summary> Holds the description of the action get bikes occupied. </summary>
public const string BIKES_OCCUPIED_ACTIONTEXT = "Abfrage der reservierten/ gebuchten Fahrräder fehlgeschlagen.";
/// <summary> Holds the description of the action cancel reservation. </summary>
public const string BIKES_CANCELREQUEST_ACTIONTEXT = "Aufheben der Reservierung fehlgeschlagen.";
/// <summary> Holds the description of the action return bike. </summary>
public const string BIKES_RETURNBIKE_ACTIONTEXT = "Rückgabe des Rads fehlgeschlagen.";
/// <summary>
/// Checks if log in response is ok.
/// </summary>
/// <param name="response">Response to check whether it is valid.</param>
/// <param name="mail">Mail addess used to create details error message in case log in response is invalid.</param>
/// <returns></returns>
public static AuthorizationResponse GetIsResponseOk(this AuthorizationResponse response, string mail)
{
if (response == null)
{
throw new InvalidResponseException<AuthorizationResponse>("Anmeldung fehlgeschlagen.", null);
}
if (response.response_state.ToUpper() == InvalidAuthorizationResponseException.AUTH_FAILURE_STATUS_MESSAGE_UPPERCASE)
{
throw new InvalidAuthorizationResponseException(mail, response);
}
else if (!response.response_state.Trim().ToUpper().StartsWith(RESPONSE_OK))
{
throw new InvalidResponseException<AuthorizationResponse>($"Anmeldung {mail} fehlgeschlagen.\r\nServer Antwort: {response.response_text}", response);
}
return response;
}
/// <summary>
/// Checks if log out response is ok.
/// </summary>
/// <param name="p_oResponse">Response to check whether it is valid.</param>
/// <returns></returns>
public static AuthorizationoutResponse GetIsResponseOk(this AuthorizationoutResponse p_oResponse)
{
if (AuthcookieNotDefinedException.IsAuthcookieNotDefined(p_oResponse, BIKES_LOGOUT_ACTIONTEXT, out AuthcookieNotDefinedException exception))
{
throw exception;
}
GetIsResponseOk(p_oResponse, BIKES_LOGOUT_ACTIONTEXT);
if (p_oResponse.authcookie != "1")
{
throw new InvalidResponseException<AuthorizationoutResponse>(
BIKES_LOGOUT_ACTIONTEXT,
p_oResponse);
}
return p_oResponse;
}
/// <summary>Gets if a call to reserve bike succeeded or not by checking a booking response.</summary>
/// <param name="bikeId">Id of bike which should be booked.</param>
/// <param name="sessionCookie">Sessiong cookie of logged in user.</param>
/// <param name="bookingResponse">Response to check.</param>
/// <returns></returns>
public static BikeInfoReservedOrBooked GetIsReserveResponseOk(
this ReservationBookingResponse bookingResponse,
int bikeId)
{
GetIsResponseOk(bookingResponse, string.Format(AppResources.ExceptionTextReservationBikeFailedGeneral, bikeId));
if (BookingDeclinedException.IsBookingDeclined(bookingResponse.response_state, out BookingDeclinedException exception))
{
throw exception;
}
// Get bike which has to be booked.
var bikeInfoRequestedOccupied = bookingResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (bikeInfoRequestedOccupied == null)
{
throw new System.Exception(string.Format(
AppResources.ExceptionTextReservationBikeFailedUnavailalbe,
bikeId,
!string.IsNullOrWhiteSpace(bookingResponse?.response_text) ? $"\r\n{bookingResponse.response_text}" : string.Empty));
}
return bikeInfoRequestedOccupied;
}
/// <summary> Gets if a booking call succeeded or not by checking a booking response. </summary>
/// <param name="bikeId">Id of bike which should be booked.</param>
/// <param name="bookingResponse">Response to check.</param>
/// <returns></returns>
public static BikeInfoReservedOrBooked GetIsBookingResponseOk(
this ReservationBookingResponse bookingResponse,
int bikeId)
{
GetIsResponseOk(bookingResponse, string.Format(AppResources.ExceptionTextRentingBikeFailedGeneral, bikeId));
// Get bike which has to be booked.
var bikeInfoRequestedOccupied = bookingResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (bikeInfoRequestedOccupied == null)
{
throw new System.Exception(string.Format(
AppResources.ExceptionTextRentingBikeFailedUnavailalbe,
bikeId,
!string.IsNullOrWhiteSpace(bookingResponse?.response_text) ? $"\r\n{bookingResponse.response_text}" : string.Empty));
}
return bikeInfoRequestedOccupied;
}
/// <summary> Gets if request is ok.</summary>
/// <param name="response">Response to verify.</param>
/// <param name="textOfAction">Text describing request which is shown if validation fails.</param>
/// <returns>Verified response.</returns>
public static T GetIsResponseOk<T>(this T response, string textOfAction) where T : ResponseBase
{
if (response == null)
{
throw new InvalidResponseException<T>(textOfAction, null);
}
if (AuthcookieNotDefinedException.IsAuthcookieNotDefined(response, textOfAction, out AuthcookieNotDefinedException exception))
{
throw exception;
}
if (!response.response_state.Trim().ToUpper().StartsWith(RESPONSE_OK))
{
throw new InvalidResponseException<T>(
$"{textOfAction}\r\nServer Antwort: {response.response_text}",
response);
}
return response;
}
/// <summary> Gets if cancel or booking request is ok.</summary>
/// <param name="cancelBookingResponse">Response to verify.</param>
/// <param name="textOfAction">Text describing request which is shown if validation fails.</param>
/// <param name="bikeId">Id of bike.</param>
/// <returns>Verified response.</returns>
public static ReservationCancelReturnResponse GetIsCancelReservationResponseOk(
this ReservationCancelReturnResponse cancelBookingResponse,
int bikeId)
{
GetIsResponseOk<ResponseBase>(cancelBookingResponse, BIKES_CANCELREQUEST_ACTIONTEXT);
// Get bike which has to be booked.
var l_oBikeInfo = cancelBookingResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (l_oBikeInfo != null)
{
throw new ReturnBikeException(
cancelBookingResponse,
$"{BIKES_CANCELREQUEST_ACTIONTEXT} Aufruf wurde erfolgreich ausgeführt, aber Rad ist noch in Liste der reservierten Räder enthalten.");
}
return cancelBookingResponse;
}
/// <summary> Gets if return bike request is ok.</summary>
/// <param name="returnBikeResponse">Response to verify.</param>
/// <param name="textOfAction">Text describing request which is shown if validation fails.</param>
/// <param name="bikeId">Id of bike.</param>
/// <returns>Verified response.</returns>
public static ReservationCancelReturnResponse GetIsReturnBikeResponseOk(
this ReservationCancelReturnResponse returnBikeResponse,
int bikeId)
{
// Check if bike is at station.
if (NotAtStationException.IsNotAtStation(returnBikeResponse.response_state.ToUpper(), out NotAtStationException notAtStationException))
{
throw notAtStationException;
}
// Check if GPS data was send to copri.
if (NoGPSDataException.IsNoGPSData(returnBikeResponse.response_state.ToUpper(), out NoGPSDataException noGPSDataException))
{
throw noGPSDataException;
}
GetIsResponseOk<ResponseBase>(returnBikeResponse, BIKES_RETURNBIKE_ACTIONTEXT);
// Get bike which has to be booked.
var l_oBikeInfo = returnBikeResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (l_oBikeInfo != null)
{
throw new ReturnBikeException(
returnBikeResponse,
$"{BIKES_RETURNBIKE_ACTIONTEXT} Aufruf wurde erfolgreich ausgeführt, aber Rad ist noch in Liste der gemieteten Räder enthalten.");
}
return returnBikeResponse;
}
/// <summary>
/// Gets the response for bikes occupied request with no bikes reserved.
/// </summary>
/// <param name="p_strSesstionCookie"></param>
/// <returns></returns>
public static BikesReservedOccupiedResponse GetBikesOccupiedNone(string p_strSesstionCookie = null)
{
var l_oJson = BIKES_OCCUPIED_REQUEST_NONE_FILE.Replace(@"""authcookie"": """"", @"""authcookie"": """ + (p_strSesstionCookie ?? string.Empty) + @"""");
return CopriCallsStatic.DeserializeBikesOccupiedResponse(l_oJson);
}
/// <summary>
/// Holds an empty bikes occupied response.
/// </summary>
private const string BIKES_OCCUPIED_REQUEST_NONE_FILE = @"
{
""tinkjson"": {
""response_state"": ""OK"",
""bikes_occupied"": { },
""authcookie"": """",
""response"": ""user_bikes_occupied"",
""apiserver"": ""https://tinkwwp.copri-bike.de""
}
}";
}
}

View file

@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace TINK.Model.Repository.Response
{
/// <summary>
/// Holds the information about all stations and is used for deserialization of copri answer.
/// </summary>
[DataContract]
public class StationsAllResponse : ResponseBase
{
/// <summary>
/// Holds info about a single station.
/// </summary>
[DataContract]
public class StationInfo
{
/// <summary>
/// Unique id of the station.
/// </summary>
[DataMember]
public int station { get; private set; }
[DataMember]
public string station_group { get; private set; }
[DataMember]
public string description { get; private set; }
/// <summary>
/// Position of the station.
/// </summary>
[DataMember]
public string gps { get; private set; }
}
/// <summary>
/// Dictionary of bikes.
/// </summary>
[DataMember]
public Dictionary<int, StationInfo> stations { get; private set; }
}
}

View file

@ -0,0 +1,8 @@
using TINK.Model.Repository.Response;
namespace TINK.Repository.Response
{
public class SubmitFeedbackResponse : ResponseBase
{
}
}

View file

@ -0,0 +1,47 @@
using System.Runtime.Serialization;
namespace TINK.Repository.Response
{
/// <summary>
/// Holds tariff info for a single bike.
/// </summary>
[DataContract]
public record TariffDescription
{
/// <summary>
/// Name of the tariff.
/// </summary>
[DataMember]
public string name { get; private set; }
/// <summary>
/// Number of the tariff.
/// </summary>
[DataMember]
public string number { get; private set; }
/// <summary>
/// Costs per hour in euro.
/// </summary>
[DataMember]
public string eur_per_hour { get; private set; }
/// <summary>
/// Costs of the abo per month.
/// </summary>
[DataMember]
public string abo_eur_per_month { get; private set; }
/// <summary>
/// Costs per hour in euro.
/// </summary>
[DataMember]
public string free_hours { get; private set; }
/// <summary>
/// Maximum fee per day.
/// </summary>
[DataMember]
public string max_eur_per_day { get; private set; }
}
}