|
@@ -1,6 +1,7 @@
|
|
|
package gateway
|
|
|
|
|
|
import (
|
|
|
+ "bufio"
|
|
|
"confrontation-training/common"
|
|
|
"confrontation-training/constant"
|
|
|
errors "confrontation-training/err"
|
|
@@ -14,6 +15,7 @@ import (
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
+ "io"
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"strconv"
|
|
@@ -617,16 +619,36 @@ func FindGateway(c *gin.Context) {
|
|
|
func SetGatewayMac(c *gin.Context) {
|
|
|
var param gateway.GatewayInfo
|
|
|
c.ShouldBindJSON(¶m)
|
|
|
- file, err := os.OpenFile("./config/application.yaml", os.O_APPEND, 0666)
|
|
|
+ file, err := os.OpenFile("./config/application.yaml", os.O_RDWR, 0666)
|
|
|
+ defer file.Close()
|
|
|
if err != nil {
|
|
|
response.Failed("读取配置文件失败", c)
|
|
|
return
|
|
|
}
|
|
|
- _, err = file.Write([]byte(param.Mac))
|
|
|
- if err != nil {
|
|
|
- response.Failed("写入配置文件失败", c)
|
|
|
- return
|
|
|
+ pos := int64(0)
|
|
|
+ reader := bufio.NewReader(file)
|
|
|
+ for {
|
|
|
+ line, err := reader.ReadString('\n')
|
|
|
+ if err != nil {
|
|
|
+ if err == io.EOF {
|
|
|
+ if line == "" {
|
|
|
+ break
|
|
|
+ } else {
|
|
|
+ global.Log4J.Info("File read ok!")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ global.Log4J.Info("Read File error", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ global.Log4J.Info(line)
|
|
|
+ if strings.Contains(line, " gatewayMac:") {
|
|
|
+ bytes := []byte(" gatewayMac: " + param.Mac)
|
|
|
+ file.WriteAt(bytes, pos)
|
|
|
+ }
|
|
|
+ pos += int64(len(line))
|
|
|
}
|
|
|
+ global.EmqConfig.GatewayMac = param.Mac
|
|
|
response.Success("网关信息保存成功,请重启软件!", "", c)
|
|
|
return
|
|
|
}
|