Skip to content

Commit

Permalink
Add user-friendly args to chargemeup
Browse files Browse the repository at this point in the history
One can now query with --address or --lat/--lon
  • Loading branch information
neomantra committed Feb 29, 2024
1 parent 13f7ae8 commit 8d4d0ab
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
79 changes: 71 additions & 8 deletions cmd/chargemeup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/deepmap/oapi-codegen/pkg/securityprovider"
openchargemap "github.com/neomantra/go-openchargemap"
"github.com/spf13/pflag"
nominatim "github.com/yuriizinets/go-nominatim"
)

/////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -18,9 +19,12 @@ var usageFormat string = `usage: %s <options> [input]
"chargemeup" assists with queries to OpenChargeMap.
Around Newark is:
chargemeup -p "(40.63010790372053,-74.2775717248681),(40.7356464076158,-74.09370618215354)"
chargemup -a "Newark, NJ" -r 10
chargemeup --lat 40.7356464076158 --lon -74.09370618215354 --radius 5
`

const defaultServer = "https://api.openchargemap.io/v3"
Expand All @@ -29,17 +33,46 @@ type ChargeMeUpConfig struct {
Server string
APIKey string
Verbose bool

// Area to search, in order of priority:
// BoundingBox
// Latitude + Longitude + Radius
BoundingBox string
Latitude float32
Longitude float32
Radius float32
}

// Returns nil if the Search Area in the Config is 'roughly valid', otherwise an error describing the issue with it.
// 'Roughly valid' means that it has the appropriate data, not that the data makes any sense or that OCM will accept it.
func (c ChargeMeUpConfig) IsAreaValid() error {
// see order in ChargeMeUp struct above
if c.BoundingBox != "" {
return nil
}
if c.Latitude == 0 && c.Longitude == 0 {
return fmt.Errorf("either --bbox, --address, or --lat/--lon is required")
}
if c.Radius == 0 {
return fmt.Errorf("--radius is required with --address or --latitude / --longitude")
}
return nil
}

/////////////////////////////////////////////////////////////////////////////////////

func main() {
// Set up configuration
var config ChargeMeUpConfig
var boundingBox string
var address string
var showHelp bool

pflag.StringVarP(&boundingBox, "bbox", "b", "", "bounding box for query, \"(lat1,lon1),(lat2,lon2)\"")
pflag.StringVarP(&config.BoundingBox, "bbox", "b", "", "bounding box for query, \"(lat1,lon1),(lat2,lon2)\"")
pflag.StringVarP(&address, "address", "a", "", "address to query (requires --radius)")
pflag.Float32VarP(&config.Radius, "radius", "r", 0, "radial distance to query, in kilometers (requires --address)")
pflag.Float32VarP(&config.Latitude, "lat", "", 0, "latitude to query (requires --lon and --radius)")
pflag.Float32VarP(&config.Longitude, "lon", "", 0, "longitude to query (requires --lon and --radius)")

pflag.StringVarP(&config.Server, "server", "s", defaultServer, "API Server for OpenChargeMap, env var OCM_SERVER")
pflag.StringVarP(&config.APIKey, "key", "k", "", "API key for OpenChargeMap, env var OCM_KEY)")
pflag.BoolVarP(&config.Verbose, "verbose", "v", false, "verbose output to stderr")
Expand All @@ -52,6 +85,29 @@ func main() {
os.Exit(0)
}

if address != "" {
n := nominatim.Nominatim{}
results, err := n.Search(nominatim.SearchParameters{ // Check SearchResult struct for details
Query: address,
IncludeGeoJSON: true,
})
if err != nil {
fmt.Fprintf(os.Stderr, "error finding address '%s': %s\n", address, err.Error())
os.Exit(1)
}
if len(results) == 0 {
fmt.Fprintf(os.Stderr, "no location found for address '%s'\n", address)
os.Exit(1)
}
config.Latitude = float32(results[0].Lat)
config.Longitude = float32(results[0].Lng)
}

if err := config.IsAreaValid(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}

if config.Server == "" {
config.Server = os.Getenv("OCM_SERVER")
if config.Server == "" {
Expand All @@ -73,7 +129,7 @@ func main() {
os.Exit(1)
}

if err := lookupChargePoints(config, ocmClient, boundingBox); err != nil {
if err := lookupChargePoints(config, ocmClient); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
os.Exit(1)
}
Expand All @@ -98,12 +154,19 @@ func makeClient(config ChargeMeUpConfig) (*openchargemap.ClientWithResponses, er

///////////////////////////////////////////////////////////////////////////////

func lookupChargePoints(config ChargeMeUpConfig, ocmClient *openchargemap.ClientWithResponses, place string) error {

func lookupChargePoints(config ChargeMeUpConfig, ocmClient *openchargemap.ClientWithResponses) error {
ctx := context.Background()
params := &openchargemap.GetPoiParams{
Boundingbox: &place,
params := &openchargemap.GetPoiParams{}
if config.BoundingBox != "" {
params.Boundingbox = &config.BoundingBox
} else {
kmUnit := "km"
params.Distance = &config.Radius
params.Distanceunit = &kmUnit
params.Latitude = &config.Latitude
params.Longitude = &config.Longitude
}

resp, err := ocmClient.GetPoiWithResponse(ctx, params)
if err != nil {
return fmt.Errorf("openchargemap.GetPoi: %w", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/deepmap/oapi-codegen v1.16.2
github.com/oapi-codegen/runtime v1.1.1
github.com/spf13/pflag v1.0.5
github.com/yuriizinets/go-nominatim v1.3.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuriizinets/go-nominatim v1.3.0 h1:q0uI1K3br1jJKCNrhy8vDAoEiyMEuCkvuhHLljvRs6E=
github.com/yuriizinets/go-nominatim v1.3.0/go.mod h1:RsEKQ1sskjMf5wvzr/KbDeJSFGyh6Bg5mTMyhM5pJSc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 8d4d0ab

Please sign in to comment.