diff --git a/lib/controller.js b/lib/controller.js index 0a45692a..8351e476 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -400,7 +400,42 @@ class Controller { } else { cleanup(); } - } else { + } else if (option === 'rename') { + // Convert the MQTT message to a Zigbee message. + let json = null; + try { + json = JSON.parse(message); + } catch (e) { + // Cannot be parsed to JSON, assume state message. + return; + } + if (!json.hasOwnProperty('oldname')) { + logger.error('Failed to renamed - oldname property is missing'); + return; + } + if (!json.hasOwnProperty('newname')) { + logger.error('Failed to renamed - newname property is missing'); + return; + } + if(json.oldname.toString() == null || json.oldname.toString() == '' || json.oldname.toString().length <= 0) { + logger.error('Failed to renamed - oldname value is missing or Null'); + return; + } + if(json.newname.toString() == null || json.newname.toString() == '' || json.newname.toString().length <= 0) { + logger.error('Failed to renamed - newname value is missing or Null'); + return; + } + if(json.newname.toString() === json.oldname.toString()) { + logger.error(`Failed to renamed - ${json.oldname} and ${json.newname} seem to be eqal.`); + return; + } + const renamed = settings.RenameFriendlyName(json.oldname,json.newname); + if(renamed) { + logger.info(`Successfully renamed - ${json.oldname} to ${json.newname} `); + }else { + logger.error(`Failed to renamed - ${json.oldname} to ${json.newname}`); + } + } else { logger.warn(`Cannot handle MQTT config option '${option}' with message '${message}'`); } }