first test
This commit is contained in:
3
etc/actions/index.js
Normal file
3
etc/actions/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
set: require('./set.js')
|
||||
}
|
||||
51
etc/actions/set.js
Normal file
51
etc/actions/set.js
Normal file
@@ -0,0 +1,51 @@
|
||||
var net = require('net');
|
||||
|
||||
module.exports = function(query){
|
||||
var client = new net.Socket();
|
||||
|
||||
var addr = query.device.addr.split(':')
|
||||
|
||||
if(query.device.type != "recever"){
|
||||
return 1 // ERROR 1: devices isn't a recever
|
||||
}
|
||||
|
||||
var data = {}
|
||||
for (var i = 0; i < query.device.values.length; i++) {
|
||||
// TODO: error on not given required value(s) for devices
|
||||
if(query.hasOwnProperty(query.device.values[i].name)){
|
||||
var value = query[query.device.values[i].name]
|
||||
|
||||
switch (query.device.value[i].type) {
|
||||
case "number":
|
||||
if(value > query.device.value[i].max){
|
||||
return 3 // ERROR 3: invalid value
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return 2 // ERROR 2: invalid device value type
|
||||
}
|
||||
|
||||
data[query.device.values[i].name] = value
|
||||
}
|
||||
}
|
||||
|
||||
client.connect(addr[1], addr[0], function() {
|
||||
for (var key in value) {
|
||||
if (!value.hasOwnProperty(key)) continue;
|
||||
|
||||
client.write(key)
|
||||
client.write("=")
|
||||
client.write(value.key)
|
||||
}
|
||||
client.end()
|
||||
});
|
||||
|
||||
client.on('data', function(data) {
|
||||
if(data.toString() != "OK"){
|
||||
console.log("set query failt");
|
||||
}else{
|
||||
console.log("send sucesfull");
|
||||
}
|
||||
client.destroy();
|
||||
});
|
||||
}
|
||||
3
etc/config.js
Normal file
3
etc/config.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
listen: 8080
|
||||
}
|
||||
23
etc/devices.js
Normal file
23
etc/devices.js
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
devices = {
|
||||
simulation: {
|
||||
addr: "localhost:8081",
|
||||
name: "Test simulations",
|
||||
type: "recever",
|
||||
values: [
|
||||
{
|
||||
"name": "status",
|
||||
"type": "number",
|
||||
"max": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.get = function(deviceID){
|
||||
if(devices.hasOwnProperty(deviceID)){
|
||||
return devices[deviceID]
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
76
etc/index.js
Normal file
76
etc/index.js
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
var DEBUG = true;
|
||||
|
||||
var http = require('http'),
|
||||
devices = require('./devices.js'),
|
||||
config = require('./config.js'),
|
||||
action = require('./actions');
|
||||
|
||||
var server = http.createServer(function (req, res) {
|
||||
|
||||
var dir, query, queryTmp;
|
||||
|
||||
if(DEBUG){
|
||||
console.log("New request ======================")
|
||||
console.log("url: " + req.url);
|
||||
}
|
||||
|
||||
// split dirrectory and query from url
|
||||
// TODO: accept GET and POST.
|
||||
[dir, queryTmp] = req.url.split(/\?(.+)/)
|
||||
if(!queryTmp){
|
||||
queryTmp = req.read();
|
||||
}
|
||||
|
||||
// read query values
|
||||
if(queryTmp){
|
||||
queryTmp = queryTmp.split('&');
|
||||
query = {};
|
||||
for (var i = 0; i < queryTmp.length; i++) {
|
||||
queryTmp[i] = queryTmp[i].split('=')
|
||||
query[queryTmp[i][0]] = queryTmp[i][1]
|
||||
}
|
||||
}else{
|
||||
error(404, "No query data given")
|
||||
return
|
||||
}
|
||||
delete queryTmp
|
||||
|
||||
// check for a divice
|
||||
if(query.hasOwnProperty("device")){
|
||||
var device;
|
||||
if(device = devices.get(query.device)){
|
||||
query.device = device;
|
||||
}else{
|
||||
error(404,"Device not found");
|
||||
if(DEBUG)
|
||||
console.log("ERROR: (device: "+query.device+")");
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if(DEBUG)
|
||||
console.log(query)
|
||||
|
||||
// start the action
|
||||
switch (dir) {
|
||||
case "/set":
|
||||
action.set(query);
|
||||
break;
|
||||
default:
|
||||
error(404,"Invalid directory");
|
||||
if(DEBUG)
|
||||
console.log("ERROR: (dir: "+dir+")")
|
||||
return;
|
||||
}
|
||||
|
||||
res.end('{\r\n\t"errno":"0"\r\n}');
|
||||
|
||||
function error(code, msg){
|
||||
res.end('{\r\n\t"errno":"'+code+'",\r\n\t"error":"'+msg+'"\r\n}');
|
||||
if(DEBUG)
|
||||
console.log("ERROR: " + code + " " + msg)
|
||||
}
|
||||
})
|
||||
|
||||
server.listen(config.listen);
|
||||
Reference in New Issue
Block a user