New MySubaru version
This commit is contained in:
@ -9,14 +9,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
ms "git.savin.nyc/alex/mysubaru/config"
|
msc "git.savin.nyc/alex/mysubaru/config"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config .
|
// Config .
|
||||||
type Config struct {
|
type Config struct {
|
||||||
MQTT MQTT `json:"mqtt" yaml:"mqtt"`
|
MQTT MQTT `json:"mqtt" yaml:"mqtt"`
|
||||||
MySubaru ms.MySubaru `json:"mysubaru" yaml:"mysubaru"`
|
MySubaru msc.MySubaru `json:"mysubaru" yaml:"mysubaru"`
|
||||||
Hassio Hassio `json:"hassio" yaml:"hassio"`
|
Hassio Hassio `json:"hassio" yaml:"hassio"`
|
||||||
Listeners map[string]*Listener `json:"listeners" yaml:"listeners"`
|
Listeners map[string]*Listener `json:"listeners" yaml:"listeners"`
|
||||||
Consul *Consul `json:"consul,omitempty" yaml:"consul,omitempty"`
|
Consul *Consul `json:"consul,omitempty" yaml:"consul,omitempty"`
|
||||||
|
5
go.mod
5
go.mod
@ -5,7 +5,7 @@ go 1.24
|
|||||||
replace github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.5.3
|
replace github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.5.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.savin.nyc/alex/mysubaru v0.0.0-20250522040944-19134aa756e1
|
git.savin.nyc/alex/mysubaru v0.0.0-20250604165950-9783b483c9cd
|
||||||
github.com/eclipse/paho.mqtt.golang v1.5.0
|
github.com/eclipse/paho.mqtt.golang v1.5.0
|
||||||
github.com/hashicorp/consul/api v1.32.1
|
github.com/hashicorp/consul/api v1.32.1
|
||||||
github.com/spf13/viper v1.20.1
|
github.com/spf13/viper v1.20.1
|
||||||
@ -13,7 +13,6 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Jeffail/gabs/v2 v2.7.0 // indirect
|
|
||||||
github.com/armon/go-metrics v0.5.4 // indirect
|
github.com/armon/go-metrics v0.5.4 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||||
github.com/fatih/color v1.18.0 // indirect
|
github.com/fatih/color v1.18.0 // indirect
|
||||||
@ -38,7 +37,7 @@ require (
|
|||||||
github.com/sagikazarmark/locafero v0.9.0 // indirect
|
github.com/sagikazarmark/locafero v0.9.0 // indirect
|
||||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||||
github.com/spf13/afero v1.14.0 // indirect
|
github.com/spf13/afero v1.14.0 // indirect
|
||||||
github.com/spf13/cast v1.8.0 // indirect
|
github.com/spf13/cast v1.9.2 // indirect
|
||||||
github.com/spf13/pflag v1.0.6 // indirect
|
github.com/spf13/pflag v1.0.6 // indirect
|
||||||
github.com/subosito/gotenv v1.6.0 // indirect
|
github.com/subosito/gotenv v1.6.0 // indirect
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
|
@ -17,6 +17,7 @@ import (
|
|||||||
"git.savin.nyc/alex/mysubaru"
|
"git.savin.nyc/alex/mysubaru"
|
||||||
"git.savin.nyc/alex/mysubaru-mq/bus"
|
"git.savin.nyc/alex/mysubaru-mq/bus"
|
||||||
"git.savin.nyc/alex/mysubaru-mq/config"
|
"git.savin.nyc/alex/mysubaru-mq/config"
|
||||||
|
msc "git.savin.nyc/alex/mysubaru/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MySubaruClient is a client that connects to the MySubaru server by establishing tcp connection
|
// MySubaruClient is a client that connects to the MySubaru server by establishing tcp connection
|
||||||
@ -33,10 +34,6 @@ type MySubaruClient struct {
|
|||||||
|
|
||||||
// NewMySubaruClient initialises and returns a MySubaru client
|
// NewMySubaruClient initialises and returns a MySubaru client
|
||||||
func NewMySubaruClient(id string, bus *bus.Bus, config *config.Config) *MySubaruClient {
|
func NewMySubaruClient(id string, bus *bus.Bus, config *config.Config) *MySubaruClient {
|
||||||
if config == nil {
|
|
||||||
slog.Error("")
|
|
||||||
}
|
|
||||||
|
|
||||||
s := &MySubaruClient{
|
s := &MySubaruClient{
|
||||||
id: id,
|
id: id,
|
||||||
bus: bus,
|
bus: bus,
|
||||||
@ -60,7 +57,7 @@ func (s *MySubaruClient) Type() string {
|
|||||||
func (s *MySubaruClient) Init(log *slog.Logger) error {
|
func (s *MySubaruClient) Init(log *slog.Logger) error {
|
||||||
s.log = log
|
s.log = log
|
||||||
|
|
||||||
mys, err := mysubaru.New(log, &s.config.MySubaru)
|
mys, err := mysubaru.New(&msc.Config{MySubaru: s.config.MySubaru, TimeZone: s.config.Timezone, Logger: s.log})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Error("couldn't connect to MySubaru server", "error", err.Error())
|
s.log.Error("couldn't connect to MySubaru server", "error", err.Error())
|
||||||
}
|
}
|
||||||
@ -285,7 +282,7 @@ func (s *MySubaruClient) mySubaruConfigToMQTTHassioConfig(v *mysubaru.Vehicle) [
|
|||||||
var hassioConfig = map[string]string{}
|
var hassioConfig = map[string]string{}
|
||||||
|
|
||||||
// availability := `{"avty":["{"topic":"mysubaru/` + v.Vin + `"}"]}`
|
// availability := `{"avty":["{"topic":"mysubaru/` + v.Vin + `"}"]}`
|
||||||
origin := `"o":{"name":"MySubaruMQ","sw":"1.0.1","url":"https://www.git.savin.nyc/alex/mysubaru-mq"},`
|
origin := `"o":{"name":"MySubaruMQ","sw":"1.0.1","url":"https://git.savin.nyc/alex/mysubaru-mq"},`
|
||||||
// availability := `"avty":[{"t":"musubarymq/status"}],"avty_t":"{{value_json.value}}",`
|
// availability := `"avty":[{"t":"musubarymq/status"}],"avty_t":"{{value_json.value}}",`
|
||||||
device := `"dev":{"ids":["` + v.Vin + `"],"name":"` + v.CarNickname + `","mf":"Subaru Corp.","mdl":"` + v.CarNickname + `","hw":"` + v.ModelCode + `"},` // TODO chnage model to the proper one
|
device := `"dev":{"ids":["` + v.Vin + `"],"name":"` + v.CarNickname + `","mf":"Subaru Corp.","mdl":"` + v.CarNickname + `","hw":"` + v.ModelCode + `"},` // TODO chnage model to the proper one
|
||||||
obj_id_prefix := strings.Replace(strings.ToLower(v.CarNickname), " ", "_", -1)
|
obj_id_prefix := strings.Replace(strings.ToLower(v.CarNickname), " ", "_", -1)
|
||||||
@ -348,7 +345,6 @@ func (s *MySubaruClient) mySubaruConfigToMQTTHassioConfig(v *mysubaru.Vehicle) [
|
|||||||
msgs = s.messages(topic, 1, true, payload, msgs)
|
msgs = s.messages(topic, 1, true, payload, msgs)
|
||||||
s.log.Debug("hassio mqtt configuration", "topic", string(topic), "payload", string(payload))
|
s.log.Debug("hassio mqtt configuration", "topic", string(topic), "payload", string(payload))
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgs
|
return msgs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -356,7 +352,11 @@ func (s *MySubaruClient) mySubaruConfigToMQTTHassioConfig(v *mysubaru.Vehicle) [
|
|||||||
func (s *MySubaruClient) mySubaruStatusToMQTTMessage(v *mysubaru.Vehicle) []*bus.Message {
|
func (s *MySubaruClient) mySubaruStatusToMQTTMessage(v *mysubaru.Vehicle) []*bus.Message {
|
||||||
var state = map[string]string{}
|
var state = map[string]string{}
|
||||||
|
|
||||||
state[`mysubarumq/`+v.Vin+`/state`] = `{"odometer_km":` + strconv.Itoa(v.Odometer.Kilometers) + `,"odometer_mi":` + strconv.Itoa(v.Odometer.Miles) + `,"dist_to_empty_km":` + strconv.Itoa(v.DistanceToEmpty.Kilometers) + `,"dist_to_empty_mi":` + strconv.Itoa(v.DistanceToEmpty.Miles) + `,"dist_to_empty_pc":` + strconv.Itoa(v.DistanceToEmpty.Percentage) + `,"consumption_us":` + fmt.Sprintf("%.2f", v.FuelConsumptionAvg.MPG) + `,"consumption_eu":` + fmt.Sprintf("%.2f", v.FuelConsumptionAvg.LP100Km) + `,"engine_state":"` + v.EngineState + `"}`
|
tank := ""
|
||||||
|
if v.DistanceToEmpty.Percentage > 0 && 101 >= v.DistanceToEmpty.Percentage {
|
||||||
|
tank = `"dist_to_empty_pc":` + strconv.Itoa(v.DistanceToEmpty.Percentage) + `,`
|
||||||
|
}
|
||||||
|
state[`mysubarumq/`+v.Vin+`/state`] = `{` + tank + `"odometer_km":` + strconv.Itoa(v.Odometer.Kilometers) + `,"odometer_mi":` + strconv.Itoa(v.Odometer.Miles) + `,"dist_to_empty_km":` + strconv.Itoa(v.DistanceToEmpty.Kilometers) + `,"dist_to_empty_mi":` + strconv.Itoa(v.DistanceToEmpty.Miles) + `,"consumption_us":` + fmt.Sprintf("%.2f", v.FuelConsumptionAvg.MPG) + `,"consumption_eu":` + fmt.Sprintf("%.2f", v.FuelConsumptionAvg.LP100Km) + `,"engine_state":"` + v.EngineState + `"}`
|
||||||
state[`mysubarumq/`+v.Vin+`/attr`] = `{"source_type":"gps","latitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Latitude) + `,"longitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Longitude) + `,"course":` + strconv.Itoa(v.GeoLocation.Heading) + `,"speed":` + fmt.Sprintf("%.2f", v.GeoLocation.Speed) + `,"friendly_name":"` + v.CarNickname + `"}`
|
state[`mysubarumq/`+v.Vin+`/attr`] = `{"source_type":"gps","latitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Latitude) + `,"longitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Longitude) + `,"course":` + strconv.Itoa(v.GeoLocation.Heading) + `,"speed":` + fmt.Sprintf("%.2f", v.GeoLocation.Speed) + `,"friendly_name":"` + v.CarNickname + `"}`
|
||||||
|
|
||||||
var msgs []*bus.Message
|
var msgs []*bus.Message
|
||||||
|
Reference in New Issue
Block a user