Created
January 27, 2019 03:25
-
-
Save manekinekko/a726ede6d255e6ec657774a8eb6c253a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 http = require('http'); | |
const heartbeat = 'echo heartbeat > /sys/class/leds/omega2p\:amber\:system/trigger'; | |
const o = require('omega2-gpio'); | |
const g = new o(); | |
const run = (pin, done) => { | |
g.tests().then( () => { | |
const p = g.pin({pin: pin, debug: true, mode: 'output'}); | |
p.set(1); | |
setTimeout( () => { | |
p.set(0); | |
done(); | |
}, 800); | |
}); | |
}; | |
const send = (rs, text, status) => { | |
rs.writeHead(status, { 'Content-Type': 'application/json' }); | |
rs.end(text, 'utf-8'); | |
}; | |
http.createServer( (rq, rs) => { | |
console.log('request: ', rq.url); | |
if (rq.url === '/mode/1') { | |
run(1, () => send(rs, '{"mode": 1, "status": "OK"}', 200)); | |
} | |
else if (rq.url === '/mode/2') { | |
run(2, () => send(rs, '{"mode": 2, "status": "OK"}', 200)); | |
} | |
else if (rq.url === '/mode/3') { | |
run(3, () => send(rs, '{"mode": 3, "status": "OK"}', 200)); | |
} | |
else { | |
send(rs, '404', 404); | |
} | |
}).listen(1337, '0.0.0.0', (err) => { | |
if (err) { | |
console.error('Unable to start the server', err); | |
return false; | |
} | |
console.log('Server running at http://0.0.0.0:1337'); | |
const exec = require('child_process').execSync(heartbeat); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment