Ejemplo n.º 1
0
        private void OnMessage(InsteonMessage message)
        {
            if (message.Properties.ContainsKey(PropertyKey.FromAddress))
            {
                int address = message.Properties[PropertyKey.FromAddress];
                if (network.Devices.ContainsKey(address))
                {
                    logger.DebugFormat("Device {0} received message {1}", InsteonAddress.Format(address), message.ToString());
                    InsteonDevice device = network.Devices.Find(address);
                    device.OnMessage(message);
                }
                else if (message.MessageType == InsteonMessageType.SetButtonPressed)
                {
                    // don't warn about SetButtonPressed message from unknown devices, because it may be from a device about to be added
                }
                else if (network.AutoAdd)
                {
                    logger.DebugFormat("Unknown device {0} received message {1}, adding device", InsteonAddress.Format(address), message.ToString());

                    //note: due to how messages are handled and how devices cannot receive new messages while pending sends (I think) we should only add on certain message types.
                    // right now I've only tested devices where we get broadcast messages. Thus, we wait until the last message received.
                    if (message.MessageType == InsteonMessageType.SuccessBroadcast)
                    {
                        InsteonIdentity?id;

                        // TODO: probably shouldn't be in a while loop. Need a better way to address this
                        while (!network.Controller.TryGetLinkIdentity(new InsteonAddress(address), out id))
                        {
                            if (id != null)
                            {
                                InsteonDevice device = network.Devices.Add(new InsteonAddress(address), id.Value);
                                device.OnMessage(message);
                            }
                        }
                    }
                }
                else
                {
                    logger.WarnFormat("Unknown device {0} received message {1}. Could be Identification process.", InsteonAddress.Format(address), message.ToString());
                }
            }
            else
            {
                logger.DebugFormat("Controller received message {0}", message.ToString());
                network.Controller.OnMessage(message);
            }
        }