domotz-remote-pawn
Version:
Domotz Agent
107 lines (106 loc) • 3.86 kB
JavaScript
/**
* This file is part of Domotz Agent.
* Copyright (C) 2018 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* Created by Alessio Sanfratello <alessio@domotz.com> on 02/10/18.
*/
var childProcess = require('child_process');
const q = require('q');
function factory(fs) {
const mapVersion = {
qnap: _getQnapFirmwareVersion,
syno: _getSynoFirmwareVersion,
netgear_router: _getNetgearRouterFirmwareVersion,
default: _getDefaultFirmwareVersion,
};
function _getDefaultFirmwareVersion() {
try {
return _getNetgearRouterFirmwareVersion();
} catch (e) {
return null;
}
}
function _getNetgearRouterFirmwareVersion() {
var data = fs.readFileSync('/etc/version', 'utf8');
return data.trim();
}
function _getQnapFirmwareVersion() {
var data;
childProcess.exec('getcfg system version', function (err, stdout, stderr) {
if (err || stderr) {
data = null;
console.warn('No firmware version found from getcfg system version command');
} else {
data = stdout.trim();
console.debug('Found firmware version from getcfg system version command : %s', data);
}
});
if (!data) {
try {
data = fs.readFileSync('/etc/config/uLinux.conf', 'utf8');
// Find first "Version" occurrence after "[System]"
var match = data.match(/\[System\][\s\S]*?Version\s*=\s*(.*)/);
if (match) {
data = match[1].trim();
console.debug('Found firmware version in /etc/config/uLinux.conf : %s', data);
} else {
console.warn('No firmware version found in /etc/config/uLinux.conf');
data = null;
}
} catch (err) {
console.warn('No QNAP firmware version found');
}
}
return data;
}
function _getSynoFirmwareVersion() {
var data = null;
try {
data = fs.readFileSync('/etc.defaults/VERSION', 'utf8');
const match = data.match(/productversion\s*=\s*"([^"]*)"/);
if (match) {
data = match[1].trim();
console.debug('Found Syno firmware version in /etc.defaults/VERSION : %s', data);
} else {
data = null;
console.warn('No firmware version found in /etc.defaults/VERSION');
}
} catch (err) {
console.warn('No Syno firmware version found');
}
return data;
}
function getFirmwareVersion() {
var deferred = q.defer();
var data = null;
try {
data = mapVersion[process.env.DPLATFORM]();
} catch (err) {
data = mapVersion['default']();
}
if (data) {
deferred.resolve(data);
} else {
console.warn('No firmware version found');
deferred.reject();
}
return deferred.promise;
}
return {
getFirmwareVersion: getFirmwareVersion,
};
}
module.exports.factory = factory;