Develop a NetLogic to configure network
Prerequisites
Set the default external code editor. See Set the default code editor.
- In theProject viewpane, expand theUIfolder.
- Right-clickMainWindow (type)and select .
- Hover-over the NetLogic, select , and enterNetworkConfigurationor.
- Double-click the NetLogic.The external code editor opens.
- In the code editor, paste the following code:#region Using directives using UAManagedCore; using FTOptix.Core; using OpcUa = UAManagedCore.OpcUa; using FTOptix.HMIProject; using FTOptix.NetLogic; using FTOptix.System; #endregion public class NetworkConfigurator : BaseNetLogic { public override void Start() { // Insert code to be executed when the user-defined logic is started var model = Project.Current.Get("Model"); systemNode = model.Get<FTOptix.System.System>("System1"); if (systemNode == null) throw new CoreConfigurationException("SystemNode not found"); } public override void Stop() { // Insert code to be executed when the user-defined logic is stopped } [ExportMethod] public void Refresh() { systemNode.Refresh(); } [ExportMethod] public void EnableDHCP(string interfaceName, bool enabled) { foreach (var networkInterface in systemNode.NetworkInterfaces) { if (networkInterface.InterfaceName == interfaceName) networkInterface.DhcpEnabled = enabled; } } [ExportMethod] public void AddIpAddressToNetworkInterface(string interfaceName, string ipAddress, string mask) { foreach(var networkInterface in systemNode.NetworkInterfaces) { if (networkInterface.InterfaceName == interfaceName) { networkInterface.DhcpEnabled = false; networkInterface.IPAddress = ipAddress; networkInterface.IPAddressVariable.Mask = mask; } } } [ExportMethod] public void UpdateDNSAddressesToNetworkInterface(string interfaceName, string dns1, string dns2) { foreach (var networkInterface in systemNode.NetworkInterfaces) { if (networkInterface.InterfaceName == interfaceName) { networkInterface.DhcpEnabled = false; networkInterface.DNS1 = dns1; networkInterface.DNS2 = dns2; } } } [ExportMethod] public void UpdateDefaultGatewayToNetworkInterface(string interfaceName, string defaultGateway) { foreach (var networkInterface in systemNode.NetworkInterfaces) { if (networkInterface.InterfaceName == interfaceName) { networkInterface.DhcpEnabled = false; networkInterface.DefaultGateway = defaultGateway; } } } [ExportMethod] public void AddAdditionalIp(string interfaceName, string ipAddress, string mask) { foreach (var networkInterface in systemNode.NetworkInterfaces) { if (networkInterface.InterfaceName == interfaceName) { var additionalIPAddressVariable = InformationModel.MakeVariable<MaskedIPAddress>("MyAdditionalIP", OpcUa::DataTypes.String); additionalIPAddressVariable.SetValue(ipAddress); additionalIPAddressVariable.Mask = mask; networkInterface.AdditionalIPAddresses.Add(additionalIPAddressVariable); } } } private FTOptix.System.System systemNode; }
- Save the code.
The following APIs are available:
public PlaceholderChildNodeCollection<NetworkInterface> NetworkInterfaces { get; } public IUAMethod RefreshMethod { get; } public IUAMethod RebootMethod { get; } public void Reboot(); public void Refresh();
NetworkInterfaces
represents the list of all network interfaces. The structure of a network interface is as follows:
public IUAVariable InterfaceNameVariable { get; } public bool DhcpEnabled { get; set; } public IUAVariable DhcpEnabledVariable { get; } public string IPAddress { get; set; } public MaskedIPAddress IPAddressVariable { get; } public string DNS1 { get; set; } public IUAVariable DNS1Variable { get; } public string DNS2 { get; set; } public IUAVariable DNS2Variable { get; } public string DefaultGateway { get; set; } public IUAVariable DefaultGatewayVariable { get; } public PlaceholderChildNodeCollection<MaskedIPAddress> AdditionalIPAddresses { get; }
- To enable DHCP, see Configure a dynamic network.
- To disable DHCP, see Configure a static network.
- To update the values stored in the System1 object (as a result of an external change), invoke theRefreshmethodsystemNode.Refresh();
- To restart the system, invoke theRestartmethodsystemNode.Reboot();
Provide Feedback