Skip to content

Commit

Permalink
Merge pull request #190 from ckhmer1/bug_fix
Browse files Browse the repository at this point in the history
path.join fix
  • Loading branch information
Caprico85 authored Sep 28, 2021
2 parents 88fd908 + a230ac9 commit 877e30f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
21 changes: 21 additions & 0 deletions devices/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,7 @@ module.exports = function (RED) {
states[key] = {};
old_state = states[key];
}
let mandatory_to_delete = [];
Object.keys(state_values).forEach(function (ikey) {
if (typeof value[ikey] !== 'undefined' && value[ikey] != null) {
if (typeof old_state[ikey] == 'undefined') {
Expand All @@ -2524,6 +2525,26 @@ module.exports = function (RED) {
differs = true;
}
} else if (typeof state_values[ikey] === 'number' && !(state_values[ikey] & formats.MANDATORY)) {
if (typeof states[ikey] != 'undefined') {
differs = true;
}
delete states[ikey];
} else {
mandatory_to_delete.push(ikey);
}
});
mandatory_to_delete.forEach(ikey => {
const e_states = exclusive_states[ikey] || [];
let exclusive_state_found = false;
e_states.forEach(e_state => {
if (typeof states[e_state] !== 'undefined') {
exclusive_state_found = false;
}
});
if (!exclusive_state_found) {
if (typeof states[ikey] != 'undefined') {
differs = true;
}
delete states[ikey];
} else {
RED.log.error('key "' + key + '.' + ikey + '" is mandatory.');
Expand Down
6 changes: 3 additions & 3 deletions lib/HttpActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class HttpActions {
* }
* }
*/
appHttp.post(path.join(httpRoot, 'smarthome'), function(request, response) {
appHttp.post(me.Path_join(httpRoot, 'smarthome'), function(request, response) {
let reqdata = request.body;

me.debug('HttpActions:httpActionsRegister(/smarthome): request.headers = ' + JSON.stringify(request.headers));
Expand Down Expand Up @@ -232,15 +232,15 @@ class HttpActions {
/**
* Endpoint to check HTTP(S) reachability.
*/
appHttp.get(path.join(httpRoot, 'check'), function(request, response) {
appHttp.get(me.Path_join(httpRoot, 'check'), function(request, response) {
me.debug('HttpActions:httpActionsRegister(/check)');
response.send('SUCCESS');
});

/**
* Enables preflight (OPTIONS) requests made cross-domain.
*/
appHttp.options(path.join(httpRoot, 'smarthome'), function(request, response) {
appHttp.options(me.Path_join(httpRoot, 'smarthome'), function(request, response) {
response.status(200).set({
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
}).send('null');
Expand Down
10 changes: 5 additions & 5 deletions lib/HttpAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class HttpAuth {
* &response_type=code
* - The string code
*/
appHttp.get(path.join(httpRoot, 'oauth'), function(req, res) {
appHttp.get(me.Path_join(httpRoot, 'oauth'), function(req, res) {
me.debug('HttpAuth:httpAuthRegister(GET /oauth)');

if (req.query.response_type !== 'code') {
Expand Down Expand Up @@ -83,7 +83,7 @@ class HttpAuth {
//
//
//
appHttp.post(path.join(httpRoot, 'oauth'), function(req, res) {
appHttp.post(me.Path_join(httpRoot, 'oauth'), function(req, res) {
me.debug('HttpAuth:httpAuthRegister(POST /oauth): body = ' + JSON.stringify(req.body));

// client_id in POST date is not decoded automatically by bodyParser and needs to be decoded manually
Expand Down Expand Up @@ -145,7 +145,7 @@ class HttpAuth {
* &grant_type=refresh_token
* &refresh_token=REFRESH_TOKEN
*/
appHttp.all(path.join(httpRoot, 'token'), function(req, res) {
appHttp.all(me.Path_join(httpRoot, 'token'), function(req, res) {
me.debug('HttpAuth:httpAuthRegister(/token): query = ' + JSON.stringify(req.query));
me.debug('HttpAuth:httpAuthRegister(/token): body = ' + JSON.stringify(req.body));

Expand Down Expand Up @@ -188,7 +188,7 @@ class HttpAuth {
this._mgmtNode.error('HttpAuth:handleUserAuth(): invalid user');
return res.redirect(util.format(
'%s?client_id=%s&redirect_uri=%s&state=%s&response_type=code&error=invalid_user',
path.join(httpRoot, 'oauth'), req.body.client_id, encodeURIComponent(req.body.redirect_uri), req.body.state));
this.Path_join(httpRoot, 'oauth'), req.body.client_id, encodeURIComponent(req.body.redirect_uri), req.body.state));
}

this.debug('HttpAuth:handleUserAuth(): login successful');
Expand All @@ -211,7 +211,7 @@ class HttpAuth {
this._mgmtNode.error('HttpAuth:handleUserAuth(): authCode failed');
return res.redirect(util.format(
'%s?client_id=%s&redirect_uri=%s&state=%s&response_type=code',
path.join(httpRoot, 'oauth'), req.body.client_id, encodeURIComponent(req.body.redirect_uri), req.body.state));
this.Path_join(httpRoot, 'oauth'), req.body.client_id, encodeURIComponent(req.body.redirect_uri), req.body.state));
}
}
/**
Expand Down
35 changes: 27 additions & 8 deletions lib/SmartHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const morgan = require('morgan');
const cors = require('cors');
const fs = require('fs');
const events = require('events');
const path = require('path');

const Aggregation = require('./Aggregation.js');
const Auth = require('./Auth.js');
Expand All @@ -50,7 +49,7 @@ class GoogleSmartHome extends Aggregation(Auth, Devices, HttpAuth, HttpActions)
this._reportStateTimer = null;
this._reportStateInterval = reportStateInterval; // minutes
this._httpNodeRoot = httpNodeRoot;
this._httpPath = path.join('/', httpPath || '');
this._httpPath = this.Path_join('/', httpPath || '');
this._httpsPort = httpsPort;
this._sslOffload = ssloffload;
this._publicKey = publicKey;
Expand All @@ -63,7 +62,7 @@ class GoogleSmartHome extends Aggregation(Auth, Devices, HttpAuth, HttpActions)
this._httpServerRunning = false;
this._syncScheduled = false;
this._getStateScheduled = false;
this._httpPath = path.join((usehttpnoderoot ? this._httpNodeRoot || '/' : '/'), this._httpPath);
this._httpPath = this.Path_join((usehttpnoderoot ? this._httpNodeRoot || '/' : '/'), this._httpPath);

this.loadAuth(nodeId, userDir, username || 'dummy');
this.setClientIdSecret(clientid, clientsecret);
Expand Down Expand Up @@ -96,6 +95,25 @@ class GoogleSmartHome extends Aggregation(Auth, Devices, HttpAuth, HttpActions)
//
//
//
Path_join() {
let full_path = '';
for (var i = 0; i < arguments.length; i++) {
let ipath=arguments[i];
let fpe = full_path.endsWith('/');
let ips = ipath.startsWith('/');
if (fpe && ips) {
full_path += ipath.substring(1);
} else if (!fpe && !ips) {
full_path += '/' + ipath;
} else {
full_path += ipath;
}
}
return full_path;
}
//
//
//
GetRouteType(route) {
if (route) {
if (route.route.methods['get'] && route.route.methods['post']) return "all";
Expand All @@ -110,11 +128,12 @@ class GoogleSmartHome extends Aggregation(Auth, Devices, HttpAuth, HttpActions)
//
UnregisterUrl(REDapp) {
let me = this;
if (this._httpNodeRoot !== false) {
var get_urls = [path.join(me._httpPath, 'oauth'), path.join(me._httpPath, 'check')];
var post_urls = [path.join(me._httpPath, 'oauth'), path.join(me._httpPath, 'smarthome')];
var options_urls = [path.join(me._httpPath, 'smarthome')];
var all_urls = [path.join(me._httpPath, 'token')];
if (this._httpNodeRoot !== false && REDapp._router) {
me.debug("SmartHome:UnregisterUrl(): use the Node-RED server port, path " + this._httpPath);
var get_urls = [me.Path_join(me._httpPath, 'oauth'), me.Path_join(me._httpPath, 'check')];
var post_urls = [me.Path_join(me._httpPath, 'oauth'), me.Path_join(me._httpPath, 'smarthome')];
var options_urls = [me.Path_join(me._httpPath, 'smarthome')];
var all_urls = [me.Path_join(me._httpPath, 'token')];

let to_remove = [];
REDapp._router.stack.forEach(function (route, i/*, routes*/) {
Expand Down
8 changes: 8 additions & 0 deletions test/sh/device_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,14 @@ test_payload ".color.spectrumHsv.value" 3
test_payload ".color.temperatureK" null
test_payload ".color.spectrumRgb" null

execute $NODE_ID1 ColorAbsolute 'Bianco Caldo' 2000
test_payload ".color.temperatureK" 2000
test_payload ".color.spectrumRgb" null
test_payload ".color.spectrumHsv.hue" null
test_payload ".color.spectrumHsv.saturation" null
test_payload ".color.spectrumHsv.value" null
test_out ".payload.commands[0].states.online" true

# Cook
echo
echo Cook
Expand Down

0 comments on commit 877e30f

Please sign in to comment.