This commit is contained in:
MickMake 2022-02-14 21:21:54 +11:00
parent 62af223b1e
commit a30e33270c
7 changed files with 295 additions and 417 deletions

85
.idea/workspace.xml generated
View File

@ -5,53 +5,13 @@
</component>
<component name="ChangeListManager">
<list default="true" id="7d634523-07a9-40e1-bec3-1dc036abc5e9" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/iSolarCloud/api/call.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/api/struct_area.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/pkgreflect.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/struct.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/struct.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AppService/getPowerDevicePointNames/const.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" 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/commands.go" beforeDir="false" afterPath="$PROJECT_DIR$/cmd/commands.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmd/struct.go" beforeDir="false" afterPath="$PROJECT_DIR$/cmd/struct.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.sum" beforeDir="false" afterPath="$PROJECT_DIR$/go.sum" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/AliSmsService/init.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AliSmsService/init.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/AliSmsService/pkgreflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/AliSmsService/pkgreflect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/AppService/getPowerDevicePointNames/getPowerDevicePointNames.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/AppService/getPowerDevicePointNames/init.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/AppService/getPowerDevicePointNames/pkgreflect.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/AppService/init.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/AppService/pkgreflect.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/MttvScreenService/init.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/MttvScreenService/init.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/MttvScreenService/pkgreflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/MttvScreenService/pkgreflect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/PowerPointService/init.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/PowerPointService/init.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/PowerPointService/pkgreflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/PowerPointService/pkgreflect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/WebAppService/init.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/WebAppService/init.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/WebAppService/pkgreflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/WebAppService/pkgreflect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/WebIscmAppService/init.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/WebIscmAppService/init.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/WebIscmAppService/pkgreflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/WebIscmAppService/pkgreflect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/api.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/content_handler.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/content_handler.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/content_type.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/content_type.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/init.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/init.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/pkgreflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/pkgreflect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api/struct_areas.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/struct_areas.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/struct_endpoints.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api/struct_endpoints.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/api_init.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api_init.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api_resource_handler.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api_resource_handler.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/api_resource_type.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/api_resource_type.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/content_types.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/content_types.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/login.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/login.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/pkgreflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/pkgreflect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/something.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/something.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/struct.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/struct.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/sungro.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/sungro.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/web/get.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/web/get.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/web/pkgreflect.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/iSolarCloud/web/token.go" beforeDir="false" afterPath="$PROJECT_DIR$/iSolarCloud/sungro/web/token.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/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" />
@ -93,8 +53,8 @@
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/iSolarCloud/sungro" />
<recent name="$PROJECT_DIR$/iSolarCloud/AppService/getPowerDevicePointNames" />
<recent name="$PROJECT_DIR$/iSolarCloud" />
<recent name="$PROJECT_DIR$/iSolarCloud/AppService/getPowerDevicePointNames" />
<recent name="$PROJECT_DIR$/iSolarCloud/api" />
<recent name="$PROJECT_DIR$/printTable" />
</key>
@ -219,42 +179,42 @@
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/api/struct_endpoint.go</url>
<line>76</line>
<line>75</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>72</line>
<line>71</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>68</line>
<line>67</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>64</line>
<line>63</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>91</line>
<line>90</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>95</line>
<line>94</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>80</line>
<line>79</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>100</line>
<line>99</line>
<option name="timeStamp" value="166" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
@ -279,9 +239,24 @@
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/sungro/AppService/login/struct.go</url>
<line>170</line>
<line>160</line>
<option name="timeStamp" value="215" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/sungro/web/token.go</url>
<line>54</line>
<option name="timeStamp" value="218" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/iSolarCloud/sungro/web/token.go</url>
<line>160</line>
<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" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
<watches-manager>

View File

