Beispiel #1
0
        private void ProcessActions(out bool waiting)
        {
            waiting = false;

            // Wait until there's actions to be processed.
            if (mActionQueue.Count == 0)
            {
                return;
            }

            // Acquire radio lock
            if (!mRadioLock.TryEnter(2000, "rfxcom"))
            {
                //// Wait a while for the radio lock to be freed up.
                //EventWaitHandle.WaitAny(mEvents, 100);
                waiting = true;
                return;
            }

            lock (mRadioLock)
            {
                mLockOwned = true;
            }

            DeviceAction action;

            lock (mActionQueue)
            {
                action = mActionQueue.Dequeue();
            }

            bool       result = false;
            DeviceBase device = action.Device;

            if (device is NexaLampDevice)
            {
                var package = Lighting2Protocol.BuildPackage((NexaLampDevice)device, action.Level);
                result = SendPackage(package);
            }
            else if (device is EverflourishDevice)
            {
                var package = Lighting5Protocol.BuildPackage((EverflourishDevice)device, action.Value);
                result = SendPackage(package);
            }

            if (!result)
            {
                Log.Warning("Failed processing action, releasing lock directly");
                mRadioLock.Release();
            }
        }
Beispiel #2
0
        private void HandlePackage(byte[] data)
        {
            var    packageSize = data.Length;
            IntPtr memory      = Marshal.AllocHGlobal(packageSize);

            // Decode first part of the package so we can detect the type
            Marshal.Copy(data, 0, memory, packageSize);
            RFXPACKET packet = (RFXPACKET)Marshal.PtrToStructure(memory, typeof(RFXPACKET));

            if (packet.PacketType == IRESPONSE.TYPE)
            {
                // TODO - Log errors
            }
            else if (packet.PacketType == RXRESPONSE.TYPE)
            {
                HandleRXResponsePackage(memory);
            }
            else if (packet.PacketType == Lighting2Protocol.PacketType)
            {
                NexaEvent nexaEvent = Lighting2Protocol.HandlePackage(mDeviceManager, memory);
                if (nexaEvent != null && nexaEvent.Device != null)
                {
                    //Log.Debug("Dispatching Nexa event");
                    // Notify device
                    nexaEvent.Device.OnServiceEvent(nexaEvent);
                }
            }
            else if (packet.PacketType == Lighting5Protocol.PacketType)
            {
                EverflourishEvent everflourishEvent = Lighting5Protocol.HandlePackage(mDeviceManager, memory);
                if (everflourishEvent != null)
                {
                    //Log.Debug("Dispatching Everflourish event");
                    if (OnEverflourishEvent != null)
                    {
                        OnEverflourishEvent(this, everflourishEvent);
                    }
                }
            }
            else
            {
                Log.Warning("Discarding unhandled package. Size: {0}, Type: {1}, subType: {2}, sequence: {3}",
                            data.Length, packet.PacketType, packet.Subtype, packet.SequenceNumber);
                PrintRawPackage(data);
            }

            // Clean up the memory
            Marshal.FreeHGlobal(memory);
        }