Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
        }