dashboard/controller/unisc/httphandle.go

77 lines
1.5 KiB
Go

package unisc
import (
"dashboard/dao/dadis/unisd"
"dashboard/models"
"net/http"
"reflect"
"time"
"github.com/gin-gonic/gin"
)
type httpHandle struct {
httpC chan *models.UnisHttpRequest
}
func newhttpHandle(httpc chan *models.UnisHttpRequest) *httpHandle {
res := new(httpHandle)
res.httpC = httpc
return res
}
func httpSendWithExpire(httpc chan *models.UnisHttpRequest, data *models.UnisHttpRequest) (*models.UnisHttpResponse, error) {
resc := make(chan *models.UnisHttpResponse)
data.ResC = resc
select {
case httpc <- data:
case <-time.After(2 * time.Second):
return nil, models.ErrorTimeOut
}
select {
case resp := <-resc:
return resp, nil
case <-time.After(2 * time.Second):
return nil, models.ErrorTimeOut
}
}
func (u *httpHandle) stationConfig(c *gin.Context) error {
config := new(models.StationConfigParams)
if err := c.ShouldBindJSON(config); err != nil {
return err
}
if !config.StationConfig.ConfigType {
res, ok := unisd.UnisDataGet(string(unisd.DadisKey_UnisStationInfo))
if ok {
oldCfg := res.(*models.StationConfigParams)
oldCfg.StationConfig.ConfigType = config.StationConfig.ConfigType
if reflect.DeepEqual(config, oldCfg) {
c.JSON(http.StatusOK, models.UnisHttpResponseOk)
return nil
}
}
}
req := &models.UnisHttpRequest{
Id: models.UnisHttpUrl(c.Request.URL.String()).GetMsgId(),
Msg: config,
}
resp, err := httpSendWithExpire(u.httpC, req)
if err != nil {
return err
}
c.JSON(http.StatusOK, resp)
return nil
}