Skip to content

Instantly share code, notes, and snippets.

@dragouf
Last active October 29, 2017 17:29
Show Gist options
  • Save dragouf/45f236932e3bcef635f7023a41b0ab70 to your computer and use it in GitHub Desktop.
Save dragouf/45f236932e3bcef635f7023a41b0ab70 to your computer and use it in GitHub Desktop.
bash amazon dash handler
#!/bin/bash
MAC_ON="18:74:2e:26:95:d9"
MAC_PAMPERS="b4:7c:9c:bd:eb:f0"
function onClick {
#code to be ran on click
#node ./mqtt.js
#mosquitto_pub -h 192.168.1.12 -t cmnd/chevet2/power -m TOGGLE
found_mac=$2
if [[ $found_mac == *"$MAC_ON"* ]]; then
END1=$(date +%s);
diff=`echo $((END1-START1)) | awk 'int($1%60)'` # Get time since last click.
if [ $(($diff)) -gt 3 ] # wait more than 3 seconds after last click - tcdump has multiple outputs.
then
START1=$(date +%s)
mosquitto_pub -h 192.168.1.12 -t cmnd/chevet2/power -m TOGGLE
fi
elif [[ $found_mac == *"$MAC_PAMPERS"* ]]; then
END2=$(date +%s);
diff=`echo $((END2-START2)) | awk 'int($1%60)'` # Get time since last click.
if [ $(($diff)) -gt 3 ] # wait more than 3 seconds after last click - tcdump has multiple outputs.
then
START2=$(date +%s)
mosquitto_pub -h 192.168.1.12 -t cmnd/salon/power -m TOGGLE
fi
fi
}
START1=$(date +%s);
START2=$(date +%s);
tcpdump -l -i eth0 ether host "$MAC_PAMPERS" or ether host "$MAC_ON" | while read b; do
onClick $b & # run in background
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment