Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

73 lignes
2.2 KiB

  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  6. "github.com/AFASystems/presence/internal/pkg/model"
  7. "github.com/redis/go-redis/v9"
  8. )
  9. type RedisHashable interface {
  10. RedisHashable() (map[string]any, error)
  11. model.BeaconEvent | model.HTTPLocation
  12. }
  13. func persistBeaconValkey[T RedisHashable](id string, msg T, client *redis.Client, ctx context.Context) error {
  14. key := fmt.Sprintf("beacon:%s", id)
  15. hashM, err := msg.RedisHashable()
  16. if err != nil {
  17. fmt.Println("Error in converting location into hashmap for Redis insert: ", err)
  18. return err
  19. }
  20. if err := client.HSet(ctx, key, hashM).Err(); err != nil {
  21. fmt.Println("Error in persisting set in Redis key: ", key)
  22. return err
  23. }
  24. if err := client.SAdd(ctx, "beacons", key).Err(); err != nil {
  25. fmt.Println("Error in adding beacon to the beacons list for get all operation: ", err)
  26. return err
  27. }
  28. return nil
  29. }
  30. func LocationToBeaconService(msg model.HTTPLocation, appState *appcontext.AppState, client *redis.Client, ctx context.Context) error {
  31. id := msg.ID
  32. beacon, ok := appState.GetBeacon(id)
  33. if !ok {
  34. appState.UpdateBeacon(id, model.Beacon{ID: id, Location: msg.Location, Distance: msg.Distance, LastSeen: msg.LastSeen, PreviousConfidentLocation: msg.PreviousConfidentLocation})
  35. } else {
  36. beacon.ID = id
  37. beacon.Location = msg.Location
  38. beacon.Distance = msg.Distance
  39. beacon.LastSeen = msg.LastSeen
  40. beacon.PreviousConfidentLocation = msg.PreviousConfidentLocation
  41. appState.UpdateBeacon(id, beacon)
  42. }
  43. if err := persistBeaconValkey(id, msg, client, ctx); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. func EventToBeaconService(msg model.BeaconEvent, appState *appcontext.AppState, client *redis.Client, ctx context.Context) error {
  49. id := msg.ID
  50. beacon, ok := appState.GetBeacon(id)
  51. if !ok {
  52. appState.UpdateBeacon(id, model.Beacon{ID: id, BeaconType: msg.Type, HSBattery: int64(msg.Battery), Event: msg.Event})
  53. } else {
  54. beacon.ID = id
  55. beacon.BeaconType = msg.Type
  56. beacon.HSBattery = int64(msg.Battery)
  57. beacon.Event = msg.Event
  58. appState.UpdateBeacon(id, beacon)
  59. }
  60. if err := persistBeaconValkey(id, msg, client, ctx); err != nil {
  61. return err
  62. }
  63. return nil
  64. }