Skip to content

Commit

Permalink
Check version of app.js script
Browse files Browse the repository at this point in the history
  • Loading branch information
Caprico85 committed Dec 29, 2022
1 parent d0d4b6b commit 29deae8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/HttpActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ class HttpActions {
case 'action.devices.IDENTIFY':
me._smarthome.debug('HttpActions:httpActionsRegister(/' + url + '): IDENTIFY');

me._smarthome.checkAppJsVersion(reqdata.appJsVersion);

response.status(200).json({
requestId: reqdata.requestId,
payload: {
Expand Down
38 changes: 38 additions & 0 deletions lib/SmartHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const helmet = require('helmet');
const morgan = require('morgan');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const events = require('events');
const dnssd = require('@gravitysoftware/dnssd');
const udp = require('dgram');
Expand Down Expand Up @@ -574,6 +575,43 @@ class GoogleSmartHome {
}, me._setStateDelay);
}
}

/**
* Checks if the app.js script running on the smart speaker is the most up-to-date version.
*
* During the IDENTIFY request, the app.js script running on the smart speaker sends its version number. Here, we
* read the local app.js file, parse the version number out of the file and compare the version numbers with each
* other. If the version numbers do not match, the user gets a message on Node-RED's debug panel.
*
* @param {string} remoteAppJsVersion
*/
checkAppJsVersion(remoteAppJsVersion) {
const appJsPath = path.resolve(__dirname, '../local-execution/app.js');
fs.readFile(appJsPath, 'utf8', (err, data) => {
if (err) {
this.error('SmartHome:checkAppJsVersion(): Cannot read app.js file (' + err + ')');
return;
}

const regex = /VERSION\s*=\s*'([0-9.]+)'/;
const matches = data.match(regex);
const localAppJsVersion = matches[1];

if(typeof localAppJsVersion === 'undefined') {
this.error('SmartHome:checkAppJsVersion(): Cannot parse version from app.js file');
return;
}

if(remoteAppJsVersion === localAppJsVersion) {
this.debug('SmartHome:checkAppJsVersion(): app.js on smart speaker is up to date (v' + localAppJsVersion + ')');
} else if(typeof remoteAppJsVersion === 'undefined') {
this._mgmtNode.warn('SmartHome:checkAppJsVersion(): app.js on smart speaker did not report version number. Please upload latest app.js as explained on https://github.com/mikejac/node-red-contrib-google-smarthome/blob/master/docs/local_fulfillment.md#updating-appjs.');
} else {
this._mgmtNode.warn('SmartHome:checkAppJsVersion(): app.js version on smart speaker did not match local app. Expected ' + localAppJsVersion + ', got ' + remoteAppJsVersion + '. Please upload latest app.js as explained on https://github.com/mikejac/node-red-contrib-google-smarthome/blob/master/docs/local_fulfillment.md#updating-appjs.');
}
});
}

//
//
//
Expand Down
3 changes: 2 additions & 1 deletion local-execution/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const VERSION = '2.6';
const VERSION = '2.7';

/// <reference types="@google/local-home-sdk" />
/*
Expand Down Expand Up @@ -143,6 +143,7 @@ const identifyHandler = async (request) => {
}
try {
const nodeRedData = findNodeRedDeviceDataByClientId(request.requestId, request.devices, clientId);
request.appJsVersion = VERSION;
return await forwardRequest(nodeRedData, "", request);
}
catch (err) {
Expand Down

0 comments on commit 29deae8

Please sign in to comment.