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.
StephaneLenclud@193
     1
//
StephaneLenclud@193
     2
// Copyright (C) 2014-2016 Stéphane Lenclud.
StephaneLenclud@193
     3
//
StephaneLenclud@193
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@193
     5
//
StephaneLenclud@193
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@193
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@193
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@193
     9
// (at your option) any later version.
StephaneLenclud@193
    10
//
StephaneLenclud@193
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@193
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@193
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@193
    14
// GNU General Public License for more details.
StephaneLenclud@193
    15
//
StephaneLenclud@193
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@193
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@193
    18
//
StephaneLenclud@193
    19
StephaneLenclud@193
    20
using System;
StephaneLenclud@192
    21
using System.Collections.Generic;
StephaneLenclud@192
    22
using System.ComponentModel;
StephaneLenclud@192
    23
using System.Data;
StephaneLenclud@192
    24
using System.Drawing;
StephaneLenclud@192
    25
using System.Linq;
StephaneLenclud@192
    26
using System.Text;
StephaneLenclud@192
    27
using System.Threading.Tasks;
StephaneLenclud@192
    28
using System.Windows.Forms;
StephaneLenclud@193
    29
using System.Diagnostics;
StephaneLenclud@193
    30
using SharpLib.Display;
StephaneLenclud@193
    31
StephaneLenclud@192
    32
StephaneLenclud@192
    33
