using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; using System.Threading; using System.Windows.Forms; using DevExpress.XtraEditors; using GCAS.Code; using GCAS.Dto; using GCAS.Localization; using GCAS.Model; namespace GCAS { public partial class NewDeviceForm : XtraForm { private List L; public DeviceModel deviceModel; private DeviceRepository deviceRepository; public OperatorModel currentUser; private ActionsRepository actionsRepository; public NewDeviceForm() { InitializeComponent(); L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture); } private void NewDeviceForm_Load(object sender, EventArgs e) { actionsRepository = new ActionsRepository(); deviceRepository = new DeviceRepository(); InitView(); } private void InitView() { if (deviceModel != null) { text_name.Text = deviceModel.Name; text_ip.Text = deviceModel.Ip; text_port.Text = deviceModel?.Port + ""; } } private async void btn_save_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(text_name.Text)) { XtraMessageBox.Show(L.GetString("equipmentNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); } if (string.IsNullOrEmpty(text_ip.Text) || !IPAddress.TryParse(text_ip.Text, out IPAddress ipAddress)) { XtraMessageBox.Show(L.GetString("ipError"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); } int.TryParse(text_port.Text.Trim(), out int port); if (string.IsNullOrEmpty(text_port.Text) || port > 65535 || port <= 0) { XtraMessageBox.Show(L.GetString("portError"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); } if (deviceModel == null) { deviceModel = new DeviceModel(); } deviceModel.Ip = text_ip.Text; deviceModel.Port = port; deviceModel.Name = text_name.Text; deviceModel.Code = deviceModel.Ip.Split('.').LastOrDefault(); int result = 0; if (deviceModel.Id == 0) { result = await deviceRepository.Insert(deviceModel); if (result > 0) { await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("addEquipmentLog"), deviceModel.Name), Operator = currentUser.Name, RoleName = currentUser.RoleName, Time = DateTime.Now }); } } else { result = await deviceRepository.Update(deviceModel); if (result > 0) { await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("editEquipmentLog"), deviceModel.Name), Operator = currentUser.Name, RoleName = currentUser.RoleName, Time = DateTime.Now }); } } if (result == 0) { XtraMessageBox.Show(L.GetString("saveFailed"), L.GetString("exclamation"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { DialogResult = DialogResult.OK; Close(); } } private void text_name_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } private void text_ip_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } private void text_port_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } } }