Skip to content

Commit

Permalink
Merge pull request #10 from AaronSaikovski/v2.0_refactor
Browse files Browse the repository at this point in the history
V2.0 refactor
  • Loading branch information
AaronSaikovski authored Feb 14, 2024
2 parents ad750b4 + 31160f9 commit 0e5450e
Show file tree
Hide file tree
Showing 41 changed files with 672 additions and 590 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"
go-version: "1.22"

- name: Verify dependencies
run: go mod verify
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
*.DS_Store

# Test binary, built with `go test -c`
*.test
Expand All @@ -28,3 +29,5 @@ bin/
output*.*
out*.*
out.txt

.idea/
3 changes: 2 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ builds:
goos:
- linux
- windows
- darwin
- darwin
main: ./cmd/gogoodwe

archives:
- format: tar.gz
Expand Down
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
# CHANGELOG
# GoGoodwe - CHANGELOG

## v2.0.0 (2024-01-22)

- Major refactoring to cleanup project structure.
- Simplified package structure.
- refactored structs to use pointers more efficiently.

## v1.4.0 (2023-08-30)

- Major refactoring to move non-shared code to /internal folder
- Abstracted core away from main()

## v1.1.0 (2023-08-16)

- refactored code to make errors bubble back to main package and better error reporting/logging
- refactored main package to include run() method
- removed staticcheck as there is a bug with Go v1.21

## v1.0.0 (2023-08-10)

- initial version 1.0 release
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023, Aaron Saikovski
Copyright (c) 2024, Aaron Saikovski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
82 changes: 53 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,68 +1,92 @@
# Define Go command and flags
GO = go
GOFLAGS = -ldflags="-s -w"

#export PATH=$PATH:$HOME/go/bin;

# Define the target executable
TARGET = gogoodwe
MAINAPPPATH = ./cmd/${TARGET}/main.go

default: help

.PHONY: help
## help - Display help about make targets for this Makefile
help:
@cat Makefile | grep '^## ' --color=never | cut -c4- | sed -e "`printf 's/ - /\t- /;'`" | column -s "`printf '\t'`" -t

## localrelease - Builds the project in preparation for (local)release
localrelease:
go build $(GOFLAGS) -o bin/${TARGET} main.go

.PHONY: release
## release - Builds the project in preparation for (local)release
release: vet lint seccheck
go build $(GOFLAGS) -o bin/${TARGET} ${MAINAPPPATH}
file bin/${TARGET}

## release - Builds the project in preparation for release
release:
goreleaser release --snapshot --clean

## debug - Builds the project in preparation for debug

.PHONY: goreleaser
## goreleaser - Builds the project in preparation for release
goreleaser:
goreleaser release --snapshot --clean


.PHONY: docs
## docs - updates the swagger docs
docs:
swag init


.PHONY: build
## build - Builds the project in preparation for debug
build:
go build -o bin/${TARGET} main.go
go build -o bin/${TARGET} ${MAINAPPPATH}
file bin/${TARGET}

## buildandrun - builds and runs the program on the target platform
buildandrun: build
./bin/${TARGET}

## run - runs main.go for testing
run: dep
go run main.go
.PHONY: run
## run - builds and runs the program on the target platform
run:
go run ${MAINAPPPATH}


.PHONY: clean
## clean - Remove the old builds and any debug information
clean:
go clean -cache
go clean
rm -rf dist
rm bin/${TARGET}

## test - executes unit test

.PHONY: test
## test - executes unit tests
test:
go test ./...
go test -v ./test/...

## dep - fetches any external dependencies
dep:

.PHONY: deps
## deps - fetches any external dependencies and updates
deps:
go mod tidy
go mod download
go get -u ./...


.PHONY: vet
## vet - Vet examines Go source code and reports suspicious constructs
vet:
go vet ./...

## staticcheck - Runs static code analyzer staticcheck
staticcheck:
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./...

.PHONY: staticcheck
## staticcheck - Runs static code analyzer staticcheck - currently broken
staticcheck:
staticcheck ./...


.PHONY: seccheck
## seccheck - Code vulnerability check
seccheck:
brew install govulncheck
govulncheck ./...
#govulncheck ./...


.PHONY: lint
## lint - format code and tidy modules
lint:
go fmt ./...
go mod tidy -v
go mod tidy -v
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<div align="center">
# GoGoodwe V2.0
=======

# GoGoodwe

A command line tool and Go packages to query the GOODWE SEMS Portal APIs - written in 100% Go.
A command line tool and query the GOODWE SEMS Inverter APIs - written in 100% Go.

[![Build Status](https://github.com/AaronSaikovski/gogoodwe/workflows/build/badge.svg)](https://github.com/AaronSaikovski/gogoodwe/actions)
[![Coverage Status](https://coveralls.io/repos/github/AaronSaikovski/gogoodwe/badge.svg?branch=main)](https://coveralls.io/github/AaronSaikovski/gogoodwe?branch=main)
[![Licence](https://img.shields.io/github/license/AaronSaikovski/gogoodwe)](LICENSE)

</div>
Expand All @@ -15,17 +14,19 @@ A command line tool and Go packages to query the GOODWE SEMS Portal APIs - writt
The toolchain is mainly driven by the Makefile.

```bash
* help - Display help about make targets for this Makefile
* release - Builds the project in preparation for release
* debug - Builds the project in preparation for debug
* buildandrun - builds and runs the program on the target platform
* run - runs main.go for testing
* clean - Remove the old builds and any debug information
* test - executes unit test
* dep - fetches any external dependencies
* vet - Vet examines Go source code and reports suspicious constructs
* staticcheck - Runs static code analyzer staticcheck
* lint - format code and tidy modules
help - Display help about make targets for this Makefile
release - Builds the project in preparation for (local)release
goreleaser - Builds the project in preparation for release
docs - updates the swagger docs
build - Builds the project in preparation for debug
run - builds and runs the program on the target platform
clean - Remove the old builds and any debug information
test - executes unit tests
deps - fetches any external dependencies and updates
vet - Vet examines Go source code and reports suspicious constructs
staticcheck - Runs static code analyzer staticcheck - currently broken
seccheck - Code vulnerability check
lint - format code and tidy modules
```

To get started type,
Expand All @@ -46,10 +47,12 @@ From the command line the usage is pretty simple:

```bash
##Note the use of single quotes ''
./gogoodwe --account '<user@email.com>' --pwd '<password>' --powerstationid '<powerstation id>'
./gogoodwe --account '<user@email.com>' --pwd '<password>' --powerstationid '<powerstation id>' --dailysummary

# Or
./gogoodwe -a '<user@email.com>' -p '<password>' -i '<powerstation id>'
./gogoodwe -a '<user@email.com>' -p '<password>' -i '<powerstation id>' -d

##where daily summary provides a shorter daily view of the inverter data
```

To get the help on using the command line tool, type:
Expand Down
24 changes: 17 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# gogoodwe TODO
# GoGoodwe V2 - TODO

GoGoodwe backlog
### ToDo

### Todo

- [ ] Add ability to output to file with a flag
- [ ] Add ability to have a smaller output struct of just key reporting data
- [ ] Add Golang contexts for API calls
- [ ]

### In Progress

- [ ] add unit tests

### Done ✓

- [x] Add the ability to query the inverter status for Generation today and Status (check if operational).

### Future/Roadmap

- [ ] Format the inverter output to make it more human readable.
- [ ] Add ability to output inverter data to a file.
- [ ] Add the ability to query historical data for a single day.
- [ ] Have the ability to have a realtime logging to the screen or to a file in 5 minute intervals.
- [ ] Add the ability to produce a daily summary of key data (Generation today, Income today, total generation, total income).
- [ ] Add goroutines and wait groups for the API calls and maybe channels for success/failed API calls.
- [ ] Investigate the ability to generate .CSV files as output.
30 changes: 30 additions & 0 deletions cmd/gogoodwe/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package app

// Main package - This is the main program entry point
import (
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/powerstation"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils"
"github.com/alexflint/go-arg"
)

// Run - main program runner
func Run() error {

//Get the args input data
var args utils.Args
p := arg.MustParse(&args)

//check for valid email address input
if !utils.CheckValidEmail(args.Account) {
p.Fail("Invalid Email address format - should be: 'user@somedomain.com'.")
}

//check for valid powerstation Id
if !utils.CheckValidPowerstationID(args.PowerStationID) {
p.Fail("Invalid Powerstation ID format: - should be: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'.")
}

// Get the data from the API, return any errors. Pass in args as string
return powerstation.FetchData(args.Account, args.Password, args.PowerStationID, args.DailySummary)

}
10 changes: 10 additions & 0 deletions cmd/gogoodwe/interfaces/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package interfaces

import (
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types"
)

// Constraints for functions that return data from the API via marshalled structs
type ISemsDataConstraint interface {
types.InverterData | types.DailySummaryData
}
19 changes: 19 additions & 0 deletions cmd/gogoodwe/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Package main implements a program that authenticates to and queries the SEMS Solar inverter API.
*/
package main

import (
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/app"
"log"
)

func main() {
if err := runApp(); err != nil {
log.Fatalf("error: %v", err)
}
}

func runApp() error {
return app.Run()
}
9 changes: 9 additions & 0 deletions cmd/gogoodwe/powerstation/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package powerstation

const (
// Powerstation API Url
PowerStationURL string = "v2/PowerStation/GetMonitorDetailByPowerstationId"

// Default timeout value
HTTPTimeout int = 20
)
39 changes: 39 additions & 0 deletions cmd/gogoodwe/powerstation/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
# Name: data - fetches data from the goodwe API - and processes it to pass back to caller
# Author: Aaron Saikovski - asaikovski@outlook.com
*/
package powerstation

import (
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/semsapi"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils"
)

func FetchData(Account string, Password string, PowerStationID string, DailySummary bool) error {

// User account struct
creds := &types.LoginCredentials{
Account: Account,
Password: Password,
PowerStationID: PowerStationID,
}

// Do the login..check for errors
loginApiResponse, err := semsapi.Login(creds)
if err != nil {
utils.HandleError(err)
return err
}

//fetch data based on
if DailySummary {
getMonitorSummaryByPowerstationId(creds, loginApiResponse)

} else {
//powerstationData = types.InverterData
getMonitorDetailByPowerstationId(creds, loginApiResponse)
}

return nil
}
Loading

0 comments on commit 0e5450e

Please sign in to comment.