/// <summary> /// Adds a LightPoint to the Controller using an ID, and its coordinates, and returns it once added. /// </summary> /// <param name="id">The ID of the LED</param> /// <param name="x">The X position of the LED</param> /// <param name="y">The Y position of the LED</param> /// <param name="z">The Z position of the LED</param> /// <returns>The newly reated LightPoint</returns> public LightPoint addLightPoint(byte id, double x, double y, double z) { LightPoint lightPoint = new LightPoint(id, x, y, z); LightPoints.Add(lightPoint); return(lightPoint); }
/// <summary> /// Loads the Scene from the XML file located at the parameter passed into /// the constructor (sceneFileLocation). An array of Controllers and LightPoints /// are generated for each valid controller. /// </summary> /// <param name="sceneFileLocation">The location of the Scene file to be loaded</param> public void LoadScene(string sceneFileLocation) { try { SceneFile = XDocument.Load(sceneFileLocation); // loads XML document from file location XNamespace ns = SceneFile.Root.GetDefaultNamespace(); // get the namespace being used in the XML document var routers = SceneFile.Descendants(ns + CONTROLLER_ELEMENT); // get all routers Routers = new Router[routers.Count()]; // initialise array of controllers int routerCounter = 0; // find min and madoubleoordinates MinX = routers.Descendants(ns + LIGHT_ELEMENT).Min(e => (double)e.Attribute("x")); MaxX = routers.Descendants(ns + LIGHT_ELEMENT).Max(e => (double)e.Attribute("x")); MinY = routers.Descendants(ns + LIGHT_ELEMENT).Min(e => (double)e.Attribute("y")); MaxY = routers.Descendants(ns + LIGHT_ELEMENT).Max(e => (double)e.Attribute("y")); VideoBufferWidth = (int)MaxX * VIDEO_BUFFER_RESOLUTION + 1; //TODO: Fix the 2x videobuffer resolution VideoBufferHeight = (int)MaxY * VIDEO_BUFFER_RESOLUTION + 1; Console.WriteLine("VBW: {0}, VBH: {1}, maxX: {2}, maxY: {3}", VideoBufferWidth, VideoBufferHeight, MaxX, MaxY); Console.WriteLine("Namespace: {0}", ns); Console.WriteLine("Loading {0} Controllers", routers.Count()); Console.WriteLine("Total number of LightPoints: {0}", routers.Descendants(ns + LIGHT_ELEMENT).Count()); Console.WriteLine("MinX: {0}, MaxX: {1}, MinY: {2}, MaxY: {3}", MinX, MaxX, MinY, MaxY); Console.WriteLine("VidideoBufferWidth: {0}, VideoBufferHeight: {1}", VideoBufferWidth, VideoBufferHeight); // loop through all routers and load their lights foreach (var router in routers) { int controllerId; var lights = router.Descendants(ns + LIGHT_ELEMENT); LightPoint[] lightPoints = new LightPoint[lights.Count()]; Console.WriteLine("Router {0} has a total of {1} LightPoints", routerCounter + 1, lights.Count()); string name = router.Attribute("name")?.Value; string hostname = router.Attribute("hostname")?.Value; int port = Int32.Parse(router.Attribute("port")?.Value); Routers[routerCounter] = new Router(name, hostname, port, UdpClient); // loop through all lightpoints on specific controller and create an instance foreach (var light in lights) { controllerId = SeparateIDBytes(Int32.Parse(light.Attribute("id")?.Value))[1]; byte lightId = SeparateIDBytes(Int32.Parse(light.Attribute("id")?.Value))[0]; double x = Double.Parse(light.Attribute("x")?.Value); double y = Double.Parse(light.Attribute("y")?.Value); double z = Double.Parse(light.Attribute("z")?.Value); // find controller in router //TODO: This could be tidied up if the controllers are always in order? Ask Joe Controller controller = Routers[routerCounter].getControllerByID(controllerId); // try to load the controller by its ID // check if controller exists yet if (controller == null) { controller = Routers[routerCounter].addController(controllerId); // if not, create a new one with the provided ID } // add new LightPopint to controller LightPoint lightPoint = controller.addLightPoint(lightId, x, y, z); lightPoint.CaluclateProjectedCoordinates(MinX, MaxX, MinY, MaxY, this.VideoBufferWidth, this.VideoBufferHeight); // calculate the projected coordinates given the min and max of each } // get max controller count for render loop if (Routers[routerCounter].Controllers.Count > MaxControllerCount) { MaxControllerCount = Routers[routerCounter].Controllers.Count; } routerCounter++; } } catch (FileNotFoundException e) { Console.WriteLine("Scene file not found at {0}", sceneFileLocation); } catch (XmlException e) { Console.WriteLine("Error reading XML in Scene file at {0}", sceneFileLocation); } catch (Exception e) { //TODO: Throw a SceneFileError Exception to end program when an invalid scene was passed Console.WriteLine("Error loading Scene file at {0}", sceneFileLocation); } }