smartbridge/cmd/smartbridge/smartbridge.go
Felix Niederwanger ff33024f80
Add Import/Export value
Push Import/Export to the database and to mqtt.
2023-06-02 17:31:43 +02:00

55 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type Reading struct {
Status string `json:"status"`
Electricity Electricity `json:"elec"`
}
type Electricity struct {
Power Power `json:"power"`
Import Power `json:"import"`
Export Power `json:"export"`
}
type Power struct {
Current Value `json:"now"`
Minimun Value `json:"min"`
Maximum Value `json:"max"`
}
type Value struct {
Value int `json:"value"`
Unit string `json:"unit"`
Timestamp int64 `json:"time"`
}
func ReadSmartbridge(remote string) (Reading, error) {
var reading Reading
uri := fmt.Sprintf("http://%s/meter/now", remote)
resp, err := http.Get(uri)
if err != nil {
return reading, err
}
if resp.StatusCode != 200 {
return reading, fmt.Errorf("http status code %d returned", resp.StatusCode)
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
return reading, err
}
err = json.Unmarshal(buf, &reading)
// Invert power so that negative = usage, positive = production
reading.Electricity.Power.Current.Value = -reading.Electricity.Power.Current.Value
reading.Electricity.Power.Minimun.Value = -reading.Electricity.Power.Minimun.Value
reading.Electricity.Power.Maximum.Value = -reading.Electricity.Power.Maximum.Value
return reading, err
}