From e871758bd82d9a4fbce42e43c9665d869c240dfb Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@iotlink.com>
Date: 星期一, 21 十月 2019 15:12:11 +0800
Subject: [PATCH] feat: add api for system clock update

---
 controllers/syssetcont.go |  214 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 156 insertions(+), 58 deletions(-)

diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index 977aeb5..6cd0e0a 100644
--- a/controllers/syssetcont.go
+++ b/controllers/syssetcont.go
@@ -1,45 +1,54 @@
 package controllers
 
 import (
-	"basic.com/dbapi.git"
-	"github.com/gin-gonic/gin"
 	"webserver/extend/code"
 	"webserver/extend/config"
+	"webserver/extend/sys"
 	"webserver/extend/util"
+
+	"basic.com/dbapi.git"
+	"github.com/gin-gonic/gin"
 )
 
 type SysSetController struct {
-
 }
 
 type LocalConfigVo struct {
-	Id string  `json:"id"`
-	AlarmIp string `json:"alarm_ip,omitempty" example:"192.168.1.182"`
-	AlarmPort int `json:"alarm_port,omitempty" example:"22122"`
-	AlarmThresholdType int `json:"alarm_threshold_type"`//鎶ヨ闃堝�肩被鍨嬭缃細1:鎸夋渶楂樺垎,2:鎸夐槇鍊间互涓�
-	AlarmThreshold int `json:"alarm_threshold"`//鎶ヨ闃堝��
-	WebPicIp string `json:"web_pic_ip,omitempty" example:""`
-	WebPicPort int `json:"web_pic_port,omitempty" example:"22122"`
-	EsPicIp string `json:"es_pic_ip,omitempty" example:""`
-	EsPicPort  int `json:"es_pic_port,omitempty" example:"22122"`
-	CutMaxDuration int `json:"cut_max_duration,omitempty" example:"20"`
-	CutMinDuration int `json:"cut_min_duration,omitempty" example:"5"`
-	ServerId string `json:"server_id,omitempty" example:"鍒嗘瀽璁惧id"`
-	ServerName string `json:"server_name,omitempty" example:"鍒嗘瀽璁惧鍚嶇О"`
-	ServerType int `json:"server_type,omitempty" example:"1"`
-	Reserved string `json:"reserved,omitempty" example:"1"`
-	RealMax int `json:"real_max"`//瀹炴椂澶勭悊鐨勬渶澶ц矾鏁�
+	Id                 string `json:"id"`
+	AlarmIp            string `json:"alarm_ip,omitempty" example:"192.168.1.182"`
+	AlarmPort          int    `json:"alarm_port,omitempty" example:"22122"`
+	AlarmThresholdType int    `json:"alarm_threshold_type"` //鎶ヨ闃堝�肩被鍨嬭缃細1:鎸夋渶楂樺垎,2:鎸夐槇鍊间互涓�
+	AlarmThreshold     int    `json:"alarm_threshold"`      //鎶ヨ闃堝��
+	WebPicIp           string `json:"web_pic_ip,omitempty" example:""`
+	WebPicPort         int    `json:"web_pic_port,omitempty" example:"22122"`
+	EsPicIp            string `json:"es_pic_ip,omitempty" example:""`
+	EsPicPort          int    `json:"es_pic_port,omitempty" example:"22122"`
+	CutMaxDuration     int    `json:"cut_max_duration,omitempty" example:"20"`
+	CutMinDuration     int    `json:"cut_min_duration,omitempty" example:"5"`
+	ServerId           string `json:"server_id,omitempty" example:"鍒嗘瀽璁惧id"`
+	ServerName         string `json:"server_name,omitempty" example:"鍒嗘瀽璁惧鍚嶇О"`
+	ServerType         int    `json:"server_type,omitempty" example:"1"`
+	Reserved           string `json:"reserved,omitempty" example:"1"`
+	RealMax            int    `json:"real_max"` //瀹炴椂澶勭悊鐨勬渶澶ц矾鏁�
 }
 
 type Gb28181ConfigVo struct {
-	Id string `json:"Id"`
-	ServerIp string `json:"ServerIp" example:"鍥芥爣鏈嶅姟鍣↖P"`
-	ServerPort int `json:"ServerPort" example:"骞冲彴鏈嶅姟绔彛:7060"`
-	PublicId string `json:"PublicId" example:"骞冲彴鏈嶅姟Id"`
-	GbServerPort int `json:"GbServerPort" example:"鍥芥爣鏈嶅姟绔彛:8060"`
-	IsAuth bool `json:"IsAuth" example:"鏄惁寮�鍚壌鏉�:true"`
-	Password string `json:"Password" example:"瀵嗙爜"`
-	UpdateTime string `json:"UpdateTime"`
+	Id           string `json:"Id"`
+	ServerIp     string `json:"ServerIp" example:"鍥芥爣鏈嶅姟鍣↖P"`
+	ServerPort   int    `json:"ServerPort" example:"骞冲彴鏈嶅姟绔彛:7060"`
+	PublicId     string `json:"PublicId" example:"骞冲彴鏈嶅姟Id"`
+	GbServerPort int    `json:"GbServerPort" example:"鍥芥爣鏈嶅姟绔彛:8060"`
+	IsAuth       bool   `json:"IsAuth" example:"鏄惁寮�鍚壌鏉�:true"`
+	Password     string `json:"Password" example:"瀵嗙爜"`
+	UpdateTime   string `json:"UpdateTime"`
+}
+
+type SysClockConfigVo struct {
+	TimeZone  string `json:"timeZone" example:"鏃跺尯"`
+	NTP       bool   `json:"ntp" example:"NTP鏍℃椂"`
+	NTPServer string `json:"ntpServer" example:"NTP鏈嶅姟鍣ㄥ湴鍧�"`
+	Interval  int    `json:"interval" example:"鑷姩鏍℃椂闂撮殧"`
+	NewTime   string `json:"newTime" example:"鎵嬪姩鎸囧畾鏃堕棿"`
 }
 
 // @Summary 瀛樺偍淇℃伅鏌ヨ
