Basic idle client functionality.
2 // Copyright (C) 2014-2016 Stéphane Lenclud.
4 // This file is part of SharpDisplayManager.
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.
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.
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/>.
21 using System.Collections.Generic;
22 using System.ComponentModel;
27 using System.Threading.Tasks;
28 using System.Windows.Forms;
29 using System.Diagnostics;
30 using SharpLib.Display;
33 namespace SharpDisplayIdleClient
37 /// Sharp Display Client designed to act as an idle client.
38 /// It should take care of screen saving and other such concerns.
40 public partial class FormClientIdle : Form
42 public StartParams Params { get; set; }
45 ContentAlignment iAlignment;
48 public delegate void CloseDelegate();
49 public delegate void CloseConnectionDelegate();
52 public FormClientIdle()
54 InitializeComponent();
60 /// <param name="sender"></param>
61 /// <param name="e"></param>
62 private void FormClientIdle_Load(object sender, EventArgs e)
65 iClient = new Client();
66 iClient.CloseOrderEvent += OnCloseOrder;
68 iClient.SetName("Idle");
69 iClient.SetPriority(SharpLib.Display.Priorities.Background);
73 iTimer.Interval = IntervalToNextMinute();
80 /// <returns></returns>
81 public static int IntervalToNextMinute()
83 DateTime now = DateTime.Now;
84 return 60000 - now.Millisecond;
91 /// <returns></returns>
92 public bool IsClientReady()
94 return (iClient != null && iClient.IsReady());
100 public void SetupDisplayClient()
104 //Set one column one line layout
105 TableLayout layout = new TableLayout(1, 1);
106 iClient.SetLayout(layout);
109 iAlignment = ContentAlignment.MiddleCenter;
110 iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
113 iClient.CreateFields(new DataField[]
119 public void OnCloseOrder()
127 public void CloseThreadSafe()
129 if (this.InvokeRequired)
131 //Not in the proper thread, invoke ourselves
132 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
133 this.Invoke(d, new object[] { });
137 //We are in the proper thread
145 public void CloseConnectionThreadSafe()
147 if (this.InvokeRequired)
149 //Not in the proper thread, invoke ourselves
150 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
151 this.Invoke(d, new object[] { });
155 //We are in the proper thread
158 string sessionId = iClient.SessionId;
159 Trace.TraceInformation("Closing client: " + sessionId);
161 Trace.TraceInformation("Closed client: " + sessionId);
171 /// <param name="sender"></param>
172 /// <param name="e"></param>
173 private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
175 CloseConnectionThreadSafe();
182 /// <param name="sender"></param>
183 /// <param name="e"></param>
184 private void iTimer_Tick(object sender, EventArgs e)
187 iTimer.Interval = IntervalToNextMinute();
191 if (String.IsNullOrEmpty(iTextField.Text))
193 //Time to show our time
194 iTextField.Text = DateTime.Now.ToString("t");
198 //Do some screen saving
199 iTextField.Text = "";
203 iClient.SetField(iTextField);
206 private void FormClientIdle_Shown(object sender, EventArgs e)