Initial Commit

This commit is contained in:
2021-10-16 15:01:47 -04:00
commit 42c46d9d84
18 changed files with 553 additions and 0 deletions

View 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
}
}

View 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
)
}
}