Clients/Idle/FormClientIdle.cs
author StephaneLenclud
Wed, 08 Mar 2017 18:33:00 +0100
changeset 283 f18d6170a223
parent 197 c66ec88ed19d
permissions -rw-r--r--
Starting renaming to HTCIC.
Setup update and test.
     1 //
     2 // Copyright (C) 2014-2016 Stéphane Lenclud.
     3 //
     4 // This file is part of SharpDisplayManager.
     5 //
     6 // SharpDisplayManager is free software: you can redistribute it and/or modify
     7 // it under the terms of the GNU General Public License as published by
     8 // the Free Software Foundation, either version 3 of the License, or
     9 // (at your option) any later version.
    10 //
    11 // SharpDisplayManager is distributed in the hope that it will be useful,
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 // GNU General Public License for more details.
    15 //
    16 // You should have received a copy of the GNU General Public License
    17 // along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    18 //
    19 
    20 using System;
    21 using System.Collections.Generic;
    22 using System.ComponentModel;
    23 using System.Data;
    24 using System.Drawing;
    25 using System.Linq;
    26 using System.Text;
    27 using System.Threading.Tasks;
    28 using System.Windows.Forms;
    29 using System.Diagnostics;
    30 using SharpLib.Display;
    31 
    32 
    33 namespace SharpDisplayClientIdle
    34 {
    35 
    36     /// <summary>
    37     /// Sharp Display Client designed to act as an idle client.
    38     /// It should take care of screen saving and other such concerns.
    39     /// </summary>
    40     public partial class FormClientIdle : Form
    41     {
    42         public StartParams Params { get; set; }
    43 
    44         Client iClient;
    45         ContentAlignment iAlignment;
    46         TextField iTextField;
    47         //Used to determine if screen saver need to kick in
    48         private PowerManager.SettingNotifier iPowerSettingNotifier;
    49         private bool MonitorPowerOn;
    50 
    51         public delegate void CloseDelegate();
    52         public delegate void CloseConnectionDelegate();
    53 
    54 
    55         public FormClientIdle()
    56         {
    57             InitializeComponent();
    58         }
    59 
    60         /// <summary>
    61         /// 
    62         /// </summary>
    63         /// <param name="sender"></param>
    64         /// <param name="e"></param>
    65         private void FormClientIdle_Load(object sender, EventArgs e)
    66         {
    67             //Prevents showing in the Open Task view (Windows Key + Tab)
    68             Visible = false;
    69 
    70             //Display client
    71             iClient = new Client();
    72             iClient.CloseOrderEvent += OnCloseOrder;
    73             iClient.Open();
    74             iClient.SetName("Idle");
    75             iClient.SetPriority(Priorities.Background);
    76             SetupDisplayClient();
    77 
    78             //Timer
    79             iTimer.Interval = IntervalToNextMinute();
    80             iTimer.Start();
    81 
    82             //Create our power setting notifier and register the event we are interested in
    83             iPowerSettingNotifier = new PowerManager.SettingNotifier(Handle);
    84             iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
    85             iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
    86             MonitorPowerOn = true;
    87 
    88         }
    89 
    90         /// <summary>
    91         /// Broadcast messages to subscribers.
    92         /// </summary>
    93         /// <param name="message"></param>
    94         protected override void WndProc(ref Message aMessage)
    95         {
    96             //Hook in our power manager
    97             if (iPowerSettingNotifier != null)
    98             {
    99                 iPowerSettingNotifier.WndProc(ref aMessage);
   100             }
   101 
   102             base.WndProc(ref aMessage);
   103         }
   104 
   105 
   106 
   107         /// <summary>
   108         /// 
   109         /// </summary>
   110         private void OnMonitorPowerOn()
   111         {
   112             MonitorPowerOn = true;
   113             UpdateDisplay();
   114         }
   115 
   116         /// <summary>
   117         /// 
   118         /// </summary>
   119         private void OnMonitorPowerOff()
   120         {
   121             MonitorPowerOn = false;
   122             UpdateDisplay();
   123         }
   124 
   125 
   126         /// <summary>
   127         /// 
   128         /// </summary>
   129         /// <returns></returns>
   130         public static int IntervalToNextMinute()
   131         {
   132             DateTime now = DateTime.Now;
   133             return 60000 - now.Millisecond;
   134         }
   135 
   136 
   137         /// <summary>
   138         /// 
   139         /// </summary>
   140         /// <returns></returns>
   141         public bool IsClientReady()
   142         {
   143             return (iClient != null && iClient.IsReady());
   144         }
   145 
   146         /// <summary>
   147         /// 
   148         /// </summary>
   149         public void SetupDisplayClient()
   150         {
   151             //Setup our layout
   152 
   153             //Set one column one line layout
   154             TableLayout layout = new TableLayout(1, 1);
   155             iClient.SetLayout(layout);
   156 
   157             //Setup our fields
   158             iAlignment = ContentAlignment.MiddleCenter;
   159             iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
   160 
   161             //Set our fields
   162             iClient.CreateFields(new DataField[]
   163             {
   164                 iTextField
   165             });
   166         }
   167 
   168         public void OnCloseOrder()
   169         {
   170             CloseThreadSafe();
   171         }
   172 
   173         /// <summary>
   174         ///
   175         /// </summary>
   176         public void CloseThreadSafe()
   177         {
   178             if (this.InvokeRequired)
   179             {
   180                 //Not in the proper thread, invoke ourselves
   181                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   182                 this.Invoke(d, new object[] { });
   183             }
   184             else
   185             {
   186                 //We are in the proper thread
   187                 Close();
   188             }
   189         }
   190 
   191         /// <summary>
   192         ///
   193         /// </summary>
   194         public void CloseConnectionThreadSafe()
   195         {
   196             if (this.InvokeRequired)
   197             {
   198                 //Not in the proper thread, invoke ourselves
   199                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   200                 this.Invoke(d, new object[] { });
   201             }
   202             else
   203             {
   204                 //We are in the proper thread
   205                 if (IsClientReady())
   206                 {
   207                     string sessionId = iClient.SessionId;
   208                     Trace.TraceInformation("Closing client: " + sessionId);
   209                     iClient.Close();
   210                     Trace.TraceInformation("Closed client: " + sessionId);
   211                 }
   212 
   213                 iClient = null;
   214             }
   215         }
   216 
   217         /// <summary>
   218         /// 
   219         /// </summary>
   220         /// <param name="sender"></param>
   221         /// <param name="e"></param>
   222         private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
   223         {
   224             CloseConnectionThreadSafe();
   225         }
   226 
   227 
   228         /// <summary>
   229         /// 
   230         /// </summary>
   231         /// <param name="sender"></param>
   232         /// <param name="e"></param>
   233         private void iTimer_Tick(object sender, EventArgs e)
   234         {
   235             //Timer
   236             iTimer.Interval = IntervalToNextMinute();
   237             iTimer.Start();
   238 
   239             UpdateDisplay();
   240 
   241         }
   242 
   243         /// <summary>
   244         /// 
   245         /// </summary>
   246         private void UpdateDisplay()
   247         {
   248             //
   249             if (String.IsNullOrEmpty(iTextField.Text))
   250             {
   251                 //Time to show our time
   252                 iTextField.Text = DateTime.Now.ToString("t");
   253             }
   254             else
   255             {
   256                 //Do some screen saving                
   257                 iTextField.Text = "";
   258             }
   259 
   260             // Update our field
   261             iClient.SetField(iTextField);
   262 
   263             //Now make sure we save our screen from any running system monitor
   264             //We don't go into screen saving mode if our monitor is still on
   265             if (String.IsNullOrEmpty(iTextField.Text) && !MonitorPowerOn)
   266             {
   267                 //If text it empty it means we need to cool down our display even if system monitor is running
   268                 iClient.SetPriority(Priorities.SystemMonitor + 1);
   269             }
   270             else
   271             {
   272                 //Switch back to background then, leaving system monitor if any doing its magic
   273                 iClient.SetPriority(Priorities.Background);
   274             }
   275         }
   276 
   277         /// <summary>
   278         /// 
   279         /// </summary>
   280         /// <param name="sender"></param>
   281         /// <param name="e"></param>
   282         private void FormClientIdle_Shown(object sender, EventArgs e)
   283         {
   284             //Visible = false;
   285         }
   286     }
   287 
   288 }