-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationConfig.cs
More file actions
240 lines (208 loc) · 9.52 KB
/
NotificationConfig.cs
File metadata and controls
240 lines (208 loc) · 9.52 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace WiFiManager
{
public class NotificationConfig
{
[JsonPropertyName("enableWindowsToast")]
public bool EnableWindowsToast { get; set; } = true;
[JsonPropertyName("enableGrowl")]
public bool EnableGrowl { get; set; } = true;
[JsonPropertyName("growlHosts")]
public List<GrowlHost> GrowlHosts { get; set; } = new()
{
new GrowlHost { Host = "127.0.0.1", Port = 23053, Enabled = true }
};
[JsonPropertyName("iconPath")]
public string? IconPath { get; set; }
[JsonPropertyName("connectionTimeout")]
public int ConnectionTimeout { get; set; } = 1000;
[JsonPropertyName("notificationPriorities")]
public Dictionary<string, int> NotificationPriorities { get; set; } = new()
{
{ "WiFi Connected", 0 },
{ "WiFi Disconnected", 2 },
{ "WiFi Changed", 0 },
{ "New WiFi Network", 0 },
{ "Signal Changed", -1 }
};
[JsonPropertyName("stickyNotifications")]
public Dictionary<string, bool> StickyNotifications { get; set; } = new()
{
{ "WiFi Connected", false },
{ "WiFi Disconnected", true },
{ "WiFi Changed", false },
{ "New WiFi Network", false },
{ "Signal Changed", false }
};
[JsonPropertyName("defaultSticky")]
public bool DefaultSticky { get; set; } = false;
// ── Signal-change thresholds ──────────────────────────────────────────────
/// <summary>
/// Minimum signal change (%) to print a console log line.
/// Default: 5
/// </summary>
[JsonPropertyName("signalLogThreshold")]
public int SignalLogThreshold { get; set; } = 5;
/// <summary>
/// Minimum signal change (%) on the ACTIVE (connected) network to send
/// a desktop notification. Must be >= signalLogThreshold.
/// Default: 10
/// </summary>
[JsonPropertyName("signalNotifyThreshold")]
public int SignalNotifyThreshold { get; set; } = 10;
/// <summary>
/// Minimum signal change (%) on BACKGROUND (not connected) networks to
/// print a console log line. No notification is sent for these.
/// Default: 15
/// </summary>
[JsonPropertyName("backgroundSignalLogThreshold")]
public int BackgroundSignalLogThreshold { get; set; } = 15;
// Track loaded config path
[JsonIgnore]
public string? LoadedFrom { get; private set; }
public static NotificationConfig Load(string? path = null)
{
List<string> validFiles;
if (!string.IsNullOrWhiteSpace(path))
{
validFiles = new List<string> { path };
}
else
{
var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var exeDir = AppContext.BaseDirectory;
validFiles = new List<string>
{
Path.Combine(userProfile, ".wifimgr", "wifimgr.json"),
Path.Combine(userProfile, ".wifimgr", "config.json"),
Path.Combine(appData, ".wifimgr", "wifimgr.json"),
Path.Combine(appData, ".wifimgr", "config.json"),
Path.Combine(exeDir, "wifimgr.json"),
Path.Combine(exeDir, "config.json"),
Path.Combine(exeDir, ".wifimgr", "wifimgr.json"),
Path.Combine(exeDir, ".wifimgr", "config.json"),
Path.Combine(exeDir, "config", "wifimgr.json"),
Path.Combine(exeDir, "config", "config.json")
};
}
string? configPath = null;
foreach (var file in validFiles)
{
Console.WriteLine($"[🔍] Try loading config from: {file}");
if (File.Exists(file))
{
configPath = file;
break;
}
}
if (configPath == null)
{
Console.WriteLine($"[⚠️] Config file not found, using default config.");
var defaultConfig = new NotificationConfig();
var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
defaultConfig.LoadedFrom = Path.Combine(userProfile, ".wifimgr", "wifimgr.json");
return defaultConfig;
}
try
{
string json = File.ReadAllText(configPath);
var config = JsonSerializer.Deserialize<NotificationConfig>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true
});
if (config != null)
{
// Clamp thresholds to sane ranges so a typo can't disable all alerts
config.SignalLogThreshold = Math.Clamp(config.SignalLogThreshold, 1, 50);
config.SignalNotifyThreshold = Math.Clamp(config.SignalNotifyThreshold, 1, 50);
config.BackgroundSignalLogThreshold = Math.Clamp(config.BackgroundSignalLogThreshold, 1, 50);
// Enforce logical ordering: notify threshold must be >= log threshold
if (config.SignalNotifyThreshold < config.SignalLogThreshold)
config.SignalNotifyThreshold = config.SignalLogThreshold;
config.LoadedFrom = configPath;
Console.WriteLine($"[✅] Config successfully loaded from: {configPath}");
Console.WriteLine($" - Windows Toast: {(config.EnableWindowsToast ? "ON" : "OFF")}");
Console.WriteLine($" - Growl: {(config.EnableGrowl ? "ON" : "OFF")}");
Console.WriteLine($" - Growl Hosts: {config.GrowlHosts.Count}");
Console.WriteLine($" - Signal log threshold: {config.SignalLogThreshold}%");
Console.WriteLine($" - Signal notify threshold: {config.SignalNotifyThreshold}%");
Console.WriteLine($" - BG signal log threshold: {config.BackgroundSignalLogThreshold}%");
return config;
}
Console.WriteLine($"[⚠️] Failed to parse config, using default config.");
return FallbackConfig();
}
catch (Exception ex)
{
Console.WriteLine($"[❌] Failed to load config: {ex.Message}");
return FallbackConfig();
}
}
private static NotificationConfig FallbackConfig()
{
var cfg = new NotificationConfig();
var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
cfg.LoadedFrom = Path.Combine(userProfile, ".wifimgr", "wifimgr.json");
return cfg;
}
public void Save(string? path = null)
{
if (string.IsNullOrWhiteSpace(path))
{
if (!string.IsNullOrWhiteSpace(LoadedFrom))
{
path = LoadedFrom;
}
else
{
var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var wifimgrDir = Path.Combine(userProfile, ".wifimgr");
if (!Directory.Exists(wifimgrDir))
Directory.CreateDirectory(wifimgrDir);
path = Path.Combine(wifimgrDir, "wifimgr.json");
}
}
try
{
var directory = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
Directory.CreateDirectory(directory);
var json = JsonSerializer.Serialize(this, new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
File.WriteAllText(path, json);
LoadedFrom = path;
Console.WriteLine($"[✅] Config saved to: {path}");
}
catch (Exception ex)
{
Console.WriteLine($"[❌] Failed to save config: {ex.Message}");
}
}
}
public class GrowlHost
{
[JsonPropertyName("host")]
public string Host { get; set; } = "127.0.0.1";
[JsonPropertyName("port")]
public int Port { get; set; } = 23053;
[JsonPropertyName("enabled")]
public bool Enabled { get; set; } = true;
[JsonPropertyName("name")]
public string? Name { get; set; }
public override string ToString()
{
var name = !string.IsNullOrEmpty(Name) ? $" ({Name})" : "";
return $"{Host}:{Port}{name}";
}
}
}