Add CommandV1

This commit is contained in:
Zhigang Fang
2017-10-26 21:28:30 +08:00
parent 539400d5a9
commit 6038bed3cb
7 changed files with 36 additions and 29 deletions

View File

@@ -14,7 +14,7 @@ extension OSCKit {
public func usingVersion2_1() -> Promise<Void> {
return async {
let session = try await(self.session)
try await(self.execute(command: .setOptions(options: [APIVersion.v2_1], sessionId: session.id)))
try await(self.execute(command: CommandV1.setOptions(options: [APIVersion.v2_1], sessionId: session.id)))
}
}

View File

@@ -11,7 +11,25 @@ import SwiftyyJSON
// swiftlint:disable identifier_name
// We want to keep it as close to API as possible
enum Command {
protocol Command {
var name: String { get }
var json: JSON { get }
}
extension Command {
var defaultJSON: JSON {
return ["name": self.name]
}
func with(params: [String: Any]) -> JSON {
return [
"name": self.name,
"parameters": params
]
}
}
enum CommandV1: Command {
case startSession
case updateSession(sessionId: String)
case closeSession(sessionId: String)
@@ -34,7 +52,7 @@ enum Command {
}
// swiftlint:enable identifier_name
extension Command {
extension CommandV1 {
var name: String {
switch self {
case .startSession: return "camera.startSession"
@@ -59,17 +77,6 @@ extension Command {
}
}
var defaultJSON: JSON {
return ["name": self.name]
}
func with(params: [String: Any]) -> JSON {
return [
"name": self.name,
"parameters": params
]
}
var json: JSON {
switch self {
case .updateSession(sessionId: let id): return with(params: ["sessionId": id])

View File

@@ -38,7 +38,7 @@ extension OSCKit {
if FileManager.default.fileExists(atPath: fileURL.path) {
return fileURL
}
let data = try await(self.requestData(command: .getImage(fileUri: url, _type: type)))
let data = try await(self.requestData(command: CommandV1.getImage(fileUri: url, _type: type)))
try data.write(to: fileURL)
return fileURL
}
@@ -47,9 +47,9 @@ extension OSCKit {
public func takePicture(format: FileFormat = .smallImage) -> Promise<String> {
return async {
let session = try await(self.session)
try await(self.execute(command: .setOptions(options: [CaptureMode.image], sessionId: session.id)))
try await(self.execute(command: .setOptions(options: [format], sessionId: session.id)))
let captureResponse = try await(self.execute(command: .takePicture(sessionId: session.id)))
try await(self.execute(command: CommandV1.setOptions(options: [CaptureMode.image], sessionId: session.id)))
try await(self.execute(command: CommandV1.setOptions(options: [format], sessionId: session.id)))
let captureResponse = try await(self.execute(command: CommandV1.takePicture(sessionId: session.id)))
let statusID = try captureResponse["id"].string !! SDKError.unableToParse(captureResponse)
let statusResponse = try await(self.waitForStatus(id: statusID))
return try statusResponse["results"]["fileUri"].string !! SDKError.unableToParse(statusResponse)

View File

@@ -79,7 +79,7 @@ extension OSCKit {
public func startLivePreview(callback: @escaping (UIImage?) -> Void) {
async {
let session = try await(OSCKit.shared.session)
try await(self.execute(command: .setOptions(options: [CaptureMode.image], sessionId: session.id)))
try await(self.execute(command: CommandV1.setOptions(options: [CaptureMode.image], sessionId: session.id)))
DispatchQueue.main.async(execute: {
LivePreview.shared.stop()
LivePreview.shared.callback = callback
@@ -89,7 +89,7 @@ extension OSCKit {
self?.startLivePreview(callback: callback)
})
}
let json = Command._getLivePreview(sessionId: session.id).json
let json = CommandV1._getLivePreview(sessionId: session.id).json
let request = self.assembleRequest(endPoint: .execute, params: json)
LivePreview.shared.play(request: request)
})

View File

@@ -45,7 +45,7 @@ public struct MediaItem {
extension OSCKit {
public var listAllMediaItems: Promise<[MediaItem]> {
return async {
let all = try await(self.execute(command: ._listAll(entryCount: 100, detail: false)))
let all = try await(self.execute(command: CommandV1._listAll(entryCount: 100, detail: false)))
let entries = try all["results"]["entries"].array !! SDKError.unableToParse(all)
return try entries.map({try MediaItem(json: $0)})
}
@@ -56,7 +56,7 @@ extension OSCKit {
if timeout < 0 {
throw SDKError.fetchTimeout
}
let all = try await(self.execute(command: ._listAll(entryCount: 1, detail: true)))
let all = try await(self.execute(command: CommandV1._listAll(entryCount: 1, detail: true)))
let json = try all["results"]["entries"].array !! SDKError.unableToParse(all)
if let first = json.first {
let item = try MediaItem(json: first)

View File

@@ -65,7 +65,7 @@ extension OSCKit {
var startSession: Promise<Session> {
return async {
let response = try await(self.execute(command: .startSession))
let response = try await(self.execute(command: CommandV1.startSession))
let session = try Session(json: response)
Session.currentSession = session
return session
@@ -75,7 +75,7 @@ extension OSCKit {
func update(session: Session) -> Promise<Session> {
return async {
do {
let response = try await(self.execute(command: .updateSession(sessionId: session.id)))
let response = try await(self.execute(command: CommandV1.updateSession(sessionId: session.id)))
let session = try Session(json: response)
Session.currentSession = session
return session
@@ -88,7 +88,7 @@ extension OSCKit {
public func end() -> Promise<Void> {
return async {
let session = try await(self.session)
try await(self.execute(command: ._finishWlan(sessionId: session.id)))
try await(self.execute(command: CommandV1._finishWlan(sessionId: session.id)))
}
}
}

View File

@@ -22,8 +22,8 @@ extension OSCKit {
public func startCapture(mode: VideoCaptureMode = .interval) -> Promise<JSON> {
return async {
let session = try await(self.session)
try await(self.execute(command: .setOptions(options: [CaptureMode.video], sessionId: session.id)))
return try await(self.execute(command: ._startCapture(sessionId: session.id, mode: mode)))
try await(self.execute(command: CommandV1.setOptions(options: [CaptureMode.video], sessionId: session.id)))
return try await(self.execute(command: CommandV1._startCapture(sessionId: session.id, mode: mode)))
}
}
@@ -35,7 +35,7 @@ extension OSCKit {
// This is due to the face THETA API v2.0 does not return a file URL when capture finishes
// https://developers.theta360.com/en/docs/v2.0/api_reference/commands/camera._stop_capture.html
let lastItem = try await(self.getLatestMediaItem(withPredicate: const(value: true)))
try await(self.execute(command: ._stopCapture(sessionId: session.id)))
try await(self.execute(command: CommandV1._stopCapture(sessionId: session.id)))
// After stop capturing video, wait until it returns a new item with type being .video
let mediaItem = try await(self.getLatestMediaItem(withPredicate: {
$0.url != lastItem.url && $0.type ~= .video
@@ -55,7 +55,7 @@ extension OSCKit {
if FileManager.default.fileExists(atPath: fileURL.path) {
return fileURL
}
let data = try await(self.requestData(command: ._getVideo(fileUri: url, _type: type)))
let data = try await(self.requestData(command: CommandV1._getVideo(fileUri: url, _type: type)))
try data.write(to: fileURL)
return fileURL
}