Example #1
0
        public void SetColor(List <Color> colors, double fadeTime)
        {
            if (colors == null)
            {
                throw new InvalidEnumArgumentException("Colors cannot be null.");
            }
            if (!Streaming)
            {
                return;
            }
            var packet = new List <Byte>();

            // Set mode to DRGB, dude.
            packet.Add(ByteUtils.IntByte(2));
            packet.Add(ByteUtils.IntByte(2));
            foreach (var color in colors)
            {
                packet.Add(ByteUtils.IntByte(color.R));
                packet.Add(ByteUtils.IntByte(color.G));
                packet.Add(ByteUtils.IntByte(color.B));
            }
            //LogUtil.Write("No, really, sending?");
            if (!colorsSet)
            {
                colorsSet = true;
                LogUtil.Write("Sending " + colors.Count + " colors to " + IpAddress);
                LogUtil.Write("First packet: " + ByteUtils.ByteString(packet.ToArray()));
            }
            _stripSender.SendTo(packet.ToArray(), ep);
            //LogUtil.Write("Sent.");
        }
Example #2
0
        public override void ParsePayload(byte[] payload)
        {
            if (payload is null)
            {
                throw new ArgumentNullException(nameof(payload));
            }
            if (payload.Length < 132)
            {
                throw new ArgumentException($"Payload length is too short: {ByteUtils.ByteString(payload)}");
            }
            var name1 = ByteUtils.ExtractString(payload, 0, 16);

            if (name1.Length == 0)
            {
                name1 = DeviceTag;
            }
            Name = name1;
            var groupName1 = ByteUtils.ExtractString(payload, 16, 32);

            if (groupName1.Length == 0)
            {
                groupName1 = "Group";
            }
            GroupName              = groupName1;
            GroupNumber            = payload[32];
            Mode                   = payload[33];
            Brightness             = payload[34];
            Zones                  = payload[35];
            ZonesBrightness        = ByteUtils.ExtractInt(payload, 36, 40);
            AmbientColor           = ByteUtils.ExtractString(payload, 40, 43, true);
            Saturation             = ByteUtils.ExtractString(payload, 43, 46, true);
            flexSetup              = ByteUtils.ExtractInt(payload, 46, 52);
            MusicModeType          = payload[52];
            musicModeColors        = ByteUtils.ExtractInt(payload, 53, 56);
            musicModeWeights       = ByteUtils.ExtractInt(payload, 56, 59);
            MinimumLuminosity      = ByteUtils.ExtractInt(payload, 59, 62);
            AmbientShowType        = payload[62];
            FadeRate               = payload[63];
            IndicatorLightAutoOff  = payload[69];
            UsbPowerEnable         = payload[70];
            SectorBroadcastControl = payload[71];
            SectorBroadcastTiming  = payload[72];
            HdmiInput              = payload[73];
            MusicModeSource        = payload[74];
            HdmiInputName1         = ByteUtils.ExtractString(payload, 75, 91);
            HdmiInputName2         = ByteUtils.ExtractString(payload, 91, 107);
            HdmiInputName3         = ByteUtils.ExtractString(payload, 107, 123);
            CecPassthroughEnable   = payload[123];
            CecSwitchingEnable     = payload[124];
            HpdEnable              = payload[125];
            VideoFrameDelay        = payload[127];
            LetterboxingEnable     = payload[128];
            HdmiActiveChannels     = payload[129];
            EspFirmwareVersion     = ByteUtils.ExtractBytes(payload, 130, 132);
            PicVersionNumber       = ByteUtils.ExtractBytes(payload, 132, 134);
            ColorBoost             = payload[134];
            if (payload.Length >= 137)
            {
                CecPowerEnable = payload[135];
            }
            if (payload.Length >= 138)
            {
                SkuSetup = payload[136];
            }
            if (payload.Length >= 139)
            {
                BootState = payload[137];
            }
            if (payload.Length >= 140)
            {
                PillarboxingEnable = payload[138];
            }
            if (payload.Length >= 141)
            {
                HdrToneRemapping = payload[139];
            }
        }
