285 lines
11 KiB
C#
285 lines
11 KiB
C#
/*********************************************************************************************************************/
|
|
/*! @file UcFM2014Device.xaml.cs
|
|
* @brief <i>User control of FM2014 prepared to use for multiple FM2014 objects</i>
|
|
*
|
|
* @author Thomas Wiedebusch
|
|
* @date 2026-Feb-03
|
|
*
|
|
* @details <i>FM2014 display of status and measurement informational fields .</i>
|
|
*
|
|
* @copyright © SENSUS GmbH 2026. All rights reserved.
|
|
*********************************************************************************************************************/using Sensus.Common.Hardware.WaterMeter.MechanicalMeter.FM2014.FM2014Core;
|
|
using Sensus.Ui.FM2014TestBench.UserControls;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Threading;
|
|
using Sensus.Common.Hardware.WaterMeter.MechanicalMeter.FM2014.FM2014Core.Consts;
|
|
using static Sensus.Common.Hardware.WaterMeter.MechanicalMeter.FM2014.FM2014Core.FM2014CmdDef;
|
|
|
|
namespace Sensus.Ui.Fm2014TestBench.UserControls
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for UcFM2014Device.xaml
|
|
/// </summary>
|
|
/// <remarks date="2026-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
public partial class UcFM2014Device : IUserControl
|
|
{
|
|
///// <summary>
|
|
///// Switches between picture or measurement fields
|
|
///// </summary>
|
|
//private Boolean MeasurementIsActive { set; get; }
|
|
|
|
private readonly List<UIElement> MeasurementBorders;
|
|
private readonly List<UIElement> MeasurementLabels;
|
|
|
|
private readonly FM2014 _fM2014;
|
|
//private readonly Action _selectedAction;
|
|
private static Brush ColorStandardDisplayField;
|
|
|
|
|
|
/// <summary>
|
|
/// Ctor
|
|
/// </summary>
|
|
/// <param name="fm2014"></param>
|
|
/// <param name="setupHasChanged">kick off storage of changed setting to configuration file</param>
|
|
/// <remarks date="2026-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public UcFM2014Device(FM2014 fm2014, Action setupHasChanged)
|
|
{
|
|
InitializeComponent();
|
|
_fM2014 = fm2014;
|
|
//_selectedAction = setupHasChanged;
|
|
MeasurementBorders = new List<UIElement>();
|
|
MeasurementLabels = new List<UIElement>();
|
|
MeasurementBorders.Add(brdMeasurement1);
|
|
MeasurementBorders.Add(brdMeasurement2);
|
|
MeasurementBorders.Add(brdMeasurement3);
|
|
MeasurementBorders.Add(brdMeasurement4);
|
|
MeasurementBorders.Add(brdMeasurement5);
|
|
MeasurementBorders.Add(brdMeasurement6);
|
|
MeasurementBorders.Add(brdMeasurement7);
|
|
MeasurementLabels.Add(lblMeasurement1);
|
|
MeasurementLabels.Add(lblMeasurement2);
|
|
MeasurementLabels.Add(lblMeasurement3);
|
|
MeasurementLabels.Add(lblMeasurement4);
|
|
MeasurementLabels.Add(lblMeasurement5);
|
|
MeasurementLabels.Add(lblMeasurement6);
|
|
MeasurementLabels.Add(lblMeasurement7);
|
|
|
|
ColorStandardDisplayField = lblStatus.Background;
|
|
|
|
Clear();
|
|
}
|
|
|
|
|
|
///<inheritdoc/>
|
|
public void Clear()
|
|
{
|
|
SetConnectionStatus(ConnectStatusName.offline);
|
|
HideMeasurements();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set a value to a label:
|
|
/// - Input values 1..7 !
|
|
/// </summary>
|
|
/// <param name="lblIndex">0 to 6</param>
|
|
/// <param name="valueStr"></param>
|
|
/// <param name="backgroundColor"></param>
|
|
/// <returns></returns>
|
|
/// <remarks date="2026-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public Boolean SetValue(Int32 lblIndex, String valueStr, Brush backgroundColor = null)
|
|
{
|
|
var retVal = true;
|
|
|
|
Dispatcher.Invoke(DispatcherPriority.Send,
|
|
new Action(() =>
|
|
{
|
|
if (lblIndex < 0 || lblIndex >= MeasurementLabels.Count ||
|
|
!MeasurementLabels[lblIndex].IsEnabled)
|
|
retVal = false;
|
|
|
|
if (!retVal) return;
|
|
|
|
var label = (Label)MeasurementLabels[lblIndex];
|
|
UpdateValue(label, valueStr, backgroundColor);
|
|
}));
|
|
|
|
return retVal;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hide measurements and switch back to display the picture of FM2014
|
|
/// </summary>
|
|
/// <remarks date="2026-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
private void HideMeasurements()
|
|
{
|
|
UiElmEnable(brdFM2014Pic, true);
|
|
UiElmEnable(picFM2014, lblStatus.Content.Equals(ConnectStatusName.online));
|
|
|
|
// Hide all measurement borders
|
|
foreach (var borders in MeasurementBorders)
|
|
{
|
|
UiElmEnable(borders, false, false);
|
|
}
|
|
// Hide all measurements
|
|
foreach (var label in MeasurementLabels)
|
|
{
|
|
UiElmEnable(label, false, false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Switch from FM2014 picture to output of measurements and vice versa if input is 0.
|
|
/// </summary>
|
|
/// <param name="numberOfMeasurementFields">0 = Off, 1..7 measurements active</param>
|
|
/// <returns></returns>
|
|
/// <remarks date="2026-Feb-03..09" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public Boolean EnableMeasurements(UInt32 numberOfMeasurementFields)
|
|
{
|
|
// Rest all labels to 0
|
|
for (var idx = 0; idx < MeasurementLabels.Count; idx++)
|
|
{
|
|
SetValue(idx, " ", ColorStandardDisplayField);
|
|
}
|
|
|
|
// Check if device is connected and logged in
|
|
if (!lblStatus.Content.Equals(GetConnectionStatusInfo(ConnectStatusName.online)))
|
|
return false;
|
|
|
|
if (numberOfMeasurementFields == 0)
|
|
{
|
|
HideMeasurements();
|
|
return true;
|
|
}
|
|
|
|
// Hide the picture and the border of it
|
|
UiElmEnable(picFM2014, false, false);
|
|
UiElmEnable(brdFM2014Pic, false, false);
|
|
|
|
// Display the measurement fields
|
|
for (var ctr = 0; ctr < numberOfMeasurementFields; ctr++)
|
|
{
|
|
if (ctr > MeasurementBorders.Count)
|
|
break;
|
|
UiElmEnable(MeasurementBorders[ctr], true);
|
|
UiElmEnable(MeasurementLabels[ctr], true);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refresh connection status
|
|
/// </summary>
|
|
/// <param name="statusName"></param>
|
|
/// <remarks date="2026-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public void SetConnectionStatus(ConnectStatusName statusName)
|
|
{
|
|
var connectionStatusStr = GetConnectionStatusInfo(statusName);
|
|
Dispatcher.Invoke(DispatcherPriority.Send,
|
|
new Action(() =>
|
|
{
|
|
lblStatus.Content = connectionStatusStr;
|
|
var isEnabled = statusName.Equals(ConnectStatusName.online);
|
|
UiElmEnable(picFM2014, isEnabled);
|
|
if (!statusName.Equals(ConnectStatusName.online))
|
|
{
|
|
HideMeasurements();
|
|
lblStatus.Background = ColorStandardDisplayField;
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Background = Brushes.Green;
|
|
lblLifetime_h.Content = _fM2014.Lifetime_h;
|
|
lblFirmwareVersion.Content = _fM2014.FwVersion;
|
|
lblSerialNumber.Content = _fM2014.SerialNumber;
|
|
}
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Output message to control without access violation.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <remarks date="????" author="Roland Drabesch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2023-07-11" author="Thomas Wiedebusch">
|
|
/// - Introduced multi line text with center alignment.
|
|
/// </remarks>
|
|
private static void UpdateContentControl(ContentControl ctl, String text)
|
|
{
|
|
ctl.Dispatcher.Invoke(DispatcherPriority.Normal,
|
|
new Action(() =>
|
|
{
|
|
ctl.Content = new TextBlock()
|
|
{
|
|
Text = text,
|
|
TextWrapping = TextWrapping.Wrap,
|
|
TextAlignment = TextAlignment.Center
|
|
};
|
|
}
|
|
));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Publish value to label
|
|
/// </summary>
|
|
/// <param name="label"></param>
|
|
/// <param name="valueStr"></param>
|
|
/// <param name="backgroundColor"></param>
|
|
/// <remarks date="2026-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
private void UpdateValue(Label label, String valueStr, Brush backgroundColor = null)
|
|
{
|
|
Dispatcher.Invoke(DispatcherPriority.Send,
|
|
new Action(() =>
|
|
{
|
|
if (backgroundColor == null)
|
|
backgroundColor = ColorStandardDisplayField;
|
|
|
|
label.Content = valueStr;
|
|
label.Background = backgroundColor;
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enable UI elements
|
|
/// </summary>
|
|
/// <param name="elm"></param>
|
|
/// <param name="isEnabled"></param>
|
|
/// <param name="isVisible"></param>
|
|
/// <remarks date="????" author="Roland Drabesch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2023-07-11" author="Thomas Wiedebusch">
|
|
/// - Introduced multi line text with center alignment.
|
|
/// </remarks>
|
|
private static void UiElmEnable(UIElement elm, Boolean isEnabled, Boolean isVisible = true)
|
|
{
|
|
elm.Dispatcher.Invoke(DispatcherPriority.Normal,
|
|
new Action(() =>
|
|
{
|
|
elm.Visibility = isVisible ? Visibility.Visible : Visibility.Hidden;
|
|
elm.IsEnabled = isEnabled;
|
|
elm.Opacity = isEnabled ? 1.0 : 0.2;
|
|
}));
|
|
}
|
|
}
|
|
}
|