Esempio n. 1
0
        private static async void BixListens_OnHttpEventReceived(object sender, HttpEvent e)
        {
            Log.Bulb($"{e.ID} Received new event");
            var responseBuilder = new StringBuilder();

            if (!IsNullOrEmpty(e.QueryString["UpdateState"]))
            {
                SendResponse(e.HttpListenerResponse, "OK");
                Log.Bulb($"{e.ID} Proccessed event");

                foreach (var bulb in Bulbs)
                {
                    _client.UpdateLightStateAsync(bulb);
                }


                return;
            }

            if (!IsNullOrEmpty(e.QueryString["Log"]))
            {
                SendResponse(e.HttpListenerResponse, Log.GetMessages(), false);
                Log.Bulb($"{e.ID} Proccessed event");
                return;
            }


            if (!IsNullOrEmpty(e.QueryString["BuildBulbs"]))
            {
                SendResponse(e.HttpListenerResponse, BuildCreateDevice());
                Log.Bulb($"{e.ID} Proccessed event");
                return;
            }

            if (!IsNullOrEmpty(e.QueryString["Status"]))
            {
                SendResponse(e.HttpListenerResponse, BuildStatus());
                Log.Bulb($"{e.ID} Proccessed event");
                return;
            }

            if (!IsNullOrEmpty(e.QueryString["ListColors"]))
            {
                SendResponse(e.HttpListenerResponse, ColorsTable);
                Log.Bulb($"{e.ID} Proccessed event");
                return;
            }

            if (!IsNullOrEmpty(e.QueryString["ListLights"]))
            {
                var lights = Bulbs.Where(a => a.State != null).Select(a => a.State.Label).OrderBy(b1 => b1).ToList();
                foreach (var light1 in lights)
                {
                    responseBuilder.AppendLine(light1);
                }

                SendResponse(e.HttpListenerResponse, responseBuilder.ToString());
                Log.Bulb($"{e.ID} Proccessed event");
                return;
            }


            if (IsNullOrEmpty(e.QueryString["Light"]))
            {
                SendResponse(e.HttpListenerResponse, "Need a light");
                Log.Bulb($"{e.ID} Proccessed event");
                return;
            }

            var light = e.QueryString["Light"];
            var bulbs = new List <LightBulb>();

            if (light == "all")
            {
                lock (Bulbs)
                {
                    bulbs.AddRange(Bulbs);
                }
            }
            else
            {
                var b = Bulbs.FirstOrDefault(a => a.State != null && a.State.Label == light);
                if (b != null)
                {
                    bulbs.Add(b);
                }
                else
                {
                    var bd = Bulbs.Where(a => a.State != null && a.State.Label.StartsWith(light)).ToList();
                    if (!bd.Any())
                    {
                        SendResponse(e.HttpListenerResponse, $"Cant find light or starts with {light}");
                        return;
                    }
                    bulbs.AddRange(bd);
                }
            }
            //var t1 =UpdateBulbs(bulbs);

            //PowerCommand
            if (!IsNullOrEmpty(e.QueryString["Power"]))
            {
                var powerstate = e.QueryString["Power"].ToLower();
                foreach (var bulb in bulbs)
                {
                    lock (new object())
                    {
                        var power = SetPower(bulb, e.ID, powerstate);
                        responseBuilder.AppendLine($"Powered {light} from {powerstate} to {power.Result}");
                    }
                }
            }
            var goodCommand = false;
            //Set Color
            ushort hue        = 0;
            ushort saturation = 0;
            ushort brightness = 0;
            ushort kelvin     = 0;

            if (!IsNullOrEmpty(e.QueryString["Dim"]))
            {
                var    dimStr = e.QueryString["Dim"].ToLower();
                ushort dim;
                if (ushort.TryParse(dimStr, out dim))
                {
                    goodCommand = true;
                    dim         = (ushort)(65535 * (dim / 100.0));
                    brightness  = dim;
                }
                else
                {
                    responseBuilder.AppendLine($"Dim {dimStr} needs to be a whole number");
                }
            }

            var colorStr = "";

            if (!IsNullOrEmpty(e.QueryString["Color"]))
            {
                var isGood = true;
                colorStr = e.QueryString["Color"].ToLower();
                if (IsNullOrEmpty(colorStr))
                {
                    isGood = false;
                    responseBuilder.AppendLine("Color is empty");
                }
                else if (colorStr.StartsWith("#") && colorStr.Length == 7)
                {
                    var color = ColorTranslator.FromHtml(colorStr);
                    colorStr = color.Name;
                }
                else if (colorStr.Length == 4 && ushort.TryParse(colorStr, out kelvin))
                {
                    if (kelvin < 2500 || kelvin > 9000)
                    {
                        responseBuilder.AppendLine($"Kelvin {kelvin} out of range (2500-9000)");
                        isGood = false;
                    }
                    else
                    {
                        hue        = 255;
                        saturation = 255;
                    }
                }
                else if (BIXColors.Colors.ContainsKey(colorStr))
                {
                    var color = BIXColors.Colors[colorStr];
                    hue        = color.LIFXHue;
                    saturation = color.LIFXSaturation;
                }
                else
                {
                    responseBuilder.AppendLine($"Cannot find color {colorStr}");
                    isGood = false;
                }


                if (!isGood)
                {
                    SendResponse(e.HttpListenerResponse, responseBuilder.ToString());
                    Log.Bulb($"{e.ID} Proccessed event");
                    return;
                }
                goodCommand = true;
            }
            if (goodCommand)
            {
                foreach (var bulb in bulbs)
                {
                    lock (new object())
                    {
                        var t = SetColor(bulb, e.ID, hue, saturation, brightness, kelvin);
                        responseBuilder.AppendLine(
                            $"Set color for bulb {bulb.State.Label} to color {colorStr} hue: {hue} saturation: {saturation} brightness: {brightness} kelvin: {kelvin}");
                    }
                }
            }
            SendResponse(e.HttpListenerResponse, responseBuilder.ToString());
            Log.Bulb($"{e.ID} Proccessed event");
        }