Skip to content

Commit

Permalink
feat(schedule): adding crontab logic
Browse files Browse the repository at this point in the history
  • Loading branch information
atbore-phx committed Mar 9, 2024
1 parent d15ef76 commit c4f9842
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 126 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/simonvetter/modbus v1.6.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
27 changes: 26 additions & 1 deletion home-assistant/addons/sbam/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@
"name": "sbam",
"version": "1.3.0",
"slug": "sbam",
"init": "false",
"description": "Smart Battery Avanced Manager",
"arch": [
"amd64"
],
"url": "https://github.com/atbore-phx/sbam",
"image": "ghcr.io/atbore-phx/ha-sbam"
"image": "ghcr.io/atbore-phx/ha-sbam",
"options": {
"url": "https://api.solcast.com.au/rooftop_sites/<YOUR-SITE>/forecasts?format=json",
"apikey": "null",
"fronius_ip": "null",
"start_hr": "00:00",
"end_hr": "06:00",
"crontab": "00 00-05 * * *",
"pw_consumption": "11000",
"max_charge": "3500",
"pw_batt_reserve": "3000",
"defaults": "true"
},
"schema": {
"url": "str",
"apikey": "password",
"fronius_ip": "match(^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$)",
"start_hr": "match(^([01]?[0-9]|2[0-3]):[0-5][0-9]$)",
"end_hr": "match(^([01]?[0-9]|2[0-3]):[0-5][0-9]$)",
"crontab": "str",
"pw_consumption": "float",
"max_charge": "float",
"pw_batt_reserve": "float",
"defaults": "bool"
}
}
13 changes: 12 additions & 1 deletion home-assistant/addons/sbam/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#!/usr/bin/with-contenv bashio

sbam -h
export URL=$(bashio::config 'url')
export APIKEY=$(bashio::config 'apikey')
export FRONIUS_IP=$(bashio::config 'fronius_ip')
export START_HR=$(bashio::config 'start_hr')
export END_HR=$(bashio::config 'end_hr')
export CRONTAB=$(bashio::config 'crontab')
export PW_CONSUMPTION=$(bashio::config 'pw_consumption')
export MAX_CHARGE=$(bashio::config 'max_charge')
export PW_BATT_RESERVE=$(bashio::config 'pw_batt_reserve')
export DEFAULTS=$(bashio::config 'defaults')

sbam schedule
51 changes: 38 additions & 13 deletions pkg/cmd/configure.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cmd

import (
"fmt"
"errors"
"sbam/pkg/fronius"
u "sbam/src/utils"
"strings"

"github.com/spf13/cobra"
Expand All @@ -15,20 +16,15 @@ var cfgCmd = &cobra.Command{
Long: `connect via modbus to the fronius inverter and set charging`,
Run: func(cmd *cobra.Command, args []string) {
fronius_ip := viper.GetString("fronius_ip")
if len(strings.TrimSpace(fronius_ip)) == 0 {
fmt.Println("The --fronius_ip flag must be set")
power := viper.GetViper().GetInt("power")

err := checkConfigure(fronius_ip)
if err != nil {
u.Log.Error(err)
return
}
if cmd.Flags().Changed("defaults") {
fronius.Setdefaults(fronius_ip)
} else if cmd.Flags().Changed("force-charge") {
power := viper.GetViper().GetInt("power")
if power == 0 {
fmt.Println("The --power flag must be set when using --force-charge")
return
}
fronius.ForceCharge(fronius_ip, int16(power))
}

configure(fronius_ip, power, cmd)

},
}
Expand All @@ -42,3 +38,32 @@ func init() {
viper.BindPFlag("power", cfgCmd.Flags().Lookup("power"))
rootCmd.AddCommand(cfgCmd)
}

func checkConfigure(fronius_ip string) error {
if len(strings.TrimSpace(fronius_ip)) == 0 {
err := errors.New("the --fronius_ip flag must be set")
return err
}
return nil
}

func configure(fronius_ip string, power int, cmd *cobra.Command) {
if cmd.Flags().Changed("defaults") {
err := fronius.Setdefaults(fronius_ip)
if err != nil {
u.Log.Error(err)
panic(err)
}
} else if cmd.Flags().Changed("force-charge") {
if power == 0 {
u.Log.Error("The --power flag must be set when using --force-charge")
return
}
err := fronius.ForceCharge(fronius_ip, int16(power))
if err != nil {
u.Log.Error(err)
panic(err)
}
}

}
55 changes: 36 additions & 19 deletions pkg/cmd/estimate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"fmt"
"errors"
"sbam/pkg/power"
"sbam/pkg/storage"
u "sbam/src/utils"
"strings"

"github.com/spf13/cobra"
Expand All @@ -18,28 +19,14 @@ var estCmd = &cobra.Command{
url := viper.GetString("url")
apiKey := viper.GetString("apikey")
fronius_ip := viper.GetString("fronius_ip")
if len(strings.TrimSpace(fronius_ip)) == 0 {
fmt.Println("The --fronius_ip flag must be set")
return
} else if len(strings.TrimSpace(apiKey)) == 0 {
fmt.Println("The --apiKey flag must be set")
return
} else if len(strings.TrimSpace(url)) == 0 {
fmt.Println("The --url flag must be set")
return
}

pwr := power.New()
_, err := pwr.Handler(apiKey, url)
err := CheckEstimate(apiKey, url, fronius_ip)
if err != nil {
panic(err)
u.Log.Error(err)
return
}
estimate(apiKey, url, fronius_ip)

str := storage.New()
_, _, err = str.Handler(fronius_ip)
if err != nil {
panic(err)
}
},
}

Expand All @@ -52,3 +39,33 @@ func init() {
viper.BindPFlag("fronius_ip", estCmd.Flags().Lookup("fronius_ip"))
rootCmd.AddCommand(estCmd)
}

func CheckEstimate(apiKey string, url string, fronius_ip string) error {
if len(strings.TrimSpace(fronius_ip)) == 0 {
err := errors.New("the --fronius_ip flag must be set")
return err
} else if len(strings.TrimSpace(apiKey)) == 0 {
err := errors.New("the --apiKey flag must be set")
return err
} else if len(strings.TrimSpace(url)) == 0 {
err := errors.New("the --url flag must be set")
return err
}
return nil
}

func estimate(apiKey string, url string, fronius_ip string) {
pwr := power.New()
_, err := pwr.Handler(apiKey, url)
if err != nil {
u.Log.Error(err)
panic(err)
}

str := storage.New()
_, _, err = str.Handler(fronius_ip)
if err != nil {
u.Log.Error(err)
panic(err)
}
}
Loading

0 comments on commit c4f9842

Please sign in to comment.