forked from EFLFE/PingoMeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
200 lines (163 loc) · 7.29 KB
/
Config.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
namespace PingoMeter
{
internal static class Config
{
private const string CONF_FILE_NAME = "config.txt";
public static int Delay = 3000;
public static int MaxPing;
public static Pen BgColor;
public static Pen GoodColor;
public static Pen NormalColor;
public static Pen BadColor;
public static bool RunOnStartup; // TODO RunOnStartup
private static string ipName;
private static IPAddress ipAddress;
public static IPAddress TheIPAddress
{
get => ipAddress;
set
{
ipAddress = value;
ipName = value.ToString();
}
}
public static string GetIPName => ipName;
public static bool AlarmConnectionLost;
public static bool AlarmTimeOut;
public static bool AlarmResumed;
/// <summary> Use numbers for the ping instead of a graph. </summary>
public static bool UseNumbers;
static Config() => Reset();
public static void SetAll(int delay, int maxPing, Color bgColor, Color goodColor, Color normalColor,
Color badColor, bool runOnStartup, IPAddress address,
bool alarmConnectionLost, bool alarmTimeOut, bool alarmResumed, bool useNumbers)
{
Delay = delay;
MaxPing = maxPing;
BgColor = new Pen(bgColor);
GoodColor = new Pen(goodColor);
NormalColor = new Pen(normalColor);
BadColor = new Pen(badColor);
RunOnStartup = runOnStartup;
TheIPAddress = address;
AlarmConnectionLost = alarmConnectionLost;
AlarmTimeOut = alarmTimeOut;
AlarmResumed = alarmResumed;
UseNumbers = useNumbers;
}
public static void Reset()
{
Delay = 3000;
MaxPing = 250;
BgColor = new Pen(Color.FromArgb(70, 0, 0));
GoodColor = new Pen(Color.FromArgb(120, 180, 0));
NormalColor = new Pen(Color.FromArgb(255, 180, 0));
BadColor = new Pen(Color.FromArgb(255, 0, 0));
RunOnStartup = true;
TheIPAddress = IPAddress.Parse("8.8.8.8"); // google ip
AlarmConnectionLost = false;
AlarmTimeOut = false;
AlarmResumed = false;
UseNumbers = false;
}
public static void Load()
{
if (File.Exists(CONF_FILE_NAME))
{
string[] conf = File.ReadAllLines(CONF_FILE_NAME);
for (int i = 0; i < conf.Length; i++)
{
if (string.IsNullOrWhiteSpace(conf[i]) || conf[i].Trim()[0] == '#')
continue;
string line = conf[i].Trim();
string[] split = line.Split(new char[] { ' ' }, 2);
if (split.Length == 2)
{
switch (split[0])
{
case nameof(Delay):
int.TryParse(split[1], out Delay);
break;
case nameof(MaxPing):
int.TryParse(split[1], out MaxPing);
break;
case nameof(BgColor):
SetPenFromString(ref BgColor, split[1]);
break;
case nameof(GoodColor):
SetPenFromString(ref GoodColor, split[1]);
break;
case nameof(NormalColor):
SetPenFromString(ref NormalColor, split[1]);
break;
case nameof(BadColor):
SetPenFromString(ref BadColor, split[1]);
break;
case nameof(RunOnStartup):
bool.TryParse(split[1], out RunOnStartup);
break;
case "ipaddress":
case nameof(TheIPAddress):
if (IPAddress.TryParse(split[1], out IPAddress ip))
TheIPAddress = ip;
break;
case nameof(AlarmConnectionLost):
bool.TryParse(split[1], out AlarmConnectionLost);
break;
case nameof(AlarmTimeOut):
bool.TryParse(split[1], out AlarmTimeOut);
break;
case nameof(AlarmResumed):
bool.TryParse(split[1], out AlarmResumed);
break;
case nameof(UseNumbers):
bool.TryParse(split[1], out UseNumbers);
break;
}
}
}
}
else
{
Reset();
Save();
}
}
private static void SetPenFromString(ref Pen pen, string str)
{
if (str.IndexOf(':') != -1)
{
string[] rgb = str.Split(':');
if (rgb.Length == 3)
{
if (int.TryParse(rgb[0], out int r) && r > -1 && r < 256 &&
int.TryParse(rgb[1], out int g) && g > -1 && g < 256 &&
int.TryParse(rgb[2], out int b) && b > -1 && b < 256)
pen = new Pen(Color.FromArgb(r, g, b));
}
}
}
public static void Save()
{
var sb = new StringBuilder();
sb.AppendLine("# PingoMeter config file");
sb.AppendLine($"{nameof(Delay)} {Delay}");
sb.AppendLine($"{nameof(MaxPing)} {MaxPing}");
sb.AppendLine($"{nameof(BgColor)} {BgColor.Color.R}:{BgColor.Color.G}:{BgColor.Color.B}");
sb.AppendLine($"{nameof(GoodColor)} {GoodColor.Color.R}:{GoodColor.Color.G}:{GoodColor.Color.B}");
sb.AppendLine($"{nameof(NormalColor)} {NormalColor.Color.R}:{NormalColor.Color.G}:{NormalColor.Color.B}");
sb.AppendLine($"{nameof(BadColor)} {BadColor.Color.R}:{BadColor.Color.G}:{BadColor.Color.B}");
sb.AppendLine($"{nameof(RunOnStartup)} {RunOnStartup}");
sb.AppendLine($"{nameof(TheIPAddress)} {TheIPAddress.ToString()}");
sb.AppendLine($"{nameof(AlarmConnectionLost)} {AlarmConnectionLost}");
sb.AppendLine($"{nameof(AlarmTimeOut)} {AlarmTimeOut}");
sb.AppendLine($"{nameof(AlarmResumed)} {AlarmResumed}");
sb.AppendLine($"{nameof(UseNumbers)} {UseNumbers}");
File.WriteAllText(CONF_FILE_NAME, sb.ToString());
}
}
}