-
Notifications
You must be signed in to change notification settings - Fork 1
/
dpram.js
223 lines (191 loc) · 7.82 KB
/
dpram.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
const utils = require('./utilities.js');
const fs = require('fs');
let addViewToContainer = utils.addViewToContainer,
createView = utils.createView,
getViewType = utils.getViewType;
let parse = (model, ui, configPath) => {
model.devices.forEach(device =>{
let dprams = findDPRAM(device);
let config = {}
dprams.forEach(dpram =>{
let filename = dpram.name + ".json"
let filepath = configPath + filename;
if(fs.existsSync(filepath)){
config = utils.loadJsonFile(filepath)
}
else{
let errorMsg = `No config found for ${dpram.name} DPRAM, expected to find ${filename}`
let errorInput = {
name: `error ${dpram.name}`,
value: errorMsg,
min: 0,
max: 0,
step: 0
}
let [references, optionsIndex] = addData(ui, device, errorInput)
errorView = createView("Error", "Text", "default", [references], [])
addViewToContainer(ui, errorView, dpram.name);
return;
}
if(!validate(config)){
let errorMsg = `Invalid config for ${dpram.name} DPRAM`
let errorInput = {
name: `error ${dpram.name}`,
value: errorMsg,
min: 0,
max: 0,
step: 0
}
let [references, optionsIndex] = addData(ui, device, errorInput)
errorView = createView("Error", "Text", "default", [references], [])
addViewToContainer(ui, errorView, dpram.name);
return;
}
let processingButtonReferences = []
config.groups.forEach((group, groupIndex) =>{
config.inputs.forEach(input => {
let [references, optionsIndex] = addData(ui, device, input, groupIndex)
let [viewType, viewVariant] = getViewType(input)
//let (references, optionsIndex) = getData(ui, input);
let inputName = input.name
if(Array.isArray(input))
inputName = input.map(x => x.name).join("_")
let view = createView(group + "_" + inputName, viewType, viewVariant, references, optionsIndex)
addViewToContainer(ui, view, group);
processingButtonReferences = processingButtonReferences.concat(references)
});
});
let [references, ] = getData(ui, dpram);
processingButtonReferences = references.concat(processingButtonReferences)
let option = createProcessingOption(ui, config, processingButtonReferences)
let optionIndex = addOption(ui, option)
let processingButton = createView("Process " + dpram.name, "ProcessingButton", "standard", processingButtonReferences, [optionIndex])
addViewToContainer(ui, processingButton, "Processing")
let processingOutputView = createView("Processing Output " + dpram.name, "Text", "standard", references, [])
addViewToContainer(ui, processingOutputView, "Processing")
});
});
}
let validate = (dpramConfig) => true;
let addOption = (ui, option) => {
return ui.options.push(option) - 1
}
let addData = (ui, device, input, nameId='') => {
let references = []
let optionsIndex = []
if(Array.isArray(input)) {
input.forEach((subInput, inputIndex) => {
[tempRef, tempOpt] = addData(ui, device, subInput, nameId)
references = references.concat(tempRef)
optionsIndex = optionsIndex.concat(tempOpt)
// let option
// if(inputIndex == 0){
// option = {
// data: [tempRef[0]],
// // This is essentially hardcoding since there is no reason for it to be the next data
// union: tempRef[0] + 1,
// }
// }
// else {
// option = {
// data: [tempRef[0]],
// noDisplay: true
// }
// }
// optionsIndex.push(addOption(ui,option))
})
return [references, optionsIndex]
}
let dpramRegister = {
name: (input.name + nameId),
type: "user-only",
device: device.name,
value: input.value,
properties: {
min: input.min,
max: input.max,
step: input.step
}
}
references.push(ui.data.push(dpramRegister) - 1)
return [references, optionsIndex]
}
let getData = (model, input) => {
let references = []
optionsIndex = []
if(Array.isArray(input)) {
input.forEach( subInput => {
[tempRef, tempOpt] = getData(model, subInput)
references = references.concat(tempRef)
optionsIndex = optionsIndex.concat(tempOpt)
})
return [references, optionsIndex]
}
references.push(model.data.findIndex(data => data.name == input.name));
optionsIndex.push(model.options.findIndex(option => option.name == input.name))
return [references, optionsIndex]
}
let createProcessingOption = (ui, dpram, dpramReferences) => {
let option = {
data: [dpramReferences[dpramReferences.length-1]],
processing: {
functions: dpram.processing.initFunctions,
inputs: [],
output: dpram.processing.output
}
}
dpram.groups.forEach((group, groupIndex) => {
dpram.processing.groupFunctions.forEach((func) => {
let tempFunc = {}
tempFunc.output_name = func.output_name.replace(/\${index}/g, groupIndex)
tempFunc.function = func.function.replace(/\${index}/g, groupIndex)
option.processing.functions = option.processing.functions.concat(tempFunc)
});
});
dpram.processing.inputs.forEach( (input) =>{
if(!input.name.includes("${index}")){
option.processing.inputs = option.processing.inputs.concat(input)
return
}
dpram.groups.forEach((group, groupIndex) => {
let numInputs = (dpramReferences.length - 1) / dpram.groups.length
let dpramInput = {}
if(Array.isArray(input.value)){
dpramInput = dpram.inputs[input.value[0]][input.value[1]]
}
else{
dpramInput = dpram.inputs[input.value]
}
let dataIndex = ui.data.findIndex((datum) => datum.name == (dpramInput.name + groupIndex));
let referencesIndex = dpramReferences.findIndex((ref) => ref == dataIndex)
let tempInput = {
name: input.name.replace(/\${index}/g, groupIndex),
type: input.type,
value: referencesIndex
}
option.processing.inputs = option.processing.inputs.concat(tempInput)
})
})
option.processing.functions = option.processing.functions.concat(dpram.processing.endFunctions)
return option
}
let findDPRAM = (device) =>{
dprams = []
device.registers.forEach(register =>{
if(register.dataType.dpram){
dprams.push(register);
}
});
return dprams;
}
let hasDPRAM = (model) => {
let result = false
model.devices.forEach(device =>{
let dprams = findDPRAM(device);
if(dprams.length > 0)
result = true
})
return result
}
exports.parse = parse;
exports.hasDPRAM = hasDPRAM;