Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Steam Train powered by the Steem Chain!
// this was really a quick hack :)
const PoweredUP = require("node-poweredup");
const poweredUP = new PoweredUP.PoweredUP();
var dsteem = require('dsteem')
message('fireing up this script');
function message(msg){
process.stdout.write('\033c');
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(msg);
}
function blinkLed(train) {
train.setLEDColor(1);
train.sleep(200);
train.setLEDColor(0);
return true;
}
function setMotorSpeed(train,speed) {
train.setMotorSpeed("MOTOR",speed);
}
poweredUP.on("discover", async (hub) => { // Wait to discover a Hub
await hub.connect(); // Connect to the Hub
message(hub.name + ' connected');
i = 1
while (true) { // Repeat indefinitely
message('set color '+ i);
//await hub.setMotorSpeed("MOTOR", 100, 1000);
await hub.setLEDColor(i);
await hub.sleep(10); // Do nothing for 1 second
i++;
if(i > 10) {
break;
}
}
// i = 0
//
// var soundarr = [3,5,7,9,10];
//
// while (true) { // Repeat indefinitely
// message('play sound '+ soundarr[i]);
// //await hub.setMotorSpeed("MOTOR", 100, 1000);
// await hub.playSound(soundarr[i]);
// await hub.sleep(1700); // Do nothing for 1 second
// i++;
// if(i > 4) {
// break;
// }
// }
var currentspeed = 0
var currenttxsize = 0
var maxtxsize = 0
var blockcount = 0;
var totaltx = 0;
// var last_irreversible = 0;
var averagespeed = 75;
var client = new dsteem.Client('https://api.steemit.com')
stream = client.blockchain.getBlockStream();
stream.on('data', function(block) {
//console.log(block);
blinkLed(hub);
if(block.witness == "roelandp") {
hub.playSound(5);
}
// analyse tx length
var txsize = block.transactions.length
totaltx += txsize;
blockcount++;
var avgtx = totaltx / blockcount;
// if tx length > maxtxsize = maxtxsize = tx.length;
var modifier = (txsize - avgtx) / avgtx;
var speedmodifier = 75 * modifier;
var trainspeed = Math.ceil((averagespeed + speedmodifier))
if(trainspeed < 50){
trainspeed = 50;
}
if(trainspeed > 100){
trainspeed = 100;
}
setMotorSpeed(hub, trainspeed)
message(block.block_id + ' signed by '+ block.witness + ' on ' + block.timestamp + '\nTransactions in block: + '+ block.transactions.length + 'tx\n\nAverage transactions: '+ avgtx+'\nModifier:'+ modifier +'\n\nSetting train speed to: '+ trainspeed);
})
.on('end', function() {
// done
message('END STREAMING BLOCKS');
});
//
// hub.subscribe("COLOR");
// hub.on("color", async (port, color) => {
// console.log(port,color);
// await hub.setLEDColor(color);
// });
});
poweredUP.scan(); // Start scanning for Hubs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.