This commit is contained in:
raluca-elena 2014-08-04 22:25:52 -07:00
Родитель 099a6e5cb3
Коммит f1daf6c7d6
10 изменённых файлов: 1 добавлений и 549 удалений

Просмотреть файл

@ -1,102 +0,0 @@
var allTags = {};
exports.init = function init(cb) {
allTags = {"led3": {"url": "pi1/led1"}, "led2": {"url": "pi2/led2"}};
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.
*/
}
exports.queryTags = function queryTags(tags, cb) {
// ["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 = {
'pi1/led1': 'off',
'pi2/led2': 'on',
};
function wget(url, cb) {
var data = webDump[url];
if (data === undefined) {
cb(undefined, "No data for url: " + url + " :(");
} else {
cb(data); // no error
}
}
function wput(url, data, cb) {
webDump[url] = data;
cb('empty-wput-response'); // no error
}
exports.readTag = function readTag(tag, cb) {
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);
}
}
exports.writeTag = function writeTag(tag, data, cb) {
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'},
}
}
exports.getFeatures = function getFeatures(cb) {
cb(allFeatures);
}
exports.setTag = function setTag(uuid, feature, tag, cb) {
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
}
}
exports.deleteTag = function deleteTag(tag, cb) {
if (allTags[tag] == undefined) {
cb("No such tag: " + tag + " :(");
} else {
delete allTags[tag];
cb(); // no error
}
}

Просмотреть файл

@ -1,47 +0,0 @@
var gpio = require("pi-gpio");
function turnOnLeds(cb) {
gpio.open(7, "output", function(err) { // Open pin 7
console.log("set ddr for led1");
gpio.write(7, 1, function() { // Set pin 7 high (1)
console.log("led1/pin7 high")
cb(7);
});
});
gpio.open(11, "output", function(err) { // Open pin 11 for output
console.log("set ddr for led2");
gpio.write(11, 1, function() { // Set pin 11 high (1)
console.log("led2/pin11 high");
cb(11);
});
});
}
function readLedState(nr) {
gpio.read(nr, function(err, value) {
if(err) throw err;
console.log("on pin" + nr + " I get this value " , value); // The current state of the pin
gpio.close(nr);
});
}
turnOnLeds(readLedState);
function turnOffLeds() {
gpio.open(7, "output", function(err) { // Open pin 7
console.log("set ddr for led1 second time");
gpio.write(7, 0, function() { // Set pin 7 low (0)
console.log("led1/pin7 low");
readLedState(7);
});
});
gpio.open(11, "output", function(err) { // Open pin 11 for output
console.log("set ddr for led2 second time");
gpio.write(11, 0, function() { // Set pin 11 low (0)
console.log("led2/pin11 low");
readLedState(11);
});
});
}
setTimeout(turnOffLeds, 30000);

Просмотреть файл

@ -1,71 +0,0 @@
var rot = require('./rot');
function forEach(obj, cb) {
for (var e in obj) {
cb(e);
}
}
function readTags(tags, cb) {
var readValues = {};
forEach(tags, function(tag) {
console.log("tag: ", tag);
rot.readTag(tag, function(data, err) {
readValues[tag] = data;
if (err)
console.log("Error from readTag: ", err);
if (Object.keys(readValues).length === Object.keys(tags).length)
cb(readValues);
});
});
}
function turnOffLigths(tags, cb) {
var readValues = {};
forEach(tags, function(tag) {
console.log("tag: ", tag);
rot.writeTag(tag, "off", function(data, err) {
readValues[tag] = data;
if (err)
console.log("Error from readTag: ", err);
if (Object.keys(readValues).length === Object.keys(tags).length)
cb(readValues);
});
});
}
var feature1 = 'gigagator-ultra-leds-inqqpa2';
var feature2 = 'factory-name-for-led1-C00FF33';
function setTags(cb) {
// exports.setTag = function setTag(uuid, feature, tagName, cb) {
console.log('--- setTag: called: ', arguments);
rot.setTag('uuid1', "factory-name-for-led1-C00FF33", 'led1', function(err) {
if (err) {
cb(err);
return;
}
rot.setTag('uuid2', "gigagator-ultra-leds-inqqpa2", 'led2', cb);
});
}
rot.init(function() {
console.log('init');
console.log("OMG: allFeatures:", allFeatures)
setTags(function(err) {
if (err) {
console.log("Error in setTags: ", err);
return;
}
rot.queryTags(['led1', 'led2'], function(tags, err) {
console.log("queryTags: ", tags, err);
readTags(tags, function (tagsData) {
console.log("read all tags: ", tagsData);
turnOffLigths(tags, function(ligthsData) {
console.log("turnOff responses: ", tagsData);
readTags(tags, console.log);
});
});
});
});
});

