502 lines
16 KiB
C#
502 lines
16 KiB
C#
using CordonelPreadjustmentUi;
|
|
using CordonelPreadjustmentUi.Processes.Itinerary;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
|
|
using Xylem.Common.Ui.CordonelPreadjustmentUi;
|
|
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/UPDATE/GET slot ==================================
|
|
|
|
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).ConfigureAwait(false);
|
|
|
|
//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).ConfigureAwait(false);
|
|
|
|
//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).ConfigureAwait(false);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciAllSlotsInfo> GetAllSlotsAsync(
|
|
CancellationToken token = default)
|
|
{
|
|
var result = await _innerMeterAPI.GetAllMetersInfo(token).ConfigureAwait(false);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciCleanSlotResult> CleanSlotAsync(
|
|
int slot,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.", nameof(slot));
|
|
|
|
var result = await _innerMeterAPI.CleanSlotAsync(slot, token).ConfigureAwait(false);
|
|
|
|
//RaiseMeterBatchStatusChanged();
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciCleanAllSlotsResult> CleanAllSlotsAsync(
|
|
CancellationToken token = default)
|
|
{
|
|
var result = await _innerMeterAPI.CleanAllSlotsAsync(token).ConfigureAwait(false);
|
|
|
|
//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.");
|
|
|
|
var result = await _innerMeterAPI.SetMeterPasswordAsync(slot, password, token).ConfigureAwait(false);
|
|
|
|
//RaiseMeterBatchStatusChanged();
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ================================== LOGIN ==================================
|
|
public async Task<PublicModels.GciLoginResult> LoginOneSlotAsync(
|
|
int slot,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await _innerMeterAPI.LoginOneSlotAsync(slot, token).ConfigureAwait(false);
|
|
return result;
|
|
|
|
//RaiseMeterBatchStatusChanged();
|
|
}
|
|
#endregion
|
|
|
|
#region ================================== CONNECTION ==================================
|
|
|
|
public async Task<GciConnectResult> ConnectOneSlotAsync(
|
|
int slot,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await _innerMeterAPI.ConnectOneSlotAsync(slot, token).ConfigureAwait(false);
|
|
|
|
//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).ConfigureAwait(false);
|
|
|
|
//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).ConfigureAwait(false);
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region ================================== READ ==================================
|
|
|
|
public async Task<RegisterReadResult> ReadRegisterAsync(
|
|
int slot,
|
|
string registerName,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.", nameof(slot));
|
|
|
|
if (string.IsNullOrWhiteSpace(registerName))
|
|
throw new ArgumentException("Register name is empty.", nameof(registerName));
|
|
|
|
var result = await _innerMeterAPI
|
|
.ReadRegisterAsync(slot, registerName, token)
|
|
.ConfigureAwait(false);
|
|
|
|
//RaiseMeterBatchStatusChanged();
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ================================== WRITE ==================================
|
|
|
|
public async Task<RegisterWriteResult> WriteRegisterAsync(
|
|
int slot,
|
|
string registerName,
|
|
object value,
|
|
bool storeToDevice = false,
|
|
bool refreshSystemState = false,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.", nameof(slot));
|
|
|
|
if (string.IsNullOrWhiteSpace(registerName))
|
|
throw new ArgumentException("Register name is empty.", nameof(registerName));
|
|
|
|
var result = await _innerMeterAPI
|
|
.WriteRegisterAsync(
|
|
slot,
|
|
registerName,
|
|
value,
|
|
storeToDevice,
|
|
refreshSystemState,
|
|
token)
|
|
.ConfigureAwait(false);
|
|
|
|
//RaiseMeterBatchStatusChanged();
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region ================================== Password ==================================
|
|
|
|
public async Task<GciSetPasswordResult> SetMeterPasswordAsync(
|
|
int slot,
|
|
string password,
|
|
CancellationToken token = default)
|
|
{
|
|
if (slot <= 0)
|
|
throw new ArgumentException("Invalid slot id.", nameof(slot));
|
|
|
|
if (string.IsNullOrWhiteSpace(password))
|
|
throw new ArgumentException("Password is empty.", nameof(password));
|
|
|
|
var result = await _innerMeterAPI
|
|
.SetMeterPasswordAsync(slot, password, token)
|
|
.ConfigureAwait(false);
|
|
|
|
//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
|
|
|
|
#region ================================== Register names ==================================
|
|
|
|
public List<string> GetAllRegisterNames()
|
|
{
|
|
return _innerMeterAPI.GetAllRegisterNames();
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Preadjustment
|
|
|
|
public Task<bool> Preadjustment_DetectAsync(
|
|
CancellationToken token = default)
|
|
{
|
|
return _innerMeterAPI.Preadjustment_DetectAsync(token);
|
|
}
|
|
|
|
public Task<bool> Preadjustment_PreparationAsync(
|
|
ProcessProgress pp,
|
|
CancellationToken token = default)
|
|
{
|
|
return _innerMeterAPI.Preadjustment_PreparationAsync(pp, token);
|
|
}
|
|
|
|
public Task<bool> Preadjustment_AmplitudeTestAsync(
|
|
ProcessProgress pp,
|
|
CancellationToken token = default)
|
|
{
|
|
return _innerMeterAPI.Preadjustment_AmplitudeTestAsync(pp, token);
|
|
}
|
|
|
|
public Task<bool> Preadjustment_TemperatureCalibrationAsync(
|
|
ProcessProgress pp,
|
|
CancellationToken token = default)
|
|
{
|
|
return _innerMeterAPI.Preadjustment_TemperatureCalibrationAsync(pp, token);
|
|
}
|
|
|
|
public Task<bool> Preadjustment_OffsetTestAsync(
|
|
ProcessProgress pp,
|
|
CancellationToken token = default)
|
|
{
|
|
return _innerMeterAPI.Preadjustment_OffsetTestAsync(pp, token);
|
|
}
|
|
|
|
public Task<bool> Preadjustment_CompletionAsync(
|
|
ProcessProgress pp,
|
|
CancellationToken token = default)
|
|
{
|
|
return _innerMeterAPI.Preadjustment_CompletionAsync(pp, token);
|
|
}
|
|
|
|
|
|
}
|
|
} |