コード例 #1
0
        private void serialManager_NodeConnectionAdded(object sender, NodeConnection e)
        {
            if (e.NodeAddress == MASTER_ADDRESS)
            {
                masterConnection = e;
                StoreConnection(e);
            }

            if (NodeConnectionDetected != null)
            {
                NodeConnectionDetected(this, e.NodeAddress);
            }

            Debug.WriteLine(string.Format("DONGLE NODE 0x{0:X4} ({1}) Connected!", e.NodeAddress, e.NodeAddress == MASTER_ADDRESS ? "Master" : "Slave"));
        }
コード例 #2
0
 private Filter.OriginTypes GetOriginType(InputHeader message, NodeConnection connection)
 {
     if (connection.NodeAddress == message.Content.SourceAddress)
     {
         return(Filter.OriginTypes.FromNode);
     }
     else if (connection.NodeAddress == MASTER_ADDRESS)
     {
         return(Filter.OriginTypes.FromMaster);
     }
     else
     {
         return(Filter.OriginTypes.Any);
     }
 }
コード例 #3
0
        void newNode_ConnectionStateChanged(object sender, NodeConnection.ConnectionStates e)
        {
            NodeConnection node = ((NodeConnection)sender);

            if (node.ConnectionState != NodeConnection.ConnectionStates.Connected)
            {
                return;
            }

            node.ConnectionStateChanged -= newNode_ConnectionStateChanged;

            unidentifiedNodes.Remove(node);
            validNodes.Add(node);

            OnNodeConnectionDetected(node);
        }
コード例 #4
0
        private void RemoveConnection(NodeConnection connection)
        {
            ushort connectionAddress = connection.NodeAddress;

            if (masterConnection == connection)
            {
                masterConnection = null;
            }

            if (this.connectionsInUse.ContainsKey(connectionAddress))
            {
                this.connectionsInUse.Remove(connectionAddress);
                this.pendingMessagesQueue.Remove(connectionAddress);
                this.currentMessages.Remove(connectionAddress);

                connection.MessageReceived -= connection_OperationReceived;
            }
        }
コード例 #5
0
        private Task <bool> EnqueueMessage(OutputHeader message, NodeConnection connection)
        {
            ushort connectionAddress = connection.NodeAddress;

            TaskCompletionSource <bool> result = new TaskCompletionSource <bool>();

            lock (thisLock)
            {
                var            currentQueue = this.pendingMessagesQueue[connectionAddress];
                PendingMessage newMsg       = new PendingMessage(result, message);
                int            index        = 0;
                foreach (PendingMessage pendingMsg in currentQueue)
                {
                    if (newMsg.Message.Priority > pendingMsg.Message.Priority)
                    {
                        currentQueue.Insert(index, newMsg);
                        break;
                    }
                    index++;
                }
                if (index == currentQueue.Count)
                {
                    currentQueue.Add(newMsg);
                }

                // If the new message is alone in the queue
                if (currentQueue.Count == 1)
                {
                    // Connection is bussy. We need to wait
                    if (!this.CheckForSend(connectionAddress))
                    {
                        Debug.Print(newMsg.Id + " waiting (1)...");
                    }
                }
                else
                {
                    Debug.Print(newMsg.Id + " waiting (" + (index + 1) + ")...");
                }
            }

            return(result.Task);
        }
コード例 #6
0
        private async void connection_OperationReceived(object sender, InputHeader e)
        {
            NodeConnection connection = (NodeConnection)sender;

            List <Task> taskList = new List <Task>();

            //Send Message to the placeHolders
            foreach (var module in this.modulesList.Where(m => m.Filter.CheckMessage(e, GetOriginType(e, connection))))
            {
                var moduleTask = new Task(() =>
                {
                    module.ProcessReceivedMessage(e.Content);
                });
                moduleTask.Start();

                taskList.Add(moduleTask);
            }

            await Task.WhenAll(taskList);
        }
コード例 #7
0
        void newNode_ConnectionStateChanged(object sender, NodeConnection.ConnectionStates e)
        {
            NodeConnection node = ((NodeConnection)sender);

            if (node.ConnectionState != NodeConnection.ConnectionStates.Connected)
                return;

            node.ConnectionStateChanged -= newNode_ConnectionStateChanged;

            unidentifiedNodes.Remove(node);
            validNodes.Add(node);

            OnNodeConnectionDetected(node);
        }
コード例 #8
0
        private void checkTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var portNames = SerialPort.GetPortNames();

            //Remove the nodes that are not phisically connected
            for (int i = unidentifiedNodes.Count - 1; i >= 0; i--)
            {
                if (!portNames.Contains(unidentifiedNodes[i].PortName))
                {
                    unidentifiedNodes.RemoveAt(i);
                }
            }

            for (int i = validNodes.Count - 1; i >= 0; i--)
            {
                if (!portNames.Contains(validNodes[i].PortName))
                {
                    OnNodeConnectionRemoved(validNodes[i]);

                    validNodes.RemoveAt(i);
                }
            }

            //Add the new nodes to the unidentified nodes list, until it was successfully identified
            foreach (string portName in portNames.Where(p => !unidentifiedNodes.Union(validNodes).Any(n => n.PortName == p)))
            {
                var newNode = new NodeConnection(portName);
                newNode.ConnectionStateChanged += newNode_ConnectionStateChanged;
                newNode.Identify();
                unidentifiedNodes.Add(newNode);
            }

            checkTimer.Start();
        }
コード例 #9
0
 private void OnNodeConnectionRemoved(NodeConnection node)
 {
     if (NodeConnectionRemoved != null)
         NodeConnectionRemoved(this, node);
 }
コード例 #10
0
 private void OnNodeConnectionDetected(NodeConnection node)
 {
     if (NodeConnectionDetected != null)
         NodeConnectionDetected(this, node);
 }