Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #9 from Pmant/master
Browse files Browse the repository at this point in the history
Improved Detection of "Activity Started"-Event

This PR introduces an improved detection after starting an activity. It does not look only into the actual command reply but also at any related, published stateDigest.
thx @Pmant for contribute!
  • Loading branch information
swissmanu committed Sep 21, 2015
2 parents 1ebe8a1 + e5f1660 commit 8b832fa
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions lib/harmonyclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,22 @@ function getActivities() {
* @param activityId
* @returns {Q.Promise}
*/
function startActivity(activityId) {
debug('start activity ' + activityId);

var timestamp = new Date().getTime()
, body = 'activityId=' + activityId + ':timestamp=' + timestamp;
function startActivity(activityId) {
var timestamp = new Date().getTime();
var body = 'activityId=' + activityId + ':timestamp=' + timestamp_temp;

return this.request('startactivity', body, 'encoded', function(stanza) {
// This canHandleStanzaFn waits for a stanza that confirms starting the activity.
// It looks something like this:
//
// <message from="HarmonyOne_Pop@qa1.com" to="ab903454-7bee-4410-9eea-bb5355bb667e">
// <event xmlns="connect.logitech.com" type="harmony.engine?startActivityFinished"><![CDATA[activityId=7596992:errorCode=200:errorString=OK]]></event>
// </message>
var event = stanza.getChild('event')
, canHandleStanza = false;

if(event && event.attr('type') === 'harmony.engine?startActivityFinished') {
var decodedResponse = xmppUtil.decodeColonSeparatedResponse(event.getText());

if(decodedResponse.activityId === activityId) {
debug('got notification that activity ' + activityId + ' started successfully');
if(event && event.attr('type') === 'connect.stateDigest?notify') {
var digest = JSON.parse(event.getText());
if (activityId === '-1' && digest.activityId === activityId && digest.activityStatus == 0){
canHandleStanza = true;
}else if(activityId !== '-1' && digest.activityId === activityId && digest.activityStatus == 2) {
canHandleStanza = true;
}
}

return canHandleStanza;
});
}
Expand Down

0 comments on commit 8b832fa

Please sign in to comment.