коррекция индикации микрофона

This commit is contained in:
Maxim
2026-07-06 15:18:26 +03:00
parent 8dcaee3d61
commit 243541fcd6
4 changed files with 54 additions and 6 deletions

View File

@@ -19,6 +19,7 @@ import (
"time"
"gpio-monitor/internal/adapter"
"gpio-monitor/internal/audio"
"gpio-monitor/internal/logger"
"gpio-monitor/internal/pipe"
)
@@ -28,8 +29,9 @@ import (
var webFiles embed.FS
type API struct {
buf *adapter.RingBuffer
startTime time.Time
buf *adapter.RingBuffer
startTime time.Time
audioMonitor *audio.Monitor
}
func writeJSON(w http.ResponseWriter, v any) {
@@ -409,6 +411,11 @@ func (a *API) HandleHealth(w http.ResponseWriter, r *http.Request) {
now := time.Now()
soundLevel := 0
if a.audioMonitor != nil {
soundLevel = a.audioMonitor.GetLevel()
}
writeJSON(w, map[string]any{
"status": status,
"uptime_sec": time.Since(a.startTime).Seconds(),
@@ -418,6 +425,7 @@ func (a *API) HandleHealth(w http.ResponseWriter, r *http.Request) {
"latest": latest,
"stats": a.buf.Stats(),
"server_time": now.Format("02.01.2006 15:04"),
"sound_level": soundLevel,
})
}
@@ -638,6 +646,13 @@ func main() {
// 4. RingBuffer с настройкой размера
buf := adapter.NewRingBuffer(*bufferSize)
// 4.5 Монитор уровня звука с микрофона
audioMonitor := audio.NewMonitor()
if err := audioMonitor.Start(); err != nil {
log.Printf("Аудио-монитор не запущен (микрофон недоступен?): %v", err)
}
defer audioMonitor.Stop()
// 5. PipeReader с настройками
pipeReader := pipe.NewPipeReader(buf, dataLogger, humanLogger, monitor, eventLogger, config)
ctx, cancel := context.WithCancel(context.Background())
@@ -646,8 +661,9 @@ func main() {
// ===== API И WEB СЕРВЕР =====
api := &API{
buf: buf,
startTime: time.Now(),
buf: buf,
startTime: time.Now(),
audioMonitor: audioMonitor,
}
// ===== API =====

View File

@@ -20,6 +20,7 @@ import {
setBars,
setSignal,
setAudioIndicator,
setSoundLevel,
DOM,
setChannelValue,
setChannelCurrentValue,
@@ -254,6 +255,8 @@ async function update(): Promise<void> {
setServerTime(health.server_time);
}
setSoundLevel(health.sound_level ?? 0);
const isOnline = health.pipe_alive && health.has_data;
if (isOnline) {
@@ -384,6 +387,7 @@ async function update(): Promise<void> {
setServerTime("--:--:--");
setBars(0, true);
setSignal(0, true);
setSoundLevel(0);
resetBufferTracking();
if (lastSignalLevel > 0) {

View File

@@ -8,6 +8,7 @@ interface HealthResponse {
has_data: boolean;
latest: number;
server_time?: string;
sound_level?: number;
stats: {
filled: number;
bytes_per_sec: number;