Point POTACAT and Graywolf at rigctld in radio configure
POTACAT gets a "rigctld Network" rig at 127.0.0.1:4532 in My Rigs, made the active rig, with the connection written where POTACAT reads it: the global settings, or the operator's profile once there is one. It's left alone if it has a radio set up, unless --force. Graywolf's web API needs its password, so its database gets the change: PTT through rigctld on each channel that transmits and has none. It's checked first without writing; only if something changes is Graywolf stopped with sudo, its database backed up through SQLite, and Graywolf started again. If sudo is refused, or no channel transmits, it says what to set in the web UI instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
892bfb9df0
commit
04d973039a
@@ -33,6 +33,8 @@ FLDIGI_CONF="$HOME/.fldigi/fldigi_def.xml"
|
||||
NOT1MM_CONF="$CONFIG_HOME/not1mm/not1mm.json"
|
||||
QLOG_FLATPAK=io.github.foldynl.QLog
|
||||
QLOG_DB="$HOME/.var/app/$QLOG_FLATPAK/data/hamradio/QLog/qlog.db"
|
||||
POTACAT_DIR="$CONFIG_HOME/potacat"
|
||||
GRAYWOLF_DB="$HOME/.local/share/graywolf/graywolf.db"
|
||||
DIREWOLF_CONF="$HAMPACK_STATE/conf.d/direwolf.conf"
|
||||
# What HamPack's conf.d/direwolf.conf ships with, which configure replaces
|
||||
DIREWOLF_DEFAULT_ADEVICE="plughw:0,0"
|
||||
@@ -54,8 +56,8 @@ Usage: radio [detect] [--probe]
|
||||
probe PORT Ask the radio on serial port PORT what it is
|
||||
configure Set up the radio (if there's more than one, give its number
|
||||
from \`radio\`): run rigctld for it as a systemd user service,
|
||||
and point WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm
|
||||
and QLog at rigctld. Only changes
|
||||
and point WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm,
|
||||
QLog, POTACAT and Graywolf at rigctld. Only changes
|
||||
settings you haven't set, and backs up each file it changes.
|
||||
Quit those programs first
|
||||
--force Replace rig settings you've set yourself
|
||||
@@ -826,6 +828,168 @@ configure_qlog() {
|
||||
esac
|
||||
}
|
||||
|
||||
# POTACAT: add rigctld to My Rigs as a "rigctld Network" rig, and make it the
|
||||
# active one, unless rigs are set up already. Rigs and the active rig are kept
|
||||
# in settings.json; the connection (catTarget) is too, until there's an
|
||||
# operator profile, which then keeps it in profiles/CALL/settings.json.
|
||||
# configure_potacat NAME
|
||||
configure_potacat() {
|
||||
local global="$POTACAT_DIR/settings.json" profile cat_file state tmp target
|
||||
target="$(jq -nc --arg host "${RIGCTLD_ADDR%:*}" --argjson port "${RIGCTLD_ADDR#*:}" '{type: "rigctldnet", host: $host, port: $port}')"
|
||||
if [ ! -x "$HOME/.local/bin/POTACAT.AppImage" ]; then
|
||||
report POTACAT "not installed"
|
||||
return
|
||||
fi
|
||||
if [ ! -f "$global" ]; then
|
||||
report POTACAT "skipped: it has no settings yet. Run POTACAT once, then \`radio configure\` again"
|
||||
return
|
||||
fi
|
||||
if running potacat; then
|
||||
report POTACAT "skipped, because it's running and would overwrite its settings when it quits. Quit it and run \`radio configure\` again"
|
||||
return
|
||||
fi
|
||||
profile="$(jq -r 'if .activeProfile and (has("catTarget") | not) then .activeProfile else "" end' "$global")" ||
|
||||
{ report POTACAT "couldn't read $global"; return 1; }
|
||||
cat_file="$global"
|
||||
[ -z "$profile" ] || cat_file="$POTACAT_DIR/profiles/$profile/settings.json"
|
||||
|
||||
local current active rigs
|
||||
current="$(jq -c '.catTarget // null' "$cat_file" 2> /dev/null || echo null)"
|
||||
if ! { active="$(jq -r '.activeRigId // ""' "$global")" && rigs="$(jq '.rigs // [] | length' "$global")"; }; then
|
||||
report POTACAT "couldn't read $global"
|
||||
return 1
|
||||
fi
|
||||
if [ "$active" = rig_hampack ] && jq -en --argjson a "$current" --argjson b "$target" '$a == $b' > /dev/null; then
|
||||
state="already"
|
||||
elif (( rigs > 0 )) || [ "$current" != null ]; then
|
||||
state="theirs"
|
||||
else
|
||||
state="empty"
|
||||
fi
|
||||
case "$state" in
|
||||
already) report POTACAT "already set up"; return ;;
|
||||
theirs)
|
||||
if (( ! FORCE )); then
|
||||
report POTACAT "left alone: it has a radio set up already. \`radio configure --force\` adds rigctld to My Rigs and makes it active"
|
||||
return
|
||||
fi ;;
|
||||
esac
|
||||
|
||||
back_up "$global"
|
||||
tmp="$(mktemp "$global.XXXXXX")" || return 1
|
||||
if ! jq --argjson t "$target" --arg name "$1 (HamPack's rigctld)" --arg cat "$cat_file" --arg global "$global" '
|
||||
.rigs = ([(.rigs // [])[] | select(.id != "rig_hampack")] + [{id: "rig_hampack", name: $name, catTarget: $t}])
|
||||
| .activeRigId = "rig_hampack"
|
||||
| if $cat == $global then .catTarget = $t else . end
|
||||
' "$global" > "$tmp" || ! chmod --reference="$global" "$tmp" || ! mv -f "$tmp" "$global"; then
|
||||
rm -f "$tmp"
|
||||
report POTACAT "couldn't change $global"
|
||||
return 1
|
||||
fi
|
||||
if [ "$cat_file" != "$global" ]; then
|
||||
back_up "$cat_file"
|
||||
mkdir -p "$(dirname "$cat_file")" && { [ -f "$cat_file" ] || echo '{}' > "$cat_file"; } || return 1
|
||||
tmp="$(mktemp "$cat_file.XXXXXX")" || return 1
|
||||
if ! jq --argjson t "$target" '.catTarget = $t' "$cat_file" > "$tmp" || ! chmod --reference="$cat_file" "$tmp" || ! mv -f "$tmp" "$cat_file"; then
|
||||
rm -f "$tmp"
|
||||
report POTACAT "couldn't change $cat_file"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
report POTACAT "rig \"$1 (HamPack's rigctld)\" is rigctld, and active"
|
||||
}
|
||||
|
||||
# Graywolf keeps its settings in graywolf.db, with PTT set per channel. Set
|
||||
# PTT to rigctld on each channel that can transmit and has none (or, with
|
||||
# --force, any other). graywolf_ptt check|BACKUP prints the number of
|
||||
# channels to change (or changed), then the number left alone, or "none"
|
||||
# with no channel that transmits, or the error. With BACKUP, it makes the
|
||||
# changes, backing up the database through SQLite first, which includes
|
||||
# what's still in graywolf.db-wal.
|
||||
graywolf_ptt() {
|
||||
python3 -I -c '
|
||||
import datetime, sqlite3, sys
|
||||
db, backup, addr, force = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] == "1"
|
||||
try:
|
||||
con = sqlite3.connect(db)
|
||||
channels = [row[0] for row in con.execute("SELECT id FROM channels WHERE output_device_id != 0 ORDER BY id")]
|
||||
if not channels:
|
||||
print("none")
|
||||
sys.exit(0)
|
||||
ptt = {row[0]: (row[1], row[2]) for row in con.execute("SELECT channel_id, method, device FROM ptt_configs")}
|
||||
change, kept = [], 0
|
||||
for channel in channels:
|
||||
method, device = ptt.get(channel, ("none", ""))
|
||||
if (method, device) == ("rigctld", addr):
|
||||
continue
|
||||
if method in ("none", "") or force:
|
||||
change.append(channel)
|
||||
else:
|
||||
kept += 1
|
||||
if change and backup != "check":
|
||||
copy = sqlite3.connect(backup)
|
||||
con.backup(copy)
|
||||
copy.close()
|
||||
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f+00:00")
|
||||
with con:
|
||||
for channel in change:
|
||||
if channel in ptt:
|
||||
con.execute("UPDATE ptt_configs SET method = ?, device = ?, updated_at = ? WHERE channel_id = ?",
|
||||
("rigctld", addr, now, channel))
|
||||
else:
|
||||
con.execute("INSERT INTO ptt_configs (channel_id, method, device, gpio_pin, ptt_method, gpio_line, "
|
||||
"invert, slot_time_ms, persist, dwait_ms, created_at, updated_at) "
|
||||
"VALUES (?, ?, ?, 0, 0, 0, 0, 10, 63, 0, ?, ?)", (channel, "rigctld", addr, now, now))
|
||||
print(len(change), kept)
|
||||
except (sqlite3.Error, OSError) as e:
|
||||
print(e)
|
||||
' "$GRAYWOLF_DB" "$1" "$RIGCTLD_ADDR" "$FORCE"
|
||||
}
|
||||
|
||||
configure_graywolf() {
|
||||
local result changed kept backup was_active=0 web="http://localhost:8080"
|
||||
local manual="in its web UI ($web), set each channel's PTT to rigctld, $RIGCTLD_ADDR"
|
||||
if [ ! -x "$HOME/.local/bin/graywolf" ]; then
|
||||
report Graywolf "not installed"
|
||||
return
|
||||
fi
|
||||
if [ ! -f "$GRAYWOLF_DB" ]; then
|
||||
report Graywolf "skipped: it has no settings yet. Open its web UI ($web) once, then \`radio configure\` again"
|
||||
return
|
||||
fi
|
||||
result="$(graywolf_ptt check)"
|
||||
read -r changed kept <<< "$result"
|
||||
if [[ "$changed" =~ ^[1-9][0-9]*$ ]]; then
|
||||
# It runs as a system service, and reads its settings when it starts
|
||||
if systemctl is-active --quiet graywolf 2> /dev/null; then
|
||||
if ! sudo systemctl stop graywolf; then
|
||||
report Graywolf "skipped: couldn't stop it to change its settings. Instead, $manual"
|
||||
return
|
||||
fi
|
||||
was_active=1
|
||||
fi
|
||||
backup="$GRAYWOLF_DB.bak-$(date +%Y%m%d-%H%M%S)"
|
||||
result="$(graywolf_ptt "$backup")"
|
||||
(( ! was_active )) || sudo systemctl start graywolf
|
||||
read -r changed kept <<< "$result"
|
||||
fi
|
||||
if [ "$changed" = none ]; then
|
||||
report Graywolf "skipped: it has no channel that transmits yet. Add one in its web UI ($web) with PTT rigctld, $RIGCTLD_ADDR"
|
||||
elif ! [[ "$changed" =~ ^[0-9]+$ ]]; then
|
||||
report Graywolf "couldn't change $GRAYWOLF_DB: $result"
|
||||
return 1
|
||||
elif (( changed == 0 && kept == 0 )); then
|
||||
report Graywolf "already set up"
|
||||
else
|
||||
(( changed == 0 )) || BACKUPS+=("$backup")
|
||||
local message=""
|
||||
(( changed == 0 )) || message="PTT through rigctld on $changed channel$( ((changed == 1)) || echo s)"
|
||||
(( kept == 0 )) ||
|
||||
message="${message:+$message. }Left $kept channel$( ((kept == 1)) || echo s) with PTT of its own alone: \`radio configure --force\` replaces it"
|
||||
report Graywolf "$message"
|
||||
fi
|
||||
}
|
||||
|
||||
# Write and (re)start rigctld's user service: write_rigctld NAME HAMLIB PORT BAUD
|
||||
write_rigctld() {
|
||||
local unit
|
||||
@@ -946,6 +1110,8 @@ configure() {
|
||||
configure_direwolf "$card"
|
||||
configure_not1mm
|
||||
configure_qlog
|
||||
configure_potacat "$name"
|
||||
configure_graywolf
|
||||
|
||||
mkdir -p "$HAMPACK_STATE" &&
|
||||
printf 'name=%s\nhamlib=%s\nport=%s\nbaud=%s\ncard=%s\n' "$name" "$model" "$port" "$baud" "$card" > "$HAMPACK_STATE/radio"
|
||||
|
||||
Reference in New Issue
Block a user