-
Notifications
You must be signed in to change notification settings - Fork 0
/
LockerManager.swift
139 lines (122 loc) · 6.44 KB
/
LockerManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// LockerManager.swift
// lngr
//
// Created by Olivier Wittop Koning on 11/09/2021.
//
import Foundation
import SwiftUI
import Combine
import os
public class LockerManager: ObservableObject {
@Published var IsOpen = false
@Published var secondsRemaining = 0.0
@Published var Colour = Color.green
private let logger = Logger(
subsystem: "nl.wittopkoning.lngr.OpenLocker",
category: "LockerManager"
)
/// Helper function to porvide user feedback when somthing has gone wrong
public func simpleError() {
WKInterfaceDevice.current().play(.failure)
logger.log("simpleError")
}
public func simpleSuccess() {
WKInterfaceDevice.current().play(.success)
logger.log("simpleSuccess")
}
func open() -> Void {
let timestamp = Int64(round(Date().timeIntervalSince1970*1000) - 30000)
let url = URL(string: "https://mapi.releezme.net/api/Lockers/9960a1c9-bc66-4d61-b00d-c4e4701cc019/Open?nocache=\(timestamp)")!
var request = URLRequest(url: url)
request.allHTTPHeaderFields = ["authorization":"Bearer DyW2waOtHIFQfFq2GQe4zEYz8txAx6NEhxHMR5unQy4/AhynnTDzf7YcjJxC+Byi60OFJ/dSJLcawjYFYOL66UQzTGaoxEj9lRvvnsX1Ce1MylKttaY10IYJwQJyNrdHzjPjglrGzeZt4p1ocGMqY2qcrNegxIfWcqbtAyLnJNYMJrRfLXTm1Q6TiowS3xcF88Swjy1shFzaHwNFJvAbDqKEmkwx7t6m57txwP7ipNf2/HA91gd7/qQxcOoWo/oyN8QnkiwsurMEr5wkNLS+B3VgpSpRYnnnIp7TYGg/mu2LF2DNRhyeF2Nen1dnh1EirRovCPcltNsMOZA6C5Zx6wipiqtMujRd93T5MsW9sCU=", "api-version":"3", "content-length":"0", "user-agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 14_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E149"]
request.httpMethod = "POST"
logger.log("Making reqeust to: \(url.absoluteString, privacy: .public)")
URLSession.shared.dataTask(with: request) {(data, response, error) in
do {
let TheStatus = try JSONDecoder().decode(LockerOpened.self, from: data!)
if TheStatus.status.isOk {
self.simpleSuccess()
for second in 0...20 {
let seconds = second/2
if seconds <= 3 {
self.logger.debug("(seconds <= 3) Setting color to yellow")
DispatchQueue.main.asyncAfter(deadline: .now() + Double(seconds)) {
withAnimation {
self.secondsRemaining += 0.5
}
}
} else if seconds <= 5 && seconds >= 3 {
self.logger.debug("(seconds <= 5) Setting color to yellow")
DispatchQueue.main.asyncAfter(deadline: .now() + Double(seconds)) {
withAnimation { self.Colour = Color.yellow }
self.secondsRemaining += 0.5
}
} else if seconds < 8 && seconds >= 3 {
self.logger.debug("(seconds < 8) Setting color to orange")
DispatchQueue.main.asyncAfter(deadline: .now() + Double(seconds)) {
withAnimation { self.Colour = Color.orange }
self.secondsRemaining += 0.5
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(seconds)) {
self.logger.debug("(else) Setting color to red")
withAnimation { self.Colour = Color.red }
self.secondsRemaining += 0.5
}
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 11.0) {
withAnimation { self.Colour = Color.green }
self.secondsRemaining = 0.0
}
} else {
self.simpleError()
self.logger.log("The reqeust wasn't ok")
if let response = response as? HTTPURLResponse {
self.logger.fault("[ERROR] The reqeust wasn't ok met het laden een url: \(url, privacy: .public) en met response: \(response, privacy: .public) met data: \n \(String(decoding: data!, as: UTF8.self), privacy: .public)")
} else {
self.logger.fault("[ERROR] The reqeust wasn't ok : \(url, privacy: .public) met data \(String(decoding: data!, as: UTF8.self), privacy: .public)")
}
}
} catch {
self.simpleError()
if let response = response as? HTTPURLResponse {
print("ERR:", error)
self.logger.fault("[ERROR] Er was geen data met het laden een url: \(url, privacy: .public) en met response: \(response, privacy: .public) Met de error: \(error.localizedDescription, privacy: .public) met data: \n \(String(decoding: data!, as: UTF8.self), privacy: .public)")
} else {
self.logger.fault("[ERROR] Er was een error terwijl de json werd geparsed: \(url, privacy: .public) met data \(String(decoding: data!, as: UTF8.self), privacy: .public) Met de error: \(error.localizedDescription, privacy: .public)")
}
}
}.resume()
}
}
struct LockerOpened: Codable, Identifiable, CustomStringConvertible, Hashable {
public var id = UUID()
public var status: Status
public var description: String {
return "{ Status: \(status) }"
}
enum CodingKeys: String, CodingKey {
case status = "Status"
}
}
struct Status: Codable, Identifiable, CustomStringConvertible, Hashable {
public var id = UUID()
public var Code: Int
public var TechnicalMessage: String
public var isOk: Bool {
if TechnicalMessage == "OK" {
return true
} else {
return false
}
}
public var description: String {
return "{ Code: \(Code), TechnicalMessage: \(TechnicalMessage) }"
}
enum CodingKeys: String, CodingKey {
case Code
case TechnicalMessage
}
}