From 863c880b169a788fe74b27187baa2e3f60b72ed2 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@iotlink.com>
Date: 星期三, 30 十月 2019 15:19:23 +0800
Subject: [PATCH] feat: add reboot
---
controllers/panTilt.go | 32 ++++++++++++++++
controllers/syssetcont.go | 21 ++++++++--
extend/sys/system.go | 58 ++++++++--------------------
extend/config/config.go | 2
router/router.go | 5 ++
5 files changed, 71 insertions(+), 47 deletions(-)
diff --git a/controllers/panTilt.go b/controllers/panTilt.go
new file mode 100644
index 0000000..5dd318c
--- /dev/null
+++ b/controllers/panTilt.go
@@ -0,0 +1,32 @@
+package controllers
+
+import (
+ "webserver/extend/code"
+ "webserver/extend/util"
+
+ "github.com/gin-gonic/gin"
+)
+
+type PanTiltController struct {
+}
+
+type PTInstruct struct {
+ ChannelId string `json:"channelId"` //鏈嶅姟鍣╥d
+ PTZType string `json:"ptzType"` //杞鍛ㄦ湡
+ PTZParam int32 `json:"ptzParam"` //寤舵椂鏃堕棿
+}
+
+func (controller PanTiltController) Controlling(c *gin.Context) {
+ var param PTInstruct
+ if err := c.BindJSON(¶m); err != nil {
+ util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎")
+ return
+ }
+
+ b := true
+ if b {
+ util.ResponseFormat(c, code.Success, "")
+ } else {
+ util.ResponseFormat(c, code.ComError, "淇濆瓨澶辫触")
+ }
+}
diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index 1ba05ba..8034844 100644
--- a/controllers/syssetcont.go
+++ b/controllers/syssetcont.go
@@ -318,11 +318,6 @@
return
}
- if root := sys.CheckRootPermissions(); !root {
- util.ResponseFormat(c, code.ServiceInsideError, "鏈嶅姟鍣ㄦ病鏈変慨鏀规椂闂寸殑鏉冮檺")
- return
- }
-
if args.TimeZone != "CST" && args.TimeZone != "UTC" {
if r := sys.SetTimeZone(args.TimeZone); !r {
util.ResponseFormat(c, code.RequestParamError, "鏃跺尯鍙傛暟閿欒")
@@ -367,3 +362,19 @@
func (sset SysSetController) GetSysThresholds(c *gin.Context) {
util.ResponseFormat(c, code.UpdateSuccess, config.Server.SysThresholds)
}
+
+// @Summary 閲嶅惎绯荤粺
+// @Description 閲嶅惎鎿嶄綔绯荤粺
+// @Produce json
+// @Tags sysset
+// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"杩斿洖閿欒淇℃伅", success:false}"
+// @Router /data/api-v/sysset/reboot [GET]
+func (sset SysSetController) RebootOS(c *gin.Context) {
+ if isOk, msg := sys.Reboot(); !isOk {
+ util.ResponseFormat(c, code.UpdateFail, msg)
+ return
+ }
+
+ util.ResponseFormat(c, code.Success, "姝e湪閲嶅惎")
+}
diff --git a/extend/config/config.go b/extend/config/config.go
index 605482a..c775712 100644
--- a/extend/config/config.go
+++ b/extend/config/config.go
@@ -26,7 +26,7 @@
ChannelCount string `mapstructure: "channelCount"` //閫氶亾涓暟
DiskCount string `mapstructure: "diskCount"` //纭洏涓暟
- SysServerPort string `mapstructure: "sysServerPort"`
+ SudoPassword string `mapstructure: "sudoPassword"`
SysThresholds []threshold `mapstructure: "sysThresholds"`
}
diff --git a/extend/sys/system.go b/extend/sys/system.go
index 526093e..cccba7e 100644
--- a/extend/sys/system.go
+++ b/extend/sys/system.go
@@ -1,11 +1,7 @@
package sys
import (
- "encoding/json"
"errors"
- "io/ioutil"
- "net/http"
- "net/url"
"os"
"os/exec"
"strconv"
@@ -18,40 +14,11 @@
"time"
)
-func execRootCommand(cmd string) (bool, string) {
- serverPort := config.Server.SysServerPort
- if serverPort == "" {
- serverPort = "18081"
- }
- sysConfigServer := fmt.Sprintf("http://127.0.0.1:%s/api/v1/command/exec", serverPort)
- resp, err := http.PostForm(sysConfigServer, url.Values{"command": {cmd}})
+func execRootCommand(cmd string) ([]byte, error) {
+ pwd := config.Server.SudoPassword
+ cmdStr := fmt.Sprintf("echo %s | sudo -S %s", pwd, cmd)
- if err != nil {
- return false, err.Error()
- }
-
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return false, err.Error()
- }
-
- type response struct {
- Code int `json:"code"`
- Success bool `json:"success"`
- Msg string `json:"msg"`
- CMD string `json:"cmd"`
- Data string `json:"data"`
- }
-
- var rsp response
- if err := json.Unmarshal(body, &rsp); err != nil {
- return false, err.Error()
- }
-
- fmt.Println(body)
- fmt.Println(rsp)
- return rsp.Success, rsp.Data
+ return exec.Command("/bin/sh", "-c", cmdStr).Output()
}
// 妫�鏌� root鏉冮檺
@@ -132,8 +99,9 @@
}
// # netconfig enp59s0f0 192.168.1.2 255.255.255.0 192.168.1.1 192.168.100.1
cmdStr := fmt.Sprintf("%s %s %s %s %s %s", networkConfigScript, ifname, ipv4, netmask, gateway, dns)
- // return exec.Command("/bin/sh", "-c", cmdStr).Run() == nil
- return execRootCommand(cmdStr)
+ stdout, err := execRootCommand(cmdStr)
+
+ return err == nil, string(stdout)
}
// 閰嶇疆鏃跺尯
@@ -192,8 +160,10 @@
return false
}
- args := []string{"-s", newTime}
- exec.Command("date", args...).Run()
+ // args := []string{"-s", newTime}
+ // exec.Command("date", args...).Run()
+ dateCMD := fmt.Sprintf("date -s \"%s\"", newTime)
+ execRootCommand(dateCMD)
stopNTPCron()
return true
@@ -227,3 +197,9 @@
ntpdate := fmt.Sprintf("/usr/sbin/ntpdate %s", server)
return true, exec.Command("/bin/sh", "-c", ntpdate).Run()
}
+
+func Reboot() (bool, string) {
+ stdout, err := execRootCommand("reboot")
+
+ return err == nil, string(stdout)
+}
diff --git a/router/router.go b/router/router.go
index 154fd75..abc6ffe 100644
--- a/router/router.go
+++ b/router/router.go
@@ -42,6 +42,7 @@
sysMenuController := new(controllers.SysMenuController)
clusterController := new(controllers.ClusterController)
sysRoleController := new(controllers.RoleController)
+ ptController := new(controllers.PanTiltController)
sysApi := r.Group("/data/api-u/sys")
{
@@ -99,6 +100,8 @@
camera.POST("/updateRunEnable", cameraController.UpdateRunEnable)
camera.POST("/changeRunType", cameraController.ChangeRunType)
camera.GET("/getAllCamerasByServer", cameraController.GetAllCamerasByServer)
+
+ camera.POST("/ptControl", ptController.Controlling)
}
cameraTaskArgsApi := r.Group(urlPrefix + "/cameraTaskArgs")
@@ -204,6 +207,8 @@
vsset.GET("/ntpTest", ssController.TestNTPServer)
vsset.GET("/sysinfo", ssController.GetSysInfo)
vsset.GET("/sysThresholds", ssController.GetSysThresholds)
+
+ vsset.GET("/reboot", ssController.RebootOS)
}
//绠楁硶搴撴搷浣�
--
Gitblit v1.8.0