package upstreamimpl import ( "context" "fmt" "git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/logic/upstreamimpl/dx" "git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/model" "git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/service" "regexp" "time" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gsvc" ) // XML命名空间正则 var xmlNamespaceRegex = regexp.MustCompile(`xmlns[^=]*="[^"]*"\s*|:web:`) type sSimDX struct { channelConfig *model.SimChannelConfigItemCache client *dx.Client } func NewSimDX() *sSimDX { return &sSimDX{} } func init() { s := NewSimDX() service.UpstreamAPI().RegisterTemp("sim_dx_v1", s) service.UpstreamAPI().RegisterTemp("sim_dx_v3", s) service.RegisterSimDX(s) } func (s *sSimDX) NewTemp(ctx context.Context, channelConfig *model.SimChannelConfigItemCache) model.SimCardChannelI { client := dx.NewClient(dx.Config{ BaseURL: dx.DefaultBaseURL, AppKey: channelConfig.Channel.AppKey, AppSecret: channelConfig.Channel.AppSecret, }) return &sSimDX{ channelConfig: channelConfig, client: client, } } func (s *sSimDX) getUpstreamUrl(ctx context.Context, serviceUrl string) (string, error) { address, err := g.Cfg().Get(ctx, "gateway.address") if err != nil { return "", err } return fmt.Sprintf("http://%s%s", address, serviceUrl), nil // return fmt.Sprintf("http://%s%s", "127.0.0.1:8110", serviceUrl), nil } func (s *sSimDX) getUpstreamTimeout(ctx context.Context) time.Duration { timeout, _ := g.Cfg().Get(ctx, "gateway.timeout") if timeout == nil { return 10 * time.Second } if d := timeout.Duration(); d > 0 { return d } if seconds := timeout.Int(); seconds > 0 { return time.Duration(seconds) * time.Second } return 10 * time.Second } func (s *sSimDX) SimBase(ctx context.Context, input *model.SimBaseReqJson) (model.SimBaseResJson, error) { r := g.RequestFromCtx(ctx) if r.Method != "POST" { return nil, gerror.New("method not allowed") } path := r.URL.Path if path == "" { return nil, gerror.New("path is required") } url, err := s.getUpstreamUrl(ctx, path) if err != nil { g.Log().Debugf(ctx, "SimBase getUpstreamUrl error %+v, ", err) return nil, err } client := g.Client() client.SetDiscovery(gsvc.GetRegistry()) client.SetTimeout(s.getUpstreamTimeout(ctx) * time.Duration(len(input.SimKeys))) resp, err := client.Post(ctx, url, input) if err != nil { g.Log().Errorf(ctx, "SimBase post error %+v,url %s, params %+v, ", err, url, input) return nil, err } return readGatewayResponse(resp) }