Initial commit
This commit is contained in:
+136
@@ -0,0 +1,136 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
/**
|
||||
* Converts an array of objects to an object.
|
||||
* The object's properties are fetched from the
|
||||
* id property of the original object of each element
|
||||
* and the value consists of the remaining properties.
|
||||
*
|
||||
* @param {Object[]} klfArray Array with internal KLF interface's result data.
|
||||
* It consists typically of objects with id, name and other properties.
|
||||
* @returns {object} Returns an object with properties for each id and their values set to
|
||||
* a copy of the original object but without the id property.
|
||||
*/
|
||||
exports.convertKlfArrayToDictionary = function convertKlfArrayToDictionary(klfArray)
|
||||
{
|
||||
return klfArray.reduce(
|
||||
function(res, curr)
|
||||
{
|
||||
if (!('id' in curr))
|
||||
{
|
||||
throw new TypeError('Missing property "id"');
|
||||
}
|
||||
res[curr.id] = _.omit(curr, 'id');
|
||||
return res;
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the difference between two arrays of KLF products.
|
||||
* Products are considered new or deleted if based on their id
|
||||
* the name, category, typeId or subtype differs.
|
||||
* Products are considered changed if only the scenes differ.
|
||||
*
|
||||
* @param {Object[]} oldProducts Array with internal KLF interface's result data (the old data)
|
||||
* @param {Object[]} newProducts Array with internal KLF interface's result data (the new data)
|
||||
* @returns {object} Returns an object with new, deleted and changed objects.
|
||||
*/
|
||||
exports.getKlfProductDifferences = function getKlfProductDifferences(oldProducts, newProducts)
|
||||
{
|
||||
const compareProperties = ['name', 'category', 'typeId', 'subtype'];
|
||||
|
||||
let result = {
|
||||
"newProducts": [],
|
||||
"deletedProducts": [],
|
||||
"changedProducts": []
|
||||
};
|
||||
|
||||
// Convert the arrays to dictionaries (makes comparision easier)
|
||||
let oldProductsDictionary = this.convertKlfArrayToDictionary(oldProducts);
|
||||
let newProductsDictionary = this.convertKlfArrayToDictionary(newProducts);
|
||||
|
||||
// Check for new or changed products:
|
||||
_.forEach(newProducts, function (value) {
|
||||
let id = value.id.toString();
|
||||
let oldProduct = oldProductsDictionary[id];
|
||||
if (undefined === oldProduct
|
||||
|| !_.isEqual(_.pick(oldProduct, compareProperties), _.pick(value, compareProperties)))
|
||||
{
|
||||
result.newProducts.push(value);
|
||||
}
|
||||
else if (!_.isEqual(oldProduct.scenes, value.scenes))
|
||||
{
|
||||
result.changedProducts.push(value);
|
||||
}
|
||||
});
|
||||
|
||||
// Check for deleted products:
|
||||
_.forEach(oldProducts, function (value) {
|
||||
let id = value.id.toString();
|
||||
let newProduct = newProductsDictionary[id];
|
||||
if (undefined === newProduct
|
||||
|| !_.isEqual(_.pick(newProduct, compareProperties), _.pick(value, compareProperties)))
|
||||
{
|
||||
result.deletedProducts.push(value);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the difference between two arrays of KLF scenes.
|
||||
* Scenes are considered new or deleted if based on their id
|
||||
* the name or products differs.
|
||||
* Scenes are considered changed if the silent property differs.
|
||||
*
|
||||
* @param {Object[]} oldScenes Array with internal KLF interface's result data (the old data)
|
||||
* @param {Object[]} newScenes Array with internal KLF interface's result data (the new data)
|
||||
* @returns {object} Returns an object with new, deleted and changed objects.
|
||||
*/
|
||||
exports.getKlfSceneDifferences = function getKlfSceneDifferences(oldScenes, newScenes)
|
||||
{
|
||||
const compareProperties = ['name', 'products'];
|
||||
|
||||
let result = {
|
||||
"newScenes": [],
|
||||
"deletedScenes": [],
|
||||
"changedScenes": []
|
||||
};
|
||||
|
||||
// Convert the arrays to dictionaries (makes comparision easier)
|
||||
let oldScenesDictionary = this.convertKlfArrayToDictionary(oldScenes);
|
||||
let newScenesDictionary = this.convertKlfArrayToDictionary(newScenes);
|
||||
|
||||
// Check for new or changed scenes:
|
||||
_.forEach(newScenes, function (value) {
|
||||
let id = value.id.toString();
|
||||
let oldScene = oldScenesDictionary[id];
|
||||
if (undefined === oldScene
|
||||
|| !_.isEqual(_.pick(oldScene, compareProperties), _.pick(value, compareProperties)))
|
||||
{
|
||||
result.newScenes.push(value);
|
||||
}
|
||||
else if (!_.isEqual(oldScene.silent, value.silent))
|
||||
{
|
||||
result.changedScenes.push(value);
|
||||
}
|
||||
});
|
||||
|
||||
// Check for deleted scenes:
|
||||
_.forEach(oldScenes, function (value) {
|
||||
let id = value.id.toString();
|
||||
let newScene = newScenesDictionary[id];
|
||||
if (undefined === newScene
|
||||
|| !_.isEqual(_.pick(newScene, compareProperties), _.pick(value, compareProperties)))
|
||||
{
|
||||
result.deletedScenes.push(value);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Converts the interface's typeId value to the corresponding channel role.
|
||||
*
|
||||
* @param {number} typeId
|
||||
* @returns {string} Returns the channel role.
|
||||
*/
|
||||
exports.getRole = function getRole(typeId) {
|
||||
const mapping = {
|
||||
"1": "blind",
|
||||
"2": "shutter.roller",
|
||||
"3": "blind.awning",
|
||||
"4": "window",
|
||||
"5": "opener.garage",
|
||||
"6": "light",
|
||||
"7": "opener.gate",
|
||||
"8": "opener.door.rolling",
|
||||
"9": "lock",
|
||||
"10": "blind",
|
||||
"11": "secureconfigurationdevice",
|
||||
"12": "repeater",
|
||||
"13": "shutter.dual",
|
||||
"14": "thermo",
|
||||
"15": "switch",
|
||||
"16": "awning.horizontal",
|
||||
"17": "blind.venetion",
|
||||
"18": "blind.louvre",
|
||||
"19": "track.curtain",
|
||||
"20": "thermo.ventilation",
|
||||
"21": "thermo.heating.outdoor",
|
||||
"22": "thermo.heating.pump",
|
||||
"23": "alarm.intrusion",
|
||||
"24": "shutter.swinging"
|
||||
};
|
||||
let result = mapping[typeId.toString()] || '';
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.getLevelType = function getLevelType(typeId) {
|
||||
const mapping = {
|
||||
"1": "level.blind",
|
||||
"2": "level.blind",
|
||||
"3": "level.blind",
|
||||
"4": "level.blind",
|
||||
"10": "level.blind",
|
||||
"13": "level.blind",
|
||||
"16": "level.blind",
|
||||
"17": "level.blind",
|
||||
"18": "level.blind",
|
||||
"24": "level.blind"
|
||||
};
|
||||
let result = mapping[typeId.toString()] || 'level.blind';
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let controllerDir;
|
||||
let appName;
|
||||
|
||||
/**
|
||||
* returns application name
|
||||
*
|
||||
* The name of the application can be different and this function finds it out.
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
function getAppName() {
|
||||
const parts = __dirname.replace(/\\/g, '/').split('/');
|
||||
return parts[parts.length - 2].split('.')[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* looks for js-controller home folder
|
||||
*
|
||||
* @param {boolean} isInstall
|
||||
* @returns {string}
|
||||
*/
|
||||
function getControllerDir(isInstall) {
|
||||
// Find the js-controller location
|
||||
const possibilities = [
|
||||
'yunkong2.js-controller',
|
||||
'yunkong2.js-controller',
|
||||
];
|
||||
/** @type {string} */
|
||||
let controllerPath;
|
||||
for (const pkg of possibilities) {
|
||||
try {
|
||||
const possiblePath = require.resolve(pkg);
|
||||
if (fs.existsSync(possiblePath)) {
|
||||
controllerPath = possiblePath;
|
||||
break;
|
||||
}
|
||||
} catch (e) { /* not found */ }
|
||||
}
|
||||
if (controllerPath == null) {
|
||||
if (!isInstall) {
|
||||
console.log('Cannot find js-controller');
|
||||
process.exit(10);
|
||||
} else {
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
// we found the controller
|
||||
return path.dirname(controllerPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* reads controller base settings
|
||||
*
|
||||
* @alias getConfig
|
||||
* @returns {object}
|
||||
*/
|
||||
function getConfig() {
|
||||
let configPath;
|
||||
if (fs.existsSync(
|
||||
configPath = path.join(controllerDir, 'conf', appName + '.json')
|
||||
)) {
|
||||
return JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
} else if (fs.existsSync(
|
||||
configPath = path.join(controllerDir, 'conf', + appName.toLowerCase() + '.json')
|
||||
)) {
|
||||
return JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
} else {
|
||||
throw new Error('Cannot find ' + controllerDir + '/conf/' + appName + '.json');
|
||||
}
|
||||
}
|
||||
appName = getAppName();
|
||||
controllerDir = getControllerDir(typeof process !== 'undefined' && process.argv && process.argv.indexOf('--install') !== -1);
|
||||
const adapter = require(path.join(controllerDir, 'lib/adapter.js'));
|
||||
|
||||
exports.controllerDir = controllerDir;
|
||||
exports.getConfig = getConfig;
|
||||
exports.Adapter = adapter;
|
||||
exports.appName = appName;
|
||||
Reference in New Issue
Block a user