Esempio n. 1
0
        public static NexaEvent HandlePackage(DeviceManager deviceManager, IntPtr memory)
        {
            var package = (LIGHTING2)Marshal.PtrToStructure(memory, typeof(LIGHTING2));

            //    //id1, id2, id3, id4, unit, command, level, rssi = struct.unpack(b'xxxxBBBBBBBB', package)

            int address = package.id1 << 24 | package.id2 << 16 | package.id3 << 8 | package.id4;

            // Make unit start at index zero
            int unit = package.unitcode - 1;

            Log.Debug("Got Nexa event. Address: {0}, unit: {1}, command: {2}, level: {3}, rssi: {4}", address, unit, package.cmnd, package.level, package.rssi);

            //0x00 = off
            //0x01 = on
            //0x02 = set level
            //0x03 = group off
            //0x04 = group on
            //0x05 = group set level
            var isGroupCommand = (package.cmnd >= 0x03);
            var isOnCommand    = package.level > 0;

            // TODO - Use a pre-calculated lookup for finding devices

            NexaSensorDevice device = null;

            foreach (var entry in deviceManager.Devices)
            {
                NexaSensorDevice nexaDevice = entry.Value as NexaSensorDevice;
                if (nexaDevice == null)
                {
                    continue;
                }

                if (nexaDevice.Address == address && nexaDevice.Unit == unit)
                {
                    device = nexaDevice;
                    break;
                }
            }

            // Convert level to 0-1 range
            float level = (float)package.level;

            level = level / 15.0f;

            return(new NexaEvent(device, isOnCommand, level));
        }
Esempio n. 2
0
 public NexaEvent(NexaSensorDevice device, bool value, float level)
 {
     Device = device;
     Value  = value;
     Level  = level;
 }
Esempio n. 3
0
 public NexaEvent(NexaSensorDevice device, bool value, float level)
 {
     Device = device;
     Value = value;
     Level = level;
 }