In this article a queue is implemented to allow the device objects to request messages to be send at any time. The messages are enqueued and then send to the Z-Wave controller when possible.
An additional thread is created to decouple the sending and receiving of messages. It will dequeue messages from the SendMessage queue and send them to the Z-Wave controller.
The entire discovery functionality will be moved to the Controller class and a mechanism is provided to allow other objects to subscribe to events concerning updates to the Z-Wave network e.g. added nodes.
class ZWavePort { public delegate void MessageHandler(byte[] sentMessage, byte[] receivedMessage); ... private Thread senderThread ... private PriorityQueue<zwavemessage , ZWaveMessagePriority> queue = new PriorityQueue</zwavemessage><zwavemessage , ZWaveMessagePriority>(); ... } class Controller : ZWaveNode { public event DiscoveryEventHandler DiscoveryEvent; ... private Dictionary<byte , ZWaveNode> devices = new Dictionary</byte><byte , ZWaveNode>(); ... public void EnqueueMessage(ZWaveMessage message) { queue.Enqueue(message, message.Priority); } ... }</byte></zwavemessage>
