154 lines
6.9 KiB
C#
154 lines
6.9 KiB
C#
namespace TuneBoss.Core.Models;
|
|
|
|
/// <summary>
|
|
/// What the app should do after the post-unlock server status check.
|
|
/// Mirrors the Flutter <c>checkEmailStatus</c> gate (doneAfterConnect):
|
|
/// Pass → continue into the app,
|
|
/// Warn → continue, but show a warning first,
|
|
/// Block → show the message, disconnect, do NOT load tables.
|
|
/// </summary>
|
|
public enum EcuStatusOutcome
|
|
{
|
|
Pass,
|
|
Warn,
|
|
Block,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Result of the post-unlock ECU status check against the TracBoss account server.
|
|
/// </summary>
|
|
public sealed record EcuStatusResult(
|
|
EcuStatusOutcome Outcome,
|
|
string Message,
|
|
int? SuccessCode,
|
|
bool ServerReachable)
|
|
{
|
|
public bool IsBlocked => Outcome == EcuStatusOutcome.Block;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pure interpretation of the server's <c>success</c> code into an app outcome.
|
|
/// No HTTP or UI here so it can be unit-tested and reused.
|
|
///
|
|
/// Ported from the Flutter app's <c>checkEmailStatus</c> (lib/services/tb_interactor.dart),
|
|
/// MINUS the ECU activation-bit (reg1Reg2) second chance and the global payment/systemSync
|
|
/// side effects — those are a later phase. Where the Flutter code relied on reg1Reg2 to
|
|
/// rescue a "not activated" verdict, this build hard-blocks instead.
|
|
/// </summary>
|
|
public static class EcuStatusInterpreter
|
|
{
|
|
/// <summary>FW below this is a legacy unit that predates activation — always allowed.</summary>
|
|
public const int LegacyFwCeiling = 150;
|
|
|
|
/// <summary>
|
|
/// Interpret a definitive server response. Mirrors the legacy Android app's
|
|
/// <c>CheckEmailStatus</c> success-code handling, including the ECU activation-bit
|
|
/// (<paramref name="isActivated"/>) rescue on the "not registered" verdicts (5 and 0).
|
|
/// </summary>
|
|
/// <param name="success">The server's <c>success</c> status code.</param>
|
|
/// <param name="fwVersion">This unit's firmware version (for the legacy-ECU branch).</param>
|
|
/// <param name="isActivated">ECU self-reported activation (<c>Reg1_Reg2 > 0</c>).</param>
|
|
/// <param name="serverMessage">Optional human message echoed from the server.</param>
|
|
public static EcuStatusResult Interpret(int success, byte fwVersion, bool isActivated,
|
|
string? serverMessage = null)
|
|
{
|
|
string msg = string.IsNullOrWhiteSpace(serverMessage) ? "" : serverMessage!.Trim();
|
|
|
|
switch (success)
|
|
{
|
|
// Registered / global / dealer-bypass — full access.
|
|
case 1:
|
|
case 2:
|
|
case 3:
|
|
case 8:
|
|
return new EcuStatusResult(
|
|
EcuStatusOutcome.Pass,
|
|
success == 8 ? "Dealer-bypassed ECU — access granted."
|
|
: "ECU registered — access granted.",
|
|
success, ServerReachable: true);
|
|
|
|
// Activated but not warranty-registered — allowed, with a warning.
|
|
case 6:
|
|
case 7:
|
|
return new EcuStatusResult(
|
|
EcuStatusOutcome.Warn,
|
|
"This ECU is activated but not registered for warranty.",
|
|
success, ServerReachable: true);
|
|
|
|
// Phone not registered. Legacy: FW < 150 → pass; else if activated → pass with a
|
|
// "password change disabled" warning; else block.
|
|
case 5:
|
|
if (fwVersion < LegacyFwCeiling)
|
|
return new EcuStatusResult(EcuStatusOutcome.Pass,
|
|
"Legacy ECU — access granted.", success, ServerReachable: true);
|
|
return isActivated
|
|
? new EcuStatusResult(EcuStatusOutcome.Warn,
|
|
"This ECU is activated but not registered for warranty. "
|
|
+ "Password change is disabled.",
|
|
success, ServerReachable: true)
|
|
: NotActivated(success);
|
|
|
|
// Unregistered. Legacy: FW < 150 → pass; else if activated → pass; else block.
|
|
case 0:
|
|
if (fwVersion < LegacyFwCeiling)
|
|
return new EcuStatusResult(EcuStatusOutcome.Pass,
|
|
"Legacy ECU — access granted.", success, ServerReachable: true);
|
|
return isActivated
|
|
? new EcuStatusResult(EcuStatusOutcome.Pass,
|
|
"ECU activated — access granted.", success, ServerReachable: true)
|
|
: NotActivated(success);
|
|
|
|
// Account removed — warranty void, but access is allowed.
|
|
case 9:
|
|
return new EcuStatusResult(
|
|
EcuStatusOutcome.Warn,
|
|
"The account for this ECU has been removed — warranty is void.",
|
|
success, ServerReachable: true);
|
|
|
|
// App out of date (server rejected the request shape).
|
|
case 98:
|
|
return new EcuStatusResult(
|
|
EcuStatusOutcome.Block,
|
|
"Missing parameter. Please update TuneBoss PC to the latest version.",
|
|
success, ServerReachable: true);
|
|
|
|
default:
|
|
return new EcuStatusResult(
|
|
EcuStatusOutcome.Block,
|
|
$"Unrecognised ECU status from server (code {success})"
|
|
+ (msg.Length > 0 ? $": {msg}" : "."),
|
|
success, ServerReachable: true);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Outcome when the server can't be reached (offline, timeout, or a transport/parse error).
|
|
/// Mirrors the legacy Android offline / server-error fallback exactly: FW < 150 passes,
|
|
/// an activated ECU (<paramref name="isActivated"/>) passes, and only a genuinely
|
|
/// unactivated modern unit is blocked. This is why an activated unit still works offline.
|
|
/// </summary>
|
|
public static EcuStatusResult Unreachable(byte fwVersion, bool isActivated, string? reason = null)
|
|
{
|
|
if (fwVersion < LegacyFwCeiling)
|
|
return new EcuStatusResult(EcuStatusOutcome.Pass,
|
|
"Legacy ECU — access granted (server not contacted).", SuccessCode: null,
|
|
ServerReachable: false);
|
|
|
|
if (isActivated)
|
|
return new EcuStatusResult(EcuStatusOutcome.Pass,
|
|
"ECU activated — access granted (server not contacted).", SuccessCode: null,
|
|
ServerReachable: false);
|
|
|
|
string why = string.IsNullOrWhiteSpace(reason) ? "" : $" ({reason})";
|
|
return new EcuStatusResult(EcuStatusOutcome.Block,
|
|
"This ECU has not been activated, and the activation server could not be reached "
|
|
+ "to verify it. Please activate the ECU with your dealer." + why,
|
|
SuccessCode: null, ServerReachable: false);
|
|
}
|
|
|
|
private static EcuStatusResult NotActivated(int success) => new(
|
|
EcuStatusOutcome.Block,
|
|
"This ECU has not been activated. Please activate it with your dealer.",
|
|
success, ServerReachable: true);
|
|
}
|