444 lines
14 KiB
C#
444 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using static GenesisCordonelInterface.API.PublicModels;
|
|
|
|
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;
|
|
|
|
public event Action<List<MeterBatchDebugStatus>> MeterBatchStatusChanged;
|
|
|
|
public InterfaceOutsideToGCI()
|
|
{
|
|
_innerMeterAPI = new InterfaceGCIToLaatzen();
|
|
}
|
|
|
|
#region ================================== PORT DETECTION ==================================
|
|
|
|
public PortDetectionResult DetectStreamingPort(int slot)
|
|
{
|
|
var result = _innerMeterAPI.DetectStreamingPort(slot);
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
public async Task<PortDetectionResult> DetectStreamingPortAsync(
|
|
int slot,
|
|
CancellationToken token = default(CancellationToken))
|
|
{
|
|
var result = await _innerMeterAPI.DetectStreamingPortAsync(slot, token);
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
public PortDetectionResult DetectRequestPort(int slot)
|
|
{
|
|
var result = _innerMeterAPI.DetectRequestPort(slot);
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
public async Task<PortDetectionResult> DetectRequestPortAsync(
|
|
int slot,
|
|
CancellationToken token = default(CancellationToken))
|
|
{
|
|
var result = await _innerMeterAPI.DetectRequestPortAsync(slot, token);
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ================================== INIT ==================================
|
|
|
|
/*public async Task<GciInitSlotResult> InitSlotAsync(GciInitSlotRequest request, CancellationToken token = default)
|
|
{
|
|
if (request == null)
|
|
throw new ArgumentNullException(nameof(request));
|
|
|
|
var result = await _innerMeterAPI.InitOneMeterFromExternAsync(
|
|
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<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),
|
|
token);
|
|
|
|
RaiseMeterBatchStatusChanged();
|
|
|
|
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)
|
|
{
|
|
if (slotId <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await _innerMeterAPI.GetOneMeterInfo(slotId, token);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciAllSlotsInfo> GetAllSlotsAsync(
|
|
CancellationToken token = default)
|
|
{
|
|
var result = await _innerMeterAPI.GetAllMetersInfo(token);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciCleanSlotsResult> CleanSlotsAsync(
|
|
CancellationToken token = default)
|
|
{
|
|
var result = await _innerMeterAPI.CleanSlotsAsync(token);
|
|
|
|
RaiseMeterBatchStatusChanged();
|
|
|
|
return result;
|
|
}
|
|
#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 ==================================
|
|
|
|
public async Task<GciConnectResult> ConnectOneSlotAsync(
|
|
int slot,
|
|
CancellationToken token = default)
|
|
{
|
|
GciConnectResult result = await _innerMeterAPI.ConnectOneSlotAsync(slot, token);
|
|
|
|
RaiseMeterBatchStatusChanged();
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciDisconnectResult> DisconnectAsync(
|
|
int slot,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await _innerMeterAPI.DisconnectAsync(slot, token);
|
|
|
|
RaiseMeterBatchStatusChanged();
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region ================================== PCB ==================================
|
|
public async Task<GciGetPcbIdResult> GetPcbIdAsync(
|
|
int slot,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await _innerMeterAPI.GetPcbIdAsync(slot, token);
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region ================================== READ ==================================
|
|
// ----------------------------------------------------
|
|
|
|
public RegisterReadResult ReadRegister(
|
|
int slot,
|
|
string registerName)
|
|
{
|
|
return _innerMeterAPI.ReadRegister(slot, registerName);
|
|
}
|
|
|
|
public Task<RegisterReadResult> ReadRegisterAsync(
|
|
int slot,
|
|
string registerName,
|
|
CancellationToken token = default(CancellationToken))
|
|
{
|
|
return _innerMeterAPI.ReadRegisterAsync(slot, registerName, token);
|
|
}
|
|
|
|
// ----------------------------------------------------
|
|
#endregion
|
|
|
|
#region ================================== WRITE ==================================
|
|
// ----------------------------------------------------
|
|
|
|
public async Task<RegisterWriteResult> WriteRegisterAsync(
|
|
int slot,
|
|
string registerName,
|
|
object value,
|
|
bool storeToDevice = false,
|
|
bool refreshSystemState = false,
|
|
CancellationToken token = default)
|
|
{
|
|
var result = await _innerMeterAPI.WriteRegisterAsync(
|
|
slot,
|
|
registerName,
|
|
value,
|
|
storeToDevice,
|
|
refreshSystemState,
|
|
token);
|
|
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
public RegisterWriteResult WriteRegister(
|
|
int slot,
|
|
string registerName,
|
|
object value,
|
|
bool storeToDevice = false,
|
|
bool refreshSystemState = false)
|
|
{
|
|
var result = _innerMeterAPI.WriteRegister(
|
|
slot,
|
|
registerName,
|
|
value,
|
|
storeToDevice,
|
|
refreshSystemState);
|
|
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciSetPasswordResult> SetMeterPasswordAsync(int slot, string password)
|
|
{
|
|
var result = await _innerMeterAPI.SetMeterPasswordAsync(slot, password);
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
public GciSetPasswordResult SetMeterPassword(int slot, string password)
|
|
{
|
|
var result = _innerMeterAPI.SetMeterPassword(slot, password);
|
|
RaiseMeterBatchStatusChanged();
|
|
return result;
|
|
}
|
|
|
|
// ----------------------------------------------------
|
|
#endregion
|
|
|
|
#region ================================== DEBUG STATUS ==================================
|
|
// ----------------------------------------------------
|
|
|
|
public List<WorkerDebugStatus> GetWorkerDebugStatuses()
|
|
{
|
|
return _innerMeterAPI.GetWorkerDebugStatuses();
|
|
}
|
|
|
|
public List<MeterBatchDebugStatus> GetMeterBatchDebugStatuses()
|
|
{
|
|
return _innerMeterAPI.GetMeterBatchDebugStatuses();
|
|
}
|
|
|
|
public void RaiseMeterBatchStatusChanged()
|
|
{
|
|
var statuses = GetMeterBatchDebugStatuses();
|
|
|
|
var handler = MeterBatchStatusChanged;
|
|
if (handler != null)
|
|
handler(statuses);
|
|
}
|
|
|
|
// ----------------------------------------------------
|
|
#endregion
|
|
|
|
#region ================================== SLOT SELECTION ==================================
|
|
// ----------------------------------------------------
|
|
|
|
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 ==================================
|
|
// ----------------------------------------------------
|
|
|
|
/*public void SetSlotRequestPort(int slot, string portName)
|
|
{
|
|
lock (_portLock)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(portName))
|
|
{
|
|
_requestPorts.Remove(slot);
|
|
}
|
|
else
|
|
{
|
|
_requestPorts[slot] = new GciPortConfig
|
|
{
|
|
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
|
|
{
|
|
PortName = portName,
|
|
Type = "Serial"
|
|
};
|
|
}
|
|
}
|
|
|
|
RaiseMeterBatchStatusChanged();
|
|
}
|
|
|
|
public GciPortConfig? GetSlotRequestPort(int slot)
|
|
{
|
|
lock (_portLock)
|
|
{
|
|
GciPortConfig port;
|
|
if (_requestPorts.TryGetValue(slot, out port))
|
|
return port;
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public GciPortConfig? GetSlotStreamingPort(int slot)
|
|
{
|
|
lock (_portLock)
|
|
{
|
|
GciPortConfig port;
|
|
if (_streamingPorts.TryGetValue(slot, out port))
|
|
return port;
|
|
|
|
return null;
|
|
}
|
|
}*/
|
|
|
|
// ----------------------------------------------------
|
|
#endregion
|
|
|
|
#region ================================== METER BATCH SETUP ==================================
|
|
// ----------------------------------------------------
|
|
|
|
public void ReloadSlotSetup()
|
|
{
|
|
_innerMeterAPI.ReloadSlotSetup();
|
|
RaiseMeterBatchStatusChanged();
|
|
}
|
|
|
|
public void SaveSlotSetup(List<MeterBatchDebugStatus> data)
|
|
{
|
|
_innerMeterAPI.SaveSlotSetup(data);
|
|
RaiseMeterBatchStatusChanged();
|
|
}
|
|
|
|
// ----------------------------------------------------
|
|
#endregion
|
|
|
|
public List<string> GetAllRegisterNames()
|
|
{
|
|
return _innerMeterAPI.GetAllRegisterNames();
|
|
}
|
|
}
|
|
} |