domotz-remote-pawn
Version:
Domotz Agent
61 lines (54 loc) • 2.19 kB
JavaScript
/** 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 tom on 04/02/16.
*/
var q = require('q');
var factory = function (configuration, engineRequest, devicesInfo, interfacesBindingStorage) {
var SERVICE_NAME = "ANALYZE_TYPE";
var dit = configuration.dummy_internal_type;
var dummyInternalType = (dit !== undefined) ? dit : 0;
function askEngineToAnalyzeDevice(device) {
"use strict";
if (!device) {
console.info("ANALYZE_TYPE: Ignoring missing device - most probably the device has been just discovered");
} else if (device.type.internal_id === dummyInternalType) {
console.info('ANALYZE_TYPE: Requesting type analysis for device. ' +
'MAC:' + device.mac + ', ID: ' + device.id);
engineRequest.send('/device/' + device.id + '/type',
'PUT', {});
devicesInfo.setUpdateTimeToNow(device.mac, SERVICE_NAME);
}
}
function request() {
var devices = interfacesBindingStorage.listFresh();
var promises = [];
devices.forEach(function (dev) {
var mac = dev.mac;
if (devicesInfo.isToBeUpdated(mac, SERVICE_NAME)) {
promises.push(devicesInfo.get(mac).then(ret.askEngineToAnalyzeDevice));
}
});
return q.all(promises);
}
var ret = {
request: request,
askEngineToAnalyzeDevice: askEngineToAnalyzeDevice
};
return ret;
};
module.exports.factory = factory;