Skip to content

Commit

Permalink
signal path helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Dec 16, 2023
1 parent efb605f commit 620d732
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ open class OcaBlock<ActionObject: OcaRoot>: OcaWorker {
)
public var signalPaths = OcaMap<OcaUint16, OcaSignalPath>()

public func add(signalPath path: OcaSignalPath) async throws -> OcaUint16 {
let index: OcaUint16 = signalPaths.keys.sorted().last ?? 1
signalPaths[index] = path
return index
}

public func remove(signalPathAt index: OcaUint16) async throws {
if !signalPaths.keys.contains(index) {
throw Ocp1Error.status(.parameterOutOfRange)
}
signalPaths.removeValue(forKey: index)
}

@OcaDeviceProperty(
propertyID: OcaPropertyID("3.4"),
getMethodID: OcaMethodID("3.11")
Expand Down Expand Up @@ -263,16 +276,12 @@ open class OcaBlock<ActionObject: OcaRoot>: OcaWorker {
case OcaMethodID("3.7"):
try await ensureWritable(by: controller)
let path: OcaSignalPath = try decodeCommand(command)
let index: OcaUint16 = signalPaths.keys.sorted().last ?? 1
signalPaths[index] = path
let index = try await add(signalPath: path)
return try encodeResponse(index)
case OcaMethodID("3.8"):
try await ensureWritable(by: controller)
let index: OcaUint16 = try decodeCommand(command)
if !signalPaths.keys.contains(index) {
throw Ocp1Error.status(.parameterOutOfRange)
}
signalPaths.removeValue(forKey: index)
try await remove(signalPathAt: index)
case OcaMethodID("3.9"):
try await ensureReadable(by: controller)
return try encodeResponse(signalPaths)
Expand Down

0 comments on commit 620d732

Please sign in to comment.