Skip to content

Commit

Permalink
Send less mqtt messages
Browse files Browse the repository at this point in the history
  • Loading branch information
klassm committed May 27, 2022
1 parent 8dff5d7 commit 6a14169
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions walkingpad-client/services/MqttService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ class MqttService {
}
}

public func publish(state: DeviceState) {
public func publish(oldState: DeviceState?, newState: DeviceState) {
if (oldState?.speed != newState.speed) {
return
}
guard let connection = self.connection else { return }
let config = connection.config
connection.mqtt.publish(CocoaMQTTMessage(topic: "\(config.topic)/speed", string: "\(state.speedKmh())"))
connection.mqtt.publish(CocoaMQTTMessage(topic: "\(config.topic)/steps", string: "\(state.steps)"))
connection.mqtt.publish(CocoaMQTTMessage(topic: "\(config.topic)/speed", string: "\(newState.speedKmh())"))
connection.mqtt.publish(CocoaMQTTMessage(topic: "\(config.topic)/steps", string: "\(newState.steps)"))
}

private func loadConfigFile() -> Data? {
Expand Down
2 changes: 1 addition & 1 deletion walkingpad-client/walkingpad_clientApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
self.walkingPadService.callback = { oldState, newState in
self.workout.update(oldState, newState)
self.mqttService.publish(state: newState)
self.mqttService.publish(oldState: oldState, newState: newState)
}

self.mqttService.start()
Expand Down

0 comments on commit 6a14169

Please sign in to comment.