-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviceassetlist.html
executable file
·225 lines (200 loc) · 8.08 KB
/
deviceassetlist.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Define triggering rules</title>
<!-- Custom Siemens Theme -->
<link href="siemens-theme.css" rel="stylesheet">
</head>
<script>
window.onload = function() {
// Get and show all assets
getAssets();
}
function filterAssets() {
// User input
var input = document.getElementById('asset_filter').value;
// Get asset with specific name
var filter = '"name": {"contains": {"value": "' + input + '"}}';
getAssets(filter);
}
function getAssets(filterParams) {
// Set url
var assetUrl = "/api/assetmanagement/v3/assets";
// Filter asset type
//var tenant = "casseoe"; // Hard coded tenant name !!!
var tenant = "solarc02"; // Hard coded tenant name !!!
var filter = '"typeId": {"startsWith": {"value": "' + tenant + '"}}'; // Device asset types start with tenant name
// Filter asset name
if(filterParams) {
filter = filter + ', ' + filterParams;
}
assetUrl = assetUrl + '?filter=' + encodeURIComponent('{' + filter + '}')
// Create request to get assets
var assetReq = new XMLHttpRequest();
assetReq.open("GET", assetUrl, false); // 'false' makes the request synchronous
// Get assets
var assetlist = [];
assetReq.onreadystatechange = function() {
if (assetReq.readyState === 4) { // When readyState property is 4 and status property is 200, the response is ready
if (assetReq.status === 200) {
console.log('GET assets response: ' + assetReq.response);
var jsonResponse = JSON.parse(assetReq.response);
assetlist = jsonResponse['_embedded']['assets'];
}
else {
console.error('Request status text: ' + assetReq.statusText);
}
}
}
console.log('GET assets: ' + assetUrl);
assetReq.send(null);
// Get aspects
assetlist.forEach(function(element) {
var aspectUrl = '/api/assetmanagement/v3/assets/' + element.assetId + '/aspects';
// Create request to get aspects
var aspectReq = new XMLHttpRequest();
aspectReq.open("GET", aspectUrl, false); // 'false' makes the request synchronous
// Get all aspects for one asset
aspectReq.onreadystatechange = function() {
if (aspectReq.readyState === 4) { // When readyState property is 4 and status property is 200, the response is ready
if (aspectReq.status === 200) {
console.log('GET aspects response: ' + aspectReq.response);
var jsonResponse = JSON.parse(aspectReq.response);
element.aspects = jsonResponse['_embedded']['aspects'];
// for every aspects, we get timeseries
getLatestValueForAspects(element, 5);
}
else {
console.error('Request status text: ' + aspectReq.statusText);
}
}
}
console.log('GET aspects: ' + aspectUrl);
aspectReq.send(null);
});
// call timeseries to get latest data
// and zip to assetList
displayAssets(assetlist);
}
function getLatestValueForAspects(element, limit){
limit = limit || 1;
var to = new Date.now().toISOString();
var from = new Date(to).setMinues(to.getMinutes() - 1).toISOString();
var timeSeriesReq = new XMLHttpRequest();
var address = 'api/iottimeseries/v3/timeseries/' + element.assetId + "/";
// api/iottimeseries/v3/timeseries/b43703b007924c2889adc9da9f818a5b/Sensors?from=2018-05-01T00:00:00Z&to=2018-05-01T23:50:00Z&limit=100
element.aspects.forEach(function(aspect){
timeSeriesReq.open("GET", address + aspect.name + "?from="+from + "&to=" +to + "&limit=" + limit , false); // 'false' makes the request synchronous
timeSeriesReq.onreadystatechange = function() {
if (timeSeriesReq.readyState === 4) { // When readyState property is 4 and status property is 200, the response is ready
if (timeSeriesReq.status === 200) {
console.log('GET timeseries response: ' + timeSeriesReq.response);
var timeSeriesArray = JSON.parse(timeSeriesReq.response);
var latestValue = timeSeriesArray && timeSeriesArray.length > 0 && timeSeriesArray[timeSeriesArray.length -1];
aspect.variables.forEach(function(variable){
variable.latestValue = latestData[variable.name];
})
}
else {
console.error('Request status text: ' + timeSeriesReq.statusText);
}
}
}
});
}
function displayAssets(assetlist){
var htmlStr = '<table name="asssettable" id="common_table"> '+
'<tr id="header_row">'+
'<th>'+
'<label>Asset Name</label>'+
'</th>'+
'<th>'+
'<label>Asset ID</label>'+
'</th>'+
'<th>'+
'<label>Asset Description</label>'+
'</th>'+
'<th>'+
'<label>Asset Type</label>'+
'</th>'+
'<th>'+
'<label>Asset Location</label>'+
'</th>'+
'</tr>';
// Display asset data
assetlist.forEach(function(asset) {
htmlStr = htmlStr + '<tr id="common_table_level1">'+
'<td>'+
'<label>' + asset.name + '</label>'+
'</td>'+
'<td>'+
'<label>' + asset.assetId + '</label>'+
'</td>'+
'<td>'+
'<label>' + asset.description + '</label>'+
'</td>'+
'<td>'+
'<label>' + asset.typeId + '</label>'+
'</td>'+
'<td>'+
'<label>' + asset.location.streetAddress + ' -' + asset.location.postalCode + '- ' + asset.location.locality + '</label>'+
'</td>'+
'</tr>';
// Display aspect data
asset.aspects.forEach(function(aspect) {
htmlStr = htmlStr + '<tr id="common_table_level2">'+
'<td colspan="3">'+
'<label>Aspect Name: ' + aspect.name + '</label>'+
'</td>'+
'<td colspan="1">'+
'<label>Trigger Condition' + '</label>'+
'</td>'
'<td id="triggerValue-' + asset.assetId + '-' + + 'colspan="1">'+
'<label>Trigger Value' + '</label>'+
'</td>'
'</tr>';
// Display variable data
aspect.variables.forEach(function(variable) {
htmlStr = htmlStr + '<tr id="common_table_level3">'+
'<td colspan="4">'+
'<label>Variable Name: ' + variable.name + ' | Data Type: ' + variable.dataType + '</label>'+
'</td>'+
'<td colspan="4">'+
'<label>Variable Name: ' + variable.name + ' | Data Type: ' + variable.dataType + '| Latest value: ' + variable.latestValue + '</label>'+
'</td>'+
'</tr>';
});
});
});
htmlStr = htmlStr + '</table>' ;
document.getElementById("assetObj").innerHTML = htmlStr;
}
</script>
<!-- MindSphere OS bar -->
<script src="https://static.eu1.mindsphere.io/osbar/js/main.min.js"></script>
<script>
_msb.init({
title: "WebApp - Assets",
appId: "_mscontent"
});
</script>
<body>
<div id="_mscontent">
<!-- Side Navigation-->
<div class="sidenav">
<img src="siemens.svg" class="sidenav-title">
<a href="deviceassetlist.html">Device Assets</a>
<a href="agentassetlist.html">Agent Assets</a>
</div>
<!-- Side Content -->
<div class="main">
<div style="padding: 10px;">
Search for an asset name: <input type="text" id="asset_filter">
<button type="submit" onclick="filterAssets()">Search</button>
</div>
<div id="assetObj"></div>
</div>
</div>
</body>
</html>