removed device_id from all entities except car. Replaced with car_id . Car -> device_id

This commit is contained in:
2026-07-16 17:40:59 +04:00
parent 41eaf49bb2
commit f6547cd1f1
18 changed files with 126 additions and 78 deletions
+14 -20
View File
@@ -187,7 +187,7 @@ func (s *Service) RemoveDriver(id string) {
d.Status = DriverStatusOffline
d.LastSeenMs = time.Now().UnixMilli()
d.RaceID = ""
d.DeviceID = nil
d.CarID = nil
hook := s.onRaceRemoved
s.mu.Unlock()
@@ -244,9 +244,9 @@ func (s *Service) AddDriverToRace(driverID, raceID string) error {
s.mu.Unlock()
return ErrDriverNotFound
}
if d.DeviceID == nil {
if d.CarID == nil || *d.CarID == "" {
s.mu.Unlock()
return fmt.Errorf("driver %s has no device selected", driverID)
return fmt.Errorf("driver %s has no car selected", driverID)
}
r, ok := s.races[raceID]
if !ok {
@@ -306,7 +306,7 @@ func (s *Service) RemoveDriverFromRace(driverID string) {
}
d.Status = DriverStatusIdle
d.RaceID = ""
d.DeviceID = nil
d.CarID = nil
prevRaceID := d.RaceID
hook := s.onRaceRemoved
s.mu.Unlock()
@@ -349,7 +349,7 @@ func (s *Service) SetRaceStatus(raceID string, status RaceStatus) error {
if d, ok := s.drivers[did]; ok {
d.Status = DriverStatusIdle
d.RaceID = ""
d.DeviceID = nil
d.CarID = nil
affected = append(affected, *d)
}
}
@@ -454,16 +454,10 @@ func (s *Service) GetDriver(driverID string) (DriverMeta, error) {
return *d, nil
}
// SelectDevice assigns a physical device ID (0..127) to a driver.
// Pass deviceID = nil to release the device.
// Returns an error if the device is already taken by another online driver.
func (s *Service) SelectDevice(driverID string, deviceID *int) error {
if deviceID != nil {
if *deviceID < 0 || *deviceID > 127 {
return fmt.Errorf("invalid device ID: must be 0..127")
}
}
// SelectCar assigns a catalog car ID to a driver.
// If carID is nil, it clears the driver's car assignment.
// It returns an error if the car is already taken by another active driver.
func (s *Service) SelectCar(driverID string, carID *string) error {
s.mu.Lock()
d, ok := s.drivers[driverID]
if !ok {
@@ -471,17 +465,17 @@ func (s *Service) SelectDevice(driverID string, deviceID *int) error {
return ErrDriverNotFound
}
// Enforce that device cannot be selected if already assigned to another driver.
if deviceID != nil {
// Enforce that car cannot be selected if already assigned to another driver.
if carID != nil && *carID != "" {
for otherID, otherD := range s.drivers {
if otherID != driverID && otherD.Status != DriverStatusOffline && otherD.DeviceID != nil && *otherD.DeviceID == *deviceID {
if otherID != driverID && otherD.Status != DriverStatusOffline && otherD.CarID != nil && *otherD.CarID == *carID {
s.mu.Unlock()
return fmt.Errorf("device %d is already taken by driver %s", *deviceID, otherID)
return fmt.Errorf("car %s is already taken by driver %s", *carID, otherID)
}
}
}
d.DeviceID = deviceID
d.CarID = carID
out := *d
s.mu.Unlock()