@@ -12,10 +12,9 @@ import (
1212 "time"
1313)
1414
15- const growattUrl = "https://server-api.growatt.com"
16-
1715type Client struct {
1816 client * http.Client
17+ serverUrl string
1918 username string
2019 password string
2120 userAgent string
@@ -24,23 +23,30 @@ type Client struct {
2423 jar * cookiejar.Jar
2524}
2625
27- func NewClient (username string , password string ) * Client {
26+ func NewClient (serverUrl string , username string , password string ) * Client {
2827 jar , err := cookiejar .New (nil )
2928 if err != nil {
3029 slog .Error ("could not create cookie jar" , slog .String ("error" , err .Error ()))
3130 misc .Panic (err )
3231 }
3332
33+ if len (serverUrl ) == 0 {
34+ serverUrl = "https://server-api.growatt.com"
35+ }
36+
37+ slog .Info ("setting server url" , slog .String ("url" , serverUrl ))
38+
3439 return & Client {
3540 client : & http.Client {
3641 Transport : nil ,
3742 CheckRedirect : nil ,
3843 Jar : jar ,
3944 Timeout : 10 * time .Second ,
4045 },
41- username : username ,
42- password : hashPassword (password ),
43- jar : jar ,
46+ serverUrl : serverUrl ,
47+ username : username ,
48+ password : hashPassword (password ),
49+ jar : jar ,
4450 }
4551}
4652
@@ -65,7 +71,7 @@ func (h *Client) Login() error {
6571 }
6672
6773 var data LoginResult
68- if _ , err := h .postForm (growattUrl + "/newTwoLoginAPIV2.do" , url.Values {
74+ if _ , err := h .postForm (h . serverUrl + "/newTwoLoginAPIV2.do" , url.Values {
6975 "userName" : {h .username },
7076 "password" : {h .password },
7177 "newLogin" : {"1" },
@@ -93,7 +99,7 @@ func (h *Client) Login() error {
9399
94100func (h * Client ) GetPlantList () (* PlantListV2 , error ) {
95101 var data PlantListV2
96- if _ , err := h .postForm (growattUrl + "/newTwoPlantAPI.do?op=getAllPlantListTwo" , url.Values {
102+ if _ , err := h .postForm (h . serverUrl + "/newTwoPlantAPI.do?op=getAllPlantListTwo" , url.Values {
97103 "plantStatus" : {"" },
98104 "pageSize" : {"20" },
99105 "language" : {"1" },
@@ -107,7 +113,7 @@ func (h *Client) GetPlantList() (*PlantListV2, error) {
107113
108114func (h * Client ) GetNoahPlantInfo (plantId string ) (* NoahPlantInfo , error ) {
109115 var data NoahPlantInfo
110- if _ , err := h .postForm (growattUrl + "/noahDeviceApi/noah/isPlantNoahSystem" , url.Values {
116+ if _ , err := h .postForm (h . serverUrl + "/noahDeviceApi/noah/isPlantNoahSystem" , url.Values {
111117 "plantId" : {plantId },
112118 }, & data ); err != nil {
113119 return nil , err
@@ -117,7 +123,7 @@ func (h *Client) GetNoahPlantInfo(plantId string) (*NoahPlantInfo, error) {
117123
118124func (h * Client ) GetNoahStatus (serialNumber string ) (* NoahStatus , error ) {
119125 var data NoahStatus
120- if _ , err := h .postForm (growattUrl + "/noahDeviceApi/noah/getSystemStatus" , url.Values {
126+ if _ , err := h .postForm (h . serverUrl + "/noahDeviceApi/noah/getSystemStatus" , url.Values {
121127 "deviceSn" : {serialNumber },
122128 }, & data ); err != nil {
123129 return nil , err
@@ -127,7 +133,7 @@ func (h *Client) GetNoahStatus(serialNumber string) (*NoahStatus, error) {
127133
128134func (h * Client ) GetNoahInfo (serialNumber string ) (* NoahInfo , error ) {
129135 var data NoahInfo
130- if _ , err := h .postForm (growattUrl + "/noahDeviceApi/noah/getNoahInfoBySn" , url.Values {
136+ if _ , err := h .postForm (h . serverUrl + "/noahDeviceApi/noah/getNoahInfoBySn" , url.Values {
131137 "deviceSn" : {serialNumber },
132138 }, & data ); err != nil {
133139 return nil , err
@@ -138,7 +144,7 @@ func (h *Client) GetNoahInfo(serialNumber string) (*NoahInfo, error) {
138144
139145func (h * Client ) GetBatteryData (serialNumber string ) (* BatteryInfo , error ) {
140146 var data BatteryInfo
141- if _ , err := h .postForm (growattUrl + "/noahDeviceApi/noah/getBatteryData" , url.Values {
147+ if _ , err := h .postForm (h . serverUrl + "/noahDeviceApi/noah/getBatteryData" , url.Values {
142148 "deviceSn" : {serialNumber },
143149 }, & data ); err != nil {
144150 return nil , err
@@ -150,7 +156,7 @@ func (h *Client) GetBatteryData(serialNumber string) (*BatteryInfo, error) {
150156func (h * Client ) SetDefaultPower (serialNumber string , power float64 ) error {
151157 p := math .Max (0 , math .Min (800 , power ))
152158 var data map [string ]any
153- if _ , err := h .postForm (growattUrl + "/noahDeviceApi/noah/set" , url.Values {
159+ if _ , err := h .postForm (h . serverUrl + "/noahDeviceApi/noah/set" , url.Values {
154160 "serialNum" : {serialNumber },
155161 "type" : {"default_power" },
156162 "param1" : {fmt .Sprintf ("%.0f" , p )},
@@ -165,7 +171,7 @@ func (h *Client) SetSocLimit(serialNumber string, chargingLimit float64, dischar
165171 c := math .Max (70 , math .Min (100 , chargingLimit ))
166172 d := math .Max (0 , math .Min (30 , dischargeLimit ))
167173 var data map [string ]any
168- if _ , err := h .postForm (growattUrl + "/noahDeviceApi/noah/set" , url.Values {
174+ if _ , err := h .postForm (h . serverUrl + "/noahDeviceApi/noah/set" , url.Values {
169175 "serialNum" : {serialNumber },
170176 "type" : {"charging_soc" },
171177 "param1" : {fmt .Sprintf ("%.0f" , c )},
0 commit comments