From e2c85dd7f8beca448ff98c60fc40fd5c7442c9c8 Mon Sep 17 00:00:00 2001 From: aaronsaikovski Date: Fri, 26 Apr 2024 14:25:41 +1000 Subject: [PATCH 1/2] big refactor --- .../apilogincredentials.go} | 11 +-- .../apiloginresponse.go} | 13 +--- .../{semsapi => apilogin}/constants.go | 2 +- cmd/gogoodwe/{semsapi => apilogin}/login.go | 11 ++- .../utils.go => apilogin/loginutils.go} | 8 +-- cmd/gogoodwe/app/app.go | 34 ++++++++- .../constants.go | 2 +- .../dailysummarydata.go | 3 +- .../{interfaces => monitordata}/interfaces.go | 8 +-- .../{types => monitordata}/inverterdata.go | 2 +- .../monitordata.go} | 15 ++-- .../{powerstation => monitordata}/output.go | 2 +- .../{powerstation => monitordata}/utils.go | 11 ++- cmd/gogoodwe/powerstation/fetchdata.go | 70 ------------------- 14 files changed, 65 insertions(+), 127 deletions(-) rename cmd/gogoodwe/{types/logincredentials.go => apilogin/apilogincredentials.go} (84%) rename cmd/gogoodwe/{types/loginresponse.go => apilogin/apiloginresponse.go} (81%) rename cmd/gogoodwe/{semsapi => apilogin}/constants.go (98%) rename cmd/gogoodwe/{semsapi => apilogin}/login.go (88%) rename cmd/gogoodwe/{semsapi/utils.go => apilogin/loginutils.go} (90%) rename cmd/gogoodwe/{powerstation => monitordata}/constants.go (98%) rename cmd/gogoodwe/{types => monitordata}/dailysummarydata.go (98%) rename cmd/gogoodwe/{interfaces => monitordata}/interfaces.go (90%) rename cmd/gogoodwe/{types => monitordata}/inverterdata.go (99%) rename cmd/gogoodwe/{powerstation/monitordetails.go => monitordata/monitordata.go} (84%) rename cmd/gogoodwe/{powerstation => monitordata}/output.go (98%) rename cmd/gogoodwe/{powerstation => monitordata}/utils.go (85%) delete mode 100644 cmd/gogoodwe/powerstation/fetchdata.go diff --git a/cmd/gogoodwe/types/logincredentials.go b/cmd/gogoodwe/apilogin/apilogincredentials.go similarity index 84% rename from cmd/gogoodwe/types/logincredentials.go rename to cmd/gogoodwe/apilogin/apilogincredentials.go index f913774..9868486 100644 --- a/cmd/gogoodwe/types/logincredentials.go +++ b/cmd/gogoodwe/apilogin/apilogincredentials.go @@ -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 - asaikovski@outlook.com -*/ -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"` diff --git a/cmd/gogoodwe/types/loginresponse.go b/cmd/gogoodwe/apilogin/apiloginresponse.go similarity index 81% rename from cmd/gogoodwe/types/loginresponse.go rename to cmd/gogoodwe/apilogin/apiloginresponse.go index 55dbca4..7d4d4e1 100644 --- a/cmd/gogoodwe/types/loginresponse.go +++ b/cmd/gogoodwe/apilogin/apiloginresponse.go @@ -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 - asaikovski@outlook.com -*/ -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"` diff --git a/cmd/gogoodwe/semsapi/constants.go b/cmd/gogoodwe/apilogin/constants.go similarity index 98% rename from cmd/gogoodwe/semsapi/constants.go rename to cmd/gogoodwe/apilogin/constants.go index 9defda5..7fc98be 100644 --- a/cmd/gogoodwe/semsapi/constants.go +++ b/cmd/gogoodwe/apilogin/constants.go @@ -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" diff --git a/cmd/gogoodwe/semsapi/login.go b/cmd/gogoodwe/apilogin/login.go similarity index 88% rename from cmd/gogoodwe/semsapi/login.go rename to cmd/gogoodwe/apilogin/login.go index 3d9e832..73b2412 100644 --- a/cmd/gogoodwe/semsapi/login.go +++ b/cmd/gogoodwe/apilogin/login.go @@ -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 } diff --git a/cmd/gogoodwe/semsapi/utils.go b/cmd/gogoodwe/apilogin/loginutils.go similarity index 90% rename from cmd/gogoodwe/semsapi/utils.go rename to cmd/gogoodwe/apilogin/loginutils.go index d54b103..62a53df 100644 --- a/cmd/gogoodwe/semsapi/utils.go +++ b/cmd/gogoodwe/apilogin/loginutils.go @@ -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 @@ -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 diff --git a/cmd/gogoodwe/app/app.go b/cmd/gogoodwe/app/app.go index ff130ce..2091023 100644 --- a/cmd/gogoodwe/app/app.go +++ b/cmd/gogoodwe/app/app.go @@ -25,7 +25,8 @@ 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/apilogin" + "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/monitordata" "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils" "github.com/alexflint/go-arg" ) @@ -48,6 +49,35 @@ 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 powerstation.FetchData(args.Account, args.Password, args.PowerStationID, args.DailySummary) + return fetchData(args.Account, args.Password, args.PowerStationID, args.DailySummary) } + +// fetchData - Fetches data based on user account credentials and power station ID, and can retrieve daily summary if specified. +func fetchData(Account string, Password string, PowerStationID string, DailySummary bool) error { + + // User account struct + loginCreds := &apilogin.ApiLoginCredentials{ + Account: Account, + Password: Password, + PowerStationID: PowerStationID, + } + + // Do the login..check for errors + loginApiResponse, err := loginCreds.APILogin() + if err != nil { + utils.HandleError(err) + return err + } + + //fetch data based on + if DailySummary { + monitordata.GetMonitorSummaryByPowerstationId(loginCreds, loginApiResponse) + + } else { + monitordata.GetMonitorDetailByPowerstationId(loginCreds, loginApiResponse) + } + + return nil +} diff --git a/cmd/gogoodwe/powerstation/constants.go b/cmd/gogoodwe/monitordata/constants.go similarity index 98% rename from cmd/gogoodwe/powerstation/constants.go rename to cmd/gogoodwe/monitordata/constants.go index 384ec7a..cba0086 100644 --- a/cmd/gogoodwe/powerstation/constants.go +++ b/cmd/gogoodwe/monitordata/constants.go @@ -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 diff --git a/cmd/gogoodwe/types/dailysummarydata.go b/cmd/gogoodwe/monitordata/dailysummarydata.go similarity index 98% rename from cmd/gogoodwe/types/dailysummarydata.go rename to cmd/gogoodwe/monitordata/dailysummarydata.go index 0b8d0ec..847902d 100644 --- a/cmd/gogoodwe/types/dailysummarydata.go +++ b/cmd/gogoodwe/monitordata/dailysummarydata.go @@ -22,11 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - /* # Name: DailySummaryData - Struct to hold daily summary data */ -package types +package monitordata type DailySummaryData struct { Language string `json:"language"` diff --git a/cmd/gogoodwe/interfaces/interfaces.go b/cmd/gogoodwe/monitordata/interfaces.go similarity index 90% rename from cmd/gogoodwe/interfaces/interfaces.go rename to cmd/gogoodwe/monitordata/interfaces.go index 61f6fff..152277f 100644 --- a/cmd/gogoodwe/interfaces/interfaces.go +++ b/cmd/gogoodwe/monitordata/interfaces.go @@ -21,13 +21,9 @@ 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 interfaces - -import ( - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types" -) +package monitordata // Constraints for functions that return data from the API via marshalled structs type ISemsDataConstraint interface { - types.InverterData | types.DailySummaryData + InverterData | DailySummaryData } diff --git a/cmd/gogoodwe/types/inverterdata.go b/cmd/gogoodwe/monitordata/inverterdata.go similarity index 99% rename from cmd/gogoodwe/types/inverterdata.go rename to cmd/gogoodwe/monitordata/inverterdata.go index ff288e5..dd10f6f 100644 --- a/cmd/gogoodwe/types/inverterdata.go +++ b/cmd/gogoodwe/monitordata/inverterdata.go @@ -27,7 +27,7 @@ SOFTWARE. # Minimised version - removed any sensitive data # Author: Aaron Saikovski - asaikovski@outlook.com */ -package types +package monitordata // InverterData - Struct to hold data returned from the Inverter Powerstation API type InverterData struct { diff --git a/cmd/gogoodwe/powerstation/monitordetails.go b/cmd/gogoodwe/monitordata/monitordata.go similarity index 84% rename from cmd/gogoodwe/powerstation/monitordetails.go rename to cmd/gogoodwe/monitordata/monitordata.go index 3628742..2ef5952 100644 --- a/cmd/gogoodwe/powerstation/monitordetails.go +++ b/cmd/gogoodwe/monitordata/monitordata.go @@ -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 import ( "bytes" @@ -29,13 +29,12 @@ import ( "net/http" "time" - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/interfaces" - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types" + "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/apilogin" "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils" ) // Generic function to retrieve data from the API via an ISemsDataConstraint Interface of defined structs -func getMonitorData[T interfaces.ISemsDataConstraint](LoginCredentials *types.LoginCredentials, LoginApiResponse *types.LoginResponse, InverterOutput *T) error { +func getMonitorData[T ISemsDataConstraint](LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse, InverterOutput *T) error { // get the Token header data apiResponseJsonData, err := dataTokenJSON(LoginApiResponse) @@ -88,8 +87,8 @@ func getMonitorData[T interfaces.ISemsDataConstraint](LoginCredentials *types.Lo } // Get Monitor Detailed data -func getMonitorDetailByPowerstationId(LoginCredentials *types.LoginCredentials, LoginApiResponse *types.LoginResponse) { - var powerstationData types.InverterData +func GetMonitorDetailByPowerstationId(LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse) { + var powerstationData InverterData err := getMonitorData(LoginCredentials, LoginApiResponse, &powerstationData) if err != nil { @@ -110,9 +109,9 @@ func getMonitorDetailByPowerstationId(LoginCredentials *types.LoginCredentials, } // Get Monitor summary data -func getMonitorSummaryByPowerstationId(LoginCredentials *types.LoginCredentials, LoginApiResponse *types.LoginResponse) { +func GetMonitorSummaryByPowerstationId(LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse) { - var powerstationData types.DailySummaryData + var powerstationData DailySummaryData err := getMonitorData(LoginCredentials, LoginApiResponse, &powerstationData) if err != nil { utils.HandleError(errors.New("error: fetching powerstation summary data")) diff --git a/cmd/gogoodwe/powerstation/output.go b/cmd/gogoodwe/monitordata/output.go similarity index 98% rename from cmd/gogoodwe/powerstation/output.go rename to cmd/gogoodwe/monitordata/output.go index d5f04f4..a5fac79 100644 --- a/cmd/gogoodwe/powerstation/output.go +++ b/cmd/gogoodwe/monitordata/output.go @@ -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 import ( "errors" diff --git a/cmd/gogoodwe/powerstation/utils.go b/cmd/gogoodwe/monitordata/utils.go similarity index 85% rename from cmd/gogoodwe/powerstation/utils.go rename to cmd/gogoodwe/monitordata/utils.go index c54fbb0..0a305b2 100644 --- a/cmd/gogoodwe/powerstation/utils.go +++ b/cmd/gogoodwe/monitordata/utils.go @@ -21,15 +21,14 @@ 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 import ( "encoding/json" "net/http" "strconv" - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/interfaces" - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types" + "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/apilogin" "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils" ) @@ -40,7 +39,7 @@ func setHeaders(r *http.Request, tokenstring []byte) { } // PowerStationIdJSON - Makes a map for the powerStationId to be passed to the Data API header and returns a JSON string -func powerStationIdJSON(UserLogin *types.LoginCredentials) ([]byte, error) { +func powerStationIdJSON(UserLogin *apilogin.ApiLoginCredentials) ([]byte, error) { powerStationMap := make(map[string]string) powerStationMap["powerStationId"] = UserLogin.PowerStationID @@ -49,7 +48,7 @@ func powerStationIdJSON(UserLogin *types.LoginCredentials) ([]byte, error) { return jsonStr, err } -func dataTokenJSON(SemsResponseData *types.LoginResponse) ([]byte, error) { +func dataTokenJSON(SemsResponseData *apilogin.ApiLoginResponse) ([]byte, error) { tokenMap := make(map[string]string) tokenMap["version"] = "v2.1.0" tokenMap["client"] = "ios" @@ -64,7 +63,7 @@ func dataTokenJSON(SemsResponseData *types.LoginResponse) ([]byte, error) { } // parse json data -func getDataJSON[T interfaces.ISemsDataConstraint](data T) ([]byte, error) { +func getDataJSON[T ISemsDataConstraint](data T) ([]byte, error) { // Get the response and return any errors resp, err := utils.MarshalStructToJSON(&data) diff --git a/cmd/gogoodwe/powerstation/fetchdata.go b/cmd/gogoodwe/powerstation/fetchdata.go deleted file mode 100644 index f6c57f3..0000000 --- a/cmd/gogoodwe/powerstation/fetchdata.go +++ /dev/null @@ -1,70 +0,0 @@ -/* -MIT License - -# 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 -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -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 - -import ( - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/semsapi" - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/types" - "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 -// -// Return type: -// -// error -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 -} From 4033cf9a22ba7568886aeacb44b8794219d95e8f Mon Sep 17 00:00:00 2001 From: aaronsaikovski Date: Fri, 26 Apr 2024 15:14:19 +1000 Subject: [PATCH 2/2] fixed error handling --- cmd/gogoodwe/app/app.go | 36 +------- cmd/gogoodwe/app/fetchdata.go | 67 +++++++++++++++ cmd/gogoodwe/monitordata/dailysummarydata.go | 4 +- .../{monitordata.go => getmonitordata.go} | 84 ++++++++++++++----- 4 files changed, 137 insertions(+), 54 deletions(-) create mode 100644 cmd/gogoodwe/app/fetchdata.go rename cmd/gogoodwe/monitordata/{monitordata.go => getmonitordata.go} (52%) diff --git a/cmd/gogoodwe/app/app.go b/cmd/gogoodwe/app/app.go index 2091023..ac9f418 100644 --- a/cmd/gogoodwe/app/app.go +++ b/cmd/gogoodwe/app/app.go @@ -25,13 +25,14 @@ package app // Main package - This is the main program entry point import ( - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/apilogin" - "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/monitordata" "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 @@ -49,35 +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) } - -// fetchData - Fetches data based on user account credentials and power station ID, and can retrieve daily summary if specified. -func fetchData(Account string, Password string, PowerStationID string, DailySummary bool) error { - - // User account struct - loginCreds := &apilogin.ApiLoginCredentials{ - Account: Account, - Password: Password, - PowerStationID: PowerStationID, - } - - // Do the login..check for errors - loginApiResponse, err := loginCreds.APILogin() - if err != nil { - utils.HandleError(err) - return err - } - - //fetch data based on - if DailySummary { - monitordata.GetMonitorSummaryByPowerstationId(loginCreds, loginApiResponse) - - } else { - monitordata.GetMonitorDetailByPowerstationId(loginCreds, loginApiResponse) - } - - return nil -} diff --git a/cmd/gogoodwe/app/fetchdata.go b/cmd/gogoodwe/app/fetchdata.go new file mode 100644 index 0000000..76ae8a5 --- /dev/null +++ b/cmd/gogoodwe/app/fetchdata.go @@ -0,0 +1,67 @@ +/* +MIT License + +# 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 +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +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 + +// Main package - This is the main program entry point +import ( + "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: 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. +// +// 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 + loginCreds := &apilogin.ApiLoginCredentials{ + Account: Account, + Password: Password, + PowerStationID: PowerStationID, + } + + // Do the login..check for errors + loginApiResponse, err := loginCreds.APILogin() + if err != nil { + utils.HandleError(err) + return err + } + + //fetch data and output + dataErr := monitordata.GetData(loginCreds, loginApiResponse, DailySummary) + if dataErr != nil { + utils.HandleError(dataErr) + return dataErr + } + + return nil +} diff --git a/cmd/gogoodwe/monitordata/dailysummarydata.go b/cmd/gogoodwe/monitordata/dailysummarydata.go index 847902d..79131e2 100644 --- a/cmd/gogoodwe/monitordata/dailysummarydata.go +++ b/cmd/gogoodwe/monitordata/dailysummarydata.go @@ -22,11 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/* -# Name: DailySummaryData - Struct to hold daily summary data -*/ package monitordata +// DailySummaryData - Struct to hold daily summary data type DailySummaryData struct { Language string `json:"language"` HasError bool `json:"hasError"` diff --git a/cmd/gogoodwe/monitordata/monitordata.go b/cmd/gogoodwe/monitordata/getmonitordata.go similarity index 52% rename from cmd/gogoodwe/monitordata/monitordata.go rename to cmd/gogoodwe/monitordata/getmonitordata.go index 2ef5952..1017f75 100644 --- a/cmd/gogoodwe/monitordata/monitordata.go +++ b/cmd/gogoodwe/monitordata/getmonitordata.go @@ -33,7 +33,12 @@ import ( "github.com/AaronSaikovski/gogoodwe/cmd/gogoodwe/utils" ) -// Generic function to retrieve data from the API via an ISemsDataConstraint Interface of defined structs +// getMonitorData retrieves data from the API using the provided login credentials and login API response. +// +// LoginCredentials: The login credentials for the API. +// LoginApiResponse: The login API response. +// InverterOutput: The output struct to store the retrieved data. +// error: An error if any occurred during the retrieval process. func getMonitorData[T ISemsDataConstraint](LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse, InverterOutput *T) error { // get the Token header data @@ -86,46 +91,87 @@ func getMonitorData[T ISemsDataConstraint](LoginCredentials *apilogin.ApiLoginCr } -// Get Monitor Detailed data -func GetMonitorDetailByPowerstationId(LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse) { - var powerstationData InverterData - +// getMonitorDataOutput retrieves monitor data for a given LoginCredentials and LoginApiResponse, +// and then processes the data to generate an output. It takes in three parameters: +// - LoginCredentials: a pointer to an ApiLoginCredentials struct representing the login credentials. +// - LoginApiResponse: a pointer to an ApiLoginResponse struct representing the API login response. +// - InverterOutput: a pointer to a generic type T that implements the ISemsDataConstraint interface. +// The function returns nothing. It first calls the getMonitorData function to retrieve the monitor data, +// then calls the getDataJSON function to convert the data to JSON format, and finally calls the parseOutput +// function to parse the JSON data and print the output. If any errors occur during the process, the +// utils.HandleError function is called to handle the error. +func getMonitorDataOutput[T ISemsDataConstraint](LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse, InverterOutput *T) error { + + var powerstationData T err := getMonitorData(LoginCredentials, LoginApiResponse, &powerstationData) if err != nil { - utils.HandleError(errors.New("error: fetching powerstation data")) + return err } dataOutput, err := getDataJSON(powerstationData) if err != nil { - utils.HandleError(errors.New("error: converting powerstation data")) + return err } output, err := parseOutput(dataOutput) if err != nil { utils.HandleError(err) + return err } printOutput(output) + return nil } -// Get Monitor summary data -func GetMonitorSummaryByPowerstationId(LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse) { +// GetMonitorDetailByPowerstationId retrieves the monitor details for a specific power station ID. +// +// LoginCredentials: The login credentials for the API. +// LoginApiResponse: The login API response. +// +// No return value. +func getMonitorDetailByPowerstationId(LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse) error { + var powerstationData InverterData + err := getMonitorDataOutput(LoginCredentials, LoginApiResponse, &powerstationData) - var powerstationData DailySummaryData - err := getMonitorData(LoginCredentials, LoginApiResponse, &powerstationData) if err != nil { - utils.HandleError(errors.New("error: fetching powerstation summary data")) + utils.HandleError(errors.New("error: fetching powerstation data")) + return err } - dataOutput, err := getDataJSON(powerstationData) - if err != nil { - utils.HandleError(errors.New("error: converting powerstation summary data")) - } + return nil +} + +// GetMonitorSummaryByPowerstationId retrieves the monitor summary data for a specific power station ID. +// +// LoginCredentials: The login credentials for the API. +// LoginApiResponse: The login API response. +// +// No return value. +func getMonitorSummaryByPowerstationId(LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse) error { + + var powerstationData DailySummaryData + err := getMonitorDataOutput(LoginCredentials, LoginApiResponse, &powerstationData) - output, err := parseOutput(dataOutput) if err != nil { - utils.HandleError(err) + utils.HandleError(errors.New("error: fetching powerstation summary data")) + return err } - printOutput(output) + return nil + +} + +// GetData retrieves data based on the provided login credentials and login API response. +// +// LoginCredentials: A pointer to an ApiLoginCredentials struct representing the login credentials. +// LoginApiResponse: A pointer to an ApiLoginResponse struct representing the login API response. +// isDailySummary: A boolean indicating whether to retrieve daily summary data. +func GetData(LoginCredentials *apilogin.ApiLoginCredentials, LoginApiResponse *apilogin.ApiLoginResponse, isDailySummary bool) error { + + if isDailySummary { + return getMonitorSummaryByPowerstationId(LoginCredentials, LoginApiResponse) + + } else { + return getMonitorDetailByPowerstationId(LoginCredentials, LoginApiResponse) + } }