public DebugForm() { InitializeComponent(); users = DatabaseHandler.GetUsers(); lbKeyUsers.DataSource = users; if (!DebugLock) grbLock.Enabled = false; if (!DebugPrinter) grbPrinter.Enabled = false; instance = this; }
/// <summary> /// Backgroundworker DoWork method: represents the routine for the startup of the cashpoint. /// Step 1: check completeness of config file /// Step 2: check database connectivity /// Step 3: connect to printer device /// Step 4: connect to addimat lock /// Step 5: load profiles (including articles), users, config /// Step 6: initialize status update sender object /// Step 7: create forms, e.g. RegisterForm() + draw articles, popup(), submit cashpoint state /// Step 8: debug options /// </summary> void InitializeCashpoint(object sender, DoWorkEventArgs e) { bgwInitialize.ReportProgress(2, "StartupRoutine"); bool configCorrect = false, databaseCorrect = false, printerFound = false, lockFound = false, dataLoaded = false; int currentStep = 0; int goToStep; try { goToStep = Convert.ToInt32(e.Argument); } catch (Exception) { goToStep = 0; } #region step 1 currentStep++; bgwInitialize.ReportProgress(0, currentStep); if (goToStep <= 1) do { try { bgwInitialize.ReportProgress(1, ""); ConfigReader = new ConfigurationReader(ConfigurationReader.CONFIGURATION_FILE_NAME); ConfigReader.Check(); configCorrect = true; } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); bgwInitialize.ReportProgress(1, ex.Message); System.Threading.Thread.Sleep(2500); bgwInitialize.ReportProgress(1, ""); System.Threading.Thread.Sleep(750); } } while (!configCorrect); #endregion #region step 2 currentStep++; bgwInitialize.ReportProgress(0, currentStep); if (goToStep <= 2) do { try { bool missing = false; bgwInitialize.ReportProgress(1, ""); DatabaseHandler.ConnectionString = DatabaseHandler.GenerateConnectionString(ConfigReader.GetValue("server")); for (int i = 0; i < 3; i++) if (!DatabaseHandler.TestConnection()) throw new Exception("Verbindung konnte nicht hergestellt werden"); CashpointSettings = DatabaseHandler.GetSettings(); // check config settings from database string[] required = new string[] { "DoPrint", "IsEnabled", "IsServiceMode", "OrganizationName", "AutoRefresh", "AutoRefreshInterval" }; foreach (string s in required) if (CashpointSettings[s] == null) { missing = true; bgwInitialize.ReportProgress(1, "Benötigte Informationen konnten nicht geladen werden (\"" + s + "\")"); System.Threading.Thread.Sleep(250); } databaseCorrect = !missing; } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); bgwInitialize.ReportProgress(1, ex.Message); System.Threading.Thread.Sleep(2500); bgwInitialize.ReportProgress(1, ex.Message); System.Threading.Thread.Sleep(750); } } while (!databaseCorrect); #endregion #region step 3 currentStep++; bgwInitialize.ReportProgress(0, currentStep); if (goToStep <= 3) do { try { if (printDevice != null) { while (printDevice.PrintStatus == PrintStatus.Printing || _closePrinter.Visible) System.Threading.Thread.Sleep(250); printDevice.Dispose(); } bgwInitialize.ReportProgress(1, ""); printDevice = new Printer(ConfigReader.GetValue("PrinterName"), !DebugPrinter); printDevice.DeviceStatusChanged += printDevice_DeviceStatusChanged; printDevice.CheckAvailability(); printerFound = true; printDevice_DeviceStatusChanged(this, new Microsoft.PointOfService.StatusUpdateEventArgs(printDevice.DeviceStatusCode)); } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); if (!ex.Message.Contains("Fehler beim Setzen des aktuellen Kassenstatus.")) bgwInitialize.ReportProgress(1, "Druckerverbindung konnte nicht hergestellt werden.\n" + ex.Message); System.Threading.Thread.Sleep(2000); } } while (!printerFound); #endregion #region step 4 currentStep++; bgwInitialize.ReportProgress(0, currentStep); // check if "ignorelock" is configured string result; if (ConfigReader.TryGetValue("NOLOCK", out result)) IgnoreLock = true; if (goToStep <= 4 && !IgnoreLock) { do { try { bgwInitialize.ReportProgress(1, ""); string errmsg = ""; if (addimatLock != null) addimatLock.CloseComPort(); addimatLock = new VirtualComPort(!DebugLock); addimatLock.PortReadEvent += new VirtualComPort.ReadEventHandler(addimatLock_PortReadEvent); if (!addimatLock.OpenComPort(ConfigReader.GetValue("Lockport"), ref errmsg)) throw new Exception(errmsg); Application.DoEvents(); tLock.Start(); Application.DoEvents(); lockFound = true; } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); bgwInitialize.ReportProgress(1, "Verbindungsaufbau zum Kellnerschloss fehlgeschlagen\n" + ex.Message); System.Threading.Thread.Sleep(5000); } } while (!lockFound); } #endregion #region step 5 currentStep++; bgwInitialize.ReportProgress(0, currentStep); if (goToStep <= 5) do { try { bgwInitialize.ReportProgress(1, ""); CashpointSettings = DatabaseHandler.GetSettings(); this.users = DatabaseHandler.GetUsers(); this.profiles = DatabaseHandler.GetProfiles(); dataLoaded = true; } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); bgwInitialize.ReportProgress(1, ex.Message); System.Threading.Thread.Sleep(2000); } } while (!dataLoaded); #endregion #region step 6 StatusUpdater updater = new StatusUpdater(LoginForm.CashpointSettings["LiveViewEndpoint"]); updater.Start(); #endregion #region step 7 currentStep++; bgwInitialize.ReportProgress(0, currentStep); if (goToStep <= 6) try { regForm = new DepositRegisterForm(profiles, users, printDevice); if (CashpointSettings["IsEnabled"] == "True") // cashpoint unlocked { StatusUpdater.Instance.Update(null, CashpointAction.ACTION_LOGIN); string mode; if (ConfigReader.TryGetValue("Mode", out mode)) // is auto mode ? { if (mode.ToLower() == "auto") // auto mode { if (CashpointSettings["IsServiceMode"] == "False") bgwInitialize.ReportProgress(2, "PinLogin"); else bgwInitialize.ReportProgress(2, "KeyLogin"); } else // no auto, config.ini defined { string value = ConfigReader.GetValue("Mode"); switch (value.ToLower()) { case "pin": bgwInitialize.ReportProgress(2, "PinLogin"); break; case "lock": bgwInitialize.ReportProgress(2, "KeyLogin"); break; default: // invalid value for attribute "mode", so use database setting if (CashpointSettings["IsServiceMode"] == "False") bgwInitialize.ReportProgress(2, "PinLogin"); else bgwInitialize.ReportProgress(2, "KeyLogin"); break; } } } else // no mode defined, use database setting { if (CashpointSettings["IsServiceMode"] == "False") bgwInitialize.ReportProgress(2, "PinLogin"); else bgwInitialize.ReportProgress(2, "KeyLogin"); } string doPrintConfigIni; if (ConfigReader.TryGetValue("Printer", out doPrintConfigIni)) { try { DepositRegisterForm.DoPrint = Convert.ToBoolean(doPrintConfigIni); } catch (Exception) { DepositRegisterForm.DoPrint = null; } } else DepositRegisterForm.DoPrint = null; } else // cashpoint locked { StatusUpdater.Instance.Update(null, CashpointAction.ACTION_LOCKED); bgwInitialize.ReportProgress(2, "Disabled"); } if (refresher != null) refresher.Stop(); refresher = new AutoUpdateChecker(int.Parse(LoginForm.CashpointSettings["AutoRefreshInterval"])); refresher.UpdateRequested += refresher_UpdateRequested; if (CashpointSettings["Autorefresh"].ToLower() == "auto") refresher.Start(); else refresher.Stop(); DatabaseHandler.TransmitCurrentState(IgnoreLock ? 6 : 1); } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); bgwInitialize.ReportProgress(1, ex.Message); } #endregion #region step 8 if (DebugPrinter || DebugLock) { Invoke(new Action(() => { // create toolbox (recreation needed after cashpoint reloaded) if (debugToolbox != null && debugToolbox.Visible) debugToolbox.Close(); if (DebugToolboxVisible) this.Invoke(new Action(() => { debugToolbox = new DebugForm(printDevice, addimatLock); // form settings to prevent showDialog() to keep toolbox focusable KasseForm.DebugEnabled = true; debugToolbox.Show(); })); })); } #endregion }
internal static void Recreate(Form owner) { DebugForm oldInstance = null; Printer p = null; VirtualComPort vcp = null; if (instance != null) { oldInstance = instance; p = instance.printer; vcp = instance.lockPort; } DebugForm newForm = new DebugForm(p, vcp); if (oldInstance != null) { newForm = new DebugForm(p, vcp, oldInstance.users); newForm.btnLockPull.Enabled = oldInstance.btnLockPull.Enabled; newForm.btnLockPush.Enabled = oldInstance.btnLockPush.Enabled; newForm.grbLock.Enabled = oldInstance.grbLock.Enabled; newForm.grbPrinter.Enabled = oldInstance.grbPrinter.Enabled; newForm.txtPrinterOutput.Text = oldInstance.txtPrinterOutput.Text; newForm.lbKeyUsers.SelectedIndex = oldInstance.lbKeyUsers.SelectedIndex; newForm.Location = oldInstance.Location; } newForm.Owner = owner; newForm.Show(); DebugForm.KeyPluggedIn = DebugForm.KeyPluggedIn; }