예제 #1
0
        public static void LoadSupportedLights()
        {
            try
            {
                listAvailableLights = new Dictionary <string, Supportedlight>()
                {
                    { "default", defaultLightImages },
                };

                string[] listlightsfiles = Directory.GetFiles("lights", "*.lt");

                foreach (string file in listlightsfiles)
                {
                    string json = string.Empty;
                    try
                    {
                        TextReader reader = File.OpenText(file);
                        json = reader.ReadToEnd();
                        reader.Close();
                    }
                    catch (Exception ex)
                    {
                        log.Error($"Error reading file : {file}.", ex);
                    }

                    try
                    {
                        Supportedlight sl = JsonConvert.DeserializeObject <Supportedlight>(json,
                                                                                           new SupportedLightConverter(typeof(Supportedlight)));
                        log.Info($"Loaded supported light : {sl.name}, modelid : {sl.modelid}, type : {sl.type}");
                        listAvailableLights.Add(sl.modelid, sl);
                    }
                    catch (OverflowException ex)
                    {
                        log.Error($"Invalid max or min in file : {file}. Make sure the max and min are within allowed values.", ex);
                    }
                    catch (ArgumentNullException ex)
                    {
                        log.Error($"Missing tag in file : {file}. Make sure all the xml tags are present in the file.", ex);
                    }
                    catch (Exception ex)
                    {
                        log.Error($"Error reading file : {file}. Make sure the json is properly formatted.", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Error reading supported lights.", ex);
            }
        }
예제 #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject obj = (JObject)serializer.Deserialize(reader);

            Supportedlight supportedlight = new Supportedlight();


            if (obj["modelid"] != null)
            {
                supportedlight.modelid = obj["modelid"].Value <string>();
            }
            if (obj["name"] != null)
            {
                supportedlight.name = obj["name"].Value <string>();
            }
            if (obj["type"] != null)
            {
                supportedlight.type = obj["type"].Value <string>();
            }

            if (obj["hue"] != null)
            {
                if (obj["hue"]["max"] != null && obj["hue"]["min"] != null)
                {
                    supportedlight.hue = new MaxMin <ushort>()
                    {
                        max = obj["hue"]["max"].Value <ushort>(),
                        min = obj["hue"]["min"].Value <ushort>()
                    }
                }
                ;
            }

            if (obj["sat"] != null)
            {
                if (obj["sat"]["max"] != null && obj["sat"]["min"] != null)
                {
                    supportedlight.sat = new MaxMin <byte>()
                    {
                        max = obj["sat"]["max"].Value <byte>(),
                        min = obj["sat"]["min"].Value <byte>()
                    }
                }
                ;
            }

            if (obj["bri"] != null)
            {
                if (obj["bri"]["max"] != null && obj["bri"]["min"] != null)
                {
                    supportedlight.bri = new MaxMin <byte>()
                    {
                        max = obj["bri"]["max"].Value <byte>(),
                        min = obj["bri"]["min"].Value <byte>()
                    }
                }
                ;
            }

            if (obj["ct"] != null)
            {
                if (obj["ct"]["max"] != null && obj["ct"]["min"] != null)
                {
                    supportedlight.ct = new MaxMin <ushort>()
                    {
                        max = obj["hue"]["max"].Value <ushort>(),
                        min = obj["hue"]["min"].Value <ushort>()
                    }
                }
                ;
            }

            if (obj["img"] != null)
            {
                if (obj["img"]["on"] != null && obj["img"]["off"] != null && obj["img"]["unr"] != null)
                {
                    string path = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
                    supportedlight.img = new Dictionary <string, ImageSource>();

                    if (File.Exists(path + @"\lights\images\" + obj["img"]["on"].Value <string>()) &&
                        File.Exists(path + @"\lights\images\" + obj["img"]["off"].Value <string>()) &&
                        File.Exists(path + @"\lights\images\" + obj["img"]["unr"].Value <string>()))
                    {
                        supportedlight.img.Add("on",
                                               new BitmapImage(new Uri(path + @"\lights\images\" + obj["img"]["on"].Value <string>())));
                        supportedlight.img.Add("off",
                                               new BitmapImage(new Uri(path + @"\lights\images\" + obj["img"]["off"].Value <string>())));
                        supportedlight.img.Add("unr",
                                               new BitmapImage(new Uri(path + @"\lights\images\" + obj["img"]["unr"].Value <string>())));
                    }
                    else
                    {
                        supportedlight.img.Add("on", GDIManager.CreateImageSourceFromImage(Properties.Resources.DefaultLight_on));
                        supportedlight.img.Add("off", GDIManager.CreateImageSourceFromImage(Properties.Resources.DefaultLight_off));
                        supportedlight.img.Add("unr", GDIManager.CreateImageSourceFromImage(Properties.Resources.DefaultLight_unr));
                    }
                }
            }

            return(supportedlight);
        }