From 890ce83020fb729acc8ea4a36c8e70ff2b3580f2 Mon Sep 17 00:00:00 2001 From: tiger_zhou Date: Tue, 30 Jan 2024 13:20:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=97=E8=BD=A6=E5=8F=91=E9=80=81ws=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message_server/sfp_ms.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/message_server/sfp_ms.go b/message_server/sfp_ms.go index 0a01ae9..1559b26 100644 --- a/message_server/sfp_ms.go +++ b/message_server/sfp_ms.go @@ -2,7 +2,9 @@ package message_server import ( "fmt" + "log/slog" "reflect" + "runtime/debug" "strings" "time" @@ -409,18 +411,30 @@ func convertDynamicConfig(config *common_proto.TrainDynamicConfig, dest *state_p field := configType.Field(index) if field.IsExported() { fieldName := field.Name - destVal := reflect.ValueOf(dest).Elem().FieldByName(fieldName) - sourceData := reflect.ValueOf(config).Elem().FieldByName(fieldName).Interface() - - if destVal.Kind() == reflect.String { - destVal.Set(reflect.ValueOf(fmt.Sprintf("%v", sourceData))) - } else { - destVal.Set(reflect.ValueOf(sourceData)) - } + setVal(config, dest, fieldName) } } } +func setVal(source, dest interface{}, fieldName string) { + destVal := reflect.ValueOf(dest).Elem().FieldByName(fieldName) + sourceType := reflect.ValueOf(source).Elem().FieldByName(fieldName) + sourceData := sourceType.Interface() + defer func() { + err := recover() + if err != nil { + slog.Error(fieldName, "赋值失败,源数据类型", sourceType.Kind().String(), "目标数据类型:", destVal.Kind().String(), " 异常错误提示:", err) + debug.PrintStack() + } + }() + + if destVal.Kind() == reflect.String { + destVal.Set(reflect.ValueOf(fmt.Sprintf("%v", sourceData))) + } else { + destVal.Set(reflect.ValueOf(sourceData)) + } +} + func convertEnds(ends *common_proto.TrainEndsState) *state_proto.TrainEndsStateMqtt { return &state_proto.TrainEndsStateMqtt{SpeedSensorEnableA: ends.SpeedSensorEnableA, SpeedSensorEnableB: ends.SpeedSensorEnableB,