Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bonjour based host discovery #20

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion companion/manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../node_modules/@companion-module/base/assets/manifest.schema.json",
"id": "bmd-smartview",
"name": "bmd-smartview",
"shortname": "smart",
Expand All @@ -22,5 +23,14 @@
},
"manufacturer": "Blackmagic Design",
"products": ["SmartView", "SmartScope"],
"keywords": ["Scope", "Monitor"]
"keywords": ["Scope", "Monitor"],
"bonjourQueries": {
"bonjourHost": {
"type": "blackmagic",
"protocol": "tcp",
"txt": {
"class": "SmartView"
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bmd-smartview",
"version": "2.1.0", "main": "src/index.js",
"version": "2.2.0", "main": "src/index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
61 changes: 57 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CreateConvertToBooleanFeedbackUpgradeScript,
InstanceBase,
InstanceStatus,
Regex,
runEntrypoint,
TCPHelper,
Expand Down Expand Up @@ -84,13 +85,29 @@ class BlackmagicSmartviewInstance extends InstanceBase {
*/
getConfigFields() {
return [
{
type: 'bonjour-device',
id: 'bonjourHost',
label: 'Device',
width: 12,
},
{
type: 'textinput',
id: 'host',
label: 'Target IP',
width: 6,
isVisible: (options) => !options['bonjourHost'],
default: '',
regex: Regex.IP,
},
{
type: 'static-text',
id: 'host-filler',
width: 6,
label: '',
isVisible: (options) => !!options['bonjourHost'],
value: '',
},
{
type: 'static-text',
id: 'info',
Expand All @@ -110,6 +127,37 @@ class BlackmagicSmartviewInstance extends InstanceBase {
]
}

/**
* INTERNAL: returns the IP and port from the Bonjour host.
*
* @returns {Object} the IP and port object
* @access protected
* @since 2.2.0
*/
parseIpAndPort() {
// TODO: Switch to Regex.IP when we can convert that into a RegExp object... (it will need some processing)
const ipRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/

if (this.config.bonjourHost) {
const [ip, rawPort] = this.config.bonjourHost.split(':')
const port = Number(rawPort)
if (ip.match(ipRegex) && !isNaN(port)) {
return {
ip,
port,
}
}
} else if (this.config.host) {
if (this.config.host.match(ipRegex)) {
return {
ip: this.config.host,
port: undefined,
}
}
}
return null
}

/**
* INTERNAL: returns the desired monitor object.
*
Expand Down Expand Up @@ -182,18 +230,20 @@ class BlackmagicSmartviewInstance extends InstanceBase {
*/
initTCP() {
this.receiveBuffer = ''
let target = this.parseIpAndPort()

if (this.socket !== undefined) {
this.socket.destroy()
delete this.socket
}

if (this.config.port === undefined) {
this.config.port = 9992
if (target && target.port === undefined) {
this.log('info', 'Setting port to default')
target.port = 9992
}

if (this.config.host) {
this.socket = new TCPHelper(this.config.host, this.config.port)
if (target && target.ip) {
this.socket = new TCPHelper(target.ip, target.port)

this.socket.on('status_change', (status, message) => {
this.updateStatus(status, message)
Expand Down Expand Up @@ -258,6 +308,9 @@ class BlackmagicSmartviewInstance extends InstanceBase {
this.log('debug', 'weird response from smartview', line, line.length)
}
})
} else {
this.log('error', 'Didn\'t get a target to connect to')
this.updateStatus(InstanceStatus.Disconnected)
}
}

Expand Down