366 lines
11 KiB
Go
366 lines
11 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"gateway-ui/model"
|
|
"net/http"
|
|
"os/exec"
|
|
"path/filepath"
|
|
|
|
l "gateway-ui/log"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
logger l.Logger
|
|
}
|
|
|
|
func InitHandler(logger l.Logger) *Handler {
|
|
handler := Handler{
|
|
logger: logger,
|
|
}
|
|
return &handler
|
|
}
|
|
|
|
func (h *Handler) Ping(c *gin.Context) {
|
|
h.logger.Info("ping")
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "pong",
|
|
})
|
|
}
|
|
|
|
func (h *Handler) GetConf(c *gin.Context) {
|
|
conf := model.Config{}
|
|
if err := conf.Read(); err != nil {
|
|
c.String(http.StatusInternalServerError, fmt.Sprintf("读取config.toml文件失败: %s", err.Error()))
|
|
h.logger.Error(fmt.Sprintf("读取config.toml文件失败: %s", err.Error()))
|
|
return
|
|
}
|
|
netconf := model.NetConfig{}
|
|
if err := netconf.Read(); err != nil {
|
|
c.String(http.StatusInternalServerError, fmt.Sprintf("读取netcfg.yaml文件失败: %s", err.Error()))
|
|
h.logger.Error(fmt.Sprintf("读取netcfg.yaml文件失败: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
res := ConfReq{}
|
|
res.Addresses = netconf.Network.Ethernets.Eth1.Addresses[0]
|
|
res.Via = netconf.Network.Ethernets.Eth1.Routes[0].Via
|
|
|
|
res.Broker = conf.MQTT.Broker
|
|
h.logger.Info(conf.MQTT.Broker)
|
|
res.Client_id = conf.MQTT.Client_id
|
|
res.Device_id = conf.MQTT.Device_id
|
|
res.Fansnum = conf.MQTT.Fansnum
|
|
res.Windfarm = conf.MQTT.Windfarm
|
|
|
|
res.Ntp = conf.Sys.Ntp
|
|
|
|
res.Ch0_measurpoint = conf.MQTT.Ch0_measurpoint
|
|
res.Ch0_measurpointdirection = conf.MQTT.Ch0_measurpointdirection
|
|
res.Ch0_samplingfrequency = conf.MQTT.Ch0_samplingfrequency
|
|
res.Ch0_samplingtime = conf.MQTT.Ch0_samplingtime
|
|
res.Ch0_sensorparameters = conf.MQTT.Ch0_sensorparameters
|
|
|
|
res.Ch1_measurpoint = conf.MQTT.Ch1_measurpoint
|
|
res.Ch1_measurpointdirection = conf.MQTT.Ch1_measurpointdirection
|
|
res.Ch1_samplingfrequency = conf.MQTT.Ch1_samplingfrequency
|
|
res.Ch1_samplingtime = conf.MQTT.Ch1_samplingtime
|
|
res.Ch1_sensorparameters = conf.MQTT.Ch1_sensorparameters
|
|
|
|
res.Ch2_measurpoint = conf.MQTT.Ch2_measurpoint
|
|
res.Ch2_measurpointdirection = conf.MQTT.Ch2_measurpointdirection
|
|
res.Ch2_samplingfrequency = conf.MQTT.Ch2_samplingfrequency
|
|
res.Ch2_samplingtime = conf.MQTT.Ch2_samplingtime
|
|
res.Ch2_sensorparameters = conf.MQTT.Ch2_sensorparameters
|
|
|
|
res.Ch3_measurpoint = conf.MQTT.Ch3_measurpoint
|
|
res.Ch3_measurpointdirection = conf.MQTT.Ch3_measurpointdirection
|
|
res.Ch3_samplingfrequency = conf.MQTT.Ch3_samplingfrequency
|
|
res.Ch3_samplingtime = conf.MQTT.Ch3_samplingtime
|
|
res.Ch3_sensorparameters = conf.MQTT.Ch3_sensorparameters
|
|
|
|
res.Ch4_measurpoint = conf.MQTT.Ch4_measurpoint
|
|
res.Ch4_measurpointdirection = conf.MQTT.Ch4_measurpointdirection
|
|
res.Ch4_samplingfrequency = conf.MQTT.Ch4_samplingfrequency
|
|
res.Ch4_samplingtime = conf.MQTT.Ch4_samplingtime
|
|
res.Ch4_sensorparameters = conf.MQTT.Ch4_sensorparameters
|
|
|
|
res.Ch5_measurpoint = conf.MQTT.Ch5_measurpoint
|
|
res.Ch5_measurpointdirection = conf.MQTT.Ch5_measurpointdirection
|
|
res.Ch5_samplingfrequency = conf.MQTT.Ch5_samplingfrequency
|
|
res.Ch5_samplingtime = conf.MQTT.Ch5_samplingtime
|
|
res.Ch5_sensorparameters = conf.MQTT.Ch5_sensorparameters
|
|
|
|
res.Ch6_measurpoint = conf.MQTT.Ch6_measurpoint
|
|
res.Ch6_measurpointdirection = conf.MQTT.Ch6_measurpointdirection
|
|
res.Ch6_samplingfrequency = conf.MQTT.Ch6_samplingfrequency
|
|
res.Ch6_samplingtime = conf.MQTT.Ch6_samplingtime
|
|
res.Ch6_sensorparameters = conf.MQTT.Ch6_sensorparameters
|
|
|
|
res.Ch7_measurpoint = conf.MQTT.Ch7_measurpoint
|
|
res.Ch7_measurpointdirection = conf.MQTT.Ch7_measurpointdirection
|
|
res.Ch7_samplingfrequency = conf.MQTT.Ch7_samplingfrequency
|
|
res.Ch7_samplingtime = conf.MQTT.Ch7_samplingtime
|
|
res.Ch7_sensorparameters = conf.MQTT.Ch7_sensorparameters
|
|
|
|
c.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (h *Handler) SetConf(c *gin.Context) {
|
|
|
|
req := ConfReq{}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, fmt.Sprintf("请求转换JSON失败: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
conf := model.Config{}
|
|
if err := conf.Read(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("读取config.toml文件失败: %s", err.Error()))
|
|
return
|
|
}
|
|
netconf := model.NetConfig{}
|
|
if err := netconf.Read(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("读取netconf.yaml文件失败: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
if req.Addresses != "" {
|
|
netconf.Network.Ethernets.Eth1.Addresses[0] = req.Addresses
|
|
}
|
|
if req.Via != "" {
|
|
netconf.Network.Ethernets.Eth1.Routes[0].Via = req.Via
|
|
}
|
|
|
|
if req.Broker != "" {
|
|
conf.MQTT.Broker = req.Broker
|
|
}
|
|
if req.Client_id != "" {
|
|
conf.MQTT.Client_id = req.Client_id
|
|
}
|
|
if req.Device_id != "" {
|
|
conf.MQTT.Device_id = req.Device_id
|
|
}
|
|
if req.Fansnum != "" {
|
|
conf.MQTT.Fansnum = req.Fansnum
|
|
}
|
|
if req.Windfarm != "" {
|
|
conf.MQTT.Windfarm = req.Windfarm
|
|
}
|
|
if req.Ntp != "" {
|
|
conf.Sys.Ntp = req.Ntp
|
|
}
|
|
if req.Ch0_measurpoint != "" {
|
|
conf.MQTT.Ch0_measurpoint = req.Ch0_measurpoint
|
|
}
|
|
if req.Ch0_measurpointdirection != "" {
|
|
conf.MQTT.Ch0_measurpointdirection = req.Ch0_measurpointdirection
|
|
}
|
|
if req.Ch0_samplingfrequency != 0 {
|
|
conf.MQTT.Ch0_samplingfrequency = req.Ch0_samplingfrequency
|
|
}
|
|
if req.Ch0_samplingtime != 0 {
|
|
conf.MQTT.Ch0_samplingtime = req.Ch0_samplingtime
|
|
}
|
|
if req.Ch0_sensorparameters != 0 {
|
|
conf.MQTT.Ch0_sensorparameters = req.Ch0_sensorparameters
|
|
}
|
|
if req.Ch1_measurpoint != "" {
|
|
conf.MQTT.Ch1_measurpoint = req.Ch1_measurpoint
|
|
}
|
|
if req.Ch1_measurpointdirection != "" {
|
|
conf.MQTT.Ch1_measurpointdirection = req.Ch1_measurpointdirection
|
|
}
|
|
if req.Ch1_samplingfrequency != 0 {
|
|
conf.MQTT.Ch1_samplingfrequency = req.Ch1_samplingfrequency
|
|
}
|
|
if req.Ch1_samplingtime != 0 {
|
|
conf.MQTT.Ch1_samplingtime = req.Ch1_samplingtime
|
|
}
|
|
if req.Ch1_sensorparameters != 0 {
|
|
conf.MQTT.Ch1_sensorparameters = req.Ch1_sensorparameters
|
|
}
|
|
if req.Ch2_measurpoint != "" {
|
|
conf.MQTT.Ch2_measurpoint = req.Ch2_measurpoint
|
|
}
|
|
if req.Ch2_measurpointdirection != "" {
|
|
conf.MQTT.Ch2_measurpointdirection = req.Ch2_measurpointdirection
|
|
}
|
|
if req.Ch2_samplingfrequency != 0 {
|
|
conf.MQTT.Ch2_samplingfrequency = req.Ch2_samplingfrequency
|
|
}
|
|
if req.Ch2_samplingtime != 0 {
|
|
conf.MQTT.Ch2_samplingtime = req.Ch2_samplingtime
|
|
}
|
|
if req.Ch2_sensorparameters != 0 {
|
|
conf.MQTT.Ch2_sensorparameters = req.Ch2_sensorparameters
|
|
}
|
|
if req.Ch3_measurpoint != "" {
|
|
conf.MQTT.Ch3_measurpoint = req.Ch3_measurpoint
|
|
}
|
|
if req.Ch3_measurpointdirection != "" {
|
|
conf.MQTT.Ch3_measurpointdirection = req.Ch3_measurpointdirection
|
|
}
|
|
if req.Ch3_samplingfrequency != 0 {
|
|
conf.MQTT.Ch3_samplingfrequency = req.Ch3_samplingfrequency
|
|
}
|
|
if req.Ch3_samplingtime != 0 {
|
|
conf.MQTT.Ch3_samplingtime = req.Ch3_samplingtime
|
|
}
|
|
if req.Ch3_sensorparameters != 0 {
|
|
conf.MQTT.Ch3_sensorparameters = req.Ch3_sensorparameters
|
|
}
|
|
if req.Ch4_measurpoint != "" {
|
|
conf.MQTT.Ch4_measurpoint = req.Ch4_measurpoint
|
|
}
|
|
if req.Ch4_measurpointdirection != "" {
|
|
conf.MQTT.Ch4_measurpointdirection = req.Ch4_measurpointdirection
|
|
}
|
|
if req.Ch4_samplingfrequency != 0 {
|
|
conf.MQTT.Ch4_samplingfrequency = req.Ch4_samplingfrequency
|
|
}
|
|
if req.Ch4_samplingtime != 0 {
|
|
conf.MQTT.Ch4_samplingtime = req.Ch4_samplingtime
|
|
}
|
|
if req.Ch4_sensorparameters != 0 {
|
|
conf.MQTT.Ch4_sensorparameters = req.Ch4_sensorparameters
|
|
}
|
|
if req.Ch5_measurpoint != "" {
|
|
conf.MQTT.Ch5_measurpoint = req.Ch5_measurpoint
|
|
}
|
|
if req.Ch5_measurpointdirection != "" {
|
|
conf.MQTT.Ch5_measurpointdirection = req.Ch5_measurpointdirection
|
|
}
|
|
if req.Ch5_samplingfrequency != 0 {
|
|
conf.MQTT.Ch5_samplingfrequency = req.Ch5_samplingfrequency
|
|
}
|
|
if req.Ch5_samplingtime != 0 {
|
|
conf.MQTT.Ch5_samplingtime = req.Ch5_samplingtime
|
|
}
|
|
if req.Ch5_sensorparameters != 0 {
|
|
conf.MQTT.Ch5_sensorparameters = req.Ch5_sensorparameters
|
|
}
|
|
if req.Ch6_measurpoint != "" {
|
|
conf.MQTT.Ch6_measurpoint = req.Ch6_measurpoint
|
|
}
|
|
if req.Ch6_measurpointdirection != "" {
|
|
conf.MQTT.Ch6_measurpointdirection = req.Ch6_measurpointdirection
|
|
}
|
|
if req.Ch6_samplingfrequency != 0 {
|
|
conf.MQTT.Ch6_samplingfrequency = req.Ch6_samplingfrequency
|
|
}
|
|
if req.Ch6_samplingtime != 0 {
|
|
conf.MQTT.Ch6_samplingtime = req.Ch6_samplingtime
|
|
}
|
|
if req.Ch6_sensorparameters != 0 {
|
|
conf.MQTT.Ch6_sensorparameters = req.Ch6_sensorparameters
|
|
}
|
|
if req.Ch7_measurpoint != "" {
|
|
conf.MQTT.Ch7_measurpoint = req.Ch7_measurpoint
|
|
}
|
|
if req.Ch7_measurpointdirection != "" {
|
|
conf.MQTT.Ch7_measurpointdirection = req.Ch7_measurpointdirection
|
|
}
|
|
if req.Ch7_samplingfrequency != 0 {
|
|
conf.MQTT.Ch7_samplingfrequency = req.Ch7_samplingfrequency
|
|
}
|
|
if req.Ch7_samplingtime != 0 {
|
|
conf.MQTT.Ch7_samplingtime = req.Ch7_samplingtime
|
|
}
|
|
if req.Ch7_sensorparameters != 0 {
|
|
conf.MQTT.Ch7_sensorparameters = req.Ch7_sensorparameters
|
|
}
|
|
|
|
if err := conf.Write(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("修改netcfg.yaml文件失败: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
if req.Addresses != "" || req.Via != "" {
|
|
if err := netconf.Write(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("修改config.toml文件失败: %s", err.Error()))
|
|
return
|
|
}
|
|
command := "sleep 1 && netplan apply && sleep 60 && systemctl restart gateway.service"
|
|
cmd := exec.Command("/bin/bash", "-c", command)
|
|
go cmd.Run()
|
|
}
|
|
|
|
c.JSON(http.StatusOK, "OK")
|
|
|
|
}
|
|
|
|
func (h *Handler) GetToken(c *gin.Context) {
|
|
user := User{}
|
|
err := c.BindJSON(&user)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, fmt.Sprintf("参数错误: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
hash := md5.New()
|
|
origin := []byte(user.Name)
|
|
salt := []byte(user.Password)
|
|
_, err = hash.Write(origin)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("写hash origin错误: %s", err.Error()))
|
|
return
|
|
}
|
|
_, err = hash.Write(salt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("写hash salt错误: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
// secret := string(hash.Sum(nil))
|
|
secret := hex.EncodeToString(hash.Sum(nil))
|
|
conf := model.Config{}
|
|
if err := conf.Read(); err != nil {
|
|
c.String(http.StatusInternalServerError, fmt.Sprintf("读取secret失败: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
if secret != conf.Sys.Secret {
|
|
c.String(http.StatusInternalServerError, "用户名或密码错误")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, "OK")
|
|
}
|
|
|
|
func (h *Handler) Upgrade(c *gin.Context) {
|
|
// 最大10M
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(10<<20))
|
|
header, err := c.FormFile("upload")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, fmt.Sprintf("上传文件失败: %s", err.Error()))
|
|
return
|
|
}
|
|
c.SaveUploadedFile(header, header.Filename)
|
|
|
|
path, err := filepath.Abs(header.Filename)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("获取绝对路径失败: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
command := "/usr/local/gateway-ui/upgrade.sh " + path
|
|
// h.logger.Info(command)
|
|
cmd := exec.Command("/bin/bash", "-c", command)
|
|
var stderr bytes.Buffer
|
|
cmd.Stderr = &stderr
|
|
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, fmt.Sprintf("执行升级脚本失败: %s", stderr.String()))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, "OK")
|
|
}
|