node-opcua-json 0.19.1 → 2.182.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +141 -4
- package/dist/decode_opcua_json.d.ts +31 -0
- package/dist/decode_opcua_json.js +61 -0
- package/dist/decode_opcua_json.js.map +1 -0
- package/dist/extension_object_constructor.d.ts +46 -0
- package/dist/extension_object_constructor.js +26 -0
- package/dist/extension_object_constructor.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/json_basic_encoding_body_functor.d.ts +34 -0
- package/dist/json_basic_encoding_body_functor.js +170 -0
- package/dist/json_basic_encoding_body_functor.js.map +1 -0
- package/dist/json_basic_encoding_decoding_byte_string.d.ts +27 -0
- package/dist/json_basic_encoding_decoding_byte_string.js +27 -0
- package/dist/json_basic_encoding_decoding_byte_string.js.map +1 -0
- package/dist/json_basic_encoding_decoding_data_value.d.ts +80 -0
- package/dist/json_basic_encoding_decoding_data_value.js +289 -0
- package/dist/json_basic_encoding_decoding_data_value.js.map +1 -0
- package/dist/json_basic_encoding_decoding_date_time.d.ts +26 -0
- package/dist/json_basic_encoding_decoding_date_time.js +43 -0
- package/dist/json_basic_encoding_decoding_date_time.js.map +1 -0
- package/dist/json_basic_encoding_decoding_extension_object.d.ts +65 -0
- package/dist/json_basic_encoding_decoding_extension_object.js +461 -0
- package/dist/json_basic_encoding_decoding_extension_object.js.map +1 -0
- package/dist/json_basic_encoding_decoding_int64.d.ts +30 -0
- package/dist/json_basic_encoding_decoding_int64.js +56 -0
- package/dist/json_basic_encoding_decoding_int64.js.map +1 -0
- package/dist/json_basic_encoding_decoding_localizedtext.d.ts +33 -0
- package/dist/json_basic_encoding_decoding_localizedtext.js +62 -0
- package/dist/json_basic_encoding_decoding_localizedtext.js.map +1 -0
- package/dist/json_basic_encoding_decoding_nodeid.d.ts +42 -0
- package/dist/json_basic_encoding_decoding_nodeid.js +298 -0
- package/dist/json_basic_encoding_decoding_nodeid.js.map +1 -0
- package/dist/json_basic_encoding_decoding_qualifiedname.d.ts +35 -0
- package/dist/json_basic_encoding_decoding_qualifiedname.js +126 -0
- package/dist/json_basic_encoding_decoding_qualifiedname.js.map +1 -0
- package/dist/json_basic_encoding_decoding_statuscode.d.ts +39 -0
- package/dist/json_basic_encoding_decoding_statuscode.js +139 -0
- package/dist/json_basic_encoding_decoding_statuscode.js.map +1 -0
- package/dist/json_basic_encoding_decoding_variant.d.ts +101 -0
- package/dist/json_basic_encoding_decoding_variant.js +234 -0
- package/dist/json_basic_encoding_decoding_variant.js.map +1 -0
- package/dist/json_encoding_scheme.d.ts +50 -0
- package/dist/json_encoding_scheme.js +117 -0
- package/dist/json_encoding_scheme.js.map +1 -0
- package/package.json +57 -37
- package/source/decode_opcua_json.ts +67 -0
- package/source/extension_object_constructor.ts +48 -0
- package/source/index.ts +59 -0
- package/source/json_basic_encoding_body_functor.ts +190 -0
- package/source/json_basic_encoding_decoding_byte_string.ts +52 -0
- package/source/json_basic_encoding_decoding_data_value.ts +392 -0
- package/source/json_basic_encoding_decoding_date_time.ts +42 -0
- package/source/json_basic_encoding_decoding_extension_object.ts +531 -0
- package/source/json_basic_encoding_decoding_int64.ts +74 -0
- package/source/json_basic_encoding_decoding_localizedtext.ts +69 -0
- package/source/json_basic_encoding_decoding_nodeid.ts +356 -0
- package/source/json_basic_encoding_decoding_qualifiedname.ts +144 -0
- package/source/json_basic_encoding_decoding_statuscode.ts +151 -0
- package/source/json_basic_encoding_decoding_variant.ts +339 -0
- package/source/json_encoding_scheme.ts +114 -0
- package/LICENSE.md +0 -40
- package/dist2/decode_opcua_json.d.ts +0 -12
- package/dist2/index.d.ts +0 -17
- package/dist2/index.js +0 -1
- package/dist2/json_basic_encoding_body_functor.d.ts +0 -15
- package/dist2/json_basic_encoding_decoding_byte_string.d.ts +0 -8
- package/dist2/json_basic_encoding_decoding_data_value.d.ts +0 -26
- package/dist2/json_basic_encoding_decoding_date_time.d.ts +0 -6
- package/dist2/json_basic_encoding_decoding_extension_object.d.ts +0 -32
- package/dist2/json_basic_encoding_decoding_int64.d.ts +0 -9
- package/dist2/json_basic_encoding_decoding_localizedtext.d.ts +0 -14
- package/dist2/json_basic_encoding_decoding_nodeid.d.ts +0 -33
- package/dist2/json_basic_encoding_decoding_qualifiedname.d.ts +0 -14
- package/dist2/json_basic_encoding_decoding_statuscode.d.ts +0 -16
- package/dist2/json_basic_encoding_decoding_variant.d.ts +0 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ==========================================================================
|
|
3
|
+
* Copyright (c) 2021-2026 Sterfive - etienne.rossignon@sterfive.com
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
|
13
|
+
* all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
18
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
* IN THE SOFTWARE.
|
|
22
|
+
* ==========================================================================
|
|
23
|
+
*/
|
|
24
|
+
import type { ByteString } from "node-opcua-basic-types";
|
|
25
|
+
import { JsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
26
|
+
|
|
27
|
+
export function opcuaJsonEncodeByteString(
|
|
28
|
+
byteString: ByteString | null,
|
|
29
|
+
scheme: JsonEncodingScheme,
|
|
30
|
+
_namespaceArray?: string[]
|
|
31
|
+
): string | null | undefined {
|
|
32
|
+
if (!byteString) {
|
|
33
|
+
if (scheme === JsonEncodingScheme.Compact) {
|
|
34
|
+
return undefined; // Compact encoding does not encode null values
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (scheme === JsonEncodingScheme.Compact && byteString.length === 0) {
|
|
39
|
+
return undefined; // Compact encoding does not encode empty ByteString
|
|
40
|
+
}
|
|
41
|
+
if (scheme === JsonEncodingScheme.Verbose && byteString.length === 0) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return byteString.toString("base64");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function opcuaJsonDecodeByteString(pojo: string | null): Buffer | null {
|
|
48
|
+
if (!pojo) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return Buffer.from(pojo, "base64");
|
|
52
|
+
}
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ==========================================================================
|
|
3
|
+
* Copyright (c) 2021-2026 Sterfive - etienne.rossignon@sterfive.com
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
|
13
|
+
* all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
18
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
* IN THE SOFTWARE.
|
|
22
|
+
* ==========================================================================
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { UInt16 } from "node-opcua-basic-types";
|
|
26
|
+
import { DataValue, type DataValueOptions } from "node-opcua-data-value";
|
|
27
|
+
import { DataSetFieldContentMask } from "node-opcua-types";
|
|
28
|
+
import { DataType } from "node-opcua-variant";
|
|
29
|
+
import type { ExtensionObjectBuilder } from "./json_basic_encoding_body_functor.js";
|
|
30
|
+
import {
|
|
31
|
+
opcuaJsonDecodeStatusCode,
|
|
32
|
+
opcuaJsonEncodeStatusCode104,
|
|
33
|
+
opcuaJsonEncodeStatusCode105,
|
|
34
|
+
type StatusCodeJSON
|
|
35
|
+
} from "./json_basic_encoding_decoding_statuscode.js";
|
|
36
|
+
import {
|
|
37
|
+
opcuaJsonDecodeVariant104,
|
|
38
|
+
opcuaJsonDecodeVariant105,
|
|
39
|
+
opcuaJsonEncodeVariant104,
|
|
40
|
+
opcuaJsonEncodeVariant105,
|
|
41
|
+
type VariantJSON104,
|
|
42
|
+
type VariantJSON105,
|
|
43
|
+
type VariantJSONBody
|
|
44
|
+
} from "./json_basic_encoding_decoding_variant.js";
|
|
45
|
+
import { JsonEncoderMode104, JsonEncoderMode105, JsonEncodingScheme, toJsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
46
|
+
|
|
47
|
+
export interface DataValueStatusAndTimestamps {
|
|
48
|
+
StatusCode?: StatusCodeJSON | number;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The source timestamp associated with the value.
|
|
52
|
+
* Not encoded if the value is DateTime.MinValue.
|
|
53
|
+
*/
|
|
54
|
+
SourceTimestamp?: Date; // DateTime
|
|
55
|
+
/**
|
|
56
|
+
* The number of 10 Picosecond intervals for the SourceTimestamp.
|
|
57
|
+
* Not encoded if the value is 0.
|
|
58
|
+
*/
|
|
59
|
+
SourcePicoseconds?: UInt16;
|
|
60
|
+
/**
|
|
61
|
+
* The server timestamp associated with the value.
|
|
62
|
+
* Not encoded if the value is DateTime.MinValue.
|
|
63
|
+
*/
|
|
64
|
+
ServerTimestamp?: Date;
|
|
65
|
+
/**
|
|
66
|
+
* The number of 10 Picosecond intervals for the ServerTimestamp.
|
|
67
|
+
* Not encoded if the value is 0.
|
|
68
|
+
*/
|
|
69
|
+
ServerPicoseconds?: UInt16;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// https://reference.opcfoundation.org/Core/Part6/v104/docs/5.4.2.18
|
|
73
|
+
export interface DataValueJSON104 extends DataValueStatusAndTimestamps {
|
|
74
|
+
Value?: VariantJSON104 | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface DataValueJSON105 extends VariantJSON105, DataValueStatusAndTimestamps {}
|
|
78
|
+
|
|
79
|
+
export type DataValueJSON = DataValueJSON105;
|
|
80
|
+
function _setTimestampsAndStatusCode(
|
|
81
|
+
pojo: DataValueStatusAndTimestamps,
|
|
82
|
+
dataValue: DataValue,
|
|
83
|
+
mask: DataSetFieldContentMask,
|
|
84
|
+
mode: JsonEncodingScheme
|
|
85
|
+
): void {
|
|
86
|
+
if (mask & DataSetFieldContentMask.ServerPicoSeconds && dataValue.serverPicoseconds) {
|
|
87
|
+
pojo.ServerPicoseconds = dataValue.serverPicoseconds;
|
|
88
|
+
}
|
|
89
|
+
if (mask & DataSetFieldContentMask.SourcePicoSeconds && dataValue.sourcePicoseconds) {
|
|
90
|
+
pojo.SourcePicoseconds = dataValue.sourcePicoseconds;
|
|
91
|
+
}
|
|
92
|
+
if (mask & DataSetFieldContentMask.ServerTimestamp && dataValue.serverTimestamp) {
|
|
93
|
+
pojo.ServerTimestamp = dataValue.serverTimestamp;
|
|
94
|
+
}
|
|
95
|
+
if (mask & DataSetFieldContentMask.SourceTimestamp && dataValue.sourceTimestamp) {
|
|
96
|
+
pojo.SourceTimestamp = dataValue.sourceTimestamp;
|
|
97
|
+
}
|
|
98
|
+
if (mask & DataSetFieldContentMask.StatusCode && dataValue.statusCode.value !== 0) {
|
|
99
|
+
// statusCode is omitted if it is Good (0)
|
|
100
|
+
// https://reference.opcfoundation.org/Core/Part6/v105/docs/5.4
|
|
101
|
+
switch (mode) {
|
|
102
|
+
case JsonEncodingScheme.DeprecatedNonReversible:
|
|
103
|
+
pojo.StatusCode = opcuaJsonEncodeStatusCode104(dataValue.statusCode, JsonEncoderMode104.NonReversible);
|
|
104
|
+
break;
|
|
105
|
+
case JsonEncodingScheme.DeprecatedReversible:
|
|
106
|
+
pojo.StatusCode = opcuaJsonEncodeStatusCode104(dataValue.statusCode, JsonEncoderMode104.Reversible);
|
|
107
|
+
break;
|
|
108
|
+
case JsonEncodingScheme.Verbose:
|
|
109
|
+
pojo.StatusCode = opcuaJsonEncodeStatusCode105(dataValue.statusCode, JsonEncoderMode105.Verbose);
|
|
110
|
+
break;
|
|
111
|
+
case JsonEncodingScheme.Compact:
|
|
112
|
+
pojo.StatusCode = opcuaJsonEncodeStatusCode105(dataValue.statusCode, JsonEncoderMode105.Compact);
|
|
113
|
+
break;
|
|
114
|
+
default:
|
|
115
|
+
throw new Error("Invalid JsonEncodingScheme");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function _decodeTimestampsAndStatusCodeJson(pojo: DataValueStatusAndTimestamps, dataValueOpt: DataValueOptions) {
|
|
120
|
+
if (pojo.ServerPicoseconds) {
|
|
121
|
+
dataValueOpt.serverPicoseconds = pojo.ServerPicoseconds;
|
|
122
|
+
}
|
|
123
|
+
if (pojo.SourcePicoseconds) {
|
|
124
|
+
dataValueOpt.sourcePicoseconds = pojo.SourcePicoseconds;
|
|
125
|
+
}
|
|
126
|
+
if (pojo.ServerTimestamp) {
|
|
127
|
+
dataValueOpt.serverTimestamp = new Date(pojo.ServerTimestamp);
|
|
128
|
+
}
|
|
129
|
+
if (pojo.SourceTimestamp) {
|
|
130
|
+
dataValueOpt.sourceTimestamp = new Date(pojo.SourceTimestamp);
|
|
131
|
+
}
|
|
132
|
+
if (pojo.StatusCode) {
|
|
133
|
+
dataValueOpt.statusCode = opcuaJsonDecodeStatusCode(pojo.StatusCode);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
*
|
|
139
|
+
* @param dataValue
|
|
140
|
+
* @param mask
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
export function opcuaJsonEncodeDataValueMQTT(
|
|
144
|
+
dataValue: DataValue,
|
|
145
|
+
encoding: JsonEncodingScheme,
|
|
146
|
+
mask: DataSetFieldContentMask,
|
|
147
|
+
namespaceArray: string[] = []
|
|
148
|
+
): VariantJSONBody | VariantJSON105 | VariantJSON104 | DataValueJSON105 | DataValueJSON104 | null {
|
|
149
|
+
switch (encoding) {
|
|
150
|
+
case JsonEncodingScheme.DeprecatedNonReversible:
|
|
151
|
+
return _opcuaJsonEncodeDataValueMQTT104(dataValue, JsonEncoderMode104.NonReversible, mask, namespaceArray);
|
|
152
|
+
case JsonEncodingScheme.DeprecatedReversible:
|
|
153
|
+
return _opcuaJsonEncodeDataValueMQTT104(dataValue, JsonEncoderMode104.Reversible, mask, namespaceArray);
|
|
154
|
+
case JsonEncodingScheme.Verbose:
|
|
155
|
+
return _opcuaJsonEncodeDataValueMQTT105(dataValue, JsonEncoderMode105.Verbose, mask, namespaceArray);
|
|
156
|
+
case JsonEncodingScheme.Compact: {
|
|
157
|
+
return _opcuaJsonEncodeDataValueMQTT105(dataValue, JsonEncoderMode105.Compact, mask, namespaceArray);
|
|
158
|
+
}
|
|
159
|
+
default:
|
|
160
|
+
throw new Error("Invalid JsonEncodingScheme");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function _opcuaJsonEncodeDataValueMQTT104(
|
|
164
|
+
dataValue: DataValue,
|
|
165
|
+
mode: JsonEncoderMode104,
|
|
166
|
+
mask: DataSetFieldContentMask,
|
|
167
|
+
namespaceArray: string[] = []
|
|
168
|
+
): VariantJSONBody | VariantJSON104 | DataValueJSON104 | null {
|
|
169
|
+
/**
|
|
170
|
+
* All fields with a concrete DataType are encoded using VerboseEncoding if FieldEncoding1 is
|
|
171
|
+
* FALSE and CompactEncoding if FieldEncoding1 is TRUE.
|
|
172
|
+
*
|
|
173
|
+
* See the OPC UA JSON Data encodings defined in OPC 10000-6.
|
|
174
|
+
*
|
|
175
|
+
* The fields in the DataSetMessage are specified by the DataSetFieldContentMask in the
|
|
176
|
+
* DataSetWriter parameters.
|
|
177
|
+
*
|
|
178
|
+
* The format of the field values in the Payload depend on the setting of
|
|
179
|
+
* DataSetFieldContentMask, the FieldEncoding1 and the FieldEncoding2 flag in
|
|
180
|
+
* DataSetMessageContentMask. The resulting JSON encoding is defined in Table 111.
|
|
181
|
+
*
|
|
182
|
+
* FieldEncoding1 FieldEncoding2 Description
|
|
183
|
+
* False True The JSON VerboseEncoding is used for the DataSetMessage field encoding.
|
|
184
|
+
* True True The JSON CompactEncoding is used for the DataSetMessage field encoding.
|
|
185
|
+
* False False The deprecated JSON NonReversibleEncoding is used for the
|
|
186
|
+
* DataSetMessage field encoding.
|
|
187
|
+
* The RawData bit of the DataSetFieldContentMask shall be ignored.
|
|
188
|
+
* True False The deprecated JSON ReversibleFieldEncoding is used for the
|
|
189
|
+
* DataSetMessage field encoding.
|
|
190
|
+
* The RawData bit of the DataSetFieldContentMask shall be ignored.
|
|
191
|
+
*
|
|
192
|
+
* If the DataSetFieldContentMask is 0x0 or 0x20 (only the RawData flag is set), the
|
|
193
|
+
* DataSetMessage fields are encoded as Variant. Otherwise the fields are encoded as
|
|
194
|
+
* DataValue.
|
|
195
|
+
* If the KeyFrameCount is 0, the DataSetFieldContentMask shall be 0x0 or 0x20.
|
|
196
|
+
*
|
|
197
|
+
* If the FieldEncoding1 is FALSE in the DataSetMessageContentMask, the Variant at the top
|
|
198
|
+
* level of a field is encoded as a JSON value containing only the value of the Body field.
|
|
199
|
+
*
|
|
200
|
+
* If this Variant contains an ExtensionObject, the ExtensionObject shall be encoded as a Structure
|
|
201
|
+
* without the UaTypeId field.
|
|
202
|
+
* This also applies to the Variant in a DataValue at the top level of a field.
|
|
203
|
+
*
|
|
204
|
+
* If the RawData flag is set, the UaType fields of Variants and the UaTypeId fields of
|
|
205
|
+
* ExtensionObjects are always omitted.
|
|
206
|
+
*
|
|
207
|
+
* If the RawData flag ist set, it is not possible to reverse the data in a DataSetReader in the
|
|
208
|
+
* following cases.
|
|
209
|
+
*
|
|
210
|
+
* * DataSet fields have an abstract DataType in the DataSetMetaData.
|
|
211
|
+
* * DataSet field values do not match the DataType specified in the DataSetMetaData if
|
|
212
|
+
* they are Structure DataTypes
|
|
213
|
+
**/
|
|
214
|
+
if (mask === DataSetFieldContentMask.None) {
|
|
215
|
+
return opcuaJsonEncodeVariant104(dataValue.value, mode, namespaceArray);
|
|
216
|
+
}
|
|
217
|
+
const pojoVariant = opcuaJsonEncodeVariant104(dataValue.value, mode, namespaceArray);
|
|
218
|
+
if (mask & DataSetFieldContentMask.RawData) {
|
|
219
|
+
// RawData: the field value is a Variant encoded using the non-reversible OPC UA JSON Data Encoding defined in OPC 10000-6.
|
|
220
|
+
return pojoVariant;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* DataValue values shall be encoded as a JSON object with the fields below.
|
|
225
|
+
* JSON Object Definition for a DataValue
|
|
226
|
+
* Name DataType Description
|
|
227
|
+
* Value Variant The value.
|
|
228
|
+
* Status StatusCode The status associated with the value.
|
|
229
|
+
* SourceTimestamp DateTime The source timestamp associated with the value.
|
|
230
|
+
* SourcePicoSeconds UInt16 The number of 10 picosecond intervals for the SourceTimestamp.
|
|
231
|
+
* ServerTimestamp DateTime The Server timestamp associated with the value.
|
|
232
|
+
* ServerPicoSeconds UInt16 The number of 10 picosecond intervals for the ServerTimestamp.
|
|
233
|
+
*
|
|
234
|
+
* If a field has a null or default value it is omitted. Each field is encoded using the rules
|
|
235
|
+
* defined for the built-in type specified in the Data Type column.
|
|
236
|
+
*/
|
|
237
|
+
const pojo: DataValueJSON104 = {
|
|
238
|
+
Value: pojoVariant as VariantJSON104
|
|
239
|
+
};
|
|
240
|
+
_setTimestampsAndStatusCode(pojo, dataValue, mask, toJsonEncodingScheme(mode));
|
|
241
|
+
|
|
242
|
+
return pojo;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
*
|
|
247
|
+
* @param dataValue
|
|
248
|
+
* @param mask
|
|
249
|
+
* @returns
|
|
250
|
+
*/
|
|
251
|
+
export function _opcuaJsonEncodeDataValueMQTT105(
|
|
252
|
+
dataValue: DataValue,
|
|
253
|
+
mode: JsonEncoderMode105,
|
|
254
|
+
mask: DataSetFieldContentMask,
|
|
255
|
+
namespaceArray: string[] = []
|
|
256
|
+
): VariantJSONBody | DataValueJSON105 | null {
|
|
257
|
+
if (mask === DataSetFieldContentMask.None) {
|
|
258
|
+
if (mode === JsonEncoderMode105.Verbose) {
|
|
259
|
+
return opcuaJsonEncodeVariant105(dataValue.value, mode, namespaceArray);
|
|
260
|
+
} else {
|
|
261
|
+
return opcuaJsonEncodeVariant105(dataValue.value, mode, namespaceArray);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const pojoVariant = opcuaJsonEncodeVariant105(dataValue.value, mode, namespaceArray) as VariantJSON105 | VariantJSONBody;
|
|
266
|
+
if (mask & DataSetFieldContentMask.RawData) {
|
|
267
|
+
// RawData: the field value is a Variant encoded using the non-reversible OPC UA JSON Data Encoding defined in OPC 10000-6.
|
|
268
|
+
return pojoVariant;
|
|
269
|
+
}
|
|
270
|
+
const pojo: DataValueJSON105 = pojoVariant as DataValueJSON105;
|
|
271
|
+
_setTimestampsAndStatusCode(pojo, dataValue, mask, toJsonEncodingScheme(mode));
|
|
272
|
+
return pojo;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const DataSetFieldContentMaskAll =
|
|
276
|
+
DataSetFieldContentMask.StatusCode |
|
|
277
|
+
DataSetFieldContentMask.SourceTimestamp |
|
|
278
|
+
DataSetFieldContentMask.SourcePicoSeconds |
|
|
279
|
+
DataSetFieldContentMask.ServerTimestamp |
|
|
280
|
+
DataSetFieldContentMask.ServerPicoSeconds;
|
|
281
|
+
|
|
282
|
+
export function opcuaJsonEncodeDataValue104(
|
|
283
|
+
dataValue: DataValue,
|
|
284
|
+
mode: JsonEncoderMode104,
|
|
285
|
+
namespaceArray: string[]
|
|
286
|
+
): DataValueJSON104 {
|
|
287
|
+
const pojoVariant = opcuaJsonEncodeVariant104(dataValue.value, mode, namespaceArray);
|
|
288
|
+
if (mode === JsonEncoderMode104.Reversible) {
|
|
289
|
+
const pojo: DataValueJSON104 = {
|
|
290
|
+
Value: pojoVariant as VariantJSON104
|
|
291
|
+
};
|
|
292
|
+
_setTimestampsAndStatusCode(pojo, dataValue, DataSetFieldContentMaskAll, toJsonEncodingScheme(mode));
|
|
293
|
+
return pojo;
|
|
294
|
+
}
|
|
295
|
+
const pojo: DataValueJSON104 = {
|
|
296
|
+
Value: pojoVariant as VariantJSON104
|
|
297
|
+
};
|
|
298
|
+
_setTimestampsAndStatusCode(pojo, dataValue, DataSetFieldContentMaskAll, toJsonEncodingScheme(mode));
|
|
299
|
+
return pojo;
|
|
300
|
+
}
|
|
301
|
+
export function opcuaJsonEncodeDataValue105(
|
|
302
|
+
dataValue: DataValue,
|
|
303
|
+
mode: JsonEncoderMode105,
|
|
304
|
+
namespaceArray: string[]
|
|
305
|
+
): DataValueJSON105 {
|
|
306
|
+
if (mode === JsonEncoderMode105.Verbose) {
|
|
307
|
+
const pojoVariant = opcuaJsonEncodeVariant105(dataValue.value, mode, namespaceArray);
|
|
308
|
+
const pojo: DataValueJSON105 = pojoVariant as DataValueJSON105;
|
|
309
|
+
_setTimestampsAndStatusCode(pojo, dataValue, DataSetFieldContentMaskAll, toJsonEncodingScheme(mode));
|
|
310
|
+
return pojo;
|
|
311
|
+
} else {
|
|
312
|
+
const pojoVariant = opcuaJsonEncodeVariant105(dataValue.value, mode, namespaceArray);
|
|
313
|
+
const pojo: DataValueJSON105 = pojoVariant as DataValueJSON105;
|
|
314
|
+
_setTimestampsAndStatusCode(pojo, dataValue, DataSetFieldContentMaskAll, toJsonEncodingScheme(mode));
|
|
315
|
+
return pojo;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function opcuaJsonEncodeDataValue(
|
|
320
|
+
dataValue: DataValue,
|
|
321
|
+
mode: JsonEncodingScheme.Compact | JsonEncodingScheme.Verbose,
|
|
322
|
+
namespaceArray: string[]
|
|
323
|
+
): DataValueJSON105;
|
|
324
|
+
export function opcuaJsonEncodeDataValue(
|
|
325
|
+
dataValue: DataValue,
|
|
326
|
+
mode: JsonEncodingScheme.DeprecatedNonReversible | JsonEncodingScheme.DeprecatedReversible,
|
|
327
|
+
namespaceArray: string[]
|
|
328
|
+
): DataValueJSON104;
|
|
329
|
+
export function opcuaJsonEncodeDataValue(
|
|
330
|
+
dataValue: DataValue,
|
|
331
|
+
mode: JsonEncodingScheme,
|
|
332
|
+
namespaceArray: string[]
|
|
333
|
+
): DataValueJSON105 | DataValueJSON104 {
|
|
334
|
+
switch (mode) {
|
|
335
|
+
case JsonEncodingScheme.DeprecatedNonReversible:
|
|
336
|
+
return opcuaJsonEncodeDataValue104(dataValue, JsonEncoderMode104.NonReversible, namespaceArray);
|
|
337
|
+
case JsonEncodingScheme.DeprecatedReversible:
|
|
338
|
+
return opcuaJsonEncodeDataValue104(dataValue, JsonEncoderMode104.Reversible, namespaceArray);
|
|
339
|
+
case JsonEncodingScheme.Verbose:
|
|
340
|
+
return opcuaJsonEncodeDataValue105(dataValue, JsonEncoderMode105.Verbose, namespaceArray);
|
|
341
|
+
case JsonEncodingScheme.Compact:
|
|
342
|
+
return opcuaJsonEncodeDataValue105(dataValue, JsonEncoderMode105.Compact, namespaceArray);
|
|
343
|
+
default:
|
|
344
|
+
throw new Error("Invalid JsonEncodingScheme");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function opcuaJsonDecodeDataValue104(
|
|
349
|
+
pojo: DataValueJSON104 | null | undefined,
|
|
350
|
+
builder: ExtensionObjectBuilder,
|
|
351
|
+
namespaceArray: string[]
|
|
352
|
+
): DataValue {
|
|
353
|
+
if (!pojo) {
|
|
354
|
+
return new DataValue({ value: { dataType: DataType.Null } });
|
|
355
|
+
}
|
|
356
|
+
const dataValueOpt: DataValueOptions = {};
|
|
357
|
+
_decodeTimestampsAndStatusCodeJson(pojo, dataValueOpt);
|
|
358
|
+
if (pojo.Value) {
|
|
359
|
+
dataValueOpt.value = opcuaJsonDecodeVariant104(pojo.Value, builder, namespaceArray);
|
|
360
|
+
}
|
|
361
|
+
return new DataValue(dataValueOpt);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export function opcuaJsonDecodeDataValue105(
|
|
365
|
+
pojo: DataValueJSON105 | null | undefined,
|
|
366
|
+
builder: ExtensionObjectBuilder,
|
|
367
|
+
namespaceArray: string[]
|
|
368
|
+
): DataValue {
|
|
369
|
+
if (!pojo) {
|
|
370
|
+
return new DataValue({ value: { dataType: DataType.Null } });
|
|
371
|
+
}
|
|
372
|
+
const dataValueOpt: DataValueOptions = {};
|
|
373
|
+
_decodeTimestampsAndStatusCodeJson(pojo, dataValueOpt);
|
|
374
|
+
if (pojo.Value) {
|
|
375
|
+
dataValueOpt.value = opcuaJsonDecodeVariant105(pojo, builder, namespaceArray);
|
|
376
|
+
}
|
|
377
|
+
return new DataValue(dataValueOpt);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export function opcuaJsonDecodeDataValue(
|
|
381
|
+
pojo: DataValueJSON104 | DataValueJSON105 | null | undefined,
|
|
382
|
+
builder: ExtensionObjectBuilder,
|
|
383
|
+
namespaceArray: string[]
|
|
384
|
+
): DataValue {
|
|
385
|
+
if (!pojo) {
|
|
386
|
+
return new DataValue({ value: { dataType: DataType.Null } });
|
|
387
|
+
}
|
|
388
|
+
if ("UaType" in pojo) {
|
|
389
|
+
return opcuaJsonDecodeDataValue105(pojo as DataValueJSON105, builder, namespaceArray);
|
|
390
|
+
}
|
|
391
|
+
return opcuaJsonDecodeDataValue104(pojo as DataValueJSON104, builder, namespaceArray);
|
|
392
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ==========================================================================
|
|
3
|
+
* Copyright (c) 2021-2026 Sterfive - etienne.rossignon@sterfive.com
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
|
13
|
+
* all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
18
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
* IN THE SOFTWARE.
|
|
22
|
+
* ==========================================================================
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { minDate } from "node-opcua-date-time";
|
|
26
|
+
import type { JsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
27
|
+
|
|
28
|
+
const maxDate = new Date("9999-12-31T23:59:59.000Z");
|
|
29
|
+
const reallyMinDate = new Date("0001-01-01T00:00:00.000Z");
|
|
30
|
+
export function opcuaJsonEncodeDateTime(date: Date, _scheme: JsonEncodingScheme, _namespaceArray?: string[]): Date {
|
|
31
|
+
if (date.getTime() <= minDate.getTime()) {
|
|
32
|
+
return reallyMinDate;
|
|
33
|
+
}
|
|
34
|
+
if (date.getTime() >= maxDate.getTime()) {
|
|
35
|
+
return maxDate;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return date;
|
|
39
|
+
}
|
|
40
|
+
export function opcuaJsonDecodeDateTime(pojo: Date | string): Date {
|
|
41
|
+
return typeof pojo === "string" ? new Date(pojo) : (pojo as Date);
|
|
42
|
+
}
|