Clients/Idle/FormClientIdle.cs
author StephaneLenclud
Wed, 03 Feb 2016 16:04:37 +0100
changeset 193 317fdfa4fbb6
parent 192 d0919a87be12
child 194 b0c0aa5aea05
permissions -rw-r--r--
Basic idle client functionality.
     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 SharpDisplayIdleClient
    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 
    48         public delegate void CloseDelegate();
    49         public delegate void CloseConnectionDelegate();
    50 
    51 
    52         public FormClientIdle()
    53         {
    54             InitializeComponent();
    55         }
    56 
    57         /// <summary>
    58         /// 
    59         /// </summary>
    60         /// <param name="sender"></param>
    61         /// <param name="e"></param>
    62         private void FormClientIdle_Load(object sender, EventArgs e)
    63         {
    64             //Display client
    65             iClient = new Client();
    66             iClient.CloseOrderEvent += OnCloseOrder;
    67             iClient.Open();
    68             iClient.SetName("Idle");
    69             iClient.SetPriority(SharpLib.Display.Priorities.Background);
    70             SetupDisplayClient();
    71 
    72             //Timer
    73             iTimer.Interval = IntervalToNextMinute();
    74             iTimer.Start();
    75         }
    76 
    77         /// <summary>
    78         /// 
    79         /// </summary>
    80         /// <returns></returns>
    81         public static int IntervalToNextMinute()
    82         {
    83             DateTime now = DateTime.Now;
    84             return 60000 - now.Millisecond;
    85         }
    86 
    87 
    88         /// <summary>
    89         /// 
    90         /// </summary>
    91         /// <returns></returns>
    92         public bool IsClientReady()
    93         {
    94             return (iClient != null && iClient.IsReady());
    95         }
    96 
    97         /// <summary>
    98         /// 
    99         /// </summary>
   100         public void SetupDisplayClient()
   101         {
   102             //Setup our layout
   103 
   104             //Set one column one line layout
   105             TableLayout layout = new TableLayout(1, 1);
   106             iClient.SetLayout(layout);
   107 
   108             //Setup our fields
   109             iAlignment = ContentAlignment.MiddleCenter;
   110             iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
   111 
   112             //Set our fields
   113             iClient.CreateFields(new DataField[]
   114             {
   115                 iTextField
   116             });
   117         }
   118 
   119         public void OnCloseOrder()
   120         {
   121             CloseThreadSafe();
   122         }
   123 
   124         /// <summary>
   125         ///
   126         /// </summary>
   127         public void CloseThreadSafe()
   128         {
   129             if (this.InvokeRequired)
   130             {
   131                 //Not in the proper thread, invoke ourselves
   132                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   133                 this.Invoke(d, new object[] { });
   134             }
   135             else
   136             {
   137                 //We are in the proper thread
   138                 Close();
   139             }
   140         }
   141 
   142         /// <summary>
   143         ///
   144         /// </summary>
   145         public void CloseConnectionThreadSafe()
   146         {
   147             if (this.InvokeRequired)
   148             {
   149                 //Not in the proper thread, invoke ourselves
   150                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   151                 this.Invoke(d, new object[] { });
   152             }
   153             else
   154             {
   155                 //We are in the proper thread
   156                 if (IsClientReady())
   157                 {
   158                     string sessionId = iClient.SessionId;
   159                     Trace.TraceInformation("Closing client: " + sessionId);
   160                     iClient.Close();
   161                     Trace.TraceInformation("Closed client: " + sessionId);
   162                 }
   163 
   164                 iClient = null;
   165             }
   166         }
   167 
   168         /// <summary>
   169         /// 
   170         /// </summary>
   171         /// <param name="sender"></param>
   172         /// <param name="e"></param>
   173         private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
   174         {
   175             CloseConnectionThreadSafe();
   176         }
   177 
   178 
   179         /// <summary>
   180         /// 
   181         /// </summary>
   182         /// <param name="sender"></param>
   183         /// <param name="e"></param>
   184         private void iTimer_Tick(object sender, EventArgs e)
   185         {
   186             //Timer
   187             iTimer.Interval = IntervalToNextMinute();
   188             iTimer.Start();
   189 
   190             //
   191             if (String.IsNullOrEmpty(iTextField.Text))
   192             {
   193                 //Time to show our time
   194                 iTextField.Text = DateTime.Now.ToString("t");
   195             }
   196             else
   197             {
   198                 //Do some screen saving
   199                 iTextField.Text = "";
   200             }
   201 
   202             // Update our field
   203             iClient.SetField(iTextField);
   204         }
   205 
   206         private void FormClientIdle_Shown(object sender, EventArgs e)
   207         {
   208             //Visible = false;
   209         }
   210     }
   211 
   212 }