StephaneLenclud@193: // StephaneLenclud@193: // Copyright (C) 2014-2016 Stéphane Lenclud. StephaneLenclud@193: // StephaneLenclud@193: // This file is part of SharpDisplayManager. StephaneLenclud@193: // StephaneLenclud@193: // SharpDisplayManager is free software: you can redistribute it and/or modify StephaneLenclud@193: // it under the terms of the GNU General Public License as published by StephaneLenclud@193: // the Free Software Foundation, either version 3 of the License, or StephaneLenclud@193: // (at your option) any later version. StephaneLenclud@193: // StephaneLenclud@193: // SharpDisplayManager is distributed in the hope that it will be useful, StephaneLenclud@193: // but WITHOUT ANY WARRANTY; without even the implied warranty of StephaneLenclud@193: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the StephaneLenclud@193: // GNU General Public License for more details. StephaneLenclud@193: // StephaneLenclud@193: // You should have received a copy of the GNU General Public License StephaneLenclud@193: // along with SharpDisplayManager. If not, see . StephaneLenclud@193: // StephaneLenclud@193: StephaneLenclud@193: using System; StephaneLenclud@192: using System.Collections.Generic; StephaneLenclud@192: using System.ComponentModel; StephaneLenclud@192: using System.Data; StephaneLenclud@192: using System.Drawing; StephaneLenclud@192: using System.Linq; StephaneLenclud@192: using System.Text; StephaneLenclud@192: using System.Threading.Tasks; StephaneLenclud@192: using System.Windows.Forms; StephaneLenclud@193: using System.Diagnostics; StephaneLenclud@193: using SharpLib.Display; StephaneLenclud@193: StephaneLenclud@192: StephaneLenclud@192: namespace SharpDisplayIdleClient StephaneLenclud@192: { StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// Sharp Display Client designed to act as an idle client. StephaneLenclud@193: /// It should take care of screen saving and other such concerns. StephaneLenclud@193: /// StephaneLenclud@192: public partial class FormClientIdle : Form StephaneLenclud@192: { StephaneLenclud@192: public StartParams Params { get; set; } StephaneLenclud@192: StephaneLenclud@193: Client iClient; StephaneLenclud@193: ContentAlignment iAlignment; StephaneLenclud@193: TextField iTextField; StephaneLenclud@193: StephaneLenclud@193: public delegate void CloseDelegate(); StephaneLenclud@193: public delegate void CloseConnectionDelegate(); StephaneLenclud@193: StephaneLenclud@193: StephaneLenclud@192: public FormClientIdle() StephaneLenclud@192: { StephaneLenclud@192: InitializeComponent(); StephaneLenclud@192: } StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: private void FormClientIdle_Load(object sender, EventArgs e) StephaneLenclud@193: { StephaneLenclud@193: //Display client StephaneLenclud@193: iClient = new Client(); StephaneLenclud@193: iClient.CloseOrderEvent += OnCloseOrder; StephaneLenclud@193: iClient.Open(); StephaneLenclud@193: iClient.SetName("Idle"); StephaneLenclud@193: iClient.SetPriority(SharpLib.Display.Priorities.Background); StephaneLenclud@193: SetupDisplayClient(); StephaneLenclud@193: StephaneLenclud@193: //Timer StephaneLenclud@193: iTimer.Interval = IntervalToNextMinute(); StephaneLenclud@193: iTimer.Start(); StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: public static int IntervalToNextMinute() StephaneLenclud@193: { StephaneLenclud@193: DateTime now = DateTime.Now; StephaneLenclud@193: return 60000 - now.Millisecond; StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: public bool IsClientReady() StephaneLenclud@193: { StephaneLenclud@193: return (iClient != null && iClient.IsReady()); StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: public void SetupDisplayClient() StephaneLenclud@193: { StephaneLenclud@193: //Setup our layout StephaneLenclud@193: StephaneLenclud@193: //Set one column one line layout StephaneLenclud@193: TableLayout layout = new TableLayout(1, 1); StephaneLenclud@193: iClient.SetLayout(layout); StephaneLenclud@193: StephaneLenclud@193: //Setup our fields StephaneLenclud@193: iAlignment = ContentAlignment.MiddleCenter; StephaneLenclud@193: iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0); StephaneLenclud@193: StephaneLenclud@193: //Set our fields StephaneLenclud@193: iClient.CreateFields(new DataField[] StephaneLenclud@193: { StephaneLenclud@193: iTextField StephaneLenclud@193: }); StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: public void OnCloseOrder() StephaneLenclud@193: { StephaneLenclud@193: CloseThreadSafe(); StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: public void CloseThreadSafe() StephaneLenclud@193: { StephaneLenclud@193: if (this.InvokeRequired) StephaneLenclud@193: { StephaneLenclud@193: //Not in the proper thread, invoke ourselves StephaneLenclud@193: CloseDelegate d = new CloseDelegate(CloseThreadSafe); StephaneLenclud@193: this.Invoke(d, new object[] { }); StephaneLenclud@193: } StephaneLenclud@193: else StephaneLenclud@193: { StephaneLenclud@193: //We are in the proper thread StephaneLenclud@193: Close(); StephaneLenclud@193: } StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: public void CloseConnectionThreadSafe() StephaneLenclud@193: { StephaneLenclud@193: if (this.InvokeRequired) StephaneLenclud@193: { StephaneLenclud@193: //Not in the proper thread, invoke ourselves StephaneLenclud@193: CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe); StephaneLenclud@193: this.Invoke(d, new object[] { }); StephaneLenclud@193: } StephaneLenclud@193: else StephaneLenclud@193: { StephaneLenclud@193: //We are in the proper thread StephaneLenclud@193: if (IsClientReady()) StephaneLenclud@193: { StephaneLenclud@193: string sessionId = iClient.SessionId; StephaneLenclud@193: Trace.TraceInformation("Closing client: " + sessionId); StephaneLenclud@193: iClient.Close(); StephaneLenclud@193: Trace.TraceInformation("Closed client: " + sessionId); StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: iClient = null; StephaneLenclud@193: } StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e) StephaneLenclud@193: { StephaneLenclud@193: CloseConnectionThreadSafe(); StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: /// StephaneLenclud@193: private void iTimer_Tick(object sender, EventArgs e) StephaneLenclud@193: { StephaneLenclud@193: //Timer StephaneLenclud@193: iTimer.Interval = IntervalToNextMinute(); StephaneLenclud@193: iTimer.Start(); StephaneLenclud@193: StephaneLenclud@193: // StephaneLenclud@193: if (String.IsNullOrEmpty(iTextField.Text)) StephaneLenclud@193: { StephaneLenclud@193: //Time to show our time StephaneLenclud@193: iTextField.Text = DateTime.Now.ToString("t"); StephaneLenclud@193: } StephaneLenclud@193: else StephaneLenclud@193: { StephaneLenclud@193: //Do some screen saving StephaneLenclud@193: iTextField.Text = ""; StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: // Update our field StephaneLenclud@193: iClient.SetField(iTextField); StephaneLenclud@193: } StephaneLenclud@193: StephaneLenclud@193: private void FormClientIdle_Shown(object sender, EventArgs e) StephaneLenclud@193: { StephaneLenclud@193: //Visible = false; StephaneLenclud@193: } StephaneLenclud@192: } StephaneLenclud@193: StephaneLenclud@192: }