From 0c23ccce1e720e9383d4dd70a4f439c1cad24ab6 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 15 Dec 2023 18:17:46 +0100 Subject: [PATCH] Finished keyboard color change implementation and solved bugs of shell script --- roles/driver-msi-keyboard-color/README.md | 5 +- .../files/keyboard_color.py | 10 +- .../files/keyboard_color.sh | 139 ------------------ .../tasks/setup_script.yml | 4 +- .../templates/keyboard-color.service.j2 | 2 +- .../tests/keyboard_color_simulator.bash | 29 ++++ 6 files changed, 41 insertions(+), 148 deletions(-) delete mode 100644 roles/driver-msi-keyboard-color/files/keyboard_color.sh create mode 100644 roles/driver-msi-keyboard-color/tests/keyboard_color_simulator.bash diff --git a/roles/driver-msi-keyboard-color/README.md b/roles/driver-msi-keyboard-color/README.md index 670bdaa3..9e4a5cd2 100644 --- a/roles/driver-msi-keyboard-color/README.md +++ b/roles/driver-msi-keyboard-color/README.md @@ -38,5 +38,8 @@ This role was created by [Kevin Veen-Birkenbach](https://github.com/kevinveenbir ## Chat Conversation -To see how this role was developed, you can refer to the [Chat Conversation](https://chat.openai.com/share/41c47fdb-a92d-466d-9e92-5a894fe6bec3) that produced this software. +To see how this role was developed, you can refer to the following ChatGPT Conversation that produced this software: +- https://chat.openai.com/share/1f1dde28-3fb2-4df7-be89-4f7ffe67e5c7 +- https://chat.openai.com/share/41c47fdb-a92d-466d-9e92-5a894fe6bec3 +- https://chat.openai.com/share/e45d186a-f2e1-4c96-9390-36269e274193 diff --git a/roles/driver-msi-keyboard-color/files/keyboard_color.py b/roles/driver-msi-keyboard-color/files/keyboard_color.py index d38d4e4d..482f7e39 100644 --- a/roles/driver-msi-keyboard-color/files/keyboard_color.py +++ b/roles/driver-msi-keyboard-color/files/keyboard_color.py @@ -69,6 +69,10 @@ def calculate_transition_ratio(current_time, start_time, end_time): start_timestamp = datetime.datetime.combine(today, start_time).timestamp() end_timestamp = datetime.datetime.combine(today, end_time).timestamp() + # If start and end times are the same, return 0 to indicate no transition + if start_timestamp == end_timestamp: + return 0 + # Calculate the current timestamp current_timestamp = datetime.datetime.combine(today, current_time).timestamp() @@ -107,11 +111,7 @@ def main(vendor_and_product_id): "05:00": ("5bc0eb", "ff7f00"), # Morning Blue to Dawn "04:00": ("990000", "5bc0eb"), # Night to Blue } - - # Ensure 24-hour coverage - if "00:00" not in color_times: - raise ValueError("Time periods must cover the full 24-hour cycle.") - + current_time = datetime.datetime.now().time() start_color, end_color = get_current_period(current_time, color_times) diff --git a/roles/driver-msi-keyboard-color/files/keyboard_color.sh b/roles/driver-msi-keyboard-color/files/keyboard_color.sh deleted file mode 100644 index d24a4b0f..00000000 --- a/roles/driver-msi-keyboard-color/files/keyboard_color.sh +++ /dev/null @@ -1,139 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -# Check if the vendor and product ID is provided -if [ -z "${1-}" ]; then - echo "Error: Vendor and product ID is missing." - echo "Usage: /opt/keyboard_color.sh " - exit 1 -fi - -# Extract the vendor and product ID from the command-line argument -vendor_and_product_id=$1 - -# Define the color transition parameters -morning_blue_color="5bc0eb" # Morning Blue Color -dawn_color="ff7f00" # Dawn Color -morning_golden_hour_color="f9c80e" # Morning Golden Hour Color -sunrise_color="ffd700" # Sunrise Color -noon_color="ffffff" # Noon Color -twilight_color="ff6f61" # Twilight Color -evening_golden_hour_color="fdbe51" # Evening Golden Hour Color -evening_blue_color="0000ff" # Evening Blue Color -sunset_color="ff4500" # Sunset Color -night_color="990000" # Night Color - -# Function to calculate the color based on the transition ratio -calculate_color() { - local color_start=$1 - local color_end=$2 - local transition_ratio=$3 - - local start_r=$((16#${color_start:0:2})) - local start_g=$((16#${color_start:2:2})) - local start_b=$((16#${color_start:4:2})) - - local end_r=$((16#${color_end:0:2})) - local end_g=$((16#${color_end:2:2})) - local end_b=$((16#${color_end:4:2})) - - local current_r=$(awk "BEGIN { r = ${start_r} + (${end_r} - ${start_r}) * ${transition_ratio}; printf(\"%.0f\", r) }") - local current_g=$(awk "BEGIN { g = ${start_g} + (${end_g} - ${start_g}) * ${transition_ratio}; printf(\"%.0f\", g) }") - local current_b=$(awk "BEGIN { b = ${start_b} + (${end_b} - ${start_b}) * ${transition_ratio}; printf(\"%.0f\", b) }") - - local calculated_color=$(printf "%02x%02x%02x" "$current_r" "$current_g" "$current_b") - - if [ "$calculated_color" = "ffff100" ]; then - calculated_color="ffffff" - fi - - echo "$calculated_color" -} - -# Get the current time in HH:MM format -current_time=$(date +%H:%M) - -# Calculate the transition ratio based on the current time -if [[ "$current_time" > "22:00" || "$current_time" < "04:00" ]]; then - # Night (22:00 to 04:00) - color_start="$night_color" - color_end="$night_color" - color_start_time="22:00" - color_end_time="04:00" -elif [[ "$current_time" > "21:00" ]]; then - # Evening Blue Hour to Night (21:00 to 22:00) - color_start="$evening_blue_color" - color_end="$night_color" - color_start_time="21:00" - color_end_time="22:00" -elif [[ "$current_time" > "20:00" ]]; then - # Evening Golden Hour to Evening Blue Hour (20:00 to 21:00) - color_start="$evening_golden_hour_color" - color_end="$evening_blue_color" - color_start_time="20:00" - color_end_time="21:00" -elif [[ "$current_time" > "19:30" ]]; then - # Sunset to Evening Golden Hour (19:30 to 20:00) - color_start="$sunset_color" - color_end="$evening_golden_hour_color" - color_start_time="19:30" - color_end_time="20:00" -elif [[ "$current_time" > "12:00" ]]; then - # Noon to Sunset (12:00 to 19:30) - color_start="$noon_color" - color_end="$sunset_color" - color_start_time="12:00" - color_end_time="19:30" -elif [[ "$current_time" > "07:30" ]]; then - # Sunrise to Noon (07:30 to 12:00) - color_start="$sunrise_color" - color_end="$noon_color" - color_start_time="07:30" - color_end_time="12:00" -elif [[ "$current_time" > "07:00" ]]; then - # Morning Golden Hour to Sunrise (07:00 to 07:30) - color_start="$morning_golden_hour_color" - color_end="$sunrise_color" - color_start_time="07:00" - color_end_time="07:30" -elif [[ "$current_time" > "06:00" ]]; then - # Dawn to Morning Golden Hour (06:00 to 07:00) - color_start="$dawn_color" - color_end="$morning_golden_hour_color" - color_start_time="06:00" - color_end_time="07:00" -elif [[ "$current_time" > "05:00" ]]; then - # Morning Blue Hour to Dawn (05:00 to 06:00) - color_start="$morning_blue_color" - color_end="$dawn_color" - color_start_time="05:00" - color_end_time="06:00" -elif [[ "$current_time" > "04:00" ]]; then - # Night to Morning Blue Hour (22:00 to 04:00) - color_start="$night_color" - color_end="$morning_blue_color" - color_start_time="04:00" - color_end_time="05:00" -fi - -# Get the current date in YYYY-MM-DD format -current_date=$(date +%Y-%m-%d) - -# Calculate the start and end timestamps based on the current date and time -start_timestamp=$(date -d "${current_date} ${color_start_time}" +%s) -end_timestamp=$(date -d "${current_date} ${color_end_time}" +%s) - -# Get the current timestamp -current_timestamp=$(date +%s) - -# Calculate the transition ratio -transition_ratio=$(awk "BEGIN { ratio = ($current_timestamp - $start_timestamp) / ($end_timestamp - $start_timestamp); print ratio }") - -# Calculate the current color based on the transition ratio -current_color=$(calculate_color "$color_start" "$color_end" "$transition_ratio") - -echo "Changing keyboard color to #$current_color." - -# Set the color using msi-perkeyrgb -msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id" \ No newline at end of file diff --git a/roles/driver-msi-keyboard-color/tasks/setup_script.yml b/roles/driver-msi-keyboard-color/tasks/setup_script.yml index 6129f668..a0888f6d 100644 --- a/roles/driver-msi-keyboard-color/tasks/setup_script.yml +++ b/roles/driver-msi-keyboard-color/tasks/setup_script.yml @@ -1,8 +1,8 @@ --- - name: Copy keyboard_color.sh script copy: - src: keyboard_color.sh - dest: /opt/keyboard_color.sh + src: keyboard_color.py + dest: /opt/keyboard_color.py mode: 0755 tags: - keyboard-color diff --git a/roles/driver-msi-keyboard-color/templates/keyboard-color.service.j2 b/roles/driver-msi-keyboard-color/templates/keyboard-color.service.j2 index c8390410..e603a143 100644 --- a/roles/driver-msi-keyboard-color/templates/keyboard-color.service.j2 +++ b/roles/driver-msi-keyboard-color/templates/keyboard-color.service.j2 @@ -4,4 +4,4 @@ OnFailure=systemd-notifier@%n.service [Service] Type=oneshot -ExecStart=/opt/keyboard_color.sh {{ vendor_and_product_id }} \ No newline at end of file +ExecStart=/bin/python /opt/keyboard_color.py {{ vendor_and_product_id }} \ No newline at end of file diff --git a/roles/driver-msi-keyboard-color/tests/keyboard_color_simulator.bash b/roles/driver-msi-keyboard-color/tests/keyboard_color_simulator.bash new file mode 100644 index 00000000..7b80d578 --- /dev/null +++ b/roles/driver-msi-keyboard-color/tests/keyboard_color_simulator.bash @@ -0,0 +1,29 @@ +#!/bin/bash + +# Pfad zum Keyboard-Color-Skript +KEYBOARD_COLOR_SCRIPT="$1" +DEVICE_ID="$2" + +# NTP deaktivieren +timedatectl set-ntp false + +# Startzeit speichern +START_TIME=$(date +%s) + +# 2 Minuten (120 Sekunden) für den gesamten Tag simulieren +for i in {0..60}; do + # Berechnung der aktuellen simulierten Zeit + CURRENT_TIME=$((START_TIME + (( 86400 / 60 ) * i ))) + + # Systemzeit auf die simulierte Zeit setzen + date +%s -s "@$CURRENT_TIME" + + # Keyboard-Color-Skript ausführen + python $KEYBOARD_COLOR_SCRIPT $DEVICE_ID + + # 2 Sekunden Pause + sleep 2 +done + +# NTP wieder aktivieren +timedatectl set-ntp true \ No newline at end of file