Skip to content
Snippets Groups Projects
main.js 3.11 KiB
Newer Older
  • Learn to ignore specific revisions
  • Tobias Ullerich's avatar
    Tobias Ullerich committed
    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')
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    const WebSocket = require('ws')
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    
    class ModuleInstance extends InstanceBase {
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    	isInitialized = false
    	// language=RegExp
    	wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$'
    
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    	constructor(internal) {
    		super(internal)
    	}
    
    	async init(config) {
    		this.config = config
    
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    		this.initWebSocket()
    		this.isInitialized = true
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    
    		this.updateActions() // export actions
    		this.updateFeedbacks() // export feedbacks
    		this.updateVariableDefinitions() // export variable definitions
    	}
    	// When module gets deleted
    	async destroy() {
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    		this.isInitialized = false
    
    		if (this.ws) {
    			this.ws.close(1000)
    			delete this.ws
    		}
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    	}
    
    	async configUpdated(config) {
    		this.config = config
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    		this.initWebSocket()
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    	}
    
    	// 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,
    			},
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    			{
    				type: 'checkbox',
    				id: 'debug_messages',
    				label: 'Debug messages',
    				tooltip: 'Log incomming and outcomming messages',
    				width: 6,
    			},
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    		]
    	}
    
    	updateActions() {
    		UpdateActions(this)
    	}
    
    	updateFeedbacks() {
    		UpdateFeedbacks(this)
    	}
    
    	updateVariableDefinitions() {
    		UpdateVariableDefinitions(this)
    	}
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    
    	// 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.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}`)
    		})
    	}
    
    	messageReceivedFromWebSocket(data) {
    		if (this.config.debug_messages) {
    			this.log('debug', `Message received: ${data}`)
    		}
    
    		let msgValue = null
    		try {
    			msgValue = JSON.parse(data)
    		} catch (e) {
    			msgValue = data
    		}
    	}
    
    Tobias Ullerich's avatar
    Tobias Ullerich committed
    }
    
    runEntrypoint(ModuleInstance, UpgradeScripts)