| 1 | // Copyright (C) 2018 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 "qopcuawriteitem.h" | 
| 5 |  | 
| 6 | QT_BEGIN_NAMESPACE | 
| 7 |  | 
| 8 | /*! | 
| 9 |     \class QOpcUaWriteItem | 
| 10 |     \inmodule QtOpcUa | 
| 11 |     \brief This class stores the options for a write operation. | 
| 12 |  | 
| 13 |     A write operation on an OPC UA server overwrites the entire value or a certain index range of the value of an | 
| 14 |     attribute of a node on the server. This class contains the necessary information for the backend to make | 
| 15 |     a write request to the server. | 
| 16 |  | 
| 17 |     One or multiple objects of this class make up the request of a \l QOpcUaClient::writeNodeAttributes() operation. | 
| 18 |  | 
| 19 |     \sa QOpcUaClient::writeNodeAttributes() QOpcUaWriteResult | 
| 20 | */ | 
| 21 | class QOpcUaWriteItemData : public QSharedData | 
| 22 | { | 
| 23 | public: | 
| 24 |     QString nodeId; | 
| 25 |     QOpcUa::NodeAttribute attribute {QOpcUa::NodeAttribute::Value}; | 
| 26 |     QString indexRange; | 
| 27 |     QVariant value; | 
| 28 |     QOpcUa::Types type {QOpcUa::Types::Undefined}; | 
| 29 |     QDateTime sourceTimestamp; | 
| 30 |     QDateTime serverTimestamp; | 
| 31 |     QOpcUa::UaStatusCode statusCode {QOpcUa::UaStatusCode::Good}; | 
| 32 |     bool hasStatusCode {false}; | 
| 33 | }; | 
| 34 |  | 
| 35 | /*! | 
| 36 |     Default constructs a write item with no parameters set. | 
| 37 | */ | 
| 38 | QOpcUaWriteItem::QOpcUaWriteItem() | 
| 39 |     : data(new QOpcUaWriteItemData) | 
| 40 | { | 
| 41 | } | 
| 42 |  | 
| 43 | /*! | 
| 44 |     Creates a new write item from \a other. | 
| 45 | */ | 
| 46 | QOpcUaWriteItem::QOpcUaWriteItem(const QOpcUaWriteItem &other) | 
| 47 |     : data(other.data) | 
| 48 | { | 
| 49 | } | 
| 50 |  | 
| 51 | /*! | 
| 52 |     Creates a write item for the attribute \a attribute from node \a nodeId. | 
| 53 |     The value \a value of type \a type will be written at position \a indexRange of \a attribute. | 
| 54 | */ | 
| 55 | QOpcUaWriteItem::QOpcUaWriteItem(const QString &nodeId, QOpcUa::NodeAttribute attribute, | 
| 56 |                                  const QVariant &value, QOpcUa::Types type, const QString &indexRange) | 
| 57 |     : data(new QOpcUaWriteItemData) | 
| 58 | { | 
| 59 |     setNodeId(nodeId); | 
| 60 |     setAttribute(attribute); | 
| 61 |     setValue(value); | 
| 62 |     setType(type); | 
| 63 |     setIndexRange(indexRange); | 
| 64 | } | 
| 65 |  | 
| 66 | /*! | 
| 67 |     Sets the values from \a rhs in this write item. | 
| 68 | */ | 
| 69 | QOpcUaWriteItem &QOpcUaWriteItem::operator=(const QOpcUaWriteItem &rhs) | 
| 70 | { | 
| 71 |     if (this != &rhs) | 
| 72 |         data.operator=(o: rhs.data); | 
| 73 |     return *this; | 
| 74 | } | 
| 75 |  | 
| 76 | QOpcUaWriteItem::~QOpcUaWriteItem() | 
| 77 | { | 
| 78 | } | 
| 79 |  | 
| 80 | /*! | 
| 81 |     Returns the node id of the write item. | 
| 82 | */ | 
| 83 | QString QOpcUaWriteItem::nodeId() const | 
| 84 | { | 
| 85 |     return data->nodeId; | 
| 86 | } | 
| 87 |  | 
| 88 | /*! | 
| 89 |     Sets the node id of the write item to \a nodeId. | 
| 90 | */ | 
| 91 | void QOpcUaWriteItem::setNodeId(const QString &nodeId) | 
| 92 | { | 
| 93 |     data->nodeId = nodeId; | 
| 94 | } | 
| 95 |  | 
| 96 | /*! | 
| 97 |     Returns the attribute of the write item. | 
| 98 | */ | 
| 99 | QOpcUa::NodeAttribute QOpcUaWriteItem::attribute() const | 
| 100 | { | 
| 101 |     return data->attribute; | 
| 102 | } | 
| 103 |  | 
| 104 | /*! | 
| 105 |     Sets the attribute of the write item to \a attribute. | 
| 106 | */ | 
| 107 | void QOpcUaWriteItem::setAttribute(QOpcUa::NodeAttribute attribute) | 
| 108 | { | 
| 109 |     data->attribute = attribute; | 
| 110 | } | 
| 111 |  | 
| 112 | /*! | 
| 113 |     Returns the index range of the write item. | 
| 114 | */ | 
| 115 | QString QOpcUaWriteItem::indexRange() const | 
| 116 | { | 
| 117 |     return data->indexRange; | 
| 118 | } | 
| 119 |  | 
| 120 | /*! | 
| 121 |     Sets the index range of the write item to \a indexRange. | 
| 122 | */ | 
| 123 | void QOpcUaWriteItem::setIndexRange(const QString &indexRange) | 
| 124 | { | 
| 125 |     data->indexRange = indexRange; | 
| 126 | } | 
| 127 |  | 
| 128 | /*! | 
| 129 |     Returns the value of the write item. | 
| 130 | */ | 
| 131 | QVariant QOpcUaWriteItem::value() const | 
| 132 | { | 
| 133 |     return data->value; | 
| 134 | } | 
| 135 |  | 
| 136 | /*! | 
| 137 |     Sets the value of the write item to \a value. | 
| 138 |     If given, the type information from \l setType() will be used in converting | 
| 139 |     the value to a SDK specific data type. | 
| 140 |  | 
| 141 |     \sa setType() | 
| 142 | */ | 
| 143 | void QOpcUaWriteItem::setValue(const QVariant &value) | 
| 144 | { | 
| 145 |     data->value = value; | 
| 146 | } | 
| 147 |  | 
| 148 | /*! | 
| 149 |     Sets the value of the write item to \value and the type of the value to \a type. | 
| 150 | */ | 
| 151 | void QOpcUaWriteItem::setValue(const QVariant &value, QOpcUa::Types type) | 
| 152 | { | 
| 153 |     data->value = value; | 
| 154 |     data->type = type; | 
| 155 | } | 
| 156 |  | 
| 157 | /*! | 
| 158 |     Returns the type of the value of the write item. | 
| 159 | */ | 
| 160 | QOpcUa::Types QOpcUaWriteItem::type() const | 
| 161 | { | 
| 162 |     return data->type; | 
| 163 | } | 
| 164 |  | 
| 165 | /*! | 
| 166 |     Sets the type of the value of the write item to \a type. | 
| 167 | */ | 
| 168 | void QOpcUaWriteItem::setType(QOpcUa::Types type) | 
| 169 | { | 
| 170 |     data->type = type; | 
| 171 | } | 
| 172 |  | 
| 173 | /*! | 
| 174 |     Returns the source timestamp for the value to write. | 
| 175 | */ | 
| 176 | QDateTime QOpcUaWriteItem::sourceTimestamp() const | 
| 177 | { | 
| 178 |     return data->sourceTimestamp; | 
| 179 | } | 
| 180 |  | 
| 181 | /*! | 
| 182 |     Sets the source timestamp for the value to write to \a sourceTimestamp. | 
| 183 |     If the source timestamp is invalid, it is ignored by the client and not sent to the server. | 
| 184 |     If the server doesn't support writing timestamps, the write operation for this item | 
| 185 |     will fail with status code \l {QOpcUa::UaStatusCode} {BadWriteNotSupported}. | 
| 186 | */ | 
| 187 | void QOpcUaWriteItem::setSourceTimestamp(const QDateTime &sourceTimestamp) | 
| 188 | { | 
| 189 |     data->sourceTimestamp = sourceTimestamp; | 
| 190 | } | 
| 191 |  | 
| 192 | /*! | 
| 193 |     Returns the server timestamp for the value to write. | 
| 194 | */ | 
| 195 | QDateTime QOpcUaWriteItem::serverTimestamp() const | 
| 196 | { | 
| 197 |     return data->serverTimestamp; | 
| 198 | } | 
| 199 |  | 
| 200 | /*! | 
| 201 |     Sets the server timestamp for the value to write to \a serverTimestamp. | 
| 202 |     If the server timestamp is invalid, it is ignored by the client and not sent to the server. | 
| 203 |     If the server doesn't support writing timestamps, the write operation for this item | 
| 204 |     will fail with status code \l {QOpcUa::UaStatusCode} {BadWriteNotSupported}. | 
| 205 | */ | 
| 206 | void QOpcUaWriteItem::setServerTimestamp(const QDateTime &serverTimestamp) | 
| 207 | { | 
| 208 |     data->serverTimestamp = serverTimestamp; | 
| 209 | } | 
| 210 |  | 
| 211 | /*! | 
| 212 |     Returns the status code for the value to write. | 
| 213 | */ | 
| 214 | QOpcUa::UaStatusCode QOpcUaWriteItem::statusCode() const | 
| 215 | { | 
| 216 |     return data->statusCode; | 
| 217 | } | 
| 218 |  | 
| 219 | /*! | 
| 220 |     Returns true if a status code for the value to write has been set. | 
| 221 | */ | 
| 222 | bool QOpcUaWriteItem::hasStatusCode() const | 
| 223 | { | 
| 224 |     return data->statusCode; | 
| 225 | } | 
| 226 |  | 
| 227 | /*! | 
| 228 |     Sets the status code for the value to write to \a statusCode. | 
| 229 |     If no status code is set, no status code is sent to the server. | 
| 230 | */ | 
| 231 | void QOpcUaWriteItem::setStatusCode(QOpcUa::UaStatusCode statusCode) | 
| 232 | { | 
| 233 |     data->statusCode = statusCode; | 
| 234 |     data->hasStatusCode = true; | 
| 235 | } | 
| 236 |  | 
| 237 | QT_END_NAMESPACE | 
| 238 |  |