Просмотреть файл

@ -1,80 +0,0 @@
//get all features on all slaves
//var baseurl = "http://10.251.43.233:8080/ls";
var baseurl = "http://10.0.0.6:8080/ls";
function getFeatures(uuid) {
var url = "http://10.0.0.6:8080/thing/" + uuid;
var xhr = new XMLHttpRequest();
xhr.onload = function (e) {
if (e.target.status == 200) {
var features = JSON.parse(e.target.responseText);
console.log("FEATURES ", features.localURL.split(":")[1]);
var anotherUrl = features.localURL.split(":")[1] + ":80" + "/features";
console.log("another url", anotherUrl);
getFeaturesFromSlave(anotherUrl);
} else {
console.log("BALBALALALALALZZZZZZZZZALA " + url + " with " +
e.target.status);
}
};
xhr.open("get", url, true);
xhr.send();
}
function getFeaturesFromSlave(url) {
console.log("URL IS ", url);
var xhr = new XMLHttpRequest();
xhr.onload = function (e) {
if (e.target.status == 200) {
var features = JSON.parse(e.target.responseText);
console.log("kdshsakhdkashdkashdAAAAAAAA----", features)
} else {
console.log("BALBALALALALALZZZZZZZZZALA " + url + " with " +
e.target.status);
}
};
console.log("<><><><> ", url);
xhr.open("get", url, true);
xhr.send();
}
function getAllFeatures() {
//$.ajax({"url":baseurl + "/ls"}).done(function (data) {
var xhr = new XMLHttpRequest();
xhr.onload = function (e) {
if (e.target.status == 200) {
var things = JSON.parse(e.target.responseText);
for (var i = 0; i < things.length; i++) {
console.log("the thing is ", things[i]);
getFeatures(things[i]);
}
} else {
console.log("BALBALALALALALALA " + url + " with " +
e.target.status);
}
};
xhr.open("get", baseurl, true);
xhr.send();
console.log("dlkadlsflsdhflsd");
//ls -- to registry server
//for every elem in ls call /thing/elem --> urls to things --> tags {cam1:link}
//make a dictionary to all and keep that in mem
//for every thing url ->/features --> list of features --> {}
}
function queryTags(tags) {
//ls -- to registry server
//for every elem in ls call /thing/elem --> urls to things --> tags {cam1:link}
//make a dictionary to all and keep that in mem
//for every thing url ->/features --> list of features --> {}
//for a list of features queryTags(["securityCamera", "garageDoorRelay"]) --> list of urls
//read data from urls/ write data to urls
}
getAllFeatures();

Просмотреть файл

@ -1,34 +0,0 @@
var defaultUrl = "localhost:8090/?action=stream"
var http = require("http");
var url = require("url");
var express = require('express');
var getRawBody = require('raw-body');
var cors = require('cors');
var gpio = require("pi-gpio");
var request = require("superagent");
var argv = process.argv;
var app = express();
var port = argv[2];
//handle post/put
app.use(function (req, res, next) {
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8'
}, function (err, string) {
if (err)
return next(err);
req.text = string;
next();
})
});
app.use(cors());
app.get("/", function(req, res) {
res.send(defaultUrl);
});
app.listen(port);

Просмотреть файл

@ -1,50 +0,0 @@
var http = require("http");
var url = require("url");
var express = require('express');
var getRawBody = require('raw-body');
var cors = require('cors');
function generate(){
var x = Math.random()* (255 - 0) + 0;;
console.log("random value is", x);
return x;
}
var argv = process.argv;
var port = argv.split(" ");
port.shift();
port.shift();
var app = express();
//handle post/put
app.use(function (req, res, next) {
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8'
}, function (err, string) {
if (err)
return next(err)
req.text = string
next()
})
})
app.get("/", function(req, res) {
var x =generate();
console.log("random value between 0 and 255", x);
res.json(x);
})
app.put(/^\/tags\/[A-z:.-]+$/, function(req, res, next) {
var tagName = req.path.substr("/tags/".length);
console.log('i am in put and i want to write a tag : ', tagName);
state[tagName] = req.text;
commit(function(err) {
if (err)
return next(err);
res.send("OK");
});
})
app.listen(6666);

Просмотреть файл

@ -1,42 +0,0 @@
var http = require("http");
var url = require("url");
var express = require('express');
var getRawBody = require('raw-body');
var cors = require('cors');
var argv = process.argv;
var port = argv[2]
var app = express();
//handle post/put
app.use(function (req, res, next) {
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8'
}, function (err, string) {
if (err)
return next(err)
req.text = string
next()
})
});
app.use(cors());
var ledState = "off";
app.get("/", function(req, res) {
console.log("GET /, fake led state is: ", ledState);
res.send(ledState);
})
app.put("/", function(req, res, next) {
console.log("PUT /, fake led state state is: ", ledState, " new value is: ", req.text);
ledState = req.text;
res.send(ledState);
})
app.listen(port);

Просмотреть файл

@ -1,88 +0,0 @@
var http = require("http");
var url = require("url");
var express = require('express');
var getRawBody = require('raw-body');
var cors = require('cors');
var gpio = require("pi-gpio");
var argv = process.argv;
var port = argv[2];
var pin = argv[3];
if (pin == undefined)
pin = 11;
var app = express();
//led on pin 11
//handle post/put
app.use(function (req, res, next) {
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8'
}, function (err, string) {
if (err)
return next(err);
req.text = string;
next();
})
});
app.use(cors());
function setLed(nr){
gpio.open(pin, "output", function(err) { // Open pin 11
console.log("set ddr for led1");
//var val = 0; //default led state set to high
gpio.write(pin, 1, function(err, value) {
if(err) throw err;
console.log("on pin" + nr + " I get this value " , value);
readLedState(pin);
})
//gpio.close(pin);
})
}
setLed();
function readLedState(nr, res) {
gpio.read(nr, function(err, value) {
if(err) throw err;
console.log("on pin" + nr + " state is: " , value); // The current state of the pin
ledState = value;
if (res != undefined) {
console.log("send a response with led state: ", value);
if (value == 1) {
res.send("on");
} else {
res.send("off");
}
}
//gpio.close(pin);
});
}
function writeState(state, cb) {
console.log("set ddr for led1");
var val = 0;
if (state == "on")
val = 1;
gpio.write(pin, val, function() { // Set pin 11 high (1)
console.log("led1/pin11 high ", val);
//cb();
});
}
app.get("/", function(req, res) {
console.log("GET /, led state is: ", ledState);
readLedState(pin, res);
})
app.put("/", function(req, res, next) {
console.log("PUT /, led state is: ", ledState, " new value is: ", req.text);
ledState = req.text;
writeState(ledState, readLedState(pin, res));
})
app.listen(port);
var ledState = 0;

Просмотреть файл

@ -27,7 +27,7 @@ app.use(cors());
app.post("/", function(req, res, next) {
var text = req.text;
console.log("POST /, text is : ", text);
require('child_process').spawn('node', ["textToSpeechServer.js", text]);
require('child_process').spawn('node', ["textToSpeech.js", text]);
res.send("text processed is " + text);
});

Просмотреть файл

@ -1,34 +0,0 @@
var argv = process.argv;
console.log("text to process ***************", argv[2]);
var spawn = require('child_process').spawn,
echo = spawn('echo', [argv[2]]),
festival = spawn('festival', ['--tts']);
echo.stdout.on('data', function (data) {
festival.stdin.write(data);
});
echo.stderr.on('data', function (data) {
console.log('echo stderr: ' + data);
});
echo.on('close', function (code) {
if (code !== 0) {
console.log('echo process exited with code ' + code);
}
festival.stdin.end();
});
festival.stdout.on('data', function (data) {
console.log('' + data);
});
festival.stderr.on('data', function (data) {
console.log('festival stderr: ' + data);
});
festival.on('close', function (code) {
if (code !== 0) {
console.log('festival process exited with code ' + code);
}
});