| @@ -132,34 +132,32 @@ func getLikelyLocations(appState *appcontext.AppState, writer *kafka.Writer) { | |||||
| if beacon.LocationConfidence == settings.LocationConfidence && beacon.PreviousConfidentLocation != bestLocName { | if beacon.LocationConfidence == settings.LocationConfidence && beacon.PreviousConfidentLocation != bestLocName { | ||||
| beacon.LocationConfidence = 0 | beacon.LocationConfidence = 0 | ||||
| // Why do I need this if I am sending entire structure anyways? who knows | |||||
| js, err := json.Marshal(model.LocationChange{ | |||||
| Method: "LocationChange", | |||||
| BeaconRef: beacon, | |||||
| Name: beacon.Name, | |||||
| PreviousLocation: beacon.PreviousConfidentLocation, | |||||
| NewLocation: bestLocName, | |||||
| Timestamp: time.Now().Unix(), | |||||
| }) | |||||
| if err != nil { | |||||
| eMsg := fmt.Sprintf("Error in marshaling: %v", err) | |||||
| slog.Error(eMsg) | |||||
| beacon.PreviousConfidentLocation = bestLocName | |||||
| beacon.PreviousLocation = bestLocName | |||||
| appState.UpdateBeacon(beacon.ID, beacon) | |||||
| continue | |||||
| } | |||||
| msg := kafka.Message{ | |||||
| Value: js, | |||||
| } | |||||
| err = writer.WriteMessages(context.Background(), msg) | |||||
| if err != nil { | |||||
| fmt.Println("Error in sending Kafka message") | |||||
| } | |||||
| // js, err := json.Marshal(model.LocationChange{ | |||||
| // Method: "LocationChange", | |||||
| // BeaconRef: beacon, | |||||
| // Name: beacon.Name, | |||||
| // PreviousLocation: beacon.PreviousConfidentLocation, | |||||
| // NewLocation: bestLocName, | |||||
| // Timestamp: time.Now().Unix(), | |||||
| // }) | |||||
| // if err != nil { | |||||
| // eMsg := fmt.Sprintf("Error in marshaling: %v", err) | |||||
| // slog.Error(eMsg) | |||||
| // beacon.PreviousConfidentLocation = bestLocName | |||||
| // beacon.PreviousLocation = bestLocName | |||||
| // appState.UpdateBeacon(beacon.ID, beacon) | |||||
| // continue | |||||
| // } | |||||
| // msg := kafka.Message{ | |||||
| // Value: js, | |||||
| // } | |||||
| // err = writer.WriteMessages(context.Background(), msg) | |||||
| // if err != nil { | |||||
| // fmt.Println("Error in sending Kafka message") | |||||
| // } | |||||
| } | } | ||||
| beacon.PreviousLocation = bestLocName | beacon.PreviousLocation = bestLocName | ||||
| @@ -145,10 +145,6 @@ eventLoop: | |||||
| fmt.Printf("Error in saving decoder event for beacon: %s\n", id) | fmt.Printf("Error in saving decoder event for beacon: %s\n", id) | ||||
| continue | continue | ||||
| } | } | ||||
| // if err := service.EventToBeaconService(msg, appState, ctx); err != nil { | |||||
| // eMsg := fmt.Sprintf("Error in writing event change to beacon: %v\n", err) | |||||
| // slog.Error(eMsg) | |||||
| // } | |||||
| } | } | ||||
| } | } | ||||
| @@ -24,38 +24,65 @@ func UpdateDB(db *gorm.DB, ctx context.Context, cfg *config.Config, writer *kafk | |||||
| if err != nil { | if err != nil { | ||||
| return err | return err | ||||
| } | } | ||||
| trackers, err := GetTrackers(token, client) | trackers, err := GetTrackers(token, client) | ||||
| if err != nil { | if err != nil { | ||||
| fmt.Printf("Error in getting trackers: %+v\n", err) | fmt.Printf("Error in getting trackers: %+v\n", err) | ||||
| } | |||||
| } else { | |||||
| if err := controller.SendKafkaMessage(writer, &model.ApiUpdate{Method: "DELETE", MAC: "all"}, ctx); err != nil { | |||||
| fmt.Printf("Error in sending delete all from lookup message: %v", err) | |||||
| } | |||||
| if err := controller.SendKafkaMessage(writer, &model.ApiUpdate{Method: "DELETE", MAC: "all"}, ctx); err != nil { | |||||
| fmt.Printf("Error in sending delete all from lookup message: %v", err) | |||||
| } | |||||
| for _, v := range trackers { | |||||
| apiUpdate := model.ApiUpdate{ | |||||
| Method: "POST", | |||||
| ID: v.ID, | |||||
| MAC: v.MAC, | |||||
| } | |||||
| for _, v := range trackers { | |||||
| apiUpdate := model.ApiUpdate{ | |||||
| Method: "POST", | |||||
| ID: v.ID, | |||||
| MAC: v.MAC, | |||||
| if err := controller.SendKafkaMessage(writer, &apiUpdate, ctx); err != nil { | |||||
| fmt.Printf("Error in sending POST kafka message: %v", err) | |||||
| } | |||||
| } | } | ||||
| if err := controller.SendKafkaMessage(writer, &apiUpdate, ctx); err != nil { | |||||
| fmt.Printf("Error in sending POST kafka message: %v", err) | |||||
| var ids []string | |||||
| for _, t := range trackers { | |||||
| ids = append(ids, t.ID) | |||||
| } | } | ||||
| db.Where("id NOT IN ?", ids).Delete(&model.Tracker{}) | |||||
| } | } | ||||
| gateways, err := GetGateways(token, client) | gateways, err := GetGateways(token, client) | ||||
| if err != nil { | if err != nil { | ||||
| fmt.Printf("Error in getting gateways: %+v\n", err) | fmt.Printf("Error in getting gateways: %+v\n", err) | ||||
| } else { | |||||
| var ids []string | |||||
| for _, g := range gateways { | |||||
| ids = append(ids, g.ID) | |||||
| } | |||||
| db.Where("id NOT IN ?", ids).Delete(&model.Gateway{}) | |||||
| } | } | ||||
| zones, err := GetZones(token, client) | zones, err := GetZones(token, client) | ||||
| if err != nil { | if err != nil { | ||||
| fmt.Printf("Error in getting zones: %+v\n", err) | fmt.Printf("Error in getting zones: %+v\n", err) | ||||
| } else { | |||||
| var ids []string | |||||
| for _, z := range zones { | |||||
| ids = append(ids, z.ID) | |||||
| } | |||||
| db.Where("id NOT IN ?", ids).Delete(&model.Zone{}) | |||||
| } | } | ||||
| trackerZones, err := GetTrackerZones(token, client) | trackerZones, err := GetTrackerZones(token, client) | ||||
| if err != nil { | if err != nil { | ||||
| fmt.Printf("Error in getting tracker zones: %+v\n", err) | fmt.Printf("Error in getting tracker zones: %+v\n", err) | ||||
| } else { | |||||
| var ids []string | |||||
| for _, tz := range trackerZones { | |||||
| ids = append(ids, tz.ID) | |||||
| } | |||||
| db.Where("id NOT IN ?", ids).Delete(&model.TrackerZones{}) | |||||
| } | } | ||||
| db.Clauses(clause.OnConflict{ | db.Clauses(clause.OnConflict{ | ||||
| @@ -15,6 +15,7 @@ import ( | |||||
| func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB, writer *kafka.Writer, ctx context.Context) { | func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB, writer *kafka.Writer, ctx context.Context) { | ||||
| if msg.ID == "" { | if msg.ID == "" { | ||||
| fmt.Println("empty ID") | |||||
| return | return | ||||
| } | } | ||||
| @@ -23,8 +24,6 @@ func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB, writer *kafka. | |||||
| return | return | ||||
| } | } | ||||
| fmt.Printf("zones: %v\n", zones) | |||||
| var allowedZones []string | var allowedZones []string | ||||
| for _, z := range zones { | for _, z := range zones { | ||||
| allowedZones = append(allowedZones, z.ZoneList...) | allowedZones = append(allowedZones, z.ZoneList...) | ||||