HomeForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using Model;
  2. using PlcComponent;
  3. using PlcUiControl;
  4. using PlcUiForm;
  5. using StandardLibrary;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace YangjieTester.主界面
  16. {
  17. [FormDescriptionAttribute("主界面")]
  18. public partial class HomeForm : PlcBaseForm
  19. {
  20. public HomeForm()
  21. {
  22. InitializeComponent();
  23. BackgroundService.Instance.AlarmMonitor.OnDatasEqualTarget += AlarmMonitor_OnDatasEqualTarget;
  24. BackgroundService.Instance.AlarmMonitor.OnAllDataCleared += AlarmMonitor_OnAllDataCleared;
  25. //BackgroundService.Instance.AlarmMonitor.OnAllDataRefreshed += AlarmMonitor_OnAllDataRefreshed;
  26. BackgroundService.Instance.TipsMonitor.OnDatasEqualTarget += TipsMonitor_OnDatasEqualTarget;
  27. BackgroundService.Instance.TipsMonitor.OnAllDataCleared += TipsMonitor_OnAllDataCleared;
  28. }
  29. int count;
  30. private void AlarmMonitor_OnAllDataRefreshed(object arg1, List<PlcComponent.MonitorRule> arg2)
  31. {
  32. count++;
  33. panelAlarm.Text = count.ToString();
  34. }
  35. private void Form主界面_Load(object sender, EventArgs e)
  36. {
  37. FormEx.ShowSubForm(uiPanel1, new RunDataForm());
  38. }
  39. private void btn工站时间监控_Click(object sender, EventArgs e)
  40. {
  41. FormEx.ShowSubForm(uiPanel1, new SpeedMonitorForm());
  42. }
  43. private void btn跑马灯_Click(object sender, EventArgs e)
  44. {
  45. FormEx.ShowSubForm(uiPanel1, new FlashLightForm());
  46. }
  47. private void btn生产数据_Click(object sender, EventArgs e)
  48. {
  49. FormEx.ShowSubForm(uiPanel1, new RunDataForm());
  50. }
  51. private void btnAlarmLog_Click(object sender, EventArgs e)
  52. {
  53. FormEx.ShowSubForm(uiPanel1, new HistoryDataForm());
  54. }
  55. private void btn吸嘴故障计数监控_Click(object sender, EventArgs e)
  56. {
  57. FormEx.ShowSubForm(uiPanel1, new NozzleLifeCountForm());
  58. }
  59. private void os确认系统退出_Button1MouseDown(object sender, EventArgs e)
  60. {
  61. //FormEx.NeedCloseApp();
  62. var timer = new Timer() { Interval = 200 };
  63. timer.Tick += (s, e1) => { timer.Stop(); AppEx.NeedCloseApp(); /*Application.Exit();*/ };
  64. timer.Start();
  65. }
  66. bool haveAlarmHappened;
  67. private void AlarmMonitor_OnDatasEqualTarget(object arg1, List<PlcComponent.MonitorRule> arg2)
  68. {
  69. if (this.InvokeRequired)
  70. {
  71. this.BeginInvoke(new Action(() => AlarmMonitor_OnDatasEqualTarget(arg1, arg2)));
  72. }
  73. else
  74. {
  75. var monitor = (PlcComponent.PlcDataMonitor)arg1;
  76. haveAlarmHappened = true;
  77. panelAlarm.Style = Sunny.UI.UIStyle.Red;
  78. var msg = arg2.Select(m => m.Message).ToList();
  79. txtAlarm.Text = string.Join(Environment.NewLine, msg);
  80. }
  81. }
  82. private void AlarmMonitor_OnAllDataCleared(object obj)
  83. {
  84. if (this.InvokeRequired)
  85. {
  86. this.BeginInvoke(new Action(() => AlarmMonitor_OnAllDataCleared(obj)));
  87. }
  88. else
  89. {
  90. if (haveAlarmHappened)
  91. {
  92. haveAlarmHappened = false;
  93. panelAlarm.Style = Sunny.UI.UIStyle.Green;
  94. txtAlarm.Text = string.Empty;
  95. }
  96. }
  97. }
  98. bool haveTipsShown;
  99. private void TipsMonitor_OnDatasEqualTarget(object arg1, List<PlcComponent.MonitorRule> arg2)
  100. {
  101. if (this.InvokeRequired)
  102. {
  103. this.BeginInvoke(new Action(() => TipsMonitor_OnDatasEqualTarget(arg1, arg2)));
  104. }
  105. else
  106. {
  107. var monitor = (PlcComponent.PlcDataMonitor)arg1;
  108. haveTipsShown = true;
  109. panelTips.Style = Sunny.UI.UIStyle.LayuiOrange;
  110. var msg = arg2.Select(m => m.Message).ToList();
  111. txtTips.Text = string.Join(Environment.NewLine, msg);
  112. }
  113. }
  114. private void TipsMonitor_OnAllDataCleared(object obj)
  115. {
  116. if (this.InvokeRequired)
  117. {
  118. this.BeginInvoke(new Action(() => TipsMonitor_OnAllDataCleared(obj)));
  119. }
  120. else
  121. {
  122. if (haveTipsShown)
  123. {
  124. haveTipsShown = false;
  125. panelTips.Style = Sunny.UI.UIStyle.Inherited;
  126. txtTips.Text = string.Empty;
  127. }
  128. }
  129. }
  130. private void Form主界面_FormClosing(object sender, FormClosingEventArgs e)
  131. {
  132. BackgroundService.Instance.AlarmMonitor.OnDatasEqualTarget -= AlarmMonitor_OnDatasEqualTarget;
  133. BackgroundService.Instance.AlarmMonitor.OnAllDataCleared -= AlarmMonitor_OnAllDataCleared;
  134. //BackgroundService.Instance.AlarmMonitor.OnAllDataRefreshed -= AlarmMonitor_OnAllDataRefreshed;
  135. BackgroundService.Instance.TipsMonitor.OnDatasEqualTarget -= TipsMonitor_OnDatasEqualTarget;
  136. BackgroundService.Instance.TipsMonitor.OnAllDataCleared -= TipsMonitor_OnAllDataCleared;
  137. }
  138. private void btn工站时间监控_Click_1(object sender, EventArgs e)
  139. {
  140. new SpeedMonitorForm().ShowDialog();
  141. }
  142. private void btn测试模式选择_Click(object sender, EventArgs e)
  143. {
  144. new TestModeSettingForm().ShowDialog();
  145. }
  146. private void btn吸嘴良率统计_Click(object sender, EventArgs e)
  147. {
  148. new NozzleNGCountForm().ShowDialog();
  149. }
  150. private void btnNG停止设置_Click(object sender, EventArgs e)
  151. {
  152. new TestNGStopSettingForm().ShowDialog();
  153. }
  154. private void btn胶膜载带设置_Click(object sender, EventArgs e)
  155. {
  156. new TapeSettingForm().ShowDialog();
  157. }
  158. private void btn首件打样设置_Click(object sender, EventArgs e)
  159. {
  160. new FirstTestSettingForm().ShowDialog();
  161. }
  162. private void myUiButton1_Click(object sender, EventArgs e)
  163. {
  164. new NozzleLifeCountForm().ShowDialog();
  165. }
  166. private void btn视觉仿真设置_Click(object sender, EventArgs e)
  167. {
  168. new VisionModeSettingForm().ShowDialog();
  169. }
  170. private void btn消耗品寿命设定_Click(object sender, EventArgs e)
  171. {
  172. new ConsumablesSettingForm().ShowDialog();
  173. }
  174. private void btn测试数量查询_Click(object sender, EventArgs e)
  175. {
  176. new TestCountForm().ShowDialog();
  177. }
  178. private void btn开启批次_BeforeWrite(object sender, CancelEventArgs e)
  179. {
  180. if (AppSession.MachineState.Status != Status.Ready && AppSession.MachineState.Status != Status.Paused)
  181. {
  182. MessageBox.Show("设备不在就绪或暂停状态,不能新建批次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  183. e.Cancel = true;
  184. }
  185. else
  186. {
  187. var kb = new FullKeyboardForm() { Caption = "批次号" };
  188. var dr = kb.ShowDialog();
  189. if (dr != DialogResult.OK)
  190. {
  191. e.Cancel = true;//不让PLC进行清零动作。
  192. }
  193. else
  194. {
  195. var b = BatchService.CreateBatchLog(kb.Value, AppSession.CurrentUser, out string err);
  196. if (!b)
  197. {
  198. MessageBox.Show(err, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  199. e.Cancel = true;
  200. return;
  201. }
  202. else
  203. {
  204. AppSession.CurrentBatch.BatchID = kb.Value;
  205. AppSession.CurrentBatch.StartTime = DateTime.Now;
  206. AppSession.CurrentBatch.RecipeName = null;
  207. MessageBox.Show("新建批次成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  208. return;
  209. }
  210. }
  211. }
  212. }
  213. private void btn结束批次_BeforeWrite(object sender, CancelEventArgs e)
  214. {
  215. if (AppSession.MachineState.Status != Status.Ready && AppSession.MachineState.Status != Status.Paused)
  216. {
  217. MessageBox.Show("设备不在就绪或暂停状态,不能结束批次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  218. e.Cancel = true;
  219. }
  220. else
  221. {
  222. var b = BackgroundService.Instance.RefreshInstanceFromPLC(AppSession.CurrentBatch);//刷新当前批次的数据。
  223. //后续增加批次中的各种报警代码计数,MTTB之类的计算。
  224. if (!b)
  225. {
  226. MessageBox.Show("PLC读取失败,不能结束批次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  227. e.Cancel = true;
  228. return;
  229. }
  230. b = BatchService.FinishLatestBatch(AppSession.CurrentBatch, AppSession.CurrentUser, out string err);
  231. if (!b)
  232. {
  233. MessageBox.Show(err, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  234. e.Cancel = true;
  235. return;
  236. }
  237. else
  238. {
  239. AppSession.CurrentBatch.EndTime = DateTime.Now;
  240. MessageBox.Show("批次结束成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  241. }
  242. }
  243. }
  244. private void bnt当前批次报告_Click(object sender, EventArgs e)
  245. {
  246. var batch = BatchService.GetLatestBatch(out string error);
  247. if (batch == null)
  248. {
  249. MessageBox.Show(error, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  250. return;
  251. }
  252. else if (batch.Finished)
  253. {
  254. MessageBox.Show("当前批次已完成,请到批次记录查询", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  255. }
  256. else//此时的batch仅有开始时间,开始人员信息有意义。
  257. {
  258. new BatchReportForm(batch).ShowDialog();
  259. }
  260. }
  261. }
  262. }