Last active
November 11, 2024 04:04
-
-
Save viscount-monty/16f17d8e6299108516d60f7b05c22fb4 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
#!/bin/bash | |
# ANSI escape codes | |
# Black 0;30 Dark Gray 1;30 | |
# Red 0;31 Light Red 1;31 | |
# Green 0;32 Light Green 1;32 | |
# Brown/Orange 0;33 Yellow 1;33 | |
# Blue 0;34 Light Blue 1;34 | |
# Purple 0;35 Light Purple 1;35 | |
# Cyan 0;36 Light Cyan 1;36 | |
# Light Gray 0;37 White 1;37 | |
# Define colour variables | |
BLACK='\033[0;30m' | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
ORANGE='\033[0;33m' | |
BLUE='\033[0;34m' | |
PURPLE='\033[0;35m' | |
CYAN='\033[0;36m' | |
LIGHT_GRAY='\033[0;37m' | |
DARK_GRAY='\033[1;30m' | |
LIGHT_RED='\033[1;31m' | |
LIGHT_GREEN='\033[1;32m' | |
YELLOW='\033[1;33m' | |
LIGHT_BLUE='\033[1;34m' | |
LIGHT_PURPLE='\033[1;35m' | |
LIGHT_CYAN='\033[1;36m' | |
WHITE='\033[1;37m' | |
NC='\033[0m' | |
# Create array of variables to iterate over | |
declare -A colours | |
colours[black]=${BLACK} | |
colours[red]=${RED} | |
colours[green]=${GREEN} | |
colours[orange]=${ORANGE} | |
colours[blue]=${BLUE} | |
colours[purple]=${PURPLE} | |
colours[cyan]=${CYAN} | |
colours[light_gray]=${LIGHT_GRAY} | |
colours[dark_gray]=${DARK_GRAY} | |
colours[light_red]=${LIGHT_RED} | |
colours[light_green]=${LIGHT_GREEN} | |
colours[yellow]=${YELLOW} | |
colours[light_blue]=${LIGHT_BLUE} | |
colours[light_purple]=${LIGHT_PURPLE} | |
colours[light_cyan]=${LIGHT_CYAN} | |
colours[white]=${WHITE} | |
colours[nc]=${NC} | |
# Demonstrate all colours | |
# for colour_key in "${!colours[@]}" | |
# do | |
# printf "${NC}$colour_key text: ${colours[$colour_key]} example text\n" | |
# done | |
# This script monitors the USB connectivity of the corne v4.1 keyboard | |
# Functionality stages | |
# 1. Print to terminal every second with timestamps, message when keyboard not detected | |
# 1. Log to file `corne_disconnects.log` | |
# 3. Send `ntfy` notification upon disconnect | |
# Define log file | |
LOGFILE="./corne_disconnects.log" | |
# If keyboard isn't connected, print message and wait until connected | |
TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S') | |
MSG="$TIMESTAMP - Script started" | |
printf "${NC}$MSG\n" | |
printf "$MSG\n" >> $LOGFILE | |
# Variable to only log disconnect once | |
dc_logged=false | |
while true | |
do | |
lsusb_output=$(lsusb -d 4653:0004) | |
TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S') | |
if ! [ -z "$lsusb_output" ]; then | |
# If keyboard detected, print lsusb output for device to consol with timestamp | |
dc_logged=false | |
MSG="$TIMESTAMP - $lsusb_output" | |
printf "\r" | |
printf "${GREEN}$MSG${NC}" | |
# "2024-11-09_13-30-07 - corne keyboard disconnected " | |
# "2024-11-09_13-27-27 - Bus 001 Device 038: ID 4653:0004 foostan Corne v4" | |
else | |
MSG="$TIMESTAMP - corne keyboard disconnected " | |
printf "\r" | |
printf "${LIGHT_RED}$MSG${NC}" | |
# 2024-11-09_13-15-58 - Bus 001 Device 037: ID 4653:0004 foostan Corne v4 | |
if ! $dc_logged ; then | |
# Print newline to leave disconnection log in terminal history | |
printf "\n" | |
# Log disconnect here | |
printf "$MSG\n" >> $LOGFILE | |
# Send notification, suppress output to terminal | |
# curl -u <usr>:<pwd> -d "$MSG" <ntfy server>corne > /dev/null 2>&1 | |
dc_logged=true | |
fi | |
fi | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment