NewDeviceForm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using DevExpress.XtraEditors;
  9. using GCAS.Code;
  10. using GCAS.Dto;
  11. using GCAS.Localization;
  12. using GCAS.Model;
  13. namespace GCAS
  14. {
  15. public partial class NewDeviceForm : XtraForm
  16. {
  17. private List<NameValue> L;
  18. public DeviceModel deviceModel;
  19. private DeviceRepository deviceRepository;
  20. public OperatorModel currentUser;
  21. private ActionsRepository actionsRepository;
  22. public NewDeviceForm()
  23. {
  24. InitializeComponent();
  25. L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture);
  26. }
  27. private void NewDeviceForm_Load(object sender, EventArgs e)
  28. {
  29. actionsRepository = new ActionsRepository();
  30. deviceRepository = new DeviceRepository();
  31. InitView();
  32. }
  33. private void InitView()
  34. {
  35. if (deviceModel != null)
  36. {
  37. text_name.Text = deviceModel.Name;
  38. text_ip.Text = deviceModel.Ip;
  39. text_port.Text = deviceModel?.Port + "";
  40. }
  41. }
  42. private async void btn_save_Click(object sender, EventArgs e)
  43. {
  44. if (string.IsNullOrEmpty(text_name.Text))
  45. {
  46. XtraMessageBox.Show(L.GetString("equipmentNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  47. }
  48. if (string.IsNullOrEmpty(text_ip.Text) || !IPAddress.TryParse(text_ip.Text, out IPAddress ipAddress))
  49. {
  50. XtraMessageBox.Show(L.GetString("ipError"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  51. }
  52. int.TryParse(text_port.Text.Trim(), out int port);
  53. if (string.IsNullOrEmpty(text_port.Text) || port > 65535 || port <= 0)
  54. {
  55. XtraMessageBox.Show(L.GetString("portError"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  56. }
  57. if (deviceModel == null)
  58. {
  59. deviceModel = new DeviceModel();
  60. }
  61. deviceModel.Ip = text_ip.Text;
  62. deviceModel.Port = port;
  63. deviceModel.Name = text_name.Text;
  64. deviceModel.Code = deviceModel.Ip.Split('.').LastOrDefault();
  65. int result = 0;
  66. if (deviceModel.Id == 0)
  67. {
  68. result = await deviceRepository.Insert(deviceModel);
  69. if (result > 0)
  70. {
  71. await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("addEquipmentLog"), deviceModel.Name), Operator = currentUser.Name, RoleName = currentUser.RoleName, Time = DateTime.Now });
  72. }
  73. }
  74. else
  75. {
  76. result = await deviceRepository.Update(deviceModel);
  77. if (result > 0)
  78. {
  79. await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("editEquipmentLog"), deviceModel.Name), Operator = currentUser.Name, RoleName = currentUser.RoleName, Time = DateTime.Now });
  80. }
  81. }
  82. if (result == 0)
  83. {
  84. XtraMessageBox.Show(L.GetString("saveFailed"), L.GetString("exclamation"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  85. }
  86. else
  87. {
  88. DialogResult = DialogResult.OK;
  89. Close();
  90. }
  91. }
  92. private void text_name_Click(object sender, EventArgs e)
  93. {
  94. SoftKeyboard.ActiveSoftKeyboard(sender);
  95. }
  96. private void text_ip_Click(object sender, EventArgs e)
  97. {
  98. SoftKeyboard.ActiveSoftKeyboard(sender);
  99. }
  100. private void text_port_Click(object sender, EventArgs e)
  101. {
  102. SoftKeyboard.ActiveSoftKeyboard(sender);
  103. }
  104. }
  105. }