namespace SharpDisplayIdleClient
StephaneLenclud@192
    34
{
StephaneLenclud@193
    35
StephaneLenclud@193
    36
    /// <summary>
StephaneLenclud@193
    37
    /// Sharp Display Client designed to act as an idle client.
StephaneLenclud@193
    38
    /// It should take care of screen saving and other such concerns.
StephaneLenclud@193
    39
    /// </summary>
StephaneLenclud@192
    40
    public partial class FormClientIdle : Form
StephaneLenclud@192
    41
    {
StephaneLenclud@192
    42
        public StartParams Params { get; set; }
StephaneLenclud@192
    43
StephaneLenclud@193
    44
        Client iClient;
StephaneLenclud@193
    45
        ContentAlignment iAlignment;
StephaneLenclud@193
    46
        TextField iTextField;
StephaneLenclud@193
    47
StephaneLenclud@193
    48
        public delegate void CloseDelegate();
StephaneLenclud@193
    49
        public delegate void CloseConnectionDelegate();
StephaneLenclud@193
    50
StephaneLenclud@193
    51
StephaneLenclud@192
    52
        public FormClientIdle()
StephaneLenclud@192
    53
        {
StephaneLenclud@192
    54
            InitializeComponent();
StephaneLenclud@192
    55
        }
StephaneLenclud@193
    56
StephaneLenclud@193
    57
        /// <summary>
StephaneLenclud@193
    58
        /// 
StephaneLenclud@193
    59
        /// </summary>
StephaneLenclud@193
    60
        /// <param name="sender"></param>
StephaneLenclud@193
    61
        /// <param name="e"></param>
StephaneLenclud@193
    62
        private void FormClientIdle_Load(object sender, EventArgs e)
StephaneLenclud@193
    63
        {
StephaneLenclud@193
    64
            //Display client
StephaneLenclud@193
    65
            iClient = new Client();
StephaneLenclud@193
    66
            iClient.CloseOrderEvent += OnCloseOrder;
StephaneLenclud@193
    67
            iClient.Open();
StephaneLenclud@193
    68
            iClient.SetName("Idle");
StephaneLenclud@193
    69
            iClient.SetPriority(SharpLib.Display.Priorities.Background);
StephaneLenclud@193
    70
            SetupDisplayClient();
StephaneLenclud@193
    71
StephaneLenclud@193
    72
            //Timer
StephaneLenclud@193
    73
            iTimer.Interval = IntervalToNextMinute();
StephaneLenclud@193
    74
            iTimer.Start();
StephaneLenclud@193
    75
        }
StephaneLenclud@193
    76
StephaneLenclud@193
    77
        /// <summary>
StephaneLenclud@193
    78
        /// 
StephaneLenclud@193
    79
        /// </summary>
StephaneLenclud@193
    80
        /// <returns></returns>
StephaneLenclud@193
    81
        public static int IntervalToNextMinute()
StephaneLenclud@193
    82
        {
StephaneLenclud@193
    83
            DateTime now = DateTime.Now;
StephaneLenclud@193
    84
            return 60000 - now.Millisecond;
StephaneLenclud@193
    85
        }
StephaneLenclud@193
    86
StephaneLenclud@193
    87
StephaneLenclud@193
    88
        /// <summary>
StephaneLenclud@193
    89
        /// 
StephaneLenclud@193
    90
        /// </summary>
StephaneLenclud@193
    91
        /// <returns></returns>
StephaneLenclud@193
    92
        public bool IsClientReady()
StephaneLenclud@193
    93
        {
StephaneLenclud@193
    94
            return (iClient != null && iClient.IsReady());
StephaneLenclud@193
    95
        }
StephaneLenclud@193
    96
StephaneLenclud@193
    97
        /// <summary>
StephaneLenclud@193
    98
        /// 
StephaneLenclud@193
    99
        /// </summary>
StephaneLenclud@193
   100
        public void SetupDisplayClient()
StephaneLenclud@193
   101
        {
StephaneLenclud@193
   102
            //Setup our layout
StephaneLenclud@193
   103
StephaneLenclud@193
   104
            //Set one column one line layout
StephaneLenclud@193
   105
            TableLayout layout = new TableLayout(1, 1);
StephaneLenclud@193
   106
            iClient.SetLayout(layout);
StephaneLenclud@193
   107
StephaneLenclud@193
   108
            //Setup our fields
StephaneLenclud@193
   109
            iAlignment = ContentAlignment.MiddleCenter;
StephaneLenclud@193
   110
            iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
StephaneLenclud@193
   111
StephaneLenclud@193
   112
            //Set our fields
StephaneLenclud@193
   113
            iClient.CreateFields(new DataField[]
StephaneLenclud@193
   114
            {
StephaneLenclud@193
   115
                iTextField
StephaneLenclud@193
   116
            });
StephaneLenclud@193
   117
        }
StephaneLenclud@193
   118
StephaneLenclud@193
   119
        public void OnCloseOrder()
StephaneLenclud@193
   120
        {
StephaneLenclud@193
   121
            CloseThreadSafe();
StephaneLenclud@193
   122
        }
StephaneLenclud@193
   123
StephaneLenclud@193
   124
        /// <summary>
StephaneLenclud@193
   125
        ///
StephaneLenclud@193
   126
        /// </summary>
StephaneLenclud@193
   127
        public void CloseThreadSafe()
StephaneLenclud@193
   128
        {
StephaneLenclud@193
   129
            if (this.InvokeRequired)
StephaneLenclud@193
   130
            {
StephaneLenclud@193
   131
                //Not in the proper thread, invoke ourselves
StephaneLenclud@193
   132
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
StephaneLenclud@193
   133
                this.Invoke(d, new object[] { });
StephaneLenclud@193
   134
            }
StephaneLenclud@193
   135
            else
StephaneLenclud@193
   136
            {
StephaneLenclud@193
   137
                //We are in the proper thread
StephaneLenclud@193
   138
                Close();
StephaneLenclud@193
   139
            }
StephaneLenclud@193
   140
        }
StephaneLenclud@193
   141
StephaneLenclud@193
   142
        /// <summary>
StephaneLenclud@193
   143
        ///
StephaneLenclud@193
   144
        /// </summary>
StephaneLenclud@193
   145
        public void CloseConnectionThreadSafe()
StephaneLenclud@193
   146
        {
StephaneLenclud@193
   147
            if (this.InvokeRequired)
StephaneLenclud@193
   148
            {
StephaneLenclud@193
   149
                //Not in the proper thread, invoke ourselves
StephaneLenclud@193
   150
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
StephaneLenclud@193
   151
                this.Invoke(d, new object[] { });
StephaneLenclud@193
   152
            }
StephaneLenclud@193
   153
            else
StephaneLenclud@193
   154
            {
StephaneLenclud@193
   155
                //We are in the proper thread
StephaneLenclud@193
   156
                if (IsClientReady())
StephaneLenclud@193
   157
                {
StephaneLenclud@193
   158
                    string sessionId = iClient.SessionId;
StephaneLenclud@193
   159
                    Trace.TraceInformation("Closing client: " + sessionId);
StephaneLenclud@193
   160
                    iClient.Close();
StephaneLenclud@193
   161
                    Trace.TraceInformation("Closed client: " + sessionId);
StephaneLenclud@193
   162
                }
StephaneLenclud@193
   163
StephaneLenclud@193
   164
                iClient = null;
StephaneLenclud@193
   165
            }
StephaneLenclud@193
   166
        }
StephaneLenclud@193
   167
StephaneLenclud@193
   168
        /// <summary>
StephaneLenclud@193
   169
        /// 
StephaneLenclud@193
   170
        /// </summary>
StephaneLenclud@193
   171
        /// <param name="sender"></param>
StephaneLenclud@193
   172
        /// <param name="e"></param>
StephaneLenclud@193
   173
        private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
StephaneLenclud@193
   174
        {
StephaneLenclud@193
   175
            CloseConnectionThreadSafe();
StephaneLenclud@193
   176
        }
StephaneLenclud@193
   177
StephaneLenclud@193
   178
StephaneLenclud@193
   179
        /// <summary>
StephaneLenclud@193
   180
        /// 
StephaneLenclud@193
   181
        /// </summary>
StephaneLenclud@193
   182
        /// <param name="sender"></param>
StephaneLenclud@193
   183
        /// <param name="e"></param>
StephaneLenclud@193
   184
        private void iTimer_Tick(object sender, EventArgs e)
StephaneLenclud@193
   185
        {
StephaneLenclud@193
   186
            //Timer
StephaneLenclud@193
   187
            iTimer.Interval = IntervalToNextMinute();
StephaneLenclud@193
   188
            iTimer.Start();
StephaneLenclud@193
   189
StephaneLenclud@193
   190
            //
StephaneLenclud@193
   191
            if (String.IsNullOrEmpty(iTextField.Text))
StephaneLenclud@193
   192
            {
StephaneLenclud@193
   193
                //Time to show our time
StephaneLenclud@193
   194
                iTextField.Text = DateTime.Now.ToString("t");
StephaneLenclud@193
   195
            }
StephaneLenclud@193
   196
            else
StephaneLenclud@193
   197
            {
StephaneLenclud@193
   198
                //Do some screen saving
StephaneLenclud@193
   199
                iTextField.Text = "";
StephaneLenclud@193
   200
            }
StephaneLenclud@193
   201
StephaneLenclud@193
   202
            // Update our field
StephaneLenclud@193
   203
            iClient.SetField(iTextField);
StephaneLenclud@193
   204
        }
StephaneLenclud@193
   205
StephaneLenclud@193
   206
        private void FormClientIdle_Shown(object sender, EventArgs e)
StephaneLenclud@193
   207
        {
StephaneLenclud@193
   208
            //Visible = false;
StephaneLenclud@193
   209
        }
StephaneLenclud@192
   210
    }
StephaneLenclud@193
   211
StephaneLenclud@192
   212
}