Example #3
0
        private void ProcessData(byte[] receivedBytes, IPEndPoint receivedIpEndPoint)
        {
            // Convert data to ASCII and print in console
            if (!MsgUtils.CheckCrc(receivedBytes))
            {
                return;
            }
            string     command       = null;
            string     flag          = null;
            var        from          = receivedIpEndPoint.Address.ToString();
            var        replyPoint    = new IPEndPoint(receivedIpEndPoint.Address, 8888);
            var        payloadString = string.Empty;
            var        payload       = Array.Empty <byte>();
            BaseDevice msgDevice     = null;
            var        writeState    = false;
            var        msg           = new DreamScreenMessage(receivedBytes, from);

            if (msg.IsValid)
            {
                payload       = msg.GetPayload();
                payloadString = msg.PayloadString;
                command       = msg.Command;
                msgDevice     = msg.Device;
                string[] ignore = { "SUBSCRIBE", "READ_CONNECT_VERSION?", "COLOR_DATA", "DEVICE_DISCOVERY" };
                if (!ignore.Contains(command))
                {
                    Console.WriteLine($@"{from} -> {JsonConvert.SerializeObject(msg)}.");
                }
                flag = msg.Flags;
                var groupMatch = msg.Group == dev.GroupNumber || msg.Group == 255;
                if ((flag == "11" || flag == "21") && groupMatch)
                {
                    dev        = GetDeviceData();
                    writeState = true;
                }
            }

            switch (command)
            {
            case "SUBSCRIBE":
                if (devMode == 1 || devMode == 2)
                {
                    DreamSender.SendUdpWrite(0x01, 0x0C, new byte[] { 0x01 }, 0x10, group, replyPoint);
                }
                break;

            case "COLOR_DATA":
                if (devMode == 1 || devMode == 2)
                {
                    var colorData  = ByteUtils.SplitHex(payloadString, 6);    // Swap this with payload
                    var lightCount = 0;
                    var colors     = new string[12];
                    foreach (var colorValue in colorData)
                    {
                        colors[lightCount] = colorValue;
                        if (lightCount > 11)
                        {
                            break;
                        }
                        lightCount++;
                    }

                    SendColors(colors);
                }

                break;

            case "DEVICE_DISCOVERY":
                if (flag == "30" && from != "0.0.0.0")
                {
                    SendDeviceStatus(replyPoint);
                }
                else if (flag == "60")
                {
                    if (msgDevice != null)
                    {
                        string dsIpCheck = DreamData.GetItem("dsIp");
                        if (dsIpCheck == "0.0.0.0" &&
                            msgDevice.Tag.Contains("DreamScreen", StringComparison.CurrentCulture))
                        {
                            Console.WriteLine(@"No DS IP Set, setting.");
                            DreamData.SetItem("dsIp", from);
                            targetEndpoint = replyPoint;
                        }

                        if (_searching)
                        {
                            Devices.Add(msgDevice);
                        }
                    }
                }

                break;

            case "GROUP_NAME":
                var gName = Encoding.ASCII.GetString(payload);
                if (writeState)
                {
                    dev.GroupName = gName;
                }

                break;

            case "GROUP_NUMBER":
                int gNum = payload[0];
                if (writeState)
                {
                    dev.GroupNumber = gNum;
                }

                break;

            case "NAME":
                var dName = Encoding.ASCII.GetString(payload);
                if (writeState)
                {
                    dev.Name = dName;
                }

                break;

            case "BRIGHTNESS":
                brightness = payload[0];
                if (writeState)
                {
                    Console.WriteLine($@"Setting brightness to {brightness}.");
                    dev.Brightness = payload[0];
                    UpdateBrightness(payload[0]);
                }

                break;

            case "SATURATION":
                if (writeState)
                {
                    dev.Saturation = ByteUtils.ByteString(payload);
                }

                break;

            case "MODE":
                if (writeState)
                {
                    dev.Mode = payload[0];
                    Console.WriteLine($@"Updating mode: {dev.Mode}.");
                    UpdateMode(dev.Mode);
                }

                break;

            case "AMBIENT_MODE_TYPE":
                if (writeState)
                {
                    dev.AmbientModeType = payload[0];
                    UpdateAmbientMode(dev.Mode);
                }

                break;

            case "AMBIENT_SCENE":
                if (writeState)
                {
                    ambientShow         = payload[0];
                    dev.AmbientShowType = ambientShow;
                    UpdateAmbientShow(ambientShow);
                    Console.WriteLine($@"Scene updated: {ambientShow}.");
                }

                break;

            case "AMBIENT_COLOR":
                if (writeState)
                {
                    dev.AmbientColor = ByteUtils.ByteString(payload);
                    UpdateAmbientColor(dev.AmbientColor);
                }

                break;
            }

            if (writeState)
            {
                DreamData.SetItem <BaseDevice>("myDevice", dev);
            }
        }
