dashboard/controller/unisc/httphandle.go

94 lines
2.1 KiB
Go

package unisc
import (
"dashboard/models"
"dashboard/utils"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
)
type httpHandle struct {
httpC []chan *models.UnisHttpRequest
pool sync.Pool
}
func newhttpHandle(httpc []chan *models.UnisHttpRequest) *httpHandle {
res := new(httpHandle)
res.httpC = httpc
res.pool.New = func() any { return new(models.UnisHttpRequest) }
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.ErrorSendTimeOut
}
select {
case resp := <-resc:
return resp, nil
case <-time.After(2 * time.Second):
return nil, models.ErrorReciveTimeOut
}
}
func (u *httpHandle) stationConfig(c *gin.Context) error {
log, _ := utils.GetLogFromContext(c)
config := new(models.StationConfigParams)
if err := c.ShouldBindJSON(config); err != nil {
log.Sugar().Errorf("stationConfig bind json err: %v", err)
return err
}
channel := c.GetInt(models.GinContextChannel)
if channel > len(u.httpC)-1 {
log.Sugar().Errorf("stationConfig channel err: %d", channel)
return models.ErrorParamsErr
}
// if !config.StationConfig.ConfigType {
// res, ok := unisd.UnisDataGet(unisd.DadisKey_UnisStationInfo.KeyWithChannel(channel))
// if ok {
// oldCfg := res.(*models.StationConfigParams)
// oldCfg.StationConfig.ConfigType = config.StationConfig.ConfigType
// if reflect.DeepEqual(config, oldCfg) {
// log.Sugar().Infof("stationConfig is same")
// c.JSON(http.StatusOK, models.UnisHttpResponseOk)
// return nil
// }
// }
// }
req := u.pool.Get().(*models.UnisHttpRequest)
req.Id = models.UnisHttpUrl(c.Request.URL.String()).GetMsgId()
req.Msg = config
// req := &models.UnisHttpRequest{
// Id: models.UnisHttpUrl(c.Request.URL.String()).GetMsgId(),
// Msg: config,
// }
resp, err := httpSendWithExpire(u.httpC[channel], req)
if err != nil {
log.Sugar().Errorf("stationConfig send or recive err: %v", err)
return err
}
c.JSON(http.StatusOK, resp)
return nil
}