-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
import Foundation
public struct TransportService {
public static let id = "com.roonlabs.transport:2"
public struct SingleLineDisplay: Decodable, Hashable {
public let line: String
public enum CodingKeys: String, CodingKey {
case line = "line1"
}
}
public struct DoubleLineDisplay: Decodable, Hashable {
public let line1: String
public let line2: String?
}
public struct TripleLineDisplay: Decodable, Hashable {
public let line1: String
public let line2: String?
public let line3: String?
}
public struct NowPlaying: Decodable, Hashable {
public let seekPosition: UInt64?
public let length: UInt64?
public let imageKey: String?
public let singleLine: SingleLineDisplay
public let doubleLine: DoubleLineDisplay
public let tripleLine: TripleLineDisplay
public enum CodingKeys: String, CodingKey {
case seekPosition = "seek_position"
case length = "length"
case imageKey = "image_key"
case singleLine = "one_line"
case doubleLine = "two_line"
case tripleLine = "three_line"
}
}
public enum PlaybackState: String, Decodable {
case playing
case paused
case loading
case stopped
}
public struct OutputVolume: Decodable, Hashable {
public let type: String
public let min: Float64?
public let max: Float64?
public let value: Float64?
public let step: Float64?
public let isMuted: Bool?
public enum CodingKeys: String, CodingKey {
case type = "type"
case min = "min"
case max = "max"
case value = "value"
case step = "step"
case isMuted = "is_muted"
}
}
public struct Output: Decodable, Identifiable, Hashable {
public let id: String
public let displayName: String
public let volume: OutputVolume?
public enum CodingKeys: String, CodingKey {
case id = "output_id"
case displayName = "display_name"
case volume = "volume"
}
}
public struct Zone: Decodable, Identifiable, Hashable {
public let id: String
public let displayName: String
public let outputs: [Output]
public let nowPlaying: NowPlaying?
public let state: PlaybackState
public let isPreviousAllowed: Bool?
public let isNextAllowed: Bool?
public let isPauseAllowed: Bool?
public let isPlayAllowed: Bool?
public let isSeekAllowed: Bool?
public enum CodingKeys: String, CodingKey {
case id = "zone_id"
case displayName = "display_name"
case outputs = "outputs"
case nowPlaying = "now_playing"
case state = "state"
case isPreviousAllowed = "is_previous_allowed"
case isNextAllowed = "is_next_allowed"
case isPauseAllowed = "is_pause_allowed"
case isPlayAllowed = "is_play_allowed"
case isSeekAllowed = "is_seek_allowed"
}
}
public struct SeekChangeEvent: Decodable {
public let zoneID: String
public let seekPosition: UInt64?
public enum CodingKeys: String, CodingKey {
case zoneID = "zone_id"
case seekPosition = "seek_position"
}
}
public struct ZoneChangeEvent: Decodable {
private let _removedZoneIDs: [String]?
private let _addedZones: [Zone]?
private let _changedZones: [Zone]?
private let _seekChanges: [SeekChangeEvent]?
public var removedZoneIDs: [String] {
_removedZoneIDs ?? []
}
public var addedZones: [Zone] {
_addedZones ?? []
}
public var changedZones: [Zone] {
_changedZones ?? []
}
public var seekChanges: [SeekChangeEvent] {
_seekChanges ?? []
}
public enum CodingKeys: String, CodingKey {
case _removedZoneIDs = "zones_removed"
case _addedZones = "zones_added"
case _changedZones = "zones_changed"
case _seekChanges = "zones_seek_changed"
}
public init?(_ msg: Moo) {
do {
self = try msg.json(type: Self.self)
} catch {
return nil
}
}
}
}
// MARK: - Subscribe changes to zone
extension TransportService {
public struct SubscribeZoneChangesRequest: Encodable {
public let subscriptionID: String
public init(subscriptionID: String) {
self.subscriptionID = subscriptionID
}
public init(subscriptionID: any BinaryInteger) {
self.subscriptionID = String(subscriptionID, radix: 10)
}
public enum CodingKeys: String, CodingKey {
case subscriptionID = "subscription_key"
}
}
public struct SubscribeZoneChangesResponse: Decodable {
public enum DecodeError: Error {
case unexpectedStatus(String)
case bodyError(Moo.JsonBodyError)
}
public let zones: [Zone]
public init(_ msg: Moo) throws(DecodeError) {
guard msg.service == "Subscribed" else {
throw .unexpectedStatus(msg.service)
}
do {
self = try msg.json(type: Self.self)
} catch {
throw .bodyError(error)
}
}
}
}
extension Moo {
public init(
subscribeZoneChange: TransportService.SubscribeZoneChangesRequest
) throws {
try self.init(
jsonBody: subscribeZoneChange,
service: "\(TransportService.id)/subscribe_zones"
)
}
}
// MARK: - Send playback control
extension TransportService {
public enum ControlAction: String, Codable {
case play, pause, playpause, stop, previous, next
}
public struct ControlRequest: Codable {
public let zoneOrOutputID: String
public let control: ControlAction
public enum CodingKeys: String, CodingKey {
case zoneOrOutputID = "zone_or_output_id"
case control = "control"
}
public init(zoneID: String, control: ControlAction) {
self.zoneOrOutputID = zoneID
self.control = control
}
}
public struct ControlResponse: Codable {
public enum DecodeError: Error {
case nonSuccess
}
public init(_ msg: Moo) throws(DecodeError) {
guard msg.service == "Success" else {
throw .nonSuccess
}
}
}
}
extension Moo {
public init(control: TransportService.ControlRequest) throws {
try self.init(jsonBody: control, service: "\(TransportService.id)/control")
}
}
// MARK: - Seek current playing song
extension TransportService {
public enum SeekMode: String, Codable {
case relative, absolute
}
public struct SeekRequest: Codable {
public let zoneOrOutputID: String
public let mode: SeekMode
public let seconds: Int64
public enum CodingKeys: String, CodingKey {
case zoneOrOutputID = "zone_or_output_id"
case mode = "how"
case seconds = "seconds"
}
public init(zoneID: String, seconds: Int64, mode: SeekMode = .absolute) {
self.zoneOrOutputID = zoneID
self.mode = mode
self.seconds = seconds
}
}
public struct SeekResponse: Codable {
public enum DecodeError: Error {
case nonSuccess
}
public init(_ msg: Moo) throws(DecodeError) {
guard msg.service == "Success" else {
throw .nonSuccess
}
}
}
}
extension Moo {
public init(seek: TransportService.SeekRequest) throws {
try self.init(jsonBody: seek, service: "\(TransportService.id)/seek")
}
}
// MARK: - Change output volume
extension TransportService {
public enum ChangeVolumeMode: String, Codable {
case absolute, relative
case relativeStep = "relative_step"
}
public struct ChangeVolumeRequest: Codable {
public let outputID: String
public let mode: ChangeVolumeMode
public let value: Float64
public enum CodingKeys: String, CodingKey {
case outputID = "output_id"
case mode = "how"
case value = "value"
}
public init(
outputID: String,
value: Float64,
mode: ChangeVolumeMode = .absolute
) {
self.outputID = outputID
self.value = value
self.mode = mode
}
}
public struct ChangeVolumeResponse: Codable {
public enum DecodeError: Error {
case nonSuccess
}
public init(_ msg: Moo) throws(DecodeError) {
guard msg.service == "Success" else {
throw .nonSuccess
}
}
}
}
extension Moo {
public init(changeVolume: TransportService.ChangeVolumeRequest) throws {
try self.init(
jsonBody: changeVolume,
service: "\(TransportService.id)/change_volume"
)
}
}