Created
October 16, 2020 18:33
-
-
Save Sarverott/ae07c867245264315301dddb53740213 to your computer and use it in GitHub Desktop.
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
const express=require('express'); | |
const johnnyFive=require("johnny-five"); | |
const app=express(); | |
const port=3000; | |
const board=new johnnyFive.Board({port:"COM8"}); | |
class AnubisLightsControll{ | |
constructor(name,pin){ | |
this.pin=pin; | |
this.name=name; | |
this.active=true; | |
AnubisLightsControll.addLight(this); | |
} | |
switchState(mode){ | |
switch(mode){ | |
case "on": | |
this.pin.on(); | |
return this.pin.isOn; | |
break; | |
case "off": | |
this.pin.off(); | |
return !this.pin.isOn; | |
break; | |
default: | |
return false; | |
} | |
} | |
static test(id=0){ | |
AnubisLightsControll.failsafeLights(); | |
console.log(id); | |
//console.log(AnubisLightsControll.lights.length); | |
if(id<AnubisLightsControll.lights.length){ | |
AnubisLightsControll.lights[id].switchState("on"); | |
setTimeout( | |
function(){ | |
AnubisLightsControll.lights[id].switchState("off"); | |
AnubisLightsControll.test(++id); | |
}, | |
500 | |
); | |
}else{ | |
return true; | |
} | |
} | |
static failsafeLights(){ | |
} | |
static deactivateLight(name){ | |
AnubisLightsControll.failsafeLights(); | |
var result=false; | |
for(var i in AnubisLightsControll.lights){ | |
if(name==AnubisLightsControll.lights[i].name){ | |
AnubisLightsControll.lights[i].active=false; | |
result=true; | |
} | |
} | |
return result; | |
} | |
static activateLight(name){ | |
AnubisLightsControll.failsafeLights(); | |
var result=false; | |
for(var i in AnubisLightsControll.lights){ | |
if(name==AnubisLightsControll.lights[i].name){ | |
AnubisLightsControll.lights[i].active=true; | |
result=true; | |
} | |
} | |
return result; | |
} | |
static addLight(object){ | |
AnubisLightsControll.failsafeLights(); | |
AnubisLightsControll.lights.push(object); | |
} | |
static getLight(name){ | |
AnubisLightsControll.failsafeLights(); | |
var result=null; | |
for(var i in AnubisLightsControll.lights){ | |
//console.log(AnubisLightsControll.lights[i]); | |
if(name==AnubisLightsControll.lights[i].name){ | |
result=AnubisLightsControll.lights[i]; | |
} | |
} | |
return result; | |
} | |
static switchLight(mode, name){ | |
var light=AnubisLightsControll.getLight(name); | |
if(light===null&&light.active){ | |
return false; | |
}else{ | |
return light.switchState(mode); | |
} | |
} | |
}; | |
AnubisLightsControll.lights=[]; | |
board.on("ready", function() { | |
new AnubisLightsControll("gerald-room", new johnnyFive.Led(10)); | |
new AnubisLightsControll("hall", new johnnyFive.Led(9)); | |
new AnubisLightsControll("pepe-bed", new johnnyFive.Led(8)); | |
new AnubisLightsControll("pepe-workshop", new johnnyFive.Led(7)); | |
new AnubisLightsControll("pepe-table", new johnnyFive.Led(6)); | |
this.repl.inject({ | |
geraldRoom: AnubisLightsControll.getLight("gerald-room").pin, | |
hall: AnubisLightsControll.getLight("hall").pin, | |
pepeBed: AnubisLightsControll.getLight("pepe-bed").pin, | |
pepeWorkshop: AnubisLightsControll.getLight("pepe-workshop").pin, | |
pepeRoom: AnubisLightsControll.getLight("pepe-table").pin, | |
}); | |
//AnubisLightsControll.test(); | |
}); | |
app.get('/lights/:lightname', function(req, res){ | |
var effect=AnubisLightsControll.getLight(req.params.lightname); | |
if(effect!=null){ | |
res.json({ | |
result:"success", | |
name:effect.name, | |
state:effect.pin.isOn, | |
pin:effect.pin.pin, | |
active:effect.active, | |
time:Date.now() | |
}); | |
}else{ | |
res.status(500).json({ | |
result:"error", | |
params:req.params, | |
time:Date.now() | |
}); | |
} | |
res.end(); | |
}); | |
app.get('/lights/:lightname/:state', function(req, res){ | |
var effect=AnubisLightsControll.switchLight( | |
req.params.state, | |
req.params.lightname | |
); | |
console.log(effect); | |
if(effect){ | |
res.json({ | |
result:"success", | |
time:Date.now() | |
}); | |
}else{ | |
res.status(500).json({ | |
result:"error", | |
params:req.params, | |
time:Date.now() | |
}); | |
} | |
res.end(); | |
}); | |
app.listen(port, function(){console.log(`Example app listening on port ${port}!`)}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment