fix dhw values not stored in db

This commit is contained in:
Jonas Leder 2025-01-26 19:19:45 +01:00
commit ab943be63c
3 changed files with 25 additions and 2 deletions

21
helper/flattenJson.go Normal file
View file

@ -0,0 +1,21 @@
package helper
func FlattenJSON(prefix string, input map[string]interface{}, output map[string]interface{}) {
for key, value := range input {
// Build the new key
newKey := key
if prefix != "" {
newKey = prefix + "_" + key
}
// Check the type of value
switch v := value.(type) {
case map[string]interface{}:
// If it's a nested JSON object, recursively flatten it
FlattenJSON(newKey, v, output)
default:
// Otherwise, add it to the output map
output[newKey] = v
}
}
}

View file

@ -18,7 +18,7 @@ func main() {
log.Init()
queue.Init()
if os.Getenv("OUTPUT_DATABSE") != "" && os.Getenv("CLIENT_USE_SERVER") != "true" {
if (os.Getenv("OUTPUT_DATABSE") != "" && os.Getenv("CLIENT_USE_SERVER") != "true") || os.Getenv("LOGGER_SERVER") == "true" {
database.Init()
if os.Getenv("LOGGER_SERVER") != "true" {
database.CreateInstance()

View file

@ -98,6 +98,8 @@ func runDbClient() {
var jsonData map[string]interface{}
err = json.Unmarshal([]byte(task.Data), &jsonData)
flattenedData := make(map[string]interface{})
helper.FlattenJSON("", jsonData, flattenedData)
if err != nil {
log.Log.Error("Failed decoding boiler JSON: ", err.Error())
queue.MainQueue.Enqueue(taskData, 60)
@ -105,7 +107,7 @@ func runDbClient() {
continue
}
valuesToInsert := []tables.Value{}
for key, value := range jsonData {
for key, value := range flattenedData {
valueType := database.GetOrCreateValueType(key)
dbValue := tables.Value{
Date: task.Date,