forked from eclipse-aaswc/aaswc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAASParser.js
134 lines (117 loc) · 4.11 KB
/
AASParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) 2021 Lenze SE
* SPDX-License-Identifier: Apache-2.0
*/
class AASParser extends ParserBase {
constructor(printer) {
super();
/* general */
this.run = this.run.bind(this);
this.trimSuffixSlash = this.trimSuffixSlash.bind(this);
/* AAS */
this.parseAASRaw = this.parseAASRaw.bind(this);
this.printer = printer;
this.AjaxHelper = new AjaxHelper();
/* Variables */
this.aasURL = "";
this.submodelsURL = "";
this.AASRoot = this.newTreeObject("AASRoot", null,
"AssetAdministrationShellRoot");
this.treeRoot = this.AASRoot;
}
run() {
var aasStorageHandler = new AASWebStorageHandler();
var shellURL = this.getQueryVariable("endpoint");
if (shellURL) {
shellURL = decodeURIComponent(shellURL);
shellURL = this.trimSuffixSlash(shellURL);
aasStorageHandler.setCurrentAAS(shellURL);
}
// Set extra base URL
var aasURL = new URL(aasStorageHandler.getCurrentAAS());
this.setRootURLS(this.AASRoot, aasURL, 2);
this.AASRoot.tURL = aasStorageHandler.getCurrentAAS();
this.getByURL(this.AASRoot,
this.AASRoot.tURL,
this.parseAASRaw,
this.setErrorAAS);
}
trimSuffixSlash(URL) {
if (!URL.endsWith("/"))
return URL;
return URL.slice(0, - 1);
}
/* unbound for compound -> this */
setErrorAAS(status) {
var URL = this.URL;
var object = this.object;
var that = this.parentObj;
if (status.status == 401) {
var error = that.newTreeObject("AASError", object,
"tError");
that.parseString("Could not retrieve AAS", "Description", error);
that.parseString(URL, "URL", error);
that.parseValue(status.status, "ErrorCode", error);
that.printer.printError(error, "");
return;
}
if (this.retry < 2) {
this.retry++;
this.parentObj.AjaxHelper.getJSON(this.URL,
this.onSuccess,
this.onError,
this);
return;
}
var error = that.newTreeObject("AASError", object,
"tError");
that.parseString("Could not retrieve AAS","Description", error);
that.parseString(URL, "URL", error);
if (status.status != 0)
that.parseValue(status.status, "ErrorCode", error);
that.printer.printError(error, "");
}
/* unbound for compound -> this */
setErrorSubmodels(status) {
if (this.retry < 10) {
this.retry++;
this.parentObj.AjaxHelper.getJSON(this.URL,
this.parentObj.parseSubmodels,
this.parentObj.setErrorSubmodelsRaw,
this);
return;
}
var URL = this.URL;
var object = this.object;
var that = this.parentObj;
var error = that.newTreeObject("SubmodelError" + URL, object,
"tError");
that.parseString("Could not retrieve Submodel","Description", error);
that.parseString(URL, "URL", error);
if (status.status != 0)
that.parseValue(status.status, "ErrorCode", error);
that.printer.printError(error, "");
}
setError(errObj) {
console.log(errObj);
}
parseAASRaw(JSON) {
var AAS = JSON;
if (JSON.hasOwnProperty("AAS"))
AAS = JSON.AAS;
if (JSON.hasOwnProperty("entity"))
AAS = JSON.entity;
var aas = this.parseAAS(AAS, this.AASRoot, true, this.parseSubmodelsRaw,
this.setErrorSubmodels);
this.printer.printAAS(this.printer.rootElement, aas);
}
/* unbound for compound -> this */
parseSubmodelsRaw(JSON) {
var submodels = JSON;
if (JSON.hasOwnProperty("entity"))
submodels = JSON.entity;
this.parentObj.parseSubmodels(submodels, this.object, true,
this.parentObj.parseSubmodelRaw,
this.parentObj.setErrorSubmodel);
}
}