chore: change logging verbosity

This commit is contained in:
Siyuan Miao 2025-04-12 22:54:45 +02:00
parent edba5c94f5
commit 338d8fd55e
7 changed files with 114 additions and 22 deletions

View File

@ -139,19 +139,33 @@ var (
) )
) )
type CloudConnectionState uint8
const (
CloudConnectionStateNotConfigured CloudConnectionState = iota
CloudConnectionStateDisconnected
CloudConnectionStateConnecting
CloudConnectionStateConnected
)
var ( var (
cloudConnectionAlive bool cloudConnectionState CloudConnectionState = CloudConnectionStateNotConfigured
cloudConnectionAliveLock = &sync.Mutex{} cloudConnectionStateLock = &sync.Mutex{}
cloudDisconnectChan chan error cloudDisconnectChan chan error
cloudDisconnectLock = &sync.Mutex{} cloudDisconnectLock = &sync.Mutex{}
) )
func setCloudConnectionAlive(alive bool) { func setCloudConnectionState(state CloudConnectionState) {
cloudConnectionAliveLock.Lock() cloudConnectionStateLock.Lock()
defer cloudConnectionAliveLock.Unlock() defer cloudConnectionStateLock.Unlock()
cloudConnectionAlive = alive if cloudConnectionState == CloudConnectionStateDisconnected &&
(config.CloudToken == "" || config.CloudURL == "") {
state = CloudConnectionStateNotConfigured
}
cloudConnectionState = state
go waitCtrlAndRequestDisplayUpdate() go waitCtrlAndRequestDisplayUpdate()
} }
@ -297,6 +311,8 @@ func runWebsocketClient() error {
wsURL.Scheme = "wss" wsURL.Scheme = "wss"
} }
setCloudConnectionState(CloudConnectionStateConnecting)
header := http.Header{} header := http.Header{}
header.Set("X-Device-ID", GetDeviceID()) header.Set("X-Device-ID", GetDeviceID())
header.Set("X-App-Version", builtAppVersion) header.Set("X-App-Version", builtAppVersion)
@ -314,12 +330,12 @@ func runWebsocketClient() error {
c, resp, err := websocket.Dial(dialCtx, wsURL.String(), &websocket.DialOptions{ c, resp, err := websocket.Dial(dialCtx, wsURL.String(), &websocket.DialOptions{
HTTPHeader: header, HTTPHeader: header,
OnPingReceived: func(ctx context.Context, payload []byte) bool { OnPingReceived: func(ctx context.Context, payload []byte) bool {
scopedLogger.Info().Bytes("payload", payload).Int("length", len(payload)).Msg("ping frame received") scopedLogger.Debug().Bytes("payload", payload).Int("length", len(payload)).Msg("ping frame received")
metricConnectionTotalPingReceivedCount.WithLabelValues("cloud", wsURL.Host).Inc() metricConnectionTotalPingReceivedCount.WithLabelValues("cloud", wsURL.Host).Inc()
metricConnectionLastPingReceivedTimestamp.WithLabelValues("cloud", wsURL.Host).SetToCurrentTime() metricConnectionLastPingReceivedTimestamp.WithLabelValues("cloud", wsURL.Host).SetToCurrentTime()
setCloudConnectionAlive(true) setCloudConnectionState(CloudConnectionStateConnected)
return true return true
}, },
@ -350,7 +366,7 @@ func runWebsocketClient() error {
if err != nil { if err != nil {
if errors.Is(err, context.Canceled) { if errors.Is(err, context.Canceled) {
cloudLogger.Info().Msg("websocket connection canceled") cloudLogger.Info().Msg("websocket connection canceled")
setCloudConnectionAlive(false) setCloudConnectionState(CloudConnectionStateDisconnected)
return nil return nil
} }
@ -540,6 +556,8 @@ func rpcDeregisterDevice() error {
cloudLogger.Info().Msg("device deregistered, disconnecting from cloud") cloudLogger.Info().Msg("device deregistered, disconnecting from cloud")
disconnectCloud(fmt.Errorf("device deregistered")) disconnectCloud(fmt.Errorf("device deregistered"))
setCloudConnectionState(CloudConnectionStateNotConfigured)
return nil return nil
} }

View File

@ -91,7 +91,7 @@ cd "${REMOTE_PATH}"
chmod +x jetkvm_app_debug chmod +x jetkvm_app_debug
# Run the application in the background # Run the application in the background
PION_LOG_TRACE=jetkvm,cloud,websocket ./jetkvm_app_debug PION_LOG_TRACE=jetkvm,cloud,websocket,native ./jetkvm_app_debug
EOF EOF
echo "Deployment complete." echo "Deployment complete."

View File

@ -1,6 +1,7 @@
package kvm package kvm
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -53,6 +54,18 @@ func lvObjShow(objName string) (*CtrlResponse, error) {
return lvObjClearFlag(objName, "LV_OBJ_FLAG_HIDDEN") return lvObjClearFlag(objName, "LV_OBJ_FLAG_HIDDEN")
} }
func lvObjSetOpacity(objName string, opacity int) (*CtrlResponse, error) {
return CallCtrlAction("lv_obj_set_style_opa_layered", map[string]interface{}{"obj": objName, "opa": opacity})
}
func lvObjFadeIn(objName string, duration uint32) (*CtrlResponse, error) {
return CallCtrlAction("lv_obj_fade_in", map[string]interface{}{"obj": objName, "time": duration})
}
func lvObjFadeOut(objName string, duration uint32) (*CtrlResponse, error) {
return CallCtrlAction("lv_obj_fade_out", map[string]interface{}{"obj": objName, "time": duration})
}
func lvLabelSetText(objName string, text string) (*CtrlResponse, error) { func lvLabelSetText(objName string, text string) (*CtrlResponse, error) {
return CallCtrlAction("lv_label_set_text", map[string]interface{}{"obj": objName, "text": text}) return CallCtrlAction("lv_label_set_text", map[string]interface{}{"obj": objName, "text": text})
} }
@ -69,13 +82,20 @@ func updateLabelIfChanged(objName string, newText string) {
} }
func switchToScreenIfDifferent(screenName string) { func switchToScreenIfDifferent(screenName string) {
displayLogger.Info().Str("from", currentScreen).Str("to", screenName).Msg("switching screen")
if currentScreen != screenName { if currentScreen != screenName {
displayLogger.Info().Str("from", currentScreen).Str("to", screenName).Msg("switching screen")
switchToScreen(screenName) switchToScreen(screenName)
} }
} }
var (
cloudBlinkCtx context.Context
cloudBlinkCancel context.CancelFunc
cloudBlinkTicker *time.Ticker
)
func updateDisplay() { func updateDisplay() {
updateLabelIfChanged("ui_Home_Content_Ip", networkState.IPv4String()) updateLabelIfChanged("ui_Home_Content_Ip", networkState.IPv4String())
if usbState == "configured" { if usbState == "configured" {
updateLabelIfChanged("ui_Home_Footer_Usb_Status_Label", "Connected") updateLabelIfChanged("ui_Home_Footer_Usb_Status_Label", "Connected")
@ -99,16 +119,67 @@ func updateDisplay() {
switchToScreenIfDifferent("ui_No_Network_Screen") switchToScreenIfDifferent("ui_No_Network_Screen")
} }
if config.CloudToken == "" || config.CloudURL == "" { if cloudConnectionState == CloudConnectionStateNotConfigured {
lvObjHide("ui_Home_Header_Cloud_Status_Icon") lvObjHide("ui_Home_Header_Cloud_Status_Icon")
} else { } else {
lvObjShow("ui_Home_Header_Cloud_Status_Icon") lvObjShow("ui_Home_Header_Cloud_Status_Icon")
// TODO: blink the icon if establishing connection
if cloudConnectionAlive {
_, _ = lvImgSetSrc("ui_Home_Header_Cloud_Status_Icon", "cloud.png")
} else {
_, _ = lvImgSetSrc("ui_Home_Header_Cloud_Status_Icon", "cloud_disconnected.png")
} }
switch cloudConnectionState {
case CloudConnectionStateDisconnected:
lvImgSetSrc("ui_Home_Header_Cloud_Status_Icon", "cloud_disconnected.png")
stopCloudBlink()
case CloudConnectionStateConnecting:
lvImgSetSrc("ui_Home_Header_Cloud_Status_Icon", "cloud.png")
startCloudBlink()
case CloudConnectionStateConnected:
lvImgSetSrc("ui_Home_Header_Cloud_Status_Icon", "cloud.png")
stopCloudBlink()
}
}
func startCloudBlink() {
if cloudBlinkTicker == nil {
cloudBlinkTicker = time.NewTicker(2 * time.Second)
}
if cloudBlinkCtx != nil {
cloudBlinkCancel()
}
cloudBlinkCtx, cloudBlinkCancel = context.WithCancel(appCtx)
cloudBlinkTicker.Reset(2 * time.Second)
go func() {
defer cloudBlinkTicker.Stop()
for {
select {
case <-cloudBlinkTicker.C:
if cloudConnectionState != CloudConnectionStateConnecting {
return
}
_, _ = lvObjFadeIn("ui_Home_Header_Cloud_Status_Icon", 1000)
time.Sleep(1000 * time.Millisecond)
_, _ = lvObjFadeOut("ui_Home_Header_Cloud_Status_Icon", 1000)
time.Sleep(1000 * time.Millisecond)
case <-cloudBlinkCtx.Done():
time.Sleep(1000 * time.Millisecond)
_, _ = lvObjFadeIn("ui_Home_Header_Cloud_Status_Icon", 1000)
time.Sleep(1000 * time.Millisecond)
_, _ = lvObjSetOpacity("ui_Home_Header_Cloud_Status_Icon", 255)
return
}
}
}()
}
func stopCloudBlink() {
if cloudBlinkTicker != nil {
cloudBlinkTicker.Stop()
}
if cloudBlinkCtx != nil {
cloudBlinkCancel()
} }
} }
@ -128,7 +199,7 @@ func requestDisplayUpdate() {
} }
go func() { go func() {
wakeDisplay(false) wakeDisplay(false)
displayLogger.Info().Msg("display updating") displayLogger.Debug().Msg("display updating")
//TODO: only run once regardless how many pending updates //TODO: only run once regardless how many pending updates
updateDisplay() updateDisplay()
}() }()

View File

@ -335,7 +335,10 @@ func ensureBinaryUpdated(destPath string) error {
_, err = os.Stat(destPath) _, err = os.Stat(destPath)
if shouldOverwrite(destPath, srcHash) || err != nil { if shouldOverwrite(destPath, srcHash) || err != nil {
nativeLogger.Info().Msg("writing jetkvm_native") nativeLogger.Info().
Interface("hash", srcHash).
Msg("writing jetkvm_native")
_ = os.Remove(destPath) _ = os.Remove(destPath)
destFile, err := os.OpenFile(destPath, os.O_CREATE|os.O_RDWR, 0755) destFile, err := os.OpenFile(destPath, os.O_CREATE|os.O_RDWR, 0755)
if err != nil { if err != nil {

Binary file not shown.

View File

@ -1 +1 @@
b556ac0d6f38518f20dcf212ba65fe97981aa169ae418c68b2cbb155447affda 4b925c7aa73d2e35a227833e806658cb17e1d25900611f93ed70b11ac9f1716d

4
web.go
View File

@ -203,7 +203,7 @@ func handleLocalWebRTCSignal(c *gin.Context) {
wsOptions := &websocket.AcceptOptions{ wsOptions := &websocket.AcceptOptions{
InsecureSkipVerify: true, // Allow connections from any origin InsecureSkipVerify: true, // Allow connections from any origin
OnPingReceived: func(ctx context.Context, payload []byte) bool { OnPingReceived: func(ctx context.Context, payload []byte) bool {
scopedLogger.Info().Bytes("payload", payload).Msg("ping frame received") scopedLogger.Debug().Bytes("payload", payload).Msg("ping frame received")
metricConnectionTotalPingReceivedCount.WithLabelValues("local", source).Inc() metricConnectionTotalPingReceivedCount.WithLabelValues("local", source).Inc()
metricConnectionLastPingReceivedTimestamp.WithLabelValues("local", source).SetToCurrentTime() metricConnectionLastPingReceivedTimestamp.WithLabelValues("local", source).SetToCurrentTime()
@ -244,7 +244,7 @@ func handleWebRTCSignalWsMessages(
runCtx, cancelRun := context.WithCancel(context.Background()) runCtx, cancelRun := context.WithCancel(context.Background())
defer func() { defer func() {
if isCloudConnection { if isCloudConnection {
setCloudConnectionAlive(false) setCloudConnectionState(CloudConnectionStateDisconnected)
} }
cancelRun() cancelRun()
}() }()