-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
204 lines (174 loc) · 8.84 KB
/
main.js
File metadata and controls
204 lines (174 loc) · 8.84 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const { InstanceBase, InstanceStatus, runEntrypoint } = require('@companion-module/base')
const { getActionDefinitions } = require('./actions.js')
const { getConfigFields, extractChannelsFromField, loadScene } = require('./config.js')
const { init_variables, update_variables } = require('./variables.js')
const { SCENE_COUNT } = require('./constants.js')
const { UpgradeScripts } = require('./upgrades.js')
class MathMatrixInstance extends InstanceBase {
async init(config) {
this.config = config
this.setActionDefinitions(getActionDefinitions(this))
this.bitmask = 0
this.primary_dimmer = 100
this.secondary_dimmer = 100
// set up data with 512 channels and zero as a value
this.data = {}
for (let i = 1; i <= 512; i++) {
this.data[i] = 0
}
await this.configUpdated(config)
init_variables(this)
this.updateStatus(InstanceStatus.Ok)
}
async configUpdated(config) {
this.config = config
// extract channels we act on
this.output_channels = extractChannelsFromField(this, 'output_channels')
console.log(`output_channels: ${this.output_channels}`)
// make sure we have the non-fade-channels-pre and non-fade-channels-post keys
if (!config.hasOwnProperty('non-fade-channels-pre')) {
config['non-fade-channels-pre'] = []
}
if (!config.hasOwnProperty('non-fade-channels-post')) {
config['non-fade-channels-post'] = []
}
// sort the channels 1-512 into pre-fade, post-fade, and fade
this.pre_fade_channels = extractChannelsFromField(this, 'non-fade-channels-pre')
this.post_fade_channels = extractChannelsFromField(this, 'non-fade-channels-post')
this.fade_channels = []
for (let i = 1; i <= 512; i++) {
if (!this.pre_fade_channels.includes(i) && !this.post_fade_channels.includes(i)) {
this.fade_channels.push(i)
}
}
// remove channels that are not in the output channels
this.pre_fade_channels = this.pre_fade_channels.filter(channel => this.output_channels.includes(channel))
this.post_fade_channels = this.post_fade_channels.filter(channel => this.output_channels.includes(channel))
this.fade_channels = this.fade_channels.filter(channel => this.output_channels.includes(channel))
console.log('pre-fade', this.pre_fade_channels)
console.log('post-fade', this.post_fade_channels)
console.log('fade', this.fade_channels)
// do the same for primary and secondary dimmer channels
if (!config.hasOwnProperty('secondary-dimmer-channels')) {
config['secondary-dimmer-channels'] = []
}
this.secondary_dimmer_channels = extractChannelsFromField(this, 'secondary-dimmer-channels')
// remove any channels that are in the pre-fade, post-fade channels or the secondary dimmer channels
this.primary_dimmer_channels = this.fade_channels.filter(channel => !this.pre_fade_channels.includes(channel) && !this.post_fade_channels.includes(channel) && !this.secondary_dimmer_channels.includes(channel))
// remove any channels that are in the pre-fade, post-fade channels as these are handled differently
this.secondary_dimmer_channels = this.secondary_dimmer_channels.filter(channel => !this.pre_fade_channels.includes(channel) && !this.post_fade_channels.includes(channel))
console.log('primary dimmer', this.primary_dimmer_channels)
console.log('secondary dimmer', this.secondary_dimmer_channels)
// load scenes
this.scenes = {}
for (let i = 1; i <= SCENE_COUNT; i++) {
loadScene(this, i)
}
console.log(this.scenes)
// Generate wiki documentation
this.generateWikiDocumentation()
}
generateWikiDocumentation() {
let doc = 'Konfigurationsdokumentation - Generic Bitmask Dimmer\n\n'
// Kanäle-Übersicht
doc += '## Kanäle Übersicht\n\n'
doc += `**Gesamtzahl Kanäle**: ${this.output_channels.length}\n\n`
doc += '### Kanäle nach Typ\n\n'
doc += `- **Pre-Fade Kanäle** (nicht gedimmt): ${this.pre_fade_channels.length} - ${this.pre_fade_channels.length > 0 ? this.pre_fade_channels.join(', ') : 'keine'}\n`
doc += `- **Post-Fade Kanäle** (nicht gedimmt): ${this.post_fade_channels.length} - ${this.post_fade_channels.length > 0 ? this.post_fade_channels.join(', ') : 'keine'}\n`
doc += `- **Secondary Dimmer Kanäle** (mit Secondary Dimmer): ${this.secondary_dimmer_channels.length} - ${this.secondary_dimmer_channels.length > 0 ? this.secondary_dimmer_channels.join(', ') : 'keine'}\n`
doc += `- **Primary Dimmer Kanäle** (mit Primary Dimmer): ${this.primary_dimmer_channels.length} - ${this.primary_dimmer_channels.length > 0 ? this.primary_dimmer_channels.join(', ') : 'keine'}\n\n`
// Szenen-Dokumentation
doc += '## BitSzenen\n\n'
let hasAnyScene = false
for (let i = 1; i <= SCENE_COUNT; i++) {
// Skip empty scenes
if (!this.scenes[i] || Object.keys(this.scenes[i]).length === 0) {
continue
}
hasAnyScene = true
const sceneName = this.config[`scene-${i}-name`] || `Szene ${i}`
const bitValue = 1 << (i - 1)
doc += `### Szene ${i}: ${sceneName}\n`
doc += `**Bit-Wert**: ${bitValue}\n\n`
doc += `| Kanal | Wert | Typ |\n`
doc += `|-------|------|-----|\n`
// Get sorted channel keys for this scene
const channelKeys = Object.keys(this.scenes[i])
.map(Number)
.sort((a, b) => a - b)
channelKeys.forEach(channel => {
const value = this.scenes[i][channel]
let type = 'Standard (Primary Dimmer)'
if (this.pre_fade_channels.includes(channel)) {
type = 'Pre-Fade (nicht gedimmt)'
} else if (this.post_fade_channels.includes(channel)) {
type = 'Post-Fade (nicht gedimmt)'
} else if (this.secondary_dimmer_channels.includes(channel)) {
type = 'Secondary Dimmer'
}
doc += `| ${channel} | ${value} | ${type} |\n`
})
doc += '\n'
}
if (!hasAnyScene) {
doc += '*Keine Szenen konfiguriert.*\n\n'
}
doc += '```'
// Log the documentation
console.log('\n' + '='.repeat(80))
console.log('WIKI-DOKUMENTATION:')
console.log('='.repeat(80))
console.log(doc)
console.log('='.repeat(80) + '\n')
}
// When module gets deleted
async destroy() {
this.terminate()
}
// Return config fields for web config
getConfigFields() {
return getConfigFields()
}
terminate() {
delete this.data
}
updateVariables() {
console.log('updating variables')
// loop through the scenes and check which match the bitmask
// if there is a match, update the data
let new_data = {}
for (let channel = 1; channel <= 512; channel++) {
// check if channel is in output channels
if (this.output_channels.includes(channel)) {
new_data[channel] = 0
}
}
for (let scene = 1; scene < SCENE_COUNT; scene++) {
if (!this.scenes[scene]) {
continue
}
if ((this.bitmask & (1 << (scene - 1))) > 0) {
console.log('adding scene', scene, '(', (1 << (scene - 1)), ')')
// get the keys of new_data, those are the only channels we need to update
Object.keys(new_data).forEach(channel => {
// if the channel is in the scene, update it
if (this.scenes[scene][channel]) {
// if the channel is in a non-fade channel, just set it
if (this.pre_fade_channels.includes(Number(channel)) || this.post_fade_channels.includes(Number(channel))) {
new_data[channel] = Math.max(this.scenes[scene][channel], new_data[channel])
} else if (this.secondary_dimmer_channels.includes(Number(channel))) {
new_data[channel] = Math.max(Math.floor(this.scenes[scene][channel] * this.secondary_dimmer / 100), new_data[channel])
} else {
new_data[channel] = Math.max(Math.floor(this.scenes[scene][channel] * this.primary_dimmer / 100), new_data[channel])
}
}
})
}
}
this.data = new_data
console.log('resulting data', this.data)
update_variables(this)
}
}
runEntrypoint(MathMatrixInstance, UpgradeScripts)