From 83f5dbf38fc064ced8309fd3ebd63a3def118bd3 Mon Sep 17 00:00:00 2001 From: sberk42 Date: Mon, 14 Feb 2022 16:07:29 +0100 Subject: [PATCH] fix for values containing not only numbers --- fritzbox_lua/lua_client.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/fritzbox_lua/lua_client.go b/fritzbox_lua/lua_client.go index 29c25ae..070ea14 100644 --- a/fritzbox_lua/lua_client.go +++ b/fritzbox_lua/lua_client.go @@ -76,6 +76,12 @@ type LabelRename struct { Name string } +// regex to remove leading/training characters from numbers +var ( + regexNonNumberBegin = regexp.MustCompile("^\\D+") + regexNonNumberEnd = regexp.MustCompile("\\D+$") +) + func (lua *LuaSession) doLogin(response string) error { urlParams := "" if response != "" { @@ -310,8 +316,11 @@ VALUE: floatVal = 0 } } else { - // convert value to float - floatVal, err = strconv.ParseFloat(sVal, 64) + // convert value to float, but first remove all non numbers from begin or end of value + // needed if value contains unit + sNum := regexNonNumberEnd.ReplaceAllString(regexNonNumberBegin.ReplaceAllString(sVal, ""), "") + + floatVal, err = strconv.ParseFloat(sNum, 64) if err != nil { continue VALUE }