Skip to content

Commit

Permalink
Fix #2 Adding more validation on button configs. (#3)
Browse files Browse the repository at this point in the history
* Fix #2 Adding more validation on button configs.
  • Loading branch information
MarcHagen authored Jun 16, 2020
1 parent fbcbf12 commit 6a314ee
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

---

---
## [1.0.3] (2020-06-16)

## Bug Fixes
* [#2](https://github.com/bitfocus/companion-module-epiphan-pearl/issues/2) - Fixed error on non existing action.

---

## [1.0.2] (2020-04-15)

## Bug Fixes
Expand Down
107 changes: 101 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const TYPE_PUBLISHER = 2;
* Companion instance class for the Epiphan Pearl.
*
* @extends instanceSkel
* @version 1.0.0
* @version 1.0.3
* @since 1.0.0
* @author Marc Hagen <[email protected]>
*/
Expand Down Expand Up @@ -132,6 +132,22 @@ class EpiphanPearl extends instanceSkel {
switch (action.action) {
case 'channelChangeLayout': {
const [channelId, layoutId] = action.options.channelIdlayoutId.split('-');
if (!this._getChannelById(channelId)) {
this._setStatus(
this.STATUS_ERROR,
'Action on non existing channel! Please review you\'re button config'
);
this.log('error', 'Action on non existing channel: ' + channelId);
break;
}
if (!this._checkValidLayoutId(channelId, layoutId)) {
this._setStatus(
this.STATUS_ERROR,
'Action on non existing layout! Please review you\'re button config'
);
this.log('error', 'Action on non existing layout ' + layoutId + ' on channel ' + channelId);
break;
}

type = 'put';
url = '/api/channels/' + channelId + '/layouts/active';
Expand All @@ -145,7 +161,32 @@ class EpiphanPearl extends instanceSkel {
}
case 'channelStreaming': {
const [channelId, publishersId] = action.options.channelIdpublisherId.split('-');
const startStopAction = this._getStartStopActionFromOptions(action.options);
if (!this._getChannelById(channelId)) {
this._setStatus(
this.STATUS_ERROR,
'Action on non existing channel! Please review you\'re button config.'
);
this.log('error', 'Action on non existing channel: ' + channelId);
break;
}
if (!this._checkValidPublisherId(channelId, publishersId)) {
this._setStatus(
this.STATUS_ERROR,
'Action on non existing publisher! Please review you\'re button config.'
);
this.log('error', 'Action on non existing publisher ' + publishersId + ' on channel ' + channelId);
break;
}

const startStopAction = this._getStartStopActionFromOptions(action.options);
if (startStopAction === null) {
this._setStatus(
this.STATUS_ERROR,
'The was called for a unknow action! Please review you\'re button config.'
);
this.log('error', 'The was called for a unknow action: ' + action.options.startStopAction);
break;
}

type = 'post';
if (publishersId !== 'all') {
Expand All @@ -156,8 +197,25 @@ class EpiphanPearl extends instanceSkel {
break;
}
case 'recorderRecording': {
const recorderId = action.options.recorderId;
if (!this._getRecorderById(recorderId)) {
this._setStatus(
this.STATUS_ERROR,
'Action on non existing recorder! Please review you\'re button config.'
);
this.log('error', 'Action on non existing recorder ' + recorderId);
break;
}

const startStopAction = this._getStartStopActionFromOptions(action.options);
const recorderId = action.options.recorderId;
if (startStopAction === null) {
this._setStatus(
this.STATUS_ERROR,
'The was called for a unknow action! Please review you\'re button config.'
);
this.log('error', 'The was called for a unknow action: ' + action.options.startStopAction);
break;
}

type = 'post';
url = '/api/recorders/' + recorderId + '/control/' + startStopAction;
Expand Down Expand Up @@ -290,9 +348,12 @@ class EpiphanPearl extends instanceSkel {
* @param {Object} options - Option object gotten from a performed action [action()]
*/
_getStartStopActionFromOptions(options) {
return this.CHOICES_STARTSTOP.find((e) => {
return e.id === parseInt(options.startStopAction);
}).action;
const startStopActionId = parseInt(options.startStopAction);
const action = this.CHOICES_STARTSTOP.find((e) => {
return e.id === startStopActionId;
}).action

return action ? action : null;
}

/**
Expand Down Expand Up @@ -471,6 +532,40 @@ class EpiphanPearl extends instanceSkel {
return recorder.status.isRecording ? recorder.status.isRecording : false;
}

/**
* INTERNAL: Check if the publisher id we get from the button is valid.
*
* @private
* @since 1.0.3
* @param {String|Number} channelId
* @param {String|Number} publisherId
*/
_checkValidPublisherId(channelId, publisherId) {
const channel = this._getChannelById(channelId);
if (!channel) {
return;
}

return this._getTypeFromChannelById(TYPE_PUBLISHER, channel, publisherId)
}

/**
* INTERNAL: Check if the layout id we get from the button is valid.
*
* @private
* @since 1.0.3
* @param {String|Number} channelId
* @param {String|Number} layoutId
*/
_checkValidLayoutId(channelId, layoutId) {
const channel = this._getChannelById(channelId);
if (!channel) {
return;
}

return this._getTypeFromChannelById(TYPE_LAYOUT, channel, layoutId)
}

/**
* INTERNAL: Get action from the options for start and stop
*
Expand Down

0 comments on commit 6a314ee

Please sign in to comment.