1
1
package drivers
2
2
3
-
4
3
import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "io"
5
7
"net/http"
8
+ "net/url"
9
+ "strconv"
10
+ "time"
6
11
)
7
12
8
13
// DO NOT USE DIRECTLY
9
- type zte8810ft struct {
10
- TargetIP string
11
- basePath string
12
- }
14
+ type (
15
+ zte8810ft struct {
16
+ ip string
17
+ basePath string
18
+ }
19
+
20
+ result struct {
21
+ Result string `json:"result"`
22
+ }
23
+
24
+ pppConnected struct {
25
+ Connected string `json:"ppp_connected"`
26
+ }
27
+ )
28
+
29
+ var httpClient * http.Client
13
30
14
31
func init () {
15
- registerModel ("8810FT" , newZTE8810FT )
32
+ httpClient = & http.Client {Timeout : time .Second * 10 }
33
+ registerDriver ("ZTE 8810FT" , newZTE8810FT )
16
34
}
17
35
18
36
func newZTE8810FT (ip string ) BaseModem {
19
- return & zte8810ft {TargetIP : ip , basePath : "/goform/goform_set_cmd_process" }
37
+ return & zte8810ft {ip : ip , basePath : "/goform/goform_set_cmd_process" }
38
+ }
39
+
40
+ func (m * zte8810ft ) getBaseURL (path string ) * url.URL {
41
+ return & url.URL {Scheme : "https" , Host : m .ip , Path : path }
42
+ }
43
+
44
+ func (m * zte8810ft ) getNewRequest (method string , url * url.URL ) * http.Request {
45
+ return & http.Request {
46
+ Proto : "HTTP/1.1" ,
47
+ Method : method ,
48
+ URL : url ,
49
+ Header : http.Header {
50
+ "Referer" : {fmt .Sprintf ("http://%s/index.html" , m .ip )},
51
+ },
52
+ }
20
53
}
21
54
22
55
func (m * zte8810ft ) SetTargetIP (ip string ) error {
23
- m .TargetIP = ip
56
+ m .ip = ip
24
57
return nil
25
58
}
26
59
27
60
func (m * zte8810ft ) GetTargetIP () string {
28
- return m .TargetIP
61
+ return m .ip
29
62
}
30
63
31
64
func (m * zte8810ft ) GetModel () string {
@@ -34,17 +67,123 @@ func (m *zte8810ft) GetModel() string {
34
67
35
68
func (m * zte8810ft ) ConnectCell () error {
36
69
// GET /goform/goform_set_cmd_process?goformId=CONNECT_NETWORK
70
+ // Prepare everything to make a request
71
+ u := m .getBaseURL ("/goform/goform_set_cmd_process" )
72
+ query := u .Query ()
73
+ query .Add ("goformId" , "CONNECT_NETWORK" )
74
+ u .RawQuery = query .Encode ()
75
+ request := m .getNewRequest ("GET" , u )
76
+
77
+ resp , err := httpClient .Do (request )
78
+
79
+ // Process errors
80
+ switch {
81
+ case err != nil :
82
+ return ActionError {Action : "connect" , Err : err }
83
+ case resp .StatusCode != 200 :
84
+ return ActionError {Action : "connect" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
85
+ }
86
+
87
+ // Read the response
88
+ defer resp .Body .Close ()
89
+ body , err := io .ReadAll (resp .Body )
90
+ if err != nil {
91
+ return ErrUnknown
92
+ }
93
+
94
+ result := new (result )
95
+ if err := json .Unmarshal (body , result ); err != nil {
96
+ return ActionError {Action : "connect" , Err : UnmarshalError {RawData : & body , Err : err }}
97
+ }
98
+
99
+ if result .Result != "success" {
100
+ return ActionError {Action : "connect" , Err : fmt .Errorf ("result: %s" , result .Result )}
101
+ }
102
+
37
103
return nil
38
104
}
39
105
40
106
func (m * zte8810ft ) DisconnectCell () error {
41
107
// GET /goform/goform_set_cmd_process?goformId=DISCONNECT_NETWORK
108
+ // Prepare everything to make a request
109
+ u := m .getBaseURL ("/goform/goform_set_cmd_process" )
110
+ query := u .Query ()
111
+ query .Add ("goformId" , "DISCONNECT_NETWORK" )
112
+ u .RawQuery = query .Encode ()
113
+ request := m .getNewRequest ("GET" , u )
114
+
115
+ resp , err := httpClient .Do (request )
116
+
117
+ // Process errors
118
+ switch {
119
+ case err != nil :
120
+ return ActionError {Action : "disconnect" , Err : err }
121
+ case resp .StatusCode != 200 :
122
+ return ActionError {Action : "disconnect" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
123
+ }
124
+
125
+ // Read the response
126
+ defer resp .Body .Close ()
127
+ body , err := io .ReadAll (resp .Body )
128
+ if err != nil {
129
+ return ErrUnknown
130
+ }
131
+
132
+ result := new (result )
133
+ if err := json .Unmarshal (body , result ); err != nil {
134
+ return ActionError {Action : "disconnect" , Err : UnmarshalError {RawData : & body , Err : err }}
135
+ }
136
+
137
+ if result .Result != "success" {
138
+ return ActionError {Action : "disconnect" , Err : fmt .Errorf ("result: %s" , result .Result )}
139
+ }
140
+
42
141
return nil
43
142
}
44
143
45
144
func (m * zte8810ft ) GetCellConnStatus () (bool , error ) {
46
145
// Lines 251-258
47
- resp , err := http .Get ()
48
146
// /goform/goform_get_cmd_process?isTest=False&cmd=ppp_connected,multi_data=1&sms_received_flag_flag=0&sts_received_flag_flag=0&_=<curr_time>
49
- return false , nil
147
+
148
+ // Build URL
149
+ u := m .getBaseURL ("/goform/goform_get_cmd_process" )
150
+ query := u .Query ()
151
+ query .Add ("isTest" , "False" )
152
+ query .Add ("cmd" , "ppp_connected" )
153
+ query .Add ("multi_data" , "1" )
154
+ query .Add ("sms_received_flag_flag" , "0" )
155
+ query .Add ("sts_received_flag_flag" , "0" )
156
+ query .Add ("_" , strconv .FormatInt ((time .Now ().UnixMilli )(), 10 ))
157
+ u .RawQuery = query .Encode ()
158
+
159
+ request := m .getNewRequest ("POST" , u )
160
+
161
+ resp , err := httpClient .Do (request )
162
+
163
+ // Process errors
164
+ switch {
165
+ case err != nil :
166
+ return false , ActionError {Action : "status" , Err : err }
167
+ case resp .StatusCode != 200 :
168
+ return false , ActionError {Action : "status" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
169
+ }
170
+
171
+ // Read the response
172
+ defer resp .Body .Close ()
173
+ body , err := io .ReadAll (resp .Body )
174
+ if err != nil {
175
+ return false , ErrUnknown
176
+ }
177
+
178
+ result := new (pppConnected )
179
+ if err := json .Unmarshal (body , result ); err != nil {
180
+ return false , ActionError {Action : "status" , Err : UnmarshalError {RawData : & body , Err : err }}
181
+ }
182
+
183
+ // Process the result
184
+ if result .Connected != "ppp_connected" {
185
+ return false , nil
186
+ }
187
+
188
+ return true , nil
50
189
}
0 commit comments