-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonReadWrite.cs
More file actions
192 lines (156 loc) · 5.06 KB
/
Copy pathjsonReadWrite.cs
File metadata and controls
192 lines (156 loc) · 5.06 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
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO;
using Newtonsoft.Json;
namespace jsFetch
{
#region Build json object
public class Key
{
public ushort Number { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Command { get; set; }
public string Test { get; set; }
}
public class jsonObject
{
public List<Key> Config { get; set; }
}
#endregion
#region Event arguments for returnItemValue
public class itemValue : EventArgs
{
public ushort Number { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Command { get; set; }
}
#endregion
public class jsonReadWrite
{
jsonObject data = new jsonObject();
string fileData = "{\"Config\":[{\"Number\":1,\"Name\":\"None\",\"Command\":\"None\",\"Description\":\"Please check file path.\"}]}"; //Put somthing in fileData for default output
string filePath = "";
#region Read json config file
public void Initialize( string fileLocation)
{
filePath = fileLocation;
CrestronConsole.PrintLine("[jsonReadWrite] Attempting to access configuration file. \r\n");
if (File.Exists(filePath))
{
fileData = File.ReadToEnd(filePath, Encoding.ASCII);
CrestronConsole.PrintLine("[jsonReadWrite] Config file Found, Reading... \r\n");
}
else
CrestronConsole.PrintLine("[jsonReadWrite] Config file not found, loading default. Check your file path. \r\n");
DeserializeJson();
}
public void DeserializeJson()
{
try
{
if (fileData != "" && fileData != null)
{
data = JsonConvert.DeserializeObject<jsonObject>(fileData);
CrestronConsole.PrintLine("File Read... \r\n");
}
else
{
ErrorLog.Error("Nothing to deserialize");
CrestronConsole.PrintLine("File is Empty... \r\n");
}
}
catch (Exception e)
{
ErrorLog.Error("Error Converting file contents of jsonObject = {0}", e.Message);
}
}
public ushort GetNumberOfNames()
{
ushort number = (ushort)data.Config.Count;
return number;
}
#endregion
#region Fetch json item values
public event EventHandler<itemValue> returnItemValue;
public void fetchName(string searchName)
{
itemValue value = new itemValue();
if (data.Config != null)
{
foreach (Key item in data.Config)
{
value.Number = item.Number;
value.Name = item.Name;
value.Description = item.Description;
value.Command = item.Command;
if (value.Name == searchName)
{
if (returnItemValue != null)
{
returnItemValue(this, value);
}
}
}
}
}
#endregion
#region Add a name to json config file
public void AddName(string _Name, string _Command, string _Description)
{
ushort count = GetNumberOfNames();
if (count + 1 <= 150)
{
data.Config.Add(new Key()
{
Number = (ushort)(count + 1),
Name = _Name,
Description = _Description,
Command = _Command,
});
UpdateJSON();
}
else
ErrorLog.Error("JSON file to large");
}
#endregion
#region Remove a name from json config file
public void RemoveName()
{
ushort count = GetNumberOfNames();
if (count - 1 >= 0)
{
data.Config.RemoveAt(count - 1);
UpdateJSON();
}
else
CrestronConsole.PrintLine("[jsonReadWrite] JSON file empty. \r\n");
}
#endregion
#region Save json file
public void UpdateJSON()
{
string jsonToSave;
FileStream saveToFile;
saveToFile = new FileStream(filePath, FileMode.Create);
try
{
jsonToSave = JsonConvert.SerializeObject(data);
saveToFile.Write(jsonToSave, Encoding.ASCII);
}
catch (Exception e)
{
CrestronConsole.PrintLine("Error saving Contacts to File, Reason: {0}", e.Message);
}
finally
{
saveToFile.Close();
}
}
#endregion
}
}