Skip to content

Commit

Permalink
Merge pull request #12 from AaronSaikovski/v2.0.2_refactored
Browse files Browse the repository at this point in the history
V2.0.2 refactored
  • Loading branch information
AaronSaikovski authored Apr 26, 2024
2 parents 0b943d3 + 4033cf9 commit c7b5698
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package apilogin

/*
# Name: LoginCredentials - Struct to hold User login data
# Author: Aaron Saikovski - [email protected]
*/
package types

// LoginCredentials - Struct to hold User login data
type LoginCredentials struct {
// ApiLoginCredentials - Struct to hold User login credentials
type ApiLoginCredentials struct {
Account string `json:"account"`
Password string `json:"pwd"`
PowerStationID string `json:"powerstationid"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package apilogin

/*
# Name: LoginResponse - SEMS API Response Data struct
# Contains all the JSON Response data returned from the authentication API - "https://www.semsportal.com/api/v2/Common/CrossLogin"
# Will be unmarshalled to a struct via a pointer
# Author: Aaron Saikovski - [email protected]
*/
package types

// LoginResponse - SEMS API Response Data struct
type LoginResponse struct {
// LoginResponse - SEMS API Response struct
type ApiLoginResponse struct {
HasError bool `json:"hasError"`
Code int32 `json:"code"`
Msg string `json:"msg"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package semsapi
package apilogin

const (
AuthLoginURL = "https://www.semsportal.com/api/v2/Common/CrossLogin"
Expand Down
11 changes: 5 additions & 6 deletions cmd/gogoodwe/semsapi/login.go → cmd/gogoodwe/apilogin/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,29 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package semsapi
package apilogin

import (
"bytes"
"net/http"
"time"

"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils"
)

// Login - Login to the SEMS API passing in a LoginCredentials struct and returning a LoginResponse struct.
func Login(LoginCredentials *types.LoginCredentials) (*types.LoginResponse, error) {
func (loginCredentials *ApiLoginCredentials) APILogin() (*ApiLoginResponse, error) {

// API Response struct
loginApiResponse := types.LoginResponse{}
loginApiResponse := ApiLoginResponse{}

//check if the UserLogin struct is empty
if err := checkUserLoginInfo(LoginCredentials); err != nil {
if err := checkUserLoginInfo(loginCredentials); err != nil {
return nil, err
}

// User login struct to be converted to JSON
loginData, err := utils.MarshalStructToJSON(LoginCredentials)
loginData, err := utils.MarshalStructToJSON(loginCredentials)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package semsapi
package apilogin

import (
"errors"
"net/http"
"strings"

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

// SetHeaders - Set the login headers for the SEMS API login
Expand All @@ -46,8 +44,8 @@ func checkUserLoginResponse(loginResponse string) error {
}

// CheckUserLoginInfo - Check user login struct is valid/not null
func checkUserLoginInfo(UserLogin *types.LoginCredentials) error {
if *UserLogin == (types.LoginCredentials{}) {
func checkUserLoginInfo(userLogin *ApiLoginCredentials) error {
if *userLogin == (ApiLoginCredentials{}) {
return errors.New("**Error: User Login details are empty or invalid..**")
}
return nil
Expand Down
8 changes: 5 additions & 3 deletions cmd/gogoodwe/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ 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
// Run is the main program runner.
//
// No parameters.
// Returns an error.
func Run() error {

//Get the args input data
Expand All @@ -48,6 +50,6 @@ func Run() error {
}

// 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)
return fetchData(args.Account, args.Password, args.PowerStationID, args.DailySummary)

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,46 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package app

package powerstation

// Main package - This is the main program entry point
import (
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/semsapi"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/apilogin"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/monitordata"
"github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils"
)

// FetchData fetches data based on user account credentials and power station ID, and can retrieve daily summary if specified.
// Parameters:
//
// Account string - user account
// Password string - account password
// PowerStationID string - ID of the power station
// DailySummary bool - whether to retrieve daily summary
// fetchData fetches data based on user account credentials and power station ID, and can retrieve daily summary if specified.
//
// Return type:
// Parameters:
// - Account: the email account associated with the user.
// - Password: the password associated with the user's account.
// - PowerStationID: the ID of the power station.
// - DailySummary: a boolean indicating whether to retrieve a daily summary.
//
// error
func FetchData(Account string, Password string, PowerStationID string, DailySummary bool) error {
// Returns:
// - error: an error if there was a problem logging in or fetching data.
func fetchData(Account string, Password string, PowerStationID string, DailySummary bool) error {

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

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

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

} else {
//powerstationData = types.InverterData
getMonitorDetailByPowerstationId(creds, loginApiResponse)
//fetch data and output
dataErr := monitordata.GetData(loginCreds, loginApiResponse, DailySummary)
if dataErr != nil {
utils.HandleError(dataErr)
return dataErr
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package powerstation
package monitordata

const (
// Powerstation API Url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package monitordata

/*
# Name: DailySummaryData - Struct to hold daily summary data
*/
package types

// DailySummaryData - Struct to hold daily summary data
type DailySummaryData struct {
Language string `json:"language"`
HasError bool `json:"hasError"`
Expand Down
Loading

0 comments on commit c7b5698

Please sign in to comment.