Fixes issue where connectivity issues can lead to persistent "Offline" status in UI

This fixes the problem where either internet connectivity issues on the device or intermittant
server issues can cause a persistent "OFFLINE" indicator even though that is not the case.

This requires the server protocol to be more granular about what their connection status is,
and adds a new signal from the multiplexer to the MainViewController allowing it to notify the
delegate that an acknowledgement was heard from one of the servers, allowing the main view
controller to update its UI according to whatever status best represents the group of servers
as a whole.

Testing
- This can be tested by using the network link conditioner to simulate connection issues
  *after* the control panel has connected to all servers in the multiplex.
- Tested losing internet connection and regaining internet connection
- Tested starting off with no connection and regaining connection later
This commit is contained in:
2020-07-04 19:43:50 -07:00
parent ee29602640
commit d1da677932
5 changed files with 55 additions and 31 deletions

View File

@@ -12,6 +12,7 @@ protocol ServerMultiplexDelegate
{
func serverMultiplex(_ multiplex: ServerMultiplex, didAddDevices devices: [AnyDevice])
func serverMultiplex(_ multiplex: ServerMultiplex, devicesStateChanged devices: [AnyDevice])
func serverMultiplex(_ multiplex: ServerMultiplex, didReceiveAcknowledgementFromServer server: Server)
func serverMultiplex(_ multiplex: ServerMultiplex, didEncounterError error: Error)
}
@@ -33,9 +34,26 @@ class ServerMultiplex
servers.append(server)
}
public func numberOfConnectedServers() -> Int
/// Returns a status that best represents the status of the whole group, as if it were one device
public func groupConnectionStatus() -> ConnectionStatus
{
return servers.filter { $0.connected == true }.count
let relevancyRank: (ConnectionStatus) -> Int = { status in
switch status {
case .disconnected: return 0
case .connected: return 1
case .connecting: return 2
case .error: return 3
}
}
var groupStatus = ConnectionStatus.connected
servers.forEach { server in
if relevancyRank(server.connectionStatus) > relevancyRank(groupStatus) {
groupStatus = server.connectionStatus
}
}
return groupStatus
}
public func toggleDeviceState(_ device: AnyDevice, state: DeviceState, completion: @escaping (Error?) -> Void)
@@ -56,7 +74,7 @@ class ServerMultiplex
{
self.servers.forEach { (server: Server) in
server.fetchDevices { (result: Result<[AnyDevice], Error>) in
self.handleServerFetchResult(result)
self.handleServerFetchResult(forServer: server, result: result)
}
}
}
@@ -64,18 +82,22 @@ class ServerMultiplex
extension ServerMultiplex
{
private func handleServerFetchResult(_ result: Result<[AnyDevice], Error>)
private func handleServerFetchResult(forServer server: Server, result: Result<[AnyDevice], Error>)
{
switch result {
case .success(let devices):
handleDevicesChanged(devices)
handleDevicesChanged(forServer: server, devicesChanged: devices)
case .failure(let error):
handleError(error)
handleError(forServer: server, error: error)
}
}
private func handleDevicesChanged(_ devicesChanged: [AnyDevice])
private func handleDevicesChanged(forServer server: Server, devicesChanged: [AnyDevice])
{
// First send server acknowledgement
self.delegate?.serverMultiplex(self, didReceiveAcknowledgementFromServer: server)
// Then, optionally notify about new devices or device state changes
let newDevicesSet = Set<AnyDevice>(devicesChanged)
let additions = newDevicesSet.subtracting(self.devices)
let changed = newDevicesSet.filter { (device: AnyDevice) in
@@ -101,7 +123,7 @@ extension ServerMultiplex
}
}
private func handleError(_ error: Error)
private func handleError(forServer server: Server, error: Error)
{
DispatchQueue.main.async {
self.delegate?.serverMultiplex(self, didEncounterError: error)