An object describing a Wiz light on the network.
Beispiel #1
0
 /// <summary>
 /// Sends data asynchronously to a connected OpenWiz.WizSocket.
 /// </summary>
 /// <param name="s">The data to send.</param>
 /// <param name="handle">The handle to the remote light.</param>
 /// <param name="callback">The System.AsyncCallback delegate to fall after the send completes.</param>
 /// <param name="state">An object that will be passed into the callback.</param>
 /// <exception cref="System.ArgumentNullException">
 /// s is null.
 /// </exception>
 /// <exception cref="System.Net.Sockets.SocketException">
 /// An error occurred when attempting to access the underlying socket.
 /// </exception>
 /// <exception cref="System.ObjectDisposedException">
 /// The underlying socket has been closed.
 /// </exception>
 /// <returns>An System.IAsyncResult that references the asynchronous send.</returns>
 ///
 public IAsyncResult BeginSend(WizState s, WizHandle handle, AsyncCallback callback, object state)
 {
     byte[] buffer = s.ToUTF8();
     return(socket.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None,
                               new IPEndPoint(handle.Ip, PORT_DISCOVER),
                               callback, state));
 }
Beispiel #2
0
        /// <summary>
        /// Ends a pending asynchronous recieve.
        /// </summary>
        /// <param name="asyncResult">An System.IAsyncResult that stores state information
        /// for this asynchronous operation.</param>
        /// <exception cref="System.ArgumentException">
        /// asyncResult was not returned by a call to OpenWiz.WizSocket.BeginRecieve(System.AsyncCallback,System.Object)
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// OpenWiz.WizSocket.EndReceive(System.IAsyncResult) was previously called
        /// for the asynchronous receive.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        /// asyncResult or handle is null.
        /// </exception>
        /// <exception cref="System.Net.Sockets.SocketException">
        /// An error occurred when attempting to access the underlying socket.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The underlying socket has been closed.
        /// </exception>
        /// <returns>If successful, the OpenWiz.WizState;
        /// otherwise, an invalid System.Net.Sockets.Socket error.</returns>
        ///
        public WizState EndReceiveFrom(WizHandle handle, IAsyncResult result)
        {
            EndPoint ep   = new IPEndPoint(handle.Ip, 0);
            int      rLen = socket.EndReceiveFrom(result, ref ep);

            byte[] buffer = bufferMap[handle.Ip.ToString()];
            bufferMap.Remove(handle.Ip.ToString());

            return(WizState.Parse(new ArraySegment <byte>(buffer, 0, rLen)));
        }
Beispiel #3
0
        /// <summary>
        /// Recieves data asynchronously from a connected OpenWiz.WizSocket.
        /// </summary>
        /// <param name="handle">The handle to the remote light.</param>
        /// <param name="callback">The System.AsyncCallback delegate to fall after the receive completes.</param>
        /// <param name="state">An object that will be passed into the callback.</param>
        /// <exception cref="System.Net.Sockets.SocketException">
        /// An error occurred when attempting to access the underlying socket.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The underlying socket has been closed.
        /// </exception>
        /// <returns>An System.IAsyncResult that references the asynchronous receive.</returns>
        ///
        public IAsyncResult BeginRecieveFrom(WizHandle handle, AsyncCallback callback, object state)
        {
            byte[] buffer = new byte[BUFFER_SIZE];
            bufferMap.Add(handle.Ip.ToString(), buffer);

            EndPoint ep = new IPEndPoint(handle.Ip, 0);

            return(socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None,
                                           ref ep, callback, state));
        }
Beispiel #4
0
        /// <summary>
        /// Receives data from a bound OpenWiz.WizSocket.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">
        /// handle is null.
        /// </exception>
        /// <exception cref="System.Net.Sockets.SocketException">
        /// An error occurred when attempting to access the underlying socket.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The underlying socket has been disposed.
        /// </exception>
        /// <exception cref="System.Security.SecurityException">
        /// A caller in the call stack does not have the required permissions.
        /// </exception>
        /// <returns>The remote state.</returns>
        ///
        public WizState ReceiveFrom(WizHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException("handle cannot be null");
            }
            byte[] buffer = new byte[BUFFER_SIZE];

            EndPoint ep   = new IPEndPoint(handle.Ip, 0);
            int      rLen = socket.ReceiveFrom(buffer, ref ep);

            return(WizState.Parse(new ArraySegment <byte>(buffer, 0, rLen)));
        }
Beispiel #5
0
        /// <summary>
        /// Sends data to a connected OpenWiz.WizSocket.
        /// </summary>
        /// <param name="s">A OpenWiz.WizState containing the data to be sent.</param>
        /// <param name="handle">The handle to the remote light.</param>
        /// <exception cref="System.ArgumentNullException">
        /// s or handle are null.
        /// </exception>
        /// <exception cref="System.Net.Sockets.SocketException">
        /// An error occurred when attempting to access the underlying socket.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The underlying socket has been disposed.
        /// </exception>
        /// <returns>the number of bytes sent</returns>
        ///
        public int SendTo(WizState s, WizHandle handle)
        {
            if (s == null)
            {
                throw new ArgumentNullException("s cannot be null");
            }
            if (handle == null)
            {
                throw new ArgumentNullException("handle cannot be null");
            }

            byte[] bytes = s.ToUTF8();
            return(socket.SendTo(bytes, new IPEndPoint(handle.Ip, PORT_DISCOVER)));
        }
        /// Responsible for handling responses from Wiz lights
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (!KeepAlive)
            {
                return;
            }

            IPEndPoint ep = new IPEndPoint(IPAddress.Any, PORT_DISCOVERY);

            byte[]   data       = discoveryClient.EndReceive(ar, ref ep);
            string   jsonString = Encoding.UTF8.GetString(data);
            WizState wState     = WizState.Parse(data);

            Action <WizHandle> discoveryAction = (Action <WizHandle>)ar.AsyncState;

            if (wState == null)
            {
                Debug.WriteLine($"[WARNING] WizDiscoveryService@{hostIp}: Got bad json message:");
                Debug.WriteLine($"\t{jsonString}");
            }
            else if (wState.Error != null)
            {
                Debug.Write($"[WARNING] WizDiscoveryService@{hostIp}: Encountered ");

                if (wState.Error.Code == null)
                {
                    Debug.Write("unknwon error");
                }
                else
                {
                    Debug.Write($"error {wState.Error.Code}");
                }
                if (wState.Error.Message != null)
                {
                    Debug.Write($" -- {wState.Error.Message}");
                }
                Debug.WriteLine($" from {ep.Address}");
                Debug.WriteLine($"\t{jsonString}");
            }
            else if (wState.Result != null)
            {
                Debug.WriteLine($"[INFO] WizDiscoveryService@{hostIp}: Got response:");
                Debug.WriteLine($"\t{jsonString}");
                WizHandle handle = new WizHandle(wState.Result.Mac, ep.Address);
                discoveryAction.Invoke(handle);
            }

            discoveryClient.BeginReceive(new AsyncCallback(ReceiveCallback), discoveryAction);
        }