Skip to content

Commit

Permalink
fixed #159
Browse files Browse the repository at this point in the history
  • Loading branch information
windkh committed Jun 24, 2024
1 parent 589853b commit dad8948
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 81 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [10.25.2] - 2024-06-24
### fixed gen 3 problems: device was not detected correctly - [#159](https://github.com/windkh/node-red-contrib-shelly/issues/159)

## [10.25.1] - 2024-06-15
### added unique timeouts to hunt down issue 157 - [#157](https://github.com/windkh/node-red-contrib-shelly/issues/157)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-shelly",
"version": "10.25.1",
"version": "10.25.2",
"description": "Shelly nodes.",
"node-red": {
"version": ">=0.1.0",
Expand Down
103 changes: 23 additions & 80 deletions shelly/99-shelly.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,74 +263,18 @@ module.exports = function (RED) {
}

// Note that this function has a reduced timeout.
function shellyTryGet(route, node, credentials, timeout, callback, errorCallback){
let data;
let params;
return shellyTryRequest('GET', route, params, data, node, credentials, timeout, callback, errorCallback);
}

// Note that this function has a reduced timeout.
function shellyTryRequest(method, route, params, data, node, credentials, timeout, callback, errorCallback){
async function shellyTryGet(route, node, credentials, timeout, callback, errorCallback){
try {
let data;
let params;

if (timeout === undefined || timeout === null){
timeout = 5001;
};

// We avoid an invalid timeout by taking a default if 0.
let requestTimeout = timeout;
if (requestTimeout <= 0){
requestTimeout = 5002;
}

let headers = getHeaders(credentials);

let baseUrl = 'http://' + credentials.hostname;
let config = {
baseURL : baseUrl,
url : route,
method : method,
params : params,
data : data,
headers : headers,
timeout: requestTimeout,
validateStatus : (status) => status === 200 || status === 401
};

try
{
const request = axios.request(config);

request.then(response => {
if (response.status == 200){
callback(response.data);
}
else if (response.status == 401){
config.headers = {
'Authorization': getDigestAuthorization(response, credentials, config)
}

const digestRequest = axios.request(config);
digestRequest.then(response => {
if (response.status == 200){
callback(response.data);
}
else {
node.status({ fill: "red", shape: "ring", text: "Error: " + response.statusText });
node.warn("Error: " + response.statusText + ' ' + config.url);
}
})
}
else {
node.status({ fill: "red", shape: "ring", text: "Error: " + response.statusText });
node.warn("Error: " + response.statusText );
}
})
.catch(error => {
errorCallback(error);
});
let result = await shellyRequestAsync('GET', route, params, data, credentials, timeout);
callback(result);
}
catch(error2) {
errorCallback(error2);
catch (error) {
node.status({ fill: "red", shape: "ring", text: "Error: " + error });
node.warn("Error: " + error );
errorCallback(error);
}
}

Expand Down Expand Up @@ -362,8 +306,7 @@ module.exports = function (RED) {
validateStatus : (status) => status === 200 || status === 401
};

try
{
try {
const request = axios.request(config);

request.then(response => {
Expand Down Expand Up @@ -444,7 +387,7 @@ module.exports = function (RED) {
node.status({ fill: "green", shape: "ring", text: "Connected." });
}
else{
node.status({ fill: "red", shape: "ring", text: "Shelly type mismatch: " + deviceType });
node.status({ fill: "red", shape: "ring", text: "Shelly type mismatch: " + deviceType + " not found in [" + types.join(",") + "]"});
node.warn("Shelly type mismatch: " + deviceType);
}
}
Expand Down Expand Up @@ -482,7 +425,7 @@ module.exports = function (RED) {

} // Generation 3 devices
else if (shellyInfo.model && shellyInfo.gen === 3){
deviceType = node.shellyInfo.model;
deviceType = shellyInfo.model;
requiredNodeType = 'shelly-gen2'; // right now the protocol is compatible to gen 2
}
else {
Expand Down Expand Up @@ -2124,7 +2067,7 @@ module.exports = function (RED) {
'enable' : true,
"urls": [url]
};
let createWebhookResonse = await shellyRequestAsync('POST', '/rpc/Webhook.Create', null, createParams, credentials);
let createWebhookResponse = await shellyRequestAsync('POST', '/rpc/Webhook.Create', null, createParams, credentials);

node.status({ fill: "green", shape: "ring", text: "Connected." });
success = true;
Expand Down Expand Up @@ -2457,7 +2400,7 @@ module.exports = function (RED) {
return result;
}

function executeCommand2(msg, request, node, credentials){
async function executeCommand2(msg, request, node, credentials){

let getStatusRoute = '/rpc/Shelly.GetStatus';
if (request !== undefined && request.route !== undefined && request.route !== ''){
Expand All @@ -2467,8 +2410,9 @@ module.exports = function (RED) {
let data = request.data;
let params = request.params;

shellyTryRequest(method, route, params, data, node, credentials, 5020, function(body) {

try {
let body = await shellyRequestAsync(method, route, params, data, credentials, 5020);

if (node.getStatusOnCommand) {
shellyTryGet(getStatusRoute, node, credentials, 5021, function(body) {

Expand All @@ -2492,11 +2436,11 @@ module.exports = function (RED) {
msg.payload = body;
node.send([msg]);
}
},
function(error){
node.status({ fill: "yellow", shape: "ring", text: error.message });
}
catch (error) {
node.status({ fill: "yellow", shape: "ring", text: error });
node.warn(error);
});
}
}
else {
shellyTryGet(getStatusRoute, node, credentials, 5022, function(body) {
Expand Down Expand Up @@ -2729,8 +2673,7 @@ module.exports = function (RED) {
validateStatus : (status) => status === 200
};

try
{
try {
const request = cloudAxios.request(config);

request.then(response => {
Expand Down

0 comments on commit dad8948

Please sign in to comment.