unit uConfigBascula;

interface

uses
  System.SysUtils, System.Classes, System.IniFiles, System.IOUtils;

type
  TBasculaConfig = class
  private
    FPort: string;
    FBluetoothDeviceName: string;
    FBaudRate: Integer;
    FDataBits: Integer;
    FParity: Integer;      // 0=None, 1=Odd, 2=Even, 3=Mark, 4=Space
    FStopBits: Integer;    // 0=One, 1=One5, 2=Two
    FFlowControl: Integer; // 0=None, 1=RTS/CTS, 2=XON/XOFF
    FProtocolo: Integer;   // 0=Auto/Generico, 1=Continuo CR/LF, 2=Agrigest 0, 3=Agrigest 1, 4=Agrigest 2, 5=Agrigest 3, 6=Agrigest 4
    FPollingEnabled: Boolean;
    FPollingCommand: string;
    FPollingIntervalMs: Integer;
    FAutoConnect: Boolean;
    FUnidad: string;
    FDecimales: Integer;
    FSimulacion: Boolean;
    function GetConfigFilePath: string;
  public
    constructor Create;
    procedure Load;
    procedure Save;

    property Port: string read FPort write FPort;
    property BluetoothDeviceName: string read FBluetoothDeviceName write FBluetoothDeviceName;
    property BaudRate: Integer read FBaudRate write FBaudRate;
    property DataBits: Integer read FDataBits write FDataBits;
    property Parity: Integer read FParity write FParity;
    property StopBits: Integer read FStopBits write FStopBits;
    property FlowControl: Integer read FFlowControl write FFlowControl;
    property Protocolo: Integer read FProtocolo write FProtocolo;
    property PollingEnabled: Boolean read FPollingEnabled write FPollingEnabled;
    property PollingCommand: string read FPollingCommand write FPollingCommand;
    property PollingIntervalMs: Integer read FPollingIntervalMs write FPollingIntervalMs;
    property AutoConnect: Boolean read FAutoConnect write FAutoConnect;
    property Unidad: string read FUnidad write FUnidad;
    property Decimales: Integer read FDecimales write FDecimales;
    property Simulacion: Boolean read FSimulacion write FSimulacion;
  end;

implementation

{ TBasculaConfig }

constructor TBasculaConfig.Create;
begin
  inherited Create;
  // Valores por defecto orientados a DSD TECH SH-B35 y básculas industriales
  FPort := 'COM1';
  FBluetoothDeviceName := 'DSD TECH SH-B35';
  FBaudRate := 9600;
  FDataBits := 8;
  FParity := 0; // None
  FStopBits := 0; // 1 stop bit
  FFlowControl := 0; // None
  FProtocolo := 0; // Auto
  FPollingEnabled := False;
  FPollingCommand := 'P'#13#10;
  FPollingIntervalMs := 300;
  FAutoConnect := False;
  FUnidad := 'kg';
  FDecimales := 3;
  FSimulacion := False;
end;

function TBasculaConfig.GetConfigFilePath: string;
begin
  Result := TPath.Combine(ExtractFilePath(ParamStr(0)), 'bascula_config.ini');
end;

procedure TBasculaConfig.Load;
var
  Ini: TIniFile;
  Path: string;
begin
  Path := GetConfigFilePath;
  if not FileExists(Path) then
    Exit;

  Ini := TIniFile.Create(Path);
  try
    FPort := Ini.ReadString('Comunicacion', 'Port', FPort);
    FBluetoothDeviceName := Ini.ReadString('Comunicacion', 'BluetoothDeviceName', FBluetoothDeviceName);
    FBaudRate := Ini.ReadInteger('Comunicacion', 'BaudRate', FBaudRate);
    FDataBits := Ini.ReadInteger('Comunicacion', 'DataBits', FDataBits);
    FParity := Ini.ReadInteger('Comunicacion', 'Parity', FParity);
    FStopBits := Ini.ReadInteger('Comunicacion', 'StopBits', FStopBits);
    FFlowControl := Ini.ReadInteger('Comunicacion', 'FlowControl', FFlowControl);
    FAutoConnect := Ini.ReadBool('Comunicacion', 'AutoConnect', FAutoConnect);

    FProtocolo := Ini.ReadInteger('Bascula', 'Protocolo', FProtocolo);
    FPollingEnabled := Ini.ReadBool('Bascula', 'PollingEnabled', FPollingEnabled);
    FPollingCommand := Ini.ReadString('Bascula', 'PollingCommand', FPollingCommand);
    FPollingIntervalMs := Ini.ReadInteger('Bascula', 'PollingIntervalMs', FPollingIntervalMs);
    FUnidad := Ini.ReadString('Bascula', 'Unidad', FUnidad);
    FDecimales := Ini.ReadInteger('Bascula', 'Decimales', FDecimales);
    FSimulacion := Ini.ReadBool('Bascula', 'Simulacion', FSimulacion);
  finally
    Ini.Free;
  end;
end;

procedure TBasculaConfig.Save;
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(GetConfigFilePath);
  try
    Ini.WriteString('Comunicacion', 'Port', FPort);
    Ini.WriteString('Comunicacion', 'BluetoothDeviceName', FBluetoothDeviceName);
    Ini.WriteInteger('Comunicacion', 'BaudRate', FBaudRate);
    Ini.WriteInteger('Comunicacion', 'DataBits', FDataBits);
    Ini.WriteInteger('Comunicacion', 'Parity', FParity);
    Ini.WriteInteger('Comunicacion', 'StopBits', FStopBits);
    Ini.WriteInteger('Comunicacion', 'FlowControl', FFlowControl);
    Ini.WriteBool('Comunicacion', 'AutoConnect', FAutoConnect);

    Ini.WriteInteger('Bascula', 'Protocolo', FProtocolo);
    Ini.WriteBool('Bascula', 'PollingEnabled', FPollingEnabled);
    Ini.WriteString('Bascula', 'PollingCommand', FPollingCommand);
    Ini.WriteInteger('Bascula', 'PollingIntervalMs', FPollingIntervalMs);
    Ini.WriteString('Bascula', 'Unidad', FUnidad);
    Ini.WriteInteger('Bascula', 'Decimales', FDecimales);
    Ini.WriteBool('Bascula', 'Simulacion', FSimulacion);
  finally
    Ini.Free;
  end;
end;

end.
