aboutsummaryrefslogtreecommitdiffstats
path: root/demo/NodePage.qml
blob: 6368a8ea3848ec25f1d48910706ca52c25a5e90e (plain)
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
// NodePage.qml — Displays 10 OPC UA nodes per page with tooltips.
pragma ComponentBehavior: Bound

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Bobink

Page {
    id: nodePage

    readonly property var currentPage: pages[pageNumber - 1]
    required property int pageNumber
    readonly property var pages: [
        {
            "title": "Server Info",
            "description": "Standard OPC UA server nodes (namespace 0)."
                           + " CurrentTime updates live via monitoring.",
            "nodes": ["ns=0;i=2258" // CurrentTime
                , "ns=0;i=2257" // StartTime
                , "ns=0;i=2259" // State
                , "ns=0;i=2261" // ProductName
                , "ns=0;i=2264" // SoftwareVersion
            ]
        },
        {
            "title": "Read-Write Scalars",
            "description": "Single-value nodes with read and write access.",
            "nodes": ["ns=1;s=bool_rw_scalar", "ns=1;s=int16_rw_scalar",
                "ns=1;s=uint16_rw_scalar", "ns=1;s=int32_rw_scalar",
                "ns=1;s=uint32_rw_scalar", "ns=1;s=int64_rw_scalar",
                "ns=1;s=uint64_rw_scalar", "ns=1;s=float_rw_scalar",
                "ns=1;s=double_rw_scalar", "ns=1;s=string_rw_scalar",
                "ns=1;s=sbyte_rw_scalar", "ns=1;s=byte_rw_scalar",
                "ns=1;s=datetime_rw_scalar", "ns=1;s=guid_rw_scalar",
                "ns=1;s=bytestring_rw_scalar"]
        },
        {
            "title": "Read-Only Scalars",
            "description": "Single-value nodes with read-only access.",
            "nodes": ["ns=1;s=bool_ro_scalar", "ns=1;s=int16_ro_scalar",
                "ns=1;s=uint16_ro_scalar", "ns=1;s=int32_ro_scalar",
                "ns=1;s=uint32_ro_scalar", "ns=1;s=int64_ro_scalar",
                "ns=1;s=uint64_ro_scalar", "ns=1;s=float_ro_scalar",
                "ns=1;s=double_ro_scalar", "ns=1;s=string_ro_scalar",
                "ns=1;s=sbyte_ro_scalar", "ns=1;s=byte_ro_scalar",
                "ns=1;s=datetime_ro_scalar", "ns=1;s=guid_ro_scalar",
                "ns=1;s=bytestring_ro_scalar"]
        },
        {
            "title": "Read-Write Arrays",
            "description": "Array nodes. Values are displayed comma-separated."
                           + " To write, enter comma-separated values (e.g. \"1, 2, 3\")."
                           + " Commas cannot appear inside individual values.",
            "nodes": ["ns=1;s=bool_rw_array", "ns=1;s=int16_rw_array",
                "ns=1;s=uint16_rw_array", "ns=1;s=int32_rw_array",
                "ns=1;s=uint32_rw_array", "ns=1;s=int64_rw_array",
                "ns=1;s=uint64_rw_array", "ns=1;s=float_rw_array",
                "ns=1;s=double_rw_array", "ns=1;s=string_rw_array",
                "ns=1;s=sbyte_rw_array", "ns=1;s=byte_rw_array",
                "ns=1;s=datetime_rw_array", "ns=1;s=guid_rw_array",
                "ns=1;s=bytestring_rw_array"]
        },
        {
            "title": "Index Range Write",
            "description":
            "Write to specific array elements using OPC UA index"
            + " range syntax. Examples: \"0\" = first element,"
            + " \"0:2\" = elements 0–2. Enter the range and the value(s)"
            + " to write (comma-separated for multi-element ranges).",
            "indexRange": true,
            "nodes": ["ns=1;s=int32_rw_array", "ns=1;s=float_rw_array",
                "ns=1;s=string_rw_array"]
        },
        {
            "title": "Non-Existent Nodes",
            "description": "These node IDs do not exist on the server."
                           + " The row should show no value and no metadata in the tooltip.",
            "nodes": ["ns=1;s=does_not_exist", "ns=99;i=12345",
                "ns=1;s=also_missing"]
        },
        {
            "title": "Empty (Monitoring Test)",
            "description":
            "No nodes on this page. All previous pages are inactive,"
            + " so the server log should show zero active monitored items.",
            "nodes": []
        }
    ]

    // OPC UA ServerState enum (Part 4, Table 120).
    readonly property var serverStates: ["Running", "Failed", "NoConfiguration",
        "Suspended", "Shutdown", "Test", "CommunicationFault", "Unknown"]
    required property StackView stackRef

    signal logRequested(string message)

    function formatValue(node) {
        var v = node.value;
        if (v === undefined || String(v) === "")
            return "—";
        // Map ServerState enum to human-readable string.
        if (node.nodeId === "ns=0;i=2259" && !isNaN(v))
            return serverStates[v] || String(v);
        return String(v);
    }

    StackView.onStatusChanged: {
        if (nodePage.StackView.status === StackView.Active)
        nodePage.logRequested(nodePage.currentPage.title + " page loaded ("
                              + nodePage.currentPage.nodes.length + " nodes)");
    }

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 4

        // Header
        RowLayout {
            Label {
                font.bold: true
                font.pointSize: 14
                text: nodePage.currentPage.title
            }

            Label {
                color: "gray"
                text: "(" + nodePage.pageNumber + "/" + nodePage.pages.length
                      + ")"
            }

            Item {
                Layout.fillWidth: true
            }

            Button {
                text: "Disconnect"

                onClicked: Bobink.disconnectFromServer()
            }
        }

        Label {
            id: pageDescription

            Layout.fillWidth: true
            color: "gray"
            font.italic: true
            text: nodePage.currentPage.description || ""
            visible: nodePage.currentPage.description !== undefined
            wrapMode: Text.WordWrap
        }

        // Column headers
        RowLayout {
            Layout.fillWidth: true
            Layout.leftMargin: 12
            Layout.rightMargin: 12
            spacing: 12

            Label {
                Layout.preferredWidth: 160
                font.bold: true
                text: "Identifier"
            }

            Label {
                Layout.preferredWidth: 300
                font.bold: true
                text: "Value"
            }

            Label {
                Layout.fillWidth: true
                font.bold: true
                text: "Write"
            }
        }

        Rectangle {
            id: separator

            Layout.fillWidth: true
            color: "#ccc"
            implicitHeight: 1
        }

        // Nodes
        Repeater {
            model: nodePage.currentPage.nodes

            ItemDelegate {
                id: delegate

                required property int index
                required property string modelData

                Layout.fillWidth: true
                ToolTip.delay: 400
                ToolTip.text: "Display Name: " + (node.info.displayName || "—")
                              + "\nDescription: " + (node.info.description
                                                     || "—") + "\nNode Class: "
                              + (node.info.nodeClass || "—") + "\nData Type: "
                              + (node.info.dataType || "—") + "\nValue Rank: "
                              + (node.info.valueRank || "—")
                              + "\nArray Dimensions: " + (
                                  node.info.arrayDimensions || "—")
                              + "\nAccess Level: " + node.info.accessLevel
                              + "\nStatus: " + (node.info.status || "—")
                              + "\nSource Time: " + (
                                  node.info.sourceTimestamp.toLocaleString()
                                  || "—") + "\nServer Time: " + (
                                  node.info.serverTimestamp.toLocaleString()
                                  || "—")
                ToolTip.visible: hovered
                leftPadding: 12
                padding: 4
                rightPadding: 12

                background: Rectangle {
                    color: delegate.index % 2 === 0 ? "#f8f8f8" : "transparent"
                    radius: 2
                }
                contentItem: RowLayout {
                    spacing: 12

                    // Column 1: Display name if available, otherwise short ID.
                    Label {
                        Layout.preferredWidth: 160
                        elide: Text.ElideRight
                        text: {
                            if (node.info.displayName)
                            return node.info.displayName;
                            var idx = node.nodeId.indexOf(";s=");
                            return idx >= 0 ? node.nodeId.substring(idx + 3) :
                                              node.nodeId;
                        }
                    }

                    // Column 2: Live value (always visible)
                    Label {
                        Layout.preferredWidth: 300
                        elide: Text.ElideRight
                        text: nodePage.formatValue(node)
                    }

                    // Column 3: Edit area (writable) or READ-ONLY label

                    // Normal write controls (non-index-range pages)
                    TextField {
                        id: editField

                        Layout.fillWidth: true
                        placeholderText: "Enter value..."
                        visible: node.writable &&
                                 !nodePage.currentPage.indexRange

                        onAccepted: node.writeValue(text)
                    }

                    Button {
                        text: "Write"
                        visible: node.writable &&
                                 !nodePage.currentPage.indexRange

                        onClicked: node.writeValue(editField.text)
                    }

                    // Index range write controls
                    TextField {
                        id: rangeField

                        Layout.preferredWidth: 60
                        placeholderText: "Range"
                        visible: node.writable
                                 && nodePage.currentPage.indexRange === true
                    }

                    TextField {
                        id: rangeValueField

                        Layout.fillWidth: true
                        placeholderText: "Value(s)..."
                        visible: node.writable
                                 && nodePage.currentPage.indexRange === true

                        onAccepted: node.writeValueAtRange(text,
                                                           rangeField.text)
                    }

                    Button {
                        text: "Write Range"
                        visible: node.writable
                                 && nodePage.currentPage.indexRange === true

                        onClicked: node.writeValueAtRange(rangeValueField.text,
                                                          rangeField.text)
                    }

                    Label {
                        Layout.fillWidth: true
                        color: "gray"
                        font.italic: true
                        text: "(READ-ONLY)"
                        visible: !node.writable
                    }
                }

                OpcUaMonitoredNode {
                    id: node

                    monitored: nodePage.StackView.status === StackView.Active
                    nodeId: delegate.modelData

                    onWriteCompleted: (success, message) => {
                        var short_id = node.nodeId.substring(node.nodeId.indexOf(
                                                                 ";s=") + 3);
                        nodePage.logRequested(short_id + ": " + message);
                    }
                }
            }
        }

        Item {
            Layout.fillHeight: true
        }

        // Navigation
        RowLayout {
            Layout.fillWidth: true

            Button {
                text: "← Previous"
                visible: nodePage.pageNumber > 1

                onClicked: nodePage.stackRef.pop()
            }

            Item {
                Layout.fillWidth: true
            }

            Button {
                text: "Next →"
                visible: nodePage.pageNumber < nodePage.pages.length

                onClicked: nodePage.stackRef.push("NodePage.qml", {
                                                      "stackRef":
                                                      nodePage.stackRef,
                                                      "pageNumber":
                                                      nodePage.pageNumber + 1
                                                  })
            }
        }
    }
}