Create a gist now

Instantly share code, notes, and snippets.

@andrewflash /main.js Secret
Last active Apr 24, 2016

Embed
What would you like to do?
QRCodeReader.js
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
// Leave the above lines for propper jshinting
var execSync = require('exec-sync');
var isProcessing = 0;
//var shellPython = require('python-shell');
var upmBuzzer = require("jsupm_buzzer");
var mBuzzer = new upmBuzzer.Buzzer(3); //Buzzer for sound notification
var app = require('express')(); //Express Library
var http = require('http');
http.post = require('http-post');
// Server Cloud
var serverAddress = "http://edison-hesahesa.rhcloud.com/edison/payment/iot";
var server = require('http').Server(app); //Create HTTP instance
var io = require('socket.io')(server); //Socket.IO Library
var mraa = require('mraa');
var LCD = require ('jsupm_i2clcd'); //LCD to display price & color notification
console.log('Current version of MRAA is', mraa.getVersion());
var touch = new mraa.Aio(0); //touch sensor for input testing
var touchValue;
var lcdMessage=" ";
var myLCD = new LCD.Jhd1313m1(6, 0x3E, 0x62);
var myLED = new mraa.Gpio(2);
myLED.dir(mraa.DIR_OUT);
var price = "20.000"; //sample price
var nowDate = new Date();
var trxID = "";
myLCD.setColor(0,0,255);
var success = true;
function readQRcode()
{
console.log("Reading QR code...");
//Take a picture from the USB camera - from command line
//var result = execSync('python /home/root/qrCodeScanner.py');
var resultPython = execSync('sudo python /home/root/qrCodeScanner.py');
//execSync('python',['/home/root/qrCodeScanner.py'],function(err,result){
console.log(resultPython);
console.log("Done capturing image");
isProcessing = 0;
return resultPython;
}
// run external command to activate USB
/*
function run_cmd(cmd, args, callBack ) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = "";
child.stdout.on('data', function (buffer) { resp += buffer.toString(); });
child.stdout.on('end', function() { callBack (resp); });
}*/
function showDisplay(LCD,msg1, msg2){
//update text display on LCD
LCD.clear();
LCD.setCursor(0,1);
LCD.write(msg1);
LCD.setCursor(1,15 - msg2.length);
LCD.write(msg2);
}
function successNotif(Buzzer, LED, LCD){
//give notification when transaction is success
LED.write(1);
LCD.setColor(0,255,0);
showDisplay(LCD,"SUCCESS!!", "Thank You :)");
Buzzer.playSound(1000, 100000);
Buzzer.playSound(2000, 200000);
Buzzer.stopSound();
LCD.setColor(0,0,255);
LED.write(0);
isProcessing = 0;
setTimeout(displayWelcome,5000);
}
function inputNotif(Buzzer, LED, LCD){
//give notification when transaction is success
LED.write(1);
LCD.setColor(255,255,0);
Buzzer.playSound(500, 50000);
Buzzer.stopSound();
LCD.setColor(255,255,0);
LED.write(0);
}
function failNotif(Buzzer, LED, LCD){
//give notification when transaction is failed
LCD.setColor(255,0,0);
showDisplay(LCD,"Transaction","ERROR :(");
Buzzer.playSound(20000, 600000);
Buzzer.stopSound();
LCD.setColor(255,0,0);
LED.write(0);
isProcessing = 0;
setTimeout(displayWelcome,5000);
}
// Timeout Notification
function timeoutNotif(Buzzer, LED, LCD){
//give notification when transaction is failed
LCD.setColor(255,0,0);
showDisplay(LCD,"Transaction","TIMEOUT :(");
Buzzer.playSound(20000, 600000);
Buzzer.stopSound();
LCD.setColor(255,0,0);
LED.write(0);
isProcessing = 0;
setTimeout(displayWelcome,5000);
}
// Handle Payment
function handlePayment() {
var result = readQRcode();
console.log("payment " + result);
if(result == "timeout") {
console.log("payment failed: timeout");
timeoutNotif(mBuzzer, myLED, myLCD); // timeout
} else if(result !== undefined) {
var resultSplit = result.split(" ");
if(resultSplit[1] == "QRCODE") {
if(resultSplit[3] !== ""){
var userToken = resultSplit[3].replace(/^"(.*)"$/, '$1');
console.log("userToken: " + userToken);
trxID = "TRX-" + nowDate.getYear() + nowDate.getMonth() + nowDate.getDate() + nowDate.getHours() + nowDate.getMinutes() + nowDate.getSeconds();
http.post(serverAddress, { paymenttoken: trxID, usertoken: userToken, ammount: price }, function(res){
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
if(chunk.indexOf("success") !== -1) {
console.log("payment success");
successNotif(mBuzzer,myLED,myLCD);
} else {
console.log("payment failed");
failNotif(mBuzzer, myLED, myLCD);
}
});
});
//showDisplay(myLCD,"","");
}
}
}
}
function displayWelcome(){
myLCD.setColor(0,0,255);
showDisplay(myLCD,"Welcome","ALSTUBLIEFT :)");
}
//listener for price input
io.on('connection', function(socket){
console.log("come here");
socket.on('changePrice', function(data){ //on incoming websocket message...
if(isProcessing === 0) { // Only handle one transcation at a time
inputNotif(mBuzzer,myLED,myLCD);
isProcessing = 1;
price = data;
showDisplay(myLCD, "TOTAL",price);
handlePayment();
}
});
});
server.listen(3000);
// Main function
function loop() {
lcdMessage = price;
var readTouch = touch.read();
readTouch = Math.round(readTouch*0.1);
mBuzzer.stopSound();
if(touchValue != readTouch){
touchValue = readTouch;
console.log(lcdMessage);
if(touchValue > 50){
success = !success; //alternate between success and fail
if(success){
successNotif(mBuzzer,myLED, myLCD);
}else{
failNotif(mBuzzer,myLED, myLCD);
}
}
}
setTimeout(loop,100);
}
displayWelcome();
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment