sharee.bike-App/TINKLib/Repository/Exception/NotAtStationException.cs

53 lines
1.6 KiB
C#
Raw Normal View History

2022-09-16 11:19:46 +02:00
using System.Text.RegularExpressions;
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
namespace TINK.Repository.Exception
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
public class NotAtStationException : InvalidResponseException
{
2022-09-16 11:19:46 +02:00
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_]+)";
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Prevents invalid use of exception. </summary>
private NotAtStationException() : base(typeof(NotAtStationException).Name)
{
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
public static bool IsNotAtStation(string responseState, out NotAtStationException exception)
{
// Check if there are too many bikes requested/ booked.
2022-09-16 11:19:46 +02:00
var response = responseState.Trim().ToUpper();
if (!response.StartsWith(RETURNBIKE_FAILURE_STATUS_MESSAGE_CODE))
{
exception = null;
return false;
}
2022-09-06 16:08:19 +02:00
var match = Regex.Match(
responseState.ToUpper(),
RETURNBIKE_FAILURE_STATUS_MESSAGE_UPPERCASE);
if (match.Groups.Count != 5
2022-09-16 11:19:46 +02:00
|| !int.TryParse(match.Groups[2].ToString(), out int meters))
2022-09-06 16:08:19 +02:00
{
2022-09-16 11:19:46 +02:00
exception = new NotAtStationException();
return true;
2022-09-06 16:08:19 +02:00
}
2022-09-16 11:19:46 +02:00
exception = new NotAtStationException {
Distance = meters,
StationNr = match.Groups[4].ToString()
};
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return true;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Holds the maximum count of bikes allowed to reserve/ book.</summary>
2022-09-16 11:19:46 +02:00
public int? Distance { get; private set; } = null;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Holds the maximum count of bikes allowed to reserve/ book.</summary>
2022-09-16 11:19:46 +02:00
public string StationNr { get; private set; } = string.Empty;
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:03:07 +02:00
}