Fixing broken KingOfSat parser.
2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using System.Threading.Tasks;
15 public static string Polarisation(Channel aChannel)
17 switch (aChannel.Polarisation)
23 case "L": //Check that on KingOfSat
25 case "R": //Check that on KingOfSat
30 Debug.Write("WARNING: Unknown Polarisation " + aChannel.Polarisation + " for " + aChannel.Name + "\n");
45 public static string Modulation(Channel aChannel)
47 //Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
49 switch (aChannel.Modulation)
57 Debug.Write("WARNING: Unknown modulation " + aChannel.Modulation + " for " + aChannel.Name + "\n");
103 public static string FEC(Channel aChannel)
105 //Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
107 switch (aChannel.FEC)
140 Debug.Write("WARNING: Unknown FEC " + aChannel.FEC + " for " + aChannel.Name + "\n");
151 public static void Export(List<Channel> aChannels, List<string> aTunerCards, string aFileName, bool aAddMapping)
153 //Create a dictionary for our groups
154 Dictionary<string, List<Channel>> groups = new Dictionary<string, List<Channel>>();
156 XmlDocument xmlDoc = new XmlDocument();
157 XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
158 xmlDoc.AppendChild(xmlDeclaration);
160 XmlNode rootElement = xmlDoc.CreateElement("tvserver");
161 AddAttribute(rootElement, "version", "1.0");
162 xmlDoc.AppendChild(rootElement);
163 //Create our channel element
164 XmlNode nodechannels = xmlDoc.CreateElement("channels");
165 rootElement.AppendChild(nodechannels);
168 int channelMapId = 0;
169 foreach (Channel channel in aChannels)
171 //Create group if needed
172 if (!groups.ContainsKey(channel.Category))
174 //KeyValuePair<string, List<Channel>> entry=new KeyValuePair<string, List<Channel>>();
175 groups.Add(channel.Category, new List<Channel>());
178 //Add that channel to its group
179 groups[channel.Category].Add(channel);
181 //Create and populate channel element
183 XmlNode nodechannel = xmlDoc.CreateElement("channel");
184 AddAttribute(nodechannel, "GrabEpg", "True");
185 AddAttribute(nodechannel, "IdChannel", channelId);
186 AddAttribute(nodechannel, "IsRadio", "False");
187 AddAttribute(nodechannel, "IsTv", "True");
188 AddAttribute(nodechannel, "LastGrabTime", "2000-1-1 0:0:0");
189 //AddAttribute(nodechannel, "SortOrder", channel.SortOrder);
190 //AddAttribute(nodechannel, "TimesWatched", "0");
191 AddAttribute(nodechannel, "TotalTimeWatched", "2000-1-1 0:0:0");
192 AddAttribute(nodechannel, "VisibleInGuide", !channel.Name.StartsWith("."));
193 AddAttribute(nodechannel, "DisplayName", channel.Name);
194 AddAttribute(nodechannel, "ChannelNumber", channelId);
198 //I don't think we can get mapping to work without having corresponding server and card definition elements
199 XmlNode nodeMaps = xmlDoc.CreateElement("mappings");
200 foreach (string tuner in aTunerCards)
203 XmlNode nodeMap = xmlDoc.CreateElement("map");
204 AddAttribute(nodeMap, "IdCard", tuner);
205 AddAttribute(nodeMap, "IdChannel", channelId);
206 AddAttribute(nodeMap, "IdChannelMap", channelMapId);
207 nodeMaps.AppendChild(nodeMap);
209 nodechannel.AppendChild(nodeMaps);
212 XmlNode nodeTuningDetails = xmlDoc.CreateElement("TuningDetails");
213 XmlNode nodeTune = xmlDoc.CreateElement("tune");
214 AddAttribute(nodeTune, "IdChannel", channelId);
215 AddAttribute(nodeTune, "IdTuning", channelId);
216 AddAttribute(nodeTune, "Bandwidth", "8"); //Weird
217 AddAttribute(nodeTune, "ChannelNumber", "10000");
218 AddAttribute(nodeTune, "ChannelType", "3"); //Bad hard coding, does it means DVB-S
219 AddAttribute(nodeTune, "CountryId", "31"); //Is that Germany?
220 AddAttribute(nodeTune, "Diseqc", "0"); //Disabled
221 AddAttribute(nodeTune, "FreeToAir", "False"); //Can we get that from KingOfSat? Does it matter?
222 AddAttribute(nodeTune, "Frequency", ((Int32)(Convert.ToDouble(channel.Frequency)*1000)).ToString());
223 AddAttribute(nodeTune, "MajorChannel", "-1");
224 AddAttribute(nodeTune, "MinorChannel", "-1");
225 AddAttribute(nodeTune, "Modulation", Modulation(channel));
226 AddAttribute(nodeTune, "Name", channel.Name);
227 AddAttribute(nodeTune, "NetworkId", channel.NetworkID);
228 AddAttribute(nodeTune, "PmtPid", channel.PMT);
229 AddAttribute(nodeTune, "Polarisation", Polarisation(channel));
230 AddAttribute(nodeTune, "Provider", channel.Provider); //Does that matter? Should really be SKY or CSAT
231 AddAttribute(nodeTune, "ServiceId", channel.SID);
232 AddAttribute(nodeTune, "SwitchingFrequency", "11700000"); //Does it matter?
233 AddAttribute(nodeTune, "Symbolrate", channel.SymbolRate);
234 AddAttribute(nodeTune, "TransportId", channel.TransponderID);
235 AddAttribute(nodeTune, "TuningSource", "0"); //Is that needed?
236 AddAttribute(nodeTune, "VideoSource", "0"); //Is that needed?
237 AddAttribute(nodeTune, "AudioSource", "0"); //Is that needed?
238 AddAttribute(nodeTune, "IsVCRSignal", "False");
239 AddAttribute(nodeTune, "SatIndex", "-1");
240 AddAttribute(nodeTune, "InnerFecRate", FEC(channel));
241 AddAttribute(nodeTune, "Band", "0"); //Needed?
242 AddAttribute(nodeTune, "Pilot", "-1");
243 AddAttribute(nodeTune, "RollOff", "-1");
244 AddAttribute(nodeTune, "Url", "");
245 AddAttribute(nodeTune, "Bitrate", "0"); //Should we bother putting it in there?
246 nodeTuningDetails.AppendChild(nodeTune);
248 nodechannel.AppendChild(nodeTuningDetails);
250 nodechannels.AppendChild(nodechannel);
253 //Create groups element
254 XmlNode nodeChannelGroups = xmlDoc.CreateElement("channelgroups");
255 rootElement.AppendChild(nodeChannelGroups);
257 int groupSortOrder = 0;
258 foreach (KeyValuePair<string, List<Channel>> group in groups)
260 //Create group element
261 XmlNode nodeChannelGroup = xmlDoc.CreateElement("channelgroup");
262 AddAttribute(nodeChannelGroup, "GroupName", group.Key);
263 AddAttribute(nodeChannelGroup, "SortOrder", groupSortOrder.ToString());
264 XmlNode nodeGroupMap = xmlDoc.CreateElement("mappings");
265 nodeChannelGroup.AppendChild(nodeGroupMap);
266 nodeChannelGroups.AppendChild(nodeChannelGroup);
269 List<Channel> sortedChannels = group.Value.OrderBy(o => o.Name).ToList();
271 //Add each channel to its group
272 int channelSortOrder = 0;
273 foreach (Channel channel in sortedChannels)
275 XmlNode nodeMap = xmlDoc.CreateElement("map");
276 AddAttribute(nodeMap, "ChannelName", channel.Name);
277 AddAttribute(nodeMap, "SortOrder", channelSortOrder.ToString());
278 nodeGroupMap.AppendChild(nodeMap);
285 using (TextWriter sw = new StreamWriter(aFileName, false, Encoding.UTF8)) //Set encoding
289 //xmlDoc.Save(aFileName);
292 private static void AddAttribute(XmlNode node, string tagName, string tagValue)
294 XmlAttribute attr = node.OwnerDocument.CreateAttribute(tagName);
295 attr.InnerText = tagValue;
296 node.Attributes.Append(attr);
299 private static void AddAttribute(XmlNode node, string tagName, int tagValue)
301 AddAttribute(node, tagName, tagValue.ToString());
304 // store DateTime Values as strings. Improves readability
305 private static void AddAttribute(XmlNode node, string tagName, DateTime tagValue)
307 AddAttribute(node, tagName,
308 String.Format("{0}-{1}-{2} {3}:{4}:{5}", tagValue.Year, tagValue.Month, tagValue.Day, tagValue.Hour,
309 tagValue.Minute, tagValue.Second));
312 private static void AddAttribute(XmlNode node, string tagName, bool tagValue)
314 AddAttribute(node, tagName, tagValue.ToString());