UNPKG

domotz-remote-pawn

Version:

Domotz Agent

68 lines (61 loc) 2.42 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 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 Massimiliano Cuzzoli <mcuzzoli@domotz.com> on 20/04/15. * */ var registry = function (deepEqual) { var subscriptions = {}; function createKey(host, port, path) { return JSON.stringify(host, port, path); } var ret = { insertSubscription: function (subscription) { subscriptions[createKey(subscription.device_info)] = subscription; }, getSubscriptionById: function (id) { for (var key in subscriptions) { if (subscriptions.hasOwnProperty(key) && subscriptions[key].id === id) { return subscriptions[key]; } } }, getSubscriptionByDevice: function (host, port, path) { if (port === undefined) { port = host[1]; path = host[2]; host = host[0]; } return subscriptions[createKey([host, port, path])]; }, updateSubscription: function (id, newSubscription) { var oldSubscription = ret.getSubscriptionById(id); if (!oldSubscription) { throw new Error('subscription id not found: ' + id); } if (!deepEqual(oldSubscription.device_info, newSubscription.device_info)) { throw new Error('Cannot update when changing device info: ' + id); } subscriptions[createKey(newSubscription.device_info)] = newSubscription; }, removeSubscription: function (subscription) { delete subscriptions[createKey(subscription.device_info)]; }, }; return ret; }; module.exports.registry = registry;