1
- package drivers
1
+ package zte8810ft
2
2
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
6
"io"
7
+ "log/slog"
7
8
"net/http"
8
9
"net/url"
9
10
"strconv"
10
11
"strings"
11
12
"time"
12
13
14
+ "github.com/brokenCursor/usb-modem-cli/drivers/common"
15
+ "github.com/spf13/viper"
13
16
"github.com/warthog618/sms/encoding/gsm7"
14
17
)
15
18
@@ -29,12 +32,22 @@ type (
29
32
}
30
33
)
31
34
35
+ var (
36
+ httpClient * http.Client
37
+ logger * slog.Logger
38
+ config * viper.Viper
39
+ )
40
+
32
41
func init () {
33
- registerDriver ( "ZTE 8810FT" , newZTE8810FT )
34
- // dLogger.Debug("driver registered" )
42
+ fmt . Println ( "here" )
43
+ config , logger = common . RegisterDriver ( "ZTE 8810FT" , newZTE8810FT )
35
44
}
36
45
37
- func newZTE8810FT (host string ) BaseModem {
46
+ func newZTE8810FT (host string ) common.BaseModem {
47
+ if httpClient == nil {
48
+ httpClient = & http.Client {Timeout : config .GetDuration ("cmd_ttl" ) * time .Second }
49
+ }
50
+
38
51
return & zte8810ft {host : host , basePath : "/goform/goform_set_cmd_process" }
39
52
}
40
53
@@ -53,8 +66,8 @@ func (m *zte8810ft) getNewRequest(method string, url *url.URL, headers http.Head
53
66
}
54
67
}
55
68
56
- func (m * zte8810ft ) SetHost (ip string ) error {
57
- m .host = ip
69
+ func (m * zte8810ft ) SetHost (host string ) error {
70
+ m .host = host
58
71
return nil
59
72
}
60
73
@@ -75,32 +88,32 @@ func (m *zte8810ft) ConnectCell() error {
75
88
u .RawQuery = query .Encode ()
76
89
request := m .getNewRequest ("GET" , u , http.Header {})
77
90
78
- dLogger . With ( "driver" , "8810FT" ) .Debug ("request" , request .URL .String (), nil )
91
+ logger .Debug ("request" , request .URL .String (), nil )
79
92
80
93
resp , err := httpClient .Do (request )
81
94
82
95
// Process errors
83
96
switch {
84
97
case err != nil :
85
- return ActionError {Action : "connect" , Err : err }
98
+ return common. ActionError {Action : "connect" , Err : err }
86
99
case resp .StatusCode != 200 :
87
- return ActionError {Action : "connect" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
100
+ return common. ActionError {Action : "connect" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
88
101
}
89
102
90
103
// Read the response
91
104
defer resp .Body .Close ()
92
105
body , err := io .ReadAll (resp .Body )
93
106
if err != nil {
94
- return ErrUnknown
107
+ return common . ErrUnknown
95
108
}
96
109
97
110
result := new (result )
98
111
if err := json .Unmarshal (body , result ); err != nil {
99
- return ActionError {Action : "connect" , Err : UnmarshalError {RawData : & body , Err : err }}
112
+ return common. ActionError {Action : "connect" , Err : common. UnmarshalError {RawData : & body , Err : err }}
100
113
}
101
114
102
115
if result .Result != "success" {
103
- return ActionError {Action : "connect" , Err : fmt .Errorf ("result: %s" , result .Result )}
116
+ return common. ActionError {Action : "connect" , Err : fmt .Errorf ("result: %s" , result .Result )}
104
117
}
105
118
106
119
return nil
@@ -115,37 +128,37 @@ func (m *zte8810ft) DisconnectCell() error {
115
128
u .RawQuery = query .Encode ()
116
129
request := m .getNewRequest ("GET" , u , http.Header {})
117
130
118
- dLogger . With ( "driver" , "8810FT" ) .Debug ("request" , request .URL .String (), nil )
131
+ logger .Debug ("request" , request .URL .String (), nil )
119
132
120
133
resp , err := httpClient .Do (request )
121
134
// Process errors
122
135
switch {
123
136
case err != nil :
124
- return ActionError {Action : "disconnect" , Err : err }
137
+ return common. ActionError {Action : "disconnect" , Err : err }
125
138
case resp .StatusCode != 200 :
126
- return ActionError {Action : "disconnect" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
139
+ return common. ActionError {Action : "disconnect" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
127
140
}
128
141
129
142
// Read the response
130
143
defer resp .Body .Close ()
131
144
body , err := io .ReadAll (resp .Body )
132
145
if err != nil {
133
- return ErrUnknown
146
+ return common . ErrUnknown
134
147
}
135
148
136
149
result := new (result )
137
150
if err := json .Unmarshal (body , result ); err != nil {
138
- return ActionError {Action : "disconnect" , Err : UnmarshalError {RawData : & body , Err : err }}
151
+ return common. ActionError {Action : "disconnect" , Err : common. UnmarshalError {RawData : & body , Err : err }}
139
152
}
140
153
141
154
if result .Result != "success" {
142
- return ActionError {Action : "disconnect" , Err : fmt .Errorf ("result: %s" , result .Result )}
155
+ return common. ActionError {Action : "disconnect" , Err : fmt .Errorf ("result: %s" , result .Result )}
143
156
}
144
157
145
158
return nil
146
159
}
147
160
148
- func (m * zte8810ft ) GetCellConnStatus () (* LinkStatus , error ) {
161
+ func (m * zte8810ft ) GetCellConnStatus () (* common. LinkStatus , error ) {
149
162
// Build URL
150
163
u := m .getBaseURL ("/goform/goform_get_cmd_process" )
151
164
query := u .Query ()
@@ -159,43 +172,43 @@ func (m *zte8810ft) GetCellConnStatus() (*LinkStatus, error) {
159
172
160
173
request := m .getNewRequest ("GET" , u , http.Header {})
161
174
162
- dLogger . With ( "driver" , "8810FT" ) .Debug ("request" , request .URL .String (), nil )
175
+ logger .Debug ("request" , request .URL .String (), nil )
163
176
164
177
resp , err := httpClient .Do (request )
165
178
166
179
// Process errors
167
180
switch {
168
181
case err != nil :
169
- return nil , ActionError {Action : "status" , Err : err }
182
+ return nil , common. ActionError {Action : "status" , Err : err }
170
183
case resp .StatusCode != 200 :
171
- return nil , ActionError {Action : "status" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
184
+ return nil , common. ActionError {Action : "status" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
172
185
}
173
186
174
187
// Read the response
175
188
defer resp .Body .Close ()
176
189
body , err := io .ReadAll (resp .Body )
177
190
if err != nil {
178
- return nil , ErrUnknown
191
+ return nil , common . ErrUnknown
179
192
}
180
193
181
194
result := new (pppConnected )
182
195
if err := json .Unmarshal (body , result ); err != nil {
183
- return nil , ActionError {Action : "status" , Err : UnmarshalError {RawData : & body , Err : err }}
196
+ return nil , common. ActionError {Action : "status" , Err : common. UnmarshalError {RawData : & body , Err : err }}
184
197
}
185
198
186
199
// Process the result
187
200
switch result .Connected {
188
201
case "ppp_connected" :
189
- return & LinkStatus {State : 3 }, nil
202
+ return & common. LinkStatus {State : 3 }, nil
190
203
case "ppp_connecting" :
191
- return & LinkStatus {State : 2 }, nil
204
+ return & common. LinkStatus {State : 2 }, nil
192
205
case "ppp_disconnecting" :
193
- return & LinkStatus {State : 1 }, nil
206
+ return & common. LinkStatus {State : 1 }, nil
194
207
case "ppp_disconnected" :
195
- return & LinkStatus {State : 0 }, nil
208
+ return & common. LinkStatus {State : 0 }, nil
196
209
default :
197
210
// Unknown link status occurred
198
- return nil , ErrUnknown
211
+ return nil , common . ErrUnknown
199
212
}
200
213
}
201
214
@@ -209,7 +222,7 @@ func (m *zte8810ft) SendSMS(phone string, message string) error {
209
222
210
223
}
211
224
if err != nil {
212
- return ActionError {"sms send" , err }
225
+ return common. ActionError {"sms send" , err }
213
226
}
214
227
215
228
// Format into proprietary two byte format: 1122 (0074)
@@ -244,32 +257,32 @@ func (m *zte8810ft) SendSMS(phone string, message string) error {
244
257
stringReadCloser := io .NopCloser (stringReader )
245
258
request .Body = stringReadCloser
246
259
247
- dLogger . With ( "driver" , "8810FT" ) .Debug ("url" , request .URL .String (), "body" , encoded , nil )
260
+ logger .Debug ("url" , request .URL .String (), "body" , encoded , nil )
248
261
249
262
resp , err := httpClient .Do (request )
250
263
251
264
// Process errors
252
265
switch {
253
266
case err != nil :
254
- return ActionError {Action : "sms send" , Err : err }
267
+ return common. ActionError {Action : "sms send" , Err : err }
255
268
case resp .StatusCode != 200 :
256
- return ActionError {Action : "sms send" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
269
+ return common. ActionError {Action : "sms send" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
257
270
}
258
271
259
272
// Read the response
260
273
defer resp .Body .Close ()
261
274
body , err := io .ReadAll (resp .Body )
262
275
if err != nil {
263
- return ErrUnknown
276
+ return common . ErrUnknown
264
277
}
265
278
266
279
result := new (result )
267
280
if err := json .Unmarshal (body , result ); err != nil {
268
- return ActionError {Action : "sms send" , Err : UnmarshalError {RawData : & body , Err : err }}
281
+ return common. ActionError {Action : "sms send" , Err : common. UnmarshalError {RawData : & body , Err : err }}
269
282
}
270
283
271
284
if result .Result != "success" {
272
- return ActionError {Action : "sms send" , Err : fmt .Errorf ("result: %s" , result .Result )}
285
+ return common. ActionError {Action : "sms send" , Err : fmt .Errorf ("result: %s" , result .Result )}
273
286
}
274
287
275
288
return nil
0 commit comments