diff --git a/TBF/BenchControl/Network/Adapter/AdapterInfo.cs b/TBF/BenchControl/Network/AdapterInfo.cs similarity index 87% rename from TBF/BenchControl/Network/Adapter/AdapterInfo.cs rename to TBF/BenchControl/Network/AdapterInfo.cs index f39a4ed6f..255ac85a5 100644 --- a/TBF/BenchControl/Network/Adapter/AdapterInfo.cs +++ b/TBF/BenchControl/Network/AdapterInfo.cs @@ -5,7 +5,7 @@ using System.Net.NetworkInformation; using System.Text; using System.Web; -namespace TBF.BenchControl.Network.Adapter +namespace TBF.BenchControl.Network { /// /// Class for the storing information about a network adapter. @@ -22,23 +22,32 @@ namespace TBF.BenchControl.Network.Adapter Description = description; } + /// Member function public IPAddress GetBroadcastAddress() { - byte[] ipAdressBytes = IPAddress.GetAddressBytes(); - byte[] subnetMaskBytes = NetMask.GetAddressBytes(); - - if (ipAdressBytes.Length != subnetMaskBytes.Length) - throw new ArgumentException("Lengths of IP address and subnet mask do not match."); - - byte[] broadcastAddress = new byte[ipAdressBytes.Length]; - for (int i = 0; i < broadcastAddress.Length; i++) - { - broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255)); - } - - return new IPAddress(broadcastAddress); + return AdapterInfo.GetBroadcastAddress(IPAddress, NetMask); } + /// Static function + static public IPAddress GetBroadcastAddress(IPAddress ipAddress, IPAddress netMask) + { + byte[] ipAdressBytes = ipAddress.GetAddressBytes(); + byte[] subnetMaskBytes = netMask.GetAddressBytes(); + + if (ipAdressBytes.Length != subnetMaskBytes.Length) + { + throw new ArgumentException("Lengths of IP address and subnet mask do not match."); + } + + byte[] broadcastAddress = new byte[ipAdressBytes.Length]; + for (int i = 0; i < broadcastAddress.Length; i++) + { + broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255)); + } + + return new IPAddress(broadcastAddress); + } + ///------------------------------------------------------ /// Static members and functions ///------------------------------------------------------ diff --git a/TBF/BenchControl/Output/DB/ProductionTracing/MonitoringCfg.cs b/TBF/BenchControl/Output/DB/ProductionTracing/MonitoringCfg.cs index b9e8ad1b2..e9b58b629 100644 --- a/TBF/BenchControl/Output/DB/ProductionTracing/MonitoringCfg.cs +++ b/TBF/BenchControl/Output/DB/ProductionTracing/MonitoringCfg.cs @@ -19,6 +19,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing public string Workplace; public string ConnStr; + public string NetAdapter; public bool CheckPreviousRecords; public bool SaveTracingRecords; @@ -28,6 +29,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing { ParentName = string.Empty; Workplace = "WR10"; + NetAdapter = string.Empty; ConnStr = "SERVER=10.42.128.16; DATABASE=sledovanie2019; UID=vyroba2019; PASSWORD=qwerty; CHARSET=utf8;"; CheckPreviousRecords = true; SaveTracingRecords = true; diff --git a/TBF/BenchControl/Output/DB/ProductionTracing/Tracing.cs b/TBF/BenchControl/Output/DB/ProductionTracing/Tracing.cs index 77a659c5d..7666b2044 100644 --- a/TBF/BenchControl/Output/DB/ProductionTracing/Tracing.cs +++ b/TBF/BenchControl/Output/DB/ProductionTracing/Tracing.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using log4net; using NHibernate; using NHibernate.Criterion; @@ -36,6 +37,12 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing MonitoringCfg tracingCfg; string workplace { get { return tracingCfg.Workplace; } } + readonly IPAddress ipAddress; + readonly IPAddress netMask; + + public IPAddress IPAddress { get { return ipAddress; } } + public IPAddress NetMask { get { return netMask; } } + enum OpState { @@ -84,6 +91,12 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing { tracingCfg = cfg as MonitoringCfg; if (tracingCfg == null) throw new ArgumentException("tracingCfg"); + + Network.AdapterInfo.RefreshNetAdaptersInfo(); + Network.AdapterInfo netadapter = Network.AdapterInfo.GetNetAdapter(tracingCfg.NetAdapter); + ipAddress = netadapter.IPAddress; + + netMask = netadapter.NetMask; currentOpState = OpState.None; log.Debug(this.ToString()); } @@ -95,6 +108,8 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing { if (tracingCfg.DebugLevel == DebugMode.Simulate) return; + string ipAddress = GetIPAddress(); + /// /// Session factory is used to create database sessions /// @@ -111,7 +126,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing TracingDB.DB.RegisterWorkplace(session, workplace, Users.GlobalData.GetCurrentUserName(), - "1.2.3.4", + GetIPAddress(), "", WorkstepName, DateTime.Now + new TimeSpan(15, 0, 0, 0)); @@ -123,6 +138,11 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing currentProcess = null; } /// + string GetIPAddress() + { + return (ipAddress != null) ? ipAddress.ToString() : "1.2.3.4"; + } + /// public void RunDeviceBefore() { } public void RunDeviceAfter() { } /// diff --git a/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.cs b/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.cs index f2fb80369..e4cae6f3e 100644 --- a/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.cs +++ b/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.cs @@ -5,6 +5,8 @@ using System; using System.Windows.Forms; using Config.Entities; using TBF.BenchControl.Generic; +using System.Collections.Generic; +using System.Net; namespace TBF.BenchControl.Output.DB.ProductionTracing { @@ -30,6 +32,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing private void WriterCfgCtrl_Load(object sender, EventArgs e) { + if (config == null) return; Redraw(); } @@ -47,6 +50,53 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing connStrTextBox.Text = config.ConnStr; checkPreviousRecordsCheckBox.Checked = config.CheckPreviousRecords; saveTracingRecordsCheckBox.Checked = config.SaveTracingRecords; + + Network.AdapterInfo.RefreshNetAdaptersInfo(); + IList adapters = Network.AdapterInfo.NetAdapters; + + string cfgAdapter = string.Empty; + foreach (Network.AdapterInfo ai in adapters) + { + string record; + + if (ai.IPAddress == null) + { + /// This happens with network adapters that currently have no IP address, + /// for instance not connected wireless or dial-up adapters + record = "???.???.???.???"; + } + else + { + /// Do not consider IPv6 adapters as well as "Any", "Broadcast", "Loopback" or "None" addresses + if ((ai.IPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) || + ai.IPAddress.Equals(IPAddress.Any) || + ai.IPAddress.Equals(IPAddress.Broadcast) || + ai.IPAddress.Equals(IPAddress.IPv6Any) || + ai.IPAddress.Equals(IPAddress.IPv6Loopback) || + ai.IPAddress.Equals(IPAddress.IPv6None) || + ai.IPAddress.Equals(IPAddress.Loopback) || + ai.IPAddress.Equals(IPAddress.None)) + { + continue; + } + + record = ai.IPAddress.ToString(); + } + + record += " - " + ai.Description; + int i = netAdapterComboBox.Items.Add(record); + if (ai.Description == config.NetAdapter) + { + /// Select the adapter currently in the configuration + netAdapterComboBox.SelectedIndex = i; + } + } + + /// Select the first one if the adapter from the configuration does not exist on the system + if (netAdapterComboBox.SelectedIndex < 0 && netAdapterComboBox.Items.Count > 0) + { + netAdapterComboBox.SelectedIndex = 0; + } } public void Unlock() @@ -54,6 +104,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing nameTextBox.Enabled = true; workplaceTextBox.Enabled = true; connStrTextBox.Enabled = true; + netAdapterComboBox.Enabled = true; checkPreviousRecordsCheckBox.Enabled = true; saveTracingRecordsCheckBox.Enabled = true; } @@ -80,6 +131,16 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing flags |= UpdateDifferent(ref config.CheckPreviousRecords, checkPreviousRecordsCheckBox.Checked, CfgUpdateFlags.AnyChange | CfgUpdateFlags.RestartRqrd); flags |= UpdateDifferent(ref config.SaveTracingRecords, saveTracingRecordsCheckBox.Checked, CfgUpdateFlags.AnyChange | CfgUpdateFlags.RestartRqrd); + /// Network adapter + string strAdapter = netAdapterComboBox.Text; + int iDash = strAdapter.IndexOf(" - "); + string netAdapter = (iDash < 0) ? "" : strAdapter.Substring(iDash + 3); + if (config.NetAdapter != netAdapter) + { + config.NetAdapter = netAdapter; + flags |= (CfgUpdateFlags.AnyChange | CfgUpdateFlags.RestartRqrd); + } + return flags; } } diff --git a/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.designer.cs b/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.designer.cs index a42862286..987a8e7f9 100644 --- a/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.designer.cs +++ b/TBF/BenchControl/Output/DB/ProductionTracing/TracingCfgCtrl.designer.cs @@ -40,6 +40,8 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing this.connStrLabel = new System.Windows.Forms.Label(); this.saveTracingRecordsCheckBox = new System.Windows.Forms.CheckBox(); this.checkPreviousRecordsCheckBox = new System.Windows.Forms.CheckBox(); + this.netAdapterLabel = new System.Windows.Forms.Label(); + this.netAdapterComboBox = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // nameTextBox @@ -91,7 +93,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing this.connStrTextBox.Location = new System.Drawing.Point(110, 93); this.connStrTextBox.Name = "connStrTextBox"; this.connStrTextBox.Size = new System.Drawing.Size(327, 20); - this.connStrTextBox.TabIndex = 22; + this.connStrTextBox.TabIndex = 6; // // connStrLabel // @@ -99,17 +101,17 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing this.connStrLabel.Location = new System.Drawing.Point(15, 96); this.connStrLabel.Name = "connStrLabel"; this.connStrLabel.Size = new System.Drawing.Size(89, 13); - this.connStrLabel.TabIndex = 21; + this.connStrLabel.TabIndex = 5; this.connStrLabel.Text = "Connection string"; // // saveTracingRecordsCheckBox // this.saveTracingRecordsCheckBox.AutoSize = true; this.saveTracingRecordsCheckBox.Enabled = false; - this.saveTracingRecordsCheckBox.Location = new System.Drawing.Point(110, 154); + this.saveTracingRecordsCheckBox.Location = new System.Drawing.Point(110, 177); this.saveTracingRecordsCheckBox.Name = "saveTracingRecordsCheckBox"; this.saveTracingRecordsCheckBox.Size = new System.Drawing.Size(177, 17); - this.saveTracingRecordsCheckBox.TabIndex = 24; + this.saveTracingRecordsCheckBox.TabIndex = 10; this.saveTracingRecordsCheckBox.Text = "Save production tracing records"; this.saveTracingRecordsCheckBox.UseVisualStyleBackColor = true; // @@ -117,17 +119,36 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing // this.checkPreviousRecordsCheckBox.AutoSize = true; this.checkPreviousRecordsCheckBox.Enabled = false; - this.checkPreviousRecordsCheckBox.Location = new System.Drawing.Point(110, 131); + this.checkPreviousRecordsCheckBox.Location = new System.Drawing.Point(110, 154); this.checkPreviousRecordsCheckBox.Name = "checkPreviousRecordsCheckBox"; this.checkPreviousRecordsCheckBox.Size = new System.Drawing.Size(138, 17); - this.checkPreviousRecordsCheckBox.TabIndex = 23; + this.checkPreviousRecordsCheckBox.TabIndex = 9; this.checkPreviousRecordsCheckBox.Text = "Check previous records"; this.checkPreviousRecordsCheckBox.UseVisualStyleBackColor = true; // + // netAdapterLabel + // + this.netAdapterLabel.AutoSize = true; + this.netAdapterLabel.Location = new System.Drawing.Point(15, 122); + this.netAdapterLabel.Name = "netAdapterLabel"; + this.netAdapterLabel.Size = new System.Drawing.Size(86, 13); + this.netAdapterLabel.TabIndex = 7; + this.netAdapterLabel.Text = "Network adapter"; + // + // netAdapterComboBox + // + this.netAdapterComboBox.FormattingEnabled = true; + this.netAdapterComboBox.Location = new System.Drawing.Point(110, 119); + this.netAdapterComboBox.Name = "netAdapterComboBox"; + this.netAdapterComboBox.Size = new System.Drawing.Size(327, 21); + this.netAdapterComboBox.TabIndex = 11; + // // TracingCfgCtrl // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.netAdapterComboBox); + this.Controls.Add(this.netAdapterLabel); this.Controls.Add(this.saveTracingRecordsCheckBox); this.Controls.Add(this.checkPreviousRecordsCheckBox); this.Controls.Add(this.connStrTextBox); @@ -156,5 +177,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing private System.Windows.Forms.Label connStrLabel; private System.Windows.Forms.CheckBox saveTracingRecordsCheckBox; private System.Windows.Forms.CheckBox checkPreviousRecordsCheckBox; + private System.Windows.Forms.Label netAdapterLabel; + private System.Windows.Forms.ComboBox netAdapterComboBox; } } diff --git a/TBF/MainWnd.cs b/TBF/MainWnd.cs index a8515dd44..d20d1e718 100644 --- a/TBF/MainWnd.cs +++ b/TBF/MainWnd.cs @@ -1,5 +1,5 @@ /// -/// Copyright (c) 2013-201 Sensus Slovensko a.s. +/// Copyright (c) 2013-2019 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; diff --git a/TBF/TBF.csproj b/TBF/TBF.csproj index 97f102907..8c609ef10 100644 --- a/TBF/TBF.csproj +++ b/TBF/TBF.csproj @@ -755,7 +755,7 @@ - +