Ejemplo n.º 1
0
        void GetNearestDeviceInRange(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];
            double range            = Double.Parse(request.Parameters["range"]);

            // Find the associated device in the Current Devices
            Device observer = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));

            if (observer == null)
            {
                response.StatusCode = 404;
                return;
            }

            Device nearestDevice = locator.GetNearestDeviceWithinRange(observer, range);

            if (nearestDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            IntermediateDevice intermediateDevice = PairableDevice.GetCompleteIntermediateDevice(nearestDevice);

            if (intermediateDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            // Respond with the device
            response.SetBodyWith(intermediateDevice);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handle a request for information about a device
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        void GetDevice(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];

            // Find the associated device in the Current Devices
            Device device = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));

            if (device == null)
            {
                response.StatusCode = 404;
                return;
            }

            // Get the intermediateDevice for serialization
            IntermediateDevice intermediateDevice = PairableDevice.GetCompleteIntermediateDevice(device);

            if (intermediateDevice == null)
            {
                //TODO: Should this status code be different, to reflect that the device exists but couldn't be returned due to incompleteness?
                response.StatusCode = 404;
                return;
            }

            // Respond with the device
            response.SetBodyWith(intermediateDevice);
        }
Ejemplo n.º 3
0
        void GetNearestDeviceInView(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];

            // Find the associated device in the Current Devices
            Device observer = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));

            if (observer == null)
            {
                //TODO: Should we use distinct status codes for distinct failure types here?
                response.StatusCode = 404;
                return;
            }

            // Find the nearest device that we observe
            Device nearestDevice = locator.GetNearestDeviceInView(observer);

            if (nearestDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            // Prepare the device for serialization
            IntermediateDevice intermediateDevice = PairableDevice.GetCompleteIntermediateDevice(nearestDevice);

            if (intermediateDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            response.SetBodyWith(intermediateDevice);
        }
        public void GetDeviceInfoTest()
        {
            Setup();

            PairableDevice deviceOne = new PairableDevice
            {
                Location    = new Point(1, 0),
                Orientation = 90,
                Identifier  = "deviceOne",
            };

            PairableDevice deviceTwo = new PairableDevice
            {
                Location   = new Point(-1, 0),
                Identifier = "deviceTwo",
            };

            Server.Locator.Devices.Add(deviceOne);
            Server.Locator.Devices.Add(deviceTwo);


            Server.Start();
            Client.Start();
            WaitForConnections();

            // Successful get device info test
            IARequest request = new IARequest(Routes.GetDeviceInfoRoute);

            request.Parameters["identifier"] = deviceOne.Identifier;

            Client.SendRequest(request, Server.IntAirAct.OwnDevice, delegate(IAResponse response, Exception e)
            {
                IntermediateDevice id = response.BodyAs <IntermediateDevice>();

                Assert.AreEqual("deviceOne", id.identifier);
                Assert.AreEqual(new Point(1, 0), id.location.Value);
                Assert.AreEqual(90.0, id.orientation.Value, 0.01);

                doneWaitingForResponse = true;
            });

            WaitForResponse();
            doneWaitingForResponse = false;

            // Unsuccessful get device info test
            // Device two is incomplete, missing orientation, the correct server behaviour is to return http status 404
            request = new IARequest(Routes.GetDeviceInfoRoute);
            request.Parameters["identifier"] = deviceTwo.Identifier;

            Client.SendRequest(request, Server.IntAirAct.OwnDevice, delegate(IAResponse response, Exception e)
            {
                Assert.AreEqual(404, response.StatusCode);
                doneWaitingForResponse = true;
            });

            WaitForResponse();

            Teardown();
        }
        public void AllDevicesInViewTest()
        {
            Setup();
            PairableDevice observer = new PairableDevice
            {
                Location    = new Point(0, 0),
                Orientation = 225,
                Identifier  = "observer",
            };

            PairableDevice nearest = new PairableDevice
            {
                Location    = new Point(-1, -1),
                Identifier  = "nearest",
                Orientation = 20,
            };

            PairableDevice furthest = new PairableDevice
            {
                Location    = new Point(-2, -2),
                Identifier  = "furthest",
                Orientation = 20,
            };

            Server.Locator.Devices.Add(observer);
            Server.Locator.Devices.Add(furthest);
            Server.Locator.Devices.Add(nearest);

            Server.Start();
            Client.Start();
            WaitForConnections();

            IARequest request = new IARequest(Routes.GetAllDevicesInViewRoute);

            request.Parameters["identifier"] = observer.Identifier;

            Client.SendRequest(request, Server.IntAirAct.OwnDevice, delegate(IAResponse response, Exception e)
            {
                List <IntermediateDevice> intDevices = response.BodyAs <IntermediateDevice>();

                IntermediateDevice intDevice = intDevices.Find(x => x.identifier == nearest.Identifier);
                Assert.AreEqual(nearest.Location, intDevice.location);
                Assert.AreEqual(nearest.Orientation, intDevice.orientation);

                intDevice = intDevices.Find(x => x.identifier == furthest.Identifier);
                Assert.AreEqual(furthest.Location, intDevice.location);
                Assert.AreEqual(furthest.Orientation, intDevice.orientation);

                doneWaitingForResponse = true;
            });

            WaitForResponse();
            Teardown();
        }
        public void NearestDeviceInRangeTest()
        {
            Setup();

            PairableDevice observer = new PairableDevice
            {
                Location    = new Point(10, 10),
                Orientation = 0,
                Identifier  = "observer",
            };

            PairableDevice nearest = new PairableDevice
            {
                Location    = new Point(0, 5),
                Identifier  = "nearest",
                Orientation = 0,
            };

            PairableDevice furthest = new PairableDevice
            {
                Location    = new Point(-5, 0),
                Identifier  = "furthest",
                Orientation = 0,
            };

            Server.Locator.Devices.Add(observer);
            Server.Locator.Devices.Add(furthest);
            Server.Locator.Devices.Add(nearest);

            Server.Start();
            Client.Start();
            WaitForConnections();

            IARequest request = new IARequest(Routes.GetNearestDeviceInRangeRoute);

            request.Parameters["identifier"] = observer.Identifier;
            request.Parameters["range"]      = "100.0";

            Client.SendRequest(request, Server.IntAirAct.OwnDevice, delegate(IAResponse response, Exception e)
            {
                IntermediateDevice intDevice = response.BodyAs <IntermediateDevice>();
                Assert.AreEqual(nearest.Identifier, intDevice.identifier);
                Assert.AreEqual(nearest.Location, intDevice.location);
                Assert.AreEqual(nearest.Orientation, intDevice.orientation);

                doneWaitingForResponse = true;
            });

            WaitForResponse();
            Teardown();
        }
        public void GetAllDeviceInfoTest()
        {
            Setup();

            PairableDevice deviceOne = new PairableDevice
            {
                Location    = new Point(1, 0),
                Orientation = 90,
                Identifier  = "deviceOne",
            };

            PairableDevice deviceTwo = new PairableDevice
            {
                Location    = new Point(-1, 0),
                Identifier  = "deviceTwo",
                Orientation = 20,
            };

            Server.Locator.Devices.Add(deviceOne);
            Server.Locator.Devices.Add(deviceTwo);

            Server.Start();
            Client.Start();
            WaitForConnections();

            IARequest request = new IARequest(Routes.GetAllDeviceInfoRoute);

            Client.SendRequest(request, Server.IntAirAct.OwnDevice, delegate(IAResponse response, Exception e)
            {
                List <IntermediateDevice> ids = response.BodyAs <IntermediateDevice>();


                IntermediateDevice intDevice = ids.Find(x => x.identifier.Equals("deviceOne"));
                Assert.AreEqual(deviceOne.Location, intDevice.location);
                Assert.AreEqual(deviceOne.Orientation, intDevice.orientation.Value);
                intDevice = ids.Find(x => x.identifier.Equals("deviceTwo"));
                Assert.AreEqual(deviceTwo.Location, intDevice.location);
                Assert.AreEqual(deviceTwo.Orientation, intDevice.orientation.Value);


                doneWaitingForResponse = true;
            });

            WaitForResponse();

            Teardown();
        }
Ejemplo n.º 8
0
        // For transmission, we create objects with an anonymous type where the instance variable names precisely match the ones on iOS.
        // ie, identifier instead of Identifier
        // This makes deserialization on the client easier.
        public static List <IntermediateDevice> GetCompleteIntermediateDevicesList(List <Device> devices)
        {
            List <IntermediateDevice> intermediateDevices = new List <IntermediateDevice>();

            foreach (Device device in devices)
            {
                IntermediateDevice intermediateDevice = new IntermediateDevice
                {
                    orientation       = device.Orientation,
                    identifier        = device.Identifier,
                    location          = device.Location,
                    intersectionPoint = new Point(device.intersectionPoint["x"], device.intersectionPoint["y"])
                };

                if (intermediateDevice.isComplete)
                {
                    intermediateDevices.Add(intermediateDevice);
                }
            }

            return(intermediateDevices);
        }
Ejemplo n.º 9
0
        public static IntermediateDevice GetCompleteIntermediateDevice(Device device)
        {
            if (device == null)
            {
                return(null);
            }
            IntermediateDevice intermediateDevice = new IntermediateDevice();

            intermediateDevice.identifier  = device.Identifier;
            intermediateDevice.orientation = device.Orientation;
            intermediateDevice.location    = device.Location;

            // We only want to return devices for which all of the properties are known
            if (intermediateDevice.isComplete)
            {
                return(intermediateDevice);
            }
            else
            {
                return(null);
            }
        }
        public void AllDevicesWithinRangeTest()
        {
            Setup();

            PairableDevice observer = new PairableDevice
            {
                Location    = new Point(10, 10),
                Orientation = 0,
                Identifier  = "observer",
            };

            PairableDevice nearest = new PairableDevice
            {
                Location    = new Point(0, 5),
                Identifier  = "nearest",
                Orientation = 0,
            };

            PairableDevice furthest = new PairableDevice
            {
                Location    = new Point(-5, 0),
                Identifier  = "furthest",
                Orientation = 0,
            };

            PairableDevice tooFar = new PairableDevice
            {
                Location    = new Point(100, 100),
                Identifier  = "tooFar",
                Orientation = 50,
            };

            Server.Locator.Devices.Add(observer);
            Server.Locator.Devices.Add(furthest);
            Server.Locator.Devices.Add(nearest);
            Server.Locator.Devices.Add(tooFar);

            Server.Start();
            Client.Start();
            WaitForConnections();

            IARequest request = new IARequest(Routes.GetAllDevicesInRangeRoute);

            request.Parameters["identifier"] = observer.Identifier;
            request.Parameters["range"]      = "50.0";


            Client.SendRequest(request, Server.IntAirAct.OwnDevice, delegate(IAResponse response, Exception e)
            {
                List <IntermediateDevice> intDevices = response.BodyAs <IntermediateDevice>();

                IntermediateDevice intDevice = intDevices.Find(x => x.identifier == nearest.Identifier);
                Assert.AreEqual(nearest.Location, intDevice.location);
                Assert.AreEqual(nearest.Orientation, intDevice.orientation);

                intDevice = intDevices.Find(x => x.identifier == furthest.Identifier);
                Assert.AreEqual(furthest.Location, intDevice.location);
                Assert.AreEqual(furthest.Orientation, intDevice.orientation);

                // The 'tooFar' device should not be returned
                Assert.AreEqual(2, intDevices.Count);

                doneWaitingForResponse = true;
            });

            WaitForResponse();
            Teardown();
        }