tbf/GenesisCordonelInterface/API/InterfaceOutsideToGCI.cs

441 lines
14 KiB
C#
Raw Normal View History

2026-04-23 07:09:27 +00:00
using System;
using System.Collections.Generic;
using System.IO.Ports;
2026-04-27 05:57:16 +00:00
using System.Threading;
using System.Threading.Tasks;
using static GenesisCordonelInterface.API.PublicModels;
2026-04-23 07:09:27 +00:00
namespace GenesisCordonelInterface.API
{
/// <summary>
/// Outside-facing facade for Genesis Cordonel Interface.
/// Exposes only selected operations intended for external callers.
/// </summary>
public class InterfaceOutsideToGCI
{
public readonly InterfaceGCIToLaatzen _innerMeterAPI;
2026-04-23 07:09:27 +00:00
public event Action<List<MeterBatchDebugStatus>> MeterBatchStatusChanged;
2026-04-27 05:57:16 +00:00
2026-04-23 07:09:27 +00:00
public InterfaceOutsideToGCI()
{
_innerMeterAPI = new InterfaceGCIToLaatzen();
}
#region ================================== PORT DETECTION ==================================
2026-04-27 05:57:16 +00:00
public PortDetectionResult DetectStreamingPort(int slot)
2026-04-23 07:09:27 +00:00
{
2026-04-27 05:57:16 +00:00
var result = _innerMeterAPI.DetectStreamingPort(slot);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
}
public async Task<PortDetectionResult> DetectStreamingPortAsync(
2026-04-27 05:57:16 +00:00
int slot,
CancellationToken token = default(CancellationToken))
{
var result = await _innerMeterAPI.DetectStreamingPortAsync(slot, token);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
2026-04-23 07:09:27 +00:00
}
public PortDetectionResult DetectRequestPort(int slot)
2026-04-23 07:09:27 +00:00
{
2026-04-27 05:57:16 +00:00
var result = _innerMeterAPI.DetectRequestPort(slot);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
2026-04-23 07:09:27 +00:00
}
public async Task<PortDetectionResult> DetectRequestPortAsync(
2026-04-27 05:57:16 +00:00
int slot,
CancellationToken token = default(CancellationToken))
{
var result = await _innerMeterAPI.DetectRequestPortAsync(slot, token);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
}
#endregion
#region ================================== INIT ==================================
2026-04-27 05:57:16 +00:00
public async Task<GciInitSlotResult> InitSlotAsync(
GciInitSlotRequest request,
CancellationToken token = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
var result = await _innerMeterAPI.InitSlotAsync(
request.SlotId,
ModelsMapping.MapConfigSource(request.ConfigSource),
ModelsMapping.MapPasswordSource(request.PasswordSource),
ModelsMapping.MapPort(request.RequestPort),
ModelsMapping.MapPort(request.StreamingPort),
2026-04-27 05:57:16 +00:00
token);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
}
public async Task<GciInitSlotResult> UpdateSlotAsync(
GciInitSlotRequest request,
CancellationToken token = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
var result = await _innerMeterAPI.UpdateSlotAsync(
request.SlotId,
ModelsMapping.MapConfigSource(request.ConfigSource),
ModelsMapping.MapPasswordSource(request.PasswordSource),
ModelsMapping.MapPort(request.RequestPort),
ModelsMapping.MapPort(request.StreamingPort),
token);
//RaiseMeterBatchStatusChanged();
return result;
}
public async Task<GciSlotInfo> GetSlotAsync(
int slotId,
CancellationToken token = default)
2026-04-27 05:57:16 +00:00
{
if (slotId <= 0)
throw new ArgumentException("Invalid slot id.");
var result = await _innerMeterAPI.GetOneMeterInfo(slotId, token);
return result;
2026-04-27 05:57:16 +00:00
}
public async Task<GciAllSlotsInfo> GetAllSlotsAsync(
CancellationToken token = default)
{
var result = await _innerMeterAPI.GetAllMetersInfo(token);
return result;
}
public async Task<GciCleanSlotResult> CleanSlotAsync(
int slot,
CancellationToken token = default)
2026-04-27 05:57:16 +00:00
{
if (slot <= 0)
throw new ArgumentException("Invalid slot id.", nameof(slot));
2026-04-27 05:57:16 +00:00
var result = await _innerMeterAPI.CleanSlotAsync(slot, token);
//RaiseMeterBatchStatusChanged();
return result;
}
public async Task<GciCleanAllSlotsResult> CleanAllSlotsAsync(
CancellationToken token = default)
{
var result = await _innerMeterAPI.CleanAllSlotsAsync(token);
//RaiseMeterBatchStatusChanged();
return result;
}
2026-04-27 05:57:16 +00:00
#endregion
#region ================================== PASSWORD ==================================
public async Task<GciSetPasswordResult> SetPasswordAsync(
int slot,
string password,
CancellationToken token = default)
{
if (slot <= 0)
throw new ArgumentException("Invalid slot id.");
if (string.IsNullOrWhiteSpace(password))
throw new ArgumentException("Password is empty.");
GciSetPasswordResult result = await _innerMeterAPI.SetMeterPasswordAsync(slot, password, token);
RaiseMeterBatchStatusChanged();
return result;
}
#endregion
#region ================================== LOGIN ==================================
public Task<PublicModels.GciLoginResult> LoginOneSlotAsync(
int slot,
CancellationToken token = default)
{
return _innerMeterAPI.LoginOneSlotAsync(slot, token);
}
#endregion
#region ================================== CONNECTION ==================================
2026-04-27 05:57:16 +00:00
public async Task<GciConnectResult> ConnectOneSlotAsync(
int slot,
CancellationToken token = default)
2026-04-23 07:09:27 +00:00
{
GciConnectResult result = await _innerMeterAPI.ConnectOneSlotAsync(slot, token);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
2026-04-23 07:09:27 +00:00
}
public async Task<GciDisconnectResult> DisconnectAsync(
int slot,
CancellationToken token = default)
2026-04-23 07:09:27 +00:00
{
if (slot <= 0)
throw new ArgumentException("Invalid slot id.");
var result = await _innerMeterAPI.DisconnectAsync(slot, token);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
2026-04-23 07:09:27 +00:00
}
#endregion
2026-04-23 07:09:27 +00:00
#region ================================== PCB ==================================
public async Task<GciGetPcbIdResult> GetPcbIdAsync(
int slot,
CancellationToken token = default)
2026-04-23 07:09:27 +00:00
{
if (slot <= 0)
throw new ArgumentException("Invalid slot id.");
2026-04-23 07:09:27 +00:00
var result = await _innerMeterAPI.GetPcbIdAsync(slot, token);
2026-04-27 05:57:16 +00:00
return result;
}
2026-04-27 05:57:16 +00:00
#endregion
#region ================================== READ ==================================
2026-04-27 05:57:16 +00:00
// ----------------------------------------------------
public RegisterReadResult ReadRegister(
2026-04-27 05:57:16 +00:00
int slot,
string registerName)
{
return _innerMeterAPI.ReadRegister(slot, registerName);
}
public Task<RegisterReadResult> ReadRegisterAsync(
2026-04-27 05:57:16 +00:00
int slot,
string registerName,
CancellationToken token = default(CancellationToken))
{
return _innerMeterAPI.ReadRegisterAsync(slot, registerName, token);
}
// ----------------------------------------------------
#endregion
#region ================================== WRITE ==================================
2026-04-27 05:57:16 +00:00
// ----------------------------------------------------
public async Task<RegisterWriteResult> WriteRegisterAsync(
2026-04-27 05:57:16 +00:00
int slot,
string registerName,
object value,
bool storeToDevice = false,
bool refreshSystemState = false,
CancellationToken token = default)
2026-04-23 07:09:27 +00:00
{
2026-04-27 05:57:16 +00:00
var result = await _innerMeterAPI.WriteRegisterAsync(
slot,
registerName,
value,
storeToDevice,
refreshSystemState,
token);
2026-04-27 05:57:16 +00:00
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
2026-04-23 07:09:27 +00:00
}
public RegisterWriteResult WriteRegister(
2026-04-27 05:57:16 +00:00
int slot,
2026-04-23 07:09:27 +00:00
string registerName,
object value,
bool storeToDevice = false,
bool refreshSystemState = false)
{
2026-04-27 05:57:16 +00:00
var result = _innerMeterAPI.WriteRegister(
slot,
registerName,
value,
storeToDevice,
refreshSystemState);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
2026-04-23 07:09:27 +00:00
}
public async Task<GciSetPasswordResult> SetMeterPasswordAsync(int slot, string password)
2026-04-23 07:09:27 +00:00
{
2026-04-27 05:57:16 +00:00
var result = await _innerMeterAPI.SetMeterPasswordAsync(slot, password);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
2026-04-23 07:09:27 +00:00
}
2026-04-27 05:57:16 +00:00
public GciSetPasswordResult SetMeterPassword(int slot, string password)
2026-04-27 05:57:16 +00:00
{
var result = _innerMeterAPI.SetMeterPassword(slot, password);
//RaiseMeterBatchStatusChanged();
2026-04-27 05:57:16 +00:00
return result;
}
// ----------------------------------------------------
#endregion
#region ================================== DEBUG STATUS ==================================
2026-04-27 05:57:16 +00:00
// ----------------------------------------------------
public List<WorkerDebugStatus> GetWorkerDebugStatuses()
2026-04-27 05:57:16 +00:00
{
return _innerMeterAPI.GetWorkerDebugStatuses();
}
public List<MeterBatchDebugStatus> GetMeterBatchDebugStatuses()
2026-04-27 05:57:16 +00:00
{
return _innerMeterAPI.GetMeterBatchDebugStatuses();
}
public void RaiseMeterBatchStatusChanged()
{
var statuses = GetMeterBatchDebugStatuses();
var handler = MeterBatchStatusChanged;
if (handler != null)
handler(statuses);
}
// ----------------------------------------------------
#endregion
#region ================================== SLOT SELECTION ==================================
2026-04-27 05:57:16 +00:00
// ----------------------------------------------------
public void SetSlotSelected(int slot, bool selected)
{
_innerMeterAPI.SetSlotSelected(slot, selected);
RaiseMeterBatchStatusChanged();
}
public bool IsSlotSelected(int slot)
{
return _innerMeterAPI.IsSlotSelected(slot);
}
public List<int> GetSelectedSlots()
{
return _innerMeterAPI.GetSelectedSlots();
}
// ----------------------------------------------------
#endregion
#region ================================== SLOT PORT CONFIG ==================================
2026-04-27 05:57:16 +00:00
// ----------------------------------------------------
/*public void SetSlotRequestPort(int slot, string portName)
2026-04-27 05:57:16 +00:00
{
lock (_portLock)
{
if (string.IsNullOrWhiteSpace(portName))
{
_requestPorts.Remove(slot);
}
else
{
_requestPorts[slot] = new GciPortConfig
2026-04-27 05:57:16 +00:00
{
PortName = portName,
Type = "Serial"
};
}
}
RaiseMeterBatchStatusChanged();
}
public void SetSlotStreamingPort(int slot, string portName)
{
lock (_portLock)
{
if (string.IsNullOrWhiteSpace(portName))
{
_streamingPorts.Remove(slot);
}
else
{
_streamingPorts[slot] = new GciPortConfig
2026-04-27 05:57:16 +00:00
{
PortName = portName,
Type = "Serial"
};
}
}
RaiseMeterBatchStatusChanged();
}
public GciPortConfig? GetSlotRequestPort(int slot)
2026-04-27 05:57:16 +00:00
{
lock (_portLock)
{
GciPortConfig port;
2026-04-27 05:57:16 +00:00
if (_requestPorts.TryGetValue(slot, out port))
return port;
return null;
}
}
public GciPortConfig? GetSlotStreamingPort(int slot)
2026-04-27 05:57:16 +00:00
{
lock (_portLock)
{
GciPortConfig port;
2026-04-27 05:57:16 +00:00
if (_streamingPorts.TryGetValue(slot, out port))
return port;
return null;
}
}*/
2026-04-27 05:57:16 +00:00
// ----------------------------------------------------
#endregion
#region ================================== METER BATCH SETUP ==================================
2026-04-27 05:57:16 +00:00
// ----------------------------------------------------
public void ReloadSlotSetup()
{
_innerMeterAPI.ReloadSlotSetup();
RaiseMeterBatchStatusChanged();
}
public void SaveSlotSetup(List<MeterBatchDebugStatus> data)
2026-04-27 05:57:16 +00:00
{
_innerMeterAPI.SaveSlotSetup(data);
RaiseMeterBatchStatusChanged();
}
// ----------------------------------------------------
#endregion
public List<string> GetAllRegisterNames()
{
return _innerMeterAPI.GetAllRegisterNames();
}
2026-04-23 07:09:27 +00:00
}
}