-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCToggleButton.cs
334 lines (288 loc) · 11 KB
/
CToggleButton.cs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace Ground_Floor
{
class CToggleButton
{
private Image _OffImage;
private Image _OnImage;
private PictureBox _ToggleButton;
private int _iTag;
private int _iValue;
private bool _bStatus;
public CDevices LinkedDevice;
private int _iCategory;
private int _iZone;
private CConnectionManager _cmServer;
private List<Panel> _lstPnlConfig;
private List<CToggleButton> _lstTglConfig;
// For On/OFF Switch & MSensor (Enalbe/Disable)
public CToggleButton(int width, int height, int x, int y, Image imgOn, Image imgOff, Panel pnl, int tag, int iDefaultStatus, CDevices deviceToBeLinked, int iCategory)
{
// Category
_iCategory = iCategory;
LinkedDevice = deviceToBeLinked;
// Tag & Value
_iValue = _getIntegerValue(_getPLCStatus());
_bStatus = _getBooleanValue(_iValue);
_iTag = tag;
_OffImage = imgOff;
_OnImage = imgOn;
_ToggleButton = new PictureBox();
_ToggleButton.Size = new Size(width, height);
_ToggleButton.Location = new Point(x, y);
_ToggleButton.Image = _bStatus ? imgOn : imgOff;
_ToggleButton.Tag = tag;
_ToggleButton.MouseDown += new MouseEventHandler(_ToggleButton_MouseDown);
_ToggleButton.MouseUp += new MouseEventHandler(_ToggleButton_MouseUp);
// Add the componenets to the Panel
pnl.Controls.Add(_ToggleButton);
}
// For MSensor (Configure)
public CToggleButton(int width, int height, int x, int y, Image imgOn, Image imgOff, Panel pnl, int tag, int iDefaultStatus, List<Panel> lstPnlConfig, List<CToggleButton> lstTglConfig)
{
// Tag & Value
_iValue = iDefaultStatus;
_bStatus = _getBooleanValue(_iValue);
_iTag = tag;
// List
_lstPnlConfig = lstPnlConfig;
_lstTglConfig = lstTglConfig;
_OffImage = imgOff;
_OnImage = imgOn;
_ToggleButton = new PictureBox();
_ToggleButton.Size = new Size(width, height);
_ToggleButton.Location = new Point(x, y);
_ToggleButton.Image = _bStatus ? imgOn : imgOff;
_ToggleButton.Tag = tag;
_ToggleButton.MouseDown += new MouseEventHandler(_ConfigButton_MouseDown);
_ToggleButton.MouseUp += new MouseEventHandler(_ConfigButton_MouseUp);
// Add the componenets to the Panel
pnl.Controls.Add(_ToggleButton);
}
// For Modes
public CToggleButton(int width, int height, int x, int y, Image imgOn, Image imgOff, Panel pnl, int tag, int iZone, CConnectionManager cmServer)
{
//Server
_cmServer = cmServer;
// Category
_iZone = iZone;
// Tag & Value
_iTag = tag;
switch (_iTag)
{
case CGlobal.MODE_AWAY:
_iValue = _getIntegerValue(GetAwayMode());
break;
default:
_iValue = _getIntegerValue(false);
break;
}
_bStatus = _getBooleanValue(_iValue);
_OffImage = imgOff;
_OnImage = imgOn;
_ToggleButton = new PictureBox();
_ToggleButton.Size = new Size(width, height);
_ToggleButton.Location = new Point(x, y);
_ToggleButton.Image = _bStatus ? imgOn : imgOff;
_ToggleButton.Tag = tag;
_ToggleButton.MouseDown += new MouseEventHandler(_ToggleButton_MouseDown);
_ToggleButton.MouseUp += new MouseEventHandler(_ModeButton_MouseUp);
// Add the componenets to the Panel
pnl.Controls.Add(_ToggleButton);
}
// For Global
public CToggleButton(int width, int height, int x, int y, Image imgOn, Image imgOff, Panel pnl, int iCategory)
{
// Category
_iCategory = iCategory;
// Tag & Value
_iValue = _getIntegerValue(true);
_bStatus = _getBooleanValue(_iValue);
//_iTag = tag;
_OffImage = imgOff;
_OnImage = imgOn;
_ToggleButton = new PictureBox();
_ToggleButton.Size = new Size(width, height);
_ToggleButton.Location = new Point(x, y);
_ToggleButton.Image = _bStatus ? imgOn : imgOff;
//_ToggleButton.Tag = tag;
_ToggleButton.MouseDown += new MouseEventHandler(_ToggleButton_MouseDown);
_ToggleButton.MouseUp += new MouseEventHandler(_GlobalButton_MouseUp);
// Add the componenets to the Panel
pnl.Controls.Add(_ToggleButton);
}
private void _ToggleButton_MouseDown(object sender, MouseEventArgs e)
{
_setStatus(!_bStatus);
}
private void _ConfigButton_MouseDown(object sender, MouseEventArgs e)
{
setConfigStatus(!_bStatus);
}
private void _ToggleButton_MouseUp(object sender, MouseEventArgs e)
{
switch (_iCategory)
{
case CGlobal.CATEGORY_LIGHTING:
_iValue = _getIntegerValue(_bStatus);
if (_bStatus)
{
LinkedDevice.TCInterface.SetBool(LinkedDevice.lstPrm[1], true);
}
else
{
LinkedDevice.TCInterface.SetBool(LinkedDevice.lstPrm[2], true);
}
break;
case CGlobal.CATEGORY_MS:
// Twincat manipulating ...
//_iValue = _getIntegerValue(_bStatus);
// Save the bValue for all the OutputDevices which have been controlled via InputDevice (PIR)
for (int i = 2; i < LinkedDevice.lstPrm.Count; i++)
{
LinkedDevice.TCInterface.SetBool(LinkedDevice.lstPrm[i] + ".bEnablePIR", _bStatus);
}
break;
}
}
private bool GetAwayMode()
{
bool bRet = false;
CMessage msgToSend = new CMessage();
msgToSend.SetMessage(CMessageConstants.CMD_COMM_GET_AWAY_MODE, CMessageConstants.CMD_TYPE_COMMUNICATION, 0);
_cmServer.SendMessage(msgToSend);
CMessage MsgReply = _cmServer.ReceiveMessage();
if ((MsgReply != null) && (MsgReply.Command_Type() == CMessageConstants.CMD_TYPE_COMMUNICATION) && (MsgReply.Command_Id() == CMessageConstants.CMD_COMM_GET_AWAY_MODE))
{
if (MsgReply.GetPrmInt(0) == 1) bRet = true;
}
else
{
Console.WriteLine("Invalide response received.");
}
return bRet;
}
private void _ModeButton_MouseUp(object sender, MouseEventArgs e)
{
_iValue = _getIntegerValue(_bStatus);
switch (_iTag)
{
case CGlobal.MODE_AWAY:
CMessage msgToSend = new CMessage();
if (_bStatus)
{
// Active Mode
msgToSend.SetMessage(CMessageConstants.CMD_COMM_SET_AWAY_MODE, CMessageConstants.CMD_TYPE_COMMUNICATION, 0);
msgToSend.AddIntPrm(1);//If away mode is enabled
_cmServer.SendMessage(msgToSend);
}
else
{
// Deactive Mode
// Active Mode
msgToSend.SetMessage(CMessageConstants.CMD_COMM_SET_AWAY_MODE, CMessageConstants.CMD_TYPE_COMMUNICATION, 0);
msgToSend.AddIntPrm(0);//If away mode is disabled
_cmServer.SendMessage(msgToSend);
}
break;
}
}
private void _GlobalButton_MouseUp(object sender, MouseEventArgs e)
{
switch (_iCategory)
{
case CGlobal.CATEGORY_LIGHTING:
_iValue = _getIntegerValue(_bStatus);
if (_bStatus)
{
// Global ON
}
else
{
// Global OFF
}
break;
case CGlobal.CATEGORY_MS:
_iValue = _getIntegerValue(_bStatus);
if (_bStatus)
{
// Global Enable
}
else
{
// Global Disable
}
break;
}
}
private void _ConfigButton_MouseUp(object sender, MouseEventArgs e)
{
// ...
}
private bool _getPLCStatus()
{
bool bRet = false;
if (_iCategory == CGlobal.CATEGORY_LIGHTING)
{
if (LinkedDevice.TCInterface.GetShort(LinkedDevice.lstPrm[3] + ".iValue") > 0)
bRet = true;
}
else
{
// Read the OutputDevice.bEnablePIR from the first Output Device in case there are more than one Output Device (because initialy they are all same),
bRet = LinkedDevice.TCInterface.GetBool(LinkedDevice.lstPrm[2] + ".bEnablePIR");
}
return bRet;
}
private int _getIntegerValue(bool bValue)
{
int iRet = bValue ? 1 : 0;
return iRet;
}
private bool _getBooleanValue(int iValue)
{
bool bRet = iValue == 1 ? true : false;
return bRet;
}
private void _setStatus(bool bStatus)
{
_bStatus = bStatus;
_ToggleButton.Image = _bStatus ? _OnImage : _OffImage;
}
private int getTag()
{
return _iTag;
}
public void setConfigStatus(bool bStatus)
{
// Symbol
foreach (CToggleButton locToggle in _lstTglConfig)
{
if (locToggle.getTag() == _iTag)
{
locToggle._setStatus(!_bStatus);
}
else
{
locToggle._setStatus(false);
}
}
// Panel
foreach (Panel locPanel in _lstPnlConfig)
{
if (Convert.ToInt32(locPanel.Tag) == _iTag)
{
locPanel.Visible = _bStatus;
}
else
{
locPanel.Visible = false;
}
}
}
}
}