using System.Collections.Generic;
using Peak.Can.Basic;
using TPCANHandle = System.UInt16;
namespace HC_APTBS.Models
{
///
/// Complete bench parameter configuration: CAN parameter map plus relay definitions.
/// Loaded from bench.xml at startup and edited via the BenchParamConfig view.
///
public class BenchConfiguration
{
/// PCAN channel handle used for bench communication.
public TPCANHandle Channel { get; set; } = PCANBasic.PCAN_USBBUS1;
/// All bench parameters, keyed by name for quick lookup.
public Dictionary ParametersByName { get; set; } = new();
/// Bench parameters grouped by CAN message ID for frame decoding.
public Dictionary> ParametersById { get; set; } = new();
/// Relay / solenoid output definitions, keyed by name.
public Dictionary Relays { get; set; } = new();
}
///
/// Application-wide configuration settings persisted in config.xml.
///
public class AppSettings
{
// ── Temperature control ───────────────────────────────────────────────
/// Maximum allowable oil temperature (°C) before the bench raises an alarm.
public int TempMax { get; set; } = 45;
/// Minimum oil temperature (°C) required before tests begin.
public int TempMin { get; set; } = 35;
// ── Refresh intervals ─────────────────────────────────────────────────
/// UI status refresh period (ms) during normal bench operation.
public int RefreshBenchInterfaceMs { get; set; } = 20;
/// UI refresh period (ms) while reading pump EEPROM data over K-Line.
public int RefreshWhileReadingMs { get; set; } = 1500;
/// CAN bus polling interval (ms) on the read thread.
public int RefreshCanBusReadMs { get; set; } = 2;
/// Interval (ms) between pump-status CAN request messages.
public int RefreshPumpRequestMs { get; set; } = 250;
/// Interval (ms) between pump-parameter CAN request messages.
public int RefreshPumpParamsMs { get; set; } = 4;
/// Blink period (ms) for LED indicator controls.
public int BlinkIntervalMs { get; set; } = 1000;
/// Flasher relay toggle interval (ms).
public int FlasherIntervalMs { get; set; } = 800;
// ── PID temperature controller ────────────────────────────────────────
/// Proportional gain.
public double PidP { get; set; } = 0.1;
/// Integral gain.
public double PidI { get; set; } = 0.1;
/// Derivative gain.
public double PidD { get; set; } = 0.04;
/// PID loop period (ms).
public int PidLoopMs { get; set; } = 250;
// ── Safety limits ─────────────────────────────────────────────────────
/// Maximum allowable bench speed (RPM) before the bench triggers an emergency stop.
public int SecurityRpmLimit { get; set; } = 2500;
/// Maximum allowable bench pressure (bar).
public int MaxPressureBar { get; set; } = 26;
// ── Test tolerance extensions ─────────────────────────────────────────
///
/// Fractional tolerance extension for UP tests.
/// A value of 0.08 extends each tolerance bound by 8% of its magnitude.
///
public double ToleranceUpExtension { get; set; } = 0.08;
///
/// Fractional tolerance extension for PFP tests.
/// A value of 0.1 extends each tolerance bound by 10% of its magnitude.
///
public double TolerancePfpExtension { get; set; } = 0.1;
// ── Encoder ───────────────────────────────────────────────────────────
/// Encoder pulses per revolution (default 4096 for PSG encoder).
public int EncoderResolution { get; set; } = 4096;
// ── Motor control ─────────────────────────────────────────────────────
/// Analogue output voltage (V) that corresponds to maximum RPM.
public double VoltageForMaxRpm { get; set; } = 10;
/// Maximum motor speed the bench can command (RPM).
public int MaxRpm { get; set; } = 2500;
// ── Direction ─────────────────────────────────────────────────────────
/// Default relay state for the "right" rotation direction.
public bool RightRelayValue { get; set; } = true;
/// Last rotation direction selected by the operator.
public short LastRotationDirection { get; set; } = RotationDirection.Right;
/// When true, the T-in temperature sensor check is bypassed.
public bool DefaultIgnoreTin { get; set; } = true;
// ── Log rotation ──────────────────────────────────────────────────────
/// Number of daily log files to retain before the oldest is deleted.
public int DaysKeepLogs { get; set; } = 7;
// ── Report ────────────────────────────────────────────────────────────
/// Company name printed in the report header.
public string CompanyName { get; set; } = string.Empty;
/// Company address/info printed in the report header.
public string CompanyInfo { get; set; } = string.Empty;
/// Absolute path to the company logo image for the report.
public string ReportLogoPath { get; set; } = string.Empty;
///
/// Comma-separated user:salt:hash credential entries for operator authentication
/// before report generation. Salt is 16-byte Base64, hash is PBKDF2-HMAC-SHA256 (600 000 iterations, 32-byte output) Base64.
/// Legacy user:password entries are auto-migrated on first load.
///
public string Users { get; set; } = string.Empty;
// ── K-Line port ───────────────────────────────────────────────────────
/// Serial port or FTDI device identifier for the K-Line interface.
public string KLinePort { get; set; } = string.Empty;
// ── UI ────────────────────────────────────────────────────────────────
/// UI language code, e.g. "ESP" or "ENG".
public string Language { get; set; } = "ESP";
// ── Relations ─────────────────────────────────────────────────────────
/// RPM-to-voltage lookup table for motor speed control.
public List Relations { get; set; } = new();
// ── Sensor calibration ────────────────────────────────────────────────
/// Calibration data for each analogue sensor channel (keyed by channel number).
public Dictionary Sensors { get; set; } = new();
// ── Alarms ────────────────────────────────────────────────────────────
/// Active alarm definitions loaded from alarms.xml.
public List Alarms { get; set; } = new();
}
}