Преглед изворни кода

fix: locking mechanism for beacons list

chore/restructure-decoder
Blaz Smehov пре 1 недеља
родитељ
комит
f3d83a778c
1 измењених фајлова са 13 додато и 8 уклоњено
  1. +13
    -8
      cmd/location/main.go

+ 13
- 8
cmd/location/main.go Прегледај датотеку

@@ -77,10 +77,13 @@ func main() {
} }


func getLikelyLocations(ctx *model.AppContext, writer *kafka.Writer) { func getLikelyLocations(ctx *model.AppContext, writer *kafka.Writer) {
fmt.Println("get likely locations called")
ctx.Beacons.Lock.Lock() ctx.Beacons.Lock.Lock()
beacons := ctx.Beacons.Beacons beacons := ctx.Beacons.Beacons
ctx.Beacons.Lock.Unlock()


for _, beacon := range beacons { for _, beacon := range beacons {
fmt.Printf("beacon: %+v", beacon)


// Shrinking the model because other properties have nothing to do with the location // Shrinking the model because other properties have nothing to do with the location
r := model.HTTPLocation{ r := model.HTTPLocation{
@@ -115,21 +118,15 @@ func getLikelyLocations(ctx *model.AppContext, writer *kafka.Writer) {
} }
} }


bestLocation := model.BestLocation{
Name: bestLocName,
Distance: beacon.BeaconMetrics[mSize-1].Distance,
LastSeen: beacon.BeaconMetrics[mSize-1].Timestamp,
}

if bestLocName == beacon.PreviousLocation { if bestLocName == beacon.PreviousLocation {
beacon.LocationConfidence++ beacon.LocationConfidence++
} else { } else {
beacon.LocationConfidence = 0 beacon.LocationConfidence = 0
} }


r.Distance = bestLocation.Distance
r.Distance = beacon.BeaconMetrics[mSize-1].Distance
r.Location = bestLocName r.Location = bestLocName
r.LastSeen = bestLocation.LastSeen
r.LastSeen = beacon.BeaconMetrics[mSize-1].Timestamp


if beacon.LocationConfidence == ctx.Settings.Settings.LocationConfidence && beacon.PreviousConfidentLocation != bestLocName { if beacon.LocationConfidence == ctx.Settings.Settings.LocationConfidence && beacon.PreviousConfidentLocation != bestLocName {
beacon.LocationConfidence = 0 beacon.LocationConfidence = 0
@@ -147,7 +144,9 @@ func getLikelyLocations(ctx *model.AppContext, writer *kafka.Writer) {
if err != nil { if err != nil {
beacon.PreviousConfidentLocation = bestLocName beacon.PreviousConfidentLocation = bestLocName
beacon.PreviousLocation = bestLocName beacon.PreviousLocation = bestLocName
ctx.Beacons.Lock.Lock()
ctx.Beacons.Beacons[beacon.ID] = beacon ctx.Beacons.Beacons[beacon.ID] = beacon
ctx.Beacons.Lock.Unlock()
continue continue
} }


@@ -162,7 +161,9 @@ func getLikelyLocations(ctx *model.AppContext, writer *kafka.Writer) {
} }


beacon.PreviousLocation = bestLocName beacon.PreviousLocation = bestLocName
ctx.Beacons.Lock.Lock()
ctx.Beacons.Beacons[beacon.ID] = beacon ctx.Beacons.Beacons[beacon.ID] = beacon
ctx.Beacons.Lock.Unlock()


js, err := json.Marshal(r) js, err := json.Marshal(r)
if err != nil { if err != nil {
@@ -194,12 +195,15 @@ func assignBeaconToList(adv model.BeaconAdvertisement, ctx *model.AppContext) {
return return
} }


fmt.Println("Beacon exists: ", id)

if ctx.Settings.Settings.RSSIEnforceThreshold && (int64(adv.RSSI) < ctx.Settings.Settings.RSSIMinThreshold) { if ctx.Settings.Settings.RSSIEnforceThreshold && (int64(adv.RSSI) < ctx.Settings.Settings.RSSIMinThreshold) {
return return
} }


ctx.Beacons.Lock.Lock() ctx.Beacons.Lock.Lock()
beacon := ctx.Beacons.Beacons[id] beacon := ctx.Beacons.Beacons[id]
ctx.Beacons.Lock.Unlock()


beacon.IncomingJSON = adv beacon.IncomingJSON = adv
beacon.LastSeen = now beacon.LastSeen = now
@@ -227,6 +231,7 @@ func assignBeaconToList(adv model.BeaconAdvertisement, ctx *model.AppContext) {
beacon.BeaconMetrics = append(beacon.BeaconMetrics, metric) beacon.BeaconMetrics = append(beacon.BeaconMetrics, metric)
} }


ctx.Beacons.Lock.Lock()
ctx.Beacons.Beacons[id] = beacon ctx.Beacons.Beacons[id] = beacon
ctx.Beacons.Lock.Unlock() ctx.Beacons.Lock.Unlock()
} }


Loading…
Откажи
Сачувај