-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathrelay.sh
More file actions
51 lines (42 loc) · 1.74 KB
/
relay.sh
File metadata and controls
51 lines (42 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2021-2025 Intel Corporation. All rights reserved.
# needs usbrelay package: https://github.com/darrylb123/usbrelay
# param1: --debug | switch name
# param2: switch state
usbrelay_switch()
{
if [[ "$1" == "--debug" ]]; then
dlogi "Debug mode: Current status of all relays:"
if usbrelay --debug 2>&1 | grep -q "Found 0 devices"; then
dloge "No relays detected. Found 0 devices. Check hardware connection."
dloge "The usbrelay hw module is not responding or no relays detected. Check hardware connection."
# Skip the test if no relays are detected
exit 2
fi
return 0
fi
# Declare a constant for the relay settle time
local USBRELAY_SETTLE_TIME=0.5
local switch_name=$1
local state=$2
dlogi "Setting usbrelay switch $switch_name to $state."
usbrelay "$switch_name=$state" --quiet || {
die "Failed to set usbrelay switch $switch_name to $state.
The usbrelay hw module is not responding or no relays detected.
Check hardware connection."
}
# wait for the switch to settle
sleep "$USBRELAY_SETTLE_TIME"
# Display current state of the switch
current_state=$(usbrelay | awk -F= -v name="$switch_name" '$1 == name { print $2 }')
# Check if current_state is equal to the requested state
[[ "$current_state" == "$state" ]] || {
die "usbrelay switch $switch_name failed to set to $state (current: $current_state)"
}
case "$current_state" in
'1') dlogi "Current state of $switch_name is: on";;
'0') dlogi "Current state of $switch_name is: off";;
*) die "Invalid state for $switch_name: $current_state";;
esac
}