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

283 lines
12 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System;
using System.Collections.Generic;
using Serilog;
2022-08-30 15:42:25 +02:00
using TINK.Model.Bikes;
using TINK.Model.State;
using TINK.Model.Station;
using TINK.Model.Station.Operator;
2022-08-30 15:42:25 +02:00
using TINK.Model.User.Account;
using TINK.Repository.Exception;
using TINK.Repository.Response;
2022-01-04 18:54:03 +01:00
using TINK.Services.CopriApi;
2022-08-30 15:42:25 +02:00
using Xamarin.Forms;
using BikeInfo = TINK.Model.Bikes.BikeInfoNS.BC.BikeInfo;
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
{
/// <summary>
/// Connects TINK app to copri using JSON as input data format.
/// </summary>
/// <todo>Rename to UpdateFromCopri.</todo>
public static class UpdaterJSON
{
/// <summary> Loads a bike object from copri server cancel reservation/ booking update request.</summary>
/// <param name="bike">Bike object to load response into.</param>
/// <param name="notifyLevel">Controls whether notify property changed events are fired or not.</param>
public static void Load(
this IBikeInfoMutable bike,
2022-08-30 15:42:25 +02:00
Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel notifyLevel)
2021-05-13 20:03:07 +02:00
{
bike.State.Load(InUseStateEnum.Disposable, notifyLevel: notifyLevel);
}
2022-08-30 15:42:25 +02:00
2021-05-13 20:03:07 +02:00
/// <summary>
/// Gets all statsion for station provider and add them into station list.
/// </summary>
/// <param name="p_oStationList">List of stations to update.</param>
public static StationDictionary GetStationsAllMutable(this StationsAvailableResponse stationsAllResponse)
2021-05-13 20:03:07 +02:00
{
// Get stations from Copri/ file/ memory, ....
if (stationsAllResponse == null
|| stationsAllResponse.stations == null)
2021-05-13 20:03:07 +02:00
{
// Latest list of stations could not be retrieved from provider.
return new StationDictionary();
}
Version.TryParse(stationsAllResponse.copri_version, out Version copriVersion);
2021-05-13 20:03:07 +02:00
var stations = new StationDictionary(p_oVersion: copriVersion);
2021-05-13 20:03:07 +02:00
foreach (var station in stationsAllResponse.stations)
2021-05-13 20:03:07 +02:00
{
if (stations.GetById(station.Value.station) != null)
2021-05-13 20:03:07 +02:00
{
// 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);
2021-05-13 20:03:07 +02:00
}
stations.Add(new Station.Station(
station.Value.station,
station.Value.GetGroup(),
station.Value.GetPosition(),
station.Value.description,
new Data(station.Value.operator_data?.operator_name,
station.Value.operator_data?.operator_phone,
station.Value.operator_data?.operator_hours,
station.Value.operator_data?.operator_email,
2022-04-25 22:15:15 +02:00
!string.IsNullOrEmpty(station.Value.operator_data?.operator_color)
? Color.FromHex(station.Value.operator_data?.operator_color)
: (Color?)null)));
2021-05-13 20:03:07 +02:00
}
return stations;
2021-05-13 20:03:07 +02:00
}
2022-01-04 18:54:03 +01:00
/// <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)
2022-01-22 18:16:10 +01:00
=> new GeneralData(
2022-04-25 22:15:15 +02:00
response.init_map.GetMapSpan(),
response.merchant_message,
response.TryGetCopriVersion(out Version copriVersion)
? new Version(0, 0)
2022-01-22 18:28:01 +01:00
: copriVersion,
new ResourceUrls(response.tariff_info_html, response.bike_info_html, response.agb_html, response.privacy_html, response.impress_html));
2022-01-04 18:54:03 +01:00
2021-05-13 20:03:07 +02:00
/// <summary> Gets account object from login response.</summary>
/// <param name="merchantId">Needed to extract cookie from autorization response.</param>
/// <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)
{
2021-11-08 23:11:56 +01:00
throw new ArgumentNullException(nameof(loginResponse));
2021-05-13 20:03:07 +02:00
}
return new Account(
mail,
password,
2022-01-04 18:59:16 +01:00
loginResponse.GetIsAgbAcknowledged(),
2021-05-13 20:03:07 +02:00
loginResponse.authcookie?.Replace(merchantId, ""),
loginResponse.GetGroup(),
loginResponse.debuglevel == 1
2022-04-25 22:15:15 +02:00
? Permissions.All :
(Permissions)loginResponse.debuglevel);
2021-05-13 20:03:07 +02:00
}
/// <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,
2022-08-30 15:42:25 +02:00
Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel notifyLevel = Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.All)
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
if (bike is Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable btBikeInfo)
2021-05-13 20:03:07 +02:00
{
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(),
mailAddress,
bikeInfo.timeCode,
notifyLevel);
break;
case InUseStateEnum.Booked:
bike.State.Load(
InUseStateEnum.Booked,
bikeInfo.GetFrom(),
mailAddress,
2022-04-25 22:15:15 +02:00
bikeInfo.timeCode,
2021-05-13 20:03:07 +02:00
notifyLevel);
break;
default:
throw new Exception(string.Format("Unexpected bike state detected. state is {0}.", l_oState));
}
}
/// <summary> Gets bikes available from copri server response.</summary>
2021-11-08 23:11:56 +01:00
/// <param name="bikesAvailableResponse">Response to create collection from.</param>
2021-05-13 20:03:07 +02:00
/// <returns>New collection of available bikes.</returns>
public static BikeCollection GetBikesAvailable(
2021-11-08 23:11:56 +01:00
this BikesAvailableResponse bikesAvailableResponse)
2022-08-30 15:42:25 +02:00
=> GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
new BikesReservedOccupiedResponse()?.bikes_occupied?.Values, // There are no occupied bikes.
2021-05-13 20:03:07 +02:00
string.Empty,
() => DateTime.Now);
/// <summary> Gets bikes occupied from copri server response. </summary>
/// <param name="p_oBikesAvailable">Response to create bikes from.</param>
/// <returns>New collection of occupied bikes.</returns>
public static BikeCollection GetBikesOccupied(
2021-07-12 19:30:14 +02:00
this BikesReservedOccupiedResponse bikesOccupiedResponse,
string mail,
Func<DateTime> dateTimeProvider)
2021-05-13 20:03:07 +02:00
{
return GetBikesAll(
2022-08-30 15:42:25 +02:00
new BikesAvailableResponse()?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
2021-07-12 19:30:14 +02:00
mail,
dateTimeProvider);
2021-05-13 20:03:07 +02:00
}
/// <summary> Gets bikes occupied from copri server response. </summary>
2022-08-30 15:42:25 +02:00
/// <param name="bikesAvailable">Response to create bikes available from.</param>
/// <param name="bikesOccupied">Response to create bikes occupied from.</param>
2021-05-13 20:03:07 +02:00
/// <returns>New collection of occupied bikes.</returns>
public static BikeCollection GetBikesAll(
2022-08-30 15:42:25 +02:00
IEnumerable<BikeInfoAvailable> bikesAvailable,
IEnumerable<BikeInfoReservedOrBooked> bikesOccupied,
2021-11-08 23:11:56 +01:00
string mail,
Func<DateTime> dateTimeProvider)
2021-05-13 20:03:07 +02:00
{
2021-07-12 19:30:14 +02:00
var bikesDictionary = new Dictionary<string, BikeInfo>();
var duplicates = new Dictionary<string, BikeInfo>();
2021-05-13 20:03:07 +02:00
// Get bikes from Copri/ file/ memory, ....
2022-08-30 15:42:25 +02:00
if (bikesAvailable != null)
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
foreach (var bikeInfoResponse in bikesAvailable)
2021-05-13 20:03:07 +02:00
{
2021-07-12 19:30:14 +02:00
var bikeInfo = BikeInfoFactory.Create(bikeInfoResponse);
if (bikeInfo == null)
2021-05-13 20:03:07 +02:00
{
// Response is not valid.
continue;
}
2021-07-12 19:30:14 +02:00
if (bikesDictionary.ContainsKey(bikeInfo.Id))
2021-05-13 20:03:07 +02:00
{
// Duplicates are not allowed.
2021-07-12 19:30:14 +02:00
Log.Error($"Duplicate bike with id {bikeInfo.Id} detected evaluating bikes available. Bike status is {bikeInfo.State.Value}.");
2021-05-13 20:03:07 +02:00
2021-07-12 19:30:14 +02:00
if (!duplicates.ContainsKey(bikeInfo.Id))
2021-05-13 20:03:07 +02:00
{
2021-07-12 19:30:14 +02:00
duplicates.Add(bikeInfo.Id, bikeInfo);
2021-05-13 20:03:07 +02:00
}
continue;
}
2021-07-12 19:30:14 +02:00
bikesDictionary.Add(bikeInfo.Id, bikeInfo);
2021-05-13 20:03:07 +02:00
}
}
// Get bikes from Copri/ file/ memory, ....
2022-08-30 15:42:25 +02:00
if (bikesOccupied != null)
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
foreach (var bikeInfoResponse in bikesOccupied)
2021-05-13 20:03:07 +02:00
{
2021-07-12 19:30:14 +02:00
BikeInfo bikeInfo = BikeInfoFactory.Create(
bikeInfoResponse,
2021-11-08 23:11:56 +01:00
mail,
dateTimeProvider);
2021-05-13 20:03:07 +02:00
2021-07-12 19:30:14 +02:00
if (bikeInfo == null)
2021-05-13 20:03:07 +02:00
{
continue;
}
2021-07-12 19:30:14 +02:00
if (bikesDictionary.ContainsKey(bikeInfo.Id))
2021-05-13 20:03:07 +02:00
{
// Duplicates are not allowed.
2021-07-12 19:30:14 +02:00
Log.Error($"Duplicate bike with id {bikeInfo.Id} detected evaluating bikes occupied. Bike status is {bikeInfo.State.Value}.");
if (!duplicates.ContainsKey(bikeInfo.Id))
2021-05-13 20:03:07 +02:00
{
2021-07-12 19:30:14 +02:00
duplicates.Add(bikeInfo.Id, bikeInfo);
2021-05-13 20:03:07 +02:00
}
continue;
}
2021-07-12 19:30:14 +02:00
bikesDictionary.Add(bikeInfo.Id, bikeInfo);
2021-05-13 20:03:07 +02:00
}
}
// Remove entries which are not unique.
2021-07-12 19:30:14 +02:00
foreach (var l_oDuplicate in duplicates)
2021-05-13 20:03:07 +02:00
{
2021-07-12 19:30:14 +02:00
bikesDictionary.Remove(l_oDuplicate.Key);
2021-05-13 20:03:07 +02:00
}
2021-07-12 19:30:14 +02:00
return new BikeCollection(bikesDictionary);
2021-05-13 20:03:07 +02:00
}
}
}