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); }
// Called from RfxComService public void OnServiceEvent(NexaEvent nexaEvent) { OnDeviceEvent(this, nexaEvent); }