private void ConfigSet(Config config) { this.rulesTextBox.Text = string.Join(Environment.NewLine, config.Rules.Select(p => string.Format(CultureInfo.InvariantCulture, "{0} = {1}", p.Key, p.Value))); this.intervalNumericUpDown.Value = (int)config.Interval.TotalSeconds; this.pointsTextBox.Text = string.Join(Environment.NewLine, config.Points.Select(t => string.Format(CultureInfo.InvariantCulture, "{0:00}:{1:00} = {2}", t.MinutesFromDayStart / 60, t.MinutesFromDayStart % 60, t.TempKelvin))); this.trayCheckBox.Checked = config.Tray; }
public MainForm(string configPath, bool minimize) { Config config; string message; this.InitializeComponent(); this.toolStripMenuItemName.Text += Assembly.GetExecutingAssembly().GetName().Version.ToString(2); this.Icon = Resources.AppIcon; this.rules = new Dictionary<string, Rule>(); this.configPath = configPath; this.minimize = minimize; this.points = new SortedList<int, Point>(); if (!File.Exists(configPath)) config = new Config(); else if (!Config.TryParse(File.ReadAllText(configPath), out config, out message)) { this.toolStripStatusLabel.Text = string.Format(CultureInfo.InvariantCulture, "Configuration error: {0}", message); config = new Config(); } this.ConfigSet(config); this.ModeConfigRadioCheckedChanged(this, new EventArgs()); }
private bool ConfigGet(out Config config, out string message) { if (!this.PointsGet(out message) || !this.RulesGet(out message)) { config = null; return false; } config = new Config(); config.Interval = TimeSpan.FromSeconds((int)this.intervalNumericUpDown.Value); config.Points = this.points.Values.ToArray(); config.Rules = this.rules; config.Tray = this.trayCheckBox.Checked; return true; }
public static bool TryParse(string json, out Config config, out string message) { JObject level0; JObject level1object; List<Point> points; Rule rule; Dictionary<string, Rule> rules; JToken token; try { level0 = JObject.Parse(json); } catch (Exception exception) { config = null; message = exception.Message; return false; } config = new Config(); if (level0.TryGetValue("interval", out token)) config.Interval = TimeSpan.FromSeconds(token.Value<int>()); if (level0.TryGetValue("points", out token) || level0.TryGetValue("times", out token)) { level1object = token as JObject; points = new List<Point>(); if (level1object != null) { foreach (var pair in level1object) { var match = Regex.Match(pair.Key, "^([0-9]{1,2}):([0-9]{1,2})$"); if (match.Success) { var time = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture) * 60 + int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture); points.Add(new Point(time, pair.Value.Value<int>())); } } } config.Points = points.OrderBy(o => o.MinutesFromDayStart).ToArray(); } if (level0.TryGetValue("rules", out token) || level0.TryGetValue("behaviors", out token)) { level1object = token as JObject; rules = new Dictionary<string, Rule>(); if (level1object != null) { foreach (var pair in level1object) { if (Enum.TryParse<Rule>(pair.Value.Value<string>(), true, out rule)) rules[pair.Key] = rule; } } config.Rules = rules; } if (level0.TryGetValue("tray", out token)) config.Tray = token.Value<bool>(); message = null; return true; }