Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 02a722c

Browse files
committed
后端配置保存功能
1 parent 6e3d84a commit 02a722c

File tree

6 files changed

+82
-8
lines changed

6 files changed

+82
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ bindata_assetfs.go
55
DataAddr*
66
VariablesToRead.json
77
VariablesToMod.json
8+
Config.json

FileHandle/FileConfig.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package filehandle
2+
3+
import "os"
4+
5+
type ConfigT struct {
6+
IsSaveDataAddr bool
7+
IsSaveVariablesToMod bool
8+
IsSaveVariablesToRead bool
9+
}
10+
11+
var Config ConfigT
12+
13+
func LoadConfig() {
14+
if _, err := os.Stat("Config.json"); os.IsNotExist(err) {
15+
Config.IsSaveDataAddr = true
16+
Config.IsSaveVariablesToMod = true
17+
Config.IsSaveVariablesToRead = true
18+
} else {
19+
jsonLoad("Config.json", &Config)
20+
}
21+
}
22+
23+
func SaveConfig() {
24+
jsonSave("Config.json", Config)
25+
if !Config.IsSaveDataAddr {
26+
os.Remove("DataAddr.json")
27+
} else {
28+
jsonSave("DataAddr.json", ProjectVariables)
29+
}
30+
if !Config.IsSaveVariablesToMod {
31+
os.Remove("VariablesToMod.json")
32+
}
33+
if !Config.IsSaveVariablesToRead {
34+
os.Remove("VariablesToRead.json")
35+
}
36+
}

FileHandle/FileConvert.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func Txt2json() error {
3131
for _, v := range match {
3232
ProjectVariables.Variables = append(ProjectVariables.Variables, projectVariablesT{Addr: v[1], Size: v[2], Name: v[3], Type: v[5]})
3333
}
34-
jsonSave("DataAddr.json", ProjectVariables)
34+
if Config.IsSaveDataAddr {
35+
jsonSave("DataAddr.json", ProjectVariables)
36+
}
3537
return nil
3638
}

FileHandle/FileSave.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@ func jsonSave(filename string, v interface{}) {
3838
}
3939

4040
func SaveAll() {
41-
jsonSave("VariablesToRead.json", datapack.CurrentVariables)
42-
jsonSave("VariablesToMod.json", datapack.ModVariables)
41+
SaveConfig()
42+
if Config.IsSaveVariablesToMod {
43+
jsonSave("VariablesToMod.json", datapack.ModVariables)
44+
}
45+
if Config.IsSaveVariablesToRead {
46+
jsonSave("VariablesToRead.json", datapack.CurrentVariables)
47+
}
4348
}
4449

4550
func LoadSaves() {
46-
jsonLoad("DataAddr.json", &ProjectVariables)
47-
jsonLoad("VariablesToRead.json", &datapack.CurrentVariables)
48-
jsonLoad("VariablesToMod.json", &datapack.ModVariables)
51+
LoadConfig()
52+
if Config.IsSaveDataAddr {
53+
jsonLoad("DataAddr.json", &ProjectVariables)
54+
}
55+
if Config.IsSaveVariablesToMod {
56+
jsonLoad("VariablesToMod.json", &datapack.ModVariables)
57+
}
58+
if Config.IsSaveVariablesToRead {
59+
jsonLoad("VariablesToRead.json", &datapack.CurrentVariables)
60+
}
4961
}

WebHandle/HandleFunc.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ func variableModListWebHandler(w http.ResponseWriter, _ *http.Request) {
187187

188188
func fileUploadWebHandler(w http.ResponseWriter, r *http.Request) {
189189
defer r.Body.Close()
190+
defer os.Remove("DataAddr")
190191
r.ParseMultipartForm(32 << 20)
191192
file, _, err := r.FormFile("file")
192193
if err != nil {
193194
io.WriteString(w, "{\"status\":31}")
194195
return
195196
}
196197
defer file.Close()
197-
os.Remove("DataAddr")
198198
f, err := os.OpenFile("DataAddr", os.O_WRONLY|os.O_CREATE, 0666)
199199
if err != nil {
200200
io.WriteString(w, "{\"status\":32}")
@@ -215,6 +215,28 @@ func fileVariablesWebHandler(w http.ResponseWriter, _ *http.Request) {
215215
io.WriteString(w, string(b))
216216
}
217217

218+
func fileConfigWebHandler(w http.ResponseWriter, r *http.Request) {
219+
defer r.Body.Close()
220+
r.ParseForm()
221+
if len(r.Form) == 0 {
222+
b, _ := json.Marshal(filehandle.Config)
223+
io.WriteString(w, string(b))
224+
} else {
225+
if _, ok := r.Form["sda"]; ok {
226+
filehandle.Config.IsSaveDataAddr, _ = strconv.ParseBool(strings.Join(r.Form["sda"], ""))
227+
}
228+
if _, ok := r.Form["svm"]; ok {
229+
filehandle.Config.IsSaveVariablesToMod, _ = strconv.ParseBool(strings.Join(r.Form["svm"], ""))
230+
}
231+
if _, ok := r.Form["svr"]; ok {
232+
filehandle.Config.IsSaveVariablesToRead, _ = strconv.ParseBool(strings.Join(r.Form["svr"], ""))
233+
}
234+
filehandle.SaveConfig()
235+
io.WriteString(w, "{\"status\":0}")
236+
}
237+
238+
}
239+
218240
func Start() {
219241
jsonWS := make(chan string, 10)
220242
go serialhandle.SerialThread(jsonWS)
@@ -233,6 +255,7 @@ func Start() {
233255
http.HandleFunc("/variable/modlist", variableModListWebHandler)
234256
http.HandleFunc("/file/upload", fileUploadWebHandler)
235257
http.HandleFunc("/file/variables", fileVariablesWebHandler)
258+
http.HandleFunc("/file/config", fileConfigWebHandler)
236259
http.HandleFunc("/ws", WebSocketHandler)
237260
addr := ":8080"
238261
log.Println("Listen on " + addr)

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
func main() {
16+
filehandle.LoadSaves()
1617
c := make(chan os.Signal)
1718
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
1819
go func() {
@@ -33,7 +34,6 @@ func main() {
3334
filehandle.SaveAll()
3435
}
3536
}()
36-
filehandle.LoadSaves()
3737
http.Handle("/", http.FileServer(http.Dir("./WebPage/")))
3838
webhandle.Start()
3939
}

0 commit comments

Comments
 (0)