forked from PLCHome/node-red-contrib-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathads-out.js
57 lines (53 loc) · 1.91 KB
/
ads-out.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
module.exports = function (RED) {
'use strict'
var util = require('util')
var debug = require('debug')('node-red-contrib-ads:adsOutNode')
function adsOutNode(config) {
RED.nodes.createNode(this, config)
var node = this
node.adsDatasource = RED.nodes.getNode(config.datasource)
if (node.adsDatasource) {
debug('config:',config)
this.on("input", function(msg) {
debug('adsNotificationNode:','onAdsData:','node.id',node.id,'msg',msg)
var cfg = {
symname: config.varName,
adstype: config.varTyp,
bytelength: config.varSize,
timezone: config.timezone,
outValue: config.outValue,
topic: (config.topic||'')
}
if (typeof msg.config !== 'undefined') {
if (typeof msg.config.varName !== 'undefined') {
cfg.symname = msg.config.varName
}
if (typeof msg.config.varType !== 'undefined') {
cfg.adstype = msg.config.varType
}
if (typeof msg.config.varSize !== 'undefined') {
cfg.bytelength = msg.config.varSize
}
if (typeof msg.config.timezone !== 'undefined') {
cfg.timezone = msg.config.timezone
}
if (typeof msg.config.outProperty !== 'undefined') {
cfg.outValue = msg.config.outProperty
}
if (typeof msg.config.topic !== 'undefined') {
cfg.topic = msg.config.topic||''
}
}
cfg.hasTopic = cfg.topic.length > 0
debug('adsNotificationNode:','onAdsData:','node.id',node.id,'cfg',cfg)
var value = RED.util.getMessageProperty(msg,(cfg.outValue||'payload'))
if (value !== undefined && (!cfg.hasTopic || cfg.topic == msg.topic)) {
node.adsDatasource.write(node,cfg,value)
}
})
node.on('close', function () {
})
}
}
RED.nodes.registerType('ADS Output', adsOutNode)
}