Last active
August 29, 2015 14:22
-
-
Save ehgoodenough/191ada24e03c187a4b0e to your computer and use it in GitHub Desktop.
This is our example server for streaming data from the tablet to the database.
This file contains hidden or 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
node_modules |
This file contains hidden or 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
{ | |
"name": "levelup", | |
"version": "0.0.0", | |
"main": "server.js", | |
"dependencies": { | |
"body-parser": "~1.12.4", | |
"express": "~4.12.4", | |
"mongojs": "~1.0.0", | |
"pg": "~4.4.0" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "https://gist.github.com/ehgoodenough/191ada24e03c187a4b0e" | |
} | |
} |
This file contains hidden or 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
var pg = require("pg") | |
var express = require("express") | |
var bodyparser = require("body-parser") | |
var server = express() | |
server.get("/datapoint", function(request, response) { | |
console.log("a request has been made") | |
response.status(200).send("OK :D") | |
}) | |
server.use(bodyparser.json()) | |
server.use(bodyparser.urlencoded()) | |
var database = new pg.Client("postgres://lhgpamej:[email protected]:5432/lhgpamej") | |
database.connect(function(error) { | |
if(error) { | |
console.log(error) | |
} else { | |
server.post("/datapoint", function(request, response) { | |
response.status(200).send("OK!") | |
var data = request.body | |
var timestamp = parseFloat(data.timestamp) | |
var translation = data.translation.split(", ").map(function(value) {return parseFloat(value)}) | |
var rotation = data.rotation.split(", ").map(function(value) {return parseFloat(value)}) | |
console.log(timestamp, translation, rotation) | |
database.query("INSERT INTO datapoints (timestamp, tx, ty, tz, rx, ry, rz, rq)" | |
//+ " VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", new Array().push(timestamp).join(translation).join(rotation)) | |
+ " VALUES (" + timestamp + ", " + translation[0] + ", " + translation[1] + ", " + translation[2] + ", " + | |
+ rotation[0] + ", " + rotation[1] + ", " + rotation[2] + ", " + rotation[3] + ")") | |
}) | |
} | |
}) | |
server.listen(80, function() { | |
console.log("Listening on 80") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment