Skip to content

Instantly share code, notes, and snippets.

@TBD
Created February 15, 2022 20:47
Show Gist options
  • Save TBD/0c563bf655795892bccf8c219725647e to your computer and use it in GitHub Desktop.
Save TBD/0c563bf655795892bccf8c219725647e to your computer and use it in GitHub Desktop.
mini home automation
// --- bus setup
var miniBus = function (n) {
var o;
o = function (i) {
var t;
t = i.toLowerCase();
return n[t] || (n[t] = []);
};
n = n || {};
return {
on: function (t, n) {
return o(t).push(n);
},
off: function (t, n) {
var e, i;
e = o(t);
i = e.indexOf(n);
return ~i && e.splice(i, 1);
},
emit: function (t, n) {
return o('*').concat(o(t)).forEach(function (t) {
return t(n);
});
}
};
}();
// --- class setup
class Device {
constructor(name, id) { this.name = name; this.id = id; }
get status() { console.log(`*** ${this.id} ${this.name}`) }
}
class Brain extends Device {
constructor(name, id) {
super(name, id);
this.temp = 0
miniBus.on('3', (temp) => { this.temp = temp })
}
get status() { console.log(`*** ${this.id} ${this.name} - [temp: ${this.temp}]`) }
}
class Sensor extends Device {
constructor(name, id) {
super(name, id);
setInterval(_ => {
miniBus.emit(this.id.toString(), Math.random() * 40 | 0)
}, 1000)
}
}
class Light extends Device {
constructor(name, id) {
super(name, id);
this.light = "off"
miniBus.on(this.id.toString(), data => {
this.light == "off" ? this.light = "on" : this.light = "off"
})
}
get status() { console.log(`*** ${this.id} ${this.name} - ${this.light}`) }
}
// --- device setup
let devices_config = [
{ id: 0, type: Brain, name: "Brain" },
{ id: 1, type: Light, name: 'Living Room' },
{ id: 2, type: Light, name: 'Bedroom' },
{ id: 3, type: Sensor, name: 'Kitchen temp' },
]
let devices = []
devices_config.forEach(device => {
devices.push(new device.type(device.name, device.id))
})
function _debug() { devices.forEach(device => { device.status }) }
// --- repl
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.setPrompt('s | l | q > ');
rl.prompt();
rl.on('line', function (line) {
switch (line.trim()) {
case 's':
_debug();
break;
case 'l':
miniBus.emit("1");
break
case 'q':
rl.close();
break;
default:
console.log('invalid command');
break;
}
rl.prompt();
}).on('close', function () {
console.log('exit');
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment