-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExtendedEtsSupport.cs
203 lines (194 loc) · 9.59 KB
/
ExtendedEtsSupport.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
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
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Xml;
using OpenKNXproducer;
static class ExtendedEtsSupport
{
static readonly Dictionary<DefineContent, Dictionary<string, string>> sParameterInfo = new();
public static string ParameterInfo
{
get
{
string lResult = "\nvar uctChannelParams = {";
foreach (var lEntry in sParameterInfo)
{
Dictionary<string, string> lModuleParams = lEntry.Value;
lResult += $"\n\"{lEntry.Key.prefix}\": {{";
foreach (var lModuleParam in lModuleParams)
{
lResult += $"\n {lModuleParam.Value},";
}
lResult = lResult[..^1] + "},";
}
lResult = lResult[..^1] + "\n};";
return lResult;
}
}
private static bool GenerateModuleSelector(ProcessInclude iInclude, int iApplicationVersion, int iApplicationNumber)
{
XmlNode lModuleSelector = iInclude.CreateElement("ParameterType", "Id", "%AID%_PT-ModuleSelector", "Name", "ModuleSelector");
XmlNode lTypeRestriction = iInclude.CreateElement("TypeRestriction", "Base", "Value", "SizeInBit", "8");
lTypeRestriction.AppendChild(iInclude.CreateElement("Enumeration", "Text", "Bitte wählen...", "Value", "255", "Id", "%ENID%"));
XmlNode lModuleSelectorCopy = iInclude.CreateElement("ParameterType", "Id", "%AID%_PT-ModuleSelectorWithChannels", "Name", "ModuleSelectorWithChannels");
lModuleSelector.AppendChild(lTypeRestriction);
XmlNode lTypeRestrictionCopy = iInclude.CreateElement("TypeRestriction", "Base", "Value", "SizeInBit", "8");
lTypeRestrictionCopy.AppendChild(iInclude.CreateElement("Enumeration", "Text", "Bitte wählen...", "Value", "255", "Id", "%ENID%"));
lModuleSelectorCopy.AppendChild(lTypeRestrictionCopy);
int lCount = 0;
string lVersionInformation = $"\nvar uctVersionInformation = [0x{iApplicationNumber:X}, 0x{iApplicationVersion:X}];";
string lModuleOrder = "\nvar uctModuleOrder = [";
XmlNodeList lChannels = iInclude.SelectNodes("//ApplicationProgram/Dynamic/Channel");
IEnumerator lIterator = lChannels.GetEnumerator();
lIterator.Reset();
foreach (var lEntry in sParameterInfo)
{
string lText = lEntry.Key.prefix;
if (lEntry.Key.ConfigTransferName != "")
lText = lEntry.Key.ConfigTransferName;
// if (lText == "BASE")
// lText = "Allgemein";
// else
for (int lIndex = 0; lIndex < lChannels.Count; lIndex++)
{
if (!lIterator.MoveNext())
{
lIterator.Reset();
lIterator.MoveNext();
}
XmlNode lChannel = (XmlNode)lIterator.Current;
if (lChannel.NodeAttr("Name").Contains(lText))
{
lText = lChannel.NodeAttr("Text");
break;
}
}
if (lEntry.Key.NumChannels > 0)
lTypeRestrictionCopy.AppendChild(iInclude.CreateElement("Enumeration", "Text", lText, "Value", lCount.ToString(), "Id", "%ENID%"));
lTypeRestriction.AppendChild(iInclude.CreateElement("Enumeration", "Text", lText, "Value", lCount++.ToString(), "Id", "%ENID%"));
lModuleOrder += "\"" + lEntry.Key.prefix + "\",";
}
lModuleOrder = lModuleOrder[..^1] + "];\n";
XmlNode lNode = iInclude.SelectSingleNode("//ApplicationProgram/Static/ParameterTypes");
lNode?.InsertAfter(lModuleSelector, null);
lNode?.InsertAfter(lModuleSelectorCopy, lModuleSelector);
if (lNode != null)
{
lNode = iInclude.SelectSingleNode("//ApplicationProgram/Static/Script");
if (lNode != null)
lNode.InnerText = lVersionInformation + lModuleOrder + lNode.InnerText;
}
return lNode != null;
}
public static bool AddEtsExtensions(ProcessInclude iInclude, int iApplicationVersion, int iApplicationNumber)
{
if (!DefineContent.WithConfigTransfer)
return false;
XmlNode lScript = iInclude.SelectSingleNode("//ApplicationProgram/Static/Script");
if (lScript == null)
return false;
lScript.InnerText = ParameterInfo + "\n\n" + lScript.InnerText;
// return true;
return GenerateModuleSelector(iInclude, iApplicationVersion, iApplicationNumber);
}
/// <summary>
/// Generate an array of field names for ETS extensions like channel copy
/// </summary>
public static void GenerateScriptContent(ProcessInclude iInclude, DefineContent iDefine)
{
if (iDefine.NoConfigTransfer)
return;
if (iDefine.template == "" && iDefine.share == "")
return; // pre-v1
Dictionary<string, string> lDict;
XmlNodeList lParameters = iInclude.SelectNodes("//ApplicationProgram/Static/Parameters//Parameter");
string lSuffix = "share";
string lDelimiter = "";
StringBuilder lParameterNames = new();
StringBuilder lParameterDefaults = new();
if (sParameterInfo.ContainsKey(iDefine))
lDict = sParameterInfo[iDefine];
else
{
lDict = new();
sParameterInfo.Add(iDefine, lDict);
// add version information, if available
if (iDefine.NumChannels > 0)
lDict.Add("channels", $"\"channels\": {iDefine.NumChannels}");
if (iDefine.VerifyVersion > 0)
lDict.Add("version", $"\"version\": 0x{iDefine.VerifyVersion:X}");
}
if (iDefine.template.EndsWith(Path.GetFileName(iInclude.XmlFileName)))
lSuffix = "templ";
lParameterNames.Append('[');
lParameterDefaults.Append('[');
foreach (XmlNode lNode in lParameters)
{
string lAccess = lNode.NodeAttr("Access");
if (lAccess != "None" && lAccess != "Read")
{
XmlNode lType = ProcessInclude.ParameterType(lNode.NodeAttr("ParameterType"), false);
string lTypeName = "";
if (lType != null) lTypeName = lType.Name;
// determine name
XmlNodeList lParameterRefs = iInclude.SelectNodes($"//ApplicationProgram/Static/ParameterRefs/ParameterRef[@RefId='{lNode.NodeAttr("Id")}']");
foreach (XmlNode lParameterRef in lParameterRefs)
{
string lName = lNode.NodeAttr("Name");
if (lName.Contains("%CC%") || lName.Contains("%CCC%"))
{
Program.Message(true, "Name {0} has wrong format, just %C% allowed", lName);
lName = "";
}
if (lName.Contains('~'))
{
Program.Message(true, "Name {0} contains not allowed character '~'", lName);
lName = "";
}
lName = lName.Replace("%C%", "~");
string lDefault = lNode.NodeAttr("Value");
lDefault = lParameterRef.NodeAttr("Value", lDefault); // ParameterRef default has priority
_ = int.TryParse(lParameterRef.NodeAttr("Id")[^2..], out int lRefSuffix);
if (lParameterRefs.Count > 1 || (lParameterRefs.Count == 1 && lRefSuffix != 1))
lName = $"{lName}:{lRefSuffix}";
// determine default
if (lDefault.StartsWith('%') && lDefault.EndsWith('%'))
lDefault = null;
else if ("TypeNumber,TypeRestriction".Contains(lTypeName))
{
_ = Int64.TryParse(lDefault, out long lDefaultInt);
lDefault = lDefaultInt.ToString();
}
else if (lTypeName == "TypeFloat")
{
_ = double.TryParse(lDefault, NumberStyles.Float, CultureInfo.InvariantCulture, out double lDefaultFloat);
lDefault = lDefaultFloat.ToString(CultureInfo.InvariantCulture);
}
else if (lTypeName == "TypeColor")
{
if (lDefault.Length == 7 && lDefault.StartsWith('#')) lDefault = lDefault[1..];
if (lDefault.Length == 6) lDefault = "FF" + lDefault;
_ = int.TryParse(lDefault, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int lDefaultHex);
lDefault = lDefaultHex.ToString();
}
else if (lTypeName == "TypePicture")
lDefault = "";
else
lDefault = $"\"{lDefault}\"";
if (lName != "" && lDefault != "")
{
lParameterNames.Append($"{lDelimiter}\"{lName}\"");
lParameterDefaults.Append($"{lDelimiter}{lDefault}");
lDelimiter = ",";
}
}
}
}
lParameterNames.Append(']');
lParameterDefaults.Append(']');
string lOutput = $"\"{lSuffix}\": {{\n \"names\": {lParameterNames.ToString()},\n \"defaults\": {lParameterDefaults.ToString()}\n }}";
lDict.Add(lSuffix, lOutput);
// Console.WriteLine("{2}: {0}, {1} Bytes", lOutput, lOutput.Length, iDefine.prefix);
}
}