Skip to content

Commit

Permalink
Merge pull request #114 from ckhmer1/media_device_fix
Browse files Browse the repository at this point in the history
Fix Playback commands #113
  • Loading branch information
Caprico85 authored Mar 13, 2021
2 parents ab2818e + 3b9f370 commit 0cc2217
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion devices/media.html
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ <h3>References</h3>
<p><a href="https://developers.google.com/assistant/smarthome/guides/streamingbox" target="_new">Smart Home streaming box Guide</a>.</p>
<p><a href="https://developers.google.com/assistant/smarthome/guides/streamingsoundbar" target="_new">Smart Home streaming soundbar Guide</a>.</p>
<p><a href="https://developers.google.com/assistant/smarthome/guides/streamingstick" target="_new">Smart Home streaming stick Guide</a>.</p>
<p><a href="https://developers.google.com/assistant/smarthome/guides/tv" target="_new">Smart Home TV Guide</a>.</p>
<p><a href="https://developers.google.com/assistant/smarthome/guides/tv" target="_new">Smart Home Television Guide</a>.</p>
<p><a href="https://www.npmjs.com/package/node-red-contrib-google-smarthome" target="_new">Node Information</a>.</p>
</script>

Expand Down
74 changes: 41 additions & 33 deletions devices/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ module.exports = function(RED) {
}
} else {
this.available_applications = undefined;
RED.log.debug("Applications disabled");
this.debug(".constructor: Applications disabled");
}

if (this.has_channels) {
Expand All @@ -176,7 +176,7 @@ module.exports = function(RED) {
}
} else {
this.available_channels = undefined;
RED.log.debug("Channels disabled");
this.debug(".constructor: Channels disabled");
}

if (this.has_inputs) {
Expand All @@ -187,7 +187,7 @@ module.exports = function(RED) {
}
} else {
this.available_inputs = undefined;
RED.log.debug("Inputs disabled");
this.debug(".constructor Inputs disabled");
}

if (this.has_modes) {
Expand All @@ -198,7 +198,7 @@ module.exports = function(RED) {
}
} else {
this.available_modes = undefined;
RED.log.debug("Modes disabled");
this.debug(".constructor: Modes disabled");
}

if (this.has_toggles) {
Expand All @@ -209,7 +209,7 @@ module.exports = function(RED) {
}
} else {
this.available_toggles = undefined;
RED.log.debug("Toggles disabled");
this.debug(".constructor: Toggles disabled");
}

this.states = this.clientConn.register(this, 'media', config.name, this);
Expand All @@ -225,6 +225,7 @@ module.exports = function(RED) {
}

