-
Notifications
You must be signed in to change notification settings - Fork 8
/
Settings.cs
147 lines (132 loc) · 4.24 KB
/
Settings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
namespace huecli
{
public class SettingsModel
{
public string version { get; set; }
public List<Dictionary<string, string>> hubs { get; set; }
}
public class Settings
{
public string SettingsFile { get; set; }
public SettingsModel SettingsDictionary { get; set; }
private JSchema SettingsSchema { get; set; }
public Settings(string file = null)
{
SettingsSchema = JSchema.Parse(@"{
'version': '1',
'hubs': {'type': 'array'}
}");
if (file == null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
SettingsFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+"\\huecli-settings.json";
}
else
{
SettingsFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+"/huecli-settings.json";
}
}
else
{
SettingsFile = file;
}
this.Load();
}
private void InitSettings()
{
SettingsDictionary = new SettingsModel();
SettingsDictionary.version = "1";
SettingsDictionary.hubs = new List<Dictionary<string, string>>();
}
public void Load()
{
if (!File.Exists(SettingsFile))
{
File.Create(SettingsFile).Close();
this.InitSettings();
}
else
{
string contents = File.ReadAllText(SettingsFile);
if (contents.StartsWith("{") && contents.EndsWith("}"))
{
JObject settingsObject = JObject.Parse(contents);
if (settingsObject.IsValid(this.SettingsSchema))
{
SettingsDictionary = JsonConvert.DeserializeObject<SettingsModel>(contents);
}
else
{
this.InitSettings();
}
}
else
{
this.InitSettings();
}
}
}
public void Save()
{
File.WriteAllText(SettingsFile, JsonConvert.SerializeObject(SettingsDictionary));
}
public List<Dictionary<string, string>> GetHubs()
{
return SettingsDictionary.hubs;
}
public void AddHub(Dictionary<string, string> hub)
{
for (int i = 0; i < SettingsDictionary.hubs.Count; i++)
{
if (SettingsDictionary.hubs[i]["alias"] == hub["alias"] || SettingsDictionary.hubs[i]["localipaddress"] == hub["localipaddress"])
{
return;
}
}
SettingsDictionary.hubs.Add(hub);
}
public string GetUsername(String hubAlias)
{
for (int i = 0; i < SettingsDictionary.hubs.Count; i++)
{
if (hubAlias == SettingsDictionary.hubs[i]["alias"])
{
return SettingsDictionary.hubs[i]["username"];
}
}
return "";
}
public string GetIPAddress(String hubAlias)
{
for (int i = 0; i < SettingsDictionary.hubs.Count; i++)
{
if (hubAlias == SettingsDictionary.hubs[i]["alias"])
{
return SettingsDictionary.hubs[i]["localipaddress"];
}
}
return "";
}
public void RemoveHub(String hubAlias)
{
for (int i = 0; i < SettingsDictionary.hubs.Count; i++)
{
if (hubAlias == SettingsDictionary.hubs[i]["alias"])
{
SettingsDictionary.hubs.RemoveAt(i);
break;
}
}
}
}
}