sharee.bike-App/TINKLib/Model/Connector/Updater/UpdaterJSON.cs

150 lines
4.9 KiB
C#
Raw Normal View History

2023-01-18 14:22:51 +01:00
using System;
using TINK.Model.Bikes.BikeInfoNS.BC;
2022-08-30 15:42:25 +02:00
using TINK.Model.State;
2023-04-19 12:14:14 +02:00
using TINK.Model.Stations;
2022-08-30 15:42:25 +02:00
using TINK.Model.User.Account;
using TINK.Repository.Exception;
using TINK.Repository.Response;
2023-04-19 12:14:14 +02:00
using TINK.Repository.Response.Stations;
2022-01-04 18:54:03 +01:00
using TINK.Services.CopriApi;
2022-08-30 15:42:25 +02:00
using IBikeInfoMutable = TINK.Model.Bikes.BikeInfoNS.BC.IBikeInfoMutable;
2021-05-13 20:03:07 +02:00
2022-08-30 15:42:25 +02:00
namespace TINK.Model.Connector.Updater
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
/// <summary>
/// Connects TINK app to copri using JSON as input data format.
/// </summary>
/// <todo>Rename to UpdateFromCopri.</todo>
public static class UpdaterJSON
{
/// <summary>
2023-04-19 12:14:14 +02:00
/// Gets all station for station provider and add them into station list.
2022-09-06 16:08:19 +02:00
/// </summary>
2023-05-09 08:47:52 +02:00
/// <param name="stationsAllResponse">List of stations to update.</param>
2022-09-06 16:08:19 +02:00
public static StationDictionary GetStationsAllMutable(this StationsAvailableResponse stationsAllResponse)
{
// Get stations from Copri/ file/ memory, ....
if (stationsAllResponse == null
|| stationsAllResponse.stations == null)
{
// Latest list of stations could not be retrieved from provider.
return new StationDictionary();
}
Version.TryParse(stationsAllResponse.copri_version, out Version copriVersion);
2023-05-09 08:47:52 +02:00
var stations = new StationDictionary(version: copriVersion);
2022-09-06 16:08:19 +02:00
foreach (var station in stationsAllResponse.stations)
{
if (stations.GetById(station.Value.station) != null)
{
// Can not add station to list of station. Id is not unique.
throw new InvalidResponseException<StationsAvailableResponse>(
string.Format("Station id {0} is not unique.", station.Value.station), stationsAllResponse);
}
2023-05-09 08:47:52 +02:00
stations.Add(station.Value.GetStation());
2022-09-06 16:08:19 +02:00
}
return stations;
}
/// <summary>
/// Gets general data from COPRI response.
/// </summary>
/// <param name="response">Response to get data from.</param>
/// <returns>General data object initialized form COPRI response.</returns>
public static GeneralData GetGeneralData(this ResponseBase response)
=> new GeneralData(
response.init_map.GetMapSpan(),
response.merchant_message,
response.TryGetCopriVersion(out Version copriVersion)
? new Version(0, 0)
: copriVersion,
new ResourceUrls(response.tariff_info_html, response.bike_info_html, response.agb_html, response.privacy_html, response.impress_html));
/// <summary> Gets account object from login response.</summary>
2023-04-19 12:14:14 +02:00
/// <param name="merchantId">Needed to extract cookie from authorization response.</param>
2022-09-06 16:08:19 +02:00
/// <param name="loginResponse">Response to get session cookie and debug level from.</param>
/// <param name="mail">Mail address needed to construct a complete account object (is not part of response).</param>
/// <param name="password">Password needed to construct a complete account object (is not part of response).</param>
public static IAccount GetAccount(
this AuthorizationResponse loginResponse,
string merchantId,
string mail,
string password)
{
if (loginResponse == null)
{
throw new ArgumentNullException(nameof(loginResponse));
}
return new Account(
mail,
password,
loginResponse.GetIsAgbAcknowledged(),
loginResponse.authcookie?.Replace(merchantId, ""),
loginResponse.GetGroup(),
loginResponse.debuglevel == 1
? Permissions.All :
(Permissions)loginResponse.debuglevel);
}
/// <summary> Load bike object from booking response. </summary>
/// <param name="bike">Bike object to load from response.</param>
/// <param name="bikeInfo">Booking response.</param>
/// <param name="mailAddress">Mail address of user which books bike.</param>
/// <param name="notifyLevel">Controls whether notify property changed events are fired or not.</param>
public static void Load(
this IBikeInfoMutable bike,
BikeInfoReservedOrBooked bikeInfo,
string mailAddress,
2023-01-18 14:22:51 +01:00
NotifyPropertyChangedLevel notifyLevel = NotifyPropertyChangedLevel.All)
2022-09-06 16:08:19 +02:00
{
if (bike is Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable btBikeInfo)
{
btBikeInfo.LockInfo.Load(
bikeInfo.GetBluetoothLockId(),
bikeInfo.GetBluetoothLockGuid(),
bikeInfo.GetSeed(),
bikeInfo.GetUserKey(),
bikeInfo.GetAdminKey());
}
var l_oState = bikeInfo.GetState();
switch (l_oState)
{
case InUseStateEnum.Disposable:
bike.State.Load(
InUseStateEnum.Disposable,
notifyLevel: notifyLevel);
break;
case InUseStateEnum.Reserved:
bike.State.Load(
InUseStateEnum.Reserved,
bikeInfo.GetFrom(),
2023-06-06 12:00:24 +02:00
bikeInfo.rental_description.GetMaxReservationTimeSpan(),
2022-09-06 16:08:19 +02:00
mailAddress,
bikeInfo.timeCode,
notifyLevel);
break;
case InUseStateEnum.Booked:
bike.State.Load(
InUseStateEnum.Booked,
bikeInfo.GetFrom(),
2023-06-06 12:00:24 +02:00
mailAddress: mailAddress,
code: bikeInfo.timeCode,
notifyLevel: notifyLevel);
2022-09-06 16:08:19 +02:00
break;
default:
throw new Exception(string.Format("Unexpected bike state detected. state is {0}.", l_oState));
}
}
}
2021-05-13 20:03:07 +02:00
}