-
Notifications
You must be signed in to change notification settings - Fork 10
/
ui.go
294 lines (236 loc) · 6.28 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"fmt"
"gosunxifel/config"
"gosunxifel/util"
"strings"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"
log "github.com/donnie4w/go-logger/logger"
"gosunxifel/sunxifel"
"github.com/zserge/lorca"
)
type Myweb struct {
UI lorca.UI
}
var Defaultweb Myweb
func New(width, height int) Myweb {
var myweb Myweb
var err error
myweb.UI, err = lorca.New("", "", width, height)
if err != nil {
log.Fatal(err)
}
return myweb
}
func (m *Myweb) Run() {
ui := m.UI
defer ui.Close()
// A simple way to know when UI is ready (uses body.onload even in JS)
ui.Bind("start", func() {
log.Debug("UI is ready")
})
ui.Bind("getdisk", getdisk)
ui.Bind("browseclientpath", Browseclientpath)
ui.Bind("browseclientuppage", Browseclientuppage)
ui.Bind("installdriver", installdriver)
ui.Bind("flashburn", flashburn)
ui.Bind("addoneblock", sunxifel.Default.AddOneBlock)
ui.Bind("loadblocklist", loadblocklist)
ui.Bind("clearblocklist", clearblocklist)
//fel功能
ui.Bind("loadusb", loadusb)
ui.Bind("loadbrom", loadbrom)
ui.Bind("loadspi", loadspi)
// Load HTML.
// You may also use `data:text/html,<base64>` approach to load initial HTML,
// e.g: ui.Load("data:text/html," + url.PathEscape(html))
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
go http.Serve(ln, http.FileServer(FS))
ui.Load(fmt.Sprintf("http://%s", ln.Addr()))
// go recieve(ui)
// You may use console.log to debug your JS code, it will be printed via
// log.Println(). Also exceptions are printed in a similar manner.
// ui.Eval(`
// console.log("Hello, world!");
// console.log('Multiple values:', [1, false, {"x":5}]);
// `)
// Wait until the interrupt signal arrives or browser window is closed
sigc := make(chan os.Signal)
signal.Notify(sigc, os.Interrupt)
select {
case <-sigc:
case <-ui.Done():
}
log.Debug("exiting...")
}
//更新win根目录
func getdisk() {
log.Debug("GetClientDisk", runtime.GOOS)
if runtime.GOOS != "windows" {
return
}
dinfo := util.GetDiskInfo()
if len(dinfo) <= 0 {
return
}
log.Debug(dinfo)
jsStr := `$('#disk').find("option").remove()`
Defaultweb.UI.Eval(jsStr)
for k, v := range dinfo {
jsStr := fmt.Sprintf(`$('#disk').append("<option value=%s>%s</option>")`, strconv.Itoa(k), v)
Defaultweb.UI.Eval(jsStr)
}
}
func Browseclientpath(bpath string) string {
curpath := config.Cfg.Section("file").Key("clientpath").MustString(config.GetRootdir())
log.Debug(curpath)
if bpath != "" {
if string(curpath[len(curpath)-1]) != "/" {
curpath += `/` + bpath
} else {
curpath += bpath
}
}
if len(bpath) > 4 {
if bpath[:4] == "disk" {
curpath = bpath[4:]
}
}
log.Debug(curpath)
s, err := os.Stat(curpath)
if err != nil {
log.Error(err)
log.Debug(config.GetRootdir())
return ""
}
if s.IsDir() {
files, _ := ioutil.ReadDir(curpath)
jsStr1 := fmt.Sprintf(`$('#clientpath').val("%s");$("#clientfiles").find("li").remove()`, curpath)
Defaultweb.UI.Eval(jsStr1)
for _, f := range files {
log.Debug(f.Name())
if f.IsDir() {
jsStr1 = fmt.Sprintf(`$('#clientfiles').append("<li><span><img style=\"width:20px;height:20px;\" src='./img/folder.ico'></span>%s</li>")`, f.Name())
} else {
jsStr1 = fmt.Sprintf(`$('#clientfiles').append("<li><span><img style=\"width:20px;height:20px;\" src='./img/doc.ico'></span>%s</li>")`, f.Name())
}
Defaultweb.UI.Eval(jsStr1)
}
config.Cfg.Section("file").Key("clientpath").SetValue(curpath)
config.Save()
return curpath
} else {
// files, _ := ioutil.ReadDir(curpath)
// jsStr1 := `$("#filesgroup").find("li").remove()`
// Defaultweb.UI.Eval(jsStr1)
// for _, f := range files {
// log.Debug(f.Name())
// jsStr := fmt.Sprintf(`$('#filesgroup').append("<li>%s</li>")`, f.Name())
// Defaultweb.UI.Eval(jsStr)
// }
return ""
}
}
func Browseclientuppage() {
curpath := config.Cfg.Section("file").Key("clientpath").MustString(config.GetRootdir())
log.Debug(curpath)
curpath = util.GetParentDirectory(curpath)
log.Debug(curpath)
_, err := os.Stat(curpath)
if err != nil {
log.Error(err)
return
}
files, _ := ioutil.ReadDir(curpath)
jsStr1 := fmt.Sprintf(`$('#clientpath').val("%s");$("#clientfiles").find("li").remove()`, curpath)
Defaultweb.UI.Eval(jsStr1)
for _, f := range files {
log.Debug(f.Name())
if f.IsDir() {
jsStr1 = fmt.Sprintf(`$('#clientfiles').append("<li><span><img style=\"width:20px;height:20px;\" src='./img/folder.ico'></span>%s</li>")`, f.Name())
} else {
jsStr1 = fmt.Sprintf(`$('#clientfiles').append("<li><span><img style=\"width:20px;height:20px;\" src='./img/doc.ico'></span>%s</li>")`, f.Name())
}
Defaultweb.UI.Eval(jsStr1)
}
config.Cfg.Section("file").Key("clientpath").SetValue(curpath)
config.Save()
}
func installdriver() {
go func() {
rootdir := config.GetRootdir()
cmd := exec.Command("cmd.exe", "/c", rootdir+"/sunxi-tools-win32support_f1c100s/zadig-2.3.exe")
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
}()
}
func flashburn(bs []sunxifel.Blockinfo) {
sunxifel.Default.Burn(bs, Defaultweb.UI)
}
func loadblocklist() []sunxifel.Blockinfo {
return sunxifel.Default.LoadBlockList(Defaultweb.UI)
}
func clearblocklist() {
sunxifel.Default.ClearBlockList(Defaultweb.UI)
}
//cmd信息框清空
func cmdclear() {
jsstr := `$('#cmdout').find("li").remove();`
Defaultweb.UI.Eval(jsstr)
}
//cmd信息框输出
func cmdout(str string) {
jsstr := "$('#cmdout').append(\"<li>" + str + "</li>\");"
log.Debug(jsstr)
Defaultweb.UI.Eval(jsstr)
}
//cmd 命令执行
func runcmd(arg string, ui lorca.UI) {
cmdclear()
rootdir := config.GetRootdir()
cmd := exec.Command("cmd.exe", "/c", rootdir+"/sunxi-tools-win32support_f1c100s/sunxi-fel.exe "+arg)
stdoutIn, _ := cmd.StdoutPipe()
err := cmd.Start()
if err != nil {
log.Error("cmd.Start() failed with '%s'\n", err)
}
go func(ui lorca.UI) {
for {
bs := make([]byte, 1024, 1024)
n, err := stdoutIn.Read(bs)
if n > 0 {
var re string
re = string(bs[:n])
re = re[:strings.Index(re, "\n")-1]
cmdout(re)
log.Debug(re)
}
if err != nil {
return
}
}
}(ui)
err = cmd.Wait()
}
func loadusb() {
runcmd("-l", Defaultweb.UI)
}
func loadbrom() {
runcmd("ver", Defaultweb.UI)
}
func loadspi() {
runcmd("spiflash-info", Defaultweb.UI)
}