Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

192 rader
5.1 KiB

  1. package appcontext
  2. import (
  3. "fmt"
  4. "github.com/AFASystems/presence/internal/pkg/model"
  5. "github.com/mitchellh/mapstructure"
  6. )
  7. // AppState provides centralized access to application state
  8. type AppState struct {
  9. beacons model.BeaconsList
  10. httpResults model.HTTPResultList
  11. settings model.Settings
  12. beaconEvents model.BeaconEventList
  13. beaconsLookup map[string]string
  14. }
  15. // NewAppState creates a new application context AppState with default values
  16. func NewAppState() *AppState {
  17. return &AppState{
  18. beacons: model.BeaconsList{
  19. Beacons: make(map[string]model.Beacon),
  20. },
  21. httpResults: model.HTTPResultList{
  22. Results: make(map[string]model.HTTPResult),
  23. },
  24. settings: model.Settings{
  25. ID: 1,
  26. CurrentAlgorithm: "filter", // possible values filter or AI
  27. LocationConfidence: 4,
  28. LastSeenThreshold: 15,
  29. BeaconMetricSize: 30,
  30. HASendInterval: 5,
  31. HASendChangesOnly: false,
  32. RSSIEnforceThreshold: false,
  33. RSSIMinThreshold: 100,
  34. },
  35. beaconEvents: model.BeaconEventList{
  36. Beacons: make(map[string]model.BeaconEvent),
  37. },
  38. beaconsLookup: make(map[string]string),
  39. }
  40. }
  41. // GetBeacons returns thread-safe access to beacons list
  42. func (m *AppState) GetBeacons() *model.BeaconsList {
  43. return &m.beacons
  44. }
  45. // GetSettings returns thread-safe access to settings
  46. func (m *AppState) GetSettings() *model.Settings {
  47. return &m.settings
  48. }
  49. // GetBeaconEvents returns thread-safe access to beacon events
  50. func (m *AppState) GetBeaconEvents() *model.BeaconEventList {
  51. return &m.beaconEvents
  52. }
  53. // GetBeaconsLookup returns thread-safe access to beacon lookup map
  54. func (m *AppState) GetBeaconsLookup() map[string]string {
  55. return m.beaconsLookup
  56. }
  57. // AddBeaconToLookup adds a beacon ID to the lookup map
  58. func (m *AppState) AddBeaconToLookup(id, value string) {
  59. m.beaconsLookup[id] = value
  60. }
  61. // RemoveBeaconFromLookup removes a beacon ID from the lookup map
  62. func (m *AppState) RemoveBeaconFromLookup(id string) {
  63. delete(m.beaconsLookup, id)
  64. }
  65. func (m *AppState) CleanLookup() {
  66. clear(m.beaconsLookup)
  67. }
  68. func (m *AppState) RemoveBeacon(id string) {
  69. m.beacons.Lock.Lock()
  70. delete(m.beacons.Beacons, id)
  71. m.beacons.Lock.Unlock()
  72. }
  73. func (m *AppState) RemoveHTTPResult(id string) {
  74. m.httpResults.Lock.Lock()
  75. delete(m.httpResults.Results, id)
  76. m.httpResults.Lock.Unlock()
  77. }
  78. // BeaconExists checks if a beacon exists in the lookup
  79. func (m *AppState) BeaconExists(id string) (string, bool) {
  80. val, exists := m.beaconsLookup[id]
  81. return val, exists
  82. }
  83. // GetBeacon returns a beacon by ID (thread-safe)
  84. func (m *AppState) GetBeacon(id string) (model.Beacon, bool) {
  85. m.beacons.Lock.RLock()
  86. defer m.beacons.Lock.RUnlock()
  87. beacon, exists := m.beacons.Beacons[id]
  88. return beacon, exists
  89. }
  90. // GetHTTPResult returns a beacon from HTTP results by ID (thread-safe)
  91. func (m *AppState) GetHTTPResult(id string) (model.HTTPResult, bool) {
  92. m.httpResults.Lock.RLock()
  93. defer m.httpResults.Lock.RUnlock()
  94. beacon, exists := m.httpResults.Results[id]
  95. return beacon, exists
  96. }
  97. // UpdateHTTPResult updates a beacon in the list (thread-safe)
  98. func (m *AppState) UpdateHTTPResult(id string, beacon model.HTTPResult) {
  99. m.httpResults.Lock.Lock()
  100. defer m.httpResults.Lock.Unlock()
  101. m.httpResults.Results[id] = beacon
  102. }
  103. // UpdateBeacon updates a beacon in the list (thread-safe)
  104. func (m *AppState) UpdateBeacon(id string, beacon model.Beacon) {
  105. m.beacons.Lock.Lock()
  106. defer m.beacons.Lock.Unlock()
  107. m.beacons.Beacons[id] = beacon
  108. }
  109. // GetBeaconEvent returns a beacon event by ID (thread-safe)
  110. func (m *AppState) GetBeaconEvent(id string) (model.BeaconEvent, bool) {
  111. m.beaconEvents.Lock.RLock()
  112. defer m.beaconEvents.Lock.RUnlock()
  113. event, exists := m.beaconEvents.Beacons[id]
  114. return event, exists
  115. }
  116. // UpdateBeaconEvent updates a beacon event in the list (thread-safe)
  117. func (m *AppState) UpdateBeaconEvent(id string, event model.BeaconEvent) {
  118. m.beaconEvents.Lock.Lock()
  119. defer m.beaconEvents.Lock.Unlock()
  120. m.beaconEvents.Beacons[id] = event
  121. }
  122. // GetAllBeacons returns a copy of all beacons
  123. func (m *AppState) GetAllBeacons() map[string]model.Beacon {
  124. m.beacons.Lock.RLock()
  125. defer m.beacons.Lock.RUnlock()
  126. beacons := make(map[string]model.Beacon)
  127. for id, beacon := range m.beacons.Beacons {
  128. beacons[id] = beacon
  129. }
  130. return beacons
  131. }
  132. // GetAllHttpResults returns a copy of all beacons
  133. func (m *AppState) GetAllHttpResults() map[string]model.HTTPResult {
  134. m.httpResults.Lock.RLock()
  135. defer m.httpResults.Lock.RUnlock()
  136. beacons := make(map[string]model.HTTPResult)
  137. for id, beacon := range m.httpResults.Results {
  138. beacons[id] = beacon
  139. }
  140. return beacons
  141. }
  142. // GetBeaconCount returns the number of tracked beacons
  143. func (m *AppState) GetBeaconCount() int {
  144. m.beacons.Lock.RLock()
  145. defer m.beacons.Lock.RUnlock()
  146. return len(m.beacons.Beacons)
  147. }
  148. // GetSettingsValue returns current settings as a value
  149. func (m *AppState) GetSettingsValue() model.Settings {
  150. return m.settings
  151. }
  152. // UpdateSettings updates the system settings (thread-safe)
  153. func (m *AppState) UpdateSettings(settings map[string]any) {
  154. if err := mapstructure.Decode(settings, &m.settings); err != nil {
  155. fmt.Printf("Error in persisting settings: %v\n", err)
  156. }
  157. }