@startuml System State Machine

title System State Machine

InitState: /entry: Initialize HAL and slot layouts.
InitState: /do: Get button state.
InitState: /exit: Start wifi network
InitState: /exit: Start mDNS
InitState: /exit: Start web- and update server.

APState: /entry: Setup access point.
APState: /do: Handle client requests.
APState: /do: Handle update.
APState: /exit: Disconnect all wifi connections.

IdleState: /do: Wait for manual reset.

ConnectingState: /entry: Get remote wifi SSID and passphrase.
ConnectingState: /do: Connect every 30 s to remote wifi network.

ConnectedState: /entry: Show hostname.
ConnectedState: /entry: Enable slot routing on the display.
ConnectedState: /do: Handle client requests.
ConnectedState: /do: Handle update.
ConnectedState: /exit: Disconnect all wifi connections.

ErrorState: /do: Wait for manual reset.

RestartState: /entry: Unmount filesystem
RestartState: /do: Restart

OffState: /entry: Set user button as wakeup source
OffState: /do: Clear display
OffState: /do: Enter sleep mode

[*] --> InitState: Power up
InitState --> APState: [Button pressed]\nSet wifi access point mode
InitState -> ConnectingState: [Button released]\nSet wifi station mode

APState --> ErrorState: [Failed to start\nwifi access point]
APState --> RestartState: [Update received]
APState --> OffState: [Switch off requested]

ConnectingState --> IdleState: [Remote wifi SSID or\npassword missing]
ConnectingState --> ConnectedState: [Connection successful established]
ConnectingState --> OffState: [Switch off requested]

ConnectedState --> ConnectingState: [Connection lost]
ConnectedState --> RestartState: [Update finished]
ConnectedState --> OffState: [Switch off requested]

ErrorState --> [*]: [Restart]
ErrorState --> OffState: [Switch off requested]

IdleState --> [*]: [Restart]
IdleState --> OffState: [Switch off requested]

RestartState --> [*]: [Restart]

OffState --> RestartState: [Wakeup]

@enduml