This commit is contained in:
MickMake 2022-02-16 17:17:02 +11:00
parent a30e33270c
commit 2f6a60d548
14 changed files with 1460 additions and 211 deletions

63
.idea/workspace.xml generated
View File

@ -5,13 +5,19 @@
</component>
<component name="ChangeListManager">
<list default="true" id="7d634523-07a9-40e1-bec3-1dc036abc5e9" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/getPowerDevicePointNames/const.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/getPowerStatistics/struct.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/getPsDetailWithPsType/struct.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/login/funcs.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmd/cmd_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/cmd/cmd_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmd/struct.go" beforeDir="false" afterPath="$PROJECT_DIR$/cmd/struct.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/utils.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/utils.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/getPowerDevicePointNames/struct.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/getPowerDevicePointNames/struct.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/login/struct.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/login/struct.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/nullEndPoint/struct.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/nullEndPoint/struct.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/sungro/web/get.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/web/get.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/sungro/web/token.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/web/token.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -124,22 +130,22 @@
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/cmd/cmd_api.go</url>
<line>49</line>
<line>51</line>
<option name="timeStamp" value="10" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/cmd/cmd_api.go</url>
<line>25</line>
<line>27</line>
<option name="timeStamp" value="12" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/cmd/cmd_api.go</url>
<line>122</line>
<line>143</line>
<option name="timeStamp" value="13" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/cmd/cmd_api.go</url>
<line>97</line>
<line>118</line>
<option name="timeStamp" value="53" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
@ -167,54 +173,49 @@
<line>180</line>
<option name="timeStamp" value="65" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/cmd/cmd_api.go</url>
<line>92</line>
<option name="timeStamp" value="71" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/utils.go</url>
<line>59</line>
<line>60</line>
<option name="timeStamp" value="135" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>75</line>
<line>80</line>
<option name="timeStamp" value="159" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>71</line>
<line>76</line>
<option name="timeStamp" value="160" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>67</line>
<line>72</line>
<option name="timeStamp" value="161" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>63</line>
<line>68</line>
<option name="timeStamp" value="162" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>90</line>
<line>95</line>
<option name="timeStamp" value="163" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>94</line>
<line>99</line>
<option name="timeStamp" value="164" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>79</line>
<line>84</line>
<option name="timeStamp" value="165" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>99</line>
<line>104</line>
<option name="timeStamp" value="166" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
@ -224,7 +225,7 @@
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/cmd/cmd_api.go</url>
<line>100</line>
<line>121</line>
<option name="timeStamp" value="191" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
@ -234,12 +235,12 @@
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/utils.go</url>
<line>137</line>
<line>138</line>
<option name="timeStamp" value="201" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/sungro/AppService/login/struct.go</url>
<line>160</line>
<line>180</line>
<option name="timeStamp" value="215" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
@ -253,9 +254,19 @@
<option name="timeStamp" value="224" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/sungro/web/token.go</url>
<line>268</line>
<option name="timeStamp" value="225" />
<url>file://$PROJECT_DIR$/iSolarCloud/sungro/web/get.go</url>
<line>197</line>
<option name="timeStamp" value="226" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/sungro/web/get.go</url>
<line>142</line>
<option name="timeStamp" value="228" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/cmd/cmd_api.go</url>
<line>95</line>
<option name="timeStamp" value="230" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>

View File

@ -3,7 +3,7 @@
## What is it?
This GoLang package has a complete, (almost complete), implementation of the iSolarCloud API.
There's been no published specs on this, so I've had to figure it all out based on the Android app, using javascript IDEs and various other means.
There's been no published specs on this, so I've had to figure it all out based on the [Android app](https://play.google.com/store/apps/details?id=com.isolarcloud.manager), using javascript IDEs and various other means.
Note:
- [iSolarCloud](https://isolarcloud.com) has no interest in developing a public API.

View File

@ -2,6 +2,8 @@ package cmd
import (
"GoSungro/Only"
"GoSungro/iSolarCloud/api"
"GoSungro/iSolarCloud/sungro/AppService/getPowerDevicePointNames"
"GoSungro/iSolarCloud/sungro/AppService/login"
"fmt"
"github.com/spf13/cobra"
@ -90,11 +92,30 @@ func cmdApiGetFunc(cmd *cobra.Command, args []string) {
// foo := AppService.Init()
// bar := foo.GetEndPoints()
// fmt.Printf("%v\n", bar)
hey := SunGro.GetEndpoint(args[1], args[0])
hey.SetRequest(login.Request {
hey1 := SunGro.GetEndpoint(args[1], args[0])
_ = hey1.SetRequest(login.Request{
Appkey: Cmd.ApiAppKey,
SysCode: "600",
UserAccount: "",
UserPassword: "",
})
_ = SunGro.Web.Get(login.GetUrl(), login.RequestRef(), login.ResponseRef())
hey := SunGro.GetEndpoint(args[1], args[0])
_ = hey.SetRequest(getPowerDevicePointNames.Request{
RequestCommon: api.RequestCommon {
Appkey: Cmd.ApiAppKey,
Lang: "_en_US",
SysCode: "200",
Token: SunGro.GetToken(),
UserID: "",
ValidFlag: "",
},
DeviceType: "",
})
SunGro.Web.Get(hey.GetUrl(), hey.RequestRef(), hey.ResponseRef())
fmt.Printf("HEY:%v\n", hey)
fmt.Printf("HEY:%v\n", hey.GetError())
fmt.Printf("HEY:%s\n", hey.GetRequest())

View File

@ -3,6 +3,7 @@ package cmd
import (
"GoSungro/Only"
"GoSungro/iSolarCloud/sungro"
"GoSungro/iSolarCloud/sungro/AppService/login"
"GoSungro/iSolarCloud/sungro/web"
"GoSungro/lsgo"
"GoSungro/mmGit"
@ -122,10 +123,18 @@ func (ca *CommandArgs) ProcessArgs(cmd *cobra.Command, args []string) error {
Username: ca.ApiUsername,
Password: ca.ApiPassword,
}
ca.Error = SunGro.SetAuth(auth)
if ca.Error != nil {
break
}
// ca.Error = SunGro.SetAuth(auth)
// if ca.Error != nil {
// break
// }
hey1 := SunGro.GetEndpoint(args[1], args[0])
_ = hey1.SetRequest(login.Request{
Appkey: Cmd.ApiAppKey,
SysCode: "600",
UserAccount: "",
UserPassword: "",
})
_ = SunGro.Web.Get(login.GetUrl(), login.RequestRef(), login.ResponseRef())
if SunGro.HasTokenChanged() {
ca.ApiTokenExpiry = SunGro.GetTokenExpiry()

View File

@ -12,15 +12,20 @@ type EndPoint interface {
GetName() EndPointName
GetUrl() *url.URL
Call() Json
SetRequest(ref interface{}) error
GetRequest() Json
GetResponse() Json
GetData() Json
IsValid() error
SetRequest(ref interface{}) error
RequestRef() interface{}
GetRequestJson() Json
IsRequestValid() error
ResponseRef() interface{}
GetResponseJson() Json
IsResponseValid() error
GetError() error
}
type EndPointStruct struct {
Area AreaName `json:"area"`
Name EndPointName `json:"name"`

View File

@ -3,6 +3,7 @@ package api
import (
"GoSungro/Only"
"encoding/json"
"errors"
"fmt"
"net/url"
"reflect"
@ -213,7 +214,6 @@ func GetUrl(u string) *url.URL {
return ret
}
func GetAsJson(r interface{}) Json {
var ret Json
for range Only.Once {
@ -227,6 +227,148 @@ func GetAsJson(r interface{}) Json {
}
// Response checks
func CheckResultCode(rc string) error {
var err error
for range Only.Once {
switch rc {
case "1":
err = nil
case "-1":
err = errors.New(fmt.Sprintf("error '%s'", rc))
case "010":
err = errors.New(fmt.Sprintf("error '%s'", rc))
case "000":
err = errors.New(fmt.Sprintf("error '%s'", rc))
case "201":
err = errors.New(fmt.Sprintf("error '%s'", rc))
case "E00003":
err = errors.New(fmt.Sprintf("need to login again '%s'", rc))
default:
err = errors.New(fmt.Sprintf("unknown error '%s'", rc))
}
}
return err
}
func CheckResultMessage(rc string) error {
var err error
for range Only.Once {
switch {
case rc == "success":
err = nil
case rc == "er_token_login_invalid":
err = errors.New(fmt.Sprintf("need to login again '%s'", rc))
case rc == "er_parameter_value_invalid":
err = errors.New(fmt.Sprintf("incorrect request data '%s'", rc))
case rc == "er_unknown_exception":
err = errors.New(fmt.Sprintf("API error '%s'", rc))
case strings.HasPrefix(rc, "Parameter:"):
err = errors.New(fmt.Sprintf("incorrect request data '%s'", rc))
// case err == nil:
// err = errors.New(fmt.Sprintf("unknown '%s'", rc))
default:
err = errors.New(fmt.Sprintf("unknown error '%s'", rc))
}
}
return err
}
// Request checks
func CheckString(name string, rc string) error {
var err error
for range Only.Once {
if rc == "" {
err = errors.New(name + ": empty string")
break
}
if strings.TrimSpace(rc) == "" {
err = errors.New(name + ": empty string with spaces")
break
}
}
return err
}
func (req RequestCommon) IsValid() error {
var err error
for range Only.Once {
err = CheckString("Appkey", req.Appkey)
if err != nil {
break
}
err = CheckString("Lang", req.Lang)
if err != nil {
break
}
err = CheckString("SysCode", req.SysCode)
if err != nil {
break
}
err = CheckString("Token", req.Token)
if err != nil {
break
}
err = CheckString("UserID", req.UserID)
if err != nil {
break
}
err = CheckString("ValidFlag", req.ValidFlag)
if err != nil {
break
}
}
return err
}
func (req ResponseCommon) IsValid() error {
var err error
for range Only.Once {
err = CheckResultMessage(req.ResultMsg)
if err != nil {
break
}
err = CheckResultCode(req.ResultCode)
if err != nil {
break
}
err = CheckString("ReqSerialNum", req.ReqSerialNum)
if err != nil {
break
}
if req.ResultData == nil {
err = errors.New("zero results")
break
}
}
return err
}
// func CheckEmpty(rc string) error {
// var err error
// for range Only.Once {
// // switch rc {
// // case "1":
// // err = nil
// // case "-1":
// // err = errors.New(fmt.Sprintf("error '%s'", rc))
// // case "010":
// // err = errors.New(fmt.Sprintf("error '%s'", rc))
// // case "000":
// // err = errors.New(fmt.Sprintf("error '%s'", rc))
// // case "201":
// // err = errors.New(fmt.Sprintf("error '%s'", rc))
// // case "E00003":
// // err = errors.New(fmt.Sprintf("need to login again '%s'", rc))
// // default:
// // err = errors.New(fmt.Sprintf("unknown error '%s'", rc))
// // }
// }
// return err
// }
// func MakeNewFrom(i interface{}) (new interface{}) {
// for range OnlyOnce {
// t := reflect.TypeOf(i)

View File

@ -1,4 +1,3 @@
// EndPoint
package getPowerDevicePointNames
import (
@ -31,9 +30,7 @@ type ResultData []struct {
}
func Init() EndPoint {
fmt.Println("Init()")
foo := EndPoint {
return EndPoint {
Area: api.GetArea(EndPoint{}),
Name: api.GetName(EndPoint{}),
Url: api.GetUrl(Url),
@ -41,62 +38,74 @@ func Init() EndPoint {
Response: Response{},
Error: nil,
}
fmt.Printf("endpoint: %v\n", foo)
return foo
}
func (g EndPoint) GetArea() api.AreaName {
fmt.Println("g.GetArea()")
return g.Area
func (e EndPoint) GetArea() api.AreaName {
return e.Area
}
func (g EndPoint) GetName() api.EndPointName {
fmt.Println("g.GetName()")
return g.Name
func (e EndPoint) GetName() api.EndPointName {
return e.Name
}
func (g EndPoint) GetUrl() *url.URL {
fmt.Println("g.GetUrl()")
return g.Url
func (e EndPoint) GetUrl() *url.URL {
return e.Url
}
func (g EndPoint) SetRequest(ref interface{}) error {
fmt.Println("g.SetRequest()")
fmt.Printf("ref == %v\n", ref)
return nil
func (e EndPoint) GetData() api.Json {
return api.GetAsJson(e.Response.(Response).ResultData)
}
func (g EndPoint) GetRequest() api.Json {
return api.GetAsJson(g.Request)
}
func (g EndPoint) GetResponse() api.Json {
return api.GetAsJson(g.Response)
}
func (g EndPoint) GetData() api.Json {
return api.GetAsJson(g.Response.(Response).ResultData)
}
func (g EndPoint) IsValid() error {
fmt.Println("g.IsValid() implement me")
return nil
}
func (g EndPoint) Call() api.Json {
fmt.Println("g.Call() implement me")
func (e EndPoint) Call() api.Json {
fmt.Println("e.Call() implement me")
return ""
}
func (g EndPoint) Init() *EndPoint {
fmt.Println("g.Init()")
func (e EndPoint) GetError() error {
return e.Error
}
func (e EndPoint) Init() *EndPoint {
ret := Init()
return &ret
}
func (g EndPoint) GetError() error {
return g.Error
func (e EndPoint) SetRequest(ref interface{}) error {
e.Request = ref.(Request)
return nil
}
func (e EndPoint) RequestRef() interface{} {
return e.Request
}
func (e EndPoint) GetRequestJson() api.Json {
return api.GetAsJson(e.Request)
}
func (e EndPoint) IsRequestValid() error {
return e.GetRequest().RequestCommon.IsValid()
}
func (e EndPoint) GetRequest() Request {
return e.Request.(Request)
}
func (e EndPoint) GetResponseJson() api.Json {
return api.GetAsJson(e.Response)
}
func (e EndPoint) ResponseRef() interface{} {
return e.Response
}
func (e EndPoint) IsResponseValid() error {
return e.GetResponse().ResponseCommon.IsValid()
}
func (e EndPoint) GetResponse() Response {
return e.Response.(Response)
}

View File

@ -0,0 +1,114 @@
package getPowerStatistics
import (
"GoSungro/iSolarCloud/api"
"fmt"
"net/url"
)
var Url = "/v1/powerStationService/getPowerStatistics"
var _ api.EndPoint = (*EndPoint)(nil)
type EndPoint api.EndPointStruct
type Request struct {
api.RequestCommon
PsId string `json:"ps_id"`
}
type Response struct {
api.ResponseCommon
ResultData struct {
PRVlaue string `json:"PRVlaue"`
City interface{} `json:"city"`
DayPower struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"dayPower"`
DesignCapacity struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"design_capacity"`
EqVlaue string `json:"eqVlaue"`
NowCapacity struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"nowCapacity"`
PsName string `json:"ps_name"`
PsShortName string `json:"ps_short_name"`
Status1 string `json:"status1"`
Status2 string `json:"status2"`
Status3 string `json:"status3"`
} `json:"result_data"`
}
func Init() EndPoint {
return EndPoint {
Area: api.GetArea(EndPoint{}),
Name: api.GetName(EndPoint{}),
Url: api.GetUrl(Url),
Request: Request{},
Response: Response{},
Error: nil,
}
}
func (g EndPoint) GetArea() api.AreaName {
return g.Area
}
func (g EndPoint) GetName() api.EndPointName {
return g.Name
}
func (g EndPoint) GetUrl() *url.URL {
return g.Url
}
func (g EndPoint) SetRequest(ref interface{}) error {
g.Request = ref.(Request)
return nil
}
func (g EndPoint) GetRequest() api.Json {
return api.GetAsJson(g.Request)
}
func (g EndPoint) GetResponse() api.Json {
return api.GetAsJson(g.Response)
}
func (g EndPoint) RequestRef() interface{} {
return g.Request
}
func (g EndPoint) ResponseRef() interface{} {
return g.Response
}
func (g EndPoint) GetData() api.Json {
return api.GetAsJson(g.Response.(Response).ResultData)
}
func (g EndPoint) IsValid() error {
fmt.Println("g.IsValid() implement me")
return nil
}
func (g EndPoint) Call() api.Json {
fmt.Println("g.Call() implement me")
return ""
}
func (g EndPoint) Init() *EndPoint {
ret := Init()
return &ret
}
func (g EndPoint) GetError() error {
return g.Error
}

View File

@ -0,0 +1,367 @@
package getPsDetailWithPsType
import (
"GoSungro/iSolarCloud/api"
"fmt"
"net/url"
)
var Url = "/v1/powerStationService/getPsDetailWithPsType"
var _ api.EndPoint = (*EndPoint)(nil)
type EndPoint api.EndPointStruct
type Request struct {
api.RequestCommon
PsId string `json:"ps_id"`
}
type Response struct {
api.ResponseCommon
ResultData struct {
BatteryLevelPercent string `json:"battery_level_percent"`
ChargingDischargingPowerMap struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"charging_discharging_power_map"`
Co2ReduceTotal struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"co2_reduce_total"`
CoalReduceTotal struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"coal_reduce_total"`
ConnectType string `json:"connect_type"`
CurrPower struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"curr_power"`
DesignCapacity struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"design_capacity"`
EnergyScheme interface{} `json:"energy_scheme"`
GcjLatitude string `json:"gcj_latitude"`
GcjLongitude string `json:"gcj_longitude"`
HasAmmeter int64 `json:"has_ammeter"`
HouseholdInverterData interface{} `json:"household_inverter_data"`
InstallerPsFaultStatus string `json:"installer_ps_fault_status"`
IsHaveEsInverter int64 `json:"is_have_es_inverter"`
IsSingleInverter int64 `json:"is_single_inverter"`
IsTransformSystem string `json:"is_transform_system"`
Latitude float64 `json:"latitude"`
LoadPowerMap struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"load_power_map"`
LoadPowerMapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"load_power_map_virgin"`
Longitude float64 `json:"longitude"`
MapLatitude string `json:"map_latitude"`
MapLongitude string `json:"map_longitude"`
MeterReduceTotal struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"meter_reduce_total"`
MobleTel string `json:"moble_tel"`
MonthEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"month_energy"`
MonthEnergyVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"month_energy_virgin"`
MonthIncome struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"month_income"`
NegativeLoadMsg interface{} `json:"negative_load_msg"`
OwnerPsFaultStatus string `json:"owner_ps_fault_status"`
P83081Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83081_map"`
P83081MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83081_map_virgin"`
P83102Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83102_map"`
P83102MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83102_map_virgin"`
P83102Percent string `json:"p83102_percent"`
P83118Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83118_map"`
P83118MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83118_map_virgin"`
P83119Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83119_map"`
P83119MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83119_map_virgin"`
P83120Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83120_map"`
P83120MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83120_map_virgin"`
P83122 string `json:"p83122"`
P83124Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83124_map"`
P83124MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83124_map_virgin"`
P83202Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83202_map"`
P83202MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83202_map_virgin"`
P83532MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p83532_map_virgin"`
PowerChargeSetted int64 `json:"power_charge_setted"`
PowerGridPowerMap struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"power_grid_power_map"`
PowerGridPowerMapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"power_grid_power_map_virgin"`
PsCountryID int64 `json:"ps_country_id"`
PsDeviceType string `json:"ps_device_type"`
PsFaultStatus string `json:"ps_fault_status"`
PsHealthStatus string `json:"ps_health_status"`
PsLocation string `json:"ps_location"`
PsName string `json:"ps_name"`
PsPsKey string `json:"ps_ps_key"`
PsState string `json:"ps_state"`
PsType int64 `json:"ps_type"`
PvPowerMap struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"pv_power_map"`
PvPowerMapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"pv_power_map_virgin"`
RobotNumSweepCapacity struct {
Num int64 `json:"num"`
SweepCapacity int64 `json:"sweep_capacity"`
} `json:"robot_num_sweep_capacity"`
SelfConsumptionOffsetReminder int64 `json:"self_consumption_offset_reminder"`
So2ReduceTotal struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"so2_reduce_total"`
StorageInverterData []struct {
CommunicationDevSn string `json:"communication_dev_sn"`
DevStatus int64 `json:"dev_status"`
DeviceCode int64 `json:"device_code"`
DeviceModelCode string `json:"device_model_code"`
DeviceName string `json:"device_name"`
DeviceState string `json:"device_state"`
DeviceType int64 `json:"device_type"`
DrmStatus string `json:"drm_status"`
DrmStatusName string `json:"drm_status_name"`
EnergyFlow []string `json:"energy_flow"`
HasAmmeter int64 `json:"has_ammeter"`
InstallerDevFaultStatus int64 `json:"installer_dev_fault_status"`
InverterSn string `json:"inverter_sn"`
OwnerDevFaultStatus int64 `json:"owner_dev_fault_status"`
P13003Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13003_map"`
P13003MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13003_map_virgin"`
P13119Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13119_map"`
P13119MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13119_map_virgin"`
P13121Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13121_map"`
P13121MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13121_map_virgin"`
P13126Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13126_map"`
P13126MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13126_map_virgin"`
P13141 string `json:"p13141"`
P13149Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13149_map"`
P13149MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13149_map_virgin"`
P13150Map struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13150_map"`
P13150MapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"p13150_map_virgin"`
PsKey string `json:"ps_key"`
UUID int64 `json:"uuid"`
} `json:"storage_inverter_data"`
TodayEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"today_energy"`
TodayEnergyVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"today_energy_virgin"`
TodayIncome struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"today_income"`
TotalEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"total_energy"`
TotalEnergyVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"total_energy_virgin"`
TotalIncome struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"total_income"`
TreeReduceTotal struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"tree_reduce_total"`
ValidFlag int64 `json:"valid_flag"`
WgsLatitude float64 `json:"wgs_latitude"`
WgsLongitude float64 `json:"wgs_longitude"`
ZfzyMap struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"zfzy_map"`
ZfzyMapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"zfzy_map_virgin"`
ZjzzMap struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"zjzz_map"`
ZjzzMapVirgin struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"zjzz_map_virgin"`
} `json:"result_data"`
}
func Init() EndPoint {
return EndPoint {
Area: api.GetArea(EndPoint{}),
Name: api.GetName(EndPoint{}),
Url: api.GetUrl(Url),
Request: Request{},
Response: Response{},
Error: nil,
}
}
func (g EndPoint) GetArea() api.AreaName {
return g.Area
}
func (g EndPoint) GetName() api.EndPointName {
return g.Name
}
func (g EndPoint) GetUrl() *url.URL {
return g.Url
}
func (g EndPoint) SetRequest(ref interface{}) error {
g.Request = ref.(Request)
return nil
}
func (g EndPoint) GetRequest() api.Json {
return api.GetAsJson(g.Request)
}
func (g EndPoint) GetResponse() api.Json {
return api.GetAsJson(g.Response)
}
func (g EndPoint) RequestRef() interface{} {
return g.Request
}
func (g EndPoint) ResponseRef() interface{} {
return g.Response
}
func (g EndPoint) GetData() api.Json {
return api.GetAsJson(g.Response.(Response).ResultData)
}
func (g EndPoint) IsValid() error {
fmt.Println("g.IsValid() implement me")
return nil
}
func (g EndPoint) Call() api.Json {
fmt.Println("g.Call() implement me")
return ""
}
func (g EndPoint) Init() *EndPoint {
ret := Init()
return &ret
}
func (g EndPoint) GetError() error {
return g.Error
}

View File

@ -0,0 +1,279 @@
package getPsList
import (
"GoSungro/iSolarCloud/api"
"fmt"
"net/url"
)
var Url = "/v1/powerStationService/getPsList"
var _ api.EndPoint = (*EndPoint)(nil)
type EndPoint api.EndPointStruct
type Request struct {
api.RequestCommon
}
type Response struct {
api.ResponseCommon
ResultData struct {
PageList []struct {
AlarmCount int64 `json:"alarm_count"`
AlarmDevCount int64 `json:"alarm_dev_count"`
AreaID interface{} `json:"area_id"`
AreaType interface{} `json:"area_type"`
ArrearsStatus int64 `json:"arrears_status"`
BuildDate string `json:"build_date"`
BuildStatus int64 `json:"build_status"`
Co2Reduce struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"co2_reduce"`
Co2ReduceTotal struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"co2_reduce_total"`
CurrPower struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"curr_power"`
DailyIrradiation struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"daily_irradiation"`
DailyIrradiationVirgin interface{} `json:"daily_irradiation_virgin"`
DesignCapacity string `json:"design_capacity"`
DesignCapacityUnit string `json:"design_capacity_unit"`
DesignCapacityVirgin int64 `json:"design_capacity_virgin"`
EquivalentHour struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"equivalent_hour"`
EsDisenergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"es_disenergy"`
EsEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"es_energy"`
EsPower struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"es_power"`
EsTotalDisenergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"es_total_disenergy"`
EsTotalEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"es_total_energy"`
ExpectInstallDate string `json:"expect_install_date"`
FaultAlarmOfflineDevCount int64 `json:"fault_alarm_offline_dev_count"`
FaultCount int64 `json:"fault_count"`
FaultDevCount int64 `json:"fault_dev_count"`
GcjLatitude string `json:"gcj_latitude"`
GcjLongitude string `json:"gcj_longitude"`
GprsLatitude interface{} `json:"gprs_latitude"`
GprsLongitude interface{} `json:"gprs_longitude"`
Images []struct {
FileID int64 `json:"file_id"`
ID int64 `json:"id"`
PicLanguage int64 `json:"pic_language"`
PicType int64 `json:"pic_type"`
PictureName string `json:"picture_name"`
PictureURL string `json:"picture_url"`
PsID int64 `json:"ps_id"`
PsUnitUUID interface{} `json:"ps_unit_uuid"`
} `json:"images"`
InstallDate string `json:"install_date"`
InstalledPowerMap struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"installed_power_map"`
InstalledPowerVirgin int64 `json:"installed_power_virgin"`
InstallerAlarmCount int64 `json:"installer_alarm_count"`
InstallerFaultCount int64 `json:"installer_fault_count"`
InstallerPsFaultStatus int64 `json:"installer_ps_fault_status"`
IsBankPs int64 `json:"is_bank_ps"`
IsTuv int64 `json:"is_tuv"`
JoinYearInitElec int64 `json:"join_year_init_elec"`
Latitude float64 `json:"latitude"`
Location string `json:"location"`
Longitude float64 `json:"longitude"`
MapLatitude string `json:"map_latitude"`
MapLongitude string `json:"map_longitude"`
MlpeFlag int64 `json:"mlpe_flag"`
Nmi interface{} `json:"nmi"`
OfflineDevCount int64 `json:"offline_dev_count"`
OperateYear interface{} `json:"operate_year"`
OperationBusName interface{} `json:"operation_bus_name"`
OwnerAlarmCount int64 `json:"owner_alarm_count"`
OwnerFaultCount int64 `json:"owner_fault_count"`
OwnerPsFaultStatus int64 `json:"owner_ps_fault_status"`
P83022y string `json:"p83022y"`
P83046 interface{} `json:"p83046"`
P83048 interface{} `json:"p83048"`
P83049 interface{} `json:"p83049"`
P83050 interface{} `json:"p83050"`
P83051 interface{} `json:"p83051"`
P83054 interface{} `json:"p83054"`
P83055 interface{} `json:"p83055"`
P83067 interface{} `json:"p83067"`
P83070 interface{} `json:"p83070"`
P83076 int64 `json:"p83076"`
P83077 int64 `json:"p83077"`
P83081 int64 `json:"p83081"`
P83089 int64 `json:"p83089"`
P83095 int64 `json:"p83095"`
P83118 int64 `json:"p83118"`
P83120 int64 `json:"p83120"`
P83127 int64 `json:"p83127"`
ParamCo2 float64 `json:"param_co2"`
ParamCoal float64 `json:"param_coal"`
ParamIncome int64 `json:"param_income"`
ParamMeter float64 `json:"param_meter"`
ParamNox float64 `json:"param_nox"`
ParamPowder float64 `json:"param_powder"`
ParamSo2 float64 `json:"param_so2"`
ParamTree int64 `json:"param_tree"`
ParamWater float64 `json:"param_water"`
PrScale string `json:"pr_scale"`
Producer interface{} `json:"producer"`
PsCountryID int64 `json:"ps_country_id"`
PsFaultStatus int64 `json:"ps_fault_status"`
PsHealthStatus string `json:"ps_health_status"`
PsHolder string `json:"ps_holder"`
PsID int64 `json:"ps_id"`
PsIsNotInit string `json:"ps_is_not_init"`
PsName string `json:"ps_name"`
PsShortName string `json:"ps_short_name"`
PsStatus int64 `json:"ps_status"`
PsTimezone string `json:"ps_timezone"`
PsType int64 `json:"ps_type"`
PvEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"pv_energy"`
PvPower struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"pv_power"`
Radiation struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"radiation"`
RadiationVirgin interface{} `json:"radiation_virgin"`
RecoreCreateTime string `json:"recore_create_time"`
SafeStartDate string `json:"safe_start_date"`
ShareType string `json:"share_type"`
ShippingAddress string `json:"shipping_address"`
ShippingZipCode string `json:"shipping_zip_code"`
TodayEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"today_energy"`
TodayIncome struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"today_income"`
TotalCapcity struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"total_capcity"`
TotalEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"total_energy"`
TotalIncome struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"total_income"`
TotalInitCo2Accelerate int64 `json:"total_init_co2_accelerate"`
TotalInitElec int64 `json:"total_init_elec"`
TotalInitProfit int64 `json:"total_init_profit"`
UseEnergy struct {
Unit string `json:"unit"`
Value string `json:"value"`
} `json:"use_energy"`
ValidFlag int64 `json:"valid_flag"`
WgsLatitude float64 `json:"wgs_latitude"`
WgsLongitude float64 `json:"wgs_longitude"`
ZipCode string `json:"zip_code"`
} `json:"pageList"`
RowCount int64 `json:"rowCount"`
} `json:"result_data"`
}
func Init() EndPoint {
return EndPoint {
Area: api.GetArea(EndPoint{}),
Name: api.GetName(EndPoint{}),
Url: api.GetUrl(Url),
Request: Request{},
Response: Response{},
Error: nil,
}
}
func (g EndPoint) GetArea() api.AreaName {
return g.Area
}
func (g EndPoint) GetName() api.EndPointName {
return g.Name
}
func (g EndPoint) GetUrl() *url.URL {
return g.Url
}
func (g EndPoint) SetRequest(ref interface{}) error {
g.Request = ref.(Request)
return nil
}
func (g EndPoint) GetRequest() api.Json {
return api.GetAsJson(g.Request)
}
func (g EndPoint) GetResponse() api.Json {
return api.GetAsJson(g.Response)
}
func (g EndPoint) RequestRef() interface{} {
return g.Request
}
func (g EndPoint) ResponseRef() interface{} {
return g.Response
}
func (g EndPoint) GetData() api.Json {
return api.GetAsJson(g.Response.(Response).ResultData)
}
func (g EndPoint) IsValid() error {
fmt.Println("g.IsValid() implement me")
return nil
}
func (g EndPoint) Call() api.Json {
fmt.Println("g.Call() implement me")
return ""
}
func (g EndPoint) Init() *EndPoint {
ret := Init()
return &ret
}
func (g EndPoint) GetError() error {
return g.Error
}

View File

@ -0,0 +1,237 @@
package login
import (
"GoSungro/Only"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
)
type SunGroAuth struct {
TokenExpiry string
AppKey string
Username string
Password string
}
type Token struct {
EndPoint
TokenFile string
TokenExpiry time.Time
newToken bool
retry int
}
func (e *EndPoint) Login(auth *SunGroAuth) error {
for range Only.Once {
_ = e.readTokenFile()
e.Error = e.Verify(auth)
if e.Error != nil {
break
}
e.Error = e.RetrieveToken()
if e.Error != nil {
break
}
}
return e.Error
}
func (e *EndPoint) GetToken() string {
return e.GetResponse().ResultData.Token
}
func (e *EndPoint) Verify(auth *SunGroAuth) error {
for range Only.Once {
if auth == nil {
// If nil, then assume we haven'e set anything.
break
}
if auth.AppKey == "" {
e.Error = errors.New("API AppKey")
break
}
if auth.Username == "" {
e.Error = errors.New("empty Client ApiUsername")
break
}
if auth.Password == "" {
e.Error = errors.New("empty Client ApiPassword")
break
}
e.Token = e.Response.(Response).ResultData.Token
if e.Response.ResultData.Token == "" {
e.newToken = true
}
if auth.TokenExpiry == "" {
auth.TokenExpiry = time.Now().Format(DateTimeFormat)
}
e.TokenExpiry, e.Error = time.Parse(DateTimeFormat, auth.TokenExpiry)
if e.Error != nil {
e.newToken = true
}
e.Request = login.Request {
Appkey: auth.AppKey,
SysCode: "900",
UserAccount: auth.Username,
UserPassword: auth.Password,
}
e.HasTokenExpired()
}
return e.Error
}
func (e *EndPoint) RetrieveToken() error {
for range Only.Once {
e.HasTokenExpired()
if !e.newToken {
break
}
u := fmt.Sprintf("%s%s",
e.Url.String(),
TokenRequestUrl,
)
//p, _ := json.Marshal(map[string]string {
// "user_account": e.Request.Username,
// "user_password": e.Request.Password,
// "appkey": e.Request.AppKey,
// "sys_code": "900",
//})
p, _ := json.Marshal(e.Request)
var response *http.Response
response, e.Error = http.Post(u, "application/json", bytes.NewBuffer(p))
if e.Error != nil {
break
}
//goland:noinspection GoUnhandledErrorResult
defer response.Body.Close()
if response.StatusCode != 200 {
e.Error = errors.New(fmt.Sprintf("Status Code is %d", response.StatusCode))
break
}
var body []byte
body, e.Error = ioutil.ReadAll(response.Body)
if e.Error != nil {
break
}
e.Error = json.Unmarshal(body, &e.Response)
if e.Error != nil {
break
}
e.TokenExpiry = time.Now()
e.Error = e.saveToken()
if e.Error != nil {
break
}
}
return e.Error
}
func (e *EndPoint) HasTokenExpired() bool {
for range Only.Once {
if e.TokenExpiry.Before(time.Now()) {
e.newToken = true
break
}
if e.Response.ResultData.Token == "" {
e.newToken = true
break
}
}
return e.newToken
}
func (e *EndPoint) HasTokenChanged() bool {
ok := e.newToken
if e.newToken {
e.newToken = false
}
return ok
}
func (e *EndPoint) GetTokenExpiry() time.Time {
return e.TokenExpiry
}
// Retrieves a token from a local file.
func (e *EndPoint) readTokenFile() error {
for range Only.Once {
if e.TokenFile == "" {
e.TokenFile, e.Error = os.UserHomeDir()
if e.Error != nil {
e.TokenFile = ""
break
}
e.TokenFile = filepath.Join(e.TokenFile, ".GoSungro", DefaultAuthTokenFile)
}
var f *os.File
f, e.Error = os.Open(e.TokenFile)
if e.Error != nil {
break
}
//goland:noinspection GoUnhandledErrorResult
defer f.Close()
//ret = &oauth2.token{}
e.Error = json.NewDecoder(f).Decode(&e.Response)
}
return e.Error
}
// Saves a token to a file path.
func (e *EndPoint) saveToken() error {
for range Only.Once {
if e.TokenFile == "" {
e.TokenFile, e.Error = os.UserHomeDir()
if e.Error != nil {
e.TokenFile = ""
break
}
e.TokenFile = filepath.Join(e.TokenFile, ".GoSungro", DefaultAuthTokenFile)
}
fmt.Printf("Saving token file to: %s\n", e.TokenFile)
var f *os.File
f, e.Error = os.OpenFile(e.TokenFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if e.Error != nil {
e.Error = errors.New(fmt.Sprintf("Unable to cache SUNGRO oauth token: %v", e.Error))
break
}
//goland:noinspection GoUnhandledErrorResult
defer f.Close()
e.Error = json.NewEncoder(f).Encode(e.Response.ResultData)
}
return e.Error
}

View File

@ -2,6 +2,7 @@
package login
import (
"GoSungro/Only"
"GoSungro/iSolarCloud/api"
"fmt"
"net/url"
@ -144,50 +145,97 @@ func Init() EndPoint {
}
func (g EndPoint) GetArea() api.AreaName {
return g.Area
func (e EndPoint) GetArea() api.AreaName {
return e.Area
}
func (g EndPoint) GetName() api.EndPointName {
return g.Name
func (e EndPoint) GetName() api.EndPointName {
return e.Name
}
func (g EndPoint) GetUrl() *url.URL {
return g.Url
func (e EndPoint) GetUrl() *url.URL {
return e.Url
}
func (g EndPoint) SetRequest(ref interface{}) error {
g.Request = ref.(Request)
return nil
func (e EndPoint) GetData() api.Json {
return api.GetAsJson(e.Response.(Response).ResultData)
}
func (g EndPoint) GetRequest() api.Json {
return api.GetAsJson(g.Request)
}
func (g EndPoint) GetResponse() api.Json {
return api.GetAsJson(g.Response)
}
func (g EndPoint) GetData() api.Json {
return api.GetAsJson(g.Response.(Response).ResultData)
}
func (g EndPoint) IsValid() error {
fmt.Println("g.IsValid() implement me")
return nil
}
func (g EndPoint) Call() api.Json {
fmt.Println("g.Call() implement me")
func (e EndPoint) Call() api.Json {
fmt.Println("e.Call() implement me")
return ""
}
func (g EndPoint) Init() *EndPoint {
func (e EndPoint) GetError() error {
return e.Error
}
func (e EndPoint) Init() *EndPoint {
ret := Init()
return &ret
}
func (g EndPoint) GetError() error {
return g.Error
func (e EndPoint) SetRequest(ref interface{}) error {
e.Request = ref.(Request)
return nil
}
func (e EndPoint) RequestRef() interface{} {
return e.Request
}
func (e EndPoint) GetRequestJson() api.Json {
return api.GetAsJson(e.Request)
}
func (e EndPoint) IsRequestValid() error {
for range Only.Once {
req := e.GetRequest()
e.Error = api.CheckString("SysCode", req.SysCode)
if e.Error != nil {
break
}
e.Error = api.CheckString("Appkey", req.Appkey)
if e.Error != nil {
break
}
e.Error = api.CheckString("UserAccount", req.UserAccount)
if e.Error != nil {
break
}
e.Error = api.CheckString("UserPassword", req.UserPassword)
if e.Error != nil {
break
}
}
return e.Error
}
func (e EndPoint) GetRequest() Request {
return e.Request.(Request)
}
func (e EndPoint) GetResponseJson() api.Json {
return api.GetAsJson(e.Response)
}
func (e EndPoint) ResponseRef() interface{} {
return e.Response
}
func (e EndPoint) IsResponseValid() error {
for range Only.Once {
resp := e.GetResponse()
e.Error = resp.ResponseCommon.IsValid()
if e.Error != nil {
break
}
}
return e.Error
}
func (e EndPoint) GetResponse() Response {
return e.Response.(Response)
}

View File

@ -69,6 +69,14 @@ func (g EndPoint) GetResponse() api.Json {
return api.GetAsJson(g.Response)
}
func (g EndPoint) RequestRef() interface{} {
return g.Request
}
func (g EndPoint) ResponseRef() interface{} {
return g.Response
}
func (g EndPoint) GetData() api.Json {
fmt.Println("g.GetData() implement me")
return api.GetAsJson(g.Response.(Response).ResultData)

View File

@ -12,7 +12,6 @@ import (
"path/filepath"
"reflect"
"strings"
"time"
)
@ -86,118 +85,118 @@ func (w *Web) SetUrl(u string) error {
// return w.Error
// }
func (w *Web) Get() error {
for range Only.Once {
w.HasTokenExpired()
if !w.newToken {
break
}
// func (w *Web) Get() error {
// for range Only.Once {
// w.HasTokenExpired()
// if !w.newToken {
// break
// }
//
// u := fmt.Sprintf("%s%s",
// t.Url.String(),
// TokenRequestUrl,
// )
// //p, _ := json.Marshal(map[string]string {
// // "user_account": t.Request.Username,
// // "user_password": t.Request.Password,
// // "appkey": t.Request.AppKey,
// // "sys_code": "900",
// //})
// p, _ := json.Marshal(t.Request)
//
// var response *http.Response
// response, t.Error = http.Post(u, "application/json", bytes.NewBuffer(p))
// if t.Error != nil {
// break
// }
// //goland:noinspection GoUnhandledErrorResult
// defer response.Body.Close()
// if response.StatusCode != 200 {
// t.Error = errors.New(fmt.Sprintf("Status Code is %d", response.StatusCode))
// break
// }
//
// var body []byte
// body, t.Error = ioutil.ReadAll(response.Body)
// if t.Error != nil {
// break
// }
//
// t.Error = json.Unmarshal(body, &t.Response)
// if t.Error != nil {
// break
// }
//
// t.TokenExpiry = time.Now()
//
// t.Error = t.saveToken()
// if t.Error != nil {
// break
// }
// }
//
// return t.Error
// }
u := fmt.Sprintf("%s%s",
t.Url.String(),
TokenRequestUrl,
)
//p, _ := json.Marshal(map[string]string {
// "user_account": t.Request.Username,
// "user_password": t.Request.Password,
// "appkey": t.Request.AppKey,
// "sys_code": "900",
//})
p, _ := json.Marshal(t.Request)
var response *http.Response
response, t.Error = http.Post(u, "application/json", bytes.NewBuffer(p))
if t.Error != nil {
break
}
//goland:noinspection GoUnhandledErrorResult
defer response.Body.Close()
if response.StatusCode != 200 {
t.Error = errors.New(fmt.Sprintf("Status Code is %d", response.StatusCode))
break
}
var body []byte
body, t.Error = ioutil.ReadAll(response.Body)
if t.Error != nil {
break
}
t.Error = json.Unmarshal(body, &t.Response)
if t.Error != nil {
break
}
t.TokenExpiry = time.Now()
t.Error = t.saveToken()
if t.Error != nil {
break
}
}
return t.Error
}
// func (w *Web) Get(action interface{}, query interface{}, response interface{}) error {
func (w *Web) Get(action interface{}) error {
func (w *Web) Get(u *url.URL, request interface{}, response interface{}) error {
// func (w *Web) Get(action interface{}) error {
for range Only.Once {
if w.Url == nil {
w.Error = errors.New("SUNGRO API URL is invalid")
break
}
w.Error = VerifyOptionsRequired(query)
w.Error = VerifyOptionsRequired(request)
if w.Error != nil {
break
}
objectName, actionName := GetName(action)
request := FindInStruct(action, "Request")
response := FindInStruct(action, "Response")
// objectName, actionName := GetName(action)
// request := FindInStruct(action, "Request")
// response := FindInStruct(action, "Response")
//
// objectName := GetStructName(object)
// if objectName == "" {
// w.Error = errors.New("invalid object name to structure")
// break
// }
//
// actionName := GetStructName(action)
// if objectName == "" {
// w.Error = errors.New("invalid action name to structure")
// break
// }
//
// requestString := Query(request)
// if objectName == "" {
// w.Error = errors.New("invalid request string for structure")
// break
// }
//
// responseString := Query(response)
// if objectName == "" {
// w.Error = errors.New("invalid response string for structure")
// break
// }
//
// u := fmt.Sprintf("%s?format=json&object=%s&action=%s%s",
// w.Url.String(),
// objectName,
// actionName,
// queryString,
// )
//objectName := GetStructName(object)
if objectName == "" {
w.Error = errors.New("invalid object name to structure")
break
}
//actionName := GetStructName(action)
if objectName == "" {
w.Error = errors.New("invalid action name to structure")
break
}
queryString := Query(query)
if objectName == "" {
w.Error = errors.New("invalid query string for structure")
break
}
u := fmt.Sprintf("%s?format=json&object=%s&action=%s%s",
w.Url.String(),
objectName,
actionName,
queryString,
)
// "?format=json&object=subscriber&action=count"
//fmt.Printf("Object: %s\n", objectName)
//fmt.Printf("Action: %s\n", actionName)
////fmt.Printf("Action: %s\n", actionName)
//fmt.Printf("Query: %s\n", query)
//fmt.Printf("ApiUrl: %s\n", url)
w.request, w.Error = http.NewRequest("GET", u, nil)
if w.Error != nil {
break
}
p, _ := json.Marshal(request)
// w.request, w.Error = http.NewRequest("GET", u, nil)
// if w.Error != nil {
// break
// }
//
// w.request.Header.Set("Authorization", w.Token.GetAuthHeader())
for range Only.Twice {
//w.response, w.Error = http.Get(url)
w.response, w.Error = w.client.Do(w.request)
w.response, w.Error = http.Post(u.String(), "application/json", bytes.NewBuffer(p))
if w.Error != nil {
break
}