-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
365 lines (333 loc) · 8.05 KB
/
Copy pathclient.go
File metadata and controls
365 lines (333 loc) · 8.05 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package main
import (
"fmt"
"sort"
"sync"
"github.com/astaxie/beego/logs"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type ClientItem struct {
Index int
Client ClientConfig
checked bool
}
type ClientTable struct {
sync.RWMutex
walk.TableModelBase
walk.SorterBase
sortColumn int
sortOrder walk.SortOrder
items []*ClientItem
}
func (n *ClientTable) RowCount() int {
return len(n.items)
}
func (n *ClientTable) Value(row, col int) interface{} {
item := n.items[row]
switch col {
case 0:
return item.Index
case 1:
return item.Client.Name
case 2:
return item.Client.Endpoint
case 3:
return fmt.Sprintf("%d ms", item.Client.Timeout)
case 4:
return len(item.Client.NodeList)
case 5:
return SwitchName(item.Client.Enable)
case 6:
return SwitchName(item.Client.Store)
}
panic("unexpected col")
}
func (n *ClientTable) Checked(row int) bool {
return n.items[row].checked
}
func (n *ClientTable) SetChecked(row int, checked bool) error {
n.items[row].checked = checked
return nil
}
func (m *ClientTable) Sort(col int, order walk.SortOrder) error {
m.sortColumn, m.sortOrder = col, order
sort.SliceStable(m.items, func(i, j int) bool {
a, b := m.items[i], m.items[j]
c := func(ls bool) bool {
if m.sortOrder == walk.SortAscending {
return ls
}
return !ls
}
switch m.sortColumn {
case 0:
return c(a.Index < b.Index)
case 1:
return c(a.Client.Name < b.Client.Name)
case 2:
return c(a.Client.Endpoint < b.Client.Endpoint)
case 3:
return c(a.Client.Timeout < b.Client.Timeout)
case 4:
return c(len(a.Client.NodeList) < len(b.Client.NodeList))
case 5:
return c(a.Client.Enable)
case 6:
return c(a.Client.Store)
}
panic("unreachable")
})
return m.SorterBase.Sort(col, order)
}
var clientTable *ClientTable
var clientTableView *walk.TableView
func init() {
clientTable = new(ClientTable)
clientTable.items = make([]*ClientItem, 0)
}
func ClientTableInit(clients []ClientConfig) {
clientTable.Lock()
defer clientTable.Unlock()
item := make([]*ClientItem, 0)
for i, client := range clients {
item = append(item, &ClientItem{Index: i, Client: client})
}
clientTable.items = item
clientTable.PublishRowsReset()
clientTable.Sort(clientTable.sortColumn, clientTable.sortOrder)
}
func ClientTableDelete(config *Config) {
clientTable.Lock()
defer clientTable.Unlock()
clients := make([]ClientConfig, 0)
item := make([]*ClientItem, 0)
for _, v := range clientTable.items {
if !v.checked {
item = append(item, v)
clients = append(clients, v.Client)
}
}
config.UpdateClient(clients)
clientTable.items = item
clientTable.PublishRowsReset()
clientTable.Sort(clientTable.sortColumn, clientTable.sortOrder)
}
func ClientTableSelect() *ClientItem {
clientTable.Lock()
defer clientTable.Unlock()
for _, v := range clientTable.items {
if v.checked {
return v
}
}
return nil
}
func ClientAddDialog(from walk.Form, config *Config) {
var dlg *walk.Dialog
var nameLine, addressLine *walk.LineEdit
var enableCB, storeCB *walk.CheckBox
var acceptPB, cancelPB, testPB *walk.PushButton
var number *walk.NumberEdit
_, err := Dialog{
AssignTo: &dlg,
Title: "Adding a Client Configuration",
Icon: walk.IconInformation(),
MinSize: Size{Width: 600, Height: 300},
Size: Size{Width: 600, Height: 300},
Font: DefaultFont(),
DefaultButton: &acceptPB,
CancelButton: &cancelPB,
Layout: VBox{},
Children: []Widget{
Composite{
Layout: Grid{Columns: 2},
Children: []Widget{
Label{
Text: "Client Name:",
},
LineEdit{
AssignTo: &nameLine,
},
Label{
Text: "Data Collection Address:",
},
LineEdit{
AssignTo: &addressLine,
},
Label{
Text: "Data Collection Frequency:",
},
NumberEdit{
AssignTo: &number,
Value: float64(1000),
ToolTipText: "100~10000 ms",
MaxValue: 10000,
MinValue: 100,
},
HSpacer{},
Composite{
Layout: HBox{},
Children: []Widget{
CheckBox{
Text: "Collection Enable",
AssignTo: &enableCB,
Checked: true,
},
CheckBox{
Text: "Data Store",
AssignTo: &storeCB,
Checked: true,
},
PushButton{
AssignTo: &testPB,
Text: "Connectivity Test",
OnClicked: func() {
testPB.SetEnabled(false)
defer testPB.SetEnabled(true)
err := ClientTest(addressLine.Text())
if err != nil {
ErrorBoxAction(dlg, "OPCUA connection failed:"+err.Error())
} else {
InfoBoxAction(dlg, "OPC Connection Successful!")
}
},
},
},
},
},
},
Composite{
Layout: HBox{},
Children: []Widget{
HSpacer{},
PushButton{
AssignTo: &acceptPB,
Text: "Accept",
OnClicked: func() {
if nameLine.Text() == "" {
ErrorBoxAction(dlg, "The client name cannot be empty!")
return
}
if addressLine.Text() == "" {
ErrorBoxAction(dlg, "The data collection address cannot be empty!")
return
}
err := config.Add(ClientConfig{
Name: nameLine.Text(),
Endpoint: addressLine.Text(),
Timeout: int(number.Value()),
Enable: enableCB.Checked(),
Store: storeCB.Checked()})
if err != nil {
ErrorBoxAction(dlg, "Add client failed: "+err.Error())
return
}
ClientTableInit(config.Clients)
dlg.Accept()
logs.Info("client add dialog accept")
},
},
PushButton{
AssignTo: &cancelPB,
Text: "Cancel",
OnClicked: func() {
dlg.Cancel()
logs.Info("client add dialog cancel")
},
},
HSpacer{},
},
},
},
}.Run(from)
if err != nil {
logs.Error("ClientAddDialog: %s", err.Error())
}
}
func ClientDialog(from walk.Form, config *Config) {
var dlg *walk.Dialog
var addPB, editPB, deletePB *walk.PushButton
ClientTableInit(config.Clients)
_, err := Dialog{
AssignTo: &dlg,
Title: "OPCUA Client Configuration",
Icon: walk.IconInformation(),
MinSize: Size{Width: 800, Height: 400},
Size: Size{Width: 800, Height: 400},
Font: DefaultFont(),
Layout: VBox{},
Children: []Widget{
Label{
Text: "Client List:",
},
TableView{
AssignTo: &clientTableView,
AlternatingRowBG: true,
ColumnsOrderable: true,
CheckBoxes: true,
Columns: []TableViewColumn{
{Title: "#", Width: 60},
{Title: "Client Name", Width: 120},
{Title: "Data Collection address", Width: 200},
{Title: "Data Collection Frequency", Width: 100},
{Title: "Number Nodes", Width: 80},
{Title: "Collection Enable", Width: 80},
{Title: "Data Store", Width: 80},
},
StyleCell: func(style *walk.CellStyle) {
if style.Row()%2 == 0 {
style.BackgroundColor = walk.RGB(248, 248, 255)
} else {
style.BackgroundColor = walk.RGB(220, 220, 220)
}
},
Model: clientTable,
OnItemActivated: func() {
index := clientTableView.CurrentIndex()
if index >= len(clientTable.items) {
ErrorBoxAction(from, "The edited client does not exist!")
return
}
ClientNodeEditDialog(dlg, clientTable.items[index], config)
},
},
VSpacer{},
Composite{
Layout: HBox{},
Children: []Widget{
PushButton{
AssignTo: &addPB,
Text: "Add",
OnClicked: func() {
ClientAddDialog(dlg, config)
},
},
PushButton{
AssignTo: &editPB,
Text: "Edit",
OnClicked: func() {
item := ClientTableSelect()
if item == nil {
InfoBoxAction(dlg, "Please select an item")
} else {
ClientNodeEditDialog(dlg, item, config)
}
},
},
PushButton{
AssignTo: &deletePB,
Text: "Delete",
OnClicked: func() {
ClientTableDelete(config)
},
},
HSpacer{},
},
},
},
}.Run(from)
if err != nil {
logs.Error("ClientDialog: %s", err.Error())
}
}