/
measureManagerService.js
355 lines (309 loc) · 13.1 KB
/
measureManagerService.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) 2008-2020, Alliance for Sustainable Energy, LLC. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
* products derived from this software without specific prior written permission from the respective party.
*
* (4) Other than as required in clauses (1) and (2), distributions in any form of modifications or other derivative
* works may not use the "OpenStudio" trademark, "OS", "os", or any other confusingly similar designation without
* specific prior written permission from Alliance for Sustainable Energy, LLC.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************************************************************/
import openport from 'openport';
export class MeasureManager {
constructor($log, $http, $q, DependencyManager, Message) {
'ngInject';
const vm = this;
vm.$log = $log;
vm.$http = $http;
vm.$q = $q;
vm.Message = Message;
let exeExt = '';
if (process.platform == 'win32') {
exeExt = '.exe';
}
// to run cli
vm.spawn = require('child_process').spawn;
vm.cliPath = DependencyManager.getPath('PAT_OS_CLI_PATH');
vm.url = 'http://localhost';
vm.port = 1234;
// measure Manager ready promise
vm.mmReadyDeferred = vm.$q.defer();
}
isReady() {
const vm = this;
return vm.mmReadyDeferred.promise;
}
startMeasureManager() {
const vm = this;
// find an open port
openport.find(
{
startingPort: 3100,
endingPort: 3500,
avoid: [3500]
},
function(err, port) {
if(err) { vm.$log.error('Error locating an open port for measure manager.'); return; }
vm.port = port;
vm.$log.info('Measure Manager port: ', vm.port);
vm.$log.info('Start Measure Manager Server: ', vm.cliPath, ' measure -s ', vm.port);
vm.cli = vm.spawn(vm.cliPath, ['measure', '-s', vm.port]);
vm.cli.stdout.on('data', (data) => {
// record errors
if (data.indexOf('<0>') !== -1) {
// WARNING
vm.$log.warn(`MeasureManager WARNING: ${data}`);
vm.Message.appendMeasureManagerError({type: 'warning', data: data.toString()});
} else if (data.indexOf('<1>') !== -1) {
// ERROR
vm.$log.error(`MeasureManager ERROR: ${data}`);
vm.Message.appendMeasureManagerError({type: 'error', data: data.toString()});
} else if(data.indexOf('<2>') !== -1) {
// ERROR
vm.$log.error(`MeasureManager ERROR: ${data}`);
vm.Message.appendMeasureManagerError({type: 'fatal', data: data.toString()});
}
else {
if (vm.Message.showDebug()) vm.$log.debug(`MeasureManager: ${data}`);
}
// check that the mm was started correctly: resolve readyDeferred
const str = data.toString();
if (str.indexOf('WEBrick::HTTPServer#start: pid=') !== -1) {
if (vm.Message.showDebug()) vm.$log.debug('Found WEBrick Start!, resolve promise');
vm.mmReadyDeferred.resolve();
}
// TODO: THIS IS TEMPORARY (windows):
else if (str.indexOf('Only one usage of each socket address') !== -1) {
if (vm.Message.showDebug()) vm.$log.debug('WEBrick already running...assuming MeasureManager is already up');
vm.mmReadyDeferred.resolve();
}
// TODO: THIS IS TEMPORARY (mac):
else if (str.indexOf('Error: Address already in use') !== -1) {
if (vm.Message.showDebug()) vm.$log.debug('WEBrick already running...assuming MeasureManager is already up');
vm.mmReadyDeferred.resolve();
}
});
vm.cli.stderr.on('data', (data) => {
vm.$log.info(`MeasureManager: ${data}`);
// check that the mm was started correctly: resolve readyDeferred
const str = data.toString();
if (str.indexOf('WEBrick::HTTPServer#start: pid=') !== -1) {
if (vm.Message.showDebug()) vm.$log.debug('Found WEBrick Start!, resolve promise');
vm.mmReadyDeferred.resolve();
}
// TODO: THIS IS TEMPORARY (windows):
else if (str.indexOf('Only one usage of each socket address') !== -1) {
if (vm.Message.showDebug()) vm.$log.debug('WEBrick already running...using tempMeasureManager');
vm.mmReadyDeferred.resolve();
}
// TODO: THIS IS TEMPORARY (mac):
else if (str.indexOf('Error: Address already in use') !== -1) {
if (vm.Message.showDebug()) vm.$log.debug('WEBrick already running...using tempMeasureManager');
vm.mmReadyDeferred.resolve();
}
});
vm.cli.on('close', (code) => {
vm.$log.info(`Measure Manager exited with code ${code}`);
});
}
);
}
stopMeasureManager() {
const vm = this;
vm.$log.info('Stop Measure Manager Server');
vm.cli.kill('SIGINT');
}
// This function creates a new measure on the file system in folder 'LocalBCL'
// params {
// measure_dir:
// display_name:
// class_name:
// taxonomy_tag:
// measure_type:
// description:
// modeler_description:
// }
createNewMeasure(params) {
const vm = this;
const deferred = vm.$q.defer();
// reset MeasureManagerErrors when a new action
vm.Message.resetMeasureManagerErrors();
if (vm.Message.showDebug()) vm.$log.debug('MeasureManager::createNewMeasure');
if (vm.Message.showDebug()) vm.$log.debug('MeasureManager::createNewMeasure params: ', params);
vm.$http.post(`${vm.url}:${vm.port}/create_measure`, params)
.success((data, status, headers, config) => {
vm.$log.info('MeasureManager::createNewMeasure reply: ', data);
deferred.resolve(data);
})
.error((data, status, headers, config) => {
vm.$log.error('MeasureManager::createNewMeasure error: ', data);
deferred.reject();
});
return deferred.promise;
}
// This function make a copy of a measure on the file system.
// Copy should be in 'my measures' (actual folder name: 'Measures')
// params should look like this
// params {
// old_measure_dir:
// measure_dir:
// display_name:
// class_name:
// taxonomy_tag:
// measure_type:
// description:
// modeler_description:
// force_reload:
// }
duplicateMeasure(params) {
const vm = this;
const deferred = vm.$q.defer();
// reset MeasureManagerErrors when a new action
vm.Message.resetMeasureManagerErrors();
if (vm.Message.showDebug()) vm.$log.debug('params one more time: ', params);
vm.$http.post(`${vm.url}:${vm.port}/duplicate_measure`, params)
.success((data, status, headers, config) => {
vm.$log.info('Measure Manager reply: ', data);
deferred.resolve(data);
})
.error((data, status, headers, config) => {
vm.$log.error('Measure Manager DuplicateMeasure error: ', data);
deferred.reject();
});
return deferred.promise;
}
// Update Measures
// This function updates measures at specified path
// Expects a measurePath
updateMeasures(measurePath) {
const vm = this;
const deferred = vm.$q.defer();
// fix path for windows
const newMeasurePath = measurePath.replace(/\\/g, '/'); // Evan: how to normalize the path
const params = {measures_dir: newMeasurePath};
if (vm.Message.showDebug()) vm.$log.debug('PARAMS: ', params);
vm.$http.post(`${vm.url}:${vm.port}/update_measures`, params)
.success((data, status, headers, config) => {
vm.$log.info('updateMeasures Success!');
// if (vm.Message.showDebug()) vm.$log.debug('Measure Manager reply: ', data);
deferred.resolve(data);
})
.error((data, status, headers, config) => {
vm.$log.error('Measure Manager UpdateMeasures error: ', data);
deferred.reject([]);
});
return deferred.promise;
}
// Returns the path to the myMeasures directory
getMyMeasuresDir() {
const vm = this;
const deferred = vm.$q.defer();
vm.$http.get(`${vm.url}:${vm.port}`, {
params: {}
})
.success((data, status, headers, config) => {
vm.$log.info('Measure Manager getMyMeasuresDir Success!, status: ', status);
deferred.resolve(data);
})
.error((data, status, headers, config) => {
vm.$log.error('Measure Manager getMyMeasuresDir Error: ', data);
deferred.reject([]);
});
return deferred.promise;
}
setMyMeasuresDir(path) {
const vm = this;
const deferred = vm.$q.defer();
const params = {my_measures_dir: path};
// reset MeasureManagerErrors when a new action
vm.Message.resetMeasureManagerErrors();
vm.$http.post(`${vm.url}:${vm.port}/set`, params)
.success((data, status, headers, config) => {
vm.$log.info('Measure Manager setMyMeasuresDir Success!, status: ', status);
deferred.resolve(data);
})
.error((data, status, headers, config) => {
vm.$log.error('Measure Manager SetMyMeasuresDir Error: ', data);
deferred.reject([]);
});
return deferred.promise;
}
// Retrieve Local BCL measures
getLocalBCLMeasures() {
const vm = this;
const deferred = vm.$q.defer();
const params = {};
vm.$http.post(`${vm.url}:${vm.port}/bcl_measures`, params)
.success((data, status, headers, config) => {
vm.$log.info('Measure Manager bcl_measures Success!, status: ', status);
deferred.resolve(data);
})
.error((data, status, headers, config) => {
vm.$log.error('Measure Manager bcl_measures Error: ', data);
deferred.reject([]);
});
return deferred.promise;
}
// Download a measure from online BCL
downloadBCLMeasure(uid) {
const vm = this;
const deferred = vm.$q.defer();
const params = {uid: uid};
// reset MeasureManagerErrors when a new action
vm.Message.resetMeasureManagerErrors();
vm.$http.post(`${vm.url}:${vm.port}/download_bcl_measure`, params)
.success((data, status, headers, config) => {
vm.$log.info('Measure Manager download_bcl_measure Success!, status: ', status);
vm.$log.info('Data: ', data);
deferred.resolve(data[0]);
})
.error((data, status, headers, config) => {
vm.$log.error('Measure Manager download_bcl_measure Error: ', data);
deferred.reject([]);
});
return deferred.promise;
}
// Compute Arguments
// This function computes arguments and returns all metadata for a single measure
// Expects a measurePath. and osmPath if evaluating against a specific model
computeArguments(measurePath, osmPath = null) {
const vm = this;
const deferred = vm.$q.defer();
// reset MeasureManagerErrors when a new action
vm.Message.resetMeasureManagerErrors();
// TODO: is there a situation where we want to use an empty model even though we have a seed model defined?
if (!osmPath) {
osmPath = (vm.defaultSeed == null) ? null : vm.seedDir.path(vm.defaultSeed);
}
const params = {measure_dir: measurePath, osm_path: osmPath};
if (vm.Message.showDebug()) vm.$log.debug('computeArguments params', params);
vm.$http.post(`${vm.url}:${vm.port}/compute_arguments`, params)
.success((data, status, headers, config) => {
vm.$log.info('computeArguments Success!, status: ', status);
// if (vm.Message.showDebug()) vm.$log.debug('Measure Manager reply: ', data);
deferred.resolve(data);
})
.error((data, status, headers, config) => {
vm.$log.error('Measure Manager ComputeArguments error: ', data);
deferred.reject([]);
});
return deferred.promise;
}
}