-
Notifications
You must be signed in to change notification settings - Fork 11
/
ads-symbols.js
84 lines (73 loc) · 2.46 KB
/
ads-symbols.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
module.exports = function (RED) {
'use strict'
var util = require('util')
var adsHelpers = require('./ads-helpers')
var debug = require('debug')('node-red-contrib-ads:adsSymbolsNode')
function adsSymbolsNode(config) {
RED.nodes.createNode(this, config)
var node = this
node.adsDatasource = RED.nodes.getNode(config.datasource)
if (node.adsDatasource) {
node.adscfg = {
data: config.data,
force: config.force || false
}
debug('config:',node)
node.onData = function (data,topic){
debug('onData:','node.id',node.id,'node.type',node.type,'data',data)
const msg = {
payload: data
}
if (String(topic).length > 0) {
msg.topic = topic
}
node.send(msg)
}
this.on("input", function(msg) {
debug('input:','node.id',node.id,'node.type',node.type,'data',msg)
var call = node.adsDatasource.getDatatyps
var topic = msg.topic
// overwrite default msg.topic by value in msg.config.topic (if existing)
if (typeof (msg.config) !== 'undefined' && typeof (msg.config.topic) !== 'undefined' ) {
topic = msg.config.topic
// overwrite default msg.topic by value in topic property (if used)
} else if (String(config.topic).length > 0 ){
topic = config.topic
// else -> keep original msg.topic
}
if (node.adscfg.data == 'SYMBOLES') {
call = node.adsDatasource.getSymbols
}
var ask = []
if (typeof msg.payload === 'string') {
ask.push(adsHelpers.wildcardToRegExp(msg.payload.toUpperCase()))
} else if (Array.isArray(msg.payload)) {
msg.payload.map(function(p){
if (typeof p === 'string') {
ask.push(adsHelpers.wildcardToRegExp(p.toUpperCase()))
}
})
}
call(node.adscfg.force, function (data) {
var out = []
if (ask.length > 0){
ask.map(function(m){
data.map(function(d){
if (d.name.match(m)) {
out.push(d)
}
})
})
} else {
out = data
}
node.onData(out,topic)
})
})
node.on('close', function () {
debug('close:','node.id',node.id,'node.type',node.type)
})
}
}
RED.nodes.registerType('ADS Symbols', adsSymbolsNode)
}