123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using NozzleLockConfiguration;
- namespace SiteServiceTool
- {
- public partial class FormSiteServiceTool : Form
- {
- private const string TextCloseAllNozzle = "关闭所有油枪";
- private const string TextOpenAllNozzle = "打开所有油枪";
- private const string TextBoxNozzle = "油枪列表";
- private readonly Color NozzleOpenColor = Color.LightGreen;
- private readonly Color NozzleClosedColor = Color.Red;
- private readonly Color NozzleUnknownColor = Color.White;
- private bool toCloseAllNozzles = true;
- private IEnumerable<int> nozzleIds;
- private FlowLayoutPanel groupBoxFlowLayout;
- public FormSiteServiceTool()
- {
- InitializeComponent();
- FormBorderStyle = FormBorderStyle.FixedSingle;
- try
- {
- nozzleIds = NozzleLockAccessor.RetrieveNozzleIds();
- }
- catch(Exception)
- {
- nozzleIds = null;
- }
- if (nozzleIds != null)
- {
- toCloseAllNozzles = true; // default to close all pns
- buttonOpenCloseAll.Text = TextCloseAllNozzle;
- CreateNozzleButtons();
- }
- else
- {
- buttonOpenCloseAll.Enabled = false;
- labelWarning.Text = "初始化失败,请退出程序后重试或联系售后!";
- }
- }
- private void CreateNozzleButtons()
- {
- groupBoxFps.Text = TextBoxNozzle;
- groupBoxFps.Size = new Size(this.Width - 50, 0);
- this.Controls.Add(groupBoxFps);
- groupBoxFlowLayout = new FlowLayoutPanel();
- groupBoxFlowLayout.Dock = DockStyle.Fill;
- groupBoxFps.Controls.Add(groupBoxFlowLayout);
- int extraSpace = 25;
- MethodInvoker resizeGroupBox = (() =>
- {
- int numControls = groupBoxFlowLayout.Controls.Count;
- if (numControls > 0)
- {
- Control lastControl = groupBoxFlowLayout.Controls[numControls - 1];
- int bottom = lastControl.Bounds.Bottom;
- groupBoxFps.Size = new Size(groupBoxFps.Width, bottom + extraSpace + extraSpace);
- groupBoxFlowLayout.Size = new Size(groupBoxFlowLayout.Width, bottom + extraSpace);
- }
- });
- groupBoxFlowLayout.Resize += ((s, e) => resizeGroupBox());
- groupBoxFlowLayout.ControlAdded += ((s, e) => resizeGroupBox());
- // Populate with buttons
- foreach (var id in nozzleIds)
- {
- Button button = new Button();
- button.Margin = new Padding(10, 10, 10, 10);
- button.Text = id.ToString();
- button.Size = new Size(50, 50);
- button.AutoSize = true;
- button.Font = new Font("Microsoft Sans Serif", 12, FontStyle.Bold);
- button.BackColor = GetNozzleButtonColorBasedonConfigValue(id);
- button.Click += NozzleButton_Click;
- groupBoxFlowLayout.Controls.Add(button);
- }
- }
- private NozzleLockStatus GetNozzleStatus(int nId)
- {
- NozzleLockModel model = NozzleLockAccessor.GetNozzleLockModel(nId);
- if(model != null)
- {
- return model.lock_status;
- }
- else
- {
- return NozzleLockStatus.Unknown;
- }
- }
- private Color GetNozzleButtonColorBasedonConfigValue(int nId)
- {
- NozzleLockStatus status = GetNozzleStatus(nId);
- switch (status)
- {
- case NozzleLockStatus.Clsoed:
- return NozzleClosedColor;
- case NozzleLockStatus.Open:
- return NozzleOpenColor;
- case NozzleLockStatus.Unknown:
- default:
- return NozzleUnknownColor;
- }
- }
- private void NozzleButton_Click(object sender, EventArgs e)
- {
- Button nButton = (Button)sender;
- int nId = int.Parse(nButton.Text);
- NozzleLockStatus status = GetNozzleStatus(nId);
- if (status == NozzleLockStatus.Unknown)
- {
- return;
- }
- bool isNozzleOpenCurrently = (status == NozzleLockStatus.Open);
- string msg = "确定" + (isNozzleOpenCurrently ? "关闭" : "打开") + nId + "号油枪?";
- DialogResult result = MessageBox.Show(msg, "注意", MessageBoxButtons.YesNo);
- if (result != DialogResult.Yes)
- {
- return;
- }
-
- if (isNozzleOpenCurrently)
- {
- NozzleLockAccessor.UpdateOneNozzleLockStatus(nId, NozzleLockStatus.Clsoed);
- }
- else
- {
- NozzleLockAccessor.UpdateOneNozzleLockStatus(nId, NozzleLockStatus.Open);
- }
- nButton.BackColor = GetNozzleButtonColorBasedonConfigValue(nId);
- }
- private void buttonOpenCloseAll_Click(object sender, EventArgs e)
- {
- string msg = "确定" + (toCloseAllNozzles ? "关闭" : "打开") + "所有油枪?";
- DialogResult result = MessageBox.Show(msg, "注意", MessageBoxButtons.YesNo);
- if(result != DialogResult.Yes)
- {
- return;
- }
-
- if (toCloseAllNozzles)
- {
- NozzleLockAccessor.UpdateAllNozzlesLockStatus(NozzleLockStatus.Clsoed);
- }
- else
- {
- NozzleLockAccessor.UpdateAllNozzlesLockStatus(NozzleLockStatus.Open);
- }
- toCloseAllNozzles = !toCloseAllNozzles;
- UpdateTextOfButtonOpenCloseAll();
- UpdateAllNozzleButtonsColor();
- }
- private void UpdateAllNozzleButtonsColor()
- {
- foreach (var item in groupBoxFlowLayout.Controls)
- {
- if (item is Button)
- {
- Button nButton = (Button)item;
- int nId = int.Parse(nButton.Text);
- nButton.BackColor = GetNozzleButtonColorBasedonConfigValue(nId);
- }
- }
-
- }
- private void UpdateTextOfButtonOpenCloseAll()
- {
- if (toCloseAllNozzles)
- {
- buttonOpenCloseAll.Text = TextCloseAllNozzle;
- }
- else
- {
- buttonOpenCloseAll.Text = TextOpenAllNozzle;
- }
- }
- private void FormServiceUtility_Load(object sender, EventArgs e)
- {
- }
- }
- }
|