| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <private/opcuaeventfilter_p.h> |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | /*! |
| 9 | \qmltype EventFilter |
| 10 | \inqmlmodule QtOpcUa |
| 11 | \brief Defines an EventFilter for a monitored item. |
| 12 | \since QtOpcUa 5.13 |
| 13 | \deprecated [6.9] |
| 14 | |
| 15 | An event filter is required for monitoring events on the server. |
| 16 | It consists of \c select clauses and a \c where clause. |
| 17 | |
| 18 | The \c select clauses are used to specify the data the user wants to receive when an event occurs. |
| 19 | It consists of \l {SimpleAttributeOperand} simple attribute operands which select |
| 20 | attributes of child nodes of an event type, for example the value attribute of the "Message" |
| 21 | property of BaseEventType. |
| 22 | |
| 23 | The \c where clause is used to restrict the reported events by matching against certain criteria. |
| 24 | Several operators and four different operand types allow filtering based on the values of the |
| 25 | attributes of the child nodes of an event type. |
| 26 | |
| 27 | The select clause consists of an array of \l SimpleAttributeOperand. |
| 28 | The where clause consists of an array of \l SimpleAttributeOperand, \l LiteralOperand, \l ElementOperand or \l AttributeOperand. |
| 29 | |
| 30 | The following EventFilter tells the server to report the value of the "Message" field for events that have a "Severity" field with value >= 500: |
| 31 | |
| 32 | \code |
| 33 | import QtOpcUa as QtOpcUa |
| 34 | QtOpcUa.Node { |
| 35 | ... |
| 36 | |
| 37 | eventFilter: QtOpcUa.EventFilter { |
| 38 | select: [ |
| 39 | QtOpcUa.SimpleAttributeOperand { |
| 40 | browsePath: [ |
| 41 | QtOpcUa.NodeId { |
| 42 | identifier: "Severity" |
| 43 | ns: "http://opcfoundation.org/UA/" |
| 44 | } |
| 45 | ] |
| 46 | }, |
| 47 | QtOpcUa.SimpleAttributeOperand { |
| 48 | browsePath: [ |
| 49 | QtOpcUa.NodeId { |
| 50 | identifier: "Message" |
| 51 | ns: "http://opcfoundation.org/UA/" |
| 52 | } |
| 53 | ] |
| 54 | } |
| 55 | ] |
| 56 | |
| 57 | where: [ |
| 58 | QtOpcUa.FilterElement { |
| 59 | operator: QtOpcUa.FilterElement.GreaterThanOrEqual |
| 60 | firstOperand: QtOpcUa.SimpleAttributeOperand { |
| 61 | browsePath: [ |
| 62 | QtOpcUa.NodeId { |
| 63 | identifier: "Severity" |
| 64 | ns: "http://opcfoundation.org/UA/" |
| 65 | } |
| 66 | ] |
| 67 | } |
| 68 | secondOperand: QtOpcUa.LiteralOperand { |
| 69 | value: 700 |
| 70 | type: QtOpcUa.Constants.UInt16 |
| 71 | } |
| 72 | } |
| 73 | ] |
| 74 | } |
| 75 | } |
| 76 | \endcode |
| 77 | |
| 78 | For a more complex example with two conditions, see \l QOpcUaElementOperand. |
| 79 | |
| 80 | \sa FilterElement |
| 81 | */ |
| 82 | |
| 83 | /*! |
| 84 | \qmlproperty list<FilterElement> EventFilter::where |
| 85 | |
| 86 | Content filter used to restrict the reported events to events matching certain criteria. |
| 87 | */ |
| 88 | |
| 89 | /*! |
| 90 | \qmlproperty list<SimpleAttributeOperand> EventFilter::select |
| 91 | |
| 92 | Selected event fields that shall be included when a new event is reported. |
| 93 | */ |
| 94 | |
| 95 | OpcUaEventFilter::OpcUaEventFilter(QObject *parent) |
| 96 | : QObject(parent) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | OpcUaEventFilter::~OpcUaEventFilter() = default; |
| 101 | |
| 102 | bool OpcUaEventFilter::operator==(const OpcUaEventFilter &other) const |
| 103 | { |
| 104 | return this->m_filterElements == other.m_filterElements |
| 105 | && this->m_selectors == other.m_selectors; |
| 106 | } |
| 107 | |
| 108 | QOpcUaMonitoringParameters::EventFilter OpcUaEventFilter::filter(QOpcUaClient *client) const |
| 109 | { |
| 110 | QOpcUaMonitoringParameters::EventFilter filterValue; |
| 111 | for (const auto &i : std::as_const(t: m_selectors)) |
| 112 | filterValue.selectClausesRef().append(t: i->toSimpleAttributeOperand(client)); |
| 113 | |
| 114 | for (const auto &i : std::as_const(t: m_filterElements)) |
| 115 | filterValue.whereClauseRef().append(t: i->toFilterElement(client)); |
| 116 | |
| 117 | return filterValue; |
| 118 | } |
| 119 | |
| 120 | QQmlListProperty<OpcUaFilterElement> OpcUaEventFilter::filterElements() |
| 121 | { |
| 122 | return QQmlListProperty<OpcUaFilterElement>(this, this, |
| 123 | &OpcUaEventFilter::appendFilterElement, |
| 124 | &OpcUaEventFilter::filterElementCount, |
| 125 | &OpcUaEventFilter::filterElement, |
| 126 | &OpcUaEventFilter::clearFilterElements); |
| 127 | } |
| 128 | |
| 129 | void OpcUaEventFilter::appendFilterElement(OpcUaFilterElement *nodeId) |
| 130 | { |
| 131 | m_filterElements.append(t: nodeId); |
| 132 | emit dataChanged(); |
| 133 | } |
| 134 | |
| 135 | int OpcUaEventFilter::filterElementCount() const |
| 136 | { |
| 137 | return m_filterElements.size(); |
| 138 | } |
| 139 | |
| 140 | OpcUaFilterElement *OpcUaEventFilter::filterElement(int index) const |
| 141 | { |
| 142 | return m_filterElements.at(i: index); |
| 143 | } |
| 144 | |
| 145 | void OpcUaEventFilter::clearFilterElements() |
| 146 | { |
| 147 | m_filterElements.clear(); |
| 148 | emit dataChanged(); |
| 149 | } |
| 150 | |
| 151 | void OpcUaEventFilter::appendFilterElement(QQmlListProperty<OpcUaFilterElement> *list, OpcUaFilterElement *nodeId) |
| 152 | { |
| 153 | reinterpret_cast<OpcUaEventFilter*>(list->data)->appendFilterElement(nodeId); |
| 154 | } |
| 155 | |
| 156 | qsizetype OpcUaEventFilter::filterElementCount(QQmlListProperty<OpcUaFilterElement> *list) |
| 157 | { |
| 158 | return reinterpret_cast<OpcUaEventFilter*>(list->data)->filterElementCount(); |
| 159 | } |
| 160 | |
| 161 | OpcUaFilterElement *OpcUaEventFilter::filterElement(QQmlListProperty<OpcUaFilterElement> *list, qsizetype index) |
| 162 | { |
| 163 | return reinterpret_cast<OpcUaEventFilter*>(list->data)->filterElement(index); |
| 164 | } |
| 165 | |
| 166 | void OpcUaEventFilter::clearFilterElements(QQmlListProperty<OpcUaFilterElement> *list) |
| 167 | { |
| 168 | reinterpret_cast<OpcUaEventFilter*>(list->data)->clearFilterElements(); |
| 169 | } |
| 170 | |
| 171 | QQmlListProperty<OpcUaSimpleAttributeOperand> OpcUaEventFilter::selectors() |
| 172 | { |
| 173 | return QQmlListProperty<OpcUaSimpleAttributeOperand>(this, this, |
| 174 | &OpcUaEventFilter::appendSelector, |
| 175 | &OpcUaEventFilter::selectorCount, |
| 176 | &OpcUaEventFilter::selector, |
| 177 | &OpcUaEventFilter::clearSelectors); |
| 178 | } |
| 179 | |
| 180 | void OpcUaEventFilter::appendSelector(OpcUaSimpleAttributeOperand *nodeId) |
| 181 | { |
| 182 | m_selectors.append(t: nodeId); |
| 183 | emit dataChanged(); |
| 184 | } |
| 185 | |
| 186 | int OpcUaEventFilter::selectorCount() const |
| 187 | { |
| 188 | return m_selectors.size(); |
| 189 | } |
| 190 | |
| 191 | OpcUaSimpleAttributeOperand *OpcUaEventFilter::selector(int index) const |
| 192 | { |
| 193 | return m_selectors.at(i: index); |
| 194 | } |
| 195 | |
| 196 | void OpcUaEventFilter::clearSelectors() |
| 197 | { |
| 198 | m_selectors.clear(); |
| 199 | emit dataChanged(); |
| 200 | } |
| 201 | |
| 202 | void OpcUaEventFilter::appendSelector(QQmlListProperty<OpcUaSimpleAttributeOperand> *list, OpcUaSimpleAttributeOperand *nodeId) |
| 203 | { |
| 204 | reinterpret_cast<OpcUaEventFilter*>(list->data)->appendSelector(nodeId); |
| 205 | } |
| 206 | |
| 207 | qsizetype OpcUaEventFilter::selectorCount(QQmlListProperty<OpcUaSimpleAttributeOperand> *list) |
| 208 | { |
| 209 | return reinterpret_cast<OpcUaEventFilter*>(list->data)->selectorCount(); |
| 210 | } |
| 211 | |
| 212 | OpcUaSimpleAttributeOperand *OpcUaEventFilter::selector(QQmlListProperty<OpcUaSimpleAttributeOperand> *list, qsizetype index) |
| 213 | { |
| 214 | return reinterpret_cast<OpcUaEventFilter*>(list->data)->selector(index); |
| 215 | } |
| 216 | |
| 217 | void OpcUaEventFilter::clearSelectors(QQmlListProperty<OpcUaSimpleAttributeOperand> *list) |
| 218 | { |
| 219 | reinterpret_cast<OpcUaEventFilter*>(list->data)->clearSelectors(); |
| 220 | } |
| 221 | |
| 222 | QT_END_NAMESPACE |
| 223 | |