Delegate.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Delegate.swift
  3. // MAVLink
  4. //
  5. // Created by Max Odnovolyk on 10/28/16.
  6. // Copyright © 2016 Build Apps. All rights reserved.
  7. //
  8. import Foundation
  9. import MAVLink
  10. class Delegate {
  11. typealias DidReceiveHandler = (Packet, Channel, MAVLink) -> Void
  12. typealias DidFailToReceiveHandler = (Packet?, MAVLinkError, Channel, MAVLink) -> Void
  13. typealias DidParseHandler = (Message, Packet, Channel, MAVLink) -> Void
  14. typealias DidFailToParseMessageHandler = (Packet, MAVLinkError, Channel, MAVLink) -> Void
  15. typealias DidFinalizeHandler = (Message, Packet, Data, Channel, MAVLink) -> Void
  16. var didReceive: DidReceiveHandler?
  17. var didFailToReceive: DidFailToReceiveHandler?
  18. var didParse: DidParseHandler?
  19. var didFailToParseMessage: DidFailToParseMessageHandler?
  20. var didFinalize: DidFinalizeHandler?
  21. init(didReceive: DidReceiveHandler? = nil, didFailToReceive: DidFailToReceiveHandler? = nil, didParse: DidParseHandler? = nil, didFailToParseMessage: DidFailToParseMessageHandler? = nil, didFinalize: DidFinalizeHandler? = nil) {
  22. self.didReceive = didReceive
  23. self.didFailToReceive = didFailToReceive
  24. self.didParse = didParse
  25. self.didFailToParseMessage = didFailToParseMessage
  26. self.didFinalize = didFinalize
  27. }
  28. }
  29. extension Delegate: MAVLinkDelegate {
  30. func didReceive(packet: Packet, on channel: Channel, via link: MAVLink) {
  31. didReceive?(packet, channel, link)
  32. }
  33. func didFailToReceive(packet: Packet?, with error: MAVLinkError, on channel: Channel, via link: MAVLink) {
  34. didFailToReceive?(packet, error, channel, link)
  35. }
  36. func didParse(message: Message, from packet: Packet, on channel: Channel, via link: MAVLink) {
  37. didParse?(message, packet, channel, link)
  38. }
  39. func didFailToParseMessage(from packet: Packet, with error: MAVLinkError, on channel: Channel, via link: MAVLink) {
  40. didFailToParseMessage?(packet, error, channel, link)
  41. }
  42. func didFinalize(message: Message, from packet: Packet, to data: Data, on channel: Channel, in link: MAVLink) {
  43. didFinalize?(message, packet, data, channel, link)
  44. }
  45. }