feat(v2.0): Auto-discovery, correzione bug critici, e documentazione completa
Build Docker Image for Raspberry Pi / build-and-push (push) Failing after 4m42s

🆕 Funzionalità Auto-Discovery
- Aggiunto metodo AutoDiscoverBufferSizes() per rilevamento automatico QPIGS/QPIRI/QMOD/QPIWS
- Supporto variabili d'ambiente (INVERTER_DEVICE, MQTT_SERVER, etc.)
- Caching persistente buffer sizes in /cache/inverter.conf.cache
- Flag -a/--auto-discover per modalità auto-detection

🐛 Bug Fixes Critici
- **Parsing interi**: Aggiunta attemptAddSettingInt() con stoi() invece di stof()
  - Fix: stof('98') = 98.0f → 97 (int), ora stoi('98') = 98 direttamente
  - Applicato a: qpiri, qpiws, qmod, qpigs
- **Thread sync**: Aggiunto ups_qpiws_changed a main loop e condizione exit poll()
  - Fix: loop principale controllava solo 3 flag su 4, causava hang
  - Fix: thread poll() non usciva in runOnce perché mancava controllo QPIWS
- **Config accuracy**: Corretti buffer sizes (qpiri: 98→103, qpiws: 36→40)
  - Rimosso sources/inverter-cli/inverter.conf che sovrascriveva config globale
  - Validato con test: inverter_poller -1 completa in 6s con JSON completo

📚 Documentazione Completa
- Creato documentation/CODE_ARCHITECTURE.md (38KB)
  - Mappa logica variabili globali
  - Flusso esecuzione main() con diagrammi ASCII
  - Sequence diagram classe cInverter (poll, query, auto-discovery)
  - Thread synchronization diagrams
  - MQTT integration bash scripts flow
  - Mappa concettuale 5-layer system architecture
  - Error handling e performance optimizations
- Organizzati file .md in documentation/ (AUTO_DISCOVERY, IMPLEMENTATION, QUICKSTART, DEBUG)
- Aggiornato README.md con sezione v2.0 e indice documentazione
- Aggiornato .github/copilot-instructions.md con novità v2.0

🔧 Miglioramenti Build & CI/CD
- Gitea Actions per build multi-arch (arm/v6, arm/v7, arm64, amd64, 386)
- Configurazione VS Code completa (tasks, launch, debug GDB)
- Script test-autodiscovery.sh e test-device.sh

 Testing Validato
