(client string, unit *models.Unit, ap apReport)
| 16 | } |
| 17 | |
| 18 | func (api *API) unitReport(client string, unit *models.Unit, ap apReport) error { |
| 19 | if parsed, err := net.ParseMAC(ap.BSSID); err != nil { |
| 20 | return fmt.Errorf("error while parsing wifi ap bssid %s from %s: %v", ap.BSSID, client, err) |
| 21 | } else { |
| 22 | // normalize |
| 23 | ap.BSSID = parsed.String() |
| 24 | } |
| 25 | |
| 26 | if existing := unit.FindAccessPoint(ap.ESSID, ap.BSSID); existing == nil { |
| 27 | log.Debug("unit %s (%s %s) reporting new wifi access point %v", unit.Identity(), unit.Address, |
| 28 | unit.Country, ap) |
| 29 | |
| 30 | newAP := models.AccessPoint{ |
| 31 | Name: ap.ESSID, |
| 32 | Mac: ap.BSSID, |
| 33 | UnitID: unit.ID, |
| 34 | } |
| 35 | |
| 36 | if err := models.Create(&newAP).Error; err != nil { |
| 37 | return fmt.Errorf("error creating ap %v: %v", newAP, err) |
| 38 | } |
| 39 | } else if err := models.Update(existing).Error; err != nil { |
| 40 | return fmt.Errorf("error updating ap %v: %v", existing, err) |
| 41 | } |
| 42 | |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | func (api *API) UnitReportAP(w http.ResponseWriter, r *http.Request) { |
| 47 | unit := Authenticate(w, r) |
no test coverage detected