Skip to content
Snippets Groups Projects
Select Git revision
  • 29a273cbd3b517f31f3c3330c3511a0c273af716
  • master default
  • renovate/junit-jupiter-engine.version
  • renovate/opencsv.version
  • renovate/org.springframework.boot-spring-boot-starter-parent-3.x
  • renovate/testcontainer.version
  • renovate/jgit.version
  • renovate/selenium.version
  • renovate/datatables.version
  • renovate/jacoco-maven-plugin.version
  • renovate/org.apache.maven.plugins-maven-surefire-plugin-3.x
  • demo
  • v1_8_1
  • v2.18.0
  • v2.17.2
  • v2.17.1
  • v2.17.0
  • v2.16.1
  • v2.16.0
  • v2.15.1
  • v2.15.0
  • v2.14.0
  • v2.13.0
  • v2.12.0
  • v2.11.0
  • v2.10.0
  • v2.9.2
  • v2.9.1
  • v2.9.0
  • v2.8.0
  • testPipeline2
  • v2.7.0
  • v2.6.1
33 results

transactionImport.ftl

Blame
  • main.js 5.16 KiB
    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 WebSocket = require('ws')
    const ProjectUpdate = require("./receive/project_update");
    const Project = require("./project");
    const PadNameUpdate = require("./receive/pad_name_update");
    const PadStatusUpdate = require("./receive/pad_status_update");
    const uuid = require('uuid');
    
    class ModuleInstance extends InstanceBase {
        isInitialized = false
        // language=RegExp
        wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$'
    
        messageHandlers = {
            'project-current': new ProjectUpdate(),
            'pad-name-changed': new PadNameUpdate(),
            'pad-status-changed': new PadStatusUpdate()
        };
    
        currentProject = new Project({});
    
        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) {
                this.log('debug', `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': uuid.v4(),
                '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 if (message.updateType != null) {
                if (this.messageHandlers[message.updateType] != null) {
                    this.messageHandlers[message.updateType].handleMessage(this, message)
                } else {
                    this.log('debug', `Cannot handle incoming message of updateType ${message.updateType}`)
                }
            } else {
                this.log('debug', `Cannot handle incoming message ${data}`)
            }
        }
    }
    
    runEntrypoint(ModuleInstance, UpgradeScripts)