Skip to content

Instantly share code, notes, and snippets.

@jlord
Last active October 26, 2017 17:16
Show Gist options
  • Save jlord/c435fc4ab68b2d8d403ba7230d353dca to your computer and use it in GitHub Desktop.
Save jlord/c435fc4ab68b2d8d403ba7230d353dca to your computer and use it in GitHub Desktop.
var exec = require('child_process').exec
var args = process.argv
// Config
var IP = ''
var SEC = ''
// Lights
var FIRST_LIGHT = '65537'
var SECOND_LIGHT = '65538'
var GROUP = '164919'
// Puts
var baseCommand = `coap-client -m put -u "Client_identity" -k`
var commandOn = `${baseCommand} "${SEC}" -e '{ "3311": [{ "5850": 1 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}"`
var commandOff = `${baseCommand} "${SEC}" -e '{ "3311": [{ "5850": 0 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}"`
var commandDim = `${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 127 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}"`
var commandDimBig = `${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 127 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 30 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 127 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 30 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 127 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 30 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
sleep 3s &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 127 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}"`
var commandFlicker = `${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 90 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 90 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 90 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 90 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
sleep 1s &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
sleep 2s &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 200 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}" &&
${baseCommand} "${SEC}" -e '{ "3311": [{ "5851": 20 }] }' "coaps://${IP}:5684/15001/${FIRST_LIGHT}"`
// Gets
var commandGetLight = `coap-client -m get -u "Client_identity" -k "${SEC}" "coaps://${IP}:5684/15001/${FIRST_LIGHT}"`
// Use --get to get a sample payload back on the first light
if (args[2] === '--get') {
return exec(commandGetLight, function gotLight (err, stdout) {
if (err) return console.log(err)
console.log(stdout)
})
}
if (args[2] === '--flick') {
return exec(commandFlicker, function flickLight (err) {
if (err) return console.log(err)
console.log("FLICKER!")
})
}
// Use --dim to dim
if (args[2] === '--dim') {
return exec(commandDim, function dimLight (err) {
if (err) return console.log(err)
console.log("DIM!")
})
}
// Use --big to dim big
if (args[2] === '--big') {
return exec(commandDimBig, function dimLight (err) {
if (err) return console.log(err)
console.log("WOW BIG DIM!")
})
}
// Pass nothing in to just turn the light on and off
exec(commandOn, function on (err) {
if (err) return console.log(err)
console.log("ON!")
exec(commandOff, function off (err) {
if (err) return console.log(err)
console.log("OFF!")
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment