private bool IsPanelDataValid(Panel panel, bool isErrorClear) { bool isValid = true; if (isErrorClear) { errorProvider.Clear(); } foreach (Control control in panel.Controls) { string rules = (string)control.Tag; if (!String.IsNullOrWhiteSpace(rules)) { ValidationResult response = validator.Validate(control.Text, rules); if (response.success == false) { isValid = false; errorProvider.SetIconPadding(control, -20); errorProvider.SetError(control, ECRDictionary.GetTranslation(response.message)); } } } return(isValid); }
private void btnLoadFile_Click(object sender, EventArgs e) { Stream inputStream = null; OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.InitialDirectory = "c:\\"; fileDialog.Filter = "Hex files (*.hex)|*.hex"; fileDialog.RestoreDirectory = true; DialogResult userReaction = fileDialog.ShowDialog(); if (userReaction == DialogResult.OK) { try { if ((inputStream = fileDialog.OpenFile()) != null) { labelFileName.Text = fileDialog.SafeFileName; using (inputStream) { this.intelHex.Load(inputStream); this.intelHex.ParseHeaders(); this.RefreshHexInfo(); } } } catch (Exception) { ShowErrorMessage(ECRDictionary.GetTranslation("Cannot open the file")); } } if (userReaction == DialogResult.Cancel || userReaction == DialogResult.Abort) { Clear(); } }
private async void btnUploadHex_Click(object sender, EventArgs e) { LoadingButton(btnUploadHex); try { byte[] buffer = this.intelHex.ToBinary(); FirmwareInfo firmwareInfo = new FirmwareInfo(); firmwareInfo.Set("Version", intelHex.Headers.Version); firmwareInfo.Set("Description", intelHex.Headers.Description); firmwareInfo.Set("FirmwareGUID", intelHex.Headers.FirmwareGUID); Task <dynamic> task = Task.Factory.StartNew(() => device.SendFirmwareInfo(firmwareInfo)); dynamic responseFirmwareInfo = await task; ResponseFirmware response = device.ParseFirmwareInfoResponse(responseFirmwareInfo); if (response.IsSuccess) { panelUploadProgress.Visible = true; Task <dynamic> taskSend = Task.Factory.StartNew(() => device.SendHexFile(buffer)); dynamic responseFirmwareUpload = await taskSend; ResetButton(btnUploadHex); panelUploadProgress.Visible = false; response = device.ParseFirmwareUploadResponse(responseFirmwareUpload); if (response.IsSuccess) { ShowInfoMessage(ECRDictionary.GetTranslation("Hex file was uploaded successfully")); } else { if (response.Error.Length > 0) { ShowErrorMessage(response.Error); } } } else { ResetButton(btnUploadHex); if (response.Error.Length > 0) { ShowErrorMessage(response.Error); } } } catch (IntelHexParserException) { panelUploadProgress.Visible = false; ResetButton(btnUploadHex); ShowErrorMessage(ECRDictionary.GetTranslation("Invalid HEX format")); } }
private async void btnConnect_Click(object sender, EventArgs e) { if (IsPanelDataValid(panelLogin, true)) { ChangePanelsState(false); LoadingButton(btnConnect); var panelData = this.FetchPanelData(panelLogin); try { device.Connect(panelData["textboxIP"], panelData["textboxUsername"], panelData["textboxPassword"]); Task <dynamic> task = Task.Factory.StartNew(() => device.GetState()); dynamic state = await task; task.Dispose(); if (state != null) { Task taskLoadData = Task.Factory.StartNew(() => device.LoadDeviceData()); await taskLoadData; taskLoadData.Dispose(); Task taskLoadDescriptions = Task.Factory.StartNew(() => device.LoadTranslations()); await taskLoadDescriptions; Task <dynamic> taskLoadInformation = Task.Factory.StartNew(() => device.GetInformation()); dynamic information = await taskLoadInformation; FillInformation(information); panelInformation.Visible = true; ResetButton(btnConnect); ShowInfoMessage(ECRDictionary.GetTranslation("Connected successfully")); ChangePanelsState(true); } else { ResetButton(btnConnect); } } catch (Exception ex) { ResetButton(btnConnect); ShowErrorMessage(ECRDictionary.GetTranslation(ex.Message)); } } }
private async void btnFlash_Click(object sender, EventArgs e) { LoadingButton(btnFlash); Task <dynamic> task = Task.Factory.StartNew(() => device.Flash()); dynamic responseFirmwareInfo = await task; ResponseFirmware response = device.ParseFirmwareResponseBurn(responseFirmwareInfo); ResetButton(btnFlash); if (response.IsSuccess) { ShowInfoMessage(ECRDictionary.GetTranslation("Flash is starting...")); Clear(); ChangePanelsState(false); } else { ResetButton(btnFlash); if (response.Error.Length > 0) { ShowErrorMessage(response.Error); } } }
private void ShowInfoMessage(string message) { MessageBox.Show(message, ECRDictionary.GetTranslation("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void ShowErrorMessage(string message) { MessageBox.Show(message, ECRDictionary.GetTranslation("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error); }
private void OnHttpError(object sender, HttpErrorEventArgs e) { ShowErrorMessage(ECRDictionary.GetTranslation(e.Message)); }
private void LoadingButton(Button button) { buttonStates.Add(button.Name, button.Text); button.Text = ECRDictionary.GetTranslation("In work..."); button.Enabled = false; }