Skip to content

Commit 9bd23c3

Browse files
committed
Automatically retry AP configuration if it crashed.
1 parent 58b7dbf commit 9bd23c3

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

network/access_point.go

+29
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type AccessPoint struct {
3232
networkSecurityEnabled bool
3333
Status string
3434
TeamWifiStatuses [6]*TeamWifiStatus
35+
lastConfiguredTeams [6]*model.Team
3536
}
3637

3738
type TeamWifiStatus struct {
@@ -90,6 +91,15 @@ func (ap *AccessPoint) Run() {
9091
time.Sleep(time.Second * accessPointPollPeriodSec)
9192
if err := ap.updateMonitoring(); err != nil {
9293
log.Printf("Failed to update access point monitoring: %v", err)
94+
continue
95+
}
96+
97+
// If the access point is in a good state but doesn't match the expected configuration, try again.
98+
if ap.Status == "ACTIVE" && !ap.statusMatchesLastConfiguration() {
99+
log.Println("Access point is ACTIVE but does not match expected configuration; retrying configuration.")
100+
if err := ap.ConfigureTeamWifi(ap.lastConfiguredTeams); err != nil {
101+
log.Printf("Failed to reconfigure access point: %v", err)
102+
}
93103
}
94104
}
95105
}
@@ -100,6 +110,8 @@ func (ap *AccessPoint) ConfigureTeamWifi(teams [6]*model.Team) error {
100110
return nil
101111
}
102112

113+
ap.Status = "CONFIGURING"
114+
ap.lastConfiguredTeams = teams
103115
request := configurationRequest{
104116
Channel: ap.channel,
105117
StationConfigurations: make(map[string]stationConfiguration),
@@ -204,6 +216,23 @@ func (ap *AccessPoint) checkAndLogApiError(err error) {
204216
}
205217
}
206218

219+
// Returns true if the access point's current status matches the last configuration that was sent to it.
220+
func (ap *AccessPoint) statusMatchesLastConfiguration() bool {
221+
for i := 0; i < 6; i++ {
222+
var expectedTeamId, actualTeamId int
223+
if ap.lastConfiguredTeams[i] != nil {
224+
expectedTeamId = ap.lastConfiguredTeams[i].Id
225+
}
226+
if ap.TeamWifiStatuses[i] != nil {
227+
actualTeamId = ap.TeamWifiStatuses[i].TeamId
228+
}
229+
if expectedTeamId != actualTeamId {
230+
return false
231+
}
232+
}
233+
return true
234+
}
235+
207236
// Generates the configuration for the given team's station and adds it to the map. If the team is nil, no entry is
208237
// added for the station.
209238
func addStation(stationsConfigurations map[string]stationConfiguration, station string, team *model.Team) {

network/access_point_test.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func TestAccessPoint_ConfigureTeamWifi(t *testing.T) {
1717
var request configurationRequest
1818
wifiStatuses := [6]*TeamWifiStatus{{}, {}, {}, {}, {}, {}}
1919
ap.SetSettings("dummy", "password1", 123, true, wifiStatuses)
20-
ap.Status = "INITIAL"
2120

2221
// Mock the radio API server.
2322
radioServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -77,7 +76,7 @@ func TestAccessPoint_ConfigureTeamWifi(t *testing.T) {
7776
if assert.NotNil(t, err) {
7877
assert.Contains(t, err.Error(), "returned status 507: oh noes")
7978
}
80-
assert.Equal(t, "INITIAL", ap.Status)
79+
assert.Equal(t, "CONFIGURING", ap.Status)
8180
}
8281

8382
func TestAccessPoint_updateMonitoring(t *testing.T) {
@@ -148,3 +147,23 @@ func TestAccessPoint_updateMonitoring(t *testing.T) {
148147
}
149148
assert.Equal(t, "ERROR", ap.Status)
150149
}
150+
151+
func TestAccessPoint_statusMatchesLastConfiguration(t *testing.T) {
152+
var ap AccessPoint
153+
wifiStatuses := [6]*TeamWifiStatus{{}, {}, {}, {}, {}, {}}
154+
ap.SetSettings("dummy", "", 123, true, wifiStatuses)
155+
156+
assert.True(t, ap.statusMatchesLastConfiguration())
157+
team1 := &model.Team{Id: 254, WpaKey: "11111111"}
158+
team2 := &model.Team{Id: 1678, WpaKey: "22222222"}
159+
ap.ConfigureTeamWifi([6]*model.Team{nil, team1, nil, team2, nil, nil})
160+
assert.False(t, ap.statusMatchesLastConfiguration())
161+
ap.TeamWifiStatuses[1].TeamId = 254
162+
assert.False(t, ap.statusMatchesLastConfiguration())
163+
ap.TeamWifiStatuses[3].TeamId = 1677
164+
assert.False(t, ap.statusMatchesLastConfiguration())
165+
ap.TeamWifiStatuses[3].TeamId = 1678
166+
assert.True(t, ap.statusMatchesLastConfiguration())
167+
ap.TeamWifiStatuses[4].TeamId = 111
168+
assert.False(t, ap.statusMatchesLastConfiguration())
169+
}

0 commit comments

Comments
 (0)