Newer
Older
const {InstanceBase, Regex, runEntrypoint, InstanceStatus} = require('@companion-module/base')
const UpgradeScripts = require('./upgrades')
const UpdateActions = require('./actions')
const UpdateFeedbacks = require('./feedbacks')
const UpdateVariableDefinitions = require('./variables')
const ProjectUpdate = require("./receive/project_update");
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
isInitialized = false
// language=RegExp
wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$'
messageHandlers = {
'project-current': new ProjectUpdate()
};
currentProject = {}
constructor(internal) {
super(internal)
}
async init(config) {
this.config = config
this.initWebSocket()
this.isInitialized = true
this.updateActions() // export actions
this.updateFeedbacks() // export feedbacks
this.updateVariableDefinitions() // export variable definitions
}
// When module gets deleted
async destroy() {
this.isInitialized = false
if (this.ws) {
this.ws.close(1000)
delete this.ws
}
}
async configUpdated(config) {
this.config = config
this.initWebSocket()
}
// Return config fields for web config
getConfigFields() {
return [
{
type: 'textinput',
id: 'host',
label: 'PlayWall IP',
width: 8,
regex: Regex.IP,
},
{
type: 'textinput',
id: 'port',
label: 'PlayWall Port',
width: 4,
regex: Regex.PORT,
},
{
type: 'checkbox',
id: 'debug_messages',
label: 'Debug messages',
tooltip: 'Log incomming and outcomming messages',
width: 6,
},
]
}
updateActions() {
UpdateActions(this)
}
updateFeedbacks() {
UpdateFeedbacks(this)
}
updateVariableDefinitions() {
UpdateVariableDefinitions(this)
}
// Websocket handling
initWebSocket() {
if (this.reconnect_timer) {
clearTimeout(this.reconnect_timer)
this.reconnect_timer = null
}
if (this.config.host == null || this.config.port == null) {
console.log(`PlayWall host '${this.config.host}' or port '${this.config.port}' is invalid`);
this.updateStatus(InstanceStatus.BadConfig, `PlayWall host '${this.config.host}' or port '${this.config.port}' is invalid`)
return
}
const url = `ws://${this.config.host}:${this.config.port}/api`
if (!url || url.match(new RegExp(this.wsRegex)) === null) {
this.updateStatus(InstanceStatus.BadConfig, `WS URL is not defined or invalid`)
return
}
this.updateStatus(InstanceStatus.Connecting)
if (this.ws) {
this.ws.close(1000)
delete this.ws
}
this.ws = new WebSocket(url)
this.ws.on('open', () => {
this.updateStatus(InstanceStatus.Ok)
this.log('debug', `Connection opened`)
this.sendToWebSocket('project-current', {});
})
this.ws.on('close', (code) => {
this.log('debug', `Connection closed with code ${code}`)
this.updateStatus(InstanceStatus.Disconnected, `Connection closed with code ${code}`)
})
this.ws.on('message', this.messageReceivedFromWebSocket.bind(this))
this.ws.on('error', (data) => {
this.log('error', `WebSocket error: ${data}`)
})
}
sendToWebSocket(type, payload) {
this.ws.send(JSON.stringify({
'type': type,
'messageId': 'ID',
'payload': payload
}));
}
messageReceivedFromWebSocket(data) {
if (this.config.debug_messages) {
this.log('debug', `Message received: ${data}`)
}
const message = JSON.parse(data)
if (message.type != null) {
if (this.messageHandlers[message.type] != null) {
this.messageHandlers[message.type].handleMessage(this, message)
} else {
this.log('debug', `Cannot handle incoming message of type ${message.type}`)
}
} else {
this.log('debug', `Cannot handle incoming message ${message}`)
}
}
}
runEntrypoint(ModuleInstance, UpgradeScripts)