From 95a3d7def61635a314ea3bb4af7a8472589071dc Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Tue, 20 Feb 2018 10:45:58 +0100 Subject: [PATCH] Network.Comet.Ambient component added, ver. 2.18.821 --- TBF/BenchControl/Ambient/Comet/Ambient.cs | 10 +- TBF/BenchControl/Ambient/Greco/Ambient.cs | 8 +- .../Network/Camera/CLP1611/CameraCfgCtrl.cs | 9 +- .../Network/Comet/Ambient/Ambient.cs | 267 ++++++++++++++++++ .../Network/Comet/Ambient/AmbientCfg.cs | 44 +++ .../Comet/Ambient/AmbientCfgCtrl.Designer.cs | 181 ++++++++++++ .../Network/Comet/Ambient/AmbientCfgCtrl.cs | 144 ++++++++++ .../Network/Comet/Ambient/AmbientCfgCtrl.resx | 120 ++++++++ .../Network/Comet/Ambient/Factory.cs | 26 ++ TBF/BenchControl/TbfComponents.cs | 1 + TBF/Properties/AssemblyInfo.cs | 4 +- TBF/TBF.csproj | 12 + 12 files changed, 810 insertions(+), 16 deletions(-) create mode 100644 TBF/BenchControl/Network/Comet/Ambient/Ambient.cs create mode 100644 TBF/BenchControl/Network/Comet/Ambient/AmbientCfg.cs create mode 100644 TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.Designer.cs create mode 100644 TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.cs create mode 100644 TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.resx create mode 100644 TBF/BenchControl/Network/Comet/Ambient/Factory.cs diff --git a/TBF/BenchControl/Ambient/Comet/Ambient.cs b/TBF/BenchControl/Ambient/Comet/Ambient.cs index 617495ac4..596ea8803 100644 --- a/TBF/BenchControl/Ambient/Comet/Ambient.cs +++ b/TBF/BenchControl/Ambient/Comet/Ambient.cs @@ -3,9 +3,7 @@ /// using System; using System.Collections.Generic; -using System.Globalization; using System.IO.Ports; -using System.Text; using log4net; using Config.Entities; using TBF.BenchControl.Generic; @@ -33,9 +31,9 @@ namespace TBF.BenchControl.Ambient.Comet /// /// Measured values when MsrmntState == MsrmntState.Valid /// - float temperature; /// [deg C] + float temperature; /// [°C] float pressure; /// [bar] - float humidity; /// [%] + float humidity; /// [R%] /// Measurement time stamp when MsrmntState == MsrmntState.Valid int msrmntTimeStamp; @@ -119,8 +117,8 @@ namespace TBF.BenchControl.Ambient.Comet msrmntTimeStamp = StateMachine.Time; msrmntState = MsrmntState.Valid; - log.InfoFormat("Ambient: temperature = {0} C, humidity = {1} %, pressure = {2} mbar", temperature.ToString("F1"), humidity.ToString("F1"), (1000 * pressure).ToString("F0")); - } + log.InfoFormat("Ambient: temperature = {0:F1} C, humidity = {1:F1} %, pressure = {2:F0} mbar", temperature, humidity, 1000 * pressure); + } } } catch (Exception e) diff --git a/TBF/BenchControl/Ambient/Greco/Ambient.cs b/TBF/BenchControl/Ambient/Greco/Ambient.cs index 297cc18f3..5fbd99295 100644 --- a/TBF/BenchControl/Ambient/Greco/Ambient.cs +++ b/TBF/BenchControl/Ambient/Greco/Ambient.cs @@ -34,9 +34,9 @@ namespace TBF.BenchControl.Ambient.Greco /// /// Measured values when MsrmntState == MsrmntState.Valid /// - float temperature; /// [deg C] + float temperature; /// [°C] float pressure; /// [bar] - float humidity; /// [%] + float humidity; /// [R%] /// Measurement time stamp when MsrmntState == MsrmntState.Valid int msrmntTimeStamp; @@ -136,8 +136,8 @@ namespace TBF.BenchControl.Ambient.Greco msrmntTimeStamp = StateMachine.Time; msrmntState = MsrmntState.Valid; - log.InfoFormat("Ambient: temperature = {0} C, humidity = {1} %, pressure = {2} mbar", temperature.ToString("F1"), humidity.ToString("F1"), (1000 * pressure).ToString("F0")); - } + log.InfoFormat("Ambient: temperature = {0:F1} C, humidity = {1:F1} %, pressure = {2:F0} mbar", temperature, humidity, 1000 * pressure); + } else { log.Warn("Invalid string received: " + measurement); diff --git a/TBF/BenchControl/Network/Camera/CLP1611/CameraCfgCtrl.cs b/TBF/BenchControl/Network/Camera/CLP1611/CameraCfgCtrl.cs index cfc6720a9..b35527e34 100644 --- a/TBF/BenchControl/Network/Camera/CLP1611/CameraCfgCtrl.cs +++ b/TBF/BenchControl/Network/Camera/CLP1611/CameraCfgCtrl.cs @@ -62,6 +62,7 @@ namespace TBF.BenchControl.Network.Camera.CLP1611 void Localize() { nameLabel.Text = Strings.Name; + parentNameLabel.Text = Strings.Parent_name; hwAddressLabel.Text = Strings.Serial_Number; } @@ -100,13 +101,13 @@ namespace TBF.BenchControl.Network.Camera.CLP1611 if (!parentNameComboBox.Items.Contains(parentNameComboBox.Text)) { flags |= CfgUpdateFlags.Error; - message += Environment.NewLine + string.Format("Invalid '{0}'", Strings.Parent_name); + message += Environment.NewLine + string.Format(Strings.Invalid_0, parentNameLabel.Text); } if (!int.TryParse(hwAddressTextBox.Text, out dummy) || dummy % 100 > 63) { flags |= CfgUpdateFlags.Error; - message += Environment.NewLine + string.Format("'{0}' is not correct", hwAddressLabel.Text); + message += Environment.NewLine + string.Format(Strings.Invalid_0, hwAddressLabel.Text); } TestImagesMode newTestImagesMode = TestImagesMode.Invalid; @@ -117,14 +118,14 @@ namespace TBF.BenchControl.Network.Camera.CLP1611 if (newTestImagesMode == TestImagesMode.Invalid) { flags |= CfgUpdateFlags.Error; - message += Environment.NewLine + string.Format("Invalid {0}", testImagesModeLabel.Text); + message += Environment.NewLine + string.Format(Strings.Invalid_0, testImagesModeLabel.Text); } if (!int.TryParse(testImagesCountTextBox.Text, out dummy) || dummy < 0 || dummy > 3000 || ((dummy == 0) && (newTestImagesMode != TestImagesMode.None))) { flags |= CfgUpdateFlags.Error; - message += Environment.NewLine + string.Format("Invalid {0}", testImagesCountLabel.Text); + message += Environment.NewLine + string.Format(Strings.Invalid_0, testImagesCountLabel.Text); } diff --git a/TBF/BenchControl/Network/Comet/Ambient/Ambient.cs b/TBF/BenchControl/Network/Comet/Ambient/Ambient.cs new file mode 100644 index 000000000..b25c9b212 --- /dev/null +++ b/TBF/BenchControl/Network/Comet/Ambient/Ambient.cs @@ -0,0 +1,267 @@ +/// +/// Copyright (c) 2018 Sensus Slovensko a.s. +/// +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using log4net; +using Config.Entities; +using TBF.BenchControl.Generic; +using TBF.Boxes; + +namespace TBF.BenchControl.Network.Comet.Ambient +{ + /// + /// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232) + /// Connection settings: 9600 Bd 8-bits No-parity 2-stop-bits Flow control: none. + /// + public class Ambient : ComponentBase, IDevice, IOperation, GenericDevices.IAmbient + { + private static readonly ILog log = LogManager.GetLogger(typeof(Ambient)); + public override string ToString() { return string.Format("Ambient({0})", Cfg.ToString(1)); } + + readonly AmbientCfg myCfg; + readonly GenericDevices.INetworkAdapter netAdapter; + readonly IPAddress ipAddress; + readonly int tcpPort; + + TcpClient tcpClient; + NetworkStream networkStream; + + enum State + { + Idle, + WaitingForTemperature, + WaitingForHumidity, + WaitingForPressure, + }; + + State state; + + /// The state of the measurement + MsrmntState msrmntState; + + /// + /// Measured values when MsrmntState == MsrmntState.Valid + /// + float temperature; /// [°C] + float pressure; /// [bar] + float humidity; /// [R%] + + /// Measurement time stamp when MsrmntState == MsrmntState.Valid + int msrmntTimeStamp; + + public Ambient() + { + } + + /// + /// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232) + /// + public Ambient(Generic.IComponentCfg cfg, IList components) + : base(cfg) + { + myCfg = cfg as AmbientCfg; + if (myCfg == null) throw new ArgumentNullException("No configuration"); + + netAdapter = TbfComponents.FindComponent(cfg.ParentName, components) as GenericDevices.INetworkAdapter; + if (netAdapter == null) throw new ArgumentNullException("No network adapter"); + + ipAddress = IPAddress.Parse(myCfg.IPAddress); + tcpPort = myCfg.TcpPort; + + log.Debug(this.ToString()); + } + + public void Initialize() + { + if (myCfg.DebugLevel == DebugMode.Simulate) + { + temperature = 20.0f; + humidity = 40.0f; + pressure = 1.0f; + msrmntTimeStamp = StateMachine.Time; + msrmntState = MsrmntState.Valid; + log.FatalFormat("Ambient: temperature = {0:F1} C, humidity = {1:F1} %, pressure = {2:F0} mbar", temperature, humidity, 1000 * pressure); + return; + } + + tcpClient = new TcpClient(); + tcpClient.Connect(ipAddress, tcpPort); + networkStream = tcpClient.GetStream(); + + state = State.Idle; + msrmntState = MsrmntState.Busy; + + log.FatalFormat("IP Address={0}, device {1}", netAdapter.IPAddress, this.ToString()); + } + + /// Run this device + public void RunDeviceBefore() + { + if (myCfg.DebugLevel == DebugMode.Simulate || myCfg.DebugLevel == DebugMode.FailureDuringOperation) + { + msrmntTimeStamp = StateMachine.Time; + return; + } + + try + { + int available = tcpClient.Available; + + if (available >= 11) + { + byte[] bytes = new byte[available]; + networkStream.Read(bytes, 0, available); + log.Debug(Telegram.LogTelegram("Received ", bytes)); + + int value; + if (available == 11 && bytes[0] == 0 && bytes[2] == 0 && bytes[3] == 0 && bytes[4] == 0 + && bytes[5] == 5 && bytes[6] == 1 && bytes[7] == 3 && bytes[8] == 2) + { + switch (state) + { + case State.WaitingForTemperature: + value = (int)bytes[9] * 256 + (int)bytes[10]; + temperature = (float)(value / 10.0); /// Conversion to °C + msrmntTimeStamp = StateMachine.Time; + msrmntState = MsrmntState.Valid; + log.InfoFormat("Ambient temperature = {0:F1} C", temperature); + state = State.Idle; + break; + case State.WaitingForHumidity: + value = (int)bytes[9] * 256 + (int)bytes[10]; + humidity = (float)(value / 10.0); /// Conversion to R% + msrmntTimeStamp = StateMachine.Time; + msrmntState = MsrmntState.Valid; + log.InfoFormat("Ambient humidity = {0:F1} %", humidity); + state = State.Idle; + break; + case State.WaitingForPressure: + value = (int)bytes[9] * 256 + (int)bytes[10]; + pressure = (float)(value / 10000.0); /// Conversion to bar + msrmntTimeStamp = StateMachine.Time; + msrmntState = MsrmntState.Valid; + log.InfoFormat("Ambient pressure = {0:F0} mbar", 1000 * pressure); + state = State.Idle; + break; + } + } + else + { + state = State.Idle; + } + } + } + catch (Exception e) + { + DebugLevel = DebugMode.FailureDuringOperation; + + log.FatalFormat("Ambient: Serail port read failure : {0}", e.Message); + if (e.InnerException != null) + { + log.FatalFormat("InnerMessage : {0}", e.InnerException.Message); + } + } + } + + /// Run this device + public void RunDeviceAfter() + { + if (myCfg.DebugLevel == DebugMode.Simulate || myCfg.DebugLevel == DebugMode.FailureDuringOperation) + { + return; + } + + try + { + /// 20 seconds cycle + byte[] message = null; + switch (StateMachine.Time % 20) + { + case 0: + state = State.WaitingForTemperature; + message = new byte[12] { 0, 0, 0, 0, 0, 0x06, 0x01, 0x03, 0, 0x30, 0, 0x01 }; + break; + case 6: + state = State.WaitingForHumidity; + message = new byte[12] { 0, 0x02, 0, 0, 0, 0x06, 0x01, 0x03, 0, 0x31, 0, 0x01 }; + break; + case 12: + state = State.WaitingForPressure; + message = new byte[12] { 0, 0x03, 0, 0, 0, 0x06, 0x01, 0x03, 0, 0x33, 0, 0x01 }; + break; + } + + if (message != null) + { + log.Debug(Telegram.LogTelegram("Sending ", message)); + networkStream.Write(message, 0, message.Length); /// Read request + } + } + catch (Exception e) + { + DebugLevel = DebugMode.FailureDuringOperation; + + log.FatalFormat("TCP/IP write error : {0}", e.Message); + if (e.InnerException != null) + { + log.FatalFormat("InnerMessage : {0}", e.InnerException.Message); + } + } + } + + /// Stop this device + public void StopDevice() + { + if (myCfg.DebugLevel != DebugMode.Simulate && myCfg.DebugLevel != DebugMode.FailureDuringOperation) + { + networkStream.Close(); + tcpClient.Close(); + } + } + + public void StopDevice2() { } + + /// + /// Boxes for the operation result + /// + FloatBox tempBox; + FloatBox pressureBox; + FloatBox humiBox; + + public IOperation ReadAmbientOp(FloatBox tempBox, FloatBox pressureBox, FloatBox humiBox) + { + this.tempBox = tempBox; + this.pressureBox = pressureBox; + this.humiBox = humiBox; + return this; + } + + /// Start this operation + public void Start() + { + if (tempBox != null) tempBox.Val = temperature; + if (pressureBox != null) pressureBox.Val = pressure; + if (humiBox != null) humiBox.Val = humidity; + } + + /// Run this operation + /// + /// Event.FlowInDone or Event.FlowOutDone + /// + public Event Run() + { + if (tempBox != null) tempBox.Val = temperature; + if (pressureBox != null) pressureBox.Val = pressure; + if (humiBox != null) humiBox.Val = humidity; + return Event.AmbientDone; + } + + /// Stop this operation + public void Stop() + { + } + } +} diff --git a/TBF/BenchControl/Network/Comet/Ambient/AmbientCfg.cs b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfg.cs new file mode 100644 index 000000000..07f7041c4 --- /dev/null +++ b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfg.cs @@ -0,0 +1,44 @@ +/// +/// Copyright (c) 2018 Sensus Slovensko a.s. +/// +using System; +using System.Net; +using System.Xml.Serialization; +using TBF.BenchControl.Generic; + +namespace TBF.BenchControl.Network.Comet.Ambient +{ + public class AmbientCfg : ComponentCfgBase, IChildComponentCfg + { + public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(AmbientCfg) })[0]; + protected override XmlSerializer GetSerializer() { return Serializer; } + + public IComponentCfgCtrl GetControl() { return new AmbientCfgCtrl(); } + + /// + /// Serialized parameters + /// + public string IPAddress; + public int TcpPort; + public string SerialNr; + + /// Private parameterless constructor invoked by all other (public) constructors + AmbientCfg() { } + + public AmbientCfg(string name, IComponentFactory factory) + : this() + { + this.Name = name; + this.Factory = factory; + ParentName = string.Empty; + IPAddress = "192.168.0.123"; + TcpPort = 502; + SerialNr = string.Empty; + } + + public string ToString(int i) + { + return string.Format("Name={0}, Parent={1}, IP={2}, Port={3}, s/n={4}", Name, ParentName, IPAddress, TcpPort, SerialNr); + } + } +} diff --git a/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.Designer.cs b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.Designer.cs new file mode 100644 index 000000000..5a4f8d2ce --- /dev/null +++ b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.Designer.cs @@ -0,0 +1,181 @@ +/// +/// Copyright (c) 2018 Sensus Slovensko a.s. +/// +namespace TBF.BenchControl.Network.Comet.Ambient +{ + partial class AmbientCfgCtrl + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.nameTextBox = new System.Windows.Forms.TextBox(); + this.nameLabel = new System.Windows.Forms.Label(); + this.classNameLabel = new System.Windows.Forms.Label(); + this.serialNrTextBox = new System.Windows.Forms.TextBox(); + this.serialNrLabel = new System.Windows.Forms.Label(); + this.parentNameComboBox = new System.Windows.Forms.ComboBox(); + this.parentNameLabel = new System.Windows.Forms.Label(); + this.ipAddressLabel = new System.Windows.Forms.Label(); + this.ipAddressTextBox = new System.Windows.Forms.TextBox(); + this.tcpPortTextBox = new System.Windows.Forms.TextBox(); + this.tcpPortLabel = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // nameTextBox + // + this.nameTextBox.Enabled = false; + this.nameTextBox.Location = new System.Drawing.Point(140, 43); + this.nameTextBox.Name = "nameTextBox"; + this.nameTextBox.Size = new System.Drawing.Size(130, 20); + this.nameTextBox.TabIndex = 2; + // + // nameLabel + // + this.nameLabel.AutoSize = true; + this.nameLabel.Location = new System.Drawing.Point(36, 46); + this.nameLabel.Name = "nameLabel"; + this.nameLabel.Size = new System.Drawing.Size(35, 13); + this.nameLabel.TabIndex = 1; + this.nameLabel.Text = "Name"; + // + // classNameLabel + // + this.classNameLabel.AutoSize = true; + this.classNameLabel.Location = new System.Drawing.Point(137, 18); + this.classNameLabel.Name = "classNameLabel"; + this.classNameLabel.Size = new System.Drawing.Size(83, 13); + this.classNameLabel.TabIndex = 0; + this.classNameLabel.Text = "ComonentName"; + // + // serialNrTextBox + // + this.serialNrTextBox.Enabled = false; + this.serialNrTextBox.Location = new System.Drawing.Point(140, 148); + this.serialNrTextBox.Name = "serialNrTextBox"; + this.serialNrTextBox.Size = new System.Drawing.Size(130, 20); + this.serialNrTextBox.TabIndex = 10; + // + // serialNrLabel + // + this.serialNrLabel.AutoSize = true; + this.serialNrLabel.Location = new System.Drawing.Point(36, 151); + this.serialNrLabel.Name = "serialNrLabel"; + this.serialNrLabel.Size = new System.Drawing.Size(71, 13); + this.serialNrLabel.TabIndex = 9; + this.serialNrLabel.Text = "Serial number"; + // + // parentNameComboBox + // + this.parentNameComboBox.Enabled = false; + this.parentNameComboBox.FormattingEnabled = true; + this.parentNameComboBox.Location = new System.Drawing.Point(140, 69); + this.parentNameComboBox.Name = "parentNameComboBox"; + this.parentNameComboBox.Size = new System.Drawing.Size(130, 21); + this.parentNameComboBox.TabIndex = 4; + // + // parentNameLabel + // + this.parentNameLabel.AutoSize = true; + this.parentNameLabel.Location = new System.Drawing.Point(36, 72); + this.parentNameLabel.Name = "parentNameLabel"; + this.parentNameLabel.Size = new System.Drawing.Size(67, 13); + this.parentNameLabel.TabIndex = 3; + this.parentNameLabel.Text = "Parent name"; + // + // ipAddressLabel + // + this.ipAddressLabel.AutoSize = true; + this.ipAddressLabel.Location = new System.Drawing.Point(36, 99); + this.ipAddressLabel.Name = "ipAddressLabel"; + this.ipAddressLabel.Size = new System.Drawing.Size(58, 13); + this.ipAddressLabel.TabIndex = 5; + this.ipAddressLabel.Text = "IP Address"; + // + // ipAddressTextBox + // + this.ipAddressTextBox.Enabled = false; + this.ipAddressTextBox.Location = new System.Drawing.Point(140, 96); + this.ipAddressTextBox.Name = "ipAddressTextBox"; + this.ipAddressTextBox.Size = new System.Drawing.Size(130, 20); + this.ipAddressTextBox.TabIndex = 6; + // + // tcpPortTextBox + // + this.tcpPortTextBox.Enabled = false; + this.tcpPortTextBox.Location = new System.Drawing.Point(140, 122); + this.tcpPortTextBox.Name = "tcpPortTextBox"; + this.tcpPortTextBox.Size = new System.Drawing.Size(130, 20); + this.tcpPortTextBox.TabIndex = 8; + // + // tcpPortLabel + // + this.tcpPortLabel.AutoSize = true; + this.tcpPortLabel.Location = new System.Drawing.Point(36, 125); + this.tcpPortLabel.Name = "tcpPortLabel"; + this.tcpPortLabel.Size = new System.Drawing.Size(65, 13); + this.tcpPortLabel.TabIndex = 7; + this.tcpPortLabel.Text = "TCP/IP Port"; + // + // AmbientCfgCtrl + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.BackColor = System.Drawing.SystemColors.Control; + this.Controls.Add(this.tcpPortTextBox); + this.Controls.Add(this.tcpPortLabel); + this.Controls.Add(this.ipAddressTextBox); + this.Controls.Add(this.ipAddressLabel); + this.Controls.Add(this.serialNrTextBox); + this.Controls.Add(this.serialNrLabel); + this.Controls.Add(this.parentNameComboBox); + this.Controls.Add(this.parentNameLabel); + this.Controls.Add(this.nameTextBox); + this.Controls.Add(this.nameLabel); + this.Controls.Add(this.classNameLabel); + this.Name = "AmbientCfgCtrl"; + this.Size = new System.Drawing.Size(400, 254); + this.Load += new System.EventHandler(this.AbbientCfgCtrl_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox nameTextBox; + private System.Windows.Forms.Label nameLabel; + private System.Windows.Forms.Label classNameLabel; + private System.Windows.Forms.TextBox serialNrTextBox; + private System.Windows.Forms.Label serialNrLabel; + private System.Windows.Forms.ComboBox parentNameComboBox; + private System.Windows.Forms.Label parentNameLabel; + private System.Windows.Forms.Label ipAddressLabel; + private System.Windows.Forms.TextBox ipAddressTextBox; + private System.Windows.Forms.TextBox tcpPortTextBox; + private System.Windows.Forms.Label tcpPortLabel; + + } +} diff --git a/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.cs b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.cs new file mode 100644 index 000000000..4e34e3a5a --- /dev/null +++ b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.cs @@ -0,0 +1,144 @@ +/// +/// Copyright (c) 2018 Sensus Slovensko a.s. +/// +using System; +using System.Net; +using System.Windows.Forms; +using log4net; +using TBF.BenchControl.Generic; +using TBF.Resources; + +namespace TBF.BenchControl.Network.Comet.Ambient +{ + public partial class AmbientCfgCtrl : UserControl, IComponentCfgCtrl + { + static readonly ILog log = LogManager.GetLogger(typeof(AmbientCfgCtrl)); + + ComponentParametersDlg parent; + + public bool ShowMore { get { return false; } } + + AmbientCfg config; + public IComponentCfg Config + { + get { return config as IComponentCfg; } + set + { + config = value as AmbientCfg; + Redraw(); + } + } + + public AmbientCfgCtrl() + { + InitializeComponent(); + } + + private void AbbientCfgCtrl_Load(object sender, EventArgs e) + { + Localize(); + + parent = ParentForm as ComponentParametersDlg; + if (parent == null) return; + + if (parent.TbfComponents != null) + { + parentNameComboBox.Items.Add("---"); + foreach (var cmpnt in parent.TbfComponents) + { + if (TbfComponents.CmpntFactoryFromClassName(cmpnt.ClassName) is TBF.BenchControl.Network.Adapter.Factory) + { + parentNameComboBox.Items.Add(cmpnt.Name); + } + } + } + + Redraw(); + } + + void Localize() + { + nameLabel.Text = Strings.Name; + parentNameLabel.Text = Strings.Parent_name; + serialNrLabel.Text = Strings.Serial_Number; + } + + public void Closing() + { + } + + void Redraw() + { + if (config == null) return; /// Control was not loaded, settings were not changed + + classNameLabel.Text = config.Factory.ClassName; + nameTextBox.Text = config.Name; + parentNameComboBox.Text = string.IsNullOrEmpty(config.ParentName) ? "---" : config.ParentName; + ipAddressTextBox.Text = config.IPAddress != null ? config.IPAddress.ToString() : "192.168.0.123"; + tcpPortTextBox.Text = config.TcpPort.ToString(); + serialNrTextBox.Text = config.SerialNr.ToString(); + } + + public void Unlock() + { + nameTextBox.Enabled = true; + parentNameComboBox.Enabled = true; + ipAddressTextBox.Enabled = true; + tcpPortTextBox.Enabled = true; + serialNrTextBox.Enabled = true; + } + + public CfgUpdateFlags VerifyCfg(ref string message) + { + CfgUpdateFlags flags = CfgUpdateFlags.None; + + if (!parentNameComboBox.Items.Contains(parentNameComboBox.Text)) + { + flags |= CfgUpdateFlags.Error; + message += Environment.NewLine + string.Format(Strings.Invalid_0, parentNameLabel.Text); + } + + IPAddress ip; + if (!IPAddress.TryParse(ipAddressTextBox.Text, out ip)) + { + flags |= CfgUpdateFlags.Error; + message += Environment.NewLine + string.Format(Strings.Invalid_0, ipAddressLabel.Text); + } + + int port; + if (!int.TryParse(tcpPortTextBox.Text, out port) || port < 0 || port > 65535) + { + flags |= CfgUpdateFlags.Error; + message += Environment.NewLine + string.Format(Strings.Invalid_0, tcpPortLabel.Text); + } + + return flags; + } + + public CfgUpdateFlags UpdateCfg() + { + CfgUpdateFlags flags = CfgUpdateFlags.None; + + if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed + + string newParentName = parentNameComboBox.Text.Equals("---") ? string.Empty : parentNameComboBox.Text; + + if (config.Name != nameTextBox.Text || + config.ParentName != newParentName || + config.IPAddress != ipAddressTextBox.Text || + config.TcpPort.ToString() != tcpPortTextBox.Text || + config.SerialNr != serialNrTextBox.Text) + { + config.Name = nameTextBox.Text; + config.ParentName = newParentName; + config.IPAddress = ipAddressTextBox.Text; + config.TcpPort = int.Parse(tcpPortTextBox.Text); + config.SerialNr = serialNrTextBox.Text; + + flags |= (CfgUpdateFlags.RestartRqrd | CfgUpdateFlags.AnyChange); + } + + return flags; + } + } +} diff --git a/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.resx b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.resx new file mode 100644 index 000000000..1af7de150 --- /dev/null +++ b/TBF/BenchControl/Network/Comet/Ambient/AmbientCfgCtrl.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TBF/BenchControl/Network/Comet/Ambient/Factory.cs b/TBF/BenchControl/Network/Comet/Ambient/Factory.cs new file mode 100644 index 000000000..90bef6517 --- /dev/null +++ b/TBF/BenchControl/Network/Comet/Ambient/Factory.cs @@ -0,0 +1,26 @@ +/// +/// Copyright (c) 2018 Sensus Slovensko a.s. +/// +using System.Collections.Generic; +using TBF.BenchControl.Generic; + +namespace TBF.BenchControl.Network.Comet.Ambient +{ + public class Factory : IComponentFactory + { + public string ClassName { get { return this.GetType().Namespace.Substring(17); } } + + public void ResetStaticProperties() { Ambient.ResetStaticProperties(); } + + public IComponent DummyComponent() { return new Ambient(); } + + public IComponent GetComponent(IComponentCfg cfg, IList components) { return new Ambient(cfg, components); } + + public IComponentCfg DefaultConfig() { return new AmbientCfg(this.GetType().Namespace.Substring(17), this); } + + public IComponentCfg CmpntCfgFromCmpntEntity(Config.Entities.Component component) + { + return ComponentCfgBase.CreateFromDbEntity(AmbientCfg.Serializer, component, this); + } + } +} diff --git a/TBF/BenchControl/TbfComponents.cs b/TBF/BenchControl/TbfComponents.cs index 186111461..ce765cba6 100644 --- a/TBF/BenchControl/TbfComponents.cs +++ b/TBF/BenchControl/TbfComponents.cs @@ -96,6 +96,7 @@ namespace TBF.BenchControl Factories.Add(new Network.Camera.Display.Factory()); Factories.Add(new Network.Camera.Roi.Factory()); Factories.Add(new Network.Camera.RoiForFixedStart.Factory()); + Factories.Add(new Network.Comet.Ambient.Factory()); Factories.Add(new Output.DB.ProductionMonitoring.Factory()); Factories.Add(new Output.DB.SaveFlowmeterCorrections.Factory()); Factories.Add(new Output.FileWriters.Basic.FactorySingle()); diff --git a/TBF/Properties/AssemblyInfo.cs b/TBF/Properties/AssemblyInfo.cs index e8d658c22..42e92b3c6 100644 --- a/TBF/Properties/AssemblyInfo.cs +++ b/TBF/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ using System.Runtime.InteropServices; // Build Number // Revision // -[assembly: AssemblyVersion("2.18.820.0")] -[assembly: AssemblyFileVersion("2.18.820.0")] +[assembly: AssemblyVersion("2.18.821.0")] +[assembly: AssemblyFileVersion("2.18.821.0")] diff --git a/TBF/TBF.csproj b/TBF/TBF.csproj index e8c530309..100b8aaf2 100644 --- a/TBF/TBF.csproj +++ b/TBF/TBF.csproj @@ -721,6 +721,15 @@ + + + + UserControl + + + AmbientCfgCtrl.cs + + @@ -2306,6 +2315,9 @@ RoiCfgCtrl.cs + + AmbientCfgCtrl.cs + AskYesNoForm.cs