- inverter_poller -1 completa in 6 secondi
- Output JSON completo con tutte le metriche
- Exit pulito senza timeout (exit code 0)
- Tutte le 4 query QMOD/QPIGS/QPIRI/QPIWS funzionanti
This commit is contained in:
Pi Developer
2026-01-25 15:00:48 +01:00
parent ac2642639f
commit 6af9fcad7e
30 changed files with 4503 additions and 139 deletions
+161 -2
View File
@@ -1,15 +1,174 @@
#!/bin/bash
export TERM=xterm
# stty -F /dev/ttyUSB0 2400 raw
echo "=== Voltronic MQTT Bridge Starting ==="
echo "Version: 2.0 with Auto-Discovery"
echo ""
# Configuration paths
CONF_FILE="/etc/inverter/inverter.conf"
DISCOVERY_FLAG="/etc/inverter/.discovery_done"
TEMP_CONF="/tmp/inverter_discovered.conf"
# Environment variables with defaults
INVERTER_DEVICE="${INVERTER_DEVICE:-/dev/ttyUSB0}"
FORCE_DISCOVERY="${FORCE_DISCOVERY:-false}"
SKIP_DISCOVERY="${SKIP_DISCOVERY:-false}"
echo "Configuration:"
echo " Device: $INVERTER_DEVICE"
echo " Force Discovery: $FORCE_DISCOVERY"
echo " Skip Discovery: $SKIP_DISCOVERY"
echo ""
# Function to update config file with discovered values
update_config_with_discovery() {
local qmod=$1
local qpigs=$2
local qpiri=$3
local qpiws=$4
echo "Updating configuration with discovered values..."
# Backup original config
cp $CONF_FILE ${CONF_FILE}.backup
# Update device
sed -i "s|^device=.*|device=$INVERTER_DEVICE|g" $CONF_FILE
# Update buffer sizes
sed -i "s/^qmod=.*/qmod=$qmod/g" $CONF_FILE
sed -i "s/^qpigs=.*/qpigs=$qpigs/g" $CONF_FILE
sed -i "s/^qpiri=.*/qpiri=$qpiri/g" $CONF_FILE
sed -i "s/^qpiws=.*/qpiws=$qpiws/g" $CONF_FILE
echo "✓ Configuration updated successfully"
echo ""
grep -E "^(device|qmod|qpigs|qpiri|qpiws)=" $CONF_FILE
}
# Function to run auto-discovery
run_discovery() {
echo "=== Running Auto-Discovery ==="
echo "This will take about 10-15 seconds..."
echo ""
# Temporarily set device in config for discovery
cp $CONF_FILE $TEMP_CONF
sed -i "s|^device=.*|device=$INVERTER_DEVICE|g" $TEMP_CONF
cp $TEMP_CONF $CONF_FILE
# Run discovery and capture output
DISCOVERY_OUTPUT=$(/opt/inverter-cli/bin/inverter_poller -d -a 2>&1)
echo "$DISCOVERY_OUTPUT"
echo ""
# Parse discovery output
QMOD=$(echo "$DISCOVERY_OUTPUT" | grep "DISCOVERY_QMOD=" | cut -d= -f2)
QPIGS=$(echo "$DISCOVERY_OUTPUT" | grep "DISCOVERY_QPIGS=" | cut -d= -f2)
QPIRI=$(echo "$DISCOVERY_OUTPUT" | grep "DISCOVERY_QPIRI=" | cut -d= -f2)
QPIWS=$(echo "$DISCOVERY_OUTPUT" | grep "DISCOVERY_QPIWS=" | cut -d= -f2)
SUCCESS=$(echo "$DISCOVERY_OUTPUT" | grep "DISCOVERY_SUCCESS=" | cut -d= -f2)
if [ "$SUCCESS" = "true" ]; then
echo "✓ Auto-discovery completed successfully!"
update_config_with_discovery $QMOD $QPIGS $QPIRI $QPIWS
# Mark discovery as done
echo "device=$INVERTER_DEVICE" > $DISCOVERY_FLAG
echo "qmod=$QMOD" >> $DISCOVERY_FLAG
echo "qpigs=$QPIGS" >> $DISCOVERY_FLAG
echo "qpiri=$QPIRI" >> $DISCOVERY_FLAG
echo "qpiws=$QPIWS" >> $DISCOVERY_FLAG
echo "timestamp=$(date -Iseconds)" >> $DISCOVERY_FLAG
echo "✓ Discovery results saved to $DISCOVERY_FLAG"
return 0
else
echo "✗ Auto-discovery failed!"
echo "Please check:"
echo " 1. Inverter is powered on"
echo " 2. Cable is properly connected"
echo " 3. Device path is correct: $INVERTER_DEVICE"
echo ""
echo "Falling back to default configuration..."
# Update device but keep default buffer sizes
sed -i "s|^device=.*|device=$INVERTER_DEVICE|g" $CONF_FILE
return 1
fi
}
# Check if we need to run discovery
NEED_DISCOVERY=false
if [ "$FORCE_DISCOVERY" = "true" ]; then
echo "⚠ Force discovery requested via environment variable"
rm -f $DISCOVERY_FLAG
NEED_DISCOVERY=true
elif [ "$SKIP_DISCOVERY" = "true" ]; then
echo "⚠ Discovery skipped via environment variable"
# Just update device in config
sed -i "s|^device=.*|device=$INVERTER_DEVICE|g" $CONF_FILE
NEED_DISCOVERY=false
elif [ ! -f "$DISCOVERY_FLAG" ]; then
echo " No previous discovery found, will run auto-discovery"
NEED_DISCOVERY=true
else
# Check if device changed
SAVED_DEVICE=$(grep "^device=" $DISCOVERY_FLAG 2>/dev/null | cut -d= -f2)
if [ "$SAVED_DEVICE" != "$INVERTER_DEVICE" ]; then
echo "⚠ Device changed from $SAVED_DEVICE to $INVERTER_DEVICE"
echo " Running new discovery..."
rm -f $DISCOVERY_FLAG
NEED_DISCOVERY=true
else
echo "✓ Using previous discovery results from $DISCOVERY_FLAG"
# Restore saved config
while IFS= read -r line; do
if [[ $line =~ ^(device|qmod|qpigs|qpiri|qpiws)= ]]; then
key=$(echo "$line" | cut -d= -f1)
value=$(echo "$line" | cut -d= -f2)
sed -i "s/^$key=.*/$key=$value/g" $CONF_FILE
fi
done < "$DISCOVERY_FLAG"
echo "Current configuration:"
grep -E "^(device|qmod|qpigs|qpiri|qpiws)=" $CONF_FILE
fi
fi
# Run discovery if needed
if [ "$NEED_DISCOVERY" = "true" ]; then
if ! run_discovery; then
echo "⚠ Continuing with default configuration..."
echo " You can manually run discovery later with:"
echo " docker exec -it <container> /opt/inverter-cli/bin/inverter_poller -a"
fi
fi
echo ""
echo "=== Starting MQTT Bridge Services ==="
echo ""
# Wait a bit for the device to be ready
sleep 2
# Init the mqtt server for the first time, then every 5 minutes
# This will re-create the auto-created topics in the MQTT server if HA is restarted...
echo "Starting MQTT initialization service..."
watch -n 300 /opt/inverter-mqtt/mqtt-init.sh > /dev/null 2>&1 &
# Run the MQTT Subscriber process in the background (so that way we can change the configuration on the inverter from home assistant)
echo "Starting MQTT subscriber for commands..."
/opt/inverter-mqtt/mqtt-subscriber.sh &
# execute exactly every 30 seconds...
echo "Starting MQTT data push service (every 30 seconds)..."
echo ""
echo "✓ All services started successfully!"
echo " Logs will appear below..."
echo ""
watch -n 30 /opt/inverter-mqtt/mqtt-push.sh > /dev/null 2>&1