UNPKG

domotz-remote-pawn

Version:

Domotz Agent

96 lines (83 loc) 3.63 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz UK LLP * * 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 Massimiliano Cuzzoli <massi@domotz.com> on 26/08/16. * */ var PROTOCOL_VERSION = 1; var messageBuilderFactory = function (resourceLocator) { var logger = resourceLocator.log.decorateLogs(); var sortedHashTable = resourceLocator.utils.dataStructure.sortedHashTable; var lodash = resourceLocator.lodash; var serviceFilter = resourceLocator.discovery.serviceFilter; var cleanupUpnpUid = lodash.get(resourceLocator, 'configuration.settings.discovery_blacklist.upnp.cleanup_upnp_uid', true); var ignoredPaths = lodash.get(resourceLocator, 'configuration.settings.discovery_blacklist.upnp.ignored_paths', []); var sortedDictionaryKeys = resourceLocator.utils.dataStructure.sortedDictionaryKeys; return { buildSuccessMessage: function (messagePayload) { ignoredPaths.forEach(function (path) { if (lodash.get(messagePayload, path)) { logger.debug('cleaning UPnP path %s', path); lodash.update(messagePayload, path, function () { return null; }); } }); serviceFilter.cleanupObject(messagePayload); return { error: false, payload: messagePayload, }; }, buildErrorMessage: function (errorLevel, errorStatusCode, errorDescription) { return { error: true, level: errorLevel, status_code: errorStatusCode, description: errorDescription, }; }, buildUpnpDeviceMessageForEngine: function (messagePayload) { var clonedPayload = JSON.parse(JSON.stringify(messagePayload)); var cleanedPayload = lodash.pickBy(clonedPayload, function (value) { return value.result.error === false; }); if (!cleanedPayload || Object.keys(cleanedPayload).length === 0) { logger.info('No UPnP data collected'); return null; } // Sorting relevant fields of the message to ease message comparison clonedPayload = sortedHashTable(cleanedPayload); for (var location in clonedPayload) { if (clonedPayload.hasOwnProperty(location)) { if (cleanupUpnpUid) { logger.debug('upnp path cleanup: removed uid'); delete clonedPayload[location].uuid; } else { clonedPayload[location].uuid = sortedDictionaryKeys(clonedPayload[location].uuid); } } } return { version: PROTOCOL_VERSION, location: clonedPayload, }; }, }; }; module.exports.factory = messageBuilderFactory;