tbf/TestBenchFramework/BenchControl/Elde/Diverter/DiverterCfgCtrl.cs
2015-04-15 20:32:56 +02:00

214 lines
5.3 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Windows.Forms;
using log4net;
using TBF.BenchControl.Generic;
namespace TBF.BenchControl.Elde.Diverter
{
public partial class DiverterCfgCtrl : UserControl, IComponentCfgCtrl
{
static readonly ILog log = LogManager.GetLogger(typeof(DiverterCfgCtrl));
ComponentParametersDlg parent;
public bool ShowMore { get { return true; } }
DiverterCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as DiverterCfg;
Redraw();
}
}
public DiverterCfgCtrl()
{
InitializeComponent();
}
private void DiverterCfgCtrl_Load(object sender, EventArgs e)
{
parent = ParentForm as ComponentParametersDlg;
if (parent == null) return;
if (parent.TbfComponents != null)
{
foreach (var cmpnt in parent.TbfComponents)
{
if (TbfComponents.CmpntFactoryFromClassName(cmpnt.ClassName) is Elde.ControlBoardFactory)
{
parentNameComboBox.Items.Add(cmpnt.Name);
}
}
}
if (config != null)
{
/// TODO
}
Redraw();
StartResponseHandler();
}
public void Closing()
{
StopResponseHandler();
}
void Redraw()
{
if (config == null) return; /// Control was not loaded, settings were not changed
classNameLabel.Text = config.Factory.ClassName;
parentNameComboBox.Text = string.IsNullOrEmpty(config.ParentName) ? "---" : config.ParentName;
nameTextBox.Text = config.Name;
bitPositionTextBox.Text = config.BitPosition.ToString();
startingEdgeTextBox.Text = config.StartingEdge.ToString();
stoppingEdgeTextBox.Text = config.StoppingEdge.ToString();
}
public void Unlock()
{
nameTextBox.Enabled = true;
parentNameComboBox.Enabled = true;
bitPositionTextBox.Enabled = true;
startingEdgeTextBox.Enabled = true;
stoppingEdgeTextBox.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
int dummy;
if (!parentNameComboBox.Items.Contains(parentNameComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Parent Name'";
}
if (!int.TryParse(bitPositionTextBox.Text, out dummy) || dummy < 0 || dummy >= 64)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Bit Position' should be between 0 and 63";
}
if (!int.TryParse(startingEdgeTextBox.Text, out dummy) || dummy < 0 || dummy > 100)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Starting Edge' should be between 0 and 100";
}
if (!int.TryParse(stoppingEdgeTextBox.Text, out dummy) || dummy < 0 || dummy > 100)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Stopping Edge' should be between 0 and 100";
}
return flags;
}
public CfgUpdateFlags UpdateCfg()
{
CfgUpdateFlags flags = CfgUpdateFlags.RestartRqrd;
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
config.Name = nameTextBox.Text;
config.ParentName = parentNameComboBox.Text.Equals("---") ? string.Empty : parentNameComboBox.Text;
config.BitPosition = int.Parse(bitPositionTextBox.Text);
config.StartingEdge = int.Parse(startingEdgeTextBox.Text);
config.StoppingEdge = int.Parse(stoppingEdgeTextBox.Text);
return flags;
}
///
/// Do some stuff here
///
enum EdgeSel { NoEdge, StartEdge, StopEdge };
EdgeSel lastTestedEdge = EdgeSel.NoEdge;
int cursorPosition = 0;
int currentEdgeLevel = 50; /// Edge level 0..100 in %
private void startButton_Click(object sender, EventArgs e)
{
lastTestedEdge = EdgeSel.StartEdge;
updateButton.Enabled = true;
}
private void stopButton_Click(object sender, EventArgs e)
{
lastTestedEdge = EdgeSel.StopEdge;
updateButton.Enabled = true;
}
private void leftButton_Click(object sender, EventArgs e)
{
if (currentEdgeLevel > 0) currentEdgeLevel--;
}
private void rightButton_Click(object sender, EventArgs e)
{
if (currentEdgeLevel < 100) currentEdgeLevel++;
}
private void updateButton_Click(object sender, EventArgs e)
{
switch (lastTestedEdge)
{
case EdgeSel.StartEdge:
startingEdgeTextBox.Text = currentEdgeLevel.ToString();
break;
case EdgeSel.StopEdge:
stoppingEdgeTextBox.Text = currentEdgeLevel.ToString();
break;
case EdgeSel.NoEdge:
default:
break;
}
}
#region Configuration Change Handling
public static void OnCmdResponse(object sender, CmdResponseArgs args)
{
if (CmdResponseHandler == null) return;
try { CmdResponseHandler(sender, args); }
catch (Exception e) { log.Error("CmdResponseHandler(...) failed", e); }
}
public static event EventHandler<CmdResponseArgs> CmdResponseHandler;
public void StartResponseHandler()
{
CmdResponseHandler += delegate(object sender, CmdResponseArgs args)
{
//if (args.Command == CfgChangeCmd.GetRVAdc1)
//{
// adc1 = args.Response;
// adcTextBox1.Text = adc1.ToString();
//}
//else if (args.Command == CfgChangeCmd.GetRVAdc2)
//{
// adc2 = args.Response;
// adcTextBox2.Text = adc2.ToString();
//}
};
}
public void StopResponseHandler()
{
CmdResponseHandler = null;
}
#endregion Configuration Change Handling
}
}