Basic idle client functionality.
authorStephaneLenclud
Wed, 03 Feb 2016 16:04:37 +0100
changeset 193317fdfa4fbb6
parent 192 d0919a87be12
child 194 b0c0aa5aea05
Basic idle client functionality.
Clients/Idle/FormClientIdle.Designer.cs
Clients/Idle/FormClientIdle.cs
Clients/Idle/FormClientIdle.resx
Clients/Idle/Program.cs
     1.1 --- a/Clients/Idle/FormClientIdle.Designer.cs	Wed Feb 03 13:23:17 2016 +0100
     1.2 +++ b/Clients/Idle/FormClientIdle.Designer.cs	Wed Feb 03 16:04:37 2016 +0100
     1.3 @@ -1,4 +1,5 @@
     1.4 -namespace SharpDisplayIdleClient
     1.5 +
     1.6 +namespace SharpDisplayIdleClient
     1.7  {
     1.8      partial class FormClientIdle
     1.9      {
    1.10 @@ -28,20 +29,31 @@
    1.11          /// </summary>
    1.12          private void InitializeComponent()
    1.13          {
    1.14 +            this.components = new System.ComponentModel.Container();
    1.15 +            this.iTimer = new System.Windows.Forms.Timer(this.components);
    1.16              this.SuspendLayout();
    1.17              // 
    1.18 -            // FormIdleClient
    1.19 +            // iTimer
    1.20 +            // 
    1.21 +            this.iTimer.Tick += new System.EventHandler(this.iTimer_Tick);
    1.22 +            // 
    1.23 +            // FormClientIdle
    1.24              // 
    1.25              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    1.26              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    1.27              this.ClientSize = new System.Drawing.Size(531, 303);
    1.28 -            this.Name = "FormIdleClient";
    1.29 +            this.Name = "FormClientIdle";
    1.30              this.Text = "Sharp Display Idle Client";
    1.31 +            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormClientIdle_FormClosing);
    1.32 +            this.Load += new System.EventHandler(this.FormClientIdle_Load);
    1.33 +            this.Shown += new System.EventHandler(this.FormClientIdle_Shown);
    1.34              this.ResumeLayout(false);
    1.35  
    1.36          }
    1.37  
    1.38          #endregion
    1.39 +
    1.40 +        private System.Windows.Forms.Timer iTimer;
    1.41      }
    1.42  }
    1.43  
     2.1 --- a/Clients/Idle/FormClientIdle.cs	Wed Feb 03 13:23:17 2016 +0100
     2.2 +++ b/Clients/Idle/FormClientIdle.cs	Wed Feb 03 16:04:37 2016 +0100
     2.3 @@ -1,4 +1,23 @@
     2.4 -using System;
     2.5 +//
     2.6 +// Copyright (C) 2014-2016 Stéphane Lenclud.
     2.7 +//
     2.8 +// This file is part of SharpDisplayManager.
     2.9 +//
    2.10 +// SharpDisplayManager is free software: you can redistribute it and/or modify
    2.11 +// it under the terms of the GNU General Public License as published by
    2.12 +// the Free Software Foundation, either version 3 of the License, or
    2.13 +// (at your option) any later version.
    2.14 +//
    2.15 +// SharpDisplayManager is distributed in the hope that it will be useful,
    2.16 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.17 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.18 +// GNU General Public License for more details.
    2.19 +//
    2.20 +// You should have received a copy of the GNU General Public License
    2.21 +// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    2.22 +//
    2.23 +
    2.24 +using System;
    2.25  using System.Collections.Generic;
    2.26  using System.ComponentModel;
    2.27  using System.Data;
    2.28 @@ -7,16 +26,187 @@
    2.29  using System.Text;
    2.30  using System.Threading.Tasks;
    2.31  using System.Windows.Forms;
    2.32 +using System.Diagnostics;
    2.33 +using SharpLib.Display;
    2.34 +
    2.35  
    2.36  namespace SharpDisplayIdleClient
    2.37  {
    2.38 +
    2.39 +    /// <summary>
    2.40 +    /// Sharp Display Client designed to act as an idle client.
    2.41 +    /// It should take care of screen saving and other such concerns.
    2.42 +    /// </summary>
    2.43      public partial class FormClientIdle : Form
    2.44      {
    2.45          public StartParams Params { get; set; }
    2.46  
    2.47 +        Client iClient;
    2.48 +        ContentAlignment iAlignment;
    2.49 +        TextField iTextField;
    2.50 +
    2.51 +        public delegate void CloseDelegate();
    2.52 +        public delegate void CloseConnectionDelegate();
    2.53 +
    2.54 +
    2.55          public FormClientIdle()
    2.56          {
    2.57              InitializeComponent();
    2.58          }
    2.59 +
    2.60 +        /// <summary>
    2.61 +        /// 
    2.62 +        /// </summary>
    2.63 +        /// <param name="sender"></param>
    2.64 +        /// <param name="e"></param>
    2.65 +        private void FormClientIdle_Load(object sender, EventArgs e)
    2.66 +        {
    2.67 +            //Display client
    2.68 +            iClient = new Client();
    2.69 +            iClient.CloseOrderEvent += OnCloseOrder;
    2.70 +            iClient.Open();
    2.71 +            iClient.SetName("Idle");
    2.72 +            iClient.SetPriority(SharpLib.Display.Priorities.Background);
    2.73 +            SetupDisplayClient();
    2.74 +
    2.75 +            //Timer
    2.76 +            iTimer.Interval = IntervalToNextMinute();
    2.77 +            iTimer.Start();
    2.78 +        }
    2.79 +
    2.80 +        /// <summary>
    2.81 +        /// 
    2.82 +        /// </summary>
    2.83 +        /// <returns></returns>
    2.84 +        public static int IntervalToNextMinute()
    2.85 +        {
    2.86 +            DateTime now = DateTime.Now;
    2.87 +            return 60000 - now.Millisecond;
    2.88 +        }
    2.89 +
    2.90 +
    2.91 +        /// <summary>
    2.92 +        /// 
    2.93 +        /// </summary>
    2.94 +        /// <returns></returns>
    2.95 +        public bool IsClientReady()
    2.96 +        {
    2.97 +            return (iClient != null && iClient.IsReady());
    2.98 +        }
    2.99 +
   2.100 +        /// <summary>
   2.101 +        /// 
   2.102 +        /// </summary>
   2.103 +        public void SetupDisplayClient()
   2.104 +        {
   2.105 +            //Setup our layout
   2.106 +
   2.107 +            //Set one column one line layout
   2.108 +            TableLayout layout = new TableLayout(1, 1);
   2.109 +            iClient.SetLayout(layout);
   2.110 +
   2.111 +            //Setup our fields
   2.112 +            iAlignment = ContentAlignment.MiddleCenter;
   2.113 +            iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
   2.114 +
   2.115 +            //Set our fields
   2.116 +            iClient.CreateFields(new DataField[]
   2.117 +            {
   2.118 +                iTextField
   2.119 +            });
   2.120 +        }
   2.121 +
   2.122 +        public void OnCloseOrder()
   2.123 +        {
   2.124 +            CloseThreadSafe();
   2.125 +        }
   2.126 +
   2.127 +        /// <summary>
   2.128 +        ///
   2.129 +        /// </summary>
   2.130 +        public void CloseThreadSafe()
   2.131 +        {
   2.132 +            if (this.InvokeRequired)
   2.133 +            {
   2.134 +                //Not in the proper thread, invoke ourselves
   2.135 +                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   2.136 +                this.Invoke(d, new object[] { });
   2.137 +            }
   2.138 +            else
   2.139 +            {
   2.140 +                //We are in the proper thread
   2.141 +                Close();
   2.142 +            }
   2.143 +        }
   2.144 +
   2.145 +        /// <summary>
   2.146 +        ///
   2.147 +        /// </summary>
   2.148 +        public void CloseConnectionThreadSafe()
   2.149 +        {
   2.150 +            if (this.InvokeRequired)
   2.151 +            {
   2.152 +                //Not in the proper thread, invoke ourselves
   2.153 +                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   2.154 +                this.Invoke(d, new object[] { });
   2.155 +            }
   2.156 +            else
   2.157 +            {
   2.158 +                //We are in the proper thread
   2.159 +                if (IsClientReady())
   2.160 +                {
   2.161 +                    string sessionId = iClient.SessionId;
   2.162 +                    Trace.TraceInformation("Closing client: " + sessionId);
   2.163 +                    iClient.Close();
   2.164 +                    Trace.TraceInformation("Closed client: " + sessionId);
   2.165 +                }
   2.166 +
   2.167 +                iClient = null;
   2.168 +            }
   2.169 +        }
   2.170 +
   2.171 +        /// <summary>
   2.172 +        /// 
   2.173 +        /// </summary>
   2.174 +        /// <param name="sender"></param>
   2.175 +        /// <param name="e"></param>
   2.176 +        private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
   2.177 +        {
   2.178 +            CloseConnectionThreadSafe();
   2.179 +        }
   2.180 +
   2.181 +
   2.182 +        /// <summary>
   2.183 +        /// 
   2.184 +        /// </summary>
   2.185 +        /// <param name="sender"></param>
   2.186 +        /// <param name="e"></param>
   2.187 +        private void iTimer_Tick(object sender, EventArgs e)
   2.188 +        {
   2.189 +            //Timer
   2.190 +            iTimer.Interval = IntervalToNextMinute();
   2.191 +            iTimer.Start();
   2.192 +
   2.193 +            //
   2.194 +            if (String.IsNullOrEmpty(iTextField.Text))
   2.195 +            {
   2.196 +                //Time to show our time
   2.197 +                iTextField.Text = DateTime.Now.ToString("t");
   2.198 +            }
   2.199 +            else
   2.200 +            {
   2.201 +                //Do some screen saving
   2.202 +                iTextField.Text = "";
   2.203 +            }
   2.204 +
   2.205 +            // Update our field
   2.206 +            iClient.SetField(iTextField);
   2.207 +        }
   2.208 +
   2.209 +        private void FormClientIdle_Shown(object sender, EventArgs e)
   2.210 +        {
   2.211 +            //Visible = false;
   2.212 +        }
   2.213      }
   2.214 +
   2.215  }
     3.1 --- a/Clients/Idle/FormClientIdle.resx	Wed Feb 03 13:23:17 2016 +0100
     3.2 +++ b/Clients/Idle/FormClientIdle.resx	Wed Feb 03 16:04:37 2016 +0100
     3.3 @@ -117,4 +117,7 @@
     3.4    <resheader name="writer">
     3.5      <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
     3.6    </resheader>
     3.7 +  <metadata name="iTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     3.8 +    <value>17, 17</value>
     3.9 +  </metadata>
    3.10  </root>
    3.11 \ No newline at end of file
     4.1 --- a/Clients/Idle/Program.cs	Wed Feb 03 13:23:17 2016 +0100
     4.2 +++ b/Clients/Idle/Program.cs	Wed Feb 03 16:04:37 2016 +0100
     4.3 @@ -1,5 +1,5 @@
     4.4  //
     4.5 -// Copyright (C) 2014-2015 Stéphane Lenclud.
     4.6 +// Copyright (C) 2014-2016 Stéphane Lenclud.
     4.7  //
     4.8  // This file is part of SharpDisplayManager.
     4.9  //
    4.10 @@ -52,6 +52,8 @@
    4.11              Application.SetCompatibleTextRenderingDefault(false);
    4.12              FormClientIdle form = new FormClientIdle();
    4.13              form.Params = (StartParams)aParams;
    4.14 +            form.WindowState = FormWindowState.Minimized;
    4.15 +            form.ShowInTaskbar = false;
    4.16              Application.Run(form);
    4.17          }
    4.18