private void ProcessPlayPatternRequest(RequestReceivedEventArgs e, BlinkStickDeviceSettings ledSettings)
        {
            Pattern pattern  = new Pattern();
            int     channel  = 0;
            int     ledStart = 0;
            int     ledEnd   = 0;

            for (int i = 0; i < e.Context.Request.QueryString.AllKeys.Length; i++)
            {
                string key   = e.Context.Request.QueryString.AllKeys[i].ToLower();
                string value = e.Context.Request.QueryString.GetValues(i)[0];

                if (key == "channel")
                {
                    try
                    {
                        channel = Convert.ToInt32(value);
                        if (channel < 0 || channel > 2)
                        {
                            throw new Exception("not within range of 0..2");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid channel parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "firstled")
                {
                    try
                    {
                        ledStart = Convert.ToInt32(value);
                        if (ledStart < 0 || ledStart > 63)
                        {
                            throw new Exception("not within range of 0..63");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid ledStart parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "lastled")
                {
                    try
                    {
                        ledEnd = Convert.ToInt32(value);
                        if (ledEnd < 0 || ledEnd > 63)
                        {
                            throw new Exception("not within range of 0..63");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid ledEnd parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "pattern")
                {
                    pattern = DataModel.FindPatternByName(value);

                    if (pattern == null)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Pattern {0} not found", value)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
            }

            if (pattern == null)
            {
                server.SendResponseJson(422, new ErrorResponse()
                {
                    error = "Missing pattern parameter"
                }, e.Context.Response);
                e.Handled = true;
                return;
            }

            try
            {
                OnPatternSend(channel, (byte)ledStart, (byte)ledEnd, ledSettings, pattern, 1, 0);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Failed to send color {0}", ex);
            }
            server.SendResponseJson(200, null, e.Context.Response);

            e.Handled = true;
        }
        private void ProcessSetColorRequest(RequestReceivedEventArgs e, BlinkStickDeviceSettings ledSettings)
        {
            RgbColor color    = RgbColor.Black();
            int      channel  = 0;
            int      firstLed = 0;
            int      lastLed  = 0;

            for (int i = 0; i < e.Context.Request.QueryString.AllKeys.Length; i++)
            {
                string key   = e.Context.Request.QueryString.AllKeys[i].ToLower();
                string value = e.Context.Request.QueryString.GetValues(i)[0];

                if (key == "channel")
                {
                    try
                    {
                        channel = Convert.ToInt32(value);
                        if (channel < 0 || channel > 2)
                        {
                            throw new Exception("not within range of 0..2");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid channel parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "firstled")
                {
                    try
                    {
                        firstLed = Convert.ToInt32(value);
                        if (firstLed < 0 || firstLed > 63)
                        {
                            throw new Exception("not within range of 0..63");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid ledStart parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "lastled")
                {
                    try
                    {
                        lastLed = Convert.ToInt32(value);
                        if (lastLed < 0 || lastLed > 63)
                        {
                            throw new Exception("not within range of 0..63");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid ledEnd parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "color")
                {
                    try
                    {
                        color = RgbColor.FromString(value);
                    }
                    catch
                    {
                        try
                        {
                            color = RgbColor.FromString("#" + value);
                        }
                        catch
                        {
                            server.SendResponseJson(422, new ErrorResponse()
                            {
                                error = "Invalid color parameter"
                            }, e.Context.Response);
                            e.Handled = true;
                            return;
                        }
                    }
                }
            }

            try
            {
                Pattern pattern = new Pattern();
                pattern.Animations.Add(new Animation());
                pattern.Animations[0].AnimationType = AnimationTypeEnum.SetColor;
                pattern.Animations[0].DelaySetColor = 0;
                pattern.Animations[0].Color         = color;
                OnPatternSend(channel, (byte)firstLed, (byte)lastLed, ledSettings, pattern, 1, 0);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Failed to send color {0}", ex);
            }
            server.SendResponseJson(200, null, e.Context.Response);

            e.Handled = true;
        }