2014-07-24 12:07:11 +04:00
|
|
|
|
|
|
|
var allTags = {};
|
2014-07-24 12:57:42 +04:00
|
|
|
exports.init = function init(cb) {
|
2014-07-24 15:17:18 +04:00
|
|
|
allTags = {"led3": {"url": "pi1/led1"}, "led2": {"url": "pi2/led2"}};
|
2014-07-24 12:07:11 +04:00
|
|
|
cb();
|
|
|
|
|
|
|
|
/*
|
|
|
|
= get my uuids => GET /ls on directory => [uuids]
|
|
|
|
= for each uuid => GET /thing/uuid/ => info registeded by each device/slave. {"uuid":uuid, "localURL": slave-rest-endpoint}
|
|
|
|
= for each uuid => GET /tags/. First time, no tags defined (likely): brand new device.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2014-07-24 12:57:42 +04:00
|
|
|
exports.queryTags = function queryTags(tags, cb) {
|
2014-07-24 12:07:11 +04:00
|
|
|
// ["tag1", "tag2"], cb (resp, err)); where resp: {"tag1": ?, "tag2": ?}
|
|
|
|
var resp = {};
|
|
|
|
for (var i = 0; i < tags.length; i++) {
|
|
|
|
resp[tags[i]] = allTags[tags[i]];
|
|
|
|
}
|
|
|
|
cb(resp); // no error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate the internets
|
|
|
|
var webDump = {
|
2014-07-24 12:57:42 +04:00
|
|
|
'pi1/led1': 'off',
|
|
|
|
'pi2/led2': 'on',
|
2014-07-24 12:07:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
function wget(url, cb) {
|
2014-07-24 12:57:42 +04:00
|
|
|
var data = webDump[url];
|
2014-07-24 12:07:11 +04:00
|
|
|
if (data === undefined) {
|
|
|
|
cb(undefined, "No data for url: " + url + " :(");
|
|
|
|
} else {
|
|
|
|
cb(data); // no error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function wput(url, data, cb) {
|
|
|
|
webDump[url] = data;
|
2014-07-24 12:57:42 +04:00
|
|
|
cb('empty-wput-response'); // no error
|
2014-07-24 12:07:11 +04:00
|
|
|
}
|
|
|
|
|
2014-07-24 12:57:42 +04:00
|
|
|
exports.readTag = function readTag(tag, cb) {
|
2014-07-24 12:07:11 +04:00
|
|
|
var tagData = allTags[tag];
|
|
|
|
if (tagData === undefined) {
|
|
|
|
cb(undefined, "No such tag: " + tag + " :(");
|
|
|
|
} else if (tagData['url'] === undefined) {
|
|
|
|
cb(undefined, "No url for tag: " + tag + " :(");
|
|
|
|
} else {
|
|
|
|
wget(tagData['url'], cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 12:57:42 +04:00
|
|
|
exports.writeTag = function writeTag(tag, data, cb) {
|
2014-07-24 12:07:11 +04:00
|
|
|
var tagData = allTags[tag];
|
|
|
|
if (tagData === undefined) {
|
|
|
|
cb(undefined, "No such tag: " + tag + " :(");
|
|
|
|
} else if (tagData['url'] === undefined) {
|
|
|
|
cb(undefined, "No url for tag: " + tag + " :(");
|
|
|
|
} else {
|
|
|
|
wput(tagData['url'], data, cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var allFeatures = {
|
|
|
|
'uuid1': {
|
|
|
|
"factory-name-for-led1-C00FF33": {'url': 'pi1/led1'},
|
|
|
|
"useless-device-MQYOK7N1WA*#22": {'url': 'pi1/sadDevice1'},
|
|
|
|
},
|
|
|
|
'uuid2': {
|
|
|
|
"wat-is-dis-3493YAT1PL--24": {'url': 'pi2/lol1'},
|
|
|
|
"gigagator-ultra-leds-inqqpa2": {'url': 'pi2/led2'},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 12:57:42 +04:00
|
|
|
exports.getFeatures = function getFeatures(cb) {
|
2014-07-24 12:07:11 +04:00
|
|
|
cb(allFeatures);
|
|
|
|
}
|
|
|
|
|
2014-07-24 15:17:18 +04:00
|
|
|
exports.setTag = function setTag(uuid, feature, tag, cb) {
|
2014-07-24 12:07:11 +04:00
|
|
|
if (allFeatures[uuid] === undefined) {
|
|
|
|
cb("No such uuid: " + uuid + " :(");
|
|
|
|
} else if (allFeatures[uuid][feature] === undefined) {
|
|
|
|
cb("No such feature: " + feature + " for uuid: " + uuid + " :(");
|
|
|
|
} else if (tag in allTags) {
|
|
|
|
cb("Tag alread used: " + tag + " :(");
|
|
|
|
} else {
|
|
|
|
// allTags = {"led1": {"url": "pi1/led1"}, "led2": {"url": "pi2/led2"}};
|
|
|
|
allTags[tag] = allFeatures[uuid][feature];
|
|
|
|
cb(); // no error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 12:57:42 +04:00
|
|
|
exports.deleteTag = function deleteTag(tag, cb) {
|
2014-07-24 12:07:11 +04:00
|
|
|
if (allTags[tag] == undefined) {
|
|
|
|
cb("No such tag: " + tag + " :(");
|
|
|
|
} else {
|
|
|
|
delete allTags[tag];
|
|
|
|
cb(); // no error
|
|
|
|
}
|
|
|
|
}
|
2014-07-24 12:57:42 +04:00
|
|
|
|