-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVEMConfigHelper.cs
More file actions
225 lines (201 loc) · 6.6 KB
/
Copy pathVEMConfigHelper.cs
File metadata and controls
225 lines (201 loc) · 6.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using BaseCommon;
namespace ModbusSample
{
public class VEMConfigHelper
{
private string filename = "";
private XDocument doc = null;
private VEMConfigHelper(string path)
{
filename = path;
doc = XDocument.Load(path);
}
private static VEMConfigHelper _VEMConfigHelper = null;
public static VEMConfigHelper Create()
{
if (_VEMConfigHelper == null)
{
_VEMConfigHelper = new VEMConfigHelper(System.Configuration.ConfigurationManager.AppSettings["VEMConfig"]);
}
return _VEMConfigHelper;
}
public void Save()
{
doc.Save(filename);
}
/*
public int GetNoteValue(bool defaultnote, string p, string key)
{
string value = doc.Root.Element(defaultnote ? "Default" : "Instance").Elements("Product").FirstOrDefault((n) =>
{
return n.Attribute("ID").Value == p;
}).Element(key).Value;
int intvalue = 0;
int.TryParse(value, out intvalue);
return intvalue;
}
*/
private string GetPort()
{
string value = doc.Root.Element("Default").Element("Com").Value;
return value;
}
private void SetPort(string com)
{
doc.Root.Element("Default").Element("Com").Value = com;
}
/*
public void SetNoteValue(bool defaultnote, string p, string key, object value)
{
doc.Root.Element(defaultnote ? "Default" : "Instance").Elements("Product").FirstOrDefault((n) =>
{
return n.Attribute("ID").Value == p;
}).Element(key).SetValue(value);
}
* */
/*
#region B
public int BChannelStart
{
get { return GetNoteValue(false, "B", "ChannelStart"); }
set { SetNoteValue(false, "B", "ChannelStart", value); }
}
public int BChannelEnd
{
get { return GetNoteValue(false, "B", "ChannelEnd"); }
set { SetNoteValue(false, "B", "ChannelEnd", value); }
}
public int BPerChannel
{
get { return GetNoteValue(false, "B", "PerChannel"); }
set { SetNoteValue(false, "B", "PerChannel", value); }
}
public int BCurrent
{
get { return GetNoteValue(false, "B", "Current"); }
set { SetNoteValue(false, "B", "Current", value); }
}
#endregion
#region A
public int AChannelStart
{
get { return GetNoteValue(false, "A", "ChannelStart"); }
set { SetNoteValue(false, "A", "ChannelStart", value); }
}
public int AChannelEnd
{
get { return GetNoteValue(false, "A", "ChannelEnd"); }
set { SetNoteValue(false, "A", "ChannelEnd", value); }
}
public int APerChannel
{
get { return GetNoteValue(false, "A", "PerChannel"); }
set { SetNoteValue(false, "A", "PerChannel", value); }
}
#endregion
*/
private int GetCurrent(string key)
{
return doc.Root.Descendants("Product").First((p) =>
{
return p.Attribute("ID").Value == key;
}).Attribute("Current").Value.TryInt();
}
private void SetCurrent(string key, int value)
{
doc.Root.Descendants("Product").First((p) =>
{
return p.Attribute("ID").Value == key;
}).Attribute("Current").Value = value.ToString();
}
public int ACurrent
{
get { return GetCurrent("A"); }
set { SetCurrent("A", value); }
}
public int BCurrent
{
get { return GetCurrent("B"); }
set { SetCurrent("B", value); }
}
public string Port
{
get { return GetPort(); }
set { SetPort(value); }
}
private List<ChannelInfo> GetProductChannels(string key)
{
List<ChannelInfo> list = new List<ChannelInfo>();
var eles = doc.Root.Descendants("Product").First((p) =>
{
return p.Attribute("ID").Value == key;
}).Descendants("Channel");
foreach (var item in eles)
{
ChannelInfo info = new ChannelInfo();
info.ID = item.Attribute("ID").Value.TryInt();
info.Count = item.Attribute("Count").Value.TryInt();
info.IndexS = item.Attribute("IndexS").Value.TryInt();
info.IndexE = item.Attribute("IndexE").Value.TryInt();
list.Add(info);
}
return list;
}
public List<ChannelInfo> GetProductAChannels()
{
return GetProductChannels("A");
}
public List<ChannelInfo> GetProductBChannels()
{
return GetProductChannels("B");
}
public void SetProductChannels(List<ChannelInfo> list, string key)
{
var index = 0;
list.ForEach((i) =>
{
i.IndexS = index;
i.IndexE = index + i.Count - 1;
index += i.Count;
});
doc.Root.Descendants("Product").First((px) =>
{
return px.Attribute("ID").Value == key;
}).Descendants("Channel").Remove();
var p = doc.Root.Descendants("Product").First((px) =>
{
return px.Attribute("ID").Value == key;
});
foreach (var item in list)
{
var Xe = new XElement("Channel");
Xe.SetAttributeValue("ID", item.ID);
Xe.SetAttributeValue("Count", item.Count);
Xe.SetAttributeValue("IndexS", item.IndexS);
Xe.SetAttributeValue("IndexE", item.IndexE);
p.Add(Xe);
}
Save();
}
public void SetProductAChannels(List<ChannelInfo> list)
{
SetProductChannels(list, "A");
}
public void SetProductBChannels(List<ChannelInfo> list)
{
SetProductChannels(list, "B");
}
}
public class ChannelInfo
{
public int ID { get; set; }
public int Count { get; set; }
public int IndexS { get; set; }
public int IndexE { get; set; }
}
}