You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

48 lines
1.3 KiB

  1. package location
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "fmt"
  6. "net/http"
  7. "github.com/AFASystems/presence/internal/pkg/apiclient"
  8. "github.com/AFASystems/presence/internal/pkg/config"
  9. "github.com/AFASystems/presence/internal/pkg/model"
  10. )
  11. // Inferencer returns inferred positions (e.g. from an AI/ML service).
  12. type Inferencer interface {
  13. Infer(ctx context.Context, cfg *config.Config) (model.PositionResponse, error)
  14. }
  15. // DefaultInferencer uses apiclient to get token and call the inference API.
  16. type DefaultInferencer struct {
  17. Client *http.Client
  18. Token string
  19. }
  20. // NewDefaultInferencer creates an inferencer with optional TLS skip verify (e.g. from config.TLSInsecureSkipVerify).
  21. func NewDefaultInferencer(skipTLSVerify bool) *DefaultInferencer {
  22. tr := &http.Transport{}
  23. tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  24. return &DefaultInferencer{
  25. Client: &http.Client{Transport: tr},
  26. Token: "",
  27. }
  28. }
  29. // Infer gets a token and calls the inference API.
  30. func (d *DefaultInferencer) Infer(ctx context.Context, cfg *config.Config) (model.PositionResponse, error) {
  31. if d.Token == "" {
  32. fmt.Printf("getting token\n")
  33. token, err := apiclient.GetToken(ctx, cfg, d.Client)
  34. if err != nil {
  35. return model.PositionResponse{}, err
  36. }
  37. d.Token = token
  38. }
  39. return apiclient.InferPosition(d.Token, d.Client, cfg)
  40. }