Created
June 16, 2011 06:46
-
-
Save dustin/1028791 to your computer and use it in GitHub Desktop.
Listen to my multicast temperatures. Store them in CouchDB.
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
{ "name": "temperature_listener", | |
"version": "0.1.0", | |
"dependencies": { "couchdb-api": ">=0.4.0" } | |
} |
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
dgram = require('dgram'); | |
couchdb = require("couchdb-api"); | |
server = couchdb.srv('minimata'); | |
db = server.db("temperature"); | |
socket = dgram.createSocket('udp4'); | |
sendData = (d) -> | |
doc = db.doc() | |
doc.body = d | |
doc.save((err, response) -> | |
if (err) | |
console.log(err, response) | |
) | |
socket.on("listening", (msg, rinfo) -> console.log("Listening.")) | |
socket.on("message", (msg, rinfo) -> | |
parts = msg.toString('utf8', 0, msg.length - 1).split('\t'); | |
parts[2] = parseFloat(parts[2]); | |
isoDate = parts[0].replace(/\//g, '-').replace(' ', 'T').split('.')[0]; | |
sendData( | |
ts: parts[0], | |
sn: parts[1], | |
reading: parseFloat(parts[2]), | |
type: 'reading', | |
_id: isoDate + '_' + parts[1]); | |
); | |
socket.addMembership('225.0.0.37'); | |
socket.bind(6789); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment