|
- package main
-
- import (
- "context"
- "time"
-
- "github.com/AFASystems/presence/internal/pkg/model"
- presenseredis "github.com/AFASystems/presence/internal/pkg/redis"
- "github.com/redis/go-redis/v9"
- )
-
- func main() {
- ctx := context.Background()
- client := redis.NewClient(&redis.Options{
- Addr: "127.0.0.1:6379",
- Password: "",
- })
- getLikelyLocations(client, ctx)
- }
-
- func getLikelyLocations(client *redis.Client, ctx context.Context) {
- httpRes := model.HTTPResultsList{
- HTTPResults: model.HTTPLocationsList{
- Beacons: make([]model.HTTPLocation, 0),
- },
- }
-
- shouldPersist := false
- beaconsList := presenseredis.LoadBeaconsList(client, ctx)
- settings := presenseredis.LoadSettings(client, ctx)
-
- for _, beacon := range beaconsList {
- length := len(beacon.Beacon_metrics)
- if length == 0 {
- continue
- }
-
- if (int64(time.Now().Unix()) - (beacon.Beacon_metrics[length-1].Timestamp)) > settings.Last_seen_threshold {
- if beacon.Expired_location == "expired" {
- continue
- }
- beacon.Expired_location = "expired" // define type expired
- } else {
- beacon.Expired_location = ""
- }
-
- locList := make(map[string]float64)
- seenWeight := 1.5
- rssiWeight := 0.75
-
- for _, metric := range beacon.Beacon_metrics {
- weightCalc := seenWeight + (rssiWeight * (1.0 - (float64(metric.Rssi) / -100.0)))
- loc, ok := locList[metric.Location]
- if !ok {
- loc = weightCalc
- } else {
- loc = loc + weightCalc
- }
- }
-
- bestName := ""
- ts := 0.0
-
- for name, timesSeen := range locList {
- if timesSeen > ts {
- bestName = name
- ts = timesSeen
- }
- }
-
- bestLocation := model.BestLocation{Name: bestName, Distance: beacon.Beacon_metrics[length-1].Distance, Last_seen: beacon.Beacon_metrics[length-1].Timestamp}
- beacon.Location_history = append(beacon.Location_history, bestName)
-
- if len(beacon.Location_history) > 10 {
- beacon.Location_history = beacon.Location_history[1:]
- }
-
- locationCounts := make(map[string]int)
- for _, loc := range beacon.Location_history {
- locationCounts[loc]++
- }
-
- maxCount := 0
- mostCommonLocation := ""
- for loc, count := range locationCounts {
- if count > maxCount {
- maxCount = count
- mostCommonLocation = loc
- }
- }
-
- if maxCount >= 7 {
- beacon.Previous_location = mostCommonLocation
- if mostCommonLocation == beacon.Previous_confident_location {
- beacon.Location_confidence++
- } else {
- beacon.Location_confidence = 1
- beacon.Previous_confident_location = mostCommonLocation
- }
- }
-
- r := model.HTTPLocation{
- Distance: bestLocation.Distance,
- Name: beacon.Name,
- Beacon_name: beacon.Name,
- Beacon_id: beacon.Beacon_id,
- Beacon_type: beacon.Beacon_type,
- HB_Battery: beacon.HB_Battery,
- HB_ButtonMode: beacon.HB_ButtonMode,
- HB_ButtonCounter: beacon.HB_ButtonCounter,
- Location: bestName,
- Last_seen: bestLocation.Last_seen,
- }
-
- if (beacon.Location_confidence == settings.Location_confidence && beacon.Previous_confident_location != bestLocation.Name) || (beacon.Expired_location == "expired") {
- shouldPersist = true
- beacon.Location_confidence = 0
- location := ""
- if beacon.Expired_location == "expired" {
- location = "expired"
- } else {
- location = bestName
- }
- }
- }
- }
-
- // get likely locations:
- /*
- 1. Locks the http_results list
- 2. inits list to empty struct type -> TODO: what is this list used for
- 3. loops through beacons list -> should be locked?
- 4. check for beacon metrics -> how do you get beacon metrics, I guess it has an array of timestamps
- 5. check for threshold value in the settings
- 5.1. check for property expired location
- 5.2. if location is not expired -> mark it as expired, generate message and send to all clients,
- if clients do not respond close the connection
- 6. Init best location with type Best_location{} -> what is this type
- 7. make locations list -> key: string, val: float64
- 7.1 set weight for seen and rssi
- 7.2 loop over metrics of the beacon -> some alogirthm based on location value
-
- I think the algorithm is recording names of different gateways and their rssi's and then from
- that it checks gateway name and makes decisions based on calculated values
-
- 7.3 writes result in best location and updates list location history with this name if the list
- is longer than 10 elements it removes the first element
-
-
- */
|