-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrack-mango-details.js
More file actions
224 lines (212 loc) · 7.28 KB
/
track-mango-details.js
File metadata and controls
224 lines (212 loc) · 7.28 KB
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
/**
* Script to track Mango, Modules, and OS details
* Last update Sept 2025
*
* Java Details
* - Java version
*
* Mango Details
* - Mango Version
* - UDMI Version
*
* OS Details
* - OS Type
* - OS Release
* - OS Distributor
*
* Note: For OS linux is necessary install lsb_release
*/
var Common = Java.type('com.serotonin.m2m2.Common');
var mangoDataPath = Common.envProps.getProperty('paths.data').toString();
var Runtime = Java.type('java.lang.Runtime');
var rt = Runtime.getRuntime();
var System = Java.type('java.lang.System');
var ModuleRegistry = Java.type('com.serotonin.m2m2.module.ModuleRegistry');
var InputStreamReader = Java.type('java.io.InputStreamReader');
var BufferedReader = Java.type('java.io.BufferedReader');
for (var i in EXTERNAL_POINTS) {
var configPoint = EXTERNAL_POINTS[i];
var configPointWrapper = configPoint.getDataPointWrapper();
if (!configPointWrapper.enabled) {
continue;
}
var locatorType = configPointWrapper.tags.locatorType;
if (!locatorType) {
LOG.error('Point ' + configPointWrapper.name + ' missing locatorType tag.');
configPoint.set('Missing locatorType tag.');
continue;
}
var locatorValue = configPointWrapper.tags.locatorValue;
if (!locatorValue) {
LOG.error('Point ' + configPointWrapper.name + ' missing locatorValue tag.');
configPoint.set('Missing locatorValue tag.');
continue;
}
LOG.debug('Processing point ' + configPointWrapper.name + ' (' + locatorType + ':' + locatorValue + ')');
switch (locatorType) {
case 'os':
configPoint.set(processOsLocatorType(locatorValue));
break;
case 'mango.version':
configPoint.set(processMangoVersionLocatorType(locatorValue));
break;
case 'java':
configPoint.set(processJavaLocatorType(locatorValue));
break;
case 'mango.properties':
configPoint.set(processMangoPropertiesLocatorType(locatorValue));
break;
case 'start-options':
configPoint.set(processStartOptionsLocatorType(locatorValue));
break;
default:
LOG.error('Unexpected locatorType: ' + locatorType);
configPoint.set('Unexpected locatorType: ' + locatorType);
}
}
function processMangoVersionLocatorType(locatorValue) {
//Find the locatorValue for this Mango Version point in the ModuleRegistry
var module = ModuleRegistry.getModule(locatorValue);
if (!module) {
LOG.error('Version Locator module ' + locatorValue + ' not found.');
return 'Module ' + locatorValue + ' not found.';
}
var versionValue
try {
//This method is not compatible in Mango 5.2 and earlier
versionValue = module.getVersion();
}
catch (err) {
LOG.debug(module.version);
//This method is compatible in Mango 5.2 and earlier
versionValue = ModuleRegistry.getModule(locatorValue).version;
}
LOG.debug(ModuleRegistry.getModule(locatorValue).version);
if (versionValue) {
LOG.debug('Version Locator ' + locatorValue + ' = ' + versionValue);
return versionValue;
}
else {
LOG.error('Version Locator ' + locatorValue + ' not found.');
return locatorValue + ' not found.';
}
}
function processJavaLocatorType(locatorValue) {
var javaValue = System.getProperty(locatorValue).toString();
if (javaValue) {
LOG.debug('Java Locator ' + locatorValue + ' = ' + javaValue);
return javaValue;
}
else {
LOG.error('Java Locator ' + locatorValue + ' not found.');
return locatorValue + ' not found.';
}
}
function processMangoPropertiesLocatorType(locatorValue) {
var mangoPropertyValue = Common.envProps.getProperty(locatorValue).toString();
if (mangoPropertyValue) {
LOG.debug('Mango.Properties Locator ' + locatorValue + ' = ' + mangoPropertyValue);
return mangoPropertyValue;
}
else {
LOG.error('Mango.Properties Locator ' + locatorValue + ' not found.');
return locatorValue + ' not found.';
}
}
var osConfigParamKeys;
var osConfigParamValues;
function processOsLocatorType(locatorValue) {
//Check if the command has already been executed before running it
if (!osConfigParamKeys) {
var osConfigLines = runCommand('lsb_release -a');
osConfigParamKeys = new Array();
osConfigParamValues = new Array();
osConfigLines.forEach(parseOsConfigLines);
}
//Find the locatorValue for this OS point in osConfigParamKeys
var osConfigValue = osConfigParamValues[osConfigParamKeys.indexOf(locatorValue)];
if (osConfigValue) {
LOG.debug('OS Locator ' + locatorValue + ' = ' + osConfigValue);
return osConfigValue;
}
else {
LOG.error('OS Locator ' + locatorValue + ' not found.');
return locatorValue + ' not found.';
}
}
function parseOsConfigLines(value) {
LOG.debug('Processing OS config line: ' + value);
var lineParts = value.split(':');
if (lineParts.length <= 1) {
LOG.debug('Config line missing separator `:` ' + lineParts.toString());
} else {
osConfigParamKeys.push(lineParts[0].trim());
osConfigParamValues.push(lineParts[1].trim());
}
}
var startOptionsParams;
function processStartOptionsLocatorType(locatorValue) {
//Check if the command has already been executed before running it
if (!startOptionsParams) {
var startOptionsLines = runCommandArray(['grep', '-v', '^#\\|^$', mangoDataPath + '/start-options.sh']);
startOptionsParams = new Array();
startOptionsLines.forEach(parseStartOptionsLines);
}
//Find the locatorValue for this OS point in startOptionsParamKeys
var matchedValues = startOptionsParams.filter(strStartsWith);
LOG.debug('matchedValues: ' + matchedValues.toString());
function strStartsWith(value) {
return value.startsWith(locatorValue);
}
if (matchedValues.length > 0) {
LOG.debug('Start Options Locator ' + locatorValue + ' = ' + matchedValues[0]);
return matchedValues[0];
}
else {
LOG.error('Start Options Locator ' + locatorValue + ' not found.');
return locatorValue + ' not found.';
}
}
function parseStartOptionsLines(value) {
LOG.debug('Processing Start Options line: ' + value);
var lineParts = value.trim().split('-');
if (lineParts.length <= 1) {
LOG.debug('Config line missing separator `-` ' + lineParts.toString());
}
lineParts.forEach(pushPart);
function pushPart(value) {
//Only push parameters that start with X
//Remove quotation marks
if (value.trim().substring(0,1) == 'X') {
startOptionsParams.push(value.trim().replace('"', ''));
}
}
LOG.debug(lineParts.toString());
}
function runCommand(commandToRun) {
return runCommandArray(commandToRun.split(' '));
}
function runCommandArray(commandArrayToRun) {
LOG.debug('Executing command: ' + commandArrayToRun.toString());
var logErrors = true;
var process = rt.exec(commandArrayToRun);
var bReader = java.io.BufferedReader;
var iStreamReader = java.io.InputStreamReader;
var line = String;
var commandOutputLines = new Array();
var returnValue = process.waitFor() === 0;
iStreamReader = new java.io.InputStreamReader(process.getInputStream());
bReader = new java.io.BufferedReader(iStreamReader);
while ((line = bReader.readLine()) != null) {
LOG.debug(line);
commandOutputLines.push(line);
}
if (logErrors) {
iStreamReader = new java.io.InputStreamReader(process.getErrorStream());
bReader = new java.io.BufferedReader(iStreamReader);
while ((line = bReader.readLine()) != null) {
LOG.error(line);
}
}
return commandOutputLines;
}