Skip to content

Commit

Permalink
Added new data API
Browse files Browse the repository at this point in the history
Added new timestamp field to data model
Bumped version
  • Loading branch information
jonasman committed Mar 12, 2017
1 parent dc15be1 commit df19ba8
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Sources/ChargeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ open class ChargeState: Mappable {
Only valid while charging
*/
open internal(set) var timeToFullCharge: Double?
open internal(set) var timeStamp: Date?

open internal(set) var tripCharging: Bool?

Expand Down Expand Up @@ -199,6 +200,8 @@ open class ChargeState: Mappable {

timeToFullCharge <- map["time_to_full_charge"]

timeStamp <- (map["timestamp"], TeslaTimeStampTransform())

tripCharging <- map["trip_charging"]

usableBatteryLevel <- map["usable_battery_level"]
Expand Down
4 changes: 4 additions & 0 deletions Sources/ClimateState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ open class ClimateState: Mappable {

open var smartPreconditioning: Int?

open var timeStamp: Date?

public required init?(map: Map) { }

open func mapping(map: Map) {
Expand Down Expand Up @@ -110,6 +112,8 @@ open class ClimateState: Mappable {


smartPreconditioning <- map["smart_preconditioning"]

timeStamp <- (map["timestamp"], TeslaTimeStampTransform())

}
}
3 changes: 3 additions & 0 deletions Sources/DriveState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ open class DriveState: Mappable {
open var longitude: CLLocationDegrees?
open var heading: CLLocationDirection?
open var date: Date?
open var timeStamp: Date?


open var position: CLLocation? {
if let latitude = latitude,
Expand Down Expand Up @@ -47,6 +49,7 @@ open class DriveState: Mappable {
longitude <- map["longitude"]
heading <- map["heading"]
date <- (map["gps_as_of"], DateTransform())
timeStamp <- (map["timestamp"], TeslaTimeStampTransform())
}


Expand Down
2 changes: 2 additions & 0 deletions Sources/GuiSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ open class GuiSettings: Mappable {
open var chargeRateUnits: String?
open var time24Hours: Bool?
open var rangeDisplay: String?
open var timeStamp: Date?

required public init?(map: Map) { }

Expand All @@ -25,5 +26,6 @@ open class GuiSettings: Mappable {
chargeRateUnits <- map["gui_charge_rate_units"]
time24Hours <- map["gui_24_hour_time"]
rangeDisplay <- map["gui_range_display"]
timeStamp <- (map["timestamp"], TeslaTimeStampTransform())
}
}
5 changes: 4 additions & 1 deletion Sources/TeslaEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum Endpoint {
case authentication
case vehicles
case mobileAccess(vehicleID: Int)
case allStates(vehicleID: Int)
case chargeState(vehicleID: Int)
case climateState(vehicleID: Int)
case driveState(vehicleID: Int)
Expand All @@ -31,6 +32,8 @@ extension Endpoint {
return "/api/1/vehicles"
case .mobileAccess(let vehicleID):
return "/api/1/vehicles/\(vehicleID)/mobile_enabled"
case .allStates(let vehicleID):
return "/api/1/vehicles/\(vehicleID)/data"
case .chargeState(let vehicleID):
return "/api/1/vehicles/\(vehicleID)/data_request/charge_state"
case .climateState(let vehicleID):
Expand All @@ -50,7 +53,7 @@ extension Endpoint {
switch self {
case .authentication, .command:
return "POST"
case .vehicles, .mobileAccess, .chargeState, .climateState, .driveState, .guiSettings, .vehicleState:
case .vehicles, .mobileAccess, .allStates, .chargeState, .climateState, .driveState, .guiSettings, .vehicleState:
return "GET"
}
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/TeslaSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ extension TeslaSwift {

}

public func getAllData(_ vehicle: Vehicle) -> Promise<VehicleExtended> {
return checkAuthentication().then(on: .global()) {
(token) -> Promise<Response<VehicleExtended>> in

let vehicleID = vehicle.id!

return self.request(.allStates(vehicleID: vehicleID))

}.then(on: .global()) {
(data: Response<VehicleExtended>) -> VehicleExtended in

data.response
}
}

/**
Fetchs the vehicle mobile access state

Expand Down
32 changes: 32 additions & 0 deletions Sources/TeslaTimeStampTransform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// TeslaTimeStampTransform.swift
// TeslaSwift
//
// Created by Joao Nunes on 12/03/2017.
// Copyright © 2017 Joao Nunes. All rights reserved.
//

import Foundation
import ObjectMapper

class TeslaTimeStampTransform: DateTransform {

open override func transformFromJSON(_ value: Any?) -> Date? {
if let timeInt = value as? Double {
return Date(timeIntervalSince1970: TimeInterval(timeInt/1000))
}

if let timeStr = value as? String {
return Date(timeIntervalSince1970: TimeInterval(atof(timeStr)))
}

return nil
}

open override func transformToJSON(_ value: Date?) -> Double? {
if let date = value {
return Double(date.timeIntervalSince1970 * 1000)
}
return nil
}
}
59 changes: 59 additions & 0 deletions Sources/VehicleConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// VehicleConfig.swift
// TeslaSwift
//
// Created by Joao Nunes on 12/03/2017.
// Copyright © 2017 Joao Nunes. All rights reserved.
//

import Foundation
import ObjectMapper

open class VehicleConfig: Mappable {

open var carSpecialType: String?
open var carType: String?
open var euVehicle: Bool?
open var exteriorColor: String?
open var hasLudicoursMode: Bool?
open var motorizedChargePort: Bool?
open var perfConfig: String?
open var rearSeatHeaters: Bool?
open var rearSeatType: Int?
open var rhd: Bool?
open var roofColor: String? // "None" for panoramic roof
open var seatType: Int?
open var spoilerType: String?
open var sunRoofInstalled: Bool?
open var thirdRowSeats: String?
open var timeStamp: Date?
open var trimBadging: String?
open var wheelType: String?

required public init?(map: Map) {

}

open func mapping(map: Map) {

carSpecialType <- map["car_special_type"]
carType <- map["car_type"]
euVehicle <- map["eu_vehicle"]
exteriorColor <- map["exterior_color"]
hasLudicoursMode <- map["has_ludicrous_mode"]
motorizedChargePort <- map["motorized_charge_port"]
perfConfig <- map["perf_config"]
rearSeatHeaters <- map["rear_seat_heaters"]
rearSeatType <- map["rear_seat_type"]
rhd <- map["rhd"]
roofColor <- map["roof_color"]
seatType <- map["seat_type"]
spoilerType <- map["spoiler_type"]
sunRoofInstalled <- map["sun_roof_installed"]
thirdRowSeats <- map["third_row_seats"]
timeStamp <- (map["timestamp"], TeslaTimeStampTransform())
trimBadging <- map["trim_badging"]
wheelType <- map["wheel_type"]
}

}
35 changes: 35 additions & 0 deletions Sources/VehicleExtended.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// VehicleExtended.swift
// TeslaSwift
//
// Created by Joao Nunes on 12/03/2017.
// Copyright © 2017 Joao Nunes. All rights reserved.
//

import Foundation
import ObjectMapper

open class VehicleExtended: Vehicle {

open var userId: Int?
open var chargeState: ChargeState?
open var climateState: ClimateState?
open var driveState: DriveState?
open var guiSettings: GuiSettings?
open var vehicleConfig: VehicleConfig?
open var vehicleState: VehicleState?


open override func mapping(map: Map) {
super.mapping(map: map)

userId <- map["user_id"]
chargeState <- map["charge_state"]
climateState <- map["climate_state"]
driveState <- map["drive_state"]
guiSettings <- map["gui_settings"]
vehicleConfig <- map["vehicle_config"]
vehicleState <- map["vehicle_state"]
}

}
4 changes: 4 additions & 0 deletions Sources/VehicleState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ open class VehicleState: Mappable {

open var thirdRowSeats: String?

open var timeStamp: Date?

open var valetMode: Bool?
open var valetPinNeeded: Bool?

Expand Down Expand Up @@ -148,6 +150,8 @@ open class VehicleState: Mappable {

thirdRowSeats <- map["third_row_seats"]

timeStamp <- (map["timestamp"], TeslaTimeStampTransform())

valetMode <- map["valet_mode"]
valetPinNeeded <- map["valet_pin_needed"]

Expand Down
2 changes: 1 addition & 1 deletion TeslaSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TeslaSwift"
s.version = "3.3.6"
s.version = "3.4.0"
s.summary = "Swift library to access the Tesla Model S API."

s.homepage = "https://github.com/jonasman/TeslaSwift"
Expand Down
28 changes: 28 additions & 0 deletions TeslaSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@
CFD2E7D11C8A13D60005E882 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CFD2E7CF1C8A13D60005E882 /* LaunchScreen.storyboard */; };
CFD2E7DC1C8A13D60005E882 /* TeslaSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFD2E7DB1C8A13D60005E882 /* TeslaSwiftTests.swift */; };
CFD2E7E81C8A13F20005E882 /* TeslaSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFD2E7E71C8A13F20005E882 /* TeslaSwift.swift */; };
CFF001A61E755C4A00B8139B /* AllStates.json in Resources */ = {isa = PBXBuildFile; fileRef = CFF001A51E755C4A00B8139B /* AllStates.json */; };
CFF001A81E755CF800B8139B /* VehicleExtended.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001A71E755CF800B8139B /* VehicleExtended.swift */; };
CFF001A91E755CF800B8139B /* VehicleExtended.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001A71E755CF800B8139B /* VehicleExtended.swift */; };
CFF001AA1E755CF800B8139B /* VehicleExtended.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001A71E755CF800B8139B /* VehicleExtended.swift */; };
CFF001AC1E7561EB00B8139B /* VehicleConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001AB1E7561EB00B8139B /* VehicleConfig.swift */; };
CFF001AD1E7561EB00B8139B /* VehicleConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001AB1E7561EB00B8139B /* VehicleConfig.swift */; };
CFF001AE1E7561EB00B8139B /* VehicleConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001AB1E7561EB00B8139B /* VehicleConfig.swift */; };
CFF001B01E7565DB00B8139B /* TeslaTimeStampTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001AF1E7565DB00B8139B /* TeslaTimeStampTransform.swift */; };
CFF001B11E7565DB00B8139B /* TeslaTimeStampTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001AF1E7565DB00B8139B /* TeslaTimeStampTransform.swift */; };
CFF001B21E7565DB00B8139B /* TeslaTimeStampTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF001AF1E7565DB00B8139B /* TeslaTimeStampTransform.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -207,6 +217,10 @@
CFD2E7DD1C8A13D60005E882 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
CFD2E7E71C8A13F20005E882 /* TeslaSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TeslaSwift.swift; sourceTree = "<group>"; };
CFD950661C8A44F4008397BD /* Pods.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Pods.framework; path = "Pods/../build/Debug-iphoneos/Pods.framework"; sourceTree = "<group>"; };
CFF001A51E755C4A00B8139B /* AllStates.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = AllStates.json; sourceTree = "<group>"; };
CFF001A71E755CF800B8139B /* VehicleExtended.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VehicleExtended.swift; sourceTree = "<group>"; };
CFF001AB1E7561EB00B8139B /* VehicleConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VehicleConfig.swift; sourceTree = "<group>"; };
CFF001AF1E7565DB00B8139B /* TeslaTimeStampTransform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TeslaTimeStampTransform.swift; sourceTree = "<group>"; };
E6CE3C92808A0629E156F4D1 /* Pods-TeslaSwift-TeslaSwiftTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TeslaSwift-TeslaSwiftTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-TeslaSwift-TeslaSwiftTests/Pods-TeslaSwift-TeslaSwiftTests.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -258,6 +272,7 @@
CF0D2F181D95A49000E5A304 /* Model */ = {
isa = PBXGroup;
children = (
CFF001AF1E7565DB00B8139B /* TeslaTimeStampTransform.swift */,
CF0D2F191D95A4A900E5A304 /* Authtentication.swift */,
CFB5D7B61E66088F003EA1EF /* ErrorMessage.swift */,
CF0D2F1A1D95A4A900E5A304 /* ChargeState.swift */,
Expand All @@ -273,6 +288,8 @@
CF1BD5981DD505EF00E4C493 /* SetSunRoofCommandOptions.swift */,
CF1BD59B1DD5070100E4C493 /* RemoteStartDriveCommandOptions.swift */,
CF0D2F221D95A4A900E5A304 /* Vehicle.swift */,
CFF001A71E755CF800B8139B /* VehicleExtended.swift */,
CFF001AB1E7561EB00B8139B /* VehicleConfig.swift */,
CF0D2F241D95A4A900E5A304 /* VehicleState.swift */,
);
name = Model;
Expand All @@ -295,6 +312,7 @@
isa = PBXGroup;
children = (
CF26AA221C99CE8400090B20 /* Authentication.json */,
CFF001A51E755C4A00B8139B /* AllStates.json */,
CF1492AE1D2BF87A0061088C /* AuthenticationFailed.json */,
CF26AA241C99CF9300090B20 /* Vehicles.json */,
CF26AA261C99D11700090B20 /* MobileAccess.json */,
Expand Down Expand Up @@ -566,6 +584,7 @@
CFA984F51CBD81C400B23C7D /* StartCharging.json in Resources */,
CF1492AF1D2BF87A0061088C /* AuthenticationFailed.json in Resources */,
CF37B81D1C9B40200000E894 /* GuiSettings.json in Resources */,
CFF001A61E755C4A00B8139B /* AllStates.json in Resources */,
CFA984E81CBD730500B23C7D /* ResetValetPin.json in Resources */,
CF26AA2B1C99D13B00090B20 /* ClimateSettings.json in Resources */,
CF5D13A71CB41BAE00744DCC /* WakeUp.json in Resources */,
Expand Down Expand Up @@ -733,7 +752,10 @@
CF25069F1E4603CF00D0E08A /* OpenTrunkOptions.swift in Sources */,
CF25068A1E46034800D0E08A /* AppDelegate.swift in Sources */,
CF2506A31E4603CF00D0E08A /* SetSunRoofCommandOptions.swift in Sources */,
CFF001AE1E7561EB00B8139B /* VehicleConfig.swift in Sources */,
CF25069B1E4603CF00D0E08A /* CommandResponse.swift in Sources */,
CFF001AA1E755CF800B8139B /* VehicleExtended.swift in Sources */,
CFF001B21E7565DB00B8139B /* TeslaTimeStampTransform.swift in Sources */,
CF25069C1E4603CF00D0E08A /* DriveState.swift in Sources */,
CF2506991E4603CF00D0E08A /* ChargeState.swift in Sources */,
);
Expand All @@ -754,16 +776,19 @@
CFD2E7E81C8A13F20005E882 /* TeslaSwift.swift in Sources */,
CF0D2F3B1D95A4A900E5A304 /* VehicleState.swift in Sources */,
CF0D2F271D95A4A900E5A304 /* ChargeState.swift in Sources */,
CFF001A81E755CF800B8139B /* VehicleExtended.swift in Sources */,
CF0D2F351D95A4A900E5A304 /* ValetCommandOptions.swift in Sources */,
CF14238D1DF360A500CB4D96 /* ViewController+TeslaSwift.swift in Sources */,
CF0D2F371D95A4A900E5A304 /* Vehicle.swift in Sources */,
CFD2E7C51C8A13D60005E882 /* AppDelegate.swift in Sources */,
CF0D2F2B1D95A4A900E5A304 /* CommandResponse.swift in Sources */,
CFF001B01E7565DB00B8139B /* TeslaTimeStampTransform.swift in Sources */,
CF0D2F291D95A4A900E5A304 /* ClimateState.swift in Sources */,
CF0D2F251D95A4A900E5A304 /* Authtentication.swift in Sources */,
CF1BD5991DD505EF00E4C493 /* SetSunRoofCommandOptions.swift in Sources */,
CF0D2F311D95A4A900E5A304 /* GuiSettings.swift in Sources */,
CF1BD5961DD5051600E4C493 /* SetTemperatureCommandOptions.swift in Sources */,
CFF001AC1E7561EB00B8139B /* VehicleConfig.swift in Sources */,
CF0D2F2D1D95A4A900E5A304 /* DriveState.swift in Sources */,
CF0D2F331D95A4A900E5A304 /* OpenTrunkOptions.swift in Sources */,
CF1BD59C1DD5070100E4C493 /* RemoteStartDriveCommandOptions.swift in Sources */,
Expand Down Expand Up @@ -791,7 +816,10 @@
CF0D2F2C1D95A4A900E5A304 /* CommandResponse.swift in Sources */,
CF0D2F321D95A4A900E5A304 /* GuiSettings.swift in Sources */,
CF4D4D0E1D81F46000CCE6ED /* TeslaSwift.swift in Sources */,
CFF001AD1E7561EB00B8139B /* VehicleConfig.swift in Sources */,
CF0D2F361D95A4A900E5A304 /* ValetCommandOptions.swift in Sources */,
CFF001A91E755CF800B8139B /* VehicleExtended.swift in Sources */,
CFF001B11E7565DB00B8139B /* TeslaTimeStampTransform.swift in Sources */,
CFB5D7B81E66088F003EA1EF /* ErrorMessage.swift in Sources */,
CF4D4D0F1D81F46B00CCE6ED /* TeslaEndpoint.swift in Sources */,
);
Expand Down
10 changes: 9 additions & 1 deletion TeslaSwiftDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="49e-Tb-3d3">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="49e-Tb-3d3">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand Down Expand Up @@ -209,6 +209,14 @@
<action selector="getVehicleState:" destination="hQa-7w-LXN" eventType="touchUpInside" id="l7O-Ay-bFQ"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ySF-V2-hJS">
<rect key="frame" x="266" y="101" width="46" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="all"/>
<connections>
<action selector="gettAll:" destination="hQa-7w-LXN" eventType="touchUpInside" id="VUo-KP-vxg"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
Expand Down
Loading

0 comments on commit df19ba8

Please sign in to comment.