node-opcua-convert-nodeset-to-javascript 2.76.2 → 2.77.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/dist/_dataType.d.ts +10 -0
- package/dist/_dataType.js +153 -0
- package/dist/_dataType.js.map +1 -0
- package/dist/convert_to_typescript.d.ts +0 -5
- package/dist/convert_to_typescript.js +21 -255
- package/dist/convert_to_typescript.js.map +1 -1
- package/dist/main.js +5 -2
- package/dist/main.js.map +1 -1
- package/dist/official_namespaces.js.map +1 -1
- package/dist/private/cache.d.ts +2 -0
- package/dist/private/cache.js +18 -3
- package/dist/private/cache.js.map +1 -1
- package/dist/private/get_corresponding_data_type.d.ts +14 -0
- package/dist/private/get_corresponding_data_type.js +104 -0
- package/dist/private/get_corresponding_data_type.js.map +1 -0
- package/dist/utils2.d.ts +7 -0
- package/dist/utils2.js +58 -0
- package/dist/utils2.js.map +1 -0
- package/package.json +19 -18
- package/source/_dataType.ts +164 -0
- package/source/convert_to_typescript.ts +4 -252
- package/source/main.ts +5 -2
- package/source/official_namespaces.ts +0 -1
- package/source/private/cache.ts +24 -7
- package/source/private/get_corresponding_data_type.ts +104 -0
- package/source/utils2.ts +55 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { NodeClass } from "node-opcua-data-model";
|
|
2
|
+
import { NodeId } from "node-opcua-nodeid";
|
|
3
|
+
import { IBasicSession } from "node-opcua-pseudo-session";
|
|
4
|
+
import { EnumDefinition, StructureDefinition, StructureField } from "node-opcua-types";
|
|
5
|
+
import { LineFile } from "node-opcua-utils";
|
|
6
|
+
import { DataType } from "node-opcua-variant";
|
|
7
|
+
import { Type } from "./convert_to_typescript";
|
|
8
|
+
import {
|
|
9
|
+
Import,
|
|
10
|
+
getDescription,
|
|
11
|
+
getDefinition,
|
|
12
|
+
getBrowseName,
|
|
13
|
+
getIsAbstract,
|
|
14
|
+
makeTypeNameNew,
|
|
15
|
+
getSubtypeNodeId
|
|
16
|
+
} from "./private-stuff";
|
|
17
|
+
import { Cache, makeName2 } from "./private/cache";
|
|
18
|
+
import { getCorrepondingJavascriptType } from "./private/get_corresponding_data_type";
|
|
19
|
+
import { f1, f2, quotifyIfNecessary, toComment, toJavascritPropertyName } from "./utils2";
|
|
20
|
+
|
|
21
|
+
// eslint-disable-next-line max-statements, complexity
|
|
22
|
+
export async function _exportDataTypeToTypescript(
|
|
23
|
+
session: IBasicSession,
|
|
24
|
+
nodeId: NodeId,
|
|
25
|
+
cache: Cache,
|
|
26
|
+
f?: LineFile
|
|
27
|
+
): Promise<{ type: Type; content: string; typeName: string }> {
|
|
28
|
+
const importCollect: any = undefined;
|
|
29
|
+
const referenceBasicType = (name: string): string => {
|
|
30
|
+
const t = { name, namespace: -1, module: "BasicType" };
|
|
31
|
+
importCollect && importCollect(t);
|
|
32
|
+
cache.ensureImported(t);
|
|
33
|
+
return t.name;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
f = f || new LineFile();
|
|
37
|
+
|
|
38
|
+
const importCollector = (i: Import) => {
|
|
39
|
+
cache.ensureImported(i);
|
|
40
|
+
};
|
|
41
|
+
const nodeClass = NodeClass.DataType;
|
|
42
|
+
const description = await getDescription(session, nodeId);
|
|
43
|
+
const definition = await getDefinition(session, nodeId);
|
|
44
|
+
const browseName = await getBrowseName(session, nodeId);
|
|
45
|
+
const isAbstract = await getIsAbstract(session, nodeId);
|
|
46
|
+
|
|
47
|
+
const interfaceImport: Import = makeTypeNameNew(nodeClass, definition, browseName);
|
|
48
|
+
const interfaceName = interfaceImport.name;
|
|
49
|
+
|
|
50
|
+
const superType = await getSubtypeNodeId(session, nodeId);
|
|
51
|
+
const baseInterfaceImport: Import = makeTypeNameNew(nodeClass, definition, superType.browseName);
|
|
52
|
+
|
|
53
|
+
cache.ensureImported(baseInterfaceImport);
|
|
54
|
+
|
|
55
|
+
const baseInterfaceName = baseInterfaceImport.name;
|
|
56
|
+
// f.write(superType.toString());
|
|
57
|
+
f.write(`/**`);
|
|
58
|
+
if (description.text) {
|
|
59
|
+
f.write(toComment(" * ", description.text || ""));
|
|
60
|
+
f.write(` *`);
|
|
61
|
+
}
|
|
62
|
+
f.write(` * | |${f1(" ")}|`);
|
|
63
|
+
f.write(` * |-----------|${f2("-")}|`);
|
|
64
|
+
f.write(` * | namespace |${f1(cache.namespace[nodeId.namespace].namespaceUri)}|`);
|
|
65
|
+
f.write(` * | nodeClass |${f1(NodeClass[nodeClass])}|`);
|
|
66
|
+
f.write(` * | name |${f1(browseName.toString())}|`);
|
|
67
|
+
f.write(` * | isAbstract|${f1(isAbstract.toString())}|`);
|
|
68
|
+
f.write(` */`);
|
|
69
|
+
|
|
70
|
+
let type: "basic" | "structure" | "enum" | "ua" = "basic";
|
|
71
|
+
if (definition instanceof EnumDefinition) {
|
|
72
|
+
type = "enum";
|
|
73
|
+
f.write(`export enum ${interfaceName} {`);
|
|
74
|
+
for (const field of definition.fields!) {
|
|
75
|
+
if (field.description.text) {
|
|
76
|
+
f.write(` /**`);
|
|
77
|
+
f.write(toComment(" * ", field.description.text || ""));
|
|
78
|
+
f.write(` */`);
|
|
79
|
+
}
|
|
80
|
+
f.write(` ${quotifyIfNecessary(field.name!)} = ${field.value[1]},`);
|
|
81
|
+
}
|
|
82
|
+
f.write(`}`);
|
|
83
|
+
} else if (definition instanceof StructureDefinition) {
|
|
84
|
+
if (baseInterfaceName === "DTUnion") {
|
|
85
|
+
type = "structure";
|
|
86
|
+
for (let i = 0; i < definition.fields!.length; i++) {
|
|
87
|
+
f.write(`export interface ${interfaceName}_${i} extends ${baseInterfaceName} {`);
|
|
88
|
+
for (let j = 0; j < definition.fields!.length; j++) {
|
|
89
|
+
const field = definition.fields![j];
|
|
90
|
+
const fieldName = toJavascritPropertyName(field.name!, { ignoreConflictingName: false });
|
|
91
|
+
if (j === i) {
|
|
92
|
+
await outputStructureField(f, field);
|
|
93
|
+
} else {
|
|
94
|
+
f.write(` ${quotifyIfNecessary(fieldName)}?: never`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
f.write(`}`);
|
|
98
|
+
}
|
|
99
|
+
f.write(`export type ${interfaceName} = `);
|
|
100
|
+
for (let i = 0; i < definition.fields!.length; i++) {
|
|
101
|
+
f.write(` | ${interfaceName}_${i}`);
|
|
102
|
+
}
|
|
103
|
+
f.write(` ;`);
|
|
104
|
+
} else {
|
|
105
|
+
type = "structure";
|
|
106
|
+
if (!definition.fields!.length && interfaceName !== "DTStructure") {
|
|
107
|
+
f.write(`export type ${interfaceName} = ${baseInterfaceName};`);
|
|
108
|
+
} else {
|
|
109
|
+
if (interfaceName === "DTStructure") {
|
|
110
|
+
f.write(`export interface ${interfaceName} {`);
|
|
111
|
+
} else {
|
|
112
|
+
f.write(`export interface ${interfaceName} extends ${baseInterfaceName} {`);
|
|
113
|
+
}
|
|
114
|
+
for (const field of definition.fields!) {
|
|
115
|
+
await outputStructureField(f, field);
|
|
116
|
+
}
|
|
117
|
+
f.write(`}`);
|
|
118
|
+
//
|
|
119
|
+
|
|
120
|
+
referenceBasicType("ExtensionObject");
|
|
121
|
+
// ache.ensureImported({ module: "BasicType", name: "ExtensionObject", namespace: 0 });
|
|
122
|
+
// interface TighteningResultOptions extends Partial<DTTighteningResult> {}
|
|
123
|
+
// interface TighteningResult extends ExtensionObject, DTTighteningResult {}
|
|
124
|
+
const full = makeName2(interfaceName);
|
|
125
|
+
|
|
126
|
+
// filter issue with UDTOpticalVerifierScanResult that has a decode:Uint32 conflicting with the ExtensionObject decode method
|
|
127
|
+
|
|
128
|
+
const toAvoid = ["decode", "encode"];
|
|
129
|
+
const collidingNames = definition
|
|
130
|
+
.fields!.map((a) => toJavascritPropertyName(a.name! ,{ ignoreConflictingName: false }))
|
|
131
|
+
.filter((d) => toAvoid.indexOf(d.toLowerCase()) !== -1);
|
|
132
|
+
|
|
133
|
+
const adpatedIntefaceName =
|
|
134
|
+
collidingNames.length === 0
|
|
135
|
+
? interfaceName
|
|
136
|
+
: `Omit<${interfaceName},${collidingNames.map((a) => `"${a}"`).join(",")}>`;
|
|
137
|
+
|
|
138
|
+
f.write(`export interface ${full} extends ExtensionObject, ${adpatedIntefaceName} {};`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
type = "basic";
|
|
143
|
+
f.write(`// NO DEFINITION`);
|
|
144
|
+
f.write(`export type ${interfaceName} = ${baseInterfaceName};`);
|
|
145
|
+
// throw new Error("Invalid " + definition?.constructor.name);
|
|
146
|
+
}
|
|
147
|
+
return { type, content: f.toString(), typeName: interfaceName };
|
|
148
|
+
|
|
149
|
+
async function outputStructureField(f: LineFile, field: StructureField) {
|
|
150
|
+
const fieldName = toJavascritPropertyName(field.name!, { ignoreConflictingName: false });
|
|
151
|
+
// special case ! fieldName=
|
|
152
|
+
if (field.description.text) {
|
|
153
|
+
f.write(` /** ${field.description.text}*/`);
|
|
154
|
+
}
|
|
155
|
+
const opt = field.isOptional ? "?" : "";
|
|
156
|
+
const arrayMarker = field.valueRank >= 1 ? "[]" : "";
|
|
157
|
+
const { dataType, jtype } = await getCorrepondingJavascriptType(session, field.dataType, cache, importCollector);
|
|
158
|
+
f.write(
|
|
159
|
+
` ${quotifyIfNecessary(fieldName)}${opt}: ${jtype}${arrayMarker}; // ${
|
|
160
|
+
DataType[dataType]
|
|
161
|
+
} ${field.dataType.toString()}`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* eslint-disable max-depth */
|
|
2
2
|
import * as chalk from "chalk";
|
|
3
|
-
import * as wrap from "wordwrap";
|
|
4
3
|
import { LocalizedText, NodeClass, QualifiedName } from "node-opcua-data-model";
|
|
5
4
|
import { NodeId } from "node-opcua-nodeid";
|
|
6
5
|
import { IBasicSession } from "node-opcua-pseudo-session";
|
|
@@ -45,11 +44,11 @@ import {
|
|
|
45
44
|
} from "./private/cache";
|
|
46
45
|
import { Options } from "./options";
|
|
47
46
|
import { toFilename } from "./private/to_filename";
|
|
47
|
+
import { f1, f2, quotifyIfNecessary, toComment, toJavascritPropertyName } from "./utils2";
|
|
48
|
+
import { _exportDataTypeToTypescript } from "./_dataType";
|
|
49
|
+
import { getCorrepondingJavascriptType2 } from "./private/get_corresponding_data_type";
|
|
48
50
|
const warningLog = make_warningLog("typescript");
|
|
49
51
|
const doDebug = false;
|
|
50
|
-
const wrapText = wrap(0, 50);
|
|
51
|
-
const f2 = (str: string) => str.padEnd(50, "-");
|
|
52
|
-
const f1 = (str: string) => str.padEnd(50, " ");
|
|
53
52
|
const baseExtension = "_Base";
|
|
54
53
|
|
|
55
54
|
const m0 = !doDebug ? "" : `/* 0 */`;
|
|
@@ -81,137 +80,6 @@ export async function convertDataTypeToTypescript(session: IBasicSession, dataTy
|
|
|
81
80
|
}
|
|
82
81
|
}
|
|
83
82
|
|
|
84
|
-
// to avoid clashes
|
|
85
|
-
function toJavascritPropertyName(childName: string, { ignoreConflictingName }: { ignoreConflictingName: boolean }): string {
|
|
86
|
-
childName = lowerFirstLetter(childName);
|
|
87
|
-
|
|
88
|
-
if (ignoreConflictingName) {
|
|
89
|
-
if (childName === "namespaceUri") {
|
|
90
|
-
childName = "$namespaceUri";
|
|
91
|
-
}
|
|
92
|
-
if (childName === "rolePermissions") {
|
|
93
|
-
childName = "$rolePermissions";
|
|
94
|
-
}
|
|
95
|
-
if (childName === "displayName") {
|
|
96
|
-
childName = "$displayName";
|
|
97
|
-
}
|
|
98
|
-
if (childName === "eventNotifier") {
|
|
99
|
-
childName = "$eventNotifier";
|
|
100
|
-
}
|
|
101
|
-
if (childName === "description") {
|
|
102
|
-
childName = "$description";
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return childName.replace(/</g, "$").replace(/>/g, "$").replace(/ |\./g, "_").replace(/#/g, "_");
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function quotifyIfNecessary(s: string): string {
|
|
109
|
-
if (s.match(/(^[^a-zA-Z])|([^a-zA-Z_0-9])/)) {
|
|
110
|
-
return `"${s}"`;
|
|
111
|
-
}
|
|
112
|
-
if (s === "nodeClass") {
|
|
113
|
-
return `["$nodeClass"]`;
|
|
114
|
-
}
|
|
115
|
-
return s;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async function getCorrepondingJavascriptType2(
|
|
119
|
-
session: IBasicSession,
|
|
120
|
-
nodeId: NodeId,
|
|
121
|
-
dataTypeNodeId: NodeId,
|
|
122
|
-
cache: Cache,
|
|
123
|
-
importCollect?: (t: Import) => void
|
|
124
|
-
): Promise<{ dataType: DataType; jtype: string }> {
|
|
125
|
-
const q = await getCorrepondingJavascriptType(session, dataTypeNodeId, cache, importCollect);
|
|
126
|
-
const valueRank = await getValueRank(session, nodeId);
|
|
127
|
-
return { dataType: q.dataType, jtype: q.jtype + (valueRank >= 1 ? "[]" : "") };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// eslint-disable-next-line complexity
|
|
131
|
-
async function getCorrepondingJavascriptType(
|
|
132
|
-
session: IBasicSession,
|
|
133
|
-
dataTypeNodeId: NodeId,
|
|
134
|
-
cache: Cache,
|
|
135
|
-
importCollect?: (t: Import) => void
|
|
136
|
-
): Promise<{ enumerationType?: string; dataType: DataType; jtype: string }> {
|
|
137
|
-
const { dataType, enumerationType } = await _convertNodeIdToDataTypeAsync(session, dataTypeNodeId);
|
|
138
|
-
|
|
139
|
-
if (enumerationType) {
|
|
140
|
-
// we have a enmeration name here
|
|
141
|
-
const jtypeImport = await referenceEnumeration(session, dataTypeNodeId);
|
|
142
|
-
const jtype = jtypeImport.name;
|
|
143
|
-
importCollect && importCollect(jtypeImport);
|
|
144
|
-
return { dataType, jtype };
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (dataType === DataType.ExtensionObject) {
|
|
148
|
-
const jtypeImport = await referenceExtensionObject(session, dataTypeNodeId);
|
|
149
|
-
const jtype = jtypeImport.name;
|
|
150
|
-
importCollect && importCollect(jtypeImport);
|
|
151
|
-
return { dataType, jtype };
|
|
152
|
-
}
|
|
153
|
-
const referenceBasicType = (name: string): string => {
|
|
154
|
-
const t = { name, namespace: -1, module: "BasicType" };
|
|
155
|
-
importCollect && importCollect(t);
|
|
156
|
-
cache.ensureImported(t);
|
|
157
|
-
return t.name;
|
|
158
|
-
};
|
|
159
|
-
switch (dataType) {
|
|
160
|
-
case DataType.Null:
|
|
161
|
-
return { dataType, jtype: "undefined" };
|
|
162
|
-
case DataType.Boolean:
|
|
163
|
-
return { dataType, jtype: "boolean" };
|
|
164
|
-
case DataType.Byte:
|
|
165
|
-
return { dataType, jtype: referenceBasicType("Byte") };
|
|
166
|
-
case DataType.ByteString:
|
|
167
|
-
return { dataType, jtype: "Buffer" };
|
|
168
|
-
case DataType.DataValue:
|
|
169
|
-
return { dataType, jtype: referenceBasicType("DataValue") };
|
|
170
|
-
case DataType.DateTime:
|
|
171
|
-
return { dataType, jtype: "Date" };
|
|
172
|
-
case DataType.DiagnosticInfo:
|
|
173
|
-
return { dataType, jtype: referenceBasicType("DiagnosticInfo") };
|
|
174
|
-
case DataType.Double:
|
|
175
|
-
return { dataType, jtype: "number" };
|
|
176
|
-
case DataType.Float:
|
|
177
|
-
return { dataType, jtype: "number" };
|
|
178
|
-
case DataType.Guid:
|
|
179
|
-
return { dataType, jtype: referenceBasicType("Guid") };
|
|
180
|
-
case DataType.Int16:
|
|
181
|
-
return { dataType, jtype: referenceBasicType("Int16") };
|
|
182
|
-
case DataType.Int32:
|
|
183
|
-
return { dataType, jtype: referenceBasicType("Int32") };
|
|
184
|
-
case DataType.UInt16:
|
|
185
|
-
return { dataType, jtype: referenceBasicType("UInt16") };
|
|
186
|
-
case DataType.UInt32:
|
|
187
|
-
return { dataType, jtype: referenceBasicType("UInt32") };
|
|
188
|
-
case DataType.UInt64:
|
|
189
|
-
return { dataType, jtype: referenceBasicType("UInt64") };
|
|
190
|
-
case DataType.Int64:
|
|
191
|
-
return { dataType, jtype: referenceBasicType("Int64") };
|
|
192
|
-
case DataType.LocalizedText:
|
|
193
|
-
return { dataType, jtype: referenceBasicType("LocalizedText") };
|
|
194
|
-
case DataType.NodeId:
|
|
195
|
-
return { dataType, jtype: referenceBasicType("NodeId") };
|
|
196
|
-
case DataType.ExpandedNodeId:
|
|
197
|
-
return { dataType, jtype: referenceBasicType("ExpandedNodeId") };
|
|
198
|
-
case DataType.QualifiedName:
|
|
199
|
-
return { dataType, jtype: referenceBasicType("QualifiedName") };
|
|
200
|
-
case DataType.SByte:
|
|
201
|
-
return { dataType, jtype: referenceBasicType("SByte") };
|
|
202
|
-
case DataType.StatusCode:
|
|
203
|
-
return { dataType, jtype: referenceBasicType("StatusCode") };
|
|
204
|
-
case DataType.String:
|
|
205
|
-
return { dataType, jtype: referenceBasicType("UAString") };
|
|
206
|
-
case DataType.Variant:
|
|
207
|
-
return { dataType, jtype: referenceBasicType("Variant") };
|
|
208
|
-
case DataType.XmlElement:
|
|
209
|
-
return { dataType, jtype: referenceBasicType("String") };
|
|
210
|
-
default:
|
|
211
|
-
throw new Error("Unsupported " + dataType + " " + DataType[dataType]);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
83
|
interface ClassDefinition {
|
|
216
84
|
nodeClass: NodeClass.VariableType | NodeClass.ObjectType;
|
|
217
85
|
browseName: QualifiedName;
|
|
@@ -881,124 +749,8 @@ function dumpUsedExport(currentType: string, namespaceIndex: number, cache: Cach
|
|
|
881
749
|
return f.toString();
|
|
882
750
|
}
|
|
883
751
|
|
|
884
|
-
function toComment(prefix: string, description: string) {
|
|
885
|
-
const d = wrapText(description);
|
|
886
|
-
return d
|
|
887
|
-
.split("\n")
|
|
888
|
-
.map((x) => prefix + x)
|
|
889
|
-
.join("\n");
|
|
890
|
-
}
|
|
891
|
-
export type Type = "enum" | "basic" | "structure" | "ua";
|
|
892
|
-
// eslint-disable-next-line max-statements, complexity
|
|
893
|
-
export async function _exportDataTypeToTypescript(
|
|
894
|
-
session: IBasicSession,
|
|
895
|
-
nodeId: NodeId,
|
|
896
|
-
cache: Cache,
|
|
897
|
-
f?: LineFile
|
|
898
|
-
): Promise<{ type: Type; content: string; typeName: string }> {
|
|
899
|
-
f = f || new LineFile();
|
|
900
|
-
|
|
901
|
-
const importCollector = (i: Import) => {
|
|
902
|
-
cache.ensureImported(i);
|
|
903
|
-
};
|
|
904
|
-
const nodeClass = NodeClass.DataType;
|
|
905
|
-
const description = await getDescription(session, nodeId);
|
|
906
|
-
const definition = await getDefinition(session, nodeId);
|
|
907
|
-
const browseName = await getBrowseName(session, nodeId);
|
|
908
|
-
const isAbstract = await getIsAbstract(session, nodeId);
|
|
909
|
-
|
|
910
|
-
const interfaceImport: Import = makeTypeNameNew(nodeClass, definition, browseName);
|
|
911
|
-
const interfaceName = interfaceImport.name;
|
|
912
|
-
|
|
913
|
-
const superType = await getSubtypeNodeId(session, nodeId);
|
|
914
|
-
const baseInterfaceImport: Import = makeTypeNameNew(nodeClass, definition, superType.browseName);
|
|
915
|
-
|
|
916
|
-
cache.ensureImported(baseInterfaceImport);
|
|
917
|
-
|
|
918
|
-
const baseInterfaceName = baseInterfaceImport.name;
|
|
919
|
-
// f.write(superType.toString());
|
|
920
|
-
f.write(`/**`);
|
|
921
|
-
if (description.text) {
|
|
922
|
-
f.write(toComment(" * ", description.text || ""));
|
|
923
|
-
f.write(` *`);
|
|
924
|
-
}
|
|
925
|
-
f.write(` * | |${f1(" ")}|`);
|
|
926
|
-
f.write(` * |-----------|${f2("-")}|`);
|
|
927
|
-
f.write(` * | namespace |${f1(cache.namespace[nodeId.namespace].namespaceUri)}|`);
|
|
928
|
-
f.write(` * | nodeClass |${f1(NodeClass[nodeClass])}|`);
|
|
929
|
-
f.write(` * | name |${f1(browseName.toString())}|`);
|
|
930
|
-
f.write(` * | isAbstract|${f1(isAbstract.toString())}|`);
|
|
931
|
-
f.write(` */`);
|
|
932
|
-
|
|
933
|
-
let type: "basic" | "structure" | "enum" | "ua" = "basic";
|
|
934
|
-
if (definition instanceof EnumDefinition) {
|
|
935
|
-
type = "enum";
|
|
936
|
-
f.write(`export enum ${interfaceName} {`);
|
|
937
|
-
for (const field of definition.fields!) {
|
|
938
|
-
if (field.description.text) {
|
|
939
|
-
f.write(` /**`);
|
|
940
|
-
f.write(toComment(" * ", field.description.text || ""));
|
|
941
|
-
f.write(` */`);
|
|
942
|
-
}
|
|
943
|
-
f.write(` ${quotifyIfNecessary(field.name!)} = ${field.value[1]},`);
|
|
944
|
-
}
|
|
945
|
-
f.write(`}`);
|
|
946
|
-
} else if (definition instanceof StructureDefinition) {
|
|
947
|
-
if (baseInterfaceName === "DTUnion") {
|
|
948
|
-
type = "structure";
|
|
949
|
-
for (let i = 0; i < definition.fields!.length; i++) {
|
|
950
|
-
f.write(`export interface ${interfaceName}_${i} extends ${baseInterfaceName} {`);
|
|
951
|
-
for (let j = 0; j < definition.fields!.length; j++) {
|
|
952
|
-
const field = definition.fields![j];
|
|
953
|
-
const fieldName = toJavascritPropertyName(field.name!, { ignoreConflictingName: false });
|
|
954
|
-
if (j === i) {
|
|
955
|
-
await outputStructureField(f, field);
|
|
956
|
-
} else {
|
|
957
|
-
f.write(` ${quotifyIfNecessary(fieldName)}?: never`);
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
f.write(`}`);
|
|
961
|
-
}
|
|
962
|
-
f.write(`export type ${interfaceName} = `);
|
|
963
|
-
for (let i = 0; i < definition.fields!.length; i++) {
|
|
964
|
-
f.write(` | ${interfaceName}_${i}`);
|
|
965
|
-
}
|
|
966
|
-
f.write(` ;`);
|
|
967
|
-
} else {
|
|
968
|
-
type = "structure";
|
|
969
|
-
if (!definition.fields!.length && interfaceName !== "DTStructure") {
|
|
970
|
-
f.write(`export type ${interfaceName} = ${baseInterfaceName};`);
|
|
971
|
-
} else {
|
|
972
|
-
if (interfaceName === "DTStructure") {
|
|
973
|
-
f.write(`export interface ${interfaceName} {`);
|
|
974
|
-
} else {
|
|
975
|
-
f.write(`export interface ${interfaceName} extends ${baseInterfaceName} {`);
|
|
976
|
-
}
|
|
977
|
-
for (const field of definition.fields!) {
|
|
978
|
-
await outputStructureField(f, field);
|
|
979
|
-
}
|
|
980
|
-
f.write(`}`);
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
} else {
|
|
984
|
-
type = "basic";
|
|
985
|
-
f.write(`// NO DEFINITION`);
|
|
986
|
-
f.write(`export type ${interfaceName} = ${baseInterfaceName};`);
|
|
987
|
-
// throw new Error("Invalid " + definition?.constructor.name);
|
|
988
|
-
}
|
|
989
|
-
return { type, content: f.toString(), typeName: interfaceName };
|
|
990
752
|
|
|
991
|
-
|
|
992
|
-
const fieldName = toJavascritPropertyName(field.name!, { ignoreConflictingName: false });
|
|
993
|
-
// special case ! fieldName=
|
|
994
|
-
if (field.description.text) {
|
|
995
|
-
f.write(`/** ${field.description.text}*/`);
|
|
996
|
-
}
|
|
997
|
-
const ar = field.valueRank >= 1 ? "[]" : "";
|
|
998
|
-
const { dataType, jtype } = await getCorrepondingJavascriptType(session, field.dataType, cache, importCollector);
|
|
999
|
-
f.write(` ${quotifyIfNecessary(fieldName)}: ${jtype}${ar}; // ${DataType[dataType]} ${field.dataType.toString()}`);
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
753
|
+
export type Type = "enum" | "basic" | "structure" | "ua";
|
|
1002
754
|
|
|
1003
755
|
function calculateChevrons(classDef: ClassDefinition, classDefDerived?: { dataType: DataType }) {
|
|
1004
756
|
const { nodeClass, dataType, dataTypeName } = classDef;
|
package/source/main.ts
CHANGED
|
@@ -20,7 +20,8 @@ async function main() {
|
|
|
20
20
|
nodesets.machineTool,
|
|
21
21
|
nodesets.cnc,
|
|
22
22
|
nodesets.woodWorking,
|
|
23
|
-
nodesets.glass
|
|
23
|
+
nodesets.glass,
|
|
24
|
+
nodesets.tightening
|
|
24
25
|
]);
|
|
25
26
|
|
|
26
27
|
const nsUA = 0;
|
|
@@ -37,6 +38,7 @@ async function main() {
|
|
|
37
38
|
const nsCommercialKitchenEquipment = addressSpace.getNamespaceIndex("http://opcfoundation.org/UA/CommercialKitchenEquipment/");
|
|
38
39
|
const nsWW = addressSpace.getNamespaceIndex("http://opcfoundation.org/UA/Woodworking/");
|
|
39
40
|
const nsGlass = addressSpace.getNamespaceIndex("http://opcfoundation.org/UA/Glass/Flat/");
|
|
41
|
+
const nsThightening = addressSpace.getNamespaceIndex("http://opcfoundation.org/UA/IJT/");
|
|
40
42
|
|
|
41
43
|
const session = new PseudoSession(addressSpace);
|
|
42
44
|
const options = {
|
|
@@ -58,7 +60,8 @@ async function main() {
|
|
|
58
60
|
convertNamespaceTypeToTypescript(session, ncCNC, options),
|
|
59
61
|
convertNamespaceTypeToTypescript(session, nsCommercialKitchenEquipment, options),
|
|
60
62
|
convertNamespaceTypeToTypescript(session, nsWW, options),
|
|
61
|
-
convertNamespaceTypeToTypescript(session, nsGlass, options)
|
|
63
|
+
convertNamespaceTypeToTypescript(session, nsGlass, options),
|
|
64
|
+
convertNamespaceTypeToTypescript(session, nsThightening, options)
|
|
62
65
|
];
|
|
63
66
|
await Promise.all(promises);
|
|
64
67
|
}
|
package/source/private/cache.ts
CHANGED
|
@@ -12,6 +12,7 @@ export interface Import {
|
|
|
12
12
|
name: string;
|
|
13
13
|
module: string;
|
|
14
14
|
namespace: number;
|
|
15
|
+
name2?: string;
|
|
15
16
|
}
|
|
16
17
|
interface CacheInterfaceNamespace {
|
|
17
18
|
namespaceUri: string;
|
|
@@ -31,7 +32,13 @@ interface RequestedSymbol {
|
|
|
31
32
|
symbols: { [key: string]: RequestedSubSymbol };
|
|
32
33
|
}[];
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
+
export function makeName2(name: string) {
|
|
36
|
+
return "U" + name;
|
|
37
|
+
// if (name === "EUInformation") {
|
|
38
|
+
// return "EUInformation2";
|
|
39
|
+
// }
|
|
40
|
+
// return name.replace(/^DT/, "").replace(/^3/, "Three").replace(/^2/, "Two");
|
|
41
|
+
}
|
|
35
42
|
export class Cache implements CacheInterface {
|
|
36
43
|
namespace: CacheInterfaceNamespace[] = [];
|
|
37
44
|
requestedSymbols: RequestedSymbol = { namespace: [] };
|
|
@@ -45,11 +52,12 @@ export class Cache implements CacheInterface {
|
|
|
45
52
|
UAVariableT: 1,
|
|
46
53
|
UAMethod: 1,
|
|
47
54
|
UADataType: 1,
|
|
48
|
-
UAProperty: 1
|
|
55
|
+
UAProperty: 1
|
|
49
56
|
},
|
|
50
57
|
"node-opcua-variant": {
|
|
51
58
|
DataType: 1,
|
|
52
|
-
Variant: 1
|
|
59
|
+
Variant: 1,
|
|
60
|
+
VariantOptions: 1,
|
|
53
61
|
},
|
|
54
62
|
"node-opcua-data-model": {
|
|
55
63
|
LocalizedText: 1,
|
|
@@ -85,7 +93,13 @@ export class Cache implements CacheInterface {
|
|
|
85
93
|
},
|
|
86
94
|
"node-opcua-types": {
|
|
87
95
|
EnumValueType: 1
|
|
96
|
+
},
|
|
97
|
+
"node-opcua-extension-object": {
|
|
98
|
+
ExtensionObject: 1
|
|
88
99
|
}
|
|
100
|
+
// "node-opcua-schemas": {
|
|
101
|
+
// ExtensionObject: 1
|
|
102
|
+
// }
|
|
89
103
|
};
|
|
90
104
|
}
|
|
91
105
|
resetRequire(): void {
|
|
@@ -133,11 +147,10 @@ export class Cache implements CacheInterface {
|
|
|
133
147
|
}
|
|
134
148
|
}
|
|
135
149
|
|
|
136
|
-
|
|
137
150
|
function getSourceFolderForNamespace(browseName: QualifiedName, options: Options, cache: Cache2) {
|
|
138
151
|
const ns = browseName.namespaceIndex;
|
|
139
152
|
const n = cache.namespaceArray[ns].split("/").filter((s: string) => s.length !== 0);
|
|
140
|
-
const nn =
|
|
153
|
+
const nn = (n[n.length - 1] !== "UA" && n[n.length - 2] !== "UA" ? n[n.length - 2] : "") + n[n.length - 1];
|
|
141
154
|
const module = options.prefix + kebabCase(nn);
|
|
142
155
|
const namespaceFolder = path.join(options.baseFolder, module);
|
|
143
156
|
const sourceFolder = path.join(namespaceFolder);
|
|
@@ -158,7 +171,6 @@ export async function referenceEnumeration(session: IBasicSession, enumerationDa
|
|
|
158
171
|
return dataTypeNameImport;
|
|
159
172
|
}
|
|
160
173
|
|
|
161
|
-
|
|
162
174
|
export class Cache2 extends Cache {
|
|
163
175
|
public namespaceArray: string[] = [];
|
|
164
176
|
}
|
|
@@ -195,7 +207,12 @@ export async function constructCache(session: IBasicSession, options: Options):
|
|
|
195
207
|
function makeBasicTypeImport(name: string): Import {
|
|
196
208
|
return { name, module: "BasicType", namespace: -1 };
|
|
197
209
|
}
|
|
198
|
-
export function makeTypeNameNew(
|
|
210
|
+
export function makeTypeNameNew(
|
|
211
|
+
nodeClass: NodeClass,
|
|
212
|
+
definition: DataTypeDefinition | null,
|
|
213
|
+
browseName: QualifiedName,
|
|
214
|
+
suffix?: string
|
|
215
|
+
): Import {
|
|
199
216
|
assert(browseName && !!browseName.name, "expecting a class name here");
|
|
200
217
|
|
|
201
218
|
const typeName = browseName.name!.toString();
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { NodeId } from "node-opcua-nodeid";
|
|
2
|
+
import { IBasicSession } from "node-opcua-pseudo-session";
|
|
3
|
+
import { DataType } from "node-opcua-variant";
|
|
4
|
+
import { Import, referenceEnumeration, referenceExtensionObject } from "./cache";
|
|
5
|
+
import { getValueRank, _convertNodeIdToDataTypeAsync } from "./utils";
|
|
6
|
+
import { Cache } from "./cache";
|
|
7
|
+
|
|
8
|
+
export async function getCorrepondingJavascriptType2(
|
|
9
|
+
session: IBasicSession,
|
|
10
|
+
nodeId: NodeId,
|
|
11
|
+
dataTypeNodeId: NodeId,
|
|
12
|
+
cache: Cache,
|
|
13
|
+
importCollect?: (t: Import) => void
|
|
14
|
+
): Promise<{ dataType: DataType; jtype: string }> {
|
|
15
|
+
const q = await getCorrepondingJavascriptType(session, dataTypeNodeId, cache, importCollect);
|
|
16
|
+
const valueRank = await getValueRank(session, nodeId);
|
|
17
|
+
return { dataType: q.dataType, jtype: q.jtype + (valueRank >= 1 ? "[]" : "") };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// eslint-disable-next-line complexity
|
|
21
|
+
export async function getCorrepondingJavascriptType(
|
|
22
|
+
session: IBasicSession,
|
|
23
|
+
dataTypeNodeId: NodeId,
|
|
24
|
+
cache: Cache,
|
|
25
|
+
importCollect?: (t: Import) => void
|
|
26
|
+
): Promise<{ enumerationType?: string; dataType: DataType; jtype: string }> {
|
|
27
|
+
const { dataType, enumerationType } = await _convertNodeIdToDataTypeAsync(session, dataTypeNodeId);
|
|
28
|
+
|
|
29
|
+
if (enumerationType) {
|
|
30
|
+
// we have a enmeration name here
|
|
31
|
+
const jtypeImport = await referenceEnumeration(session, dataTypeNodeId);
|
|
32
|
+
const jtype = jtypeImport.name;
|
|
33
|
+
importCollect && importCollect(jtypeImport);
|
|
34
|
+
return { dataType, jtype };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (dataType === DataType.ExtensionObject) {
|
|
38
|
+
const jtypeImport = await referenceExtensionObject(session, dataTypeNodeId);
|
|
39
|
+
const jtype = jtypeImport.name;
|
|
40
|
+
importCollect && importCollect(jtypeImport);
|
|
41
|
+
return { dataType, jtype };
|
|
42
|
+
}
|
|
43
|
+
const referenceBasicType = (name: string): string => {
|
|
44
|
+
const t = { name, namespace: -1, module: "BasicType" };
|
|
45
|
+
importCollect && importCollect(t);
|
|
46
|
+
cache.ensureImported(t);
|
|
47
|
+
return t.name;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
switch (dataType) {
|
|
51
|
+
case DataType.Null:
|
|
52
|
+
return { dataType: DataType.Variant, jtype: referenceBasicType("VariantOptions") };
|
|
53
|
+
case DataType.Boolean:
|
|
54
|
+
return { dataType, jtype: "boolean" };
|
|
55
|
+
case DataType.Byte:
|
|
56
|
+
return { dataType, jtype: referenceBasicType("Byte") };
|
|
57
|
+
case DataType.ByteString:
|
|
58
|
+
return { dataType, jtype: "Buffer" };
|
|
59
|
+
case DataType.DataValue:
|
|
60
|
+
return { dataType, jtype: referenceBasicType("DataValue") };
|
|
61
|
+
case DataType.DateTime:
|
|
62
|
+
return { dataType, jtype: "Date" };
|
|
63
|
+
case DataType.DiagnosticInfo:
|
|
64
|
+
return { dataType, jtype: referenceBasicType("DiagnosticInfo") };
|
|
65
|
+
case DataType.Double:
|
|
66
|
+
return { dataType, jtype: "number" };
|
|
67
|
+
case DataType.Float:
|
|
68
|
+
return { dataType, jtype: "number" };
|
|
69
|
+
case DataType.Guid:
|
|
70
|
+
return { dataType, jtype: referenceBasicType("Guid") };
|
|
71
|
+
case DataType.Int16:
|
|
72
|
+
return { dataType, jtype: referenceBasicType("Int16") };
|
|
73
|
+
case DataType.Int32:
|
|
74
|
+
return { dataType, jtype: referenceBasicType("Int32") };
|
|
75
|
+
case DataType.UInt16:
|
|
76
|
+
return { dataType, jtype: referenceBasicType("UInt16") };
|
|
77
|
+
case DataType.UInt32:
|
|
78
|
+
return { dataType, jtype: referenceBasicType("UInt32") };
|
|
79
|
+
case DataType.UInt64:
|
|
80
|
+
return { dataType, jtype: referenceBasicType("UInt64") };
|
|
81
|
+
case DataType.Int64:
|
|
82
|
+
return { dataType, jtype: referenceBasicType("Int64") };
|
|
83
|
+
case DataType.LocalizedText:
|
|
84
|
+
return { dataType, jtype: referenceBasicType("LocalizedText") };
|
|
85
|
+
case DataType.NodeId:
|
|
86
|
+
return { dataType, jtype: referenceBasicType("NodeId") };
|
|
87
|
+
case DataType.ExpandedNodeId:
|
|
88
|
+
return { dataType, jtype: referenceBasicType("ExpandedNodeId") };
|
|
89
|
+
case DataType.QualifiedName:
|
|
90
|
+
return { dataType, jtype: referenceBasicType("QualifiedName") };
|
|
91
|
+
case DataType.SByte:
|
|
92
|
+
return { dataType, jtype: referenceBasicType("SByte") };
|
|
93
|
+
case DataType.StatusCode:
|
|
94
|
+
return { dataType, jtype: referenceBasicType("StatusCode") };
|
|
95
|
+
case DataType.String:
|
|
96
|
+
return { dataType, jtype: referenceBasicType("UAString") };
|
|
97
|
+
case DataType.Variant:
|
|
98
|
+
return { dataType, jtype: referenceBasicType("Variant") };
|
|
99
|
+
case DataType.XmlElement:
|
|
100
|
+
return { dataType, jtype: referenceBasicType("String") };
|
|
101
|
+
default:
|
|
102
|
+
throw new Error("Unsupported " + dataType + " " + DataType[dataType]);
|
|
103
|
+
}
|
|
104
|
+
}
|