Example #4
0
        private void ProcessData(byte[] receivedBytes, IPEndPoint receivedIpEndPoint)
        {
            // Convert data to ASCII and print in console
            if (!MsgUtils.CheckCrc(receivedBytes))
            {
                return;
            }
            string     command       = null;
            string     flag          = null;
            var        from          = receivedIpEndPoint.Address.ToString();
            var        replyPoint    = new IPEndPoint(receivedIpEndPoint.Address, 8888);
            var        payloadString = string.Empty;
            var        payload       = Array.Empty <byte>();
            BaseDevice msgDevice     = null;
            var        writeState    = false;
            var        writeDev      = false;
            var        msg           = new DreamScreenMessage(receivedBytes, from);
            var        tDevice       = _dev;

            if (msg.IsValid)
            {
                payload       = msg.GetPayload();
                payloadString = msg.PayloadString;
                command       = msg.Command;
                msgDevice     = msg.Device;
                flag          = msg.Flags;
                var groupMatch = msg.Group == _dev.GroupNumber || msg.Group == 255;
                if ((flag == "11" || flag == "17" || flag == "21") && groupMatch)
                {
                    writeState = true;
                    writeDev   = true;
                }
                if (flag == "41")
                {
                    LogUtil.Write($"Flag is 41, we should save settings for {from}.");
                    tDevice = DataUtil.GetDreamDevice(from);
                    if (tDevice != null)
                    {
                        writeDev = true;
                    }
                }
                if (from != null && command != null && command != "COLOR_DATA" && command != "SUBSCRIBE" && tDevice != null)
                {
                    LogUtil.Write($@"{from} -> {tDevice.IpAddress}::{command} {flag}-{msg.Group}.");
                }
            }
            else
            {
                LogUtil.Write($@"Invalid message from {from}");
            }
            switch (command)
            {
            case "SUBSCRIBE":

                if (_devMode == 1 || _devMode == 2)
                {
                    DreamSender.SendUdpWrite(0x01, 0x0C, new byte[] { 0x01 }, 0x10, _group, replyPoint);
                }

                // If the device is on and capture mode is not using DS data
                if (_devMode != 0 && CaptureMode != 0)
                {
                    // If the device is replying to our sub broadcast
                    if (flag == "60")
                    {
                        // Set our count to 3, which is how many tries we get before we stop sending data
                        if (!string.IsNullOrEmpty(from))
                        {
                            if (!_subscribers.ContainsKey(from))
                            {
                                LogUtil.Write("Adding new subscriber: " + from);
                            }
                            _subscribers[from] = 3;
                        }
                        else
                        {
                            LogUtil.Write("Can't add subscriber, from is empty...");
                        }
                    }
                }
                break;

            case "DISCOVERY_START":
                LogUtil.Write("DreamScreen: Starting discovery.");
                _devices     = new List <BaseDevice>();
                _discovering = true;
                break;

            case "DISCOVERY_STOP":
                LogUtil.Write($"DreamScreen: Discovery complete, found {_devices.Count} devices.");
                _discovering = false;
                foreach (var d in _devices)
                {
                    DataUtil.InsertCollection <BaseDevice>("devices", d);
                }

                break;

            case "REMOTE_REFRESH":
                var id   = Encoding.UTF8.GetString(payload.ToArray());
                var tDev = _sDevices.FirstOrDefault(b => b.Id == id);
                LogUtil.Write($"Triggering reload of device {id}.");
                tDev?.ReloadData();
                break;

            case "COLOR_DATA":
                if (_devMode == 1 || _devMode == 2)
                {
                    var colorData = ByteUtils.SplitHex(payloadString, 6);     // Swap this with payload
                    var colors    = new List <Color>();
                    foreach (var colorValue in colorData)
                    {
                        colors.Add(ColorFromString(colorValue));
                    }

                    colors = ShiftColors(colors);
                    SendColors(colors, colors);
                }

                break;

            case "DEVICE_DISCOVERY":
                if (flag == "30" && from != "0.0.0.0")
                {
                    SendDeviceStatus(replyPoint);
                }
                else if (flag == "60")
                {
                    if (msgDevice != null)
                    {
                        string dsIpCheck = DataUtil.GetItem("DsIp");
                        if (dsIpCheck == "0.0.0.0" &&
                            msgDevice.Tag.Contains("DreamScreen", StringComparison.CurrentCulture))
                        {
                            LogUtil.Write(@"Setting a target DS IP.");
                            DataUtil.SetItem("DsIp", from);
                            _targetEndpoint = replyPoint;
                        }

                        if (_discovering)
                        {
                            LogUtil.Write("Sending request for serial!");
                            DreamSender.SendUdpWrite(0x01, 0x03, new byte[] { 0 }, 0x60, 0, replyPoint);
                            _devices.Add(msgDevice);
                        }
                    }
                }

                break;

            case "GET_SERIAL":
                if (flag == "30")
                {
                    SendDeviceSerial(replyPoint);
                }
                else
                {
                    LogUtil.Write("DEVICE SERIAL RETRIEVED: " + JsonConvert.SerializeObject(msg));
                }

                break;

            case "GROUP_NAME":
                var gName = Encoding.ASCII.GetString(payload);
                if (writeState | writeDev)
                {
                    tDevice.GroupName = gName;
                }

                break;

            case "GROUP_NUMBER":
                int gNum = payload[0];
                if (writeState | writeDev)
                {
                    tDevice.GroupNumber = gNum;
                }

                break;

            case "NAME":
                var dName = Encoding.ASCII.GetString(payload);
                if (writeState | writeDev)
                {
                    tDevice.Name = dName;
                }

                break;

            case "BRIGHTNESS":
                _brightness = payload[0];
                if (writeState | writeDev)
                {
                    LogUtil.Write($@"Setting brightness to {_brightness}.");
                    tDevice.Brightness = payload[0];
                }
                if (writeState)
                {
                    UpdateBrightness(payload[0]);
                }

                break;

            case "SATURATION":
                if (writeState | writeDev)
                {
                    tDevice.Saturation = ByteUtils.ByteString(payload);
                }

                break;

            case "MODE":
                if (writeState | writeDev)
                {
                    tDevice.Mode = payload[0];
                    LogUtil.Write("UPDATING MODE FROM REMOTE");

                    LogUtil.Write($@"Updating mode: {tDevice.Mode}.");
                }
                else
                {
                    LogUtil.Write("Mode flag set, but we're not doing anything... " + flag);
                }

                if (writeState)
                {
                    UpdateMode(tDevice.Mode);
                }

                break;

            case "REFRESH_CLIENTS":
                LogUtil.Write("Triggering discovery.");
                StartRefreshTimer(true);
                break;

            case "AMBIENT_MODE_TYPE":
                if (writeState | writeDev)
                {
                    tDevice.AmbientModeType = payload[0];
                }

                if (writeState)
                {
                    UpdateAmbientMode(tDevice.AmbientModeType);
                }

                break;

            case "AMBIENT_SCENE":
                if (writeState | writeDev)
                {
                    _ambientShow            = payload[0];
                    tDevice.AmbientShowType = _ambientShow;
                }
                if (writeState)
                {
                    UpdateAmbientShow(_ambientShow);
                }
                break;

            case "AMBIENT_COLOR":
                if (writeDev | writeState)
                {
                    if (tDevice != null)
                    {
                        tDevice.AmbientColor = ByteUtils.ByteString(payload);
                    }
                }
                if (writeState && tDevice != null)
                {
                    UpdateAmbientColor(ColorFromString(tDevice.AmbientColor));
                }

                break;

            case "SKU_SETUP":
                if (writeState | writeDev)
                {
                    tDevice.SkuSetup = payload[0];
                }

                break;

            case "FLEX_SETUP":
                if (writeState | writeDev)
                {
                    int[] fSetup = payload.Select(x => (int)x).ToArray();
                    tDevice.flexSetup = fSetup;
                }

                break;

            case "RESET_PIC":
                break;
            }

            if (writeState)
            {
                DataUtil.SetObject("myDevice", tDevice);
                _dev = tDevice;
                DreamSender.SendUdpWrite(msg.C1, msg.C2, msg.GetPayload(), 0x41, (byte)msg.Group, receivedIpEndPoint);
            }

            if (!writeState && !writeDev)
            {
                return;
            }
            // Notify if the sender was not us
            if (from != _dev.IpAddress)
            {
                NotifyClients();
            }
            DataUtil.InsertDsDevice(tDevice);
        }