Skip to content

Instantly share code, notes, and snippets.

@dustin
Created June 16, 2011 06:46
Show Gist options
  • Save dustin/1028791 to your computer and use it in GitHub Desktop.
Save dustin/1028791 to your computer and use it in GitHub Desktop.
Listen to my multicast temperatures. Store them in CouchDB.
{ "name": "temperature_listener",
"version": "0.1.0",
"dependencies": { "couchdb-api": ">=0.4.0" }
}
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