-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropagate.js
91 lines (72 loc) · 2.58 KB
/
propagate.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
module.exports = class Propagate {
constructor(server, fromDevice, toDevice) {
this.server = server;
this.serverName = this.server.httpServer.zetta.serverName;
this.fromDevice = fromDevice;
this.toDevice = toDevice;
// the "server" is assumed to be the local hub if it is not defined
if (typeof this.fromDevice.server == "undefined") {
this.fromDevice.server = this.serverName;
}
if (typeof this.toDevice.server == "undefined") {
this.toDevice.server = this.serverName;
}
this.startObservers();
}
startObservers() {
// if the from or to device is on the local server, then leave off the .from() clause
const fromDeviceQuery = (this.serverName == this.fromDevice.server) ?
this.server.where({name:this.fromDevice.name}) :
this.server.from(this.fromDevice.server).where({name:this.fromDevice.name});
const toDeviceQuery = (this.serverName == this.toDevice.server) ?
this.server.where({name:this.toDevice.name}) :
this.server.from(this.toDevice.server).where({name:this.toDevice.name});
this.server.observe([fromDeviceQuery, toDeviceQuery], (from, to) => {
from.streams[this.fromDevice.property].on('data', (message) => {
this.toDevice.transitions.forEach((trans, index) => {
// if the receiving transition wants to use the message, tack it on to the end of the argument array
if (trans.acceptsMessage) {
to.call(trans.transition, ...trans.arguments, message.data);
}
else {
to.call(trans.transition, ...trans.arguments);
}
});
});
});
}
}
/*
config.json example
{
"connections":[
{
"fromDevice":{
"server":"shop",
"name":"NorthShopSwitch",
"property":"level"
},
"toDevice":{
"server":"shop",
"name":"NorthShopLight",
"transitions": [
{"transition":"toggle-output", "arguments":[], "acceptsMessage":false}
]
}
},
{
"fromDevice":{ // the "server" is assumed to be the local hub if it is not defined
"name":"SouthShopSwitch",
"property":"level"
},
"toDevice":{ // the "server" is assumed to be the local hub if it is not defined
"name":"SouthShopLight",
"transitions": [
{"transition":"toggle-output", "arguments":[], "acceptsMessage":false}
// acceptsMessage means whether or not the transition can use the sender's message
]
}
}
]
}
*/