dashboard/controller/unisc/httphandle.go

93 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package unisc
import (
"dashboard/models"
"dashboard/utils"
"net/http"
"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 (u *httpHandle)httpSendWithExpire(req *models.UnisHttpRequest) (*models.UnisHttpResponse, error) {
select {
case u.httpC[0] <- req:
case <-time.After(2 * time.Second):
return nil, models.ErrorSendTimeOut
}
select {
case resp := <-req.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的方式而是使用配置文件启动不同进程的方式
// 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, err := getUnisHttpContextFromContext(c)
if err != nil {
log.Sugar().Errorf("stationConfig get http req from context err: %v", err)
return models.ErrorParamsErr
}
req.SetReqParam(models.UnisHttpUrl(c.Request.URL.String()).GetMsgId(), config)
// req := &models.UnisHttpRequest{
// Id: models.UnisHttpUrl(c.Request.URL.String()).GetMsgId(),
// Msg: config,
// }
resp, err := u.httpSendWithExpire(req)
if err != nil {
log.Sugar().Errorf("stationConfig send or recive err: %v", err)
return err
}
c.JSON(http.StatusOK, resp)
return nil
}