Explorar el Código

fix: locking mechanism for beacons list

chore/restructure-decoder
Blaz Smehov hace 1 semana
padre
commit
f3d83a778c
Se han modificado 1 ficheros con 13 adiciones y 8 borrados
  1. +13
    -8
      cmd/location/main.go

+ 13
- 8
cmd/location/main.go Ver fichero

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

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

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

// Shrinking the model because other properties have nothing to do with the location
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 {
beacon.LocationConfidence++
} else {
beacon.LocationConfidence = 0
}

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

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

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

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

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

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

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

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

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

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


Cargando…
Cancelar
Guardar