using SteamWare; using System; using System.Threading; using System.Timers; using System.Web.UI; namespace NKC_WF.WebUserControls { public partial class cmp_barcode : BaseUserControl, IDisposable { #region Public Properties /// /// Comando acquisito da Barcode /// public string inputAcquired { get { return hfLastCmd.Value; } set { hfLastCmd.Value = value; } } #endregion Public Properties #region Public Methods public override void Dispose() { UiTimer.Tick -= UiTimer_Tick; UiTimer.Dispose(); base.Dispose(); } /// /// Reset input del barcode SENZA generare evento... /// /// public void resetInput() { valueRead = ""; txtBarcode.Focus(); } /// /// reset messaggio /// public void resetMessage() { lbtReset.Visible = false; lblOutput.Text = ""; } /// /// Gestione output da mostrare (opzionale /// /// Enum delle classi permesse /// public void showOutput(AppData.cssClass cssClass, string messaggio) { // In primis: mostro qualcosa SOLO SE ho del testo lbtReset.Visible = !string.IsNullOrEmpty(messaggio); lblOutput.Text = messaggio; lbtReset.CssClass = $"btn btn-sm btn-block py-0 btn-{cssClass}"; } #endregion Public Methods #region Protected Properties /// /// Ultima esecuzione comando (x forzare reset) /// protected DateTime lastDtInput { get { DateTime answ = DateTime.Now; if (!string.IsNullOrEmpty(hfLastDtInput.Value)) { answ = DateTime.Parse(hfLastDtInput.Value); } return answ; } set { hfLastDtInput.Value = $"{value:yyyy-MM-dd HH:mm:ss}"; } } /// /// Valore acquisito in lettura barcode /// protected string valueRead { get { return txtBarcode.Text.Trim(); } set { txtBarcode.Text = value; } } #endregion Protected Properties #region Protected Methods /// /// reset lettura /// /// /// protected void lbtReset_Click(object sender, EventArgs e) { Response.Redirect(Request.RawUrl); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { resetInput(); resetMessage(); setTimer(); test4reload(); } else if (string.IsNullOrEmpty(inputAcquired)) { checkRaiseEv(); } } /// /// evento modifica testo /// /// /// protected void txtBarcode_TextChanged(object sender, EventArgs e) { lastDtInput = DateTime.Now; checkRaiseEv(); } protected void UiTimer_Tick(object sender, EventArgs e) { test4reload(); } #endregion Protected Methods #region Private Methods /// /// Verifica se inviare evento /// private void checkRaiseEv() { if (valueRead != inputAcquired) { // registro NUOVO cmd inputAcquired = valueRead; // alzo evento SOLO SE il nuovo valore è diverso da null... if (!string.IsNullOrEmpty(inputAcquired)) { raiseEvent(); } } else { if (string.IsNullOrEmpty(inputAcquired)) { resetInput(); } } resetInput(); } /// /// imposta il tempo di scadenza del timer x il refresh del componente /// private void setTimer() { int timer = memLayer.ML.CRI("timerBarcode"); timer = timer > 0 ? timer : 10000; UiTimer.Interval = timer; } private void test4reload() { // controllo, se ultima operazione > smartForceReloadPagina_ms --> reload! var tsLastUpdate = DateTime.Now.Subtract(lastDtInput); if (tsLastUpdate.TotalMilliseconds > memLayer.ML.CRI("smartForceReloadPagina_ms")) { lgInfo($"cmp_barcode | Reload page for timeout: {tsLastUpdate.TotalSeconds} sec from last update"); inputAcquired = "##"; Response.Redirect(Request.RawUrl); } } #endregion Private Methods } }