-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cs
More file actions
325 lines (316 loc) · 12.7 KB
/
Copy pathConfiguration.cs
File metadata and controls
325 lines (316 loc) · 12.7 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using Newtonsoft.Json;
using System.Globalization;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json.Converters;
namespace devm0n
{
public class Configuration
{
GlobalConfiguration _global = new GlobalConfiguration();
DeviceConfiguration[] _device = new DeviceConfiguration[0];
Dictionary<string,GroupConfiguration> _group = new Dictionary<string, GroupConfiguration>();
public GlobalConfiguration Global { get { return _global; } set { _global = value; } }
public DeviceConfiguration[] Devices { get { return _device; }set { _device = value; } }
public Dictionary<string,GroupConfiguration> Groups { get {return _group; } set { _group = value; } }
public Configuration()
{
_global = new GlobalConfiguration();
_device = new DeviceConfiguration[0];
_group = new Dictionary<string, GroupConfiguration>();
}
public Configuration(string GlobalPath, string DevicePath, string GroupPath)
{
try {
if (File.Exists(GlobalPath))
{
try {
//_global = JsonSerializer.Deserialize<GlobalConfiguration>(File.ReadAllText(GlobalPath), new JsonSerializerOptions() { WriteIndented = true, Converters = { new TimeSpanConverter() } });
_global = JsonConvert.DeserializeObject<GlobalConfiguration>(File.ReadAllText(GlobalPath));
}
catch(Exception _ex)
{
throw new FileLoadException("Failed to load Global configuration, see inner exception for details.", _ex);
}
}
else
throw new FileNotFoundException("Global configuration path does not exist.");
if (File.Exists(DevicePath))
{
try {
//_device = JsonSerializer.Deserialize<DeviceConfiguration[]>(File.ReadAllText(DevicePath), new JsonSerializerOptions() { WriteIndented = true, Converters = { new TimeSpanConverter() } });
_device = JsonConvert.DeserializeObject<DeviceConfiguration[]>(File.ReadAllText(DevicePath));
}
catch(Exception _ex)
{
throw new FileLoadException("Failed to load Device configuration, see inner exception for details.", _ex);
}
}
else
throw new FileNotFoundException("Device configuration path does not exist.");
if (File.Exists(GroupPath))
{
try {
_group = JsonConvert.DeserializeObject<Dictionary<string,GroupConfiguration>>(File.ReadAllText(GroupPath));
//_group = JsonSerializer.Deserialize<Dictionary<string,GroupConfiguration>>(File.ReadAllText(GroupPath), new JsonSerializerOptions() { WriteIndented = true, Converters = { new TimeSpanConverter() } });
}
catch(Exception _ex)
{
throw new FileLoadException("Failed to load Group configuration, see inner exception for details.", _ex);
}
}
else
throw new FileNotFoundException("Group configuration path does not exist.");
}
catch(Exception _ex) { throw new Exception("Failed to load configuration, see inner exception for details.",_ex); }
}
public string BuildGlobalDefault()
{
Global.Smtp.Address = $"smtp.server.com";
Global.Smtp.Port = 25;
Global.Smtp.UseAuth = false;
Global.Smtp.UseSSL = false;
Global.Smtp.EmailAddress = "from@address.com";
Global.Smtp.Credential.Username = "username@maybe.com";
Global.Smtp.Credential.Password = "password";
Global.SendGrid.ApiKey = $"SENDGRID_API_KEY";
Global.SendGrid.EmailAddress = $"from@address.com";
Global.Twilio.AccountSid = $"TWILIO_ACCOUNT_SID";
Global.Twilio.AuthToken = $"TWILIO_AUTH_TOKEN";
Global.Twilio.PhoneNumber = $"+15615550100";
try { return JsonConvert.SerializeObject(Global,Formatting.Indented); }
catch { return null; }
}
public string BuildDeviceDefault()
{
Devices = new DeviceConfiguration[] {
new DeviceConfiguration() {
Name = "Example 1",
Address = $"0.0.0.0",
Port = 80,
File = "stateFull.xml",
UseSSL = false,
PollInterval = new PollInterval(),
Fields = new Dictionary<string, DeviceFieldConfiguration>() {
{
"input0state", new DeviceFieldConfiguration() {
DisplayName = "Display Name for State 0",
Enabled = true,
Group = "Group1"
}
},
{
"input1state", new DeviceFieldConfiguration() {
DisplayName = "Display Name for State 1",
Enabled = true,
Group = "Group1"
}
},
}
},
new DeviceConfiguration() {
Name = "Example 2",
Address = $"1.1.1.1",
Port = 443,
File = "state.xml",
UseSSL = true,
PollInterval = new PollInterval(),
Fields = new Dictionary<string, DeviceFieldConfiguration>() {
{
"input0state", new DeviceFieldConfiguration() {
DisplayName = "Display Name for State 0",
Enabled = true,
Group = "Group1"
}
},
{
"input1state", new DeviceFieldConfiguration() {
DisplayName = "Display Name for State 1",
Enabled = true,
Group = "Group1"
}
},
}
}
};
try { return JsonConvert.SerializeObject(Devices,Formatting.Indented); }
catch { return null; }
}
public string BuildGroupDefault()
{
Groups = new Dictionary<string, GroupConfiguration>() {
{
"Group1",
new GroupConfiguration()
{
Enabled = true,
NotificationMethods = new NotificationMethodConfiguration[]
{
new NotificationMethodConfiguration() {
Type = NotificationType.SENDGRID,
Enabled = true,
Address = "to@address.com"
},
new NotificationMethodConfiguration() {
Type = NotificationType.SMTP,
Enabled = false,
Address = "to@address.com"
}
}
}
},
{
"Group2",
new GroupConfiguration()
{
Enabled = true,
NotificationMethods = new NotificationMethodConfiguration[]
{
new NotificationMethodConfiguration() {
Type = NotificationType.TWILIO,
Enabled = true,
Address = "+15615551212"
},
new NotificationMethodConfiguration() {
Type = NotificationType.TWILIO,
Enabled = false,
Address = "+15615557777"
}
}
}
}
};
try { return JsonConvert.SerializeObject(Groups, Formatting.Indented); }
catch { return null; }
}
}
public class GlobalConfiguration
{
public SmtpConfiguration Smtp {get; set;}
public SendGridConfiguration SendGrid { get; set; }
public TwilioConfiguration Twilio { get; set; }
public GlobalConfiguration()
{
Smtp = new SmtpConfiguration();
SendGrid = new SendGridConfiguration();
Twilio = new TwilioConfiguration();
}
}
public class DeviceConfiguration
{
public bool Enabled {get; set;}
public string Name {get; set;}
public string Address { get; set; }
public string File {get; set; }
public int Port {get; set;}
public bool UseSSL {get;set;}
public PollInterval PollInterval { get; set; }
public Dictionary<string,DeviceFieldConfiguration> Fields { get; set; }
public DeviceConfiguration()
{
Enabled = false;
Name = string.Empty;
Address = string.Empty;
File = string.Empty;
PollInterval = new PollInterval();
Fields = new Dictionary<string,DeviceFieldConfiguration>();
}
public string GetRequestUrl()
{
StringBuilder _builder = new StringBuilder();
if (UseSSL)
_builder.Append($"https");
else
_builder.Append($"http");
_builder.Append($"://");
_builder.Append($"{Address}:{Port}");
_builder.Append($"/{File}");
return _builder.ToString();
}
}
public class SmtpConfiguration
{
public string Address {get; set;}
public int Port {get; set;}
public bool UseAuth {get; set;}
public bool UseSSL {get; set;}
public string EmailAddress {get; set;}
public SmtpCredentialConfiguration Credential {get; set;}
public SmtpConfiguration()
{
Address = string.Empty;
EmailAddress = string.Empty;
Credential = new SmtpCredentialConfiguration();
}
}
public class SmtpCredentialConfiguration
{
public string Username {get;set;}
public string Password {get;set;}
public SmtpCredentialConfiguration()
{
Username = string.Empty;
Password = string.Empty;
}
}
public class SendGridConfiguration
{
public string ApiKey { get; set; }
public string EmailAddress { get; set; }
public SendGridConfiguration()
{
ApiKey = string.Empty;
EmailAddress = string.Empty;
}
}
public class TwilioConfiguration {
public string AccountSid { get; set; }
public string AuthToken { get; set; }
public string PhoneNumber { get; set; }
public TwilioConfiguration()
{
AccountSid = string.Empty;
AuthToken = string.Empty;
PhoneNumber = string.Empty;
}
}
public class DeviceFieldConfiguration
{
public string DisplayName {get; set;}
public bool Enabled {get; set;}
public string Group {get; set;}
public DeviceFieldConfiguration()
{
DisplayName = string.Empty;
Group = string.Empty;
}
}
public class GroupConfiguration
{
public bool Enabled {get; set;}
public NotificationMethodConfiguration[] NotificationMethods {get; set;}
public GroupConfiguration()
{
NotificationMethods = new NotificationMethodConfiguration[0];
}
}
public class NotificationMethodConfiguration
{
[JsonConverter(typeof(StringEnumConverter))]
public NotificationType Type { get; set; }
public string Address { get; set; }
public bool Enabled { get; set; }
public NotificationMethodConfiguration()
{
Address = string.Empty;
}
}
public enum NotificationType
{
SENDGRID,
TWILIO,
SMTP
}
}