private Location GetLocation(List <Satellite> satellites) { Point p1 = new Point(satellites[0].Location.X, satellites[0].Location.Y, satellites[0].ReceivedMessage.Distance); Point p2 = new Point(satellites[1].Location.X, satellites[1].Location.Y, satellites[1].ReceivedMessage.Distance); Point p3 = new Point(satellites[2].Location.X, satellites[2].Location.Y, satellites[2].ReceivedMessage.Distance); Point a = Trilateration.Compute(p1, p2, p3); return(new Location(a.X, a.Y)); }
public void TestTrilaterationCompute() { Point p1 = new Point(0, 3, 4); Point p2 = new Point(0, 0, 5); Point p3 = new Point(4, 0, 3); Point expected = new Point(4, 3); Point actual = Trilateration.Compute(p1, p2, p3); Assert.AreEqual(expected.X, actual.X); Assert.AreEqual(expected.Y, actual.Y); }
public Position GetLocation(List <AddSatelliteObject> satelliteList) { foreach (var satellite in satelliteList) { var satInfo = satellitesInformation.FirstOrDefault(x => x.Name == satellite.Name); if (satInfo != null) { pointList.Add(new Point(satInfo.PositionX, satInfo.PositionY, satellite.Distance)); } } var position = Trilateration.Compute(pointList.ElementAt(0), pointList.ElementAt(1), pointList.ElementAt(2)); if (position != null) { return(new Position(position[0], position[1])); } return(null); }
public async Task <Result <ShipResponse> > Handle(Command request, CancellationToken cancellationToken) { List <string> messageItems = new List <string>(); //Iterate for each satellite in order to update data and process the message to decode foreach (var satellite in request.Satellites) { var satelliteFromDb = await context.Satellites.Where(x => x.Name == satellite.Name).FirstOrDefaultAsync(cancellationToken: cancellationToken); if (satelliteFromDb == null) { return(Result <ShipResponse> .Failure(string.Format("Failed to find a satellite named '{0}'", satellite.Name))); } satelliteFromDb.Distance = satellite.Distance; satelliteFromDb.LastUpdatedDate = DateTime.Now; if (satellite.Message != null && satellite.Message.Count > 0) { satelliteFromDb.Message = satellite.Message; if (messageItems.Count == 0) { for (int c = 0; c < satellite.Message.Count; c++) { messageItems.Add(""); } } for (int c = 0; c < satellite.Message.Count; c++) { if (satellite.Message[c] != string.Empty) { var itemFound = messageItems.IndexOf(satellite.Message[c]); if (itemFound == -1) { messageItems[c] = satellite.Message[c]; } } } } } messageItems.RemoveAll(x => string.IsNullOrWhiteSpace(x)); //Save changes await context.SaveChangesAsync(cancellationToken); var satellites = await context.Satellites.ToListAsync(cancellationToken : cancellationToken); //Process the response var shipResponse = new ShipResponse(); //Calculate Ship Position by Trilateration double[] position = Trilateration.Compute(new Point(satellites[0].Y, satellites[0].X, satellites[0].Distance), new Point(satellites[1].Y, satellites[1].X, satellites[1].Distance), new Point(satellites[2].Y, satellites[2].X, satellites[2].Distance)); if (position == null || messageItems.Count == 0) { return(Result <ShipResponse> .Success(null)); } shipResponse.Position = new Position { Y = Math.Round(position[0], 2), X = Math.Round(position[1], 2) }; shipResponse.Message = string.Join(" ", messageItems); return(Result <ShipResponse> .Success(shipResponse)); }