forked from PLCHome/node-red-contrib-velux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvelux-scenes.js
125 lines (113 loc) · 4.12 KB
/
velux-scenes.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
module.exports = function (RED) {
'use strict'
var util = require('util')
var debug = require('debug')('node-red-contrib-velux:velux-scenes')
function veluxScenes(config) {
RED.nodes.createNode(this, config)
var node = this
node.veluxDatasource = RED.nodes.getNode(config.datasource)
if (node.veluxDatasource) {
node.hasTopic = (config.topic||'').length > 0
debug('config:',config)
//node.veluxDatasource.subscribeScenes(node)
node.getID = function(){
return config.index
}
node.getName = function(){
return node.veluxDatasource.nodeGetName(node.getID)
}
node.getVelocity = function(){
return node.veluxDatasource.getVelocityTagByName(config.velocity)
}
var timer = null
node.runTimer = function(seconds){
clearInterval(timer)
timer = setInterval(()=>{
//n.status({fill:fillSystem,shape:shapeSystem,text:textSystem})
node.status({text:""+--seconds+"sec"})
if (seconds<1) {
clearInterval(timer)
}
},1000)
}
node.publish = function(data){
debug('node.send:',(!(!data)))
if (data) {
if (data.apiText === "GW_COMMAND_REMAINING_TIME_NTF") {
node.runTimer(data.seconds)
}
if (data.apiText === "GW_COMMAND_RUN_STATUS_NTF") {
clearInterval(timer)
node.status({text:data.runStatusText})
}
var msg={}
if (node.hasTopic) {
msg.topic = config.topic
} else {
msg.topic = node.name
}
msg.payload = data
node.send(msg)
}
}
node.on("input", function(msg) {
debug('input:','msg',msg)
if (typeof msg.topic === 'string' && msg.topic !== '') {
debug('input:','msg.topic === string')
if (node.hasTopic && msg.topic == (config.topic||'')){
node.veluxDatasource.runScene(node,node.getID(),node.getVelocity())
.then((data)=>{node.publish(data)})
.catch((err)=>{node.error(util.format('Velux %s', err))})
return
}
var topicvals = msg.topic.split(":")
if ((topicvals[0]||'').trim()== 'velux') {
if ((topicvals[1]||'').trim()=='execute') {
var id = node.getID()
var velocity = node.getVelocity()
for (var i=1;i<topicvals.length;i++) {
if ((topicvals[i]||'').trim()=='id') {
i++
var text = (topicvals[i]||'')
debug('input:','id',text)
id = parseInt(text)
} else if ((topicvals[i]||'').trim()=='name') {
i++
var text = (topicvals[i]||'')
debug('input:','name',text)
id = node.veluxDatasource.getSceneByName(text)
} else if ((topicvals[i]||'').trim()=='velocityid') {
i++
var text = (topicvals[i]||'')
debug('input:','velocityid',text)
velocity = parseInt(text)
} else if ((topicvals[i]||'').trim()=='velocity') {
i++
var text = (topicvals[i]||'')
debug('input:','velocity',text)
velocity = node.veluxDatasource.getVelocityTagByName(text)
}
}
node.veluxDatasource.runScene(node,id,velocity)
.then((data)=>{node.publish(data)})
.catch((err)=>{node.error(util.format('Velux %s', err))})
}
return
}
if (msg.topic) return
} else {
node.veluxDatasource.runScene(node,node.getID(),node.getVelocity())
.then((data)=>{node.publish(data)})
.catch((err)=>{node.error(util.format('Velux %s', err))})
return
}
})
node.on('close', function (done) {
debug('close:')
//node.veluxDatasource.unsubscribeScenes(node)
done()
})
}
}
RED.nodes.registerType('Velux Scenes', veluxScenes)
}