-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharrowhead.js
258 lines (250 loc) · 11.5 KB
/
arrowhead.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @license
* Copyright 2020 Sébastien CANET
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* @fileoverview Arduino CLI control.
* @author [email protected] (Sébastien CANET)
*/
let startupHelper = require('./arrowhead/utils/startupHelper');
const rejectHandler = (message) => {
document.getElementById("compiler-output-text").style.color = '#FF0000';
document.getElementById("compiler-output-text").innerHTML += '<br>' + message;
console.log(message);
}
window.addEventListener('load', function load(event) {
document.getElementById('ArrowheadConfiguration_auto').onclick = async function(event) {
document.getElementById("compiler-output-text").style.color = '#00FF00';
document.getElementById("compiler-output-text").innerHTML = '<br>start Arrowhead connect script';
let systemFound = true;
let responseFunction = startupHelper.validateENV()
.then(async() => {
document.getElementById("compiler-output-text").innerHTML += '<br>ping registry service';
responseFunction = await echo()
})
.then(async() => {
console.log(responseFunction);
//specific query for existing consumer -> system
//else for service it will be on /query
responseFunction = await query('http://137.204.57.93:8443/serviceregistry/query/system', '{"address": "91.161.102.110","authenticationInfo": "","port": 8999,"systemName": "papyrus_file_consumer"}')
})
.catch(
err => {
systemFound = false;
rejectHandler(err.message);
}
)
.then(async() => {
responseFunction = await register('{"address": "91.161.102.110","authenticationInfo": "","port": 8999,"systemName": "papyrus_file_consumer"}', false);
})
.catch(
err => {
rejectHandler(err.message);
}
)
.then(async() => {
responseFunction = await orchestration('{"requesterSystem": {"systemName": "papyrus_file_consumer","address": "91.161.102.110","port": 8999},"requestedService": {"serviceDefinitionRequirement": "file-downloader","interfaceRequirements": ["HTTP-INSECURE-JSON"],"pingProviders": false},"orchestrationFlags": {"onlyPreferred": false,"overrideStore": true,"externalServiceRequest": false,"enableInterCloud": false,"enableQoS": false,"matchmaking": false,"metadataSearch": false,"triggerInterCloud": false,"pingProviders": false},"preferredProviders": [],"commands": {},"qosRequirements": {}}');
console.log(responseFunction)
})
.catch(
err => {
rejectHandler(err.message);
}
)
};
document.getElementById('papyrusConnect').onclick = async function(event) {
document.getElementById("compiler-output-text").style.color = '#00FF00';
document.getElementById("compiler-output-text").innerHTML = '<br>Start Papyrus download file';
let responseFunction = downloadFileFromPapyrus()
.then(async() => {
const { ipcRenderer } = require('electron');
ipcRenderer.sendSync('launch_with_papyrus_configuration');
})
.catch(
err => {
rejectHandler(err.message);
}
)
}
})
/*
Requesting file from Papyrus, thanks to orchestration
*/
async function downloadFileFromPapyrus() {
let papyrusURL = "http://" + sessionStorage.getItem("PapyrusProvider") + "/downloadFile/HomeAutomationSystemST4Econfig.json";
let axios = require('axios');
return new Promise((resolve, reject) => {
axios.get(papyrusURL)
.then(response => {
if (response.status === 200) {
document.getElementById("compiler-output-text").innerHTML += '<br>Successfully downloaded file';
let fs = require('fs');
fs.writeFileSync('./arrowhead/HomeAutomationSystemST4Econfig.json', JSON.stringify(response.data));
return resolve(response)
}
})
.catch(error => {
console.log('Error during orchestration', error)
document.getElementById("compiler-output-text").style.color = '#FF0000';
document.getElementById("compiler-output-text").innerHTML += error;
return reject(error.response.data.errorMessage)
})
})
}
/*
Requesting orchestration based on the received params
*/
async function orchestration(orchestrationRequestForm) {
let axios = require('axios');
let orchestrationURL = 'http://137.204.57.93:8441/orchestrator/orchestration';
let opts = {
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
console.log('Orchestration starting...')
return new Promise((resolve, reject) => {
axios.post(orchestrationURL, orchestrationRequestForm, opts)
.then(response => {
if (response.status === 200) {
document.getElementById("compiler-output-text").innerHTML += '<br>Successfully launched orchestration';
let fs = require('fs');
fs.writeFileSync('./arrowhead/orchestrationResponse.txt', JSON.stringify(response));
sessionStorage.setItem("PapyrusProvider", response.data.response[0].provider.address + ":" + response.data.response[0].provider.port);
return resolve('Successfully launched orchestration', response)
}
})
.catch(error => {
console.log('Error during orchestration', error)
document.getElementById("compiler-output-text").style.color = '#FF0000';
document.getElementById("compiler-output-text").innerHTML += error;
return reject(error.response.data.errorMessage)
})
})
}
async function echo() {
let axios = require('axios');
let orchestrationURL = 'http://137.204.57.93:8443/serviceregistry/echo';
opts = {
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
return new Promise(async(resolve, reject) => {
let response = null
const source = axios.CancelToken.source()
setTimeout(() => {
if (response === null) {
console.log('Service Registry is not available...')
source.cancel('Service Registry is not available...')
}
}, 1000)
response = await axios.get(
orchestrationURL, { cancelToken: source.token }
)
.then(response => {
console.log('Service Registry is accessible')
return resolve('Service Registry is accessible')
})
.catch(error => {
if (axios.isCancel(error)) {
console.log(error.message)
return reject(error.message)
}
console.log(error)
return reject(error)
})
})
}
async function query(URL, serviceRegistryEntry) {
let axios = require('axios');
let opts = {
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
console.log('Querying system')
return new Promise((resolve, reject) => {
if (!serviceRegistryEntry) {
document.getElementById("compiler-output-text").style.color = '#FF0000';
document.getElementById("compiler-output-text").innerHTML += 'Error during querying, missing Service Definition Requirement';
return reject('Error during querying, missing Service Definition Requirement')
}
axios.post(URL, serviceRegistryEntry, opts)
.then(response => {
if (response.status === 200) {
document.getElementById("compiler-output-text").innerHTML += '<br>Service already registered into Service Registry';
document.getElementById("compiler-output-text").innerHTML += '<br>Id: ' + response.data.id;
return resolve(response.data);
}
})
.catch(error => {
console.log('Error during querying requested service', error)
document.getElementById("compiler-output-text").style.color = '#FF0000';
document.getElementById("compiler-output-text").innerHTML += error.response.data.errorMessage;
return reject(error.response.data.errorMessage)
})
})
}
async function register(serviceRegistryEntry, force) {
let axios = require('axios');
let orchestrationURL = 'http://137.204.57.93:8443/serviceregistry/mgmt/systems';
let opts = {
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
return new Promise((resolve, reject) => {
axios.post(orchestrationURL, serviceRegistryEntry, opts)
.then((response) => {
document.getElementById("compiler-output-text").innerHTML += '<br>Successfully registered service into Service Registry';
document.getElementById("compiler-output-text").innerHTML += '<br>Id: ' + response.data.id;
return resolve('Successfully registered service into Service Registry', response.data)
})
.catch(async error => {
if (error.response.data.exceptionType === 'INVALID_PARAMETER') {
if (force) {
unregister(serviceRegistryEntry).then(async() => {
await register(serviceRegistryEntry, false)
return resolve('Successfully force registered service into Service Registry')
})
}
return resolve('Service is already registered into Service Registry')
}
document.getElementById("compiler-output-text").style.color = '#FF0000';
document.getElementById("compiler-output-text").innerHTML += error;
return reject(error.response.data.errorMessage)
})
})
}
async function unregister(Id) {
let axios = require('axios');
let orchestrationURL = 'http://137.204.57.93:8443/serviceregistry/mgmt/systems/' + Id;
let opts = {
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
return new Promise((resolve, reject) => {
axios.delete(orchestrationURL, opts)
.then((response) => {
if (response.status === 200) {
document.getElementById("compiler-output-text").innerHTML += '<br>Successfully unregistered service from Service Registry';
document.getElementById("compiler-output-text").innerHTML += '<br>Id: ' + response.data;
return resolve(`Successfully unregistered service from Service Registry`)
}
})
.catch(error => {
document.getElementById("compiler-output-text").style.color = '#FF0000';
document.getElementById("compiler-output-text").innerHTML += error;
console.log('Error removing service', error)
return reject(error.response.data.errorMessage)
})
})
}