Skip to content

Instantly share code, notes, and snippets.

@shamun
Created September 12, 2017 12:53
Show Gist options
  • Save shamun/a8e5fa96b753a02e3f53bc252544ee98 to your computer and use it in GitHub Desktop.
Save shamun/a8e5fa96b753a02e3f53bc252544ee98 to your computer and use it in GitHub Desktop.
test.md
/*
[Error]
Message=Unable to open TWAIN source
[Scan]
Pages=0
*/
// Basic loads
var fs = require("fs");
var http = require("http");
var timer_forever = null;
var timer_tick = 3000;
var read_filename = 'C:\\quickscan\\qs_done.dat';
// WebService
var kiosk = 'bri1';
var kioskID = '2';
var agentID = 'secure';
var webServiceURL = agentID + '.example.com';
function webServicePost(input) {
console.log(webServiceURL, kioskID, input);
var options = {
hostname: webServiceURL,
port: 80,
path: '/beehive/epson?userid=' + kioskID + '&status=' + input,
method: 'GET'
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
function parseINIString(data){
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([^=]+?)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
var value = {};
var lines = data.split(/[\r\n]+/);
var section = null;
lines.forEach(function(line){
if(regex.comment.test(line)){
return;
}
else if(regex.param.test(line)){
var match = line.match(regex.param);
if(section){
value[section][match[1]] = match[2];
}
else {
value[match[1]] = match[2];
}
}
else if(regex.section.test(line)){
var match = line.match(regex.section);
value[match[1]] = {};
section = match[1];
}
else if(line.length == 0 && section){
section = null;
};
});
return value;
}
function recall() {
timer_forever = setTimeout(function() {
clearTimeout(timer_forever);
listenFile();
}, timer_tick);
}
//
// NOTE:
// if the file do not exist then the 1st scan feedback will fail
// if the file name do exist before the service started. Then it will
// give feedback properly
//
function listenFile() {
try {
var data = fs.watch(read_filename, {persistent:true}, (eventType,filename) => {
try {
if(eventType=='change') {
var datas = fs.readFileSync(read_filename, 'utf8');
var data_to_ini = parseINIString(datas);
var scanned_page = data_to_ini['Scan']['Pages'];
webServicePost(scanned_page);
}
}
catch(ee) {
}
});
} catch(e) {
console.log('>>> Please wait...');
recall();
}
}
listenFile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment