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

Commit b11ca56

Browse files
committed
变量保存功能
1 parent 1838763 commit b11ca56

File tree

4 files changed

+79
-16
lines changed

4 files changed

+79
-16
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
/Robot_Monitor_Web
33
WebPageBuild
44
bindata_assetfs.go
5-
DataAddr*
5+
DataAddr*
6+
VariablesToRead.json
7+
VariablesToMod.json

FileHandle/FileConvert.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package filehandle
22

33
import (
4-
"encoding/json"
54
"io/ioutil"
65
"log"
76
"os"
@@ -39,18 +38,6 @@ func Txt2json() error {
3938
for _, v := range match {
4039
ProjectVariables.Variables = append(ProjectVariables.Variables, projectVariablesT{Addr: v[1], Size: v[2], Name: v[3], Type: v[5]})
4140
}
42-
jsonTxt, err := json.Marshal(ProjectVariables)
43-
if err != nil {
44-
log.Fatal(err)
45-
return err
46-
}
47-
os.Remove("DataAddr.json")
48-
f, err := os.OpenFile("DataAddr.json", os.O_WRONLY|os.O_CREATE, 0666)
49-
if err != nil {
50-
log.Fatal(err)
51-
return err
52-
}
53-
defer f.Close()
54-
f.Write(jsonTxt)
41+
jsonSave("DataAddr.json", ProjectVariables)
5542
return nil
5643
}

FileHandle/FileSave.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package filehandle
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
9+
datapack "www.scut-robotlab.cn/git/M3chD09/Robot_Monitor_Web/DataPack"
10+
)
11+
12+
func jsonLoad(filename string, v interface{}) {
13+
if _, err := os.Stat(filename); !os.IsNotExist(err) {
14+
log.Println(filename, "Found")
15+
data, err := ioutil.ReadFile(filename)
16+
if err != nil {
17+
log.Println(err)
18+
}
19+
err = json.Unmarshal(data, v)
20+
if err != nil {
21+
log.Println(err)
22+
}
23+
}
24+
}
25+
26+
func jsonSave(filename string, v interface{}) {
27+
if _, err := os.Stat(filename); !os.IsNotExist(err) {
28+
os.Remove(filename)
29+
}
30+
jsonTxt, err := json.Marshal(v)
31+
if err != nil {
32+
log.Println(err)
33+
}
34+
err = ioutil.WriteFile(filename, jsonTxt, 0644)
35+
if err != nil {
36+
log.Println(err)
37+
}
38+
}
39+
40+
func SaveAll() {
41+
jsonSave("VariablesToRead.json", datapack.CurrentVariables)
42+
jsonSave("VariablesToMod.json", datapack.ModVariables)
43+
}
44+
45+
func LoadSaves() {
46+
jsonLoad("DataAddr.json", &ProjectVariables)
47+
jsonLoad("VariablesToRead.json", &datapack.CurrentVariables)
48+
jsonLoad("VariablesToMod.json", &datapack.ModVariables)
49+
}

main.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,38 @@ package main
22

33
import (
44
"net/http"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
"time"
59

10+
filehandle "www.scut-robotlab.cn/git/M3chD09/Robot_Monitor_Web/FileHandle"
611
serialhandle "www.scut-robotlab.cn/git/M3chD09/Robot_Monitor_Web/SerialHandle"
712
webhandle "www.scut-robotlab.cn/git/M3chD09/Robot_Monitor_Web/WebHandle"
813
)
914

1015
func main() {
11-
defer serialhandle.CloseSerialPort()
16+
c := make(chan os.Signal)
17+
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
18+
go func() {
19+
for s := range c {
20+
switch s {
21+
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
22+
filehandle.SaveAll()
23+
serialhandle.CloseSerialPort()
24+
os.Exit(0)
25+
default:
26+
os.Exit(0)
27+
}
28+
}
29+
}()
30+
go func() {
31+
for {
32+
time.Sleep(5 * time.Second)
33+
filehandle.SaveAll()
34+
}
35+
}()
36+
filehandle.LoadSaves()
1237
http.Handle("/", http.FileServer(http.Dir("./WebPage/")))
1338
webhandle.Start()
1439
}

0 commit comments

Comments
 (0)