private void ReceiveCallback(IAsyncResult ar) { if (mIsDisposed) { return; } UdpClient client = (UdpClient)((UdpState)(ar.AsyncState)).udpClient; IPEndPoint endPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).endPoint; Byte[] receiveBytes = client.EndReceive(ar, ref endPoint); string receiveString = LifxHelper.ByteArrayToString(receiveBytes); LifxDataPacket package = new LifxDataPacket(receiveBytes); mIncomingQueue.Enqueue(new IncomingMessage(package, endPoint)); client.BeginReceive(new AsyncCallback(ReceiveCallback), (UdpState)(ar.AsyncState)); }
private void ReceiveCallback(IAsyncResult ar) { if (mIsDisposed) return; UdpClient client = (UdpClient)((UdpState)(ar.AsyncState)).udpClient; IPEndPoint endPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).endPoint; Byte[] receiveBytes = client.EndReceive(ar, ref endPoint); string receiveString = LifxHelper.ByteArrayToString(receiveBytes); LifxDataPacket package = new LifxDataPacket(receiveBytes); mIncomingQueue.Enqueue(new IncomingMessage(package, endPoint)); client.BeginReceive(new AsyncCallback(ReceiveCallback), (UdpState)(ar.AsyncState)); }
public IncomingMessage(LifxDataPacket data, IPEndPoint address) { Data = data; BulbAddress = address; }
/// <summary> /// Sends command to a bulb /// </summary> /// <param name="command"></param> /// <param name="bulb">The bulb to send the command to.</param> /// <returns>Returns the response message. If the command does not trigger a response it will reurn null. </returns> public LifxReceivedMessage SendCommand(LifxCommand command, string macAddress, string panController, IPEndPoint endPoint) { if (!IsInitialized) throw new InvalidOperationException("The communicator needs to be initialized before sending a command."); UdpClient client = GetConnectedClient(command, endPoint); LifxDataPacket packet = new LifxDataPacket(command); packet.TargetMac = LifxHelper.StringToByteArray(macAddress); packet.PanControllerMac = LifxHelper.StringToByteArray(panController); client.Send(packet.PacketData, packet.PacketData.Length); DateTime commandSentTime = DateTime.Now; if (command.ReturnMessage == null) return null; while ((DateTime.Now - commandSentTime).TotalMilliseconds < mTimeoutMilliseconds) { if (mIncomingQueue.Count != 0) { IncomingMessage mess = mIncomingQueue.Dequeue(); LifxDataPacket receivedPacket = mess.Data; if (receivedPacket.PacketType == LifxPANGatewayStateMessage.PACKET_TYPE) { //Panhandler identified LifxPANGatewayStateMessage panGateway = new LifxPANGatewayStateMessage(); panGateway.ReceivedData = receivedPacket; AddDiscoveredPanHandler(new LifxPanController( LifxHelper.ByteArrayToString(receivedPacket.TargetMac), mess.BulbAddress)); } else if (receivedPacket.PacketType == LifxLightStatusMessage.PACKET_TYPE && command.IsDiscoveryCommand) { //Panhandler identified LifxLightStatusMessage panGateway = new LifxLightStatusMessage(); panGateway.ReceivedData = receivedPacket; AddDiscoveredBulb( LifxHelper.ByteArrayToString(receivedPacket.TargetMac), LifxHelper.ByteArrayToString(receivedPacket.PanControllerMac)); } else if (receivedPacket.PacketType == command.ReturnMessage.PacketType) { command.ReturnMessage.ReceivedData = receivedPacket; mIncomingQueue.Clear(); return command.ReturnMessage; } } Thread.Sleep(30); } if (command.IsDiscoveryCommand) return null; if (command.RetryCount > 0) { command.RetryCount -= 1; //Recurssion return SendCommand(command, macAddress, panController, endPoint); } else throw new TimeoutException("Did not get a reply from bulb in a timely fashion"); }
/// <summary> /// Sends command to a bulb /// </summary> /// <param name="command"></param> /// <param name="bulb">The bulb to send the command to.</param> /// <returns>Returns the response message. If the command does not trigger a response it will reurn null. </returns> public LifxReceivedMessage SendCommand(LifxCommand command, string macAddress, string panController, IPEndPoint endPoint) { if (!IsInitialized) { throw new InvalidOperationException("The communicator needs to be initialized before sending a command."); } UdpClient client = GetConnectedClient(command, endPoint); LifxDataPacket packet = new LifxDataPacket(command); packet.TargetMac = LifxHelper.StringToByteArray(macAddress); packet.PanControllerMac = LifxHelper.StringToByteArray(panController); client.Send(packet.PacketData, packet.PacketData.Length); DateTime commandSentTime = DateTime.Now; if (command.ReturnMessage == null) { return(null); } while ((DateTime.Now - commandSentTime).TotalMilliseconds < mTimeoutMilliseconds) { if (mIncomingQueue.Count != 0) { IncomingMessage mess = mIncomingQueue.Dequeue(); LifxDataPacket receivedPacket = mess.Data; if (receivedPacket.PacketType == LifxPANGatewayStateMessage.PACKET_TYPE) { //Panhandler identified LifxPANGatewayStateMessage panGateway = new LifxPANGatewayStateMessage(); panGateway.ReceivedData = receivedPacket; AddDiscoveredPanHandler(new LifxPanController( LifxHelper.ByteArrayToString(receivedPacket.TargetMac), mess.BulbAddress)); } else if (receivedPacket.PacketType == LifxLightStatusMessage.PACKET_TYPE && command.IsDiscoveryCommand) { //Panhandler identified LifxLightStatusMessage panGateway = new LifxLightStatusMessage(); panGateway.ReceivedData = receivedPacket; AddDiscoveredBulb( LifxHelper.ByteArrayToString(receivedPacket.TargetMac), LifxHelper.ByteArrayToString(receivedPacket.PanControllerMac)); } else if (receivedPacket.PacketType == command.ReturnMessage.PacketType) { command.ReturnMessage.ReceivedData = receivedPacket; mIncomingQueue.Clear(); return(command.ReturnMessage); } } Thread.Sleep(30); } if (command.IsDiscoveryCommand) { return(null); } if (command.RetryCount > 0) { command.RetryCount -= 1; //Recurssion return(SendCommand(command, macAddress, panController, endPoint)); } else { throw new TimeoutException("Did not get a reply from bulb in a timely fashion"); } }