| 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/opcuawriteitem_p.h> | 
| 5 | #include <private/opcuawriteresult_p.h> | 
| 6 | |
| 7 | #include <QJSValue> | 
| 8 | |
| 9 | QT_BEGIN_NAMESPACE | 
| 10 | |
| 11 | /*! | 
| 12 | \qmltype WriteItem | 
| 13 | \inqmlmodule QtOpcUa | 
| 14 | \brief Specifies an item to be written to the server. | 
| 15 | \since QtOpcUa 5.13 | 
| 16 | |
| 17 | This type is used to specify items to be written to the server using the function | 
| 18 | \l Connection::writeNodeAttributes. | 
| 19 | */ | 
| 20 | |
| 21 | /*! | 
| 22 | \qmlproperty Constants.NodeAttribute WriteItem::attribute | 
| 23 | |
| 24 | Determines the attribute of the node to be written. | 
| 25 | */ | 
| 26 | |
| 27 | /*! | 
| 28 | \qmlproperty string WriteItem::indexRange | 
| 29 | |
| 30 | Determines the index range of the attribute to be written. | 
| 31 | If not needed, leave this property empty. | 
| 32 | */ | 
| 33 | |
| 34 | /*! | 
| 35 | \qmlproperty string Writetem::nodeId | 
| 36 | |
| 37 | Determines the node id of the node to be written. | 
| 38 | */ | 
| 39 | |
| 40 | /*! | 
| 41 | \qmlproperty variant WriteItem::ns | 
| 42 | |
| 43 | Determines the namespace of the node to be written. | 
| 44 | The namespace can be given by name or index. | 
| 45 | If this property is given, any namespace in the node id will be | 
| 46 | ignored. | 
| 47 | */ | 
| 48 | |
| 49 | /*! | 
| 50 | \qmlproperty datetime WriteItem::serverTimestamp | 
| 51 | |
| 52 | Sets the server timestamp for the value to be written. | 
| 53 | If the server timestamp is invalid, it is ignored by the client and not sent to the server. | 
| 54 | If the server doesn't support writing timestamps, the write operation for this item | 
| 55 | will fail. | 
| 56 | */ | 
| 57 | |
| 58 | /*! | 
| 59 | \qmlproperty datetime WriteItem::sourceTimestamp | 
| 60 | |
| 61 | Sets the source timestamp for the value to write. | 
| 62 | If the source timestamp is invalid, it is ignored by the client and not sent to the server. | 
| 63 | If the server doesn't support writing timestamps, the write operation for this item | 
| 64 | will fail. | 
| 65 | */ | 
| 66 | |
| 67 | /*! | 
| 68 | \qmlproperty variant WriteItem::value | 
| 69 | |
| 70 | Actual data that is requested to be written. | 
| 71 | */ | 
| 72 | |
| 73 | /*! | 
| 74 | \qmlproperty variant WriteItem::valueType | 
| 75 | |
| 76 | If given, the type information will be used in converting | 
| 77 | the value to a SDK specific data type. | 
| 78 | */ | 
| 79 | |
| 80 | /*! | 
| 81 | \qmlproperty OpcUaStatus WriteItem::statusCode | 
| 82 | |
| 83 | Sets the status code for the value to write. | 
| 84 | If no status code is set, no status code is sent to the server. | 
| 85 | */ | 
| 86 | |
| 87 | class OpcUaWriteItemData : public QSharedData | 
| 88 | { | 
| 89 | public: | 
| 90 | QOpcUa::NodeAttribute attribute; | 
| 91 | QString indexRange; | 
| 92 | QString nodeId; | 
| 93 | QVariant namespaceIdentifier; | 
| 94 | QDateTime serverTimestamp; | 
| 95 | QDateTime sourceTimestamp; | 
| 96 | QVariant value; | 
| 97 | QOpcUa::Types valueType = QOpcUa::Types::Undefined; | 
| 98 | OpcUaStatus::Status statusCode; | 
| 99 | bool hasStatusCode = false; | 
| 100 | }; | 
| 101 | |
| 102 | OpcUaWriteItem::OpcUaWriteItem() | 
| 103 | : data(new OpcUaWriteItemData) | 
| 104 | { | 
| 105 | data->attribute = QOpcUa::NodeAttribute::Value; | 
| 106 | } | 
| 107 | |
| 108 | OpcUaWriteItem::OpcUaWriteItem(const OpcUaWriteItem &other) | 
| 109 | : data(other.data) | 
| 110 | { | 
| 111 | } | 
| 112 | |
| 113 | OpcUaWriteItem &OpcUaWriteItem::operator=(const OpcUaWriteItem &rhs) | 
| 114 | { | 
| 115 | if (this != &rhs) | 
| 116 | data.operator=(o: rhs.data); | 
| 117 | return *this; | 
| 118 | } | 
| 119 | |
| 120 | OpcUaWriteItem::~OpcUaWriteItem() = default; | 
| 121 | |
| 122 | const QString &OpcUaWriteItem::indexRange() const | 
| 123 | { | 
| 124 | return data->indexRange; | 
| 125 | } | 
| 126 | |
| 127 | void OpcUaWriteItem::setIndexRange(const QString &indexRange) | 
| 128 | { | 
| 129 | data->indexRange = indexRange; | 
| 130 | } | 
| 131 | |
| 132 | const QString &OpcUaWriteItem::nodeId() const | 
| 133 | { | 
| 134 | return data->nodeId; | 
| 135 | } | 
| 136 | |
| 137 | void OpcUaWriteItem::setNodeId(const QString &nodeId) | 
| 138 | { | 
| 139 | data->nodeId = nodeId; | 
| 140 | } | 
| 141 | |
| 142 | QOpcUa::NodeAttribute OpcUaWriteItem::attribute() const | 
| 143 | { | 
| 144 | return data->attribute; | 
| 145 | } | 
| 146 | |
| 147 | void OpcUaWriteItem::setAttribute(QOpcUa::NodeAttribute attribute) | 
| 148 | { | 
| 149 | data->attribute = attribute; | 
| 150 | } | 
| 151 | |
| 152 | const QVariant &OpcUaWriteItem::namespaceIdentifier() const | 
| 153 | { | 
| 154 | return data->namespaceIdentifier; | 
| 155 | } | 
| 156 | |
| 157 | void OpcUaWriteItem::setNamespaceIdentifier(const QVariant &namespaceIdentifier) | 
| 158 | { | 
| 159 | data->namespaceIdentifier = namespaceIdentifier; | 
| 160 | } | 
| 161 | |
| 162 | void OpcUaWriteItem::setServerTimestamp(const QDateTime &serverTimestamp) | 
| 163 | { | 
| 164 | data->serverTimestamp = serverTimestamp; | 
| 165 | } | 
| 166 | |
| 167 | const QDateTime &OpcUaWriteItem::serverTimestamp() const | 
| 168 | { | 
| 169 | return data->serverTimestamp; | 
| 170 | } | 
| 171 | |
| 172 | void OpcUaWriteItem::setSourceTimestamp(const QDateTime &sourceTimestamp) | 
| 173 | { | 
| 174 | data->sourceTimestamp = sourceTimestamp; | 
| 175 | } | 
| 176 | |
| 177 | const QDateTime &OpcUaWriteItem::sourceTimestamp() const | 
| 178 | { | 
| 179 | return data->sourceTimestamp; | 
| 180 | } | 
| 181 | |
| 182 | void OpcUaWriteItem::setValue(const QVariant &value) | 
| 183 | { | 
| 184 | auto tmp = value; | 
| 185 | // In case of an array value the type is QJSValue, | 
| 186 | // which has to be unpacked to recognized. | 
| 187 | if (tmp.userType() == qMetaTypeId<QJSValue>()) | 
| 188 | tmp = tmp.value<QJSValue>().toVariant(); | 
| 189 | |
| 190 | data->value = tmp; | 
| 191 | } | 
| 192 | |
| 193 | const QVariant &OpcUaWriteItem::value() const | 
| 194 | { | 
| 195 | return data->value; | 
| 196 | } | 
| 197 | |
| 198 | QOpcUa::Types OpcUaWriteItem::valueType() const | 
| 199 | { | 
| 200 | return data->valueType; | 
| 201 | } | 
| 202 | |
| 203 | void OpcUaWriteItem::setValueType(QOpcUa::Types type) | 
| 204 | { | 
| 205 | data->valueType = type; | 
| 206 | } | 
| 207 | |
| 208 | OpcUaStatus::Status OpcUaWriteItem::statusCode() const | 
| 209 | { | 
| 210 | return data->statusCode; | 
| 211 | } | 
| 212 | |
| 213 | bool OpcUaWriteItem::hasStatusCode() const | 
| 214 | { | 
| 215 | return data->hasStatusCode; | 
| 216 | } | 
| 217 | |
| 218 | void OpcUaWriteItem::setStatusCode(OpcUaStatus::Status statusCode) | 
| 219 | { | 
| 220 | data->statusCode = statusCode; | 
| 221 | data->hasStatusCode = true; | 
| 222 | } | 
| 223 | |
| 224 | OpcUaWriteItem OpcUaWriteItemFactory::create() | 
| 225 | { | 
| 226 | return OpcUaWriteItem(); | 
| 227 | } | 
| 228 | |
| 229 | QT_END_NAMESPACE | 
| 230 | |
| 231 | 
