NotificationPlugin.cs
author StephaneLenclud
Tue, 08 Dec 2015 18:49:02 +0100
changeset 1 b4df02a92dff
parent 0 99720d48e299
permissions -rw-r--r--
Adding basic display client connection.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Reflection;
     7 
     8 using SetupTv;
     9 using TvControl;
    10 using TvEngine;
    11 using System.Drawing;
    12 using System.IO;
    13 using TvLibrary.Log;
    14 
    15 //
    16 using SharpLib.Display;
    17 using TvEngine.Events;
    18 using TvLibrary.Interfaces;
    19 
    20 namespace TvServerPlugin
    21 {
    22 
    23 
    24     public class RecordSatus : ITvServerPlugin
    25     {
    26 
    27         Client iClient;
    28 
    29         /// <summary>
    30         /// Reference to the tvservice's TVcontroller
    31         /// </summary>
    32         private IController _controller;
    33 
    34 
    35         /// <summary>
    36         /// Creates a new PowerScheduler plugin
    37         /// </summary>
    38         public RecordSatus() { }
    39 
    40         /// <summary>
    41         /// 
    42         /// </summary>
    43         public void OnCloseOrder()
    44         {
    45             CloseClient();
    46         }
    47 
    48         /// <summary>
    49         /// 
    50         /// </summary>
    51         public void CloseClient()
    52         {
    53             iClient.Close();
    54             iClient = null;
    55         }
    56 
    57         void OpenClient()
    58         {
    59             iClient = new Client();
    60             //iClient.CloseOrderEvent += OnCloseOrder;
    61             iClient.Open();
    62 
    63             //Connect using unique name
    64             //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
    65             string name = "Client-" + (iClient.ClientCount() - 1);
    66             iClient.SetName(name);
    67 
    68 
    69 
    70             //iClient.Open(/*"net.tcp://localhost:8111/DisplayService"*/);
    71 
    72             //Connect using unique name
    73             //string name = "Client-" + (iClient.ClientCount() - 1);
    74             //iClient.SetName(name);
    75             //Console.WriteLine("Name: " + name);
    76         }
    77 
    78         /// <summary>
    79         /// 
    80         /// </summary>
    81         public void SetLayout()
    82         {
    83             TableLayout layout = new TableLayout(1, 1);
    84             iClient.SetLayout(layout);
    85             //We need to create our fields
    86             DataField field = new TextField();
    87             iClient.CreateFields(new DataField[]
    88             {
    89                 field
    90             });
    91         }
    92 
    93         /// <summary>
    94         /// 
    95         /// </summary>
    96         public void SetTextField()
    97         {
    98             TextField field = new TextField();
    99             field.Text = "Notification TV Server Plugin";
   100             iClient.SetField(field);
   101         }
   102 
   103 
   104         private void UpdateRecordingStatus()
   105         {
   106             RecordingField field = new RecordingField();
   107             field.IsActive = _controller.IsAnyCardRecording();
   108             iClient.SetField(field);
   109         }
   110 
   111         private void OnTvServerEvent(object sender, EventArgs eventArgs)
   112         {
   113             if (iClient==null)
   114             {
   115                 //TODO: Try reconnect
   116                 return;
   117             }
   118 
   119             TvServerEventArgs tvArgs = eventArgs as TvServerEventArgs;
   120             switch (tvArgs.EventType)
   121             {
   122                 case TvServerEventType.RecordingStarted:
   123                     UpdateRecordingStatus();
   124                     break;
   125 
   126                 case TvServerEventType.RecordingEnded:
   127 
   128                     break;
   129             }
   130 
   131 
   132             /*
   133             if (_state == EpgState.Idle)
   134                 return;
   135             TvServerEventArgs tvArgs = eventArgs as TvServerEventArgs;
   136             if (eventArgs == null)
   137                 return;
   138             if (tvArgs != null)
   139                 switch (tvArgs.EventType)
   140                 {
   141                     case TvServerEventType.StartTimeShifting:
   142                         Log.Epg("epg cancelled due to start timeshifting");
   143                         OnEpgCancelled();
   144                         break;
   145 
   146                     case TvServerEventType.StartRecording:
   147                         Log.Epg("epg cancelled due to start recording");
   148                         OnEpgCancelled();
   149                         break;
   150                 }
   151                 */
   152         }
   153 
   154         /// <summary>
   155         /// Called by the tvservice PluginLoader to start this plugin
   156         /// </summary>
   157         /// <param name="controller">Reference to the tvservice's TVController</param>
   158         public void Start(IController controller)
   159         {
   160             Log.Debug(Name + " start - begins");
   161 
   162             _controller = controller;
   163 
   164 
   165             //Open Display Client Connection
   166             OpenClient();
   167             SetLayout();
   168             SetTextField();
   169             UpdateRecordingStatus();
   170 
   171             //
   172             ITvServerEvent events = GlobalServiceProvider.Instance.Get<ITvServerEvent>();
   173             events.OnTvServerEvent += new TvServerEventHandler(OnTvServerEvent);
   174 
   175             Log.Debug(Name + " start - ends");
   176         }
   177 
   178         /// <summary>
   179         /// Called by the tvservice PluginLoader to stop this plugin
   180         /// </summary>
   181         public void Stop()
   182         {
   183             Log.Debug(Name + " stop");
   184             _controller = null;
   185             CloseClient();
   186             //PowerScheduler.Instance.Stop();
   187         }
   188 
   189         /// <summary>
   190         /// Author of this plugin
   191         /// </summary>
   192         public string Author
   193         {
   194             get { return "Stéphane Lenclud"; }
   195         }
   196 
   197         /// <summary>
   198         /// Should this plugin run only on a master tvserver?
   199         /// </summary>
   200         public bool MasterOnly
   201         {
   202             get { return false; }
   203         }
   204 
   205         /// <summary>
   206         /// Name of this plugin
   207         /// </summary>
   208         public string Name
   209         {
   210             get
   211             {
   212                 return "NotificationTvServerPlugin";
   213             }
   214         }
   215 
   216         /// <summary>
   217         /// Returns the SectionSettings setup part of this plugin
   218         /// </summary>
   219         public SectionSettings Setup
   220         {
   221             get { return new RecordStatusSetup(); }
   222         }
   223 
   224         /// <summary>
   225         /// Plugin version
   226         /// </summary>
   227         public string Version
   228         {
   229             get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
   230         }
   231 
   232 
   233         /// <summary>
   234         /// Access icons from embedded resources.
   235         /// </summary>
   236         /// <param name="name"></param>
   237         /// <returns></returns>
   238         public static Icon GetIcon(string name)
   239         {
   240             string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
   241             for (int i = 0; i < names.Length; i++)
   242             {
   243                 if (names[i].EndsWith(name))
   244                 {
   245                     using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(names[i]))
   246                     {
   247                         return new Icon(stream);
   248                     }
   249                 }
   250             }
   251 
   252             return null;
   253         }
   254 
   255 
   256     }
   257 
   258 }