diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-01-04 04:04:03 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-01-04 04:04:03 +0300 |
commit | bb32e56ca28bb43d5d5afef737f25e4725887725 (patch) | |
tree | 519388eb284adeb47744a9ebc334bdf9ff5444a6 /watchos/InfiniSolar/InfiniSolar WatchKit Extension/PumpState.swift | |
parent | d549f428cbbd54c49a90f92928e670d373c4a0d9 (diff) |
move watchos app sources here (which should be rewritten anyway, and usable only in very specific conditions)
Diffstat (limited to 'watchos/InfiniSolar/InfiniSolar WatchKit Extension/PumpState.swift')
-rw-r--r-- | watchos/InfiniSolar/InfiniSolar WatchKit Extension/PumpState.swift | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/watchos/InfiniSolar/InfiniSolar WatchKit Extension/PumpState.swift b/watchos/InfiniSolar/InfiniSolar WatchKit Extension/PumpState.swift new file mode 100644 index 0000000..4d4e5cc --- /dev/null +++ b/watchos/InfiniSolar/InfiniSolar WatchKit Extension/PumpState.swift @@ -0,0 +1,86 @@ +// +// PumpState.swift +// InfiniSolar WatchKit Extension +// +// Created by Evgeny Zinoviev on 09.08.2021. +// + +import Alamofire +import SwiftyJSON + +public class PumpState: ObservableObject { + @Published var isEnabled: Bool + @Published var error: Bool + @Published var loading: Bool + @Published var setting: Bool + + var request: DataRequest? + + init() { + self.loading = true + self.error = false + self.isEnabled = false + self.request = nil + self.setting = false + } + + func fetch() { + self.request = AF.request("http://192.168.5.223:8382/get/").responseJSON { response in + self.loading = false + + switch response.result { + case .success(let value): + let json = JSON(value) + self.isEnabled = json["data"].string == "on" + + case .failure(let error): + switch (error) { + case .explicitlyCancelled: + break + + default: + print(error) + self.error = true + } + } + +// self.loading = false + self.request = nil + } + } + + func setState(on: Bool) { + self.setting = true + self.request = AF.request("http://192.168.5.223:8382/" + (on ? "on" : "off") + "/").responseJSON { response in + self.setting = false + + switch response.result { + case .success(_): + self.isEnabled = on + + case .failure(let error): + switch (error) { + case .explicitlyCancelled: + break + + default: + print(error) + self.error = true + } + } + + self.request = nil + } + } + + func abort() { + if self.request != nil { + self.request?.cancel() + self.request = nil + } + + self.error = false + self.loading = true + self.isEnabled = false + } +} |