@@ -50,13 +59,13 @@
 // @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
 // @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
 // @Router /data/api-v/sysset/alarmServerShow [GET]
-func (sset SysSetController) AlarmServerShow(c *gin.Context){
+func (sset SysSetController) AlarmServerShow(c *gin.Context) {
 	var api dbapi.SysSetApi
 	b, sysconf := api.GetServerInfo()
-	if b{
-		util.ResponseFormat(c,code.Success,sysconf)
+	if b {
+		util.ResponseFormat(c, code.Success, sysconf)
 	} else {
-		util.ResponseFormat(c,code.ComError,"鏌ヨ澶辫触")
+		util.ResponseFormat(c, code.ComError, "鏌ヨ澶辫触")
 	}
 }
 
@@ -72,17 +81,17 @@
 func (sset SysSetController) AlarmEdit(c *gin.Context) {
 	var args LocalConfigVo
 	err := c.BindJSON(&args)
-	if err !=nil {
-		util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎")
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎")
 		return
 	}
 	var api dbapi.SysSetApi
 	paramBody := util.Struct2Map(args)
-	b,data := api.AlarmEdit(paramBody)
+	b, data := api.AlarmEdit(paramBody)
 	if b {
-		util.ResponseFormat(c,code.Success,data)
-	}else {
-		util.ResponseFormat(c,code.ComError,"淇濆瓨澶辫触")
+		util.ResponseFormat(c, code.Success, data)
+	} else {
+		util.ResponseFormat(c, code.ComError, "淇濆瓨澶辫触")
 	}
 }
 
@@ -94,7 +103,7 @@
 // @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
 // @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
 // @Router /data/api-v/sysset/videoLenShow [GET]
-func (sset SysSetController) VideoLenShow(c *gin.Context){
+func (sset SysSetController) VideoLenShow(c *gin.Context) {
 	var api dbapi.SysSetApi
 	b, sysconf := api.GetServerInfo()
 	if b {
@@ -104,7 +113,7 @@
 		// 瀛樺湪鍒欎慨鏀�
 		util.ResponseFormat(c, code.Success, resData)
 	} else {
-		util.ResponseFormat(c,code.ComError,"鏌ヨ澶辫触")
+		util.ResponseFormat(c, code.ComError, "鏌ヨ澶辫触")
 	}
 }
 
@@ -125,11 +134,11 @@
 	max_video_len := c.Request.FormValue("max_video_len")
 	min_video_len := c.Request.FormValue("min_video_len")
 	var api dbapi.SysSetApi
-	b,data := api.VideoLenEdit(max_video_len,min_video_len)
-	if b{
-		util.ResponseFormat(c,code.UpdateSuccess,data)
+	b, data := api.VideoLenEdit(max_video_len, min_video_len)
+	if b {
+		util.ResponseFormat(c, code.UpdateSuccess, data)
 	} else {
-		util.ResponseFormat(c,code.ComError,"淇濆瓨澶辫触")
+		util.ResponseFormat(c, code.ComError, "淇濆瓨澶辫触")
 	}
 }
 
@@ -146,8 +155,8 @@
 	b, sysconf := api.GetServerInfo()
 	resData := make(map[string]string, 0)
 	if !b { // 鏌ヨ鏄惁瀛樺湪
-		util.ResponseFormat(c, code.ServiceInsideError,"鏌ヨ澶辫触")
-	}else {
+		util.ResponseFormat(c, code.ServiceInsideError, "鏌ヨ澶辫触")
+	} else {
 		resData["server_id"] = sysconf.GetServerId()
 		resData["server_name"] = sysconf.GetServerName()
 		ipv4, mask, _ := util.GetLocalIP(config.Server.NetworkAdapter)
@@ -178,20 +187,19 @@
 // @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
 // @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
 // @Router /data/api-v/sysset/saveDevInfo [POST]
-func (sset SysSetController) SaveDevInfo(c *gin.Context){
+func (sset SysSetController) SaveDevInfo(c *gin.Context) {
 
 	dev_name := c.PostForm("server_name")
 	var api dbapi.SysSetApi
-	paramBody :=make(map[string]interface{},0)
+	paramBody := make(map[string]interface{}, 0)
 	paramBody["serverName"] = dev_name
-	b,data := api.SaveServerInfo(paramBody)
+	b, data := api.SaveServerInfo(paramBody)
 	if b { // 鏌ヨ鏄惁瀛樺湪
-		util.ResponseFormat(c,code.Success,data)
-	} else {  // 瀛樺湪鍙婁慨鏀� 涓嶅瓨鍦� 鍗虫坊鍔�
-		util.ResponseFormat(c,code.ComError,"淇濆瓨澶辫触")
+		util.ResponseFormat(c, code.Success, data)
+	} else { // 瀛樺湪鍙婁慨鏀� 涓嶅瓨鍦� 鍗虫坊鍔�
+		util.ResponseFormat(c, code.ComError, "淇濆瓨澶辫触")
 	}
 }
-
 
 // @Summary GB28181璁剧疆鏌ヨ
 // @Description GB28181璁剧疆淇℃伅鏌ヨ
@@ -205,9 +213,9 @@
 	var api dbapi.SysSetApi
 	b, data := api.Gb28181ConfigShow()
 	if b {
-		util.ResponseFormat(c,code.Success,data)
+		util.ResponseFormat(c, code.Success, data)
 	} else {
-		util.ResponseFormat(c,code.ComError,"鏌ヨ澶辫触")
+		util.ResponseFormat(c, code.ComError, "鏌ヨ澶辫触")
 	}
 }
 
@@ -223,16 +231,106 @@
 func (sset SysSetController) Gb28181ConfigEdit(c *gin.Context) {
 	var args Gb28181ConfigVo
 	err := c.BindJSON(&args)
-	if err !=nil {
-		util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎")
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎")
 		return
 	}
 	var api dbapi.SysSetApi
 	paramBody := util.Struct2Map(args)
 	b, data := api.Gb28181ConfigEdit(paramBody)
 	if b {
-		util.ResponseFormat(c,code.UpdateSuccess,data)
+		util.ResponseFormat(c, code.UpdateSuccess, data)
 	} else {
-		util.ResponseFormat(c,code.ComError,"鏇存柊澶辫触")
+		util.ResponseFormat(c, code.ComError, "鏇存柊澶辫触")
 	}
 }
+
+// @Summary 鑾峰彇鏃堕棿閰嶇疆
+// @Description  绯荤粺鏃堕棿閰嶇疆淇℃伅
+// @Accept  json
+// @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/clockInfo [GET]
+func (sset SysSetController) GetClockinfo(c *gin.Context) {
+	resData := make(map[string]interface{}, 0)
+	resData["time_zone"], resData["local_time"] = sys.TimeZone()
+	resData["ntp"], resData["ntp_server"], resData["interval"] = sys.NTPConfig()
+
+	util.ResponseFormat(c, code.Success, resData)
+}
+
+// @Summary 娴嬭瘯鍚屾鏃堕棿
+// @Description 娴嬭瘯鍚屾鏃堕棿鏈嶅姟鍣ㄦ槸鍚﹀彲鐢�
+// @Accept  json
+// @Produce json
+// @Tags sysset
+// @Param server query string true "鏃堕棿鏈嶅姟鍣╥p"
+// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
+// @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
+// @Router /data/api-v/sysset/ntpTest [GET]
+func (sset SysSetController) TestNTPServer(c *gin.Context) {
+	ntpServer := c.Query("server")
+
+	if root := sys.CheckRootPermissions(); !root {
+		util.ResponseFormat(c, code.ServiceInsideError, "鏈嶅姟鍣ㄦ病鏈変慨鏀规椂闂寸殑鏉冮檺")
+		return
+	}
+
+	isIP, err := sys.RunNTPDate(ntpServer)
+	if !isIP {
+		util.ResponseFormat(c, code.RequestParamError, "鎸囧畾鐨勬湇鍔″櫒鍦板潃閿欒")
+		return
+	}
+
+	if err == nil {
+		util.ResponseFormat(c, code.Success, "")
+	} else {
+		util.ResponseFormat(c, code.TaskStoped, "")
+	}
+}
+
+// @Summary 璁剧疆绯荤粺鏃堕棿
+// @Description 鏍℃绯荤粺鏃堕棿
+// @Accept  json
+// @Produce json
+// @Tags sysset
+// @Param config body controllers.SysClockConfigVo true "鏍℃椂淇℃伅"
+// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
+// @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
+// @Router /data/api-v/sysset/updateClock [POST]
+func (sset SysSetController) SetSysClock(c *gin.Context) {
+	var args SysClockConfigVo
+	err := c.BindJSON(&args)
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎")
+		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, "鏃跺尯鍙傛暟閿欒")
+			return
+		}
+	}
+
+	if !args.NTP {
+		if r := sys.SetLocalTime(args.NewTime); !r {
+			util.ResponseFormat(c, code.RequestParamError, "鎸囧畾鐨勬椂闂村弬鏁伴敊璇�")
+			return
+		}
+	} else {
+		if r := sys.EnableNTPCron(args.NTPServer, args.Interval); !r {
+			util.ResponseFormat(c, code.RequestParamError, "鎸囧畾鐨勬湇鍔″櫒鍦板潃閿欒")
+			return
+		}
+	}
+
+	util.ResponseFormat(c, code.UpdateSuccess, "閰嶇疆鎴愬姛")
+}

--
Gitblit v1.8.0