-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cs
167 lines (141 loc) · 5.79 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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Win32;
namespace JNetchecker
//Class for saving/reading configuration settings
{
public static class Config
{
public static Dictionary<string, bool> configValuesType = new Dictionary<string, bool>(); //janky method of marking the expected value as int or char. TRUE = INT, FALSE =CHAR
public static Dictionary<string, string> configValues = new Dictionary<string, string>();
// List<host> hosts = new List<host>();
public static void testConfigModule(List<host> hosts)
{
//default vaulues
// hosts[1].timesSeen = 69;
}
public static void writeConfig()
{
string jsonFilepath = @"c:\temp\netcheck.json";
string databaseFilepath = SelectDatabaseFile();
string logFilepath = SelectLogFile();
if (databaseFilepath == "MISSING" || logFilepath == "MISSING")
{
MessageBox.Show("Error 9: Invalid file path provided.");
return;
}
//string filepath = @"c:\temp\netcheck.json";
ConfigurationValues cv = new ConfigurationValues() ;
cv.TemperatureCelsius = 69;
cv.DatabaseFilepath = databaseFilepath;
cv.RefreshDurationinHours = 1;
cv.LoggerFilepath = logFilepath;
DateTime sourceTime = DateTime.Now;
cv.ConfigurationDate = new DateTime(sourceTime.Year, sourceTime.Month, sourceTime.Day, sourceTime.Hour, sourceTime.Minute, 0);
var options = new JsonSerializerOptions
{
WriteIndented = true,
};
//outputs the JSON file with user friendly indentation
string jsonString = JsonSerializer.Serialize(cv,options);
File.WriteAllText(jsonFilepath, jsonString);
JLogger.WriteGenericLogFile(System.Security.Principal.WindowsIdentity.GetCurrent().Name + "\twrote new config file");
}
public static ConfigurationValues readConfig()
{
//19/04/21 reads the configuration file with a hardcoded location
string filepath = @"c:\temp\netcheck.json";
string jsonString = File.ReadAllText(filepath);
ConfigurationValues cv = new ConfigurationValues();
cv = JsonSerializer.Deserialize<ConfigurationValues>(jsonString);
return cv;
}
public static string SelectDatabaseFile()
{
//gets the filepath for the database
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Database files (*.db)|*.db|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
string filepath = (openFileDialog.FileName); //should get file path from dialogue box, send to loadCsvFile subroutine.
return filepath;
}
//todo: error action
return "MISSING";
}
public static string SelectLogFile()
{
//gets the filepath for the logs
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
string filepath = (openFileDialog.FileName); //should get file path from dialogue box, send to loadCsvFile subroutine.
return filepath;
}
//todo: error action
return "MISSING";
}
public static List<host> getHostNames()
//this can probably be removed 20/04/21 as the CSV import works better
{
OpenFileDialog openFileDialog = new OpenFileDialog();
string textFilepath = "";
if (openFileDialog.ShowDialog() == true)
{
textFilepath = (openFileDialog.FileName);
}
List<host> hosts = new List<host>();
if (File.Exists(textFilepath))
{
// Read a text file line by line.
string[] lines = File.ReadAllLines(textFilepath);
for (int i = 0; i < lines.Length; i++)
{
hosts.Add(new host() { hostname = lines[i] });
//parts.Add(new Part() {PartName="crank arm", PartId=1234});
}
return hosts;
}
//todo: return some sort of failure
return hosts;
// ValidateValues();
}
public static void addNewHost(string newHostName, List<host> hosts)
{
//this can probably be removed 20/04/21
OpenFileDialog openFileDialog = new OpenFileDialog();
string textFilepath = "";
if (openFileDialog.ShowDialog() == true)
{
textFilepath = (openFileDialog.FileName);
}
if (File.Exists(textFilepath))
{
// Read a text file line by line.
string[] lines = File.ReadAllLines(textFilepath);
using (StreamWriter file =
new StreamWriter(textFilepath, true))
{
file.WriteLine(newHostName);
//adds new host to end
}
}
}
}
}
public class ConfigurationValues
{
public int TemperatureCelsius { get; set; }
public int RefreshDurationinHours { get; set; }
public string DatabaseFilepath { get; set; }
public string LoggerFilepath { get; set; }
public DateTime ConfigurationDate { get; set; }
}