#region Copyright (C) 2005-2011 Team MediaPortal // Copyright (C) 2005-2011 Team MediaPortal // http://www.team-mediaportal.com // // MediaPortal is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // MediaPortal is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with MediaPortal. If not, see . #endregion using System; using System.Diagnostics; using System.IO; using TvLibrary.Log; using TvControl; using SetupTv; using TvEngine.Events; using TvLibrary.Interfaces; using TvLibrary.Implementations; using TvDatabase; using System.Net; using System.Windows.Forms; namespace TvEngine { public class DirecTV_SHEF : AnalogChannel, ITvServerPlugin { #region Members internal static DirecTV_Receiver Receiver1 = new DirecTV_Receiver(); internal static DirecTV_Receiver Receiver2 = new DirecTV_Receiver(); internal static DirecTV_Receiver Receiver3 = new DirecTV_Receiver(); internal static DirecTV_Receiver Receiver4 = new DirecTV_Receiver(); internal static DirecTV_Bool_Option ManagePower = new DirecTV_Bool_Option("ManagePower"); internal static bool[] DelayNeeded = new bool[4] { true, true, true, true }; internal const int DelayTime = 1000; #endregion Members #region Properties /// /// returns the name of the plugin /// public new string Name { get { return "DirecTV SHEF"; } } /// /// returns the version of the plugin /// public string Version { get { return "1.2.0.0"; } } /// /// returns the author of the plugin /// public string Author { get { return "marirs/ajp8164"; } } /// /// returns if the plugin should only run on the master server /// or also on slave servers /// public bool MasterOnly { get { return false; } } #endregion Properties #region IPlugin Members // [CLSCompliant(false)] public void Start(IController controller) { Log.Info("plugin: DirecTV_SHEF start"); LoadSettings(); GlobalServiceProvider.Instance.Get().OnTvServerEvent += DirecTV_SHEF_OnTvServerEvent; } public void Stop() { Log.Info("plugin: DirecTV_SHEF stop"); if (GlobalServiceProvider.Instance.IsRegistered()) { GlobalServiceProvider.Instance.Get().OnTvServerEvent -= DirecTV_SHEF_OnTvServerEvent; } } // [CLSCompliant(false)] public SectionSettings Setup { get { return new SetupTv.Sections.DirecTV_SHEFSetup(); } } #endregion #region Implementation private static void SendDTVCommand(string cmd, int cardID) { string url = ""; if (cardID.ToString().Equals(Receiver1.CardId)) { url = "http://" + Receiver1.IPAddress + ":8080/" + cmd; } else if (cardID.ToString().Equals(Receiver2.CardId)) { url = "http://" + Receiver2.IPAddress + ":8080/" + cmd; } else if (cardID.ToString().Equals(Receiver3.CardId)) { url = "http://" + Receiver3.IPAddress + ":8080/" + cmd; } else if (cardID.ToString().Equals(Receiver4.CardId)) { url = "http://" + Receiver4.IPAddress + ":8080/" + cmd; } else { throw new Exception("INFO: attempt to send command to unmapped receiver; command send not attempted"); } HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()); reader.ReadToEnd(); } private static void DirecTV_SHEF_OnTvServerEvent(object sender, EventArgs eventArgs) { try { TvServerEventArgs tvEvent = (TvServerEventArgs)eventArgs; if (((tvEvent.EventType == TvServerEventType.StartTimeShifting || tvEvent.EventType == TvServerEventType.StartRecording)) && ManagePower.Value) { //Log.Debug("DirecTV_SHEF - DirecTV_SHEF_OnTvServerEvent(): Power on receiver - Card Id = {0}", // tvEvent.Card.Id); //SendDTVCommand("remote/processKey?key=poweron", tvEvent.Card.Id); } else if (tvEvent.EventType == TvServerEventType.StartZapChannel) { AnalogChannel analogChannel = tvEvent.channel as AnalogChannel; if (analogChannel != null) { Log.Debug("DirecTV_SHEF - DirecTV_SHEF_OnTvServerEvent(): Changing Channel - Card Id = {0}, Channel = {1} ({2}), Video Source: {3}", tvEvent.Card.Id, analogChannel.ChannelNumber, analogChannel.Name, analogChannel.VideoSource.ToString()); // We must send a power on command since the TVE doesn't send a start timeshifting until after the channel is successfully tuned. SendDTVCommand("remote/processKey?key=poweron", tvEvent.Card.Id); if (DelayNeeded[tvEvent.Card.Id - 1]) { Log.Debug("DirecTV_SHEF - Starting {0}ms receiver sanity delay because it was probably off", DelayTime); System.Threading.Thread.Sleep(DelayTime); DelayNeeded[tvEvent.Card.Id - 1] = false; } SendDTVCommand("tv/tune?major=" + analogChannel.ChannelNumber.ToString() + "&minor=65535", tvEvent.Card.Id); } } else if (((tvEvent.EventType == TvServerEventType.EndTimeShifting && !tvEvent.Card.IsRecording) || (tvEvent.EventType == TvServerEventType.RecordingEnded && !tvEvent.Card.IsTimeShifting)) && ManagePower.Value) { Log.Debug("DirecTV_SHEF - DirecTV_SHEF_OnTvServerEvent(): Power off receiver - Card Id = {0}", tvEvent.Card.Id); SendDTVCommand("remote/processKey?key=poweroff", tvEvent.Card.Id); DelayNeeded[tvEvent.Card.Id - 1] = true; } } catch (Exception ex) { Log.Error("DirecTV_SHEF - DirecTV_SHEF_OnTvServerEvent(): {0}", ex.Message); } } internal static void LoadSettings() { try { TvBusinessLayer layer = new TvBusinessLayer(); Receiver1.LoadFrom("1"); Receiver2.LoadFrom("2"); Receiver3.LoadFrom("3"); Receiver4.LoadFrom("4"); ManagePower.Load(); } catch (Exception ex) { Log.Error("DirecTV_SHEF - LoadSettings(): {0}", ex.Message); } } internal static void SaveSettings() { try { TvBusinessLayer layer = new TvBusinessLayer(); Receiver1.PersistAs("1"); Receiver2.PersistAs("2"); Receiver3.PersistAs("3"); Receiver4.PersistAs("4"); ManagePower.Persist(); } catch (Exception ex) { Log.Error("DirecTV_SHEF - SaveSettings(): {0}", ex.Message); } } #endregion Implementation } }