Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jonasman/TeslaSwift
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasman committed Dec 28, 2023
2 parents 64805b8 + 08ece50 commit 4bb677c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 97 deletions.
135 changes: 66 additions & 69 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: SiteId) -> 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: SiteId) -> 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: SiteId) -> 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: SiteId, 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: BatteryId) -> Future<BatteryStatus, Error> {
let future = Future<BatteryStatus, Error> { (subscriber: @escaping (Result<BatteryStatus, Error>) -> Void) in

public func getBatteryStatus(batteryID: String) -> Future<BatteryStatus, Error> {
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: BatteryId) -> 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: BatteryId) -> 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 @@ -27,14 +27,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 @@ -59,9 +59,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
Loading

0 comments on commit 4bb677c

Please sign in to comment.