| 1 | // Copyright (C) 2023 basysKom GmbH, opensource@basyskom.com | 
|---|---|
| 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 "qopcuahistoryevent.h" | 
| 5 | |
| 6 | #include <QtCore/qlist.h> | 
| 7 | |
| 8 | QT_BEGIN_NAMESPACE | 
| 9 | |
| 10 | /*! | 
| 11 | \class QOpcUaHistoryEvent | 
| 12 | \inmodule QtOpcUa | 
| 13 | \brief This class stores historical events from a node. | 
| 14 | \since 6.7 | 
| 15 | |
| 16 | When a request to read history events is being handled, instances of this class | 
| 17 | are used to store information about which node has been read, its events and | 
| 18 | the status code. | 
| 19 | The finished signal of a history read response will return a list of QOpcUaHistoryEvent | 
| 20 | objects which can be parsed to review the results of the request. | 
| 21 | */ | 
| 22 | |
| 23 | class QOpcUaHistoryEventData : public QSharedData | 
| 24 | { | 
| 25 | public: | 
| 26 | QList<QVariantList> events; | 
| 27 | QOpcUa::UaStatusCode statusCode; | 
| 28 | QString nodeId; | 
| 29 | }; | 
| 30 | |
| 31 | QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QOpcUaHistoryEventData) | 
| 32 | |
| 33 | /*! | 
| 34 | Constructs an invalid history event item. | 
| 35 | */ | 
| 36 | QOpcUaHistoryEvent::QOpcUaHistoryEvent() | 
| 37 | : data(new QOpcUaHistoryEventData) | 
| 38 | { | 
| 39 | data->statusCode = QOpcUa::UaStatusCode::Good; | 
| 40 | } | 
| 41 | |
| 42 | /*! | 
| 43 | Constructs a history event item and stores which node it corresponds to. | 
| 44 | */ | 
| 45 | QOpcUaHistoryEvent::QOpcUaHistoryEvent(const QString &nodeId) | 
| 46 | : data(new QOpcUaHistoryEventData) | 
| 47 | { | 
| 48 | data->statusCode = QOpcUa::UaStatusCode::Good; | 
| 49 | data->nodeId = nodeId; | 
| 50 | } | 
| 51 | |
| 52 | /*! | 
| 53 | Constructs a history event item from \a other. | 
| 54 | */ | 
| 55 | QOpcUaHistoryEvent::QOpcUaHistoryEvent(const QOpcUaHistoryEvent &other) | 
| 56 | : data(other.data) | 
| 57 | { | 
| 58 | } | 
| 59 | |
| 60 | /*! | 
| 61 | Destroys the history event item. | 
| 62 | */ | 
| 63 | QOpcUaHistoryEvent::~QOpcUaHistoryEvent() | 
| 64 | { | 
| 65 | } | 
| 66 | |
| 67 | /*! | 
| 68 | \fn QOpcUaHistoryEvent::QOpcUaHistoryEvent(QOpcUaHistoryEvent &&other) | 
| 69 | |
| 70 | Move-constructs a new history event object from \a other. | 
| 71 | |
| 72 | \note The moved-from object \a other is placed in a | 
| 73 | partially-formed state, in which the only valid operations are | 
| 74 | destruction and assignment of a new value. | 
| 75 | */ | 
| 76 | |
| 77 | /*! | 
| 78 | \fn QOpcUaHistoryEvent &QOpcUaHistoryEvent::operator=(QOpcUaHistoryEvent &&other) | 
| 79 | |
| 80 | Move-assigns \a other to this QOpcUaHistoryEvent instance. | 
| 81 | |
| 82 | \note The moved-from object \a other is placed in a | 
| 83 | partially-formed state, in which the only valid operations are | 
| 84 | destruction and assignment of a new value. | 
| 85 | */ | 
| 86 | |
| 87 | /*! | 
| 88 | \fn void QOpcUaHistoryEvent::swap(QOpcUaHistoryEvent &other) | 
| 89 | |
| 90 | Swaps history event object \a other with this history event | 
| 91 | object. This operation is very fast and never fails. | 
| 92 | */ | 
| 93 | |
| 94 | /*! | 
| 95 | Returns the status code which indicates if an error occurred while fetching the history events. | 
| 96 | */ | 
| 97 | QOpcUa::UaStatusCode QOpcUaHistoryEvent::statusCode() const | 
| 98 | { | 
| 99 | return data->statusCode; | 
| 100 | } | 
| 101 | |
| 102 | /*! | 
| 103 | Sets the status code to \a statusCode. | 
| 104 | */ | 
| 105 | void QOpcUaHistoryEvent::setStatusCode(QOpcUa::UaStatusCode statusCode) | 
| 106 | { | 
| 107 | if (statusCode != data->statusCode) { | 
| 108 | data.detach(); | 
| 109 | data->statusCode = statusCode; | 
| 110 | } | 
| 111 | } | 
| 112 | |
| 113 | /*! | 
| 114 | Returns the list of \l QVariantList objects which contain the results of the history read request. | 
| 115 | */ | 
| 116 | QList<QVariantList> QOpcUaHistoryEvent::events() const | 
| 117 | { | 
| 118 | return data->events; | 
| 119 | } | 
| 120 | |
| 121 | /*! | 
| 122 | Returns the number of available events. | 
| 123 | */ | 
| 124 | int QOpcUaHistoryEvent::count() const | 
| 125 | { | 
| 126 | return data->events.size(); | 
| 127 | } | 
| 128 | |
| 129 | /*! | 
| 130 | Adds an event field list given by \a value. | 
| 131 | */ | 
| 132 | void QOpcUaHistoryEvent::addEvent(const QVariantList &value) | 
| 133 | { | 
| 134 | data.detach(); | 
| 135 | data->events.append(t: value); | 
| 136 | } | 
| 137 | |
| 138 | /*! | 
| 139 | Returns the nodeId of the node whose events have been stored. | 
| 140 | */ | 
| 141 | QString QOpcUaHistoryEvent::nodeId() const | 
| 142 | { | 
| 143 | return data->nodeId; | 
| 144 | } | 
| 145 | |
| 146 | /*! | 
| 147 | Sets the nodeId to \a nodeId. | 
| 148 | */ | 
| 149 | void QOpcUaHistoryEvent::setNodeId(const QString &nodeId) | 
| 150 | { | 
| 151 | if (nodeId != data->nodeId) { | 
| 152 | data.detach(); | 
| 153 | data->nodeId = nodeId; | 
| 154 | } | 
| 155 | } | 
| 156 | |
| 157 | /*! | 
| 158 | Sets the values from \a other in this history event item. | 
| 159 | */ | 
| 160 | QOpcUaHistoryEvent &QOpcUaHistoryEvent::operator=(const QOpcUaHistoryEvent &other) | 
| 161 | { | 
| 162 | if (this != &other) | 
| 163 | data.operator=(o: other.data); | 
| 164 | return *this; | 
| 165 | } | 
| 166 | |
| 167 | /*! | 
| 168 | \fn bool QOpcUaHistoryEvent::operator!=(const QOpcUaHistoryEvent &lhs, const QOpcUaHistoryEvent &rhs) | 
| 169 | |
| 170 | Returns \c true if \a lhs is not equal to \a rhs. | 
| 171 | */ | 
| 172 | |
| 173 | /*! | 
| 174 | \fn bool QOpcUaHistoryEvent::operator==(const QOpcUaHistoryEvent &lhs, const QOpcUaHistoryEvent &rhs) | 
| 175 | |
| 176 | Returns \c true if \a rhs and \a lhs contain the same values. | 
| 177 | */ | 
| 178 | bool comparesEqual(const QOpcUaHistoryEvent &lhs, const QOpcUaHistoryEvent &rhs) noexcept | 
| 179 | { | 
| 180 | return lhs.events() == rhs.events() && lhs.statusCode() == rhs.statusCode() && lhs.nodeId() == rhs.nodeId(); | 
| 181 | } | 
| 182 | |
| 183 | QT_END_NAMESPACE | 
| 184 | 
