Skip to content

Commit

Permalink
Merge pull request #157 from andoma93/master
Browse files Browse the repository at this point in the history
Refactored Combine
  • Loading branch information
jonasman authored Dec 6, 2023
2 parents e57bb3e + ab7dd2d commit 08ece50
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 94 deletions.
129 changes: 63 additions & 66 deletions Sources/Extensions/Combine/TeslaSwift+Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,228 +11,225 @@ import Combine
import TeslaSwift

extension TeslaSwift {
public func revokeWeb() -> Future<Bool, Error> {
let future = Future<Bool, Error> { (subscriber: @escaping (Result<Bool, Error>) -> Void) in
public func revokeToken() -> Future<Bool, Error> {
Future { promise in
Task {
do {
let result = try await self.revokeWeb()
subscriber(.success(result))
let result = try await self.revokeToken()
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getVehicles() -> Future<[Vehicle], Error> {
let future = Future<[Vehicle], Error> { (subscriber: @escaping (Result<[Vehicle], Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getVehicles()
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getVehicle(_ vehicleID: String) -> Future<Vehicle, Error> {
let future = Future<Vehicle, Error> { (subscriber: @escaping (Result<Vehicle, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getVehicle(vehicleID)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getVehicle(_ vehicle: Vehicle) -> Future<Vehicle, Error> {
let future = Future<Vehicle, Error> { (subscriber: @escaping (Result<Vehicle, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getVehicle(vehicle)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getAllData(_ vehicle: Vehicle) -> Future<VehicleExtended, Error> {
let future = Future<VehicleExtended, Error> { (subscriber: @escaping (Result<VehicleExtended, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getAllData(vehicle)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getVehicleMobileAccessState(_ vehicle: Vehicle) -> Future<Bool, Error> {
let future = Future<Bool, Error> { (subscriber: @escaping (Result<Bool, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getVehicleMobileAccessState(vehicle)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func wakeUp(_ vehicle: Vehicle) -> Future<Vehicle, Error> {
let future = Future<Vehicle, Error> { (subscriber: @escaping (Result<Vehicle, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.wakeUp(vehicle)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func sendCommandToVehicle(_ vehicle: Vehicle, command: VehicleCommand) -> Future<CommandResponse, Error> {
let future = Future<CommandResponse, Error> { (subscriber: @escaping (Result<CommandResponse, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.sendCommandToVehicle(vehicle, command: command)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
}

public func getNearbyChargingSite(vehicle: Vehicle) -> Future<NearbyChargingSites, Error> {
Future { promise in
Task {
do {
let result = try await self.getNearbyChargingSites(vehicle)
promise(.success(result))
} catch let error {
promise(.failure(error))
}
}
}
return future
}

public func getProducts() -> Future<[Product], Error> {
let future = Future<[Product], Error> { (subscriber: @escaping (Result<[Product], Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getProducts()
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getEnergySiteStatus(siteID: String) -> Future<EnergySiteStatus, Error> {
let future = Future<EnergySiteStatus, Error> { (subscriber: @escaping (Result<EnergySiteStatus, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getEnergySiteStatus(siteID: siteID)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getEnergySiteLiveStatus(siteID: String) -> Future<EnergySiteLiveStatus, Error> {
let future = Future<EnergySiteLiveStatus, Error> { (subscriber: @escaping (Result<EnergySiteLiveStatus, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getEnergySiteLiveStatus(siteID: siteID)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getEnergySiteInfo(siteID: String) -> Future<EnergySiteInfo, Error> {
let future = Future<EnergySiteInfo, Error> { (subscriber: @escaping (Result<EnergySiteInfo, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getEnergySiteInfo(siteID: siteID)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getEnergySiteHistory(siteID: String, period: EnergySiteHistory.Period) -> Future<EnergySiteHistory, Error> {
let future = Future<EnergySiteHistory, Error> { (subscriber: @escaping (Result<EnergySiteHistory, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getEnergySiteHistory(siteID: siteID, period: period)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getBatteryStatus(batteryID: String) -> Future<BatteryStatus, Error> {
let future = Future<BatteryStatus, Error> { (subscriber: @escaping (Result<BatteryStatus, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getBatteryStatus(batteryID: batteryID)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getBatteryData(batteryID: String) -> Future<BatteryData, Error> {
let future = Future<BatteryData, Error> { (subscriber: @escaping (Result<BatteryData, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getBatteryData(batteryID: batteryID)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}

public func getBatteryPowerHistory(batteryID: String) -> Future<BatteryPowerHistory, Error> {
let future = Future<BatteryPowerHistory, Error> { (subscriber: @escaping (Result<BatteryPowerHistory, Error>) -> Void) in
Future { promise in
Task {
do {
let result = try await self.getBatteryPowerHistory(batteryID: batteryID)
subscriber(.success(result))
promise(.success(result))
} catch let error {
subscriber(.failure(error))
promise(.failure(error))
}
}
}
return future
}
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/TeslaSwift/Model/EnergySite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ open class EnergySite: Codable {

// Also available in EnergySiteStatus
open var resourceType: String
open var siteName: String
open var gatewayID: String
open var energyLeft: Double
open var totalPackEnergy: Double
open var percentageCharged: Double
open var batteryType: String
open var backupCapable: Bool
open var batteryPower: Double
open var siteName: String?
open var gatewayID: String?
open var energyLeft: Double?
open var totalPackEnergy: Double?
open var percentageCharged: Double?
open var batteryType: String?
open var backupCapable: Bool?
open var batteryPower: Double?
open var syncGridAlertEnabled: Bool
open var breakerAlertEnabled: Bool

Expand All @@ -51,9 +51,9 @@ open class EnergySite: Codable {
// MARK: - Components
open class Components: Codable {
open var battery: Bool
open var batteryType: String
open var batteryType: String?
open var solar: Bool
open var solarType: String
open var solarType: String?
open var grid: Bool
open var loadMeter: Bool
open var marketType: String
Expand Down
36 changes: 18 additions & 18 deletions Sources/TeslaSwift/Model/EnergySiteInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import Foundation
// MARK: - EnergySiteInfo
open class EnergySiteInfo: Codable {
open var id: String
open var siteName: String
open var backupReservePercent: Double
open var defaultRealMode: String
open var siteName: String?
open var backupReservePercent: Double?
open var defaultRealMode: String?
open var installationDate: Date
open var version: String
open var batteryCount: Int
open var nameplatePower: Double
open var nameplateEnergy: Double
open var version: String?
open var batteryCount: Int?
open var nameplatePower: Double?
open var nameplateEnergy: Double?
open var installationTimeZone: String
open var offGridVehicleChargingReservePercent: Double
open var offGridVehicleChargingReservePercent: Double?

open var userSettings: UserSettings
open var touSettings: TOUSettings
open var touSettings: TOUSettings?
open var components: Components

enum CodingKeys: String, CodingKey {
Expand All @@ -45,25 +45,25 @@ open class EnergySiteInfo: Codable {
// MARK: - Components
open class Components: Codable {
open var solar: Bool
open var solarType: String
open var solarType: String?
open var battery: Bool
open var grid: Bool
open var backup: Bool
open var gateway: String
open var loadMeter: Bool
open var touCapable: Bool
open var stormModeCapable: Bool
open var touCapable: Bool?
open var stormModeCapable: Bool?
open var flexEnergyRequestCapable: Bool
open var carChargingDataSupported: Bool
open var offGridVehicleChargingReserveSupported: Bool
open var vehicleChargingPerformanceViewEnabled: Bool
open var vehicleChargingSolarOffsetViewEnabled: Bool
open var batterySolarOffsetViewEnabled: Bool
open var setIslandingModeEnabled: Bool
open var backupTimeRemainingEnabled: Bool
open var batteryType: String
open var configurable: Bool
open var gridServicesEnabled: Bool
open var setIslandingModeEnabled: Bool?
open var backupTimeRemainingEnabled: Bool?
open var batteryType: String?
open var configurable: Bool?
open var gridServicesEnabled: Bool?

enum CodingKeys: String, CodingKey {
case solar
Expand Down Expand Up @@ -114,7 +114,7 @@ open class EnergySiteInfo: Codable {

// MARK: - UserSettings
open class UserSettings: Codable {
open var stormModeEnabled: Bool
open var stormModeEnabled: Bool?
open var syncGridAlertEnabled: Bool
open var breakerAlertEnabled: Bool

Expand Down

0 comments on commit 08ece50

Please sign in to comment.