72 lines
1.7 KiB
Swift
72 lines
1.7 KiB
Swift
//
|
|
// ArduinoState.swift - The state object to store the state of the arduino process.
|
|
//
|
|
// Created by Marcus Handte on 03.11.22.
|
|
//
|
|
|
|
import Foundation
|
|
import NetworkExtension
|
|
|
|
/// The state to track the progress of the arduino configuration
|
|
class ArduinoState: ObservableObject {
|
|
|
|
var phase = ArduinoPhase.configure { willSet { objectWillChange.send() }}
|
|
var failure: ArduinoPhase?
|
|
var network = "" { willSet { objectWillChange.send() }}
|
|
var password = "" { willSet { objectWillChange.send() }}
|
|
var model = "Arduino" { willSet { objectWillChange.send() }}
|
|
var longitude = "" { willSet { objectWillChange.send() }}
|
|
var latitude = "" { willSet { objectWillChange.send() }}
|
|
var lon: Double? {
|
|
return Double(longitude)
|
|
}
|
|
var lat: Double? {
|
|
return Double(latitude)
|
|
}
|
|
|
|
init() {
|
|
updateNetwork()
|
|
}
|
|
|
|
func updateNetwork() {
|
|
guard network.count == 0 else {
|
|
return
|
|
}
|
|
NEHotspotNetwork.fetchCurrent(completionHandler: { net in
|
|
if let n = net {
|
|
if self.network.count == 0 {
|
|
self.network = n.ssid
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func reset() {
|
|
if phase == .failure || phase == .success{
|
|
failure = nil
|
|
phase = .configure
|
|
}
|
|
}
|
|
|
|
|
|
func isExecutable() -> Bool {
|
|
return phase == .configure &&
|
|
network.count > 0 &&
|
|
password.count >= 8 &&
|
|
model.count > 0 &&
|
|
lat != nil &&
|
|
lon != nil
|
|
}
|
|
}
|
|
|
|
/// The phases of the configuration process
|
|
enum ArduinoPhase {
|
|
case configure
|
|
case register
|
|
case connect
|
|
case transfer
|
|
case success
|
|
case failure
|
|
}
|
|
|