Last active
November 15, 2018 15:39
-
-
Save mathew-fleisch/db475d0ee3ec5945ca54d63d02ad6b0b to your computer and use it in GitHub Desktop.
sequence thing
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
'use strict'; | |
var echoPort = '/dev/pts/3'; | |
var track = ''; | |
var _require = require('child_process'), | |
spawn = _require.spawn, | |
exec = _require.exec; | |
var child = spawn('./dejarik'); | |
process.stdin.pipe(child.stdin); | |
var phase = 1; | |
var phaseTwoPosition = 0; | |
var upDown = true; | |
var seqTrig = false; | |
var seq = []; | |
var lastSeq = ''; | |
var debug = true; | |
child.stderr.on('data', function (data) { | |
if (debug) { | |
console.log(`${data}`); | |
} | |
}); | |
child.stdout.on('data', function (data) { | |
if (debug) { | |
console.log(`${data}`); | |
} | |
var lines = trim(data.toString()).split(/\n/); | |
for (var lineIndex in lines) { | |
var line = lines[lineIndex]; | |
process_line(line); | |
} | |
}); | |
function process_line(line) { | |
if (!debug) { | |
if (!line.match(/^\*|\#/)) | |
console.log(line); | |
} | |
if (line.match(/^#/)) { | |
track = parseInt(line.replace(/\/.*$/, '').replace(/^#/, '')) | |
// console.log('#' + track) | |
if (seqTrig) { | |
seqTrig = false; | |
} else { | |
seqTrig = true; | |
} | |
} | |
if (seqTrig) { | |
var thisLine = line.replace(/.*:\s*/, ''); | |
if (thisLine) { | |
//console.log(`add line to sequence: ${line}`) | |
seq.push(line.replace(/.*:\s*/, '')); | |
//console.log(`sequence: ${JSON.stringify(seq)}`) | |
} | |
} else { | |
//console.log(`sequence: ${JSON.stringify(seq)}`) | |
if (seq.length > 0) { | |
if (seq.length > 1) { | |
if (seq[1].match(/^Phase ONE/)) { | |
phase = 2; | |
seq = []; | |
seqTrig = true; | |
} else if (seq[1].match(/^Phase TWO/)) { | |
phase = 3; | |
seq = []; | |
seqTrig = true; | |
} | |
} | |
if (seq.length > 0) { | |
sendSequence(seq); | |
seqTrig = false; | |
seq = []; | |
} | |
} | |
} | |
} | |
var lastFirst = true; | |
function sendSequence(seq) { | |
if (!seq.length) { return; } | |
console.log("[p" + phase + "][" + pad(track, 3) + "] => " + JSON.stringify(seq)); | |
switch (phase) { | |
case 1: | |
if (track == 26 || track == 41 || track == 56 || track == 71) { | |
upDown = true; | |
} | |
if (track > 10) { | |
if (upDown) { | |
// console.log('Remove the first element from the sequence: ' + seq[0]); | |
seq.shift(); | |
} | |
if (seq.length <= 1) { | |
upDown = false; | |
} | |
} | |
break; | |
case 2: | |
// console.log(' prev: ' + lastSeq); | |
if (lastFirst) { | |
var last = seq.pop(); | |
seq.splice(0, 0, last); | |
} else { | |
var first = seq.shift(); | |
seq.push(first); | |
} | |
if (JSON.stringify(seq) === lastSeq) { | |
if (lastFirst) { | |
var first = seq.shift(); | |
seq.push(first); | |
var second = seq.shift(); | |
seq.push(second); | |
lastFirst = false; | |
} else { | |
var last = seq.pop(); | |
seq.splice(0, 0, last); | |
var second = seq.pop(); | |
seq.splice(0, 0, second); | |
lastFirst = true; | |
} | |
} | |
// console.log(" after => " + JSON.stringify(seq)); | |
lastSeq = JSON.stringify(seq) | |
break; | |
} | |
var cmd = ''; | |
for (var seqInd in seq) { | |
cmd += (cmd.length > 0 ? ' && ' : '') + "echo " + seq[seqInd] + " > " + echoPort; | |
} | |
// if (phase == 2) | |
// console.log("cmd: " + cmd); | |
exec("" + cmd, function (err, stdout, stderr) { | |
if (err) { | |
console.error("exec error: " + err); | |
return; | |
} | |
// console.log("successful!!!"); | |
}); | |
} | |
function trim(str) { | |
return str.replace(/^\s*/, '').replace(/\s*$/, ''); | |
} | |
function pad(n, width, z) { | |
z = z || '0'; | |
n = n + ''; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment