public void Controller_RemoveAllConnections() { TECControllerType type = new TECControllerType(new TECManufacturer()); type.IO.Add(new TECIO(IOType.AI)); type.IO.Add(new TECIO(IOType.AI)); TECController controller = new TECProvidedController(type); TECController childController = new TECProvidedController(type); TECController childestController = new TECProvidedController(type); TECProtocol protocol = new TECProtocol(new List <TECConnectionType> { }); type.IO.Add(new TECIO(protocol)); TECNetworkConnection connection = controller.AddNetworkConnection(protocol); connection.AddChild(childController); TECNetworkConnection childConnection = childController.AddNetworkConnection(protocol); childConnection.AddChild(childestController); childController.DisconnectAll(); Assert.AreEqual(0, childController.ChildrenConnections.Count, "Connection not removed from controller"); Assert.AreEqual(null, childController.ParentConnection, "Connection not removed from child"); Assert.AreEqual(null, childestController.ParentConnection, "Connection not removed from childest"); }
internal static bool addRequiredIOModules(TECProvidedController controller) { //The IO needed by the points connected to the controller IOCollection necessaryIO = new IOCollection(); bool needsSave = false; foreach (TECHardwiredConnection ssConnect in controller.ChildrenConnections.Where(con => con is TECHardwiredConnection)) { foreach (TECIO io in ssConnect.Child.HardwiredIO.ToList()) { for (int i = 0; i < io.Quantity; i++) { //The point IO that exists on our controller at the moment. IOCollection totalPointIO = getPointIO(controller); necessaryIO.Add(io.Type); //Check if our io that exists satisfies the IO that we need. if (!totalPointIO.Contains(necessaryIO)) { needsSave = true; bool moduleFound = false; //If it doesn't, we need to add an IO module that will satisfy it. foreach (TECIOModule module in controller.Type.IOModules) { //We only need to check for the type of the last IO that we added. if (module.IOCollection.Contains(io.Type) && controller.CanAddModule(module)) { controller.AddModule(module); moduleFound = true; break; } } if (!moduleFound) { controller.DisconnectAll(); MessageBox.Show(string.Format("The controller type of the controller '{0}' is incompatible with the connected points. Please review the controller's connections.", controller.Name)); return(true); } } } } } return(needsSave); IOCollection getPointIO(TECController con) { IOCollection pointIOCollection = new IOCollection(); foreach (TECIO pointIO in controller.IO.ToList().Where(io => (TECIO.PointIO.Contains(io.Type) || TECIO.UniversalIO.Contains(io.Type)))) { pointIOCollection.Add(pointIO); } return(pointIOCollection); } }
public void DisconnectAllTest() { TECProtocol firstProtocol = new TECProtocol(new List <TECConnectionType>()); TECProtocol secondProtocol = new TECProtocol(new List <TECConnectionType>()); TECProtocol thirdProtocol = new TECProtocol(new List <TECConnectionType>()); TECProtocol fourthProtocol = new TECProtocol(new List <TECConnectionType>()); TECConnectionType connectionType = new TECConnectionType(); TECDevice compatibleDevice = new TECDevice(new List <TECConnectionType> { connectionType }, new List <TECProtocol>() { secondProtocol, firstProtocol, fourthProtocol }, new TECManufacturer()); TECSubScope subScope = new TECSubScope(); subScope.Devices.Add(compatibleDevice); TECControllerType type = new TECControllerType(new TECManufacturer()); type.IO.Add(new TECIO(firstProtocol)); type.IO.Add(new TECIO(secondProtocol)); type.IO.Add(new TECIO(thirdProtocol)); TECProvidedController controller = new TECProvidedController(type); TECNetworkConnection connection = controller.Connect(subScope, firstProtocol) as TECNetworkConnection; TECDevice compatibleHardDevice = new TECDevice(new List <TECConnectionType> { connectionType }, new List <TECProtocol>(), new TECManufacturer()); TECSubScope hardSubScope = new TECSubScope(); hardSubScope.Devices.Add(compatibleDevice); TECPoint point = new TECPoint(); point.Type = IOType.AI; hardSubScope.Points.Add(point); type.IO.Add(new TECIO(IOType.AI)); TECHardwiredConnection hardConnection = controller.Connect(hardSubScope, hardSubScope.HardwiredProtocol()) as TECHardwiredConnection; TECController parentController = new TECProvidedController(type); parentController.Connect(controller, secondProtocol); Assert.IsNotNull(controller.ParentConnection); controller.DisconnectAll(); Assert.IsFalse(connection.Children.Contains(subScope)); Assert.IsFalse(controller.ChildrenConnections.Contains(connection)); Assert.IsNull((subScope as IConnectable).GetParentConnection()); Assert.IsFalse(controller.ChildrenConnections.Contains(hardConnection)); Assert.IsNull((hardSubScope as IConnectable).GetParentConnection()); Assert.IsNull(controller.ParentConnection); }