async Task EditRouterAddressCell(NetworkDeviceItem editedNDI, DataGridViewCell editedCell, string proposedAddress) { string oldAddress = editedNDI.RouterAddress; try { editedNDI.ChangeRouterAddress(proposedAddress); } catch (EngineeringNotSupportedException ex) { txt_Status.AppendText(ex.Message + Environment.NewLine); Debug.WriteLine(ex.Message); editedCell.Value = oldAddress; } string newAddress = editedNDI.RouterAddress; if (!oldAddress.Equals(newAddress, StringComparison.Ordinal)) { editedCell.Value = newAddress; txt_Status.AppendText($"Device {editedNDI.HMName} changed router address from: {oldAddress} to: {newAddress}." + $"{Environment.NewLine}"); } }
private static string[] CreateRecord(int Id, NetworkDeviceItem netDeviceItem, IoSystemLevel ioSystemLevel) { //IoSystemLevel ioSystemLevel_proper = netDeviceItem.IoSystemLevel; // work on that string deviceName = netDeviceItem.HMName; string ipAddress = netDeviceItem.IpAddress ?? ""; string addressMask = netDeviceItem.AddressMask ?? ""; string pnDeviceName = netDeviceItem.PnDeviceName ?? ""; string interfaceOperatingMode = netDeviceItem.InterfaceOperatingMode; string subnetName = netDeviceItem.PnSubnetName ?? "[Not connected]"; string ioSystemName = ioSystemLevel?.IoSystemName ?? "[Not connected]"; string pnDeviceNumber = netDeviceItem.PnDeviceNumber ?? "[N/A]"; string routerAddress = netDeviceItem.RouterAddress ?? "[Not used]"; bool autoPnName = netDeviceItem.autoGeneratePnName; string[] record = new string[] { Id.ToString(), interfaceOperatingMode, deviceName, autoPnName.ToString(), pnDeviceName, pnDeviceNumber, ipAddress, addressMask, routerAddress, subnetName, ioSystemName, }; return(record); }
private static string CreateCSVRecord(NetworkDeviceItem netDeviceItem, IoSystemLevel ioSystemLevel) { string deviceName = netDeviceItem.HMName; string ipAddress = netDeviceItem.IpAddress; string addressMask = netDeviceItem.AddressMask; string pnDeviceName = netDeviceItem.PnDeviceName; string interfaceOperatingMode = netDeviceItem.InterfaceOperatingMode; string subnetName = netDeviceItem.PnSubnetName ?? "[Not connected]"; string ioSystemName = ioSystemLevel?.IoSystemName ?? "[Not connected]"; string pnDeviceNumber = netDeviceItem.PnDeviceNumber ?? "[N/A]"; string routerAddress = netDeviceItem.RouterAddress ?? "[Not used]"; bool autoPnName = netDeviceItem.autoGeneratePnName; string record = "" + interfaceOperatingMode + ";" + deviceName + ";" + autoPnName.ToString() + ";" + pnDeviceName + ";" + pnDeviceNumber + ";" + ipAddress + ";" + addressMask + ";" + routerAddress + ";" + subnetName + ";" + ioSystemName; return(record); }
async Task EditSubnetNameCell(NetworkDeviceItem editedNDI, DataGridViewCell editedCell, string proposedSubnetName) { string oldName = editedNDI.PnSubnetName; editedNDI.ChangeSubnetName(proposedSubnetName); string newName = editedNDI.PnSubnetName; if (oldName.Equals(newName, StringComparison.Ordinal)) { txt_Status.AppendText($"The name of {oldName} subnet was not changed.{Environment.NewLine}"); return; } editedCell.Value = newName; foreach (DataGridViewRow row in deviceTable.Rows) { DataGridViewCell subnetCell = row.Cells[dgv_PnSubnet.Index]; string cellSubnetName = subnetCell.Value.ToString(); if (cellSubnetName.Equals(oldName)) { DataGridViewCell idCell = row.Cells[dgv_Id.Index]; int deviceKey = int.Parse(idCell.Value.ToString()); subnetCell.Value = await DeviceRowsHelper.DeviceByRowID[deviceKey].UpdatePnSubnetName(); } } txt_Status.AppendText($"Subnet {oldName} changed name to {newName}.{Environment.NewLine}"); }
async Task EditIoSystemNameCell(NetworkDeviceItem editedNDI, DataGridViewCell editedCell, string proposedIoSystemName) { string oldName = editedNDI.IoSystemName; editedNDI.ChangeIoSystemName(proposedIoSystemName); string newName = editedNDI.IoSystemName; if (oldName.Equals(newName, StringComparison.Ordinal)) { txt_Status.AppendText($"The name of \"{oldName}\" IO system was not changed.{Environment.NewLine}"); return; } editedCell.Value = newName; foreach (DataGridViewRow row in deviceTable.Rows) { DataGridViewCell ioSystemCell = row.Cells[dgv_IoSystem.Index]; string cellIoSystemName = ioSystemCell.Value.ToString(); if (!cellIoSystemName.Equals("[Not connected]")) { DataGridViewCell idCell = row.Cells[dgv_Id.Index]; int deviceKey = int.Parse(idCell.Value.ToString()); ioSystemCell.Value = await DeviceRowsHelper.DeviceByRowID[deviceKey].UpdateIoSystemName(); } } txt_Status.AppendText($"IO system \"{oldName}\" changed name to \"{newName}\".{Environment.NewLine}"); }
async Task EditPnDeviceNameCell(NetworkDeviceItem editedNDI, DataGridViewRow editedRow, DataGridViewCell editedCell, string proposedPnName) { if (String.IsNullOrWhiteSpace(proposedPnName)) { return; } string oldPnName = editedNDI.PnDeviceName; // bad idea, bc it changes autogeneration, even if it's the same name //if (oldPnName.Equals(proposedPnName, StringComparison.Ordinal)) return; //if (oldPnName.Equals(newPnName, StringComparison.Ordinal)) return; await editedNDI.ChangePnDeviceName(proposedPnName); string newPnName = editedNDI.PnDeviceName; editedCell.Value = newPnName; DataGridViewCell autoGenPnNameCell = editedRow.Cells[dgv_autoGeneratePnName.Index]; autoGenPnNameCell.Value = editedNDI.autoGeneratePnName; txt_Status.AppendText($"Device \"{editedNDI.HMName}\" changed Profinet device name from: " + $"\"{oldPnName}\" to: \"{newPnName}\".{Environment.NewLine}"); }
private static int CompareByDeviceName(NetworkDeviceItem x, NetworkDeviceItem y) { if (x == null) { if (y == null) { return(0); } else { return(-1); // y is greater. } } else { // If x is not null... if (y == null) // ...and y is null, x is greater. { return(1); } else { // ...and y is not null, compare the // lengths of the two strings. return(x.HMName.CompareTo(y.HMName)); //int retval = x.Length.CompareTo(y.Length); } } }
async Task EditIPAddressCell(NetworkDeviceItem editedNDI, DataGridViewCell editedCell, string proposedAddress) { string oldAddress = editedNDI.IpAddress; Task task = editedNDI.ChangeIPAddress(proposedAddress); try { await task.ConfigureAwait(true); } catch (ArgumentException ex) { editedCell.Value = oldAddress; txt_Status.AppendText(ex.Message + Environment.NewLine); } catch (EngineeringNotSupportedException ex) { editedCell.Value = oldAddress; txt_Status.AppendText(ex.Message + Environment.NewLine); Debug.WriteLine(ex.Message); } string newAddress = editedNDI.IpAddress; editedCell.Value = newAddress; if (editedNDI.IpAddress == proposedAddress) { txt_Status.AppendText($"Device \"{editedNDI.HMName}\" changed IP address from: {oldAddress} to: {newAddress}." + $"{Environment.NewLine}"); } DeviceRowsHelper.MarkRepeatingIPs(deviceTable.Rows, dgv_IpAddress.Index, dgv_Mode.Index); }
public static DataGridViewRow InitializeRow(int Id, NetworkDeviceItem netDeviceItem, IoSystemLevel ioSystemLevel, DataGridView dgvWithExpectedStyle) { DeviceRowsHelper.DeviceByRowID.Add(Id, netDeviceItem); DataGridViewRow row = new DataGridViewRow(); row.CreateCells(dgvWithExpectedStyle); row.SetValues(CreateRecord(Id, netDeviceItem, ioSystemLevel)); return(row); }
async void deviceTable_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (deviceTableAutoEdit) { return; } deviceTableAutoEdit = true; DataGridViewRow editedRow = deviceTable.Rows[e.RowIndex]; DataGridViewCell editedCell = editedRow.Cells[e.ColumnIndex]; string newValue = editedCell.Value?.ToString() ?? ""; int id = int.Parse(editedRow.Cells[dgv_Id.Index].Value.ToString()); NetworkDeviceItem netDeviceItem = DeviceRowsHelper.DeviceByRowID[id]; if (e.ColumnIndex == dgv_IpAddress.Index) { await EditIPAddressCell(netDeviceItem, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_DeviceName.Index) { await EditDeviceNameCell(netDeviceItem, editedRow, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_Mask.Index) { await EditMaskCell(netDeviceItem, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_PnDeviceName.Index) { await EditPnDeviceNameCell(netDeviceItem, editedRow, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_PnSubnet.Index) { await EditSubnetNameCell(netDeviceItem, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_IoSystem.Index) { await EditIoSystemNameCell(netDeviceItem, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_autoGeneratePnName.Index) { await EditAutoGenerateCell(netDeviceItem, editedRow, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_PnNumber.Index) { await EditPnNumberCell(netDeviceItem, editedCell, newValue).ConfigureAwait(true); } else if (e.ColumnIndex == dgv_RouterAddress.Index) { await EditRouterAddressCell(netDeviceItem, editedCell, newValue).ConfigureAwait(true); } deviceTableAutoEdit = false; }
private DataGridViewRow UnusedDeviceRow(NetworkDeviceItem unusedDevice) { var unusedDeviceRow = DeviceRowsHelper.InitializeRow(ID.NextID, unusedDevice, null, deviceTable); var cells = unusedDeviceRow.Cells; var deviceNameCell = cells[dgv_DeviceName.Index]; DeviceRowsHelper.PadCell(deviceNameCell, 5); DeviceRowsHelper.DisableCells(cells[dgv_IoSystem.Index], cells[dgv_PnSubnet.Index], cells[dgv_RouterAddress.Index], cells[dgv_Mask.Index], cells[dgv_PnNumber.Index]); return(unusedDeviceRow); }
private static int CompareByDeviceIpAddress(NetworkDeviceItem x, NetworkDeviceItem y) { if (x == null) { if (y == null) { return(0); } else { return(-1); // y is greater. } } else { // If x is not null... if (y == null) // ...and y is null, x is greater. { return(1); } else { // x and y exist but check if they have ip addresses if (String.IsNullOrEmpty(x.IpAddress)) { if (String.IsNullOrEmpty(y.IpAddress)) { return(0); } else { return(-1); // y.address is greater. } } else { if (String.IsNullOrEmpty(y.IpAddress)) { return(1); } else { // they both have ip addresses return(x.IpAddress.CompareTo(y.IpAddress)); } } } } }
private DataGridViewRow IoDeviceRow(NetworkDeviceItem ioDevice, IoSystemLevel ioSystem) { var ioDeviceRow = DeviceRowsHelper.InitializeRow(ID.NextID, ioDevice, ioSystem, deviceTable); var cells = ioDeviceRow.Cells; var deviceNameCell = cells[dgv_DeviceName.Index]; DeviceRowsHelper.PadCell(deviceNameCell, 25); DeviceRowsHelper.DisableCells(cells[dgv_IoSystem.Index], cells[dgv_PnSubnet.Index], cells[dgv_RouterAddress.Index], cells[dgv_Mask.Index]); if (string.IsNullOrEmpty(cells[dgv_IpAddress.Index].Value?.ToString())) // empty in future? { DeviceRowsHelper.DisableCell(cells[dgv_IpAddress.Index]); } return(ioDeviceRow); }
async Task EditPnNumberCell(NetworkDeviceItem editedNDI, DataGridViewCell editedCell, string proposedNumber) { string oldNumber = editedNDI.PnDeviceNumber; int pnNumber; if (int.TryParse(proposedNumber, out pnNumber)) { editedNDI.ChangePnNumber(pnNumber); } string newNumber = editedNDI.PnDeviceNumber; editedCell.Value = newNumber; txt_Status.AppendText($"Device \"{editedNDI.HMName}\" changed Profinet number from: " + $"\"{oldNumber}\" to: \"{newNumber}\".{Environment.NewLine}"); }
async Task EditMaskCell(NetworkDeviceItem editedNDI, DataGridViewCell editedCell, string proposedMask) { string oldMask = editedNDI.AddressMask; Task task = editedNDI.ChangeAddressMask(proposedMask); try { await task.ConfigureAwait(true); } catch (ArgumentException ex) { editedCell.Value = oldMask; txt_Status.AppendText(ex.Message + Environment.NewLine); return; } catch (EngineeringNotSupportedException ex) { editedCell.Value = oldMask; txt_Status.AppendText(ex.Message + Environment.NewLine); Debug.WriteLine(ex.Message); return; } string newMask = editedNDI.AddressMask; editedCell.Value = newMask; txt_Status.AppendText($"Device \"{editedNDI.HMName}\" changed address mask from: " + $"{oldMask} to: {newMask}.{Environment.NewLine}"); // only masks of devices in the same subnet (or iosystem) have changed so they should be filtered // actually, not really sure about that, some devices work both as IoControllers and IoDevices // in different IoSystems, so lets update everything to make sure. foreach (DataGridViewRow row in deviceTable.Rows) { // this is not valid, bc index value is already a string //int deviceKey = (int)row.Cells[dgv_Id.Index].Value; int deviceKey = int.Parse(row.Cells[dgv_Id.Index].Value.ToString()); row.Cells[dgv_Mask.Index].Value = DeviceRowsHelper.DeviceByRowID[deviceKey].UpdateAddressMask(); } }
private async Task <List <NetworkDeviceItem> > GetUnusedDeviceItems() { var validDeviceItems = new List <NetworkDeviceItem>(); var validDeviceItemTasks = new List <Task <NetworkDeviceItem> >(); UnusedDevices.ForEach(ud => { foreach (DeviceItem deviceItem in ud.DeviceItems) { foreach (DeviceItem couldBeInterfaceItem in deviceItem.DeviceItems) // going one level deep { //NetworkDeviceItem ndi; validDeviceItemTasks.Add(NetworkDeviceItem.Create(couldBeInterfaceItem, NetworkDeviceItemLevel.Project, NetworkDeviceItemWorkMode.None, null, null)); } } }); validDeviceItems.AddRange(await Task.WhenAll <NetworkDeviceItem>(validDeviceItemTasks).ConfigureAwait(false)); validDeviceItems.RemoveAll((vdi) => vdi == null); return(validDeviceItems); }
public static async Task <NetworkDeviceItem> Create(DeviceItem di, NetworkDeviceItemLevel ndil, //ndil will be unnecessary now when ndi references levels NetworkDeviceItemWorkMode workMode, SubnetLevel subnetLevel, IoSystemLevel ioSystemLevel) { NetworkInterface ni = ((IEngineeringServiceProvider)di).GetService <NetworkInterface>(); if (ni == null) { return(null); } Node node = ni.Nodes[0]; // This hack is for distinguishing between HMI and Ethernet Commissioning device. // The proper way would be to find some attribute that differentiates them but I could not find any // You could check for existence of any profinet attribute but GetAttributeInfos is **HEAVY** if (ni.IoConnectors.Count == 0 && ni.IoControllers.Count == 0) { try { node.GetAttribute("PnDeviceNameAutoGeneration"); } catch { return(null); } } string nodeType = node.GetAttribute("NodeType").ToString(); if (!nodeType.Equals("Ethernet")) { return(null); } var ndi = new NetworkDeviceItem(di, node, ndil, workMode, subnetLevel, ioSystemLevel); await ndi.UpdateAllParameters().ConfigureAwait(false); return(ndi); }
async Task EditAutoGenerateCell(NetworkDeviceItem editedNDI, DataGridViewRow editedRow, DataGridViewCell editedCell, string proposedValue) { bool proposedBool = Boolean.Parse(proposedValue); editedNDI.ChangePnDeviceNameAutoGeneration(proposedBool); bool isAutogenerated = editedNDI.autoGeneratePnName; editedCell.Value = isAutogenerated; editedRow.Cells[dgv_PnDeviceName.Index].Value = editedNDI.UpdatePnDeviceName(); if (isAutogenerated) { txt_Status.AppendText($"Profinet name of device \"{editedNDI.HMName}\" is autogenerated from " + $"its device name.{Environment.NewLine}"); } else { txt_Status.AppendText($"Profinet name of device \"{editedNDI.HMName}\" is not autogenerated." + $"{Environment.NewLine}"); } }
private async Task PopulateIoSystemLvl() { DeviceItem controller = (DeviceItem)ioSystem.Parent.Parent.Parent; var ioDeviceTasks = new List <Task <NetworkDeviceItem> >(); var IoControllerTask = NetworkDeviceItem.Create(controller, NetworkDeviceItemLevel.IoSystem, NetworkDeviceItemWorkMode.IoController, SubnetLevel, this); foreach (var connector in ioSystem.ConnectedIoDevices) { DeviceItem di = (DeviceItem)connector.Parent.Parent; ioDeviceTasks.Add(NetworkDeviceItem.Create(di, NetworkDeviceItemLevel.IoSystem, NetworkDeviceItemWorkMode.IoDevice, SubnetLevel, this)); //if (ioDevice != null) IoDevices.Add(ioDevice); } IoController = await IoControllerTask.ConfigureAwait(false); NetworkDeviceItem[] ioDevices = await Task.WhenAll <NetworkDeviceItem>(ioDeviceTasks).ConfigureAwait(false); IoDevices.AddRange(ioDevices.Where(iod => iod != null)); //IoDevices.RemoveAll((iod) => iod == null); }
private async Task <List <NetworkDeviceItem> > FindDevItemsWithoutIoSystem() { // It most likely can create less lists in the process List <NetworkDeviceItem> inSubnetButNotInIoSystem = new List <NetworkDeviceItem>(); var subnetDeviceTasks = new List <Task <NetworkDeviceItem> >(); foreach (Node node in subnet.Nodes) { NetworkInterface ni = (NetworkInterface)node.Parent; if (IsInIoSystem(ni)) { continue; } //string nodeType = node.GetAttribute("NodeType").ToString(); //if (!nodeType.Equals("Ethernet")) continue; subnetDeviceTasks.Add(NetworkDeviceItem.Create((DeviceItem)ni.Parent, NetworkDeviceItemLevel.IoSystem, NetworkDeviceItemWorkMode.None, this, null)); } NetworkDeviceItem[] inThisSubnet = await Task.WhenAll <NetworkDeviceItem>(subnetDeviceTasks).ConfigureAwait(false); inSubnetButNotInIoSystem.AddRange(inThisSubnet.Where(iod => iod != null)); return(inSubnetButNotInIoSystem); }
private static int CompareByDeviceIpAddressRev(NetworkDeviceItem x, NetworkDeviceItem y) { return(-1 * CompareByDeviceIpAddress(x, y)); }
async Task EditDeviceNameCell(NetworkDeviceItem editedNDI, DataGridViewRow editedRow, DataGridViewCell editedCell, string proposedName) { string oldName = editedNDI.HMName; string oldPnName = editedNDI.PnDeviceName; if (oldName == proposedName) { return; } if (String.IsNullOrWhiteSpace(proposedName)) { editedCell.Value = oldName; txt_Status.AppendText($"Device name cannot be empty.{Environment.NewLine}"); return; } foreach (DataGridViewRow row in deviceTable.Rows) { if (row.Equals(editedRow)) { continue; } string deviceName = row.Cells[dgv_DeviceName.Index].Value.ToString(); if (deviceName.Equals(proposedName, StringComparison.Ordinal)) { editedCell.Value = oldName; txt_Status.AppendText($"Chosen device name is already taken.{Environment.NewLine}"); return; } } await editedNDI.ChangeName(proposedName).ConfigureAwait(true); string newName = editedNDI.HMName; editedCell.Value = newName; txt_Status.AppendText($"Device \"{oldName}\" changed name to: \"{newName}\".{Environment.NewLine}"); if (editedNDI.autoGeneratePnName) { string newPnName = editedNDI.PnDeviceName; editedRow.Cells[dgv_PnDeviceName.Index].Value = newPnName; txt_Status.AppendText($"Device \"{newName}\" changed Profinet device name from: " + $"\"{oldPnName}\" to: \"{newPnName}\".{Environment.NewLine}"); } int deviceKey; deviceTableAutoEdit = true; foreach (DataGridViewRow row in deviceTable.Rows) { if (row.Equals(editedRow)) { continue; // it may be slow, look out } string rowDeviceName = row.Cells[dgv_DeviceName.Index].Value.ToString(); if (rowDeviceName != oldName) { continue; } deviceKey = int.Parse(row.Cells[dgv_Id.Index].Value.ToString()); NetworkDeviceItem modifiedDI = DeviceRowsHelper.DeviceByRowID[deviceKey]; row.Cells[dgv_DeviceName.Index].Value = await modifiedDI.UpdateHMName().ConfigureAwait(true); if (modifiedDI.autoGeneratePnName) { string oldPnNameM = modifiedDI.PnDeviceName; string newPnNameM = modifiedDI.UpdatePnDeviceName(); // update for now, bc structure logic doesn't care about updating device names. // That will change. row.Cells[dgv_PnDeviceName.Index].Value = newPnNameM; txt_Status.AppendText($"Device \"{newName}\" changed Profinet device name from: " + $"\"{oldPnNameM}\" to: \"{newPnNameM}\".{Environment.NewLine}"); } } deviceTableAutoEdit = false; }
private static int CompareByDeviceNameRev(NetworkDeviceItem x, NetworkDeviceItem y) { return(-1 * CompareByDeviceName(x, y)); }