Skip to content

Instantly share code, notes, and snippets.

@wallentx
Last active August 2, 2024 18:04
Show Gist options
  • Save wallentx/07ca8ac1888e087268d52220bb303bfd to your computer and use it in GitHub Desktop.
Save wallentx/07ca8ac1888e087268d52220bb303bfd to your computer and use it in GitHub Desktop.
No! We got kafka at home!
#!/usr/bin/env bash
# Mom, can we get kafka, pleeeasee??
# No! We got
# kafka-at-home.sh
process() {
msg="$1"
[[ "$msg" == "quit" ]] && exit 1
echo -e "Received message:"
echo "$msg" | jq -c .
cat=$(echo "$msg" | jq -r .cat)
w=$(echo "$msg" | jq -r .weight)
t=$(echo "$msg" | jq -r .timestamp)
dm=$(echo "$msg" | jq -r .device_model)
dn=$(echo "$msg" | jq -r .device_name)
ll=$(echo "$msg" | jq -r .litter_level)
pf=$(echo "$msg" | jq -r '."%full"')
ss=$(echo "$msg" | jq -r .scoops_saved)
echo -e "\nFormatted Output:"
echo -e " ๐Ÿˆ Cat: \e[34m$cat\e[0m"
echo -e " โš–๏ธ Weight: \e[34m$w lbs\e[0m"
echo -e " ๐Ÿ•“ Timestamp: \e[34m$t\e[0m"
echo -e " ๐Ÿ†” Device Model: \e[34m$dm\e[0m"
echo -e " ๐Ÿ“› Device Name: \e[34m$dn\e[0m"
echo -e " ๐Ÿ“ถ Litter Level: \e[34m$ll\e[0m"
echo -e " ๐Ÿšฎ Percent Full: \e[34m$pf%\e[0m"
echo -e " ๐Ÿค‘ Scoops Saved: \e[34m$ss\e[0m"
(( pf > 90 )) && echo -e "โš ๏ธ \e[31mAlert ๐Ÿšจ: The litter box is more than 90% full!\e[0m๐Ÿ™€"
textfade
}
export -f process
p=/tmp/mypipe
[[ -p $p ]] || mkfifo $p
nc -klu 192.168.x.x 6666 >$p &
echo "Listening..."
while IFS= read -r line; do
process "$line"
done < $p
rm $p
#!/usr/bin/env bash
ip=192.168.x.x
port=6666
send_payload() {
local payload=$1
echo -e "echo \"$payload\" | nc -q 1 -u \$ip \$port" | bat -Ppl bash
echo "$payload" | nc -q 1 -u $ip $port
}
declare -a cats=("Nano" "Boo" "Spaghetti")
declare -a weights=(10.6 9.4 12.3)
litter_level="Normal"
ll_value=80
percent_full=40
scoops_saved=498
timestamp="2024-08-01T12:34:56Z"
while [ $percent_full -le 100 ]; do
cat_index=$((RANDOM % 3))
cat=${cats[$cat_index]}
weight=${weights[$cat_index]}
timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
payload=$(jq -c -n --arg cat "$cat" --argjson weight "$weight" --arg timestamp "$timestamp" --arg model "Litter Robot 4 Connect" --arg name "Senator Turd Poos" --arg level "$litter_level" --argjson full "$percent_full" --argjson scoops "$scoops_saved" '{
cat: $cat,
weight: $weight,
timestamp: $timestamp,
device_model: $model,
device_name: $name,
litter_level: $level,
"%full": $full,
scoops_saved: $scoops
}')
send_payload "$payload"
sleep 1
# Progressively update litter level and percent full
scoops_saved=$((scoops_saved + 1))
percent_full=$((percent_full + 10))
ll_value=$((ll_value -5))
if [ $ll_value -le 30 ]; then
litter_level="Low"
elif [ $ll_value -le 60 ]; then
litter_level="Medium"
else
litter_level="Normal"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment