MediaPortal.cs
author StephaneLenclud
Fri, 15 May 2015 22:41:33 +0200
changeset 4 8372aa8d6292
parent 3 01a00603ec28
child 5 29ccfbf98e54
permissions -rw-r--r--
Adding channel name clean up.
Adding card mapping support but it's not working.
     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, List<string> aTunerCards,  string aFileName, bool aAddMapping)
   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       int channelMapId = 0;
   159 			foreach (Channel channel in aChannels)
   160 			{
   161 				channelId++;
   162 				XmlNode nodechannel = xmlDoc.CreateElement("channel");
   163 				AddAttribute(nodechannel, "GrabEpg", "True");
   164 				AddAttribute(nodechannel, "IdChannel", channelId);
   165 				AddAttribute(nodechannel, "IsRadio", "False");
   166 				AddAttribute(nodechannel, "IsTv", "True");
   167         AddAttribute(nodechannel, "LastGrabTime", "2000-1-1 0:0:0");
   168 				//AddAttribute(nodechannel, "SortOrder", channel.SortOrder);
   169         //sAddAttribute(nodechannel, "TimesWatched", "0");
   170         AddAttribute(nodechannel, "TotalTimeWatched", "2000-1-1 0:0:0");
   171 				AddAttribute(nodechannel, "VisibleInGuide", "True");
   172 				AddAttribute(nodechannel, "DisplayName", channel.Name);
   173 				AddAttribute(nodechannel, "ChannelNumber", channelId);
   174 
   175         if (aAddMapping)
   176         {
   177           //I don't think we can get mapping to work without having corresponding server and card definition elements
   178           XmlNode nodeMaps = xmlDoc.CreateElement("mappings");
   179           foreach (string tuner in aTunerCards)
   180           {
   181             channelMapId++;
   182             XmlNode nodeMap = xmlDoc.CreateElement("map");
   183             AddAttribute(nodeMap, "IdCard", tuner);
   184             AddAttribute(nodeMap, "IdChannel", channelId);
   185             AddAttribute(nodeMap, "IdChannelMap", channelMapId);
   186             nodeMaps.AppendChild(nodeMap);
   187           }
   188           nodechannel.AppendChild(nodeMaps);
   189         }
   190 
   191 				XmlNode nodeTuningDetails = xmlDoc.CreateElement("TuningDetails");
   192 				XmlNode nodeTune = xmlDoc.CreateElement("tune");
   193 				AddAttribute(nodeTune, "IdChannel", channelId);
   194 				AddAttribute(nodeTune, "IdTuning", channelId);
   195 				AddAttribute(nodeTune, "Bandwidth", "8"); //Weird
   196 				AddAttribute(nodeTune, "ChannelNumber", "10000");
   197 				AddAttribute(nodeTune, "ChannelType", "3"); //Bad hard coding, does it means DVB-S
   198 				AddAttribute(nodeTune, "CountryId", "31"); //Is that Germany?
   199 				AddAttribute(nodeTune, "Diseqc", "0"); //Disabled
   200 				AddAttribute(nodeTune, "FreeToAir", "False"); //Can we get that from KingOfSat? Does it matter?
   201 				AddAttribute(nodeTune, "Frequency", ((Int32)(Convert.ToDouble(channel.Frequency)*1000)).ToString());
   202 				AddAttribute(nodeTune, "MajorChannel", "-1");
   203 				AddAttribute(nodeTune, "MinorChannel", "-1");
   204 				AddAttribute(nodeTune, "Modulation", Modulation(channel));
   205 				AddAttribute(nodeTune, "Name", channel.Name);
   206 				AddAttribute(nodeTune, "NetworkId", channel.NetworkID);
   207 				AddAttribute(nodeTune, "PmtPid", channel.PMT);
   208 				AddAttribute(nodeTune, "Polarisation", Polarisation(channel));
   209 				AddAttribute(nodeTune, "Provider", channel.Provider); //Does that matter? Should really be SKY or CSAT
   210 				AddAttribute(nodeTune, "ServiceId", channel.SID);
   211 				AddAttribute(nodeTune, "SwitchingFrequency", "11700000"); //Does it matter?
   212 				AddAttribute(nodeTune, "Symbolrate", channel.SymbolRate);
   213 				AddAttribute(nodeTune, "TransportId", channel.TransponderID);
   214 				AddAttribute(nodeTune, "TuningSource", "0"); //Is that needed?
   215 				AddAttribute(nodeTune, "VideoSource", "0"); //Is that needed?
   216 				AddAttribute(nodeTune, "AudioSource", "0"); //Is that needed?
   217 				AddAttribute(nodeTune, "IsVCRSignal", "False");
   218 				AddAttribute(nodeTune, "SatIndex", "-1");
   219 				AddAttribute(nodeTune, "InnerFecRate", FEC(channel));
   220 				AddAttribute(nodeTune, "Band", "0"); //Needed?
   221 				AddAttribute(nodeTune, "Pilot", "-1");
   222 				AddAttribute(nodeTune, "RollOff", "-1");
   223 				AddAttribute(nodeTune, "Url", "");
   224 				AddAttribute(nodeTune, "Bitrate", "0"); //Should we bother putting it in there?
   225 				nodeTuningDetails.AppendChild(nodeTune);
   226 					
   227 				nodechannel.AppendChild(nodeTuningDetails);
   228 
   229 				nodechannels.AppendChild(nodechannel);				
   230 			}
   231 
   232 
   233 
   234 			xmlDoc.Save(aFileName);
   235 		}
   236 
   237 		private static void AddAttribute(XmlNode node, string tagName, string tagValue)
   238 		{
   239 			XmlAttribute attr = node.OwnerDocument.CreateAttribute(tagName);
   240 			attr.InnerText = tagValue;
   241 			node.Attributes.Append(attr);
   242 		}
   243 
   244 		private static void AddAttribute(XmlNode node, string tagName, int tagValue)
   245 		{
   246 			AddAttribute(node, tagName, tagValue.ToString());
   247 		}
   248 
   249 		// store DateTime Values as strings. Improves readability
   250 		private static void AddAttribute(XmlNode node, string tagName, DateTime tagValue)
   251 		{
   252 			AddAttribute(node, tagName,
   253 						 String.Format("{0}-{1}-{2} {3}:{4}:{5}", tagValue.Year, tagValue.Month, tagValue.Day, tagValue.Hour,
   254 									   tagValue.Minute, tagValue.Second));
   255 		}
   256 
   257 		private static void AddAttribute(XmlNode node, string tagName, bool tagValue)
   258 		{
   259 			AddAttribute(node, tagName, tagValue.ToString());
   260 		}
   261 	}
   262 }