Skip to content

Commit

Permalink
Add quantity and unit fields to json output. (#20)
Browse files Browse the repository at this point in the history
* Add quantity and unit fields to json output.
* Add quantity and unit fields to json output.
* Forgot to change one reference convert() -> format()
  • Loading branch information
grecky-goo authored Jun 13, 2023
1 parent c93891c commit b749228
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions output/jsonoutput.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package output

import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"

jsoniter "github.com/json-iterator/go"
)
Expand All @@ -35,8 +38,68 @@ func NewJSONOutput() Output {
return o
}

type newstruct struct {
Key string `json:"key"`
Type string `json:"type"`
Value any `json:"value"`
Quantity any `json:"quantity"`
Unit string `json:"unit"`
}

func format(d any) (any, error) {
v := d.(map[string]any)
var err error
for key, entry := range v {
t := make([]byte, 0)
buf := new(newstruct)
s := make([]string, 0)
t, err = json.Marshal(entry)
if err != nil {
fmt.Println("json.Marshal error:", err)
break
}
err = json.Unmarshal(t, buf)
if err != nil {
fmt.Println("json.Unmarshal error:", err)
break
}
// Process only string values
switch buf.Value.(type) {
case string:
if !strings.Contains(buf.Value.(string), " ") {
continue
}
s = strings.Split(buf.Value.(string), " ")
switch buf.Type {
case "flt", "sp78", "sp87":
f, err := strconv.ParseFloat(s[0], 64)
if err != nil {
break
}
buf.Quantity = f
buf.Unit = s[1]
}
}
v[key] = buf
}
return v, err

}

func (jo JSONOutput) All() {
jo.print(GetAll())
var err error
data := make(map[string]any)
data = GetAll()
for key, d := range data {
if data[key], err = format(d); err != nil {
fmt.Printf("Convert error:%v\n", err)
jo.print(GetAll())
return
}
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
out, _ := json.MarshalIndent(data, "", " ")
fmt.Fprint(jo.writer, string(out))
}

func (jo JSONOutput) Battery() {
Expand All @@ -63,8 +126,13 @@ func (jo JSONOutput) Voltage() {
jo.print(GetVoltage())
}

func (jo JSONOutput) print(v interface{}) {
func (jo JSONOutput) print(v any) {
data, err := format(v)
if err != nil {
fmt.Println("Convert error:", v)
return
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
out, _ := json.MarshalIndent(v, "", " ")
out, _ := json.MarshalIndent(data, "", " ")
fmt.Fprint(jo.writer, string(out))
}

0 comments on commit b749228

Please sign in to comment.