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,