public void StayHere()
        {
            var led = BlinkStick.FindFirst();

            led.OpenDevice();

            led.Blink(RgbColor.FromString("blue"), 1);
        }
        public void StayAway()
        {
            var led = BlinkStick.FindFirst();

            led.OpenDevice();

            led.SetColor(RgbColor.FromString("green"));
        }
        public void PleaseComeBack()
        {
            var led = BlinkStick.FindFirst();

            led.OpenDevice();

            led.Blink(RgbColor.FromString("blue"), 3);
        }
        public void AdviceUserToGoForAWalk()
        {
            var led = BlinkStick.FindFirst();

            led.OpenDevice();

            led.Blink(RgbColor.FromString("red"), 2);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Test BlinkStick device");

            var led = BlinkStick.FindFirst();

            led.OpenDevice();
            led.Blink(RgbColor.FromString("green"), 3);
            led.SetColor(RgbColor.FromString("red"));

            Console.WriteLine("Press a key to quit program");
            Console.ReadLine();

            led.TurnOff();
        }
        void ClientDataReceived(object sender, BayeuxClient.DataReceivedEventArgs e)
        {
            Regex r = new Regex("/([^/]+)$");
            Match m = r.Match(e.Channel);

            if (m.Success)
            {
                if (m.Groups[1].Value == this.AccessCode)
                {
                    int channel  = this.LedChannel;
                    int ledStart = this.LedFirstIndex;
                    int ledEnd   = this.LedLastIndex;
                    int repeat   = 1;
                    int duration = 0;

                    if (e.Data.ContainsKey("channel"))
                    {
                        try
                        {
                            channel = Convert.ToInt32((string)e.Data["channel"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert channel parameter {0}", ex);
                        }

                        if (channel < 0 || channel > 2)
                        {
                            log.Warn("Invalid channel parameter, defaulting to 0");
                            channel = 0;
                        }
                    }

                    if (e.Data.ContainsKey("firstLed"))
                    {
                        try
                        {
                            ledStart = Convert.ToInt32((string)e.Data["firstLed"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert firstLed parameter {0}", ex);
                        }

                        if (ledStart < 0 || ledStart > 63)
                        {
                            log.Warn("Invalid firstLed parameter, defaulting to 0");
                            ledStart = 0;
                        }
                    }

                    if (e.Data.ContainsKey("lastLed"))
                    {
                        try
                        {
                            ledEnd = Convert.ToInt32((string)e.Data["lastLed"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert lastLed parameter {0}", ex);
                        }

                        if (ledEnd < 0 || ledEnd > 63)
                        {
                            log.Warn("Invalid lastLed parameter, defaulting to 0");
                            ledEnd = 0;
                        }
                    }

                    if (e.Data.ContainsKey("repeat"))
                    {
                        if (e.Data["repeat"] == "loop")
                        {
                            repeat = -1;
                        }
                        else
                        {
                            try
                            {
                                repeat = Convert.ToInt32((string)e.Data["repeat"]);
                            }
                            catch (Exception ex)
                            {
                                log.WarnFormat("Failed to convert repeat parameter {0}", ex);
                            }
                        }
                    }

                    if (e.Data.ContainsKey("duration"))
                    {
                        try
                        {
                            duration = Convert.ToInt32((string)e.Data["duration"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert duration parameter {0}", ex);
                        }
                    }


                    if (e.Data.ContainsKey("status") && (String)e.Data ["status"] == "off")
                    {
                        log.InfoFormat("Blinkstick device {0} turned off", this.Name);

                        Pattern   pattern   = new Pattern();
                        Animation animation = new Animation();
                        pattern.Animations.Add(animation);
                        animation.Color         = RgbColor.Black();
                        animation.DelaySetColor = 1;

                        OnPatternSend(channel, ledStart, ledEnd, this.Device, pattern, 1, 0);

                        //OnColorSend(channel, ledStart, ledEnd, 0, 0, 0, this.Device);
                    }
                    else if (e.Data.ContainsKey("color"))
                    {
                        String color = (String)e.Data ["color"];

                        log.InfoFormat("New color received for Blinkstick device {0} - {1}", color, this.Name);

                        Pattern   pattern   = new Pattern();
                        Animation animation = new Animation();
                        pattern.Animations.Add(animation);
                        animation.Color         = RgbColor.FromString(color);
                        animation.DelaySetColor = 1;

                        OnPatternSend(channel, ledStart, ledEnd, this.Device, pattern, 1, 0);

                        //RgbColor c = RgbColor.FromString (color);
                        //OnColorSend(channel, ledStart, ledEnd, c.R, c.G, c.B, this.Device);
                    }
                    else if (e.Data.ContainsKey("pattern"))
                    {
                        String patternName = (String)e.Data ["pattern"];

                        Pattern pattern = this.DataModel.FindPatternByName(patternName);

                        if (pattern != null)
                        {
                            OnPatternSend(channel, ledStart, ledEnd, this.Device, pattern, repeat, duration);
                        }
                        else
                        {
                            log.ErrorFormat("Pattern request received for Blinkstick device {0} - {1}, but pattern not found", patternName, this.Name);
                        }
                    }
                }
            }
        }
Exemple #7
0
        private void BuildStandardPatterns()
        {
            if (Patterns.Count == 0)
            {
                // ---- pulse - ["red", "green", "blue"] (x3)
                foreach (String color in new String[] { "red", "green", "blue" })
                {
                    Patterns.Add(new Pattern()
                    {
                        Name = "pulse - " + color + " (x3)"
                    });
                    Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                    {
                        AnimationType = AnimationTypeEnum.Pulse,
                        Color         = RgbColor.FromString(color),
                        DurationPulse = 500,
                        RepeatPulse   = 3
                    });
                }

                // ---- alert - ["white", "red", "green", "blue", "yellow"]
                foreach (String color in new String[] { "white", "red", "green", "blue", "yellow" })
                {
                    Patterns.Add(new Pattern()
                    {
                        Name = "alert - " + color
                    });
                    Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                    {
                        AnimationType = AnimationTypeEnum.Blink,
                        Color         = RgbColor.FromString(color),
                        DurationBlink = 100,
                        RepeatBlink   = 5
                    });
                    Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                    {
                        AnimationType = AnimationTypeEnum.SetColor,
                        ColorString   = "#000000",
                        DelaySetColor = 1000
                    });
                }

                // ---- color - cycle
                Patterns.Add(new Pattern()
                {
                    Name = "color - cycle"
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Morph,
                    ColorString   = "#FF0000",
                    DurationMorph = 1000
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Morph,
                    ColorString   = "#00FF00",
                    DurationMorph = 1000
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Morph,
                    ColorString   = "#0000FF",
                    DurationMorph = 1000
                });

                // ---- police
                Patterns.Add(new Pattern()
                {
                    Name = "police"
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Blink,
                    ColorString   = "#FF0000",
                    DurationBlink = 100,
                    RepeatBlink   = 2
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Blink,
                    ColorString   = "#0000FF",
                    DurationBlink = 100,
                    RepeatBlink   = 2
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.SetColor,
                    ColorString   = "#000000",
                    DelaySetColor = 500
                });

                // ---- ambulance
                Patterns.Add(new Pattern()
                {
                    Name = "ambulance"
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Blink,
                    ColorString   = "#FFFF00",
                    DurationBlink = 100,
                    RepeatBlink   = 3
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.SetColor,
                    ColorString   = "#000000",
                    DelaySetColor = 500
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Blink,
                    ColorString   = "#FFFF00",
                    DurationBlink = 100,
                    RepeatBlink   = 2
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.SetColor,
                    ColorString   = "#000000",
                    DelaySetColor = 500
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Blink,
                    ColorString   = "#FFFF00",
                    DurationBlink = 100,
                    RepeatBlink   = 1
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.SetColor,
                    ColorString   = "#000000",
                    DelaySetColor = 500
                });

                // ---- status - available
                Patterns.Add(new Pattern()
                {
                    Name = "status - available"
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Morph,
                    ColorString   = "#00FF00",
                    DelaySetColor = 1000
                });

                // ---- status - away
                Patterns.Add(new Pattern()
                {
                    Name = "status - away"
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Morph,
                    ColorString   = "#FFFF00",
                    DelaySetColor = 1000
                });

                // ---- status - busy
                Patterns.Add(new Pattern()
                {
                    Name = "status - busy"
                });
                Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                {
                    AnimationType = AnimationTypeEnum.Morph,
                    ColorString   = "#FF0000",
                    DelaySetColor = 1000
                });

                // ---- color - ["white", "red", "green", "blue", "yellow", "black"]
                foreach (String color in new String[] { "white", "red", "green", "blue", "yellow", "black" })
                {
                    Patterns.Add(new Pattern()
                    {
                        Name = "color - " + color
                    });
                    Patterns[Patterns.Count - 1].Animations.Add(new Animation()
                    {
                        AnimationType = AnimationTypeEnum.SetColor,
                        Color         = RgbColor.FromString(color),
                        DelaySetColor = 1000
                    });
                }
            }
        }
        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;
        }