-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.js
87 lines (77 loc) · 2.43 KB
/
variables.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
module.exports = {
initVars: (instance) => {
instance.variables = [
{ variableId: 'modelName', name: 'Device Model Name' },
{ variableId: 'runMode', name: 'Device Run Mode' },
{ variableId: 'error', name: 'Device Status' },
]
instance.setVariableDefinitions(instance.variables)
},
// Get info from a connected console
getVars: (instance) => {
instance.sendCmd('devinfo productname') // Request Device Model
instance.sendCmd('devstatus runmode') // Request Run Mode
instance.sendCmd('devstatus error') // Request error status
},
setVar: (instance, msg) => {
switch (msg.Action) {
case 'devinfo': {
if (msg.Address == 'productname') {
if (instance.getVariableValue('modelName') == '') {
instance.log('info', `Device Model is ${msg.Val}`)
}
instance.setVariableValues({ modelName: msg.Val })
}
break
}
case 'devstatus': {
switch (msg.Address) {
case 'runmode':
instance.setVariableValues({ runMode: msg.Val })
break
case 'error':
instance.setVariableValues({ error: msg.Val })
break
}
break
}
}
},
fbCreatesVar: (instance, cmd, data) => {
const paramFuncs = require('./paramFuncs.js')
let rcpCmd = paramFuncs.findRcpCmd(cmd.Address)
if (rcpCmd.Type == 'mtr') {
data = data - 126
if (rcpCmd.Pickoff && cmd.Y > 0) {
cmd.Y = rcpCmd.Pickoff.split('|')[cmd.Y - 1] || undefined
}
}
if (rcpCmd.Type == 'integer' || rcpCmd.Type == 'freq') {
data = data == -32768 ? '-Inf' : data / rcpCmd.Scale
}
if (cmd.createVariable) {
// Auto-create a variable
let cmdName = rcpCmd.Address.slice(rcpCmd.Address.indexOf('/') + 1).replace(/\//g, '_')
let varName = `V_${cmdName}`
varName = varName + (cmd.X ? `_${cmd.X}` : '')
varName = varName + (cmd.Y ? `_${cmd.Y}` : '')
let varToAdd = { variableId: varName, name: 'Auto-Created Variable' }
let varIndex = instance.variables.findIndex((i) => i.variableId === varToAdd.variableId)
// Add new Auto-created variable and value
if (varIndex == -1) {
instance.variables.push(varToAdd)
instance.setVariableDefinitions(instance.variables)
}
let value = {}
value[varName] = data
instance.setVariableValues(value)
} else {
const reg = /@\(([^:$)]+):custom_([^)$]+)\)/
let hasCustomVar = reg.exec(cmd.Val)
if (hasCustomVar) {
// Set a custom variable value using @ syntax
instance.setCustomVariableValue(hasCustomVar[2], data)
}
}
},
}