MediaPortal.cs
author StephaneLenclud
Fri, 15 May 2015 15:16:18 +0200
changeset 2 d73e99c5333c
child 3 01a00603ec28
permissions -rw-r--r--
First MP export draft.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Xml;
     8 
     9 namespace SatChanGen
    10 {
    11 	class MediaPortal
    12 	{
    13 
    14 		public static string Polarisation(Channel aChannel)
    15 		{
    16 			switch (aChannel.Polarisation)
    17 			{
    18 				case "H":
    19 					return "1";
    20 				case "V":
    21 					return "2";
    22 				case "L": //Check that on KingOfSat
    23 					return "3";
    24 				case "R": //Check that on KingOfSat
    25 					return "4";
    26 
    27 				default:
    28 					{
    29 						Debug.Write("WARNING: Unknown Polarisation " + aChannel.Polarisation + " for " + aChannel.Name + "\n");
    30 						return "-1";
    31 					}
    32 			}
    33 
    34 			/*
    35 			"Not Set",
    36             "Not Defined",
    37             "Horizontal",
    38             "Vertical",
    39             "Circular Left",
    40             "Circular Right"
    41 			 */
    42 		}
    43 
    44 		public static string Modulation(Channel aChannel)
    45 		{
    46 			//Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
    47 
    48 			switch (aChannel.Modulation)
    49 			{
    50 				case "QPSK":
    51 					return "20";
    52 				case "8PSK":
    53 					return "27";
    54 				default:
    55 				{
    56 					Debug.Write("WARNING: Unknown modulation " + aChannel.Modulation + " for " + aChannel.Name + "\n");
    57 					return "-1";
    58 				}
    59 			}
    60 
    61 
    62 			/*
    63 			"Not Set",
    64 			"Not Defined",
    65 			"16 QAM",
    66 			"32 QAM",
    67 			"64 QAM",
    68 			"80 QAM",
    69 			"96 QAM",
    70 			"112 QAM",
    71 			"128 QAM",
    72 			"160 QAM",
    73 			"192 QAM",
    74 			"224 QAM",
    75 			"256 QAM",
    76 			"320 QAM",
    77 			"384 QAM",
    78 			"448 QAM",
    79 			"512 QAM",
    80 			"640 QAM",
    81 			"768 QAM",
    82 			"896 QAM",
    83 			"1024 QAM",
    84 			"QPSK",
    85 			"BPSK",
    86 			"OQPSK",
    87 			"8 VSB",
    88 			"16 VSB",
    89 			"Analog Amplitude",
    90 			"Analog Frequency",
    91 			"8 PSK",
    92 			"RF",
    93 			"16 APSK",
    94 			"32 APSK",
    95 			"QPSK2 (DVB-S2)",
    96 			"8 PSK2 (DVB-S2)",
    97 			"DirectTV"
    98 			 */
    99 		}
   100 
   101 
   102 		public static string FEC(Channel aChannel)
   103 		{
   104 			//Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
   105 
   106 			switch (aChannel.FEC)
   107 			{
   108 				case "1/2":
   109 					return "1";
   110 				case "2/3":
   111 					return "2";
   112 				case "3/4":
   113 					return "3";
   114 				case "3/5":
   115 					return "4";
   116 				case "4/5":
   117 					return "5";
   118 				case "5/6":
   119 					return "6";
   120 				case "5/11":
   121 					return "7";
   122 				case "7/8":
   123 					return "8";
   124 				case "1/4":
   125 					return "9";
   126 				case "1/3":
   127 					return "10";
   128 				case "2/5":
   129 					return "11";
   130 				case "6/7":
   131 					return "12";
   132 				case "8/9":
   133 					return "13";
   134 				case "9/10":
   135 					return "14";
   136 
   137 				default:
   138 				{
   139 					Debug.Write("WARNING: Unknown FEC " + aChannel.FEC + " for " + aChannel.Name + "\n");
   140 					return "-1";
   141 				}
   142 			}
   143 
   144 
   145 		}
   146 
   147 		public static void Export(List<Channel> aChannels, string aFileName)
   148 		{
   149 			XmlDocument xmlDoc = new XmlDocument();
   150 			XmlNode rootElement = xmlDoc.CreateElement("tvserver");			
   151 			AddAttribute(rootElement, "version", "1.0");
   152 			xmlDoc.AppendChild(rootElement);
   153 			//Create our channel element
   154 			XmlNode nodechannels = xmlDoc.CreateElement("channels");			
   155 			rootElement.AppendChild(nodechannels);
   156 
   157 			int channelId = 0;
   158 			foreach (Channel channel in aChannels)
   159 			{
   160 				channelId++;
   161 				XmlNode nodechannel = xmlDoc.CreateElement("channel");
   162 				AddAttribute(nodechannel, "GrabEpg", "True");
   163 				AddAttribute(nodechannel, "IdChannel", channelId);
   164 				AddAttribute(nodechannel, "IsRadio", "False");
   165 				AddAttribute(nodechannel, "IsTv", "True");
   166 				//AddAttribute(nodechannel, "LastGrabTime", channel.LastGrabTime);
   167 				//AddAttribute(nodechannel, "SortOrder", channel.SortOrder);
   168 				//AddAttribute(nodechannel, "TimesWatched", channel.TimesWatched);
   169 				//AddAttribute(nodechannel, "TotalTimeWatched", channel.TotalTimeWatched);
   170 				AddAttribute(nodechannel, "VisibleInGuide", "True");
   171 				AddAttribute(nodechannel, "DisplayName", channel.Name);
   172 				AddAttribute(nodechannel, "ChannelNumber", channelId);
   173 
   174 
   175 				XmlNode nodeTuningDetails = xmlDoc.CreateElement("TuningDetails");
   176 				XmlNode nodeTune = xmlDoc.CreateElement("tune");
   177 				AddAttribute(nodeTune, "IdChannel", channelId);
   178 				AddAttribute(nodeTune, "IdTuning", channelId);
   179 				AddAttribute(nodeTune, "Bandwidth", "8"); //Weird
   180 				AddAttribute(nodeTune, "ChannelNumber", "10000");
   181 				AddAttribute(nodeTune, "ChannelType", "3"); //Bad hard coding, does it means DVB-S
   182 				AddAttribute(nodeTune, "CountryId", "31"); //Is that Germany?
   183 				AddAttribute(nodeTune, "Diseqc", "0"); //Disabled
   184 				AddAttribute(nodeTune, "FreeToAir", "False"); //Can we get that from KingOfSat? Does it matter?
   185 				AddAttribute(nodeTune, "Frequency", ((Int32)(Convert.ToDouble(channel.Frequency)*1000)).ToString());
   186 				AddAttribute(nodeTune, "MajorChannel", "-1");
   187 				AddAttribute(nodeTune, "MinorChannel", "-1");
   188 				AddAttribute(nodeTune, "Modulation", Modulation(channel));
   189 				AddAttribute(nodeTune, "Name", channel.Name);
   190 				AddAttribute(nodeTune, "NetworkId", channel.NetworkID);
   191 				AddAttribute(nodeTune, "PmtPid", channel.PMT);
   192 				AddAttribute(nodeTune, "Polarisation", Polarisation(channel));
   193 				AddAttribute(nodeTune, "Provider", channel.Provider); //Does that matter? Should really be SKY or CSAT
   194 				AddAttribute(nodeTune, "ServiceId", channel.SID);
   195 				AddAttribute(nodeTune, "SwitchingFrequency", "11700000"); //Does it matter?
   196 				AddAttribute(nodeTune, "Symbolrate", channel.SymbolRate);
   197 				AddAttribute(nodeTune, "TransportId", channel.TransponderID);
   198 				AddAttribute(nodeTune, "TuningSource", "0"); //Is that needed?
   199 				AddAttribute(nodeTune, "VideoSource", "0"); //Is that needed?
   200 				AddAttribute(nodeTune, "AudioSource", "0"); //Is that needed?
   201 				AddAttribute(nodeTune, "IsVCRSignal", "False");
   202 				AddAttribute(nodeTune, "SatIndex", "-1");
   203 				AddAttribute(nodeTune, "InnerFecRate", FEC(channel));
   204 				AddAttribute(nodeTune, "Band", "0"); //Needed?
   205 				AddAttribute(nodeTune, "Pilot", "-1");
   206 				AddAttribute(nodeTune, "RollOff", "-1");
   207 				AddAttribute(nodeTune, "Url", "");
   208 				AddAttribute(nodeTune, "Bitrate", "0"); //Should we bother putting it in there?
   209 				nodeTuningDetails.AppendChild(nodeTune);
   210 					
   211 				nodechannel.AppendChild(nodeTuningDetails);
   212 
   213 				nodechannels.AppendChild(nodechannel);				
   214 			}
   215 
   216 
   217 
   218 			xmlDoc.Save(aFileName);
   219 		}
   220 
   221 		private static void AddAttribute(XmlNode node, string tagName, string tagValue)
   222 		{
   223 			XmlAttribute attr = node.OwnerDocument.CreateAttribute(tagName);
   224 			attr.InnerText = tagValue;
   225 			node.Attributes.Append(attr);
   226 		}
   227 
   228 		private static void AddAttribute(XmlNode node, string tagName, int tagValue)
   229 		{
   230 			AddAttribute(node, tagName, tagValue.ToString());
   231 		}
   232 
   233 		// store DateTime Values as strings. Improves readability
   234 		private static void AddAttribute(XmlNode node, string tagName, DateTime tagValue)
   235 		{
   236 			AddAttribute(node, tagName,
   237 						 String.Format("{0}-{1}-{2} {3}:{4}:{5}", tagValue.Year, tagValue.Month, tagValue.Day, tagValue.Hour,
   238 									   tagValue.Minute, tagValue.Second));
   239 		}
   240 
   241 		private static void AddAttribute(XmlNode node, string tagName, bool tagValue)
   242 		{
   243 			AddAttribute(node, tagName, tagValue.ToString());
   244 		}
   245 	}
   246 }