7272 debugLogger = ''
7373 # Set default debug level if not provided.
7474 : "'' ${DEBUG_LEVEL:=0}" # Use quoted expansion to satisfy shellcheck
75- : "'' ${DEBUG_LEVEL:=0}"
7675 LOG_FILE="/tmp/fan2go-debug-$(date +%Y%m%d%H).log"
7776 log_debug() {
7877 # Append message to the log file if debug level is 7 or higher.
7978 if [[ '' ${DEBUG_LEVEL} -ge 7 ]]; then echo "[$(date +%T)] DEBUG: $*" >> "$LOG_FILE"; fi
80- if [[ '' ${DEBUG_LEVEL} -ge 7 ]]; then echo "[$(date +%T)] [$$] DEBUG: $*" >> "$LOG_FILE"; fi
8179 }
8280 '' ;
8381
8482 # Create the bash scripts for fan control
8583 setPwmScript = pkgs . writeShellApplication {
8684 name = "setPwm.bash" ;
87- # A single, unified script to wrap all liquidctl interactions,
88- # preventing race conditions by design.
89- liquidctlWrapperScript = pkgs . writeShellApplication {
90- name = "liquidctl-wrapper.bash" ;
9185 runtimeInputs = [ pkgs . liquidctl pkgs . util-linux pkgs . coreutils ] ;
9286 text = ''
9387 # Convert fan2go PWM (0-255) to liquidctl percentage (0-100)
9488 # PWM value is passed as the first argument
9589 ${ debugLogger }
9690 log_debug "setPwm started with argument: $1"
97- ${ debugLogger }
98- ACTION="$1"
99- log_debug "Wrapper called with action: $ACTION"
10091
10192 # Check if the pwm_value argument was provided.
10293 : "'' ${1:?PWM value not provided as an argument}"
112103 done
113104 '' ;
114105 } ;
115- case "$ACTION" in
116- set-pwm )
117- PWM_VALUE = "$2"
118- : "''${ PWM_VALUE :?PWM value not provided for set-pwm action } "
119- percent = $( ( PWM_VALUE * 100 / 255 ) )
120- log_debug "Calculated percent: $percent"
121- for i in { 1. .3 } ; do
122- ( flock 200 ; liquidctl --vendor ${ liquidctlVendorId } set fan1 speed "$percent" 2 >> "$LOG_FILE" ) 200 >${liquidctlLockFile } && break
123- log_debug "Attempt #$i failed. Sleeping."
124- sleep ${ toString retrySleepDuration }
125- done
126- ; ;
127- get-pwm |get-rpm )
128- output = ""
129- for i in { 1. .3 } ; do
130- output = $( ( flock - s 200 ; liquidctl - - vendor ${ liquidctlVendorId } status 2 >> "$LOG_FILE" ) 200 >${liquidctlLockFile } )
131- [ -n "$output" ] && break
132- log_debug "Attempt #$i failed (no output). Sleeping."
133- sleep ${ toString retrySleepDuration }
134- done
135- log_debug "Raw liquidctl output: $output"
136106
137107 getPwmScript = pkgs . writeShellApplication {
138108 name = "getPwm.bash" ;
160130 exit 0
161131 fi
162132 echo 0
163- if [[ $output =~ Fan\ speed\ 1[^0-9]+([0-9]+) ]]; then
164- rpm='' ${BASH_REMATCH[1]}
165- if [[ "$ACTION" == "get-pwm" ]]; then
166- echo $((rpm * 255 / 2000))
167- else # get-rpm
168- echo "$rpm"
169- fi
170- else
171- echo 0
172- fi
173- ;;
174- *)
175- log_debug "Unknown action: $ACTION"
176- exit 1
177- ;;
178- esac
179133 '' ;
180134 } ;
181135
@@ -218,15 +172,12 @@ let
218172
219173 echo "Checking setPwm script..."
220174 shellcheck ${ setPwmScript } /bin/setPwm.bash || exit 1
221- shellcheck ${ liquidctlWrapperScript } /bin/liquidctl-wrapper.bash || exit 1
222175
223176 echo "Checking getPwm script..."
224177 shellcheck ${ getPwmScript } /bin/getPwm.bash || exit 1
225- shellcheck ${ liquidctlWrapperScript } /bin/liquidctl-wrapper.bash || exit 1
226178
227179 echo "Checking getRpm script..."
228180 shellcheck ${ getRpmScript } /bin/getRpm.bash || exit 1
229- shellcheck ${ liquidctlWrapperScript } /bin/liquidctl-wrapper.bash || exit 1
230181
231182 echo "All scripts passed shellcheck validation!"
232183 '' ;
@@ -251,24 +202,18 @@ let
251202 setPwm:
252203 exec: "${ setPwmScript } /bin/setPwm.bash"
253204 args: ["%pwm%"]
254- exec: "${ liquidctlWrapperScript } /bin/liquidctl-wrapper.bash"
255- args: ["set-pwm", "%pwm%"]
256205 env:
257206 DEBUG_LEVEL: "${ toString debugLevel } "
258207 # The `getPwm` command should return the current PWM value.
259208 # Since liquidctl doesn't provide PWM directly, we convert from the RPM value.
260209 getPwm:
261210 exec: "${ getPwmScript } /bin/getPwm.bash"
262- exec: "${ liquidctlWrapperScript } /bin/liquidctl-wrapper.bash"
263- args: ["get-pwm"]
264211 env:
265212 DEBUG_LEVEL: "${ toString debugLevel } "
266213 # The `getRpm` command gets the current RPM value from liquidctl.
267214 # This helps fan2go understand the fan's current state.
268215 getRpm:
269216 exec: "${ getRpmScript } /bin/getRpm.bash"
270- exec: "${ liquidctlWrapperScript } /bin/liquidctl-wrapper.bash"
271- args: ["get-rpm"]
272217 env:
273218 DEBUG_LEVEL: "${ toString debugLevel } "
274219 # Fan speed is a percentage for liquidctl
0 commit comments