Skip to content

Commit

Permalink
Add stt data request
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Mar 14, 2024
1 parent 0c97129 commit f901be3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Source/HAConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@ public protocol HAConnection: AnyObject {

/// Write data to websocket connection
/// - Parameters:
/// - request: The data request containing data to be written
func write(_ dataRequest: HARequest)
/// - sttDataRequest: The data request containing sttBinaryHandlerId and data (as base64 string) to be written
func write(_ sttDataRequest: HARequest)
}
25 changes: 13 additions & 12 deletions Source/Internal/HAConnectionImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,10 @@ internal class HAConnectionImpl: HAConnection {

// MARK: - Write

public func write(_ dataRequest: HARequest) {
if case .data = dataRequest.type {
defer { connectAutomaticallyIfNeeded() }
let invocation = HARequestInvocationSingle(request: dataRequest) { _ in }
requestController.add(invocation)
} else {
HAGlobal.log(.error, "Write operation can only be executed by data HARequest")
}
public func write(_ sttDataRequest: HARequest) {
defer { connectAutomaticallyIfNeeded() }
let invocation = HARequestInvocationSingle(request: sttDataRequest) { _ in }
requestController.add(invocation)
}
}

Expand Down Expand Up @@ -404,9 +400,14 @@ extension HAConnectionImpl {
}
}

private func sendWrite(_ data: Data) {
private func sendWrite(_ sttBinaryHandlerId: UInt8, audioDataString: String?) {
// If there is no audioData, handlerID will be the payload alone indicating end of audio
var audioData = Data(base64Encoded: audioDataString ?? "") ?? Data()

Check warning on line 405 in Source/Internal/HAConnectionImpl.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/HAConnectionImpl.swift#L403-L405

Added lines #L403 - L405 were not covered by tests

// Prefix audioData with handler ID so the API can map the binary data
audioData.insert(sttBinaryHandlerId, at: 0)
workQueue.async { [connection] in
connection?.write(data: data)
connection?.write(data: audioData)

Check warning on line 410 in Source/Internal/HAConnectionImpl.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/HAConnectionImpl.swift#L407-L410

Added lines #L407 - L410 were not covered by tests
}
}

Expand All @@ -419,8 +420,8 @@ extension HAConnectionImpl {
sendWebSocket(identifier: identifier, request: request, command: command)
case let .rest(method, command):
sendRest(identifier: identifier!, request: request, method: method, command: command)
case let .data(data):
sendWrite(data)
case let .sttData(sttBinaryHandlerId):
sendWrite(sttBinaryHandlerId, audioDataString: request.data["audioData"] as? String)

Check warning on line 424 in Source/Internal/HAConnectionImpl.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/HAConnectionImpl.swift#L424

Added line #L424 was not covered by tests
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Internal/RequestController/HARequestController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ internal struct HARequestControllerAllowedSendKind: OptionSet {

static let webSocket: Self = .init(rawValue: 0b1)
static let rest: Self = .init(rawValue: 0b10)
static let data: Self = .init(rawValue: 0b10)
static let all: Self = [.webSocket, .rest, .data]
static let sttData: Self = .init(rawValue: 0b10)
static let all: Self = [.webSocket, .rest, .sttData]

func allows(requestType: HARequestType) -> Bool {
switch requestType {
case .webSocket:
return contains(.webSocket)
case .rest:
return contains(.rest)
case .data:
return contains(.data)
case .sttData:
return contains(.sttData)

Check warning on line 18 in Source/Internal/RequestController/HARequestController.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/RequestController/HARequestController.swift#L18

Added line #L18 was not covered by tests
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Source/Requests/HARequestType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public enum HARequestType: Hashable, Comparable, ExpressibleByStringLiteral {
case webSocket(String)
/// Sent over REST, the HTTP method to use and the post-`api/` path
case rest(HAHTTPMethod, String)
/// Sent over WebSocket, the binary data to write
case data(Data)
/// Sent over WebSocket, the stt binary handler id
case sttData(UInt8)

/// Create a WebSocket request type by string literal
/// - Parameter value: The name of the WebSocket command
Expand All @@ -20,15 +20,15 @@ public enum HARequestType: Hashable, Comparable, ExpressibleByStringLiteral {
switch self {
case let .webSocket(command), let .rest(_, command):
return command
case .data:
case .sttData:
return ""

Check warning on line 24 in Source/Requests/HARequestType.swift

View check run for this annotation

Codecov / codecov/patch

Source/Requests/HARequestType.swift#L24

Added line #L24 was not covered by tests
}
}

/// The request is issued outside of the lifecycle of a connection
public var isPerpetual: Bool {
switch self {
case .webSocket, .data: return false
case .webSocket, .sttData: return false
case .rest: return true
}
}
Expand All @@ -45,7 +45,7 @@ public enum HARequestType: Hashable, Comparable, ExpressibleByStringLiteral {
case let (.webSocket(lhsCommand), .webSocket(rhsCommand)),
let (.rest(_, lhsCommand), .rest(_, rhsCommand)):
return lhsCommand < rhsCommand
case (.data, _), (_, .data):
case (.sttData, _), (_, .sttData):
return false

Check warning on line 49 in Source/Requests/HARequestType.swift

View check run for this annotation

Codecov / codecov/patch

Source/Requests/HARequestType.swift#L49

Added line #L49 was not covered by tests
}
}
Expand Down
12 changes: 9 additions & 3 deletions Tests/HAConnectionImpl.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1464,11 +1464,17 @@ internal class HAConnectionImplTests: XCTestCase {

func testWriteDataWritesData() {
let expectedData = "Fake data".data(using: .utf8)!
let request = HARequest(
type: .sttData(1),
data: [
"audioData": expectedData.base64EncodedString()
]
)
connection.connect()
connection.write(.init(type: .data(expectedData)))
connection.write(request)
XCTAssertNotNil(requestController.added.first(where: { invocation in
if case let .data(data) = invocation.request.type {
return data == expectedData
if case let .sttData(sttBinaryHandlerId) = invocation.request.type {
return sttBinaryHandlerId == 1 && invocation.request.data["audioData"] as! String == expectedData.base64EncodedString()

Check failure on line 1477 in Tests/HAConnectionImpl.test.swift

View workflow job for this annotation

GitHub Actions / lint

Force casts should be avoided (force_cast)
}
return false
}))
Expand Down

0 comments on commit f901be3

Please sign in to comment.