/// <summary> /// Sends the specified direct message and waits for a response. /// </summary> /// <param name="packet">Packet to send</param> /// <param name="response">The response from the device</param> /// <returns>the result of the call</returns> public PowerLineModemMessage.MessageResponse SendDirectInsteonPacket(InsteonPacket packet, out InsteonPacket response) { // Only one packet at a time. lock (this) { if (packet.Flags.MessageType != InsteonPacket.InsteonFlags.MessageTypeEnum.Direct) { response = null; return(PowerLineModemMessage.MessageResponse.Invalid); } // Send only one direct packet at a time. PowerLineModemMessage.MessageResponse messageResponse; StandardMessage message = new StandardMessage(PowerLineModemMessage.Message.SendInsteonMessage, packet); this.sendingPacket = packet; messageResponse = port.SendCommand(message); if (messageResponse == PowerLineModemMessage.MessageResponse.Ack) { // We should get an ack back for this message. if (this.linkingEvent.WaitOne(5000)) { // See if we got back an ack or nack. if (this.receivedPacket.Flags.MessageType == InsteonPacket.InsteonFlags.MessageTypeEnum.NackDirect) { messageResponse = PowerLineModemMessage.MessageResponse.Nack; } response = this.receivedPacket; } else { response = null; return(PowerLineModemMessage.MessageResponse.Unknown); } } else { response = null; } return(messageResponse); } }
/// <summary> /// Handle an insteon packet from somewhere else. /// </summary> /// <param name="packet">the incoming packet</param> /// <returns>true if something changed</returns> internal virtual StateChanged handleInsteonPacket(InsteonPacket packet) { return(null); }
/// <summary> /// Sends an extended command off to do stuff. /// </summary> /// <param name="cmd">The command to send</param> /// <param name="command2">The second part of the command to update</param> /// <returns></returns> protected Messages.PowerLineModemMessage.MessageResponse SendExtendedCommandWithResponse(InsteonPacket.ExtendedCommand cmd, byte command2, byte[] data, out InsteonPacket response) { InsteonPacket packet = new InsteonPacket(this.deviceId, cmd, command2, data); return(coms.SendDirectInsteonPacket(packet, out response)); }
/// <summary> /// Creates a new standard message from the specified data. /// </summary> /// <param name="message">The message id to create</param> /// <param name="data">The data to create it with</param> public StandardMessage(Message message, byte[] data) : base(message) { Packet = new InsteonPacket(data); }
/// <summary> /// Create a new standard message from the insteon input packet. /// </summary> /// <param name="message">The message id to create</param> /// <param name="packet">the insteon packet to create it with</param> public StandardMessage(Message message, InsteonPacket packet) : base(message) { Packet = packet; }
/// <summary> /// Called when a message comes in from the modem. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void port_ReceivedMessage(object sender, RecievedMessageEventArgs args) { switch (args.Message.MessageType) { case PowerLineModemMessage.Message.AllLinkRecordResponse: // If we get this, then query the device for more details. AllLinkRecordResponse response = (AllLinkRecordResponse)args.Message; this.linkedDevices[response.Record.Address] = response.Record; this.linkingEvent.Set(); break; case PowerLineModemMessage.Message.StandardMessageReceived: StandardMessage standardMessage = (StandardMessage)args.Message; if (standardMessage.Packet.FromAddress.Equals(this.sendingPacket.FromAddress)) { if (standardMessage.Packet.Flags.MessageType == InsteonPacket.InsteonFlags.MessageTypeEnum.AckDirect || standardMessage.Packet.Flags.MessageType == InsteonPacket.InsteonFlags.MessageTypeEnum.NackDirect) { this.receivedPacket = standardMessage.Packet; this.linkingEvent.Set(); break; } } // Find the device and send the packet there. if (this.devices.ContainsKey(standardMessage.Packet.FromAddress)) { DeviceBase device = this.devices[standardMessage.Packet.FromAddress]; StateChanged changed = device.handleInsteonPacket(standardMessage.Packet); if (changed != null) { if (DeviceChanged != null) { DeviceChanged(this, new DeviceChangedEventArgs(changed)); } } } else { // If this is a broadcast set button pressed, add into the device list. if (standardMessage.Packet.Flags.MessageType == InsteonPacket.InsteonFlags.MessageTypeEnum.Broadcast && standardMessage.Packet.Command1 == (byte)InsteonPacket.BroadcastCommand.SetButtonPressed) { byte category = standardMessage.Packet.ToAddress.Address[0]; byte subCategory = standardMessage.Packet.ToAddress.Address[1]; AddDevice(standardMessage.Packet.FromAddress, category, subCategory); } } break; case PowerLineModemMessage.Message.ExtendedMessageReceived: ExtendedMessage extendedMessage = (ExtendedMessage)args.Message; if (extendedMessage.Packet.FromAddress.Equals(this.sendingPacket.FromAddress)) { if (extendedMessage.Packet.Flags.MessageType == InsteonPacket.InsteonFlags.MessageTypeEnum.AckDirect || extendedMessage.Packet.Flags.MessageType == InsteonPacket.InsteonFlags.MessageTypeEnum.NackDirect) { this.receivedPacket = extendedMessage.Packet; this.linkingEvent.Set(); break; } } // Find the device and send the packet there. if (this.devices.ContainsKey(extendedMessage.Packet.FromAddress)) { DeviceBase device = this.devices[extendedMessage.Packet.FromAddress]; StateChanged changed = device.handleInsteonPacket(extendedMessage.Packet); if (changed != null) { if (DeviceChanged != null) { DeviceChanged(this, new DeviceChangedEventArgs(changed)); } } } break; } }
/// <summary> /// Handle an insteon packet from somewhere else. /// </summary> /// <param name="packet">the incoming packet</param> /// <returns>true if something changed</returns> internal virtual bool handleInsteonPacket(InsteonPacket packet) { return(false); }