Skip to content

Commit

Permalink
pass command to access checking APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Dec 16, 2023
1 parent 620d732 commit bf2bdaf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,55 @@ public class OcaSubscriptionManager: OcaManager {

var objectsChangedWhilstNotificationsDisabled = Set<OcaONo>()

func addSubscription(
private func addSubscription(
_ subscription: SwiftOCA.OcaSubscriptionManager.AddSubscriptionParameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await controller.addSubscription(.subscription(subscription))
}

func removeSubscription(
private func removeSubscription(
_ subscription: SwiftOCA.OcaSubscriptionManager.RemoveSubscriptionParameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)

try await ensureWritable(by: controller, command: command)
try await controller.removeSubscription(
subscription.event,
property: nil,
subscriber: subscription.subscriber
)
}

func addPropertyChangeSubscription(
private func addPropertyChangeSubscription(
_ subscription: SwiftOCA.OcaSubscriptionManager.AddPropertyChangeSubscriptionParameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await controller.addSubscription(.propertyChangeSubscription(subscription))
}

func removePropertyChangeSubscription(
private func removePropertyChangeSubscription(
_ subscription: SwiftOCA.OcaSubscriptionManager.RemovePropertyChangeSubscriptionParameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await controller.removeSubscription(
OcaEvent(emitterONo: subscription.emitter, eventID: OcaPropertyChangedEventID),
property: subscription.property,
subscriber: subscription.subscriber
)
}

func disableNotifications(from controller: any AES70Controller) async throws {
try await ensureWritable(by: controller)
private func disableNotifications(
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller, command: command)
state = .eventsDisabled
let event = OcaEvent(
emitterONo: objectNumber,
Expand All @@ -76,8 +82,11 @@ public class OcaSubscriptionManager: OcaManager {
try await deviceDelegate?.notifySubscribers(event)
}

func reenableNotifications(from controller: any AES70Controller) async throws {
try await ensureWritable(by: controller)
private func reenableNotifications(
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller, command: command)
let event = OcaEvent(
emitterONo: objectNumber,
eventID: SwiftOCA.OcaSubscriptionManager.SynchronizeStateEventID
Expand All @@ -89,35 +98,39 @@ public class OcaSubscriptionManager: OcaManager {
state = .normal
}

func addSubscription2(
private func addSubscription2(
_ subscription: SwiftOCA.OcaSubscriptionManager.AddSubscription2Parameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await controller.addSubscription(.subscription2(subscription))
}

func removeSubscription2(
private func removeSubscription2(
_ subscription: SwiftOCA.OcaSubscriptionManager.RemoveSubscription2Parameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await controller.removeSubscription(.subscription2(subscription))
}

func addPropertyChangeSubscription2(
private func addPropertyChangeSubscription2(
_ subscription: SwiftOCA.OcaSubscriptionManager.AddPropertyChangeSubscription2Parameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await controller.addSubscription(.propertyChangeSubscription2(subscription))
}

func removePropertyChangeSubscription2(
private func removePropertyChangeSubscription2(
_ subscription: SwiftOCA.OcaSubscriptionManager.RemovePropertyChangeSubscription2Parameters,
from controller: any AES70Controller
from controller: any AES70Controller,
command: Ocp1Command
) async throws {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await controller.removeSubscription(.propertyChangeSubscription2(subscription))
}

Expand All @@ -129,28 +142,36 @@ public class OcaSubscriptionManager: OcaManager {
case OcaMethodID("3.1"):
let subscription: SwiftOCA.OcaSubscriptionManager
.AddSubscriptionParameters = try decodeCommand(command)
try await addSubscription(subscription, from: controller)
try await addSubscription(subscription, from: controller, command: command)
return Ocp1Response()
case OcaMethodID("3.2"):
let subscription: SwiftOCA.OcaSubscriptionManager
.RemoveSubscriptionParameters = try decodeCommand(command)
try await removeSubscription(subscription, from: controller)
try await removeSubscription(subscription, from: controller, command: command)
return Ocp1Response()
case OcaMethodID("3.3"):
try await disableNotifications(from: controller)
try await disableNotifications(from: controller, command: command)
return Ocp1Response()
case OcaMethodID("3.4"):
try await reenableNotifications(from: controller)
try await reenableNotifications(from: controller, command: command)
return Ocp1Response()
case OcaMethodID("3.5"):
let subscription: SwiftOCA.OcaSubscriptionManager
.AddPropertyChangeSubscriptionParameters = try decodeCommand(command)
try await addPropertyChangeSubscription(subscription, from: controller)
try await addPropertyChangeSubscription(
subscription,
from: controller,
command: command
)
return Ocp1Response()
case OcaMethodID("3.6"):
let subscription: SwiftOCA.OcaSubscriptionManager
.RemovePropertyChangeSubscriptionParameters = try decodeCommand(command)
try await removePropertyChangeSubscription(subscription, from: controller)
try await removePropertyChangeSubscription(
subscription,
from: controller,
command: command
)
return Ocp1Response()
case OcaMethodID("3.7"):
// Returns maximum byte length of payload of EV1 subscriber context parameter that this
Expand All @@ -160,22 +181,30 @@ public class OcaSubscriptionManager: OcaManager {
case OcaMethodID("3.8"):
let subscription: SwiftOCA.OcaSubscriptionManager
.AddSubscription2Parameters = try decodeCommand(command)
try await addSubscription2(subscription, from: controller)
try await addSubscription2(subscription, from: controller, command: command)
return Ocp1Response()
case OcaMethodID("3.9"):
let subscription: SwiftOCA.OcaSubscriptionManager
.RemoveSubscription2Parameters = try decodeCommand(command)
try await removeSubscription2(subscription, from: controller)
try await removeSubscription2(subscription, from: controller, command: command)
return Ocp1Response()
case OcaMethodID("3.10"):
let subscription: SwiftOCA.OcaSubscriptionManager
.AddPropertyChangeSubscription2Parameters = try decodeCommand(command)
try await addPropertyChangeSubscription2(subscription, from: controller)
try await addPropertyChangeSubscription2(
subscription,
from: controller,
command: command
)
return Ocp1Response()
case OcaMethodID("3.11"):
let subscription: SwiftOCA.OcaSubscriptionManager
.RemovePropertyChangeSubscription2Parameters = try decodeCommand(command)
try await removePropertyChangeSubscription2(subscription, from: controller)
try await removePropertyChangeSubscription2(
subscription,
from: controller,
command: command
)
return Ocp1Response()
default:
return try await super.handleCommand(command, from: controller)
Expand Down
18 changes: 12 additions & 6 deletions Sources/SwiftOCADevice/OCC/ControlClasses/Root.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ Sendable {
let property = self[keyPath: propertyKeyPath] as! (any OcaDevicePropertyRepresentable)

if command.methodID == property.getMethodID {
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
return try await property.get(object: self)
} else if command.methodID == property.setMethodID {
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
try await property.set(object: self, command: command)
return Ocp1Response()
}
Expand Down Expand Up @@ -154,9 +154,12 @@ Sendable {
false
}

open func ensureReadable(by controller: any AES70Controller) async throws {
open func ensureReadable(
by controller: any AES70Controller,
command: Ocp1Command
) async throws {
if let deviceManager = await deviceDelegate?.deviceManager, deviceManager != self {
try await deviceManager.ensureReadable(by: controller)
try await deviceManager.ensureReadable(by: controller, command: command)
}

switch lockState {
Expand All @@ -171,9 +174,12 @@ Sendable {
}
}

open func ensureWritable(by controller: any AES70Controller) async throws {
open func ensureWritable(
by controller: any AES70Controller,
command: Ocp1Command
) async throws {
if let deviceManager = await deviceDelegate?.deviceManager, deviceManager != self {
try await deviceManager.ensureWritable(by: controller)
try await deviceManager.ensureWritable(by: controller, command: command)
}

switch lockState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,28 +265,28 @@ open class OcaBlock<ActionObject: OcaRoot>: OcaWorker {
// 3.3 ConstructBlockUsingFactory
// 3.4 DeleteMember
case OcaMethodID("3.5"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
let actionObjects = actionObjects.map(\.objectIdentification)
return try encodeResponse(actionObjects)
case OcaMethodID("3.6"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
let actionObjects: [OcaBlockMember] =
try await getActionObjectsRecursive(from: controller)
return try encodeResponse(actionObjects)
case OcaMethodID("3.7"):
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
let path: OcaSignalPath = try decodeCommand(command)
let index = try await add(signalPath: path)
return try encodeResponse(index)
case OcaMethodID("3.8"):
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
let index: OcaUint16 = try decodeCommand(command)
try await remove(signalPathAt: index)
case OcaMethodID("3.9"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
return try encodeResponse(signalPaths)
case OcaMethodID("3.10"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
let signalPaths: [OcaUint16: OcaSignalPath] =
try await getSignalPathsRecursive(from: controller)
return try encodeResponse(signalPaths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ open class OcaMatrix<Member: OcaRoot>: OcaWorker {
) async throws -> Ocp1Response {
switch command.methodID {
case OcaMethodID("3.3"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
let size = OcaVector2D<OcaMatrixCoordinate>(
x: OcaMatrixCoordinate(members.nX),
y: OcaMatrixCoordinate(members.nY)
Expand All @@ -277,18 +277,18 @@ open class OcaMatrix<Member: OcaRoot>: OcaWorker {
)
return try encodeResponse(matrixSize)
case OcaMethodID("3.5"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
let members = members
.map(defaultValue: OcaInvalidONo) { $0?.objectNumber ?? OcaInvalidONo }
return try encodeResponse(OcaMatrixGetMembersParameters(members: members))
case OcaMethodID("3.7"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
let coordinates: OcaVector2D<OcaMatrixCoordinate> = try decodeCommand(command)
let objectNumber = members[Int(coordinates.x), Int(coordinates.y)]?
.objectNumber ?? OcaInvalidONo
return try encodeResponse(objectNumber)
case OcaMethodID("3.8"):
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
let parameters: OcaMatrixSetMemberParameters = try decodeCommand(command)
guard parameters.x < members.nX, parameters.y < members.nY else {
throw Ocp1Error.status(.parameterOutOfRange)
Expand All @@ -304,10 +304,10 @@ open class OcaMatrix<Member: OcaRoot>: OcaWorker {
}
members[Int(parameters.x), Int(parameters.y)] = object
case OcaMethodID("3.9"):
try await ensureReadable(by: controller)
try await ensureReadable(by: controller, command: command)
return try encodeResponse(proxy.objectNumber)
case OcaMethodID("3.2"):
try await ensureWritable(by: controller)
try await ensureWritable(by: controller, command: command)
let coordinates: OcaVector2D<OcaMatrixCoordinate> = try decodeCommand(command)
guard coordinates.x < members.nX || coordinates.x == OcaMatrixWildcardCoordinate,
coordinates.y < members.nY || coordinates.y == OcaMatrixWildcardCoordinate
Expand Down

0 comments on commit bf2bdaf

Please sign in to comment.