This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
failighting-modules.js
171 lines (156 loc) · 4.76 KB
/
failighting-modules.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class failDevice {
constructor(ip, name, channels) {
this.displayname = name;
this.ip = ip;
this.port = config.port;
this.channels = channels;
this.state = "offline"; //states: offline, opening, online, tx, rx
}
async init() {
await this.open();
this.name = this.displayname;
this.values = new Array(this.channels).fill(0);
}
updateState(state) {
this.state = state;
this.handlers.updateStateHandler(this.name, this.state);
}
async sendValues() {
let jsonMessage = "{\"values\":"+JSON.stringify(this.values)+"}";
this.send(jsonMessage);
}
async send(payload) {
if (this.state=="online") {
if (this.connection.readyState==this.connection.OPEN) {
this.updateState("tx");
try {
this.connection.send(payload);
}
catch (error) {
console.log("SEND ERROR: "+error);
}
}
else {
console.log(this.name+', '+this.ip+': stick not ready to splurt');
await this.close();
await this.open();
}
}
else {
await this.open();
}
}
async open() {
if (this.state=="offline") {
this.updateState("opening");
this.connection = new WebSocket("ws://"+this.ip+":"+this.port+"/");
this.connection.addEventListener("open", () => {
console.log(this.name+", "+this.ip+": CONNECTION OPENED");
this.updateState("online");
this.send("{\"info\":\"\"}");
});
this.connection.addEventListener("close", () => {
this.updateState("offline");
this.values.fill(0);
this.handlers.updateValuesHandler(this);
});
this.connection.addEventListener("error", (e) => {
console.log(this.name+", "+this.ip+": ERROR");
this.updateState("offline");
});
this.connection.addEventListener("message", (message) => {
this.updateState("rx");
try {
this.msg = JSON.parse(message.data);
}
catch(error) {
console.log("JSON PARSE ERROR " + this.ip + " - " + message.data);
}
if(this.msg.values) {
for (var i=0; i<this.values.length; i++) {
this.values[i] = this.msg.values[i];
}
this.handlers.updateValuesHandler(this);
}
this.updateState("online");
});
return 1;
}
else {
return 0;
}
}
async close() {
this.connection.close();
this.updateState("offline");
console.log(this.name+', '+this.ip+': connection closed');
}
}
class failScene {
constructor(scenedata) {
this.name = scenedata.name;
this.group = scenedata.group;
this.scenedevices = scenedata.devices;
if (typeof(scenedata.fade) !== 'undefined') {
this.fadeduration = scenedata.fade;
}
else {
this.fadeduration = config.fade_default;
}
//gather involved devices
this.realdevices = {};
for (let i=0; i<this.scenedevices.length; i++) {
for (let j=0; j<devices.length; j++) {
if (this.scenedevices[i].name == devices[j].name) {
this.realdevices[i] = devices[j];
}
}
}
};
async trigger() {
//call fade per device
for (let i=0; i<this.scenedevices.length; i++) {
if(this.realdevices[i].values.length == this.scenedevices[i].values.length) {
this.fade(this.realdevices[i], this.scenedevices[i], this.fadeduration);
}
else {
console.log("SCENE: "+this.name+" - DEVICE: "+this.realdevices[i].name+" - ERROR: tried to set keyframe with non-matching value count. ignoring device. you can ignore this too if you dont care.")
}
}
};
async fade(device, scene, duration) {
//calculate frame count
let frames = Math.floor(duration * config.max_fps/1000);
//instantly set new value if fade length <= 1 frame
if (frames<=1) {
this.changed = false;
for (let j=0; j<device.values.length; j++) {
if ((scene.values[j] != -1) && (device.values[j] != scene.values[j])) {
this.changed = true;
device.values[j] = scene.values[j];
};
};
if (this.changed) {
device.sendValues();
}
}
//fade!
else {
let startvalues = device.values.concat();
for (let i=0; i<frames; i++) {
this.changed = false;
for (let j=0; j<device.values.length; j++) {
//if channel participates (!= -1) and is not already at target value, set device value to scene value
if ((scene.values[j] != -1) && (device.values[j] != scene.values[j])) {
this.changed = true;
device.values[j] = einterp(startvalues[j], scene.values[j], (i+1)/frames);
};
};
if (this.changed) {
device.sendValues();
}
await sleep(1000/config.max_fps);
}
}
};
};