223 lines
No EOL
5.9 KiB
JavaScript
223 lines
No EOL
5.9 KiB
JavaScript
function BridgeClient(logCb) {
|
|
var fs = require('fs');
|
|
|
|
const bridgeConfigFile = './config/bridgeConfig.json';
|
|
|
|
var bridgeConfig = {
|
|
id: "",
|
|
ip: "",
|
|
user: ""
|
|
};
|
|
|
|
var RestClient = require('node-rest-client').Client;
|
|
var restClient = new RestClient();
|
|
var connectionId;
|
|
|
|
var that = this;
|
|
|
|
var generateUrl = function (section) {
|
|
var user = bridgeConfig.user;
|
|
if (user != "") {
|
|
user = user + "/";
|
|
}
|
|
return 'http://' + bridgeConfig.ip + '/api/' + user + section;
|
|
};
|
|
|
|
function srcIncludesPropertiesOfDest(src, dest) {
|
|
for (var propertyName in src) {
|
|
if (dest[propertyName] === undefined) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function propertiesAreEqual(propsA, propsB) {
|
|
return srcIncludesPropertiesOfDest(propsA, propsB) && srcIncludesPropertiesOfDest(propsB, propsA);
|
|
}
|
|
|
|
that.loadBridgeConfig = function () {
|
|
var success = false;
|
|
|
|
try {
|
|
var serialized = fs.readFileSync(bridgeConfigFile, 'utf8');
|
|
if (serialized) {
|
|
var obj = JSON.parse(serialized);
|
|
if (propertiesAreEqual(obj, bridgeConfig)) {
|
|
bridgeConfig = obj;
|
|
success = true;
|
|
}
|
|
}
|
|
}
|
|
catch (err) {
|
|
console.log(err);
|
|
}
|
|
|
|
return success;
|
|
};
|
|
|
|
function saveBridgeConfig() {
|
|
fs.writeFileSync(bridgeConfigFile, JSON.stringify(bridgeConfig), 'utf8');
|
|
}
|
|
|
|
that.isStreaming = function (done, error) {
|
|
var req = that.requestBridgeConfig(function (data) {
|
|
if (data.streaming !== undefined) {
|
|
done(data.streaming.enabled);
|
|
}
|
|
else {
|
|
done(false);
|
|
}
|
|
});
|
|
|
|
req.on('error', function (err) {
|
|
error("request error " + err.code);
|
|
});
|
|
|
|
req.on('requestTimeout', function (req) {
|
|
req.abort();
|
|
error("no device on ip");
|
|
});
|
|
|
|
req.on('responseTimeout', function (res) {
|
|
error("device not responding on ip");
|
|
});
|
|
};
|
|
|
|
that.checkAuthorization = function (done, error) {
|
|
var authorized = false;
|
|
var req = that.requestBridgeConfig(function (data) {
|
|
if (data.bridgeid === undefined) {
|
|
error("no bridge on ip");
|
|
return;
|
|
}
|
|
|
|
if (data.whitelist !== undefined) {
|
|
bridgeConfig.id = data.bridgeid;
|
|
authorized = true;
|
|
}
|
|
|
|
done(authorized);
|
|
});
|
|
|
|
req.on('error', function (err) {
|
|
console.log(err);
|
|
error("request error " + err.code);
|
|
});
|
|
|
|
req.on('requestTimeout', function (req) {
|
|
req.abort();
|
|
error("no device on ip");
|
|
});
|
|
|
|
req.on('responseTimeout', function (res) {
|
|
error("device not responding on ip");
|
|
});
|
|
};
|
|
|
|
that.requestBridgeConfig = function (done) {
|
|
var url = generateUrl('config');
|
|
var args = {
|
|
requestConfig: {timeout: 1000},
|
|
responseConfig: {timeout: 2000}
|
|
};
|
|
|
|
return restClient.get(url, args, function (data) {
|
|
done(data);
|
|
});
|
|
};
|
|
|
|
function createUser(done) {
|
|
var url = 'http://' + bridgeConfig.ip + '/api';
|
|
|
|
var args = {
|
|
data: {devicetype: "streaming_simulator#developer"},
|
|
headers: {"Content-Type": "application/json"},
|
|
requestConfig: {timeout: 1000},
|
|
responseConfig: {timeout: 2000}
|
|
};
|
|
restClient.post(url, args, function (data) {
|
|
if (data[0].success !== undefined) {
|
|
bridgeConfig.user = data[0].success.username;
|
|
saveBridgeConfig();
|
|
}
|
|
done();
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
if (that.loadBridgeConfig() == false) {
|
|
/* save the default config */
|
|
saveBridgeConfig();
|
|
}
|
|
}
|
|
|
|
that.isConnected = function (doneCb) {
|
|
that.checkAuthorization(function (isAuthorized) {
|
|
if (isAuthorized) {
|
|
doneCb(true);
|
|
}
|
|
else {
|
|
doneCb(false);
|
|
}
|
|
}, function(error){
|
|
logCb(error);
|
|
});
|
|
};
|
|
|
|
that.connect = function (doneCb) {
|
|
clearInterval(connectionId);
|
|
var creatingUser = false;
|
|
logCb('Connecting to ' + bridgeConfig.ip);
|
|
|
|
var execTask = function (delay) {
|
|
setTimeout(function () {
|
|
that.checkAuthorization(function (isAuthorized) {
|
|
if (isAuthorized) {
|
|
saveBridgeConfig();
|
|
logCb("Authorized");
|
|
doneCb();
|
|
}
|
|
else {
|
|
if (!creatingUser) {
|
|
logCb('Press pushlink button');
|
|
creatingUser = true;
|
|
createUser(function () {
|
|
creatingUser = false;
|
|
});
|
|
execTask(1000);
|
|
}
|
|
}
|
|
}, function (message) {
|
|
logCb(message);
|
|
doneCb();
|
|
});
|
|
}, delay);
|
|
};
|
|
|
|
execTask(0);
|
|
};
|
|
|
|
that.getConfig = function () {
|
|
return bridgeConfig;
|
|
};
|
|
|
|
that.changeConfig = function (changeSet) {
|
|
var success = false;
|
|
|
|
if (srcIncludesPropertiesOfDest(changeSet, bridgeConfig)) {
|
|
for (var propertyName in changeSet) {
|
|
bridgeConfig[propertyName] = changeSet[propertyName];
|
|
}
|
|
|
|
saveBridgeConfig();
|
|
success = true;
|
|
}
|
|
return success;
|
|
};
|
|
|
|
init();
|
|
}
|
|
|
|
// export the class
|
|
module.exports = BridgeClient; |