Skip to content

Commit

Permalink
Merge pull request #11 from peternewman/presets
Browse files Browse the repository at this point in the history
Handle source input presets
  • Loading branch information
peternewman authored Aug 28, 2024
2 parents 8a769f6 + e5474df commit 7ecec2a
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: 'daily'
40 changes: 40 additions & 0 deletions .github/workflows/node.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Node CI

on:
push:
pull_request:

jobs:
test:
name: Test
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Prepare Environment
run: |
yarn install
env:
CI: true
- name: prettier
run: |
yarn prettier --check .
- name: Test jest
run: |
yarn test
env:
CI: true
- name: Test package
run: |
yarn companion-module-build
env:
CI: true
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ class braviaInstance extends InstanceBase {
this.log('info', 'Verbose mode enabled. Log entries will contain detailed information.')
}

this.updateStatus(InstanceStatus.Connecting)

this.initConnection()

this.initActions()
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "index.js",
"scripts": {
"format": "prettier -w .",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest --silent=false"
},
"repository": {
"type": "git",
Expand All @@ -17,6 +17,7 @@
"node-rest-client": "^3.1.1"
},
"devDependencies": {
"@companion-module/tools": "^1.3.2"
"@companion-module/tools": "^1.3.2",
"jest": "^29.7.0"
}
}
3 changes: 3 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ module.exports = {
id: 'kind',
choices: [
{ id: 'hdmi', label: 'HDMI' },
// TODO(Someone): Add CEC, but the URI has a type and port is optional
{ id: 'component', label: 'Component' },
{ id: 'composite', label: 'Composite' },
{ id: 'scart', label: 'SCART' },
{ id: 'widi', label: 'Wi-Fi Display' },
],
},
{
Expand Down
33 changes: 31 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
initConnection: function () {
let self = this

self.updateStatus(InstanceStatus.Ok)
self.updateStatus(InstanceStatus.Connecting)

self.sendCommand('avContent', 'getCurrentExternalInputsStatus', {}, 'allinputs')

Expand All @@ -19,7 +19,7 @@ module.exports = {

self.stopInterval()

if (self.config.interval > 0) {
if (self.config.polling !== undefined && self.config.polling && self.config.interval > 0) {
self.INTERVAL = setInterval(self.getInformation.bind(self), self.config.interval)
self.log('info', 'Starting Update Interval: Every ' + self.config.interval + 'ms')
}
Expand Down Expand Up @@ -72,13 +72,15 @@ module.exports = {
//do something with response
try {
if (response.statusCode == 200) {
self.updateStatus(InstanceStatus.Ok)
if (request) {
if (data.result) {
switch (request) {
case 'allinputs':
self.DATA.inputs = data.result[0]
self.buildInputList()
self.initFeedbacks()
self.initPresets()
break
case 'power':
self.DATA.powerState = data.result[0].status === 'active' ? true : false
Expand All @@ -104,26 +106,53 @@ module.exports = {
self.checkVariables()
} else {
if (response.statusCode == 403) {
self.updateStatus(InstanceStatus.ConnectionFailure, 'Error 403, PSK may be incorrect.')
self.log('error', 'PSK may be incorrect. Please check your PSK and try again.')
self.stopInterval()
}
}
} catch (error) {
self.updateStatus(InstanceStatus.UnknownError, 'Failed to process response: ' + error)
self.log('error', 'Error processing response: ' + error)
console.log(error)
console.log(data)
}
})
.on('error', function (error) {
self.updateStatus(InstanceStatus.UnknownError, 'Failed to sending command ' + error.toString())
self.log('error', 'Error Sending Command ' + error.toString())
})
} else {
self.updateStatus(InstanceStatus.BadConfig, 'No PSK set, not sending.')
if (self.config.verbose) {
self.log('debug', 'No PSK set. Not sending command.')
}
}
},

parseDeviceResourceURI: function (uri) {
try {
resourceURI = new URL(uri)
if (resourceURI.protocol == 'extinput:') {
let params = resourceURI.searchParams
return {
// Seemingly not URL.host!
kind: resourceURI.pathname,
port: parseInt(params.get('port')),
}
} else {
return undefined
}
} catch (e) {
// instanceof doesn't seem to work directly
if (e.name == 'TypeError') {
return undefined
} else {
throw e
}
}
},

buildInputList: function () {
let self = this

Expand Down
9 changes: 9 additions & 0 deletions src/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const api = require('./api.js')

test('parse URI', () => {
expect(api.parseDeviceResourceURI('extInput:hdmi?port=1')).toEqual({ kind: 'hdmi', port: 1 })
expect(api.parseDeviceResourceURI('extInput:component?port=4')).toEqual({ kind: 'component', port: 4 })
expect(api.parseDeviceResourceURI('foo:component?port=4')).toEqual(undefined)
expect(api.parseDeviceResourceURI('')).toEqual(undefined)
expect(api.parseDeviceResourceURI(undefined)).toEqual(undefined)
})
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
id: 'polling',
label: 'Enable Polling',
width: 12,
default: false,
},
{
type: 'static-text',
Expand Down
Loading

0 comments on commit 7ecec2a

Please sign in to comment.