FormSiteServiceTool.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using NozzleLockConfiguration;
  12. namespace SiteServiceTool
  13. {
  14. public partial class FormSiteServiceTool : Form
  15. {
  16. private const string TextCloseAllNozzle = "关闭所有油枪";
  17. private const string TextOpenAllNozzle = "打开所有油枪";
  18. private const string TextBoxNozzle = "油枪列表";
  19. private readonly Color NozzleOpenColor = Color.LightGreen;
  20. private readonly Color NozzleClosedColor = Color.Red;
  21. private readonly Color NozzleUnknownColor = Color.White;
  22. private bool toCloseAllNozzles = true;
  23. private IEnumerable<int> nozzleIds;
  24. private FlowLayoutPanel groupBoxFlowLayout;
  25. public FormSiteServiceTool()
  26. {
  27. InitializeComponent();
  28. FormBorderStyle = FormBorderStyle.FixedSingle;
  29. try
  30. {
  31. nozzleIds = NozzleLockAccessor.RetrieveNozzleIds();
  32. }
  33. catch(Exception)
  34. {
  35. nozzleIds = null;
  36. }
  37. if (nozzleIds != null)
  38. {
  39. toCloseAllNozzles = true; // default to close all pns
  40. buttonOpenCloseAll.Text = TextCloseAllNozzle;
  41. CreateNozzleButtons();
  42. }
  43. else
  44. {
  45. buttonOpenCloseAll.Enabled = false;
  46. labelWarning.Text = "初始化失败,请退出程序后重试或联系售后!";
  47. }
  48. }
  49. private void CreateNozzleButtons()
  50. {
  51. groupBoxFps.Text = TextBoxNozzle;
  52. groupBoxFps.Size = new Size(this.Width - 50, 0);
  53. this.Controls.Add(groupBoxFps);
  54. groupBoxFlowLayout = new FlowLayoutPanel();
  55. groupBoxFlowLayout.Dock = DockStyle.Fill;
  56. groupBoxFps.Controls.Add(groupBoxFlowLayout);
  57. int extraSpace = 25;
  58. MethodInvoker resizeGroupBox = (() =>
  59. {
  60. int numControls = groupBoxFlowLayout.Controls.Count;
  61. if (numControls > 0)
  62. {
  63. Control lastControl = groupBoxFlowLayout.Controls[numControls - 1];
  64. int bottom = lastControl.Bounds.Bottom;
  65. groupBoxFps.Size = new Size(groupBoxFps.Width, bottom + extraSpace + extraSpace);
  66. groupBoxFlowLayout.Size = new Size(groupBoxFlowLayout.Width, bottom + extraSpace);
  67. }
  68. });
  69. groupBoxFlowLayout.Resize += ((s, e) => resizeGroupBox());
  70. groupBoxFlowLayout.ControlAdded += ((s, e) => resizeGroupBox());
  71. // Populate with buttons
  72. foreach (var id in nozzleIds)
  73. {
  74. Button button = new Button();
  75. button.Margin = new Padding(10, 10, 10, 10);
  76. button.Text = id.ToString();
  77. button.Size = new Size(50, 50);
  78. button.AutoSize = true;
  79. button.Font = new Font("Microsoft Sans Serif", 12, FontStyle.Bold);
  80. button.BackColor = GetNozzleButtonColorBasedonConfigValue(id);
  81. button.Click += NozzleButton_Click;
  82. groupBoxFlowLayout.Controls.Add(button);
  83. }
  84. }
  85. private NozzleLockStatus GetNozzleStatus(int nId)
  86. {
  87. NozzleLockModel model = NozzleLockAccessor.GetNozzleLockModel(nId);
  88. if(model != null)
  89. {
  90. return model.lock_status;
  91. }
  92. else
  93. {
  94. return NozzleLockStatus.Unknown;
  95. }
  96. }
  97. private Color GetNozzleButtonColorBasedonConfigValue(int nId)
  98. {
  99. NozzleLockStatus status = GetNozzleStatus(nId);
  100. switch (status)
  101. {
  102. case NozzleLockStatus.Clsoed:
  103. return NozzleClosedColor;
  104. case NozzleLockStatus.Open:
  105. return NozzleOpenColor;
  106. case NozzleLockStatus.Unknown:
  107. default:
  108. return NozzleUnknownColor;
  109. }
  110. }
  111. private void NozzleButton_Click(object sender, EventArgs e)
  112. {
  113. Button nButton = (Button)sender;
  114. int nId = int.Parse(nButton.Text);
  115. NozzleLockStatus status = GetNozzleStatus(nId);
  116. if (status == NozzleLockStatus.Unknown)
  117. {
  118. return;
  119. }
  120. bool isNozzleOpenCurrently = (status == NozzleLockStatus.Open);
  121. string msg = "确定" + (isNozzleOpenCurrently ? "关闭" : "打开") + nId + "号油枪?";
  122. DialogResult result = MessageBox.Show(msg, "注意", MessageBoxButtons.YesNo);
  123. if (result != DialogResult.Yes)
  124. {
  125. return;
  126. }
  127. if (isNozzleOpenCurrently)
  128. {
  129. NozzleLockAccessor.UpdateOneNozzleLockStatus(nId, NozzleLockStatus.Clsoed);
  130. }
  131. else
  132. {
  133. NozzleLockAccessor.UpdateOneNozzleLockStatus(nId, NozzleLockStatus.Open);
  134. }
  135. nButton.BackColor = GetNozzleButtonColorBasedonConfigValue(nId);
  136. }
  137. private void buttonOpenCloseAll_Click(object sender, EventArgs e)
  138. {
  139. string msg = "确定" + (toCloseAllNozzles ? "关闭" : "打开") + "所有油枪?";
  140. DialogResult result = MessageBox.Show(msg, "注意", MessageBoxButtons.YesNo);
  141. if(result != DialogResult.Yes)
  142. {
  143. return;
  144. }
  145. if (toCloseAllNozzles)
  146. {
  147. NozzleLockAccessor.UpdateAllNozzlesLockStatus(NozzleLockStatus.Clsoed);
  148. }
  149. else
  150. {
  151. NozzleLockAccessor.UpdateAllNozzlesLockStatus(NozzleLockStatus.Open);
  152. }
  153. toCloseAllNozzles = !toCloseAllNozzles;
  154. UpdateTextOfButtonOpenCloseAll();
  155. UpdateAllNozzleButtonsColor();
  156. }
  157. private void UpdateAllNozzleButtonsColor()
  158. {
  159. foreach (var item in groupBoxFlowLayout.Controls)
  160. {
  161. if (item is Button)
  162. {
  163. Button nButton = (Button)item;
  164. int nId = int.Parse(nButton.Text);
  165. nButton.BackColor = GetNozzleButtonColorBasedonConfigValue(nId);
  166. }
  167. }
  168. }
  169. private void UpdateTextOfButtonOpenCloseAll()
  170. {
  171. if (toCloseAllNozzles)
  172. {
  173. buttonOpenCloseAll.Text = TextCloseAllNozzle;
  174. }
  175. else
  176. {
  177. buttonOpenCloseAll.Text = TextOpenAllNozzle;
  178. }
  179. }
  180. private void FormServiceUtility_Load(object sender, EventArgs e)
  181. {
  182. }
  183. }
  184. }