Last active
December 20, 2015 20:08
-
-
Save Fabryz/6187811 to your computer and use it in GitHub Desktop.
Pi-Lite Stock Ticker example made in Node.js
http://openmicros.org/index.php/articles/94-ciseco-product-documentation/raspberry-pi/302-pi-lite-stock-ticker
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
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