Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function QueryData() { | ||
|
||
} | ||
|
||
QueryData.returnJSON = function(result, res) { | ||
if (result.length == 2) { | ||
res.code = '4.04'; | ||
res.end(JSON.stringify({ | ||
error: "Not Found" | ||
})); | ||
} else { | ||
res.code = '2.05'; | ||
res.end(result); | ||
} | ||
}; | ||
|
||
module.exports = QueryData; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
var sqlite3 = require('sqlite3').verbose(); | ||
var fs = require("fs"); | ||
var file = "./iot.json"; | ||
var _ = require("underscore"); | ||
|
||
config = { | ||
"db_name": "iot.db", | ||
"db_table": "id integer primary key, value text, sensors1 float, sensors2 float", | ||
"init_table":[ | ||
"insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, 'is id 1', 19, 20);", | ||
"insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, 'is id 2', 20, 21);" | ||
], | ||
"query_table":"select * from basic;" | ||
}; | ||
|
||
function DBHelper(){ | ||
|
||
} | ||
|
||
DBHelper.initDB = function(){ | ||
var db = new sqlite3.Database(config["db_name"]); | ||
var create_table = 'create table if not exists basic (' + config["db_table"] + ');'; | ||
|
||
db.serialize(function() { | ||
db.run(create_table); | ||
_.each(config["init_table"], function(insert_data) { | ||
db.run(insert_data); | ||
}); | ||
}); | ||
db.close(); | ||
}; | ||
|
||
DBHelper.urlQueryData = function (url, callback) { | ||
var db = new sqlite3.Database("iot.db"); | ||
|
||
var result = []; | ||
console.log("SELECT * FROM basic where " + url.split('/')[1] + "=" + url.split('/')[2]); | ||
db.all("SELECT * FROM basic where " + url.split('/')[1] + "=" + url.split('/')[2], function(err, rows) { | ||
db.close(); | ||
callback(JSON.stringify(rows)); | ||
}); | ||
}; | ||
|
||
module.exports = DBHelper; |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,23 @@ | ||
var sqlite3 = require('sqlite3').verbose(); | ||
var fs = require("fs"); | ||
var file = "./iot.json"; | ||
var _ = require("underscore"); | ||
|
||
fs.readFile(file, 'utf8', function(err, data) { | ||
if (err) { | ||
console.log('Error: ' + err); | ||
return; | ||
var DBHelper = require('./db_helper.js'); | ||
var coap = require('coap'); | ||
var server = coap.createServer({}); | ||
var QueryData = require('./QueryData.js'); | ||
|
||
DBHelper.initDB(); | ||
|
||
server.on('request', function(req, res) { | ||
if (req.headers['GET'] !== 0) { | ||
res.setOption('Accept', 'application/json'); | ||
DBHelper.urlQueryData(req.url, function(result) { | ||
QueryData.returnJSON(result, res); | ||
}); | ||
} else { | ||
res.end(JSON.stringify({ | ||
error: "sorry" | ||
})); | ||
} | ||
config = JSON.parse(data); | ||
start_server(); | ||
}); | ||
|
||
function parse_url(url, callback) { | ||
var db = new sqlite3.Database(config["db_name"]); | ||
|
||
var result = []; | ||
console.log("SELECT * FROM basic where " + url.split('/')[1] + "=" + url.split('/')[2]); | ||
db.all("SELECT * FROM basic where " + url.split('/')[1] + "=" + url.split('/')[2], function(err, rows) { | ||
db.close(); | ||
callback(JSON.stringify(rows)); | ||
}); | ||
} | ||
|
||
function start_server() { | ||
|
||
var db = new sqlite3.Database(config["db_name"]); | ||
var create_table = 'create table if not exists basic (' + config["db_table"] + ');'; | ||
console.log(create_table); | ||
|
||
db.serialize(function() { | ||
db.run(create_table); | ||
_.each(config["init_table"], function(insert_data) { | ||
db.run(insert_data); | ||
}); | ||
}); | ||
|
||
db.close(); | ||
|
||
var coap = require('coap'); | ||
var server = coap.createServer({}); | ||
|
||
server.on('request', function(req, res) { | ||
console.log(req.headers); | ||
if (req.headers['GET'] !== 0) { | ||
res.setOption('Accept', 'application/json'); | ||
parse_url(req.url, function(result) { | ||
if( result.length == 2){ | ||
res.code = '4.04'; | ||
return res.end(JSON.stringify({ | ||
error: "Not Found" | ||
})); | ||
} | ||
res.code = '2.05'; | ||
return res.end(result); | ||
}); | ||
}else { | ||
res.end(JSON.stringify({ | ||
error: "sorry" | ||
})); | ||
} | ||
}); | ||
|
||
server.listen(function() { | ||
console.log('server started'); | ||
}); | ||
|
||
} | ||
server.listen(function() { | ||
console.log('server started'); | ||
}); |