Skip to content

Adds climate, hvac and temperature requests #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ fastlane/screenshots
fastlane/test_output
fastlane/README.md

Tests/Credentials.swift
Tests/Credentials.swift
8 changes: 4 additions & 4 deletions Source/Models/ChargeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class ChargeState {
public let chargerVoltage: Int
public let chargingState: ChargingState
public let estBatteryRange: Double
public let euVehicle: Bool
public let euVehicle: Bool?
public let fastChargerPresent: Bool
public let fastChargerType: String?
public let idealBatteryRange: Double
public let managedChargingActive: Bool
public let managedChargingStartTime: Date?
public let managedChargingUserCanceled: Bool
public let maxRangeChargeCounter: Int
public let motorizedChargePort: Bool
public let motorizedChargePort: Bool?
public let notEnoughPowerToHeat: Bool?
public let scheduledChargingPending: Bool
public let scheduledChargingStartTime: Date?
Expand Down Expand Up @@ -74,7 +74,7 @@ public class ChargeState {
chargerVoltage = dictionary["charger_voltage"] as! Int
chargingState = ChargingState(rawValue: dictionary["charging_state"] as! String)!
estBatteryRange = dictionary["est_battery_range"] as! Double
euVehicle = dictionary["eu_vehicle"] as! Bool
euVehicle = dictionary["eu_vehicle"] as? Bool
fastChargerPresent = dictionary["fast_charger_present"] as! Bool
fastChargerType = dictionary["fast_charger_type"] as? String
idealBatteryRange = dictionary["ideal_battery_range"] as! Double
Expand All @@ -86,7 +86,7 @@ public class ChargeState {
}
managedChargingUserCanceled = dictionary["managed_charging_user_canceled"] as! Bool
maxRangeChargeCounter = dictionary["max_range_charge_counter"] as! Int
motorizedChargePort = dictionary["motorized_charge_port"] as! Bool
motorizedChargePort = dictionary["motorized_charge_port"] as? Bool
notEnoughPowerToHeat = dictionary["not_enough_power_to_heat"] as? Bool
scheduledChargingPending = dictionary["scheduled_charging_pending"] as! Bool
if let timestamp = dictionary["scheduled_charging_start_time"] as? Double {
Expand Down
23 changes: 23 additions & 0 deletions Source/Models/ClimateState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

public struct ClimateState {
let insideTemp: Double
let outsideTemp: Double
let driverTempSetting: Double
let passengerTempSetting: Double
let isAutoConditioningOn: Bool
let isFrontDefrosterOn: Bool
let isRearDefrosterOn: Bool
let fanStatus: Int?

init(dict: [String: Any]) {
insideTemp = dict["inside_temp"] as! Double
outsideTemp = dict["outside_temp"] as! Double
driverTempSetting = dict["driver_temp_setting"] as! Double
passengerTempSetting = dict["passenger_temp_setting"] as! Double
isAutoConditioningOn = dict["is_auto_conditioning_on"] as! Bool
isFrontDefrosterOn = dict["is_front_defroster_on"] as! Bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force Cast Violation: Force casts should be avoided. (force_cast)

isRearDefrosterOn = dict["is_rear_defroster_on"] as! Bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force Cast Violation: Force casts should be avoided. (force_cast)

fanStatus = dict["fan_status"] as? Int
}
}
36 changes: 15 additions & 21 deletions Source/Requests/AuthenticateRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,29 @@ public struct AuthenticateRequest: RequestProtocol {
}

public func execute(completion: @escaping (Result<Token>) -> Void) {
let params: [String: AnyObject] = [
"email": username as NSString,
"password": password as NSString,
"grant_type": grantType as NSString,
"client_id": clientIdentifier as NSString,
"client_secret": clientSecret as NSString
]
let params = [
"email": username,
"password": password,
"grant_type": grantType,
"client_id": clientIdentifier,
"client_secret": clientSecret
]
WebRequest.request(
path: path,
method: method,
params: params) { response, error in
if let error = error {
DispatchQueue.main.async {
DispatchQueue.main.async {
if let error = error {
completion(Result.failure(error))
}
} else {
guard let responseDictionary = response as? [String: AnyObject] else {
DispatchQueue.main.async {
} else {
guard let responseDictionary = response as? [String: AnyObject] else {
completion(Result.failure(APIError()))
return
}
return
}
do {
let result = try Result.success(Token(dictionary: responseDictionary))
DispatchQueue.main.async {
do {
let result = try Result.success(Token(dictionary: responseDictionary))
completion(result)
}
} catch let error {
DispatchQueue.main.async {
} catch {
completion(Result.failure(error))
}
}
Expand Down
14 changes: 5 additions & 9 deletions Source/Requests/ChargeStateRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ public struct ChargeStateRequest: RequestProtocol {
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
DispatchQueue.main.async {
if let error = error {
completion(Result.failure(error))
}
} else if let response = response as? [String: [String: Any]],
let dictionary = response["response"] {
DispatchQueue.main.async {
} else if let response = response as? [String: [String: Any]],
let dictionary = response["response"] {
completion(Result.success(ChargeState(dictionary: dictionary)))
}
} else {
DispatchQueue.main.async {
} else {
completion(Result.failure(APIError()))
}
}
Expand Down
35 changes: 35 additions & 0 deletions Source/Requests/ClimateStateRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation

public struct ClimateStateRequest: RequestProtocol {
typealias CompletionType = ClimateState
var path: String {
return "/api/1/vehicles/\(vehicleIdentifier)/data_request/climate_state"
}
let method = WebRequest.RequestMethod.get
let accessToken: String
let vehicleIdentifier: String

public init(accessToken: String, vehicleIdentifier: String) {
self.accessToken = accessToken
self.vehicleIdentifier = vehicleIdentifier
}

public func execute(completion: @escaping (Result<ClimateState>) -> Void) {
WebRequest.request(
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
completion(Result.failure(error))
}
return
}
let responseDict = response as! [String: [String: Any]]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force Cast Violation: Force casts should be avoided. (force_cast)

let dataDict = responseDict["response"]!
DispatchQueue.main.async {
completion(Result.success(ClimateState(dict: dataDict)))
}
}
}
}
49 changes: 49 additions & 0 deletions Source/Requests/HVACRequests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Foundation

public struct HVACRequest: RequestProtocol {
public enum HVACState {
case on

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier Name Violation: Enum element name should be between 3 and 40 characters long: 'on' (identifier_name)

case off
}
typealias CompletionType = Bool
var path: String {
switch state {
case .on:
return "/api/1/vehicles/\(vehicleIdentifier)/command/auto_conditioning_start"
case .off:
return "/api/1/vehicles/\(vehicleIdentifier)/command/auto_conditioning_stop"
}
}
let method = WebRequest.RequestMethod.post
let accessToken: String
let vehicleIdentifier: String
let state: HVACState

public init(accessToken: String, vehicleIdentifier: String, state: HVACState) {
self.accessToken = accessToken
self.vehicleIdentifier = vehicleIdentifier
self.state = state
}

public func execute(completion: @escaping (Result<Bool>) -> Void) {
WebRequest.request(
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
completion(Result.failure(error))
}
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
DispatchQueue.main.async {
completion(Result.success(resultBool))
}
} else {
DispatchQueue.main.async {
completion(Result.failure(APIError()))
}
}
}
}
}
16 changes: 6 additions & 10 deletions Source/Requests/ListVehiclesRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ public struct ListVehiclesRequest: RequestProtocol {
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
DispatchQueue.main.async {
if let error = error {
completion(Result.failure(error))
}
} else {
guard let responseArray = response?["response"] as? [[String: AnyObject]] else {
DispatchQueue.main.async {
} else {
guard let responseArray = response?["response"] as? [[String: AnyObject]] else {
completion(Result.failure(APIError()))
return
}
return
}
let vehicles = responseArray.flatMap { return Vehicle(dictionary: $0) }
DispatchQueue.main.async {
let vehicles = responseArray.flatMap { return Vehicle(dictionary: $0) }
completion(Result.success(vehicles))
}
}
Expand Down
14 changes: 5 additions & 9 deletions Source/Requests/LockRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ public struct LockRequest: RequestProtocol {
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
DispatchQueue.main.async {
if let error = error {
completion(Result.failure(error))
}
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
DispatchQueue.main.async {
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
completion(Result.success(resultBool))
}
} else {
DispatchQueue.main.async {
} else {
completion(Result.failure(APIError()))
}
}
Expand Down
14 changes: 5 additions & 9 deletions Source/Requests/MobileEnabledForVehicleRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ struct MobileEnabledForVehicleRequest: RequestProtocol {
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
DispatchQueue.main.async {
if let error = error {
completion(Result.failure(error))
}
} else if let response = response as? [String: Bool],
let responseBool = response["response"] {
DispatchQueue.main.async {
} else if let response = response as? [String: Bool],
let responseBool = response["response"] {
completion(Result.success(responseBool))
}
} else {
DispatchQueue.main.async {
} else {
completion(Result.failure(APIError()))
}
}
Expand Down
14 changes: 5 additions & 9 deletions Source/Requests/OpenChargePortRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ public struct OpenChargePortRequest: RequestProtocol {
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
DispatchQueue.main.async {
if let error = error {
completion(Result.failure(error))
}
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
DispatchQueue.main.async {
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
completion(Result.success(resultBool))
}
} else {
DispatchQueue.main.async {
} else {
completion(Result.failure(APIError()))
}
}
Expand Down
43 changes: 43 additions & 0 deletions Source/Requests/SetTemperaturesRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Foundation

public struct SetTemperaturesRequest: RequestProtocol {
typealias CompletionType = Bool
var path: String {
return "/api/1/vehicles/\(vehicleIdentifier)/command/set_temps"
}
let method = WebRequest.RequestMethod.post
let accessToken: String
let vehicleIdentifier: String
let driverTemp: Double
let passengerTemp: Double

public init(accessToken: String, vehicleIdentifier: String, driverTemp: Double, passengerTemp: Double) {
self.accessToken = accessToken
self.vehicleIdentifier = vehicleIdentifier
self.driverTemp = driverTemp
self.passengerTemp = passengerTemp
}

public func execute(completion: @escaping (Result<Bool>) -> Void) {
WebRequest.request(
path: path,
method: method,
params: ["driver_temp": "\(driverTemp)", "passenger_temp": "\(passengerTemp)"],
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
completion(Result.failure(error))
}
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
DispatchQueue.main.async {
completion(Result.success(resultBool))
}
} else {
DispatchQueue.main.async {
completion(Result.failure(APIError()))
}
}
}
}
}
Loading