-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
229 lines (204 loc) · 5.88 KB
/
index.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { InstanceBase, InstanceStatus, runEntrypoint, TCPHelper } from '@companion-module/base'
import { ConfigFields } from './config.js'
import { getActionDefinitions } from './actions.js'
import { getFeedbackDefinitions } from './feedbacks.js'
import { getPresetDefinitions } from './presets.js'
import { LoStatus, PresentationStatus} from './types.js'
class DocumentfoundationLibreofficeImpress extends InstanceBase {
async init(config) {
this.log('debug', 'Init')
this.config = config
this.slides = []
this.connectionStatus = LoStatus.Unconnected
this.presentationStatus = PresentationStatus.Unconnected
this.current_slide_id = 0
this.setActionDefinitions(getActionDefinitions(this))
this.setFeedbackDefinitions(getFeedbackDefinitions(this))
this.setPresetDefinitions(getPresetDefinitions(this))
await this.configUpdated(config)
}
async configUpdated(config) {
if (this.socket) {
this.socket.destroy()
delete this.socket
}
this.config = config
this.init_tcp()
this.init_variables()
}
async destroy() {
if (this.socket) {
this.socket.destroy()
} else {
this.updateStatus(InstanceStatus.Disconnected)
}
}
// Return config fields for web config
getConfigFields() {
return ConfigFields
}
init_tcp() {
if (this.socket) {
this.socket.destroy()
delete this.socket
}
this.updateStatus(InstanceStatus.Connecting)
if (this.config.host) {
this.socket = new TCPHelper(this.config.host, this.config.port)
this.socket.on('status_change', (status, message) => {
if ( status == InstanceStatus.Ok ) {
const sendBuf = Buffer.from('LO_SERVER_CLIENT_PAIR\nCompanion\n9876\n\n', 'latin1')
this.log('debug', 'Sending Connection request ')
this.socket.send(sendBuf)
} else {
this.updateStatus(status, message)
}
})
this.socket.on('error', (err) => {
if (this.connectionStatus != LoStatus.Unconnected) {
this.log('error', 'Network error: ' + err.message)
}
this.connectionStatus = LoStatus.Unconnected
this.PresentationStatus = PresentationStatus.Unconnected
this.checkFeedbacks('running')
this.updateStatus(InstanceStatus.ConnectionFailure, err.message)
})
this.socket.on('data', (data) => {
this.update_connection(data.toString())
})
} else {
this.updateStatus(InstanceStatus.BadConfig)
}
}
check_data(count, length, cmd) {
if ( count >= length) {
this.log("error", "Data missing for command:"+count+" "+length + cmd)
return false
}
return true
}
update_connection(data) {
data = data.split('\n')
this.log('debug', '<-'+ data)
let i = 0
const DataType = {
Default: 0,
Image: 1,
Notes: 2,
}
let dataStatus = DataType.Default
while (i < data.length) {
let cmd = data[i]
switch (cmd) {
case "":
break
case 'LO_SERVER_VALIDATING_PIN':
this.connectionStatus = LoStatus.WaitingForPairing
this.updateStatus(InstanceStatus.Connecting, "Waiting for Pairing")
break;
case "LO_SERVER_SERVER_PAIRED":
this.connectionStatus = LoStatus.Paired
this.updateStatus(InstanceStatus.Ok)
break;
case "LO_SERVER_INFO":
if (this.check_data(i+1, data.length, cmd)) {
this.setVariableValues({ lo_version: data[i+1] })
i++
}
break;
case "slideshow_finished":
this.presentationStatus = PresentationStatus.Stopped
this.checkFeedbacks('running')
break
case "slideshow_started":
this.presentationStatus = PresentationStatus.Running
this.checkFeedbacks('running')
if (this.check_data(i+2, data.length, cmd)) {
this.current_slide_id = Number(data[i+2])
this.setVariableValues({
total_slides: Number(data[i+1]),
slide: this.current_slide_id +1,
})
i += 2
}
break
case "slideshow_info":
if (this.check_data(i+1, data.length, cmd)) {
this.setVariableValues({presentation_name: data[i+1]})
i++
}
break
case "slide_preview":
if (this.check_data(i+1, data.length, cmd)) {
let img = ""
i++
while (i < data.length) {
if (data[i] == "") {
break
}
img += data[i]
i++
}
}
break
case "slide_notes":
if (this.check_data(i+2, data.length, cmd)) {
let slideNotes = ""
i++
const slideId = Number(data[i])
i++
while (i < data.length) {
if (data[i] == "") {
break
}
slideNotes += data[i]
i++
}
slideNotes = slideNotes.replaceAll("<html>","")
slideNotes = slideNotes.replaceAll("</html>","")
slideNotes = slideNotes.replaceAll("<body>","")
slideNotes = slideNotes.replaceAll("</body>","")
slideNotes = slideNotes.replaceAll("<br/>","\n")
this.slides[slideId] = {id: slideId, notes: slideNotes}
if (this.current_slide_id == slideId) {
this.setVariableValues({
notes: slideNotes
})
}
}
break
case "slide_updated":
if (this.check_data(i+1, data.length, cmd)) {
this.current_slide_id = Number(data[i+1])
this.setVariableValues({
slide: this.current_slide_id +1,
notes: this.slides[Number(data[i+1])]["notes"]
})
i++
}
break
default:
this.log('debug', 'Unknown Data: ' + data)
}
i++
}
}
init_variables() {
this.setVariableDefinitions([
{name: 'Libre Office Version', variableId: 'lo_version'},
{name: 'Presentation Name', variableId: 'presentation_name'},
{name: 'Total Slides', variableId: 'total_slides'},
{name: 'Current Slide', variableId: 'slide'},
{name: 'Current Notes', variableId: 'notes'},
])
this.setVariableValues({ lo_version: '' })
this.setVariableValues({ presentation_name: '' })
this.setVariableValues({ total_slides: 0})
this.setVariableValues({ slide: 0 })
this.setVariableValues({ notes: '' })
}
updateFeedbacks() {
return
}
}
runEntrypoint(DocumentfoundationLibreofficeImpress, [])