d23004
2023-05-24 fa87a558389d9d425f70afa1c8de54c69875ef2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static SA_LTT.Module.Crevis;
using System.Threading;
using SA_LTT;
using padDLL;
 
namespace SA_LTT_UI.Screen
{
    public partial class ScreenIO : UserControl
    {
        MainFrame _mainFrame;
        Thread t_UIUpdate;
 
        bool updateCheck;
 
        private List<IoBar> InputIoBars = new List<IoBar>();
        private List<IoBar> OutputIoBars = new List<IoBar>();
        private List<AnalogBar> InputAnalogBars = new List<AnalogBar>();
        private List<AnalogBar> OutputAnalogBars = new List<AnalogBar>();
 
        public ScreenIO(MainFrame mainFrame)
        {
            InitializeComponent();
 
            _mainFrame = mainFrame;
 
            InitalizeData();
 
            t_UIUpdate = new Thread(UIUpdateTh);
            t_UIUpdate.Start();
        }
 
 
        private void InitalizeData()
        {
            FlowLayoutPanel fllayout = new FlowLayoutPanel();
 
            int i = 0;
            DigitalInput[] inputs = _mainFrame.equipment.crevis.DigitalInputs.Keys.ToArray();
            foreach (DigitalInput input in inputs)
            {
                IoBar ioBar = new IoBar(i, input.ToString());
                InputIoBars.Add(ioBar);
 
                if (i % 8 == 0)
                {
                    fllayout = new FlowLayoutPanel();
                    fllayout.AutoSize = true;
                    fllayout.BorderStyle = BorderStyle.FixedSingle;
                    fllayout.FlowDirection = FlowDirection.TopDown;
 
                    if ((i / 8) % 2 == 0)
                    {
                        flp_DigitalInputLeft.Controls.Add(fllayout);
                    }
                    else
                    {
                        flp_DigitalInputRight.Controls.Add(fllayout);
                    }
                }
 
                fllayout.Controls.Add(ioBar);
                i++;
            }
 
            i = 0;
            DigitalOutput[] outputs = _mainFrame.equipment.crevis.DigitalOutputs.Keys.ToArray();
            foreach (DigitalOutput output in outputs)
            {
                IoBar ioBar = new IoBar(i, output.ToString());
                ioBar.AddClickEvent(DigitalOutputClicked);
                OutputIoBars.Add(ioBar);
 
                if (i % 8 == 0)
                {
                    fllayout = new FlowLayoutPanel();
                    fllayout.AutoSize = true;
                    fllayout.BorderStyle = BorderStyle.FixedSingle;
                    fllayout.FlowDirection = FlowDirection.TopDown;
 
                    if ((i / 8) % 2 == 0)
                    {
                        flp_DigitalOutputLeft.Controls.Add(fllayout);
                    }
                    else
                    {
                        flp_DigitalOutputRight.Controls.Add(fllayout);
                    }
                }
 
                fllayout.Controls.Add(ioBar);
                i++;
            }
 
            i = 0;
            AnalogInput[] analogInputs = _mainFrame.equipment.crevis.AnalogInputs.Keys.ToArray();
            foreach (AnalogInput output in analogInputs)
            {
                AnalogBar analogBar = new AnalogBar(i, output.ToString());
                InputAnalogBars.Add(analogBar);
 
                if (i % 4 == 0)
                {
                    fllayout = new FlowLayoutPanel();
                    fllayout.AutoSize = true;
                    fllayout.BorderStyle = BorderStyle.FixedSingle;
                    fllayout.FlowDirection = FlowDirection.TopDown;
 
                    if ((i / 4) % 2 == 0)
                    {
                        flp_AnalogInputLeft.Controls.Add(fllayout);
                    }
                    else
                    {
                        flp_AnalogInputRight.Controls.Add(fllayout);
                    }
                }
 
                fllayout.Controls.Add(analogBar);
                i++;
            }
 
            i = 0;
            AnalogOutput[] analogOutputs = _mainFrame.equipment.crevis.AnalogOutputs.Keys.ToArray();
            foreach (AnalogOutput output in analogOutputs)
            {
                AnalogBar analogBar = new AnalogBar(i, output.ToString());
                analogBar.AddClickEvent(AnalogOutputClicked);
                OutputAnalogBars.Add(analogBar);
 
                if (i % 4 == 0)
                {
                    fllayout = new FlowLayoutPanel();
                    fllayout.AutoSize = true;
                    fllayout.BorderStyle = BorderStyle.FixedSingle;
                    fllayout.FlowDirection = FlowDirection.TopDown;
 
                    if ((i / 4) % 2 == 0)
                    {
                        flp_AnalogOutputLeft.Controls.Add(fllayout);
                    }
                    else
                    {
                        flp_AnalogOutputRight.Controls.Add(fllayout);
                    }
                }
 
                fllayout.Controls.Add(analogBar);
                i++;
            }
        }
 
        private void UIUpdateTh()
        {
            while (_mainFrame.equipment.IsDisposed == false)
            {
                Thread.Sleep(10);
 
                if (updateCheck == false)
                {
                    updateCheck = true;
                    UIUpdate();
                }
            }
        }
 
        private void UIUpdate()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new UIUpdateDelegate(UIUpdate));
                return;
            }
            else
            {
                try
                {
                    int i = 0;
                    foreach(DigitalInput digitalInput in Enum.GetValues(typeof(DigitalInput)))
                    {
                        InputIoBars[i].Status = _mainFrame.equipment.crevis.DigitalInputs[digitalInput];
                        i++;
                    }
 
                    i = 0;
                    foreach (DigitalOutput digitalOutput in Enum.GetValues(typeof(DigitalOutput)))
                    {
                        OutputIoBars[i].Status = _mainFrame.equipment.crevis.DigitalOutputs[digitalOutput];
                        i++;
                    }
 
 
                    i = 0;
                    foreach (AnalogInput input in Enum.GetValues(typeof(AnalogInput)))
                    {
                        InputAnalogBars[i].Value = _mainFrame.equipment.crevis.AnalogInputs[input];
                        i++;
                    }
                    
                    i = 0;
                    foreach (AnalogOutput input in Enum.GetValues(typeof(AnalogOutput)))
                    {
                        OutputAnalogBars[i].Value = _mainFrame.equipment.crevis.AnalogOutputs[input];
                        i++;
                    }
 
                    if(_mainFrame.equipment.ProcessStatus == ProcessStatus.Idle || _mainFrame.equipment.ProcessStatus == ProcessStatus.Pause)
                    {
                        if(flp_AnalogOutputLeft.Enabled == false)
                        {
                            flp_AnalogOutputLeft.Enabled = true;
                        }
 
                        if (flp_AnalogOutputRight.Enabled == false)
                        {
                            flp_AnalogOutputRight.Enabled = true;
                        }
 
                        if (flp_DigitalOutputLeft.Enabled == false)
                        {
                            flp_DigitalOutputLeft.Enabled = true;
                        }
 
                        if (flp_DigitalOutputRight.Enabled == false)
                        {
                            flp_DigitalOutputRight.Enabled = true;
                        }
                    }
                    else
                    {
                        if (flp_AnalogOutputLeft.Enabled == true)
                        {
                            flp_AnalogOutputLeft.Enabled = false;
                        }
 
                        if (flp_AnalogOutputRight.Enabled == true)
                        {
                            flp_AnalogOutputRight.Enabled = false;
                        }
 
                        if (flp_DigitalOutputLeft.Enabled == true)
                        {
                            flp_DigitalOutputLeft.Enabled = false;
                        }
 
                        if (flp_DigitalOutputRight.Enabled == true)
                        {
                            flp_DigitalOutputRight.Enabled = false;
                        }
                    }
                }
                catch (Exception e)
                {
                    EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace);
                }
                finally
                {
                    updateCheck = false;
                }
            }
        }
 
        private void DigitalOutputClicked(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            DigitalOutput digitalOutput = (DigitalOutput)control.Tag;
 
            EquipmentLogManager.Instance.WriteButtonLog($"ScreenIO : {digitalOutput.ToString()} {!_mainFrame.equipment.crevis.DigitalOutputs[digitalOutput]}");
            _mainFrame.equipment.crevis.WriteOutput(digitalOutput, !_mainFrame.equipment.crevis.DigitalOutputs[digitalOutput]);
        }
 
        private void AnalogOutputClicked(object sender, EventArgs e)
        {
            var nPad = new NumericPad();
            nPad.Setup("0", "10");  //범위 변경해야 함
            nPad.ShowDialog();
            double value = 0;
            if(double.TryParse(nPad.GetValue(), out value))
            {
                if (nPad.DialogResult != DialogResult.OK) return;
 
                Control control = (Control)sender;
                AnalogOutput analoglOutput = (AnalogOutput)((int)control.Tag * 10);
 
                EquipmentLogManager.Instance.WriteButtonLog($"ScreenIO : {analoglOutput.ToString()} {value}");
                _mainFrame.equipment.crevis.WriteAnalog(analoglOutput, value);
            }
        }
    }
}