Skip to content

Instantly share code, notes, and snippets.

@Fabryz
Last active December 20, 2015 20:08
Show Gist options
  • Save Fabryz/6187811 to your computer and use it in GitHub Desktop.
Save Fabryz/6187811 to your computer and use it in GitHub Desktop.
var http = require('http');
var xmldoc = require('xmldoc');
var SerialPort = require("serialport").SerialPort;
function parseXML(data) {
var document = new xmldoc.XmlDocument(data);
var symbol = document.valueWithPath("Data.Symbol");
var price = document.valueWithPath("Data.LastPrice");
// console.log(symbol +": "+ price);
if (typeof symbol !== "undefined") {
stockResult += symbol +": "+ price +" ";
}
}
function quotelookup(symbol) {
var options = {
hostname: 'dev.markitondemand.com',
port: 80,
path: '/Api/Quote?symbol='+ symbol,
method: 'GET'
};
var result = "";
var req = http.request(options, function(res) {
console.log('GET: ' + symbol + ' STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
result = parseXML(chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
}
var stockResult = [];
var stocklist = ['AAPL','FB','GOOG','MSFT','YHOO','DOWJ'];
var ticker_speed = 80;
stocklist.forEach(function(item) {
quotelookup(item);
});
var serialPort = new SerialPort("/dev/ttyAMA0", {
baudrate: 9600
}, false);
serialPort.open(function () {
console.log('Connected to Pi Lite');
serialPort.write("$$$SPEED"+ ticker_speed +"\r" + stockResult, function(err, results) {
if (err) {
console.log('Error: '+ err);
}
//console.log('Result: '+ results);
process.exit();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment