using System;
using TINK.Model.Bikes.BikeInfoNS.BC;
using TINK.Model.State;
using TINK.Model.Stations;
using TINK.Model.User.Account;
using TINK.Repository.Exception;
using TINK.Repository.Response;
using TINK.Repository.Response.Stations;
using TINK.Services.CopriApi;
using IBikeInfoMutable = TINK.Model.Bikes.BikeInfoNS.BC.IBikeInfoMutable;
namespace TINK.Model.Connector.Updater
{
///
/// Connects TINK app to copri using JSON as input data format.
///
/// Rename to UpdateFromCopri.
public static class UpdaterJSON
{
///
/// Gets all station for station provider and add them into station list.
///
/// List of stations to update.
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);
var stations = new StationDictionary(version: copriVersion);
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(
string.Format("Station id {0} is not unique.", station.Value.station), stationsAllResponse);
}
stations.Add(station.Value.GetStation());
}
return stations;
}
///
/// Gets general data from COPRI response.
///
/// Response to get data from.
/// General data object initialized form COPRI response.
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));
/// Gets account object from login response.
/// Needed to extract cookie from authorization response.
/// Response to get session cookie and debug level from.
/// Mail address needed to construct a complete account object (is not part of response).
/// Password needed to construct a complete account object (is not part of response).
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);
}
/// Load bike object from booking response.
/// Bike object to load from response.
/// Booking response.
/// Mail address of user which books bike.
/// Controls whether notify property changed events are fired or not.
public static void Load(
this IBikeInfoMutable bike,
BikeInfoReservedOrBooked bikeInfo,
string mailAddress,
NotifyPropertyChangedLevel notifyLevel = NotifyPropertyChangedLevel.All)
{
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(),
bikeInfo.rental_description.GetMaxReservationTimeSpan(),
mailAddress,
bikeInfo.timeCode,
notifyLevel);
break;
case InUseStateEnum.Booked:
bike.State.Load(
InUseStateEnum.Booked,
bikeInfo.GetFrom(),
mailAddress: mailAddress,
code: bikeInfo.timeCode,
notifyLevel: notifyLevel);
break;
default:
throw new Exception(string.Format("Unexpected bike state detected. state is {0}.", l_oState));
}
}
}
}