debug(msg) {
msg = 'google-smarthome:MediaNode' + msg;
if (this.clientConn && typeof this.clientConn.debug === 'function') {
this.clientConn.debug(msg);
} else {
Expand All @@ -237,7 +238,7 @@ module.exports = function(RED) {
*
*/
registerDevice(client, name, me) {
RED.log.debug("MediaNode(registerDevice) device_type " + me.device_type);
me.debug(".registerDevice: device_type " + me.device_type);
let states = {
online: true
};
Expand Down Expand Up @@ -273,7 +274,7 @@ module.exports = function(RED) {
this.updateAttributesForTraits(me, device);
this.updateStatesForTraits(me, device);

RED.log.debug("MediaNode(registerDevice): device = " + JSON.stringify(device));
me.debug(".registerDevice: device = " + JSON.stringify(device));

return device;
}
Expand Down Expand Up @@ -370,10 +371,12 @@ module.exports = function(RED) {
* called when state is updated from Google Assistant
*
*/
updated(device, params) {
updated(device, params, original_params) {
let states = device.states;
let command = device.command;
RED.log.debug("MediaNode(updated): states = " + JSON.stringify(states));
this.debug(".updated: states = " + JSON.stringify(states));
this.debug(".updated: params = " + JSON.stringify(params));
this.debug(".updated: original_params = " + JSON.stringify(original_params));

Object.assign(this.states, states);

Expand All @@ -398,6 +401,16 @@ module.exports = function(RED) {
}
});

if (command === 'action.devices.commands.mediaSeekRelative') {
if (original_params.hasOwnProperty('relativePositionMs')) {
msg.payload.relativePositionMs = original_params.relativePositionMs;
}
} else if (command === 'action.devices.commands.mediaSeekToPosition') {
if (original_params.hasOwnProperty('absPositionMs')) {
msg.payload.absPositionMs = original_params.absPositionMs;
}
}

this.send(msg);
};

Expand All @@ -407,12 +420,11 @@ module.exports = function(RED) {
*/
onInput(msg) {
const me = this;
RED.log.debug("MediaNode(input)");
me.debug(".input: topic = " + msg.topic);

let topicArr = String(msg.topic).split(this.topicDelim);
let topic = topicArr[topicArr.length - 1]; // get last part of topic

RED.log.debug("MediaNode(input): topic = " + topic);
try {
if (topic.toUpperCase() === 'APPLICATIONS') {
if (this.has_apps) {
Expand Down Expand Up @@ -515,14 +527,14 @@ module.exports = function(RED) {
Object.keys(this.states).forEach(function (key) {
if (topic.toUpperCase() == key.toUpperCase()) {
state_key = key;
RED.log.debug("MediaNode(input): found state " + key);
me.debug(".input: found state " + key);
}
});

if (state_key !== '') {
const differs = me.setState(state_key, msg.payload, this.states);
if (differs) {
RED.log.debug("MediaNode(input): " + state_key + ' ' + msg.payload);
me.debug(".input: " + state_key + ' ' + msg.payload);
this.clientConn.setState(this, this.states); // tell Google ...

if (this.passthru) {
Expand All @@ -533,11 +545,11 @@ module.exports = function(RED) {
this.updateStatusIcon();
}
} else {
RED.log.debug("MediaNode(input): some other topic");
me.debug(".input: some other topic");
let differs = false;
Object.keys(this.states).forEach(function (key) {
if (msg.payload.hasOwnProperty(key)) {
RED.log.debug("MediaNode(input): set state " + key + ' to ' + msg.payload[key]);
me.debug(".input: set state " + key + ' to ' + msg.payload[key]);
if (me.setState(key, msg.payload[key], me.states)) {
differs = true;
}
Expand Down Expand Up @@ -590,7 +602,7 @@ module.exports = function(RED) {

updateModesState(me, device) {
// Key/value pair with the mode name of the device as the key, and the current setting_name as the value.
RED.log.debug("Update Modes device");
me.debug(".updateModesState");
let states = device.states || {};
const currentModeSettings = states['currentModeSettings']
let new_modes = {};
Expand Down Expand Up @@ -690,7 +702,7 @@ module.exports = function(RED) {
const userDir = RED.settings.userDir;
filename = path.join(userDir, filename);
}
RED.log.debug('MediaNode:loadJson(): loading ' + filename);
this.debug('.loadJson: filename ' + filename);

try {
let jsonFile = fs.readFileSync(
Expand All @@ -701,12 +713,12 @@ module.exports = function(RED) {
});

if (jsonFile === '') {
RED.log.debug('MediaNode:loadJson(): empty data');
this.debug('.loadJson: empty data');
return defaultValue;
} else {
RED.log.debug('MediaNode:loadJson(): data loaded');
this.debug('.loadJson: data loaded');
const json = JSON.parse(jsonFile);
RED.log.debug('MediaNode:loadJson(): json = ' + JSON.stringify(json));
this.debug('.loadJson: json = ' + JSON.stringify(json));
return json;
}
}
Expand All @@ -721,7 +733,7 @@ module.exports = function(RED) {
const userDir = RED.settings.userDir;
filename = path.join(userDir, filename);
}
RED.log.debug('MediaNode:writeJson(): writing ' + filename);
this.debug('.writeJson: filename ' + filename);
if (typeof value === 'object') {
value = JSON.stringify(value);
}
Expand All @@ -734,7 +746,7 @@ module.exports = function(RED) {
'flag': fs.constants.W_OK | fs.constants.O_CREAT | fs.constants.O_TRUNC
});

RED.log.debug('MediaNode:writeJson(): data saved');
this.debug('writeJson: data saved');
return true;
}
catch (err) {
Expand All @@ -752,18 +764,10 @@ module.exports = function(RED) {
'executionStates': executionStates
};

RED.log.debug("MediaNode:execCommand(command) " + JSON.stringify(command));
RED.log.debug("MediaNode:execCommand(states) " + JSON.stringify(this.states));
// RED.log.debug("MediaNode:execCommand(device) " + JSON.stringify(device));
me.debug(".execCommand: command " + JSON.stringify(command));
me.debug(".execCommand: states " + JSON.stringify(this.states));
// me.debug(".execCommand: device " + JSON.stringify(device));

if (!command.hasOwnProperty('params')) {
// TransportControl
if (command.command == 'action.devices.commands.mediaClosedCaptioningOff') {
executionStates.push('online', 'playbackState');
return ok_result;
}
return false;
}
// Applications
if ((command.command == 'action.devices.commands.appInstall') ||
(command.command == 'action.devices.commands.appSearch') ||
Expand Down Expand Up @@ -922,6 +926,10 @@ module.exports = function(RED) {
executionStates.push('online', 'playbackState');
return ok_result;
}
else if (command.command == 'action.devices.commands.mediaClosedCaptioningOff') {
executionStates.push('online', 'playbackState');
return ok_result;
}
// Volume
else if (command.command == 'action.devices.commands.mute') {
if (command.params.hasOwnProperty('mute')) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Devices {
//
//
//
execDevice(device, doUpdate, params) {
execDevice(device, doUpdate, params, original_params) {
let me = this;

me.debug('Device:execDevice(): BEGIN');
Expand Down Expand Up @@ -177,7 +177,7 @@ class Devices {

if (doUpdate) {
me.debug('Device:execDevice(): doUpdate');
this._nodes[device.id].updated(this._devices[device.id], params);
this._nodes[device.id].updated(this._devices[device.id], params, original_params);
}

me.debug('Device:execDevice(): END');
Expand Down
3 changes: 2 additions & 1 deletion lib/HttpActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class HttpActions {
_execDevice(command, device) {
this.debug('HttpActions:_execDevice(): command = ' + JSON.stringify(command));

const params = command.hasOwnProperty('params') ? command.params : {};
let curDevice = {
id: device.id,
states: {},
Expand Down Expand Up @@ -421,7 +422,7 @@ class HttpActions {
states: {},
};

this.execDevice(curDevice, true, command.params);
this.execDevice(curDevice, true, command.params, params);

let execDevice = this.getStatus([curDevice.id]);
this.debug('HttpActions:_execDevice(): execDevice = ' + JSON.stringify(execDevice));
Expand Down

0 comments on commit 0cc2217

Please sign in to comment.