HomeForm.cs 11 KB

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