-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·111 lines (97 loc) · 3.67 KB
/
index.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
global.logContext = {
comp: 'iotAgent-OPCUA',
op: 'Index',
srv: '',
subsrv: ''
};
try {
// node-opcue dependencies
require('requirish')._(module);
const check_prop = require('./iot_agent_modules/check_properties');
if (check_prop.checkproperties().length != 0) {
console.log('WARNING!!!');
console.log('CHECK YOUR config.properties FILE, THE FOLLOWING PARAMETERS ARE NULL:');
for (const null_params in check_prop.checkproperties()) {
console.log(check_prop.checkproperties()[null_params]);
}
process.exit(1);
}
const server = require('./iot_agent_modules/services/server');
const run = require('./iot_agent_modules/run/run');
const fs = require('fs');
// custom simple logger
var logger = require('logops');
logger.format = logger.formatters.pipe;
const PropertiesReader = require('properties-reader');
const properties = PropertiesReader('./conf/config.properties');
global.properties = properties;
const endpointUrl = properties.get('endpoint');
const userName = properties.get('userName');
const password = properties.get('password');
if (endpointUrl == null) {
logger.info(logContext, '/conf/config.properties: endpoint not found...'.red);
process.exit(1);
}
let doAuto = false;
if (fs.existsSync('./conf/config.json')) {
const config = require('./conf/config.json');
global.config = config;
} else {
doAuto = true;
}
if (doAuto) {
logContext.op = 'Index.MappingTool';
logger.info(logContext, '---------------- MAPPING TOOL ----------------');
let loadingBar;
loadingBar = setInterval(function() {
process.stdout.write('.');
}, 3000);
const spawn = require('child_process').spawn;
var args = [];
try {
if (userName != 0 && password != 0) {
args = ['-jar', 'mapping_tool.jar', '-e', endpointUrl, '-f', 'conf/config.properties', '-u', userName, '-p', password];
} else {
args = ['-jar', 'mapping_tool.jar', '-e', endpointUrl, '-f', 'conf/config.properties'];
}
var child = spawn('java', args);
child.stdout.on('data', function (data) {
console.log('[MAPPING TOOL]: ' + data);
});
child.on('exit', function (code) {
console.log('child process exited with code ' + code);
if(code != 0) {
logger.error(
logContext,
'There is a problem with automatic configuration. Loading old configuration (if exists)...' +
code
);
} else {
logger.info(
logContext,
'Automatic configuration successfully created. Loading new configuration...'
);
const config = require('./conf/config.json');
global.config = config;
run.run();
server.start();
}
});
} catch (ex) {
clearInterval(loadingBar);
logger.info(
logContext,
'There is a problem with automatic configuration. Loading old configuration (if exists)...' + ex
);
}
module.exports = child;
} else {
run.run();
server.start();
}
} catch (ex) {
var logger = require('logops');
logger.error(ex);
logger.error(logContext, 'Generic error: closing application...'.red);
process.exit(1);
}