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 := getUnisHttpReqFromContext(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 }