mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-23 15:46:29 +01:00
52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace ShareeBike.Repository.Exception
|
|
{
|
|
public class NotAtStationException : InvalidResponseException
|
|
{
|
|
public const string RETURNBIKE_FAILURE_STATUS_MESSAGE_CODE = "FAILURE 2178:";
|
|
|
|
/// <summary> COPRI response status regular expression to extract detail information. </summary>
|
|
public const string RETURNBIKE_FAILURE_STATUS_MESSAGE_UPPERCASE = "(BIKE [A-Za-z0-9_]+ OUT OF GEO FENCING\\. )([0-9]+)( METER DISTANCE TO NEXT STATION )([A-Za-z0-9_]+)";
|
|
|
|
/// <summary> Prevents invalid use of exception. </summary>
|
|
private NotAtStationException() : base(typeof(NotAtStationException).Name)
|
|
{
|
|
}
|
|
|
|
public static bool IsNotAtStation(string responseState, out NotAtStationException exception)
|
|
{
|
|
// Check if there are too many bikes requested/ booked.
|
|
var response = responseState.Trim().ToUpper();
|
|
|
|
if (!response.StartsWith(RETURNBIKE_FAILURE_STATUS_MESSAGE_CODE))
|
|
{
|
|
exception = null;
|
|
return false;
|
|
}
|
|
|
|
var match = Regex.Match(
|
|
responseState.ToUpper(),
|
|
RETURNBIKE_FAILURE_STATUS_MESSAGE_UPPERCASE);
|
|
if (match.Groups.Count != 5
|
|
|| !int.TryParse(match.Groups[2].ToString(), out int meters))
|
|
{
|
|
exception = new NotAtStationException();
|
|
return true;
|
|
}
|
|
|
|
exception = new NotAtStationException {
|
|
Distance = meters,
|
|
StationNr = match.Groups[4].ToString()
|
|
};
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary> Holds the distance to next station.</summary>
|
|
public int? Distance { get; private set; } = null;
|
|
|
|
/// <summary> Holds the id of next station.</summary>
|
|
public string StationNr { get; private set; } = string.Empty;
|
|
}
|
|
}
|