示例#1
0
文件: Network.cs 项目: xrgman/HueZooi
        private List <HueLamp> ExtractLights(string json)
        {
            List <HueLamp> lamps = new List <HueLamp>();
            JsonObject     lights;

            if (!JsonObject.TryParse(json, out lights))
            {
                System.Diagnostics.Debug.WriteLine("Error: " + json);
            }
            foreach (string lightId in lights.Keys)
            {
                JsonObject light = lights.GetNamedObject(lightId, null);
                string     name  = light.GetNamedString("name", string.Empty);
                light = light.GetNamedObject("state", null);
                if (light != null)
                {
                    HueLamp hueLamp = new HueLamp(
                        name,
                        Convert.ToInt32(lightId),
                        Convert.ToInt32(light.GetNamedNumber("hue", 0)),
                        Convert.ToInt32(light.GetNamedNumber("bri", 0)),
                        Convert.ToInt32(light.GetNamedNumber("sat", 0)),
                        light.GetNamedBoolean("on", false)
                        );
                    lamps.Add(hueLamp);
                }
            }
            return(lamps.OrderBy(x => x.Id).ToList());
        }
示例#2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var            templamp = (Button)sender;
            HueLamp        lamp     = (HueLamp)templamp.DataContext;
            List <HueLamp> lamps    = new List <HueLamp>();

            lamps.Add(lamp);
            Frame.Navigate(typeof(SingleLampPage), lamps);
        }
示例#3
0
 public static void UpdateHueLamp(HueLamp lamp, JObject jsonObject)
 {
     string lampId = "" + lamp.ID;
     lamp.Name = (string)jsonObject      [lampId]["name"];
     lamp.On = (bool)jsonObject          [lampId]["state"]["on"];
     lamp.Brightness = (int)jsonObject   [lampId]["state"]["bri"];
     lamp.Hue = (long)jsonObject         [lampId]["state"]["hue"];
     lamp.Sat = (int)jsonObject          [lampId]["state"]["sat"];
 }
示例#4
0
        public static void UpdateHueLamp(HueLamp lamp, JObject jsonObject)
        {
            string lampId = "" + lamp.ID;

            lamp.Name       = (string)jsonObject      [lampId]["name"];
            lamp.On         = (bool)jsonObject          [lampId]["state"]["on"];
            lamp.Brightness = (int)jsonObject   [lampId]["state"]["bri"];
            lamp.Hue        = (long)jsonObject         [lampId]["state"]["hue"];
            lamp.Sat        = (int)jsonObject          [lampId]["state"]["sat"];
        }
示例#5
0
 public static void SendOnProperty(HueLamp lamp)
 {
     HttpStringContent content = new HttpStringContent
         (
         $"{{\"on\":{lamp.On.ToString().ToLower()}}}",
         Windows.Storage.Streams.UnicodeEncoding.Utf8,
         "application/json"
         );
     SendProperty(content, lamp.ID);
 }
示例#6
0
 public static void SendAllPropertys(HueLamp lamp)
 {
     HttpStringContent content = new HttpStringContent
             (
             $"{{\"on\":{lamp.On.ToString().ToLower()},\"bri\":{lamp.Brightness.ToString().ToLower()},\"hue\":{lamp.Hue.ToString().ToLower()},\"sat\":{lamp.Sat.ToString().ToLower()}}}",
                         Windows.Storage.Streams.UnicodeEncoding.Utf8,
                         "application/json"
             );
     SendProperty(content, lamp.ID);
 }
示例#7
0
        public static void SendOnProperty(HueLamp lamp)
        {
            HttpStringContent content = new HttpStringContent
                                        (
                $"{{\"on\":{lamp.On.ToString().ToLower()}}}",
                Windows.Storage.Streams.UnicodeEncoding.Utf8,
                "application/json"
                                        );

            SendProperty(content, lamp.ID);
        }
示例#8
0
        public static void SendAllPropertys(HueLamp lamp)
        {
            HttpStringContent content = new HttpStringContent
                                        (
                $"{{\"on\":{lamp.On.ToString().ToLower()},\"bri\":{lamp.Brightness.ToString().ToLower()},\"hue\":{lamp.Hue.ToString().ToLower()},\"sat\":{lamp.Sat.ToString().ToLower()}}}",
                Windows.Storage.Streams.UnicodeEncoding.Utf8,
                "application/json"
                                        );

            SendProperty(content, lamp.ID);
        }
示例#9
0
        public static Color getColor(this HueLamp light)
        {
            double hue = ((double)light.Hue * 360.0f) / 65535.0f;
            double sat = (double)light.Saturation / 255.0f;
            double val = (double)light.Brightness / 255.0f;

            int r, g, b;

            HsvToRgb(hue, sat, val, out r, out g, out b);
            return(Color.FromArgb(255, Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)));
        }
示例#10
0
 private void SliderChanged(object sender, RangeBaseValueChangedEventArgs e)
 {
     if (Lamps != null)
     {
         foreach (var HueLamp in Lamps)
         {
             HueLamp.UpdateRgb();
             UpdateColours();
         }
     }
 }
示例#11
0
文件: Network.cs 项目: xrgman/HueZooi
 public async void SetLamp(HueLamp hueLamp)
 {
     RootLight light = new RootLight()
     {
         on  = hueLamp.On,
         hue = Convert.ToInt32(hueLamp.Hue),
         sat = Convert.ToInt32(hueLamp.Saturation),
         bri = Convert.ToInt32(hueLamp.Brightness)
     };
     HttpContent content = new StringContent(JsonConvert.SerializeObject(light), Encoding.UTF8, "application/json");
     var         setter  = await client.PutAsync($"http://{ipAdress}:{port}/api/{hueUsername}/lights/{hueLamp.Id}/state", content);
 }
示例#12
0
 protected override void OnNavigatedTo(NavigationEventArgs e)
 {
     huelamp = (HueLamp)e.Parameter;
     NameLight.Text = huelamp.name;
     int r, g, b;
     huelamp.getRGBValue(out r, out g, out b);
     Red.Value = r;
     Green.Value = g;
     Blue.Value = b;
     SetOn.IsOn = huelamp.OnLamp;
     SolidColorBrush color = new SolidColorBrush(huelamp.getColor());
     ColorChanger.Fill = color;
 }
示例#13
0
        private void SelectionClick(object sender, RoutedEventArgs e)
        {
            var     templamp = (Button)sender;
            HueLamp lamp     = (HueLamp)templamp.DataContext;

            if (selected.Contains(lamp))
            {
                selected.Remove(lamp);
                templamp.Content = "";
            }
            else
            {
                selected.Add(lamp);
                templamp.Content = "✓";
            }
        }
    public IEnumerator DiscoverLights()
    {
        //HttpWebRequest request = (HttpWebRequest)WebRequest.Create();
        //HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        UnityWebRequest lights_json = UnityWebRequest.Get("http://" + bridgeip + "/api/" + username + "/lights");

        yield return(lights_json.Send());

        Debug.Log("http://" + bridgeip + ":" + portNumber + "/api/" + username + "/lights");

        //System.IO.Stream stream = response.GetResponseStream();
        // System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8);

        var lights = (Dictionary <string, object>)Json.Deserialize(lights_json.downloadHandler.text);

        foreach (string key in lights.Keys)
        {
            var light = (Dictionary <string, object>)lights[key];

            foreach (HueLamp hueLamp in GetComponentsInChildren <HueLamp>())
            {
                if (hueLamp.devicePath.Equals(key))
                {
                    goto Found;
                }
            }

            if (light["type"].Equals("Extended color light"))
            {
                GameObject gameObject = new GameObject();
                gameObject.name             = (string)light["name"];
                gameObject.transform.parent = transform;
                gameObject.AddComponent <HueLamp>();
                HueLamp lamp = gameObject.GetComponent <HueLamp>();
                lamp.devicePath = key;
            }

Found:
            ;
        }
    }
示例#15
0
    public void DiscoverLights()
    {
        HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create("http://" + hostName + "/api/" + username + "/lights");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Debug.Log("http" + hostName + portNumber + "/api/" + username + "/lights");

        System.IO.Stream       stream       = response.GetResponseStream();
        System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8);

        var lights = (Dictionary <string, object>)Json.Deserialize(streamReader.ReadToEnd());

        foreach (string key in lights.Keys)
        {
            var light = (Dictionary <string, object>)lights[key];

            foreach (HueLamp hueLamp in GetComponentsInChildren <HueLamp>())
            {
                if (hueLamp.devicePath.Equals(key))
                {
                    goto Found;
                }
            }

            if (light["type"].Equals("Extended color light"))
            {
                GameObject gameObject = new GameObject();
                gameObject.name             = (string)light["name"];
                gameObject.transform.parent = transform;
                gameObject.AddComponent <HueLamp>();
                HueLamp lamp = gameObject.GetComponent <HueLamp>();
                lamp.devicePath = key;
            }

Found:
            ;
        }
    }
示例#16
0
        public async void InitLights()
        {
            if ((await createUser("hueapplication")) == true)
            {
                //lampen
                String jsonreturn = await nw.GetCommand("api/" + apikey + "/lights");
                JObject lampsjson = JObject.Parse(jsonreturn);
                foreach (var i in lampsjson)
                {
                    JObject lamp= JObject.Parse(lampsjson["" + i.Key].ToString());
                    JObject lampstate = JObject.Parse(lamp["state"].ToString());
                    lamps.Add(hue = new HueLamp(
                        this,
                        i.Key,
                        getKey(lampstate, "on"),
                        getKey(lampstate, "bri"),
                        getKey(lampstate, "hue"),
                        getKey(lampstate, "sat"),
                        "0",
                        "0",
                        getKey(lampstate, "ct"),
                        getKey(lampstate, "alert"),
                        getKey(lampstate, "effect"),
                        getKey(lampstate, "colormode"),
                        getKey(lampstate, "reachable"),
                        getKey(lamp, "type"),
                        getKey(lamp, "name"),
                        getKey(lamp, "modelid"),
                        getKey(lamp, "swversion")
                        ));
                    
                }

                //groepen
                jsonreturn = await nw.GetCommand("api/" + apikey + "/groups");
                foreach (var i in JObject.Parse(jsonreturn))
                {
                    string groupinformation = await nw.GetCommand("api/" + apikey + "/groups/" + i.Key);
                    JObject group = JObject.Parse(groupinformation);
                    JObject groupaction;
                    try {groupaction = JObject.Parse(group["action"].ToString()); } catch {groupaction = group; };
                    List<HueLamp> grouplamps = new List<HueLamp>();
                    foreach(var l in group["lights"])
                    {
                        foreach(HueLamp h in lamps)
                        {
                            if(h.id == l.ToString())
                                grouplamps.Add(h);
                        }
                    }
                    groups.Add(new HueGroup(
                        this, 
                        i.Key,
                        getKey(groupaction, "on"),
                        getKey(groupaction, "bri"),
                        getKey(groupaction, "hue"),
                        getKey(groupaction, "sat"),
                        "0",
                        "0",
                        getKey(groupaction, "ct"),
                        getKey(groupaction, "alert"),
                        getKey(groupaction, "effect"),
                        getKey(groupaction, "colormode"),
                        getKey(groupaction, "reachable"),
                        getKey(group, "name"),
                        grouplamps
                    ));           
                }
            };
        }