/// <summary> /// Sends the node. /// </summary> /// <param name="node">The node.</param> /// <param name="forceSend">if set to <c>true</c> force sending the node, even if it's within the threshold for suppressing duplicate sending.</param> public void SendNode(Node node, bool forceSend = false) { // Avoid sending very small changes in node position if (!forceSend && Math.Abs(this.lastXSent - node.X) < 5 && Math.Abs(this.lastYSent - node.Y) < 5) { return; } this.lastXSent = node.X; this.lastYSent = node.Y; this.Send(node.Serialize()); }
/// <summary> /// Creates the web connection. /// </summary> private void CreateWebConnection() { this.hubConnection = new HubConnection(WebAddress); this.signalRHub = this.hubConnection.CreateProxy("node"); this.hubConnection.Start().ContinueWith(task => { if (task.IsFaulted) { Debug.WriteLine("task"); } else { this.webConnectionMade = true; } }); this.signalRHub.On<string>( "received", message => { Debug.WriteLine("RECD (web): " + message); var node = Node.DeSerialize(message); if (message == WhereIsEveryBodyMessage) { Debug.WriteLine("WEIB Received (web)"); node = new Node(WhereIsEveryBodyMessage, 0, 0); } // Don't notify about received message that we sent if (node != null && this.NotificationReceived != null && node.Id != this.selfNodeId) { this.NotificationReceived(node); } }); }
/// <summary> /// Handles a node change message being received. /// </summary> /// <param name="node">The node.</param> private void NodeChangeMessageReceived(Node node) { if (node.Id == CommManager.WhereIsEveryBodyMessage) { this.ForceSendingSelfNode(); return; } var existingNode = this.Nodes.FirstOrDefault(n => n.Id == node.Id); if (existingNode == null) { node.LastUpdated = DateTime.UtcNow; this.Nodes.Add(node); } else { var idx = this.Nodes.IndexOf(existingNode); this.Nodes[idx].LastUpdated = DateTime.UtcNow; this.Nodes[idx].X = node.X; this.Nodes[idx].Y = node.Y; this.Nodes[idx].Tag = node.Tag; } if (this.OnNodeChanged != null) { this.OnNodeChanged(node.Id); } }
/// <summary> /// Adds the self node. /// </summary> /// <param name="xPosition">The x position.</param> /// <param name="yPosition">The y position.</param> public void AddSelfNode(double xPosition, double yPosition) { if (!string.IsNullOrWhiteSpace(this.selfNodeId)) { throw new InvalidOperationException("Self node already added"); } var deviceId = GetSimplifiedDeviceId(); var node = new Node(deviceId, xPosition, yPosition) { NodeType = TypeOfNode.Self, LastUpdated = DateTime.UtcNow }; this.Nodes.Add(node); this.selfNodeId = node.Id; CommManager.Instance.SetOwnNodeId(node.Id); CommManager.Instance.SendNode(node); }
/// <summary> /// Adds the default node. /// </summary> /// <param name="xPosition">The x position.</param> /// <param name="yPosition">The y position.</param> public void AddDefaultNode(double xPosition, double yPosition) { var node = new Node(Guid.NewGuid().ToString(), xPosition, yPosition) { NodeType = TypeOfNode.Default, LastUpdated = DateTime.UtcNow }; this.Nodes.Add(node); }
/// <summary> /// Tell java script on page about a node. /// </summary> /// <param name="node">The node.</param> private void TellJsAboutNode(Node node) { this.embeddedBrowser.InvokeScript( "UpdateNode", node.Id, node.NodeType.ToString(), node.X.ToString(CultureInfo.InvariantCulture), node.Y.ToString(CultureInfo.InvariantCulture)); }
/// <summary> /// Creates the default nodes. /// </summary> /// <param name="numberOfNodes">The number of nodes.</param> private void CreateDefaultNodes(int numberOfNodes) { if (numberOfNodes > 0) { this.defaultNodes = new List<Node>(numberOfNodes); for (int i = 0; i < numberOfNodes; i++) { var node = new Node( Guid.NewGuid().ToString(), this.rand.Next(50, 430), this.rand.Next(50, 750)); node.NodeType = TypeOfNode.Default; this.defaultNodes.Add(node); this.TellJsAboutNode(node); } var simulateMovementOfDefaultNodesTimer = new DispatcherTimer(); simulateMovementOfDefaultNodesTimer.Tick += (sender, args) => { if (this.rand.Next(1, 5) == 1) { var newX = this.rand.Next(50, 430); var newY = this.rand.Next(50, 750); var index = this.rand.Next(0, this.defaultNodes.Count); this.defaultNodes[index].X = newX; this.defaultNodes[index].Y = newY; this.TellJsAboutNode(this.defaultNodes[index]); } }; simulateMovementOfDefaultNodesTimer.Interval = TimeSpan.FromMilliseconds(300); simulateMovementOfDefaultNodesTimer.Start(); } }