@ -2,7 +2,6 @@ package api
import (
"GoSungro/Only"
"GoSungro/iSolarCloud/web"
"errors"
"net/url"
)
@ -34,7 +33,7 @@ type EndPointStruct struct {
type Common struct {
Get GetFunc
Set SetFunc
Web *web.Web
// Web *web.Web
}
type RequestCommon struct {

View File

@ -0,0 +1,12 @@
package getPowerDevicePointNames
const (
DeviceType1 = "1"
DeviceType3 = "3"
DeviceType4 = "4"
DeviceType5 = "5"
DeviceType7 = "7"
DeviceType11 = "11"
DeviceType14 = "14"
DeviceType17 = "17"
)

View File

@ -16,13 +16,19 @@ type EndPoint api.EndPointStruct
type Request struct {
api.RequestCommon
Extra string
}
type Response struct {
api.ResponseCommon
Another string
DeviceType string `json:"device_type"`
}
type Response struct {
api.ResponseCommon
ResultData ResultData `json:"result_data"`
}
type ResultData []struct {
PointCalType int64 `json:"point_cal_type"`
PointID int64 `json:"point_id"`
PointName string `json:"point_name"`
}
func Init() EndPoint {
fmt.Println("Init()")

View File

@ -133,8 +133,7 @@ type Response struct {
func Init() EndPoint {
fmt.Println("Init()")
foo := EndPoint {
return EndPoint {
Area: api.GetArea(EndPoint{}),
Name: api.GetName(EndPoint{}),
Url: api.GetUrl(Url),
@ -142,32 +141,23 @@ 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 (g EndPoint) GetName() api.EndPointName {
fmt.Println("g.GetName()")
return g.Name
}
func (g EndPoint) GetUrl() *url.URL {
fmt.Println("g.GetUrl()")
return g.Url
}
func (g EndPoint) SetRequest(ref interface{}) error {
fmt.Println("g.SetRequest()")
g.Request = ref.(Request)
fmt.Printf("ref == %v\n", g.Request)
return nil
}
@ -194,7 +184,6 @@ func (g EndPoint) Call() api.Json {
}
func (g EndPoint) Init() *EndPoint {
fmt.Println("g.Init()")
ret := Init()
return &ret
}

View File

@ -2,14 +2,17 @@ package web
import (
"GoSungro/Only"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"reflect"
"strings"
"time"
)
@ -83,143 +86,197 @@ func (w *Web) SetUrl(u string) error {
// return w.Error
// }
// func (w *Web) Get(action interface{}, query interface{}, response interface{}) error {
// for range Only.Once {
// if w.Url == nil {
// w.Error = errors.New("SUNGRO API URL is invalid")
// break
// }
//
// w.Error = VerifyOptionsRequired(query)
// if w.Error != nil {
// break
// }
//
// objectName, actionName := GetName(action)
// //FindInStruct(action, "Request")
// //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
// }
//
// 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
// }
//
// 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)
// if w.Error != nil {
// break
// }
//
// if strings.Contains(w.response.Status, "The access token provided is invalid") {
// // 401 Unauthorized The access token provided is invalid.
// // Will first attempt a refresh of the token OR re-login.
// w.Error = w.Login(nil)
// if w.Error != nil {
// w.Error = errors.New(w.response.Status)
// break
// }
// //w.Error = errors.New(fmt.Sprintf("API response is %s", w.response.Status))
// continue
// }
//
// if w.response.StatusCode == 401 {
// w.Error = errors.New(w.response.Status)
// break
// }
//
// // All OK.
// break
// }
// //goland:noinspection GoUnhandledErrorResult
// defer w.response.Body.Close()
// if w.Error != nil {
// break
// }
//
// if w.response.StatusCode != 200 {
// w.Error = errors.New(fmt.Sprintf("API response is %s", w.response.Status))
// break
// }
//
// w.Body, w.Error = ioutil.ReadAll(w.response.Body)
// if w.Error != nil {
// break
// }
//
// if len(w.Body) == 0 {
// w.Error = errors.New("empty response")
// break
// }
//
// w.Error = json.Unmarshal(w.Body, &response)
// if w.Error != nil {
// fmt.Printf("ERROR: Body is:\n%s\n", w.Body)
// break
// }
// }
//
// return w.Error
// }
func (w *Web) Get() error {
for range Only.Once {
w.HasTokenExpired()
if !w.newToken {
break
}
// func Query(i interface{}) string {
// var ret string
//
// s := reflect.ValueOf(i) // .Elem()
// typeOf := s.Type()
// for id := 0; id < s.NumField(); id++ {
// value := fmt.Sprintf("%v", s.Field(id).Interface())
// if value == "" {
// continue
// }
// ret += fmt.Sprintf("&%s=%s",
// typeOf.Field(id).Tag.Get("json"),
// value,
// )
// //fmt.Printf("%d: %s %s = %v\n",
// // i,
// // typeOfT.Field(i).Name,
// // s.Field(i).Type(),
// // s.Field(i).Interface(),
// //)
// }
//
// return ret
// }
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 {
for range Only.Once {
if w.Url == nil {
w.Error = errors.New("SUNGRO API URL is invalid")
break
}
w.Error = VerifyOptionsRequired(query)
if w.Error != nil {
break
}
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
}
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
}
// 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)
if w.Error != nil {
break
}
if strings.Contains(w.response.Status, "The access token provided is invalid") {
// 401 Unauthorized The access token provided is invalid.
// Will first attempt a refresh of the token OR re-login.
w.Error = w.Login(nil)
if w.Error != nil {
w.Error = errors.New(w.response.Status)
break
}
//w.Error = errors.New(fmt.Sprintf("API response is %s", w.response.Status))
continue
}
if w.response.StatusCode == 401 {
w.Error = errors.New(w.response.Status)
break
}
// All OK.
break
}
//goland:noinspection GoUnhandledErrorResult
defer w.response.Body.Close()
if w.Error != nil {
break
}
if w.response.StatusCode != 200 {
w.Error = errors.New(fmt.Sprintf("API response is %s", w.response.Status))
break
}
w.Body, w.Error = ioutil.ReadAll(w.response.Body)
if w.Error != nil {
break
}
if len(w.Body) == 0 {
w.Error = errors.New("empty response")
break
}
w.Error = json.Unmarshal(w.Body, &response)
if w.Error != nil {
fmt.Printf("ERROR: Body is:\n%s\n", w.Body)
break
}
}
return w.Error
}
func Query(i interface{}) string {
var ret string
s := reflect.ValueOf(i) // .Elem()
typeOf := s.Type()
for id := 0; id < s.NumField(); id++ {
value := fmt.Sprintf("%v", s.Field(id).Interface())
if value == "" {
continue
}
ret += fmt.Sprintf("&%s=%s",
typeOf.Field(id).Tag.Get("json"),
value,
)
//fmt.Printf("%d: %s %s = %v\n",
// i,
// typeOfT.Field(i).Name,
// s.Field(i).Type(),
// s.Field(i).Interface(),
//)
}
return ret
}
func PrintHeader(i interface{}) string {
var ret string
@ -379,35 +436,35 @@ func ReflectAsJson(ref interface{}) string {
for range Only.Once {
switch reflect.TypeOf(ref).Kind() {
case reflect.Slice:
case reflect.Array:
fmt.Println("The interface is a slice.")
s := reflect.ValueOf(ref)
ret += "["
for i := 0; i < s.Len(); i++ {
ret += ReflectAsJson(s.Index(i))
}
ret += "]"
case reflect.Struct:
s := reflect.ValueOf(ref) // .Elem()
typeOf := s.Type()
for i := 0; i < s.NumField(); i++ {
value := fmt.Sprintf("%v", s.Field(i).Interface())
if value == "" {
continue
case reflect.Slice:
case reflect.Array:
fmt.Println("The interface is a slice.")
s := reflect.ValueOf(ref)
ret += "["
for i := 0; i < s.Len(); i++ {
ret += ReflectAsJson(s.Index(i))
}
ret += "]"
case reflect.Struct:
s := reflect.ValueOf(ref) // .Elem()
typeOf := s.Type()
for i := 0; i < s.NumField(); i++ {
value := fmt.Sprintf("%v", s.Field(i).Interface())
if value == "" {
continue
}
ret += fmt.Sprintf("%s:%s\n",
typeOf.Field(i).Tag.Get("json"),
value,
)
//fmt.Printf("%d: %s %s = %v\n",
// i,
// typeOfT.Field(i).Name,
// s.Field(i).Type(),
// s.Field(i).Interface(),
//)
}
ret += fmt.Sprintf("%s:%s\n",
typeOf.Field(i).Tag.Get("json"),
value,
)
//fmt.Printf("%d: %s %s = %v\n",
// i,
// typeOfT.Field(i).Name,
// s.Field(i).Type(),
// s.Field(i).Interface(),
//)
}
}
}
@ -470,12 +527,12 @@ func GetName(ref interface{}) (string, string) {
str = strings.ToLower(str)
sa := strings.SplitN(str, ".", 2)
switch len(sa) {
case 0:
case 1:
packageName = sa[0]
case 2:
packageName = sa[0]
structName = sa[1]
case 0:
case 1:
packageName = sa[0]
case 2:
packageName = sa[0]
structName = sa[1]
}
return packageName, structName
}

View File

@ -2,6 +2,7 @@ package web
import (
"GoSungro/Only"
"GoSungro/iSolarCloud/sungro/AppService/login"
"bytes"
"encoding/json"
"errors"
@ -37,8 +38,8 @@ type SunGroAuth struct {
type Token struct {
Url *url.URL
Request TokenRequest
Response TokenResponse
Request login.Request
Response login.Response
TokenFile string
TokenExpiry time.Time
@ -48,135 +49,10 @@ type Token struct {
Error error
}
type TokenRequest struct {
Username string `json:"user_account"`
Password string `json:"user_password"`
AppKey string `json:"appkey"`
SysCode string `json:"sys_code"`
}
type TokenResponse struct {
ReqSerialNum string `json:"req_serial_num"`
ResultCode string `json:"result_code"`
ResultData struct {
AcceptOrderNum int64 `json:"accept_order_num"`
BackgroundColor int64 `json:"background_color"`
Countryid string `json:"countryid"`
Createdate string `json:"createdate"`
Createuserid string `json:"createuserid"`
CurrentOrderNum int64 `json:"current_order_num"`
DisableTime string `json:"disable_time"`
Email string `json:"email"`
Englishname interface{} `json:"englishname"`
ErrTimes string `json:"err_times"`
GcjLatitude interface{} `json:"gcj_latitude"`
GcjLongitude interface{} `json:"gcj_longitude"`
ImToken interface{} `json:"im_token"`
IsDST string `json:"isDST"`
IsAfsFlag string `json:"is_afs_flag"`
IsAgreeGdpr int64 `json:"is_agree_gdpr"`
IsAu int64 `json:"is_au"`
IsCanModifyUserAccount int64 `json:"is_can_modify_user_account"`
IsDisableMap string `json:"is_disable_map"`
IsGdpr int64 `json:"is_gdpr"`
IsHaveIm int64 `json:"is_have_im"`
IsNewVersion int64 `json:"is_new_version"`
IsOnline string `json:"is_online"`
IsOpenProtocol int64 `json:"is_open_protocol"`
IsReceiveNotice int64 `json:"is_receive_notice"`
IsSharePosition int64 `json:"is_share_position"`
IsUploadLocation int64 `json:"is_upload_location"`
IsUseSungrowBrand string `json:"is_use_sungrow_brand"`
IsValidMobileEmail int64 `json:"is_valid_mobile_email"`
Isdst string `json:"isdst"`
Jobs interface{} `json:"jobs"`
Language string `json:"language"`
LoginFirstDate string `json:"loginFirstDate"`
LoginFristDate string `json:"loginFristDate"`
LoginLastDate string `json:"loginLastDate"`
LoginLastIP string `json:"loginLastIp"`
LoginTimes int64 `json:"loginTimes"`
LoginState string `json:"login_state"`
Logo interface{} `json:"logo"`
LogoHTTPSURL interface{} `json:"logo_https_url"`
MapType string `json:"map_type"`
MinDate string `json:"min_date"`
MobileTel interface{} `json:"mobile_tel"`
OrgID string `json:"org_id"`
OrgName string `json:"org_name"`
OrgTimezone string `json:"org_timezone"`
PasswordIsSimple int64 `json:"password_is_simple"`
PhotoID interface{} `json:"photo_id"`
PhotoURL interface{} `json:"photo_url"`
Privileges []struct {
FatherID int64 `json:"father_id"`
IconURL interface{} `json:"icon_url"`
IsOpen interface{} `json:"is_open"`
IsThirdPlatform int64 `json:"is_third_platform"`
MenuCode string `json:"menu_code"`
MenuLevel int64 `json:"menu_level"`
MenuName string `json:"menu_name"`
MenuOrder interface{} `json:"menu_order"`
MenuType string `json:"menu_type"`
MenuURL string `json:"menu_url"`
PrivilegeID int64 `json:"privilege_id"`
RoleID int64 `json:"role_id"`
URLTarget string `json:"url_target"`
VueIcon interface{} `json:"vue_icon"`
VuePath interface{} `json:"vue_path"`
} `json:"privileges"`
RoleID string `json:"role_id"`
SecondaryOrgIds []interface{} `json:"secondaryOrgIds"`
ServerTel string `json:"server_tel"`
ServiceVersion string `json:"service_version"`
Sex string `json:"sex"`
Stylename string `json:"stylename"`
TimeZone string `json:"timeZone"`
Timezone string `json:"timezone"`
Timezoneid string `json:"timezoneid"`
Toggleflag string `json:"toggleflag"`
Token string `json:"token"`
UnlockLaveMinute int64 `json:"unlock_lave_minute"`
UploadTime interface{} `json:"upload_time"`
UserAccount string `json:"user_account"`
UserAccountModifyCount int64 `json:"user_account_modify_count"`
UserAccountModifyRemainTimes int64 `json:"user_account_modify_remain_times"`
UserDealerOrgCode interface{} `json:"user_dealer_org_code"`
UserID string `json:"user_id"`
UserLevel string `json:"user_level"`
UserMasterOrgID string `json:"user_master_org_id"`
UserMasterOrgName string `json:"user_master_org_name"`
UserMasterOrgTimeZoneID string `json:"user_master_org_time_zone_id"`
UserMasterOrgTimeZoneName string `json:"user_master_org_time_zone_name"`
UserName string `json:"user_name"`
UserRoleIDList []string `json:"user_role_id_list"`
UserTelNationCode interface{} `json:"user_tel_nation_code"`
UserauthorURL []interface{} `json:"userauthorURL"`
Userauthorbutto []string `json:"userauthorbutto"`
Userdesc interface{} `json:"userdesc"`
Userpassword string `json:"userpassword"`
Validflag string `json:"validflag"`
Voice string `json:"voice"`
Welcometext string `json:"welcometext"`
Wgs84Latitude interface{} `json:"wgs84_latitude"`
Wgs84Longitude interface{} `json:"wgs84_longitude"`
WorkTel interface{} `json:"work_tel"`
} `json:"result_data"`
ResultMsg string `json:"result_msg"`
}
//func NewToken(id string, secret string, username string, password string) token {
// var t token
// t.Error = t.SetAuth(id, secret, username, password)
// return t
//}
func (t *Token) Login(auth *SunGroAuth) error {
for range Only.Once {
t.Error = t.readTokenFile()
//if t.Error == nil {
// break
//}
_ = t.readTokenFile()
t.Error = t.Verify(auth)
if t.Error != nil {
@ -187,36 +63,11 @@ func (t *Token) Login(auth *SunGroAuth) error {
if t.Error != nil {
break
}
//t.newToken = true
}
return t.Error
}
//func (t *Token) Refresh() error {
// for range Once.Once {
// //t.Error = t.readTokenFile()
// //if t.Error == nil {
// // break
// //}
//
// t.Error = t.RetrieveToken()
// if t.Error != nil {
// break
// }
//
// t.Error = t.saveToken()
// if t.Error != nil {
// break
// }
//
// t.newToken = true
// }
//
// return t.Error
//}
func (t *Token) Verify(auth *SunGroAuth) error {
for range Only.Once {
if auth == nil {
@ -237,9 +88,9 @@ func (t *Token) Verify(auth *SunGroAuth) error {
break
}
//if auth.Token == "" {
// t.newToken = true
//}
if t.Response.ResultData.Token == "" {
t.newToken = true
}
if auth.TokenExpiry == "" {
auth.TokenExpiry = time.Now().Format(DateTimeFormat)
@ -249,11 +100,11 @@ func (t *Token) Verify(auth *SunGroAuth) error {
t.newToken = true
}
t.Request = TokenRequest {
AppKey: auth.AppKey,
t.Request = login.Request {
Appkey: auth.AppKey,
SysCode: "900",
Username: auth.Username,
Password: auth.Password,
UserAccount: auth.Username,
UserPassword: auth.Password,
}
t.HasTokenExpired()
@ -310,17 +161,6 @@ func (t *Token) RetrieveToken() error {
if t.Error != nil {
break
}
////_, _ = fmt.Fprintf(os.Stderr, "Now: %s\n", t.TokenExpiry.Format("Mon Jan _2 15:04:05 MST 2006"))
////_, _ = fmt.Fprintf(os.Stderr, "Seconds: %d\n", t.Response.ExpiresIn)
//t.TokenExpiry = t.TokenExpiry.Add(time.Duration(t.Response.ExpiresIn) * time.Second)
//
////_, _ = fmt.Fprintf(os.Stderr, "Expiry: %s\n", t.TokenExpiry.Format("Mon Jan _2 15:04:05 MST 2006"))
////_, _ = fmt.Fprintf(os.Stderr, "Response:\n%s\n", response)
////_, _ = fmt.Fprintf(os.Stderr, "Body:\n%s\n", body)
////_, _ = fmt.Fprintf(os.Stderr, "t.Response:\n%v\n", t.Response)
////_, _ = fmt.Fprintf(os.Stderr, "Result:\n%s\n", result)
////_, _ = fmt.Fprintf(os.Stderr, "%s %s\n", t.Response.TokenType, t.Response.AccessToken)
}
return t.Error
@ -416,7 +256,7 @@ func (t *Token) saveToken() error {
t.TokenFile = filepath.Join(t.TokenFile, ".GoSungro", DefaultAuthTokenFile)
}
//fmt.Printf("Saving token file to: %t\n", t.TokenFile)
fmt.Printf("Saving token file to: %s\n", t.TokenFile)
var f *os.File
f, t.Error = os.OpenFile(t.TokenFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if t.Error != nil {
@ -426,7 +266,7 @@ func (t *Token) saveToken() error {
//goland:noinspection GoUnhandledErrorResult
defer f.Close()
t.Error = json.NewEncoder(f).Encode(t.Response)
t.Error = json.NewEncoder(f).Encode(t.Response.ResultData)
}
return t.Error