private void LogoutFromAcumatica() { _progress.Report(new MonitorMessage(Strings.LogoutNotify)); if (_screen != null) { try { _screen.Logout(); } catch { //Ignore all errors in logout. } _screen = null; } }
private bool LoginToAcumatica() { _progress.Report(new MonitorMessage(String.Format(Strings.LoginNotify, Properties.Settings.Default.AcumaticaUrl))); _screen = new ScreenApi.Screen(); _screen.Url = Properties.Settings.Default.AcumaticaUrl + "/Soap/.asmx"; _screen.CookieContainer = new System.Net.CookieContainer(); try { _screen.Login(Properties.Settings.Default.Login, Settings.ToInsecureString(Settings.DecryptString(Properties.Settings.Default.Password))); return(true); } catch { _screen = null; throw; } }
public Task Initialize(Progress <MonitorMessage> progress, CancellationToken cancellationToken) { _progress = progress; _queues = JsonConvert.DeserializeObject <IEnumerable <PrintQueue> >(Properties.Settings.Default.Queues).ToDictionary <PrintQueue, string>(q => q.QueueName); if (_queues.Count == 0) { _progress.Report(new MonitorMessage(Strings.PrintQueuesConfigurationMissingWarning)); return(null); } return(Task.Run(() => { while (true) { if (cancellationToken.IsCancellationRequested) { LogoutFromAcumatica(); break; } try { if (_screen != null || LoginToAcumatica()) { PollPrintJobs(); } System.Threading.Thread.Sleep(Properties.Settings.Default.PrinterPollingInterval); } catch (Exception ex) { // Assume the server went offline or our session got lost - new login will be attempted in next iteration _progress.Report(new MonitorMessage(String.Format(Strings.PollingQueueUnknownError, ex.Message), MonitorMessage.MonitorStates.Error)); _screen = null; System.Threading.Thread.Sleep(Properties.Settings.Default.ErrorWaitInterval); } } })); }
public Task Initialize(Progress <MonitorMessage> progress, CancellationToken cancellationToken) { _progress = progress; if (String.IsNullOrEmpty(Properties.Settings.Default.ScaleID)) { _progress.Report(new MonitorMessage(Strings.ScaleConfigurationMissingWarning)); return(null); } return(Task.Run(() => { while (true) { if (cancellationToken.IsCancellationRequested) { LogoutFromAcumatica(); break; } decimal currentWeight = 0; HidDevice scale = HidDevices.Enumerate(Properties.Settings.Default.ScaleDeviceVendorId, Properties.Settings.Default.ScaleDeviceProductId).FirstOrDefault(); if (scale != null) { using (scale) { scale.OpenDevice(); int waitTries = 0; while (!scale.IsConnected) { // Sometimes the scale doesn't open immediately, retry a few times. Thread.Sleep(50); waitTries++; if (waitTries > 10) { throw new ApplicationException(Strings.ScaleConnectionError); } } var inData = scale.Read(250); // This is unconfirmed - try to find documentation for the DYMO scales? // Byte 0 == Report ID? // Byte 1 == Scale Status (1 == Fault, 2 == Stable @ 0, 3 == In Motion, 4 == Stable, 5 == Under 0, 6 == Over Weight, 7 == Requires Calibration, 8 == Requires Re-Zeroing) // Byte 2 == Weight Unit? (2=GR, 11=OZ) // Byte 3 == Data Scaling (decimal placement) - signed byte is power of 10? // Byte 4 == Weight LSB // Byte 5 == Weight MSB currentWeight = (Convert.ToDecimal(inData.Data[4]) + Convert.ToDecimal(inData.Data[5]) * 256) * Properties.Settings.Default.ScaleWeightMultiplier; _progress.Report(new MonitorMessage(String.Format(Strings.ScaleWeightNotify, currentWeight))); scale.CloseDevice(); } try { if (_lastWeightSentToAcumatica != currentWeight) { if (_screen != null || LoginToAcumatica()) { UpdateWeight(Properties.Settings.Default.ScaleID, currentWeight); _lastWeightSentToAcumatica = currentWeight; } } } catch (Exception ex) { // Assume the server went offline or our session got lost - new login will be attempted in next iteration _progress.Report(new MonitorMessage(String.Format(Strings.ScaleWeightError, ex.Message), MonitorMessage.MonitorStates.Error)); _screen = null; System.Threading.Thread.Sleep(Properties.Settings.Default.ErrorWaitInterval); } } System.Threading.Thread.Sleep(Properties.Settings.Default.ScaleReadInterval); } })); }
private void okButton_Click(object sender, EventArgs e) { Uri validatedUri; if (!Uri.TryCreate(acumaticaUrlTextBox.Text, UriKind.Absolute, out validatedUri)) { MessageBox.Show(Strings.UrlMissingPrompt, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); mainTab.SelectedIndex = 0; acumaticaUrlTextBox.Focus(); return; } if (String.IsNullOrEmpty(loginTextBox.Text)) { MessageBox.Show(Strings.LoginMissingPrompt, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); mainTab.SelectedIndex = 0; loginTextBox.Focus(); return; } if (String.IsNullOrEmpty(passwordTextBox.Text)) { MessageBox.Show(Strings.PasswordMissingPrompt, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); mainTab.SelectedIndex = 0; passwordTextBox.Focus(); return; } if (_queues.Count == 0 && String.IsNullOrEmpty(acumaticaScaleIDTextBox.Text)) { MessageBox.Show(Strings.PrintQueueOrScaleConfigurationMissingPrompt, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); mainTab.SelectedIndex = 1; return; } if (!String.IsNullOrEmpty(acumaticaScaleIDTextBox.Text) && (scalesDropDown.SelectedItem == null || (scalesDropDown.SelectedItem as ScaleDevice).VendorId == 0)) { MessageBox.Show(String.Format(Strings.DeviceMissingPrompt, acumaticaScaleIDTextBox.Text), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); mainTab.SelectedIndex = 2; scalesDropDown.Focus(); return; } PrintQueue unnamedQueue = _queues.FirstOrDefault(q => q.QueueName == NewQueueName); if (unnamedQueue != null) { MessageBox.Show(Strings.QueueNameMissingPrompt, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); mainTab.SelectedIndex = 1; queueList.SelectedItem = unnamedQueue; queueName.Focus(); return; } var screen = new ScreenApi.Screen(); screen.Url = acumaticaUrlTextBox.Text + "/Soap/.asmx"; try { screen.Login(loginTextBox.Text, passwordTextBox.Text); try { screen.Logout(); } catch { } //Ignore all errors in logout. } catch (Exception ex) { MessageBox.Show(String.Format(Strings.ScreenWebServiceConnexionError, ex.Message), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); mainTab.SelectedIndex = 0; acumaticaUrlTextBox.Focus(); return; } Properties.Settings.Default.AcumaticaUrl = acumaticaUrlTextBox.Text; Properties.Settings.Default.Login = loginTextBox.Text; Properties.Settings.Default.Password = Settings.EncryptString(Settings.ToSecureString(passwordTextBox.Text)); Properties.Settings.Default.Queues = JsonConvert.SerializeObject(_queues); Properties.Settings.Default.ScaleID = acumaticaScaleIDTextBox.Text; if (scalesDropDown.SelectedItem == null) { Properties.Settings.Default.ScaleDeviceVendorId = 0; Properties.Settings.Default.ScaleDeviceProductId = 0; } else { var s = (ScaleDevice)scalesDropDown.SelectedItem; Properties.Settings.Default.ScaleDeviceVendorId = s.VendorId; Properties.Settings.Default.ScaleDeviceProductId = s.ProductId; } Properties.Settings.Default.Save(); this.DialogResult = DialogResult.OK; }
public Task Initialize(Progress <MonitorMessage> progress, CancellationToken cancellationToken) { _progress = progress; if (String.IsNullOrEmpty(Properties.Settings.Default.ScaleID)) { _progress.Report(new MonitorMessage(Strings.ScaleConfigurationMissingWarning)); return(null); } return(Task.Run(() => { while (true) { try { if (cancellationToken.IsCancellationRequested) { LogoutFromAcumatica(); break; } decimal readWeight = 0m; WeightUnits weightUnit = WeightUnits.None; HidDevice hidDevice = HidDevices.Enumerate(Properties.Settings.Default.ScaleDeviceVendorId, Properties.Settings.Default.ScaleDeviceProductId).FirstOrDefault(); if (hidDevice != null) { using (hidDevice) { hidDevice.OpenDevice(); WaitForConnection(hidDevice); readWeight = ReadWeight(hidDevice, out weightUnit); hidDevice.CloseDevice(); } if (!weightUnit.Equals(WeightUnits.None)) { _progress.Report(new MonitorMessage(String.Format(Strings.ScaleWeightNotify, readWeight, WeightUnitToStringAbbreviation[weightUnit]))); if (_lastWeightSentToAcumatica != readWeight) { if (_screen != null || LoginToAcumatica()) { UpdateWeight(Properties.Settings.Default.ScaleID, ConvertWeightToKilogram(readWeight, weightUnit)); _lastWeightSentToAcumatica = readWeight; } } } } System.Threading.Thread.Sleep(Properties.Settings.Default.ScaleReadInterval); } catch (Exception ex) { // Assume the server went offline or our session got lost - new login will be attempted in next iteration _progress.Report(new MonitorMessage(String.Format(Strings.ScaleWeightError, ex.Message), MonitorMessage.MonitorStates.Error)); _screen = null; System.Threading.Thread.Sleep(Properties.Settings.Default.ErrorWaitInterval); } } })); }