Initial Commit
This commit is contained in:
20
Sources/RelayClient/Interface.swift
Normal file
20
Sources/RelayClient/Interface.swift
Normal file
@@ -0,0 +1,20 @@
|
||||
import Foundation
|
||||
import Models
|
||||
import NIO
|
||||
|
||||
public struct RelayClient {
|
||||
public var toggle: (Relay) -> EventLoopFuture<Void>
|
||||
public var turnOn: (Relay) -> EventLoopFuture<Void>
|
||||
public var turnOff: (Relay) -> EventLoopFuture<Void>
|
||||
|
||||
public init(
|
||||
toggle: @escaping (Relay) -> EventLoopFuture<Void>,
|
||||
turnOn: @escaping (Relay) -> EventLoopFuture<Void>,
|
||||
turnOff: @escaping (Relay) -> EventLoopFuture<Void>
|
||||
) {
|
||||
self.toggle = toggle
|
||||
self.turnOn = turnOn
|
||||
self.turnOff = turnOff
|
||||
}
|
||||
}
|
||||
|
||||
37
Sources/RelayClient/Live.swift
Normal file
37
Sources/RelayClient/Live.swift
Normal file
@@ -0,0 +1,37 @@
|
||||
import Models
|
||||
import MQTTNIO
|
||||
import NIO
|
||||
|
||||
extension RelayClient {
|
||||
|
||||
public static func live(client: MQTTClient) -> RelayClient {
|
||||
.init(
|
||||
toggle: { relay in
|
||||
client.publish(relay: relay, state: .toggle)
|
||||
},
|
||||
turnOn: { relay in
|
||||
client.publish(relay: relay, state: .on)
|
||||
},
|
||||
turnOff: { relay in
|
||||
client.publish(relay: relay, state: .off)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension Relay {
|
||||
enum State: String {
|
||||
case toggle, on, off
|
||||
}
|
||||
}
|
||||
|
||||
extension MQTTClient {
|
||||
|
||||
func publish(relay: Relay, state: Relay.State, qos: MQTTQoS = .atLeastOnce) -> EventLoopFuture<Void> {
|
||||
publish(
|
||||
to: relay.topic,
|
||||
payload: ByteBufferAllocator().buffer(string: state.rawValue),
|
||||
qos: qos
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user