-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert_server_configuration_to_simulation.js
225 lines (206 loc) · 7.34 KB
/
convert_server_configuration_to_simulation.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
downloadJson({
fileStore: 'default',
filePath: 'export.json',
excludeFields: [
'publishers',
'publishedPoints',
'eventHandlers',
'mailingLists',
'excel-reports',
'excel-report-templates',
'advancedSchedules',
'advancedScheduleCalendarRuleSets',
'maintenanceEvents',
'virtualSerialPorts',
'BACnetLocalDevices',
'security-sites',
'security-doors',
'security-boards',
'security-cameras',
'security-recordings',
'security-door-groups',
'security-users',
'security-cards'
],
/*
for reference not being used (use excludeFields)
includeFields: [
'custom-xx',
'systemSettings',
'roles',
'users',
'permissions',
'dataSources',
'dataPoints',
'jsonData',
'watchLists',
'sstGlobalScripts'
], */
excludeSystemSettings: [
'licenseAgreementVersion',
'cloudConnect.',
'jwt.',
'publiclyResolvableBaseUrl',
'instanceDescription',
'emailSmtpHost',
'emailSmtpUsername',
'emailFromName',
'emailFromAddress',
'bacnet.localDeviceList'
],
excludeDsTypes: ['INTERNAL', 'VIRTUAL'],
keepDsTypes: ['META', 'SCRIPTING'],
dsEnabled: false, //true, false, null (keep original states)
ignoreMissingDpContext: true,
excludeCustom: { //custom exclusion for tables with xid
roles: ["superadmin", "user", "anonymous"],
// watchLists: ['WL_e924fa26-e7ad-485b-9744-9e15170fdac4']
}
});
/**
* You dont need to edit below here!
*/
function readJson(fileStore, filePath) {
const JavaString = Java.type('java.lang.String');
const Files = Java.type('java.nio.file.Files');
const path = services.fileStoreService.getPathForRead(fileStore, filePath);
const fileString = new JavaString(Files.readAllBytes(path), 'UTF-8');
return JSON.parse(fileString);
}
function downloadJson(options) {
const result = convertToVirtual(options);
const filename = 'import.json';
response.setContentType('application/json');
response.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
print(result);
}
function convertToVirtual(options) {
const POINT_LOCATORS = {
'BINARY': {
dataType: 'BINARY',
changeType: {
type: 'ALTERNATE_BOOLEAN',
startValue: "false"
},
settable: true
},
'MULTISTATE': {
dataType: 'MULTISTATE',
changeType: {
type: 'INCREMENT_MULTISTATE',
roll: true,
values: [0, 1, 2, 3, 4],
startValue: '0'
},
settable: true
},
'NUMERIC': {
dataType : 'NUMERIC',
changeType: {
type: 'BROWNIAN',
max: 100.0,
maxChange: 0.5,
min: 0.0,
startValue: '50'
},
settable:true
},
'ALPHANUMERIC': {
dataType: 'ALPHANUMERIC',
changeType: {
type: 'NO_CHANGE',
startValue: "abcd"
},
settable: true
}
};
const VIRTUAL_DS_TEMPLATE = {
"type": "VIRTUAL",
"polling": true,
"useCron": false,
"quantize": false,
"purgePeriod": 1,
"purgeType": "YEARS",
"purgeOverride": false,
"alarmLevels": {
"POLL_ABORTED": "URGENT"
}
};
const data = readJson(options.fileStore, options.filePath);
// exclude fields
for (const field of options.excludeFields) {
delete data[field];
}
// exclude custom Xids
Object.keys(options.excludeCustom).forEach(field => {
if (data[field]) {
data[field] = data[field].filter(item => !options.excludeCustom[field].includes(item.xid));
}
});
// exclude SystemSettings
if (data.systemSettings) {
for (const prefix of options.excludeSystemSettings) {
Object.keys(data.systemSettings)
.filter(key => key.startsWith(prefix))
.forEach(s => delete data.systemSettings[s]);
}
}
if (data.dataSources && data.dataPoints) {
const toKeepDs = data.dataSources.filter(ds => options.keepDsTypes.includes(ds.type));
const toKeepDSXids = toKeepDs.map(ds => ds.xid);
const toKeepDp = data.dataPoints.filter(dp => toKeepDSXids.includes(dp.dataSourceXid));
// Keep dataSource types
for (let ds of toKeepDs) {
if (options.dsEnabled !== null) ds.enabled = options.dsEnabled;
if (ds.context && ds.context.some(x => x.dataPointXid == null)) {
if (!options.ignoreMissingDpContext) {
print("Context point missing " + ds.xid);
print(JSON.stringify(ds.context));
return;
}
ds.context = ds.context.filter(x => x.dataPointXid !== null);
}
}
// Keep dataPoints of those dataSources
for (let dp of toKeepDp) {
if (dp.pointLocator.context && dp.pointLocator.context.some(x => x.dataPointXid == null)) {
if (!options.ignoreMissingDpContext) {
print("Context point missing " + dp.xid);
print(JSON.stringify(dp.pointLocator.context));
return;
}
dp.pointLocator.context = dp.pointLocator.context.filter(x => x.dataPointXid !== null);
}
}
// Convert rest of dataSources and dataPoints to virtual
data.dataSources = data.dataSources.filter(ds => !options.keepDsTypes.includes(ds.type) && !options.excludeDsTypes.includes(ds.type));
const dataSourceXids = data.dataSources.map(ds => ds.xid);
data.dataPoints = data.dataPoints.filter(dp => dataSourceXids.includes(dp.dataSourceXid));
const virtualDs = [];
for (let ds of data.dataSources) {
const converted = JSON.parse(JSON.stringify(VIRTUAL_DS_TEMPLATE));
converted.enabled = options.dsEnabled !== null ? options.dsEnabled : ds.enabled;
converted.xid = ds.xid;
converted.name = ds.name;
converted.data = ds.data;
converted.readPermission = ds.readPermission;
converted.editPermission = ds.editPermission;
converted.updatePeriods = ds.updatePeriods || 1;
converted.updatePeriodType = ds.updatePeriodType || 'MINUTES';
virtualDs.push(converted);
}
for (const dp of data.dataPoints) {
const dataType = dp.pointLocator.dataType;
if (dataType === 'IMAGE') {
throw new Error(`Datapoint ${dp.xid} has data type IMAGE which is unsupported by virtual data sources`);
}
dp.pointLocator = POINT_LOCATORS[dataType];
if (dataType === 'MULTISTATE' && dp.textRenderer.type === 'MULTISTATE') {
dp.pointLocator.changeType.values = dp.textRenderer.multistateValues.map(msv => msv.key);
}
}
data.dataSources = [...virtualDs, ...toKeepDs];
data.dataPoints = [...data.dataPoints, ...toKeepDp];
}
return JSON.stringify(data, null, 2);
}