-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from HappyTobi/start_charging
[NEW] charge start stop command
- Loading branch information
Showing
38 changed files
with
698 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/HappyTobi/warp/pkg/cmd/charge" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func ChargeCmd() *cobra.Command { | ||
users := &cobra.Command{ | ||
Use: "charge", | ||
Short: "Charge command", | ||
} | ||
users.AddCommand(StartCmd()) | ||
users.AddCommand(StopCmd()) | ||
|
||
return users | ||
} | ||
|
||
func StartCmd() *cobra.Command { | ||
startCmd := &cobra.Command{ | ||
Use: "start", | ||
Short: "Start charging for a specified user", | ||
RunE: charge.Start, | ||
PreRunE: ValidateChargeStart, | ||
} | ||
|
||
startCmd.Flags().StringP("user", "r", "", "User to start charging") | ||
startCmd.Flags().IntP("charge-power", "a", -1, "Ampere to charge with") | ||
_ = startCmd.MarkFlagRequired("user") | ||
|
||
return startCmd | ||
} | ||
|
||
func StopCmd() *cobra.Command { | ||
stopCmd := &cobra.Command{ | ||
Use: "stop", | ||
Short: "Stop charging", | ||
RunE: charge.Stop, | ||
PreRunE: ValidateChargeStop, | ||
} | ||
|
||
return stopCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package charge | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/HappyTobi/warp/pkg/cmd/middleware" | ||
"github.com/HappyTobi/warp/pkg/internal/evse" | ||
"github.com/HappyTobi/warp/pkg/internal/nfc" | ||
"github.com/HappyTobi/warp/pkg/internal/users" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Start(cmd *cobra.Command, args []string) error { | ||
request, err := middleware.LoadWarpRequest(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nfcTagService := nfc.NewNfcTagsService(request) | ||
nfcTags, err := nfcTagService.Config() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
userService := users.NewUsersService(request) | ||
allUsers, err := userService.AllUsernames() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
evseService := evse.NewEvseService(request) | ||
chargePower, err := evseService.CurrentChargePower() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//validate passed user | ||
userTag := nfc.UserTag{} | ||
userFilter, _ := cmd.Flags().GetString("user") | ||
for _, user := range allUsers { | ||
if strings.EqualFold(user.Username, userFilter) { | ||
for _, tag := range nfcTags.AuthorizedTags { | ||
if tag.UserId == user.Id { | ||
userTag = tag | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
if len(userTag.Id) == 0 { | ||
return fmt.Errorf("The passed user is not valid or has no valid nfc tag") | ||
} | ||
|
||
//check if chargepower is set and changed | ||
cPowerArg, _ := cmd.Flags().GetInt("charge-power") | ||
if cPowerArg > 0 && cPowerArg != chargePower.Current { | ||
//update chargepower at warp charger | ||
if err := evseService.UpdateChargePower(cPowerArg); err != nil { | ||
return err | ||
} | ||
|
||
chargePower.Current = cPowerArg | ||
} | ||
|
||
if err := nfcTagService.StartCharging(userTag); err != nil { | ||
return err | ||
} | ||
|
||
//fmt.Printf(request.OutputRenderer.Render() | ||
fmt.Printf("Charge started for user %s with %d Ampere", userFilter, chargePower.Current) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package charge | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/HappyTobi/warp/pkg/cmd/middleware" | ||
"github.com/HappyTobi/warp/pkg/internal/nfc" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Stop(cmd *cobra.Command, args []string) error { | ||
request, err := middleware.LoadWarpRequest(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nfcTagService := nfc.NewNfcTagsService(request) | ||
if err := nfcTagService.StopCharging(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Charge stopped") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.