using DevExpress.XtraEditors; using GCAS.Code; using GCAS.Dto; using GCAS.Localization; using GCAS.Model; using System; using System.Collections.Generic; using System.Configuration; using System.IO.Ports; using System.Linq; using System.Threading; using System.Windows.Forms; namespace GCAS { public partial class SystemSetForm : XtraForm { private List L; private ConfigRepository configRepository; public OperatorModel currentUser; private ActionsRepository actionsRepository; public SystemSetForm() { InitializeComponent(); L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture); } private void SystemSetForm_Load(object sender, EventArgs e) { configRepository = new ConfigRepository(); actionsRepository = new ActionsRepository(); InitFeet(); InitSerialPort(); InitLanguage(); if (currentUser.Role == 1) { panelControl1.Enabled = false; } } private void InitFeet() { var config = configRepository.Get(); text_putMin.Text = config?.PutThresholdMin.ToString(); text_putMax.Text = config?.PutThresholdMax.ToString(); } private void InitSerialPort() { string[] ports = SerialPort.GetPortNames(); cb_Serial.Properties.Items.AddRange(ports); var slave = ConfigurationManager.AppSettings["slave"]; var portName = ConfigurationManager.AppSettings["portName"]; var baudRate = ConfigurationManager.AppSettings["baudRate"]; var dataBits = ConfigurationManager.AppSettings["dataBits"]; var stopBits = ConfigurationManager.AppSettings["stopBits"]; var parity = ConfigurationManager.AppSettings["parity"]; se_slave.Value = int.Parse(slave); cb_Serial.EditValue = portName; cb_baudRate.EditValue = baudRate; cb_dataBits.EditValue = dataBits; cb_stopBits.EditValue = stopBits; cb_parity.EditValue = parity; } private void InitLanguage() { var language = Thread.CurrentThread.CurrentUICulture; switch (language.Name) { case "zh-Hans": lu_language.SelectedIndex = 0; break; case "en": lu_language.SelectedIndex = 1; break; default: lu_language.SelectedIndex = 0; break; } } private async void btn_Tare_Click(object sender, System.EventArgs e) { var entity = new ConfigDto(); if (!int.TryParse(text_putMin.Text.Trim(), out int putMin)) XtraMessageBox.Show(L.GetString("ThresholdMinError"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); if (!int.TryParse(text_putMax.Text.Trim(), out int putMax)) XtraMessageBox.Show(L.GetString("ThresholdMaxError"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); entity.PutThresholdMax = putMax; entity.PutThresholdMin = putMin; if (await configRepository.UpdateThresholdAsync(entity) > 0) { await actionsRepository.Insert(new ActionsModel { Info = L.GetString("ThresholdSave"), Operator = currentUser.Name, RoleName = currentUser.RoleName, Time = DateTime.Now }); XtraMessageBox.Show(L.GetString("saveFailed"), L.GetString("notification"), MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void btn_language_Click(object sender, EventArgs e) { var language = Thread.CurrentThread.CurrentUICulture.Name; switch (lu_language.SelectedIndex) { case 0: language = "zh-Hans"; break; case 1: language = "en"; break; } Tools.UpdateAppConfig("language", language); XtraMessageBox.Show(L.GetString("restartEffective")); } private async void btn_SerialPort_Click(object sender, EventArgs e) { var slave = se_slave.Text; var portName = cb_Serial.Text; var baudRate = cb_baudRate.Text; var dataBits = cb_dataBits.Text; var stopBits = cb_stopBits.Text; var parity = cb_parity.Text; Tools.UpdateAppConfig("slave", slave); Tools.UpdateAppConfig("portName", portName); Tools.UpdateAppConfig("baudRate", baudRate); Tools.UpdateAppConfig("dataBits", dataBits); Tools.UpdateAppConfig("stopBits", stopBits); Tools.UpdateAppConfig("parity", parity); await actionsRepository.Insert(new ActionsModel { Info = L.GetString("ThresholdSave"), Operator = currentUser.Name, RoleName = currentUser.RoleName, Time = DateTime.Now }); XtraMessageBox.Show(L.GetString("restartEffective")); } private void btn_TareSave_Click(object sender, EventArgs e) { var realForm = ParentForm.MdiChildren.FirstOrDefault(c => c.Name == "RealTimeForm") as RealTimeForm; realForm.ChangeConfigInfo(); } private void text_putMin_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } private void text_putMax_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } } }