Server/ConsumerElectronicControl.cs
author StephaneLenclud
Sat, 23 Jul 2016 19:22:56 +0200
changeset 211 96f8b4dc4300
parent 206 33be8cb90c57
child 212 1a0791daa243
permissions -rw-r--r--
Adding Events tab.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Diagnostics;
     7 using System.Windows.Forms;
     8 using CecSharp;
     9 using SharpLib.Ear;
    10 
    11 namespace SharpDisplayManager
    12 {
    13     class ConsumerElectronicControl
    14     {
    15         ///
    16         private PowerManager.SettingNotifier iPowerSettingNotifier;
    17         ///
    18         public Cec.Client Client;
    19         ///This flag will only work properly if both on and off events are monitored.
    20         ///TODO: have a more solid implementation
    21         public bool MonitorPowerOn;
    22 
    23         private bool iReconnectToPowerTv = false;
    24 
    25         public void TestSendKeys()
    26         {
    27             Client.TestSendKey();
    28         }
    29 
    30         /// <summary>
    31         /// 
    32         /// </summary>
    33         /// <param name="aWndHandle"></param>
    34         /// <param name="aDeviceName"></param>
    35         /// <param name="aHdmiPort"></param>
    36         public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff, bool aReconnectToPowerTv)
    37         {
    38             //Assuming monitor is on when we start up
    39             MonitorPowerOn = true;
    40 
    41             iReconnectToPowerTv = aReconnectToPowerTv;
    42 
    43             //Create our power setting notifier and register the event we are interested in
    44             iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
    45 
    46             //
    47             if (aMonitorOn)
    48             {
    49                 iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
    50             }
    51 
    52             //
    53             if (aMonitorOff)
    54             {
    55                 iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
    56             }
    57 
    58             //CEC
    59             Client = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice);
    60             ConnectCecClient();
    61         }
    62 
    63         //
    64         public void Stop()
    65         {
    66             //
    67             if (iPowerSettingNotifier != null)
    68             {
    69                 iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
    70                 iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
    71                 iPowerSettingNotifier = null;
    72             }
    73             //
    74             if (Client != null)
    75             {
    76                 Client.Close();
    77                 Client.Dispose();
    78                 Client = null;
    79             }
    80         }
    81 
    82         /// <summary>
    83         /// 
    84         /// </summary>
    85         private void ConnectCecClient()
    86         {
    87             //Our client takes care of closing before trying to connect
    88             if (!Client.Connect(1000))
    89             {
    90                 Debug.WriteLine("WARNING: No CEC connection!");
    91             }
    92         }
    93 
    94         private void OnMonitorPowerOn()
    95         {
    96             EventActionManager.Current.GetEvent<EventMonitorPowerOn>().Trigger();
    97 
    98             Console.WriteLine("OnMonitorPowerOn");
    99 
   100             if (iReconnectToPowerTv)
   101             {
   102                 ConnectCecClient();
   103             }            
   104 
   105             //Turn on the TV
   106             //iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
   107             //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv,CecUserControlCode.PowerOnFunction,true);
   108             //Set ourselves as the active source
   109             Client.Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
   110             MonitorPowerOn = true;
   111         }
   112 
   113         private void OnMonitorPowerOff()
   114         {
   115             EventActionManager.Current.GetEvent<EventMonitorPowerOff>().Trigger();
   116 
   117             Console.WriteLine("OnMonitorPowerOff");
   118 
   119             if (iReconnectToPowerTv)
   120             {
   121                 ConnectCecClient();
   122             }
   123 
   124             //Try turning off the TV
   125             Client.Lib.StandbyDevices(CecLogicalAddress.Tv);
   126             //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.PowerOffFunction, true);
   127             //Tell everyone that we are no longer active
   128             //iCecClient.Lib.SetInactiveView();
   129 
   130             MonitorPowerOn = false;
   131         }
   132 
   133         /// <summary>
   134         /// We need to handle WM_POWERBROADCAST.
   135         /// </summary>
   136         /// <param name="message"></param>
   137         public void OnWndProc(ref Message message)
   138         {
   139             //Hook in our power manager
   140             if (iPowerSettingNotifier != null)
   141             {
   142                 iPowerSettingNotifier.WndProc(ref message);
   143             }
   144         }
   145 
   146     }
   147 }