forked from PLCHome/node-red-contrib-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ads-symbols.js
77 lines (66 loc) · 2.08 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
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,
topic: config.topic ||''
}
node.adscfg.hasTopic = node.adscfg.topic.length > 0
debug('config:',node)
node.onData = function (data){
debug('onData:','node.id',node.id,'node.type',node.type,'data',data)
const msg = {
payload: data
}
if (node.adscfg.hasTopic) {
msg.topic = node.adscfg.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
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)
})
})
node.on('close', function () {
debug('close:','node.id',node.id,'node.type',node.type)
})
}
}
RED.nodes.registerType('ADS Symbols', adsSymbolsNode)
}