From 6a141692133182b871fee015fd5e6ac4f896e3da Mon Sep 17 00:00:00 2001 From: Matthias Klass Date: Fri, 27 May 2022 07:43:06 +0200 Subject: [PATCH] Send less mqtt messages --- walkingpad-client/services/MqttService.swift | 9 ++++++--- walkingpad-client/walkingpad_clientApp.swift | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/walkingpad-client/services/MqttService.swift b/walkingpad-client/services/MqttService.swift index 7f2ebb2..15c3c7c 100644 --- a/walkingpad-client/services/MqttService.swift +++ b/walkingpad-client/services/MqttService.swift @@ -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? { diff --git a/walkingpad-client/walkingpad_clientApp.swift b/walkingpad-client/walkingpad_clientApp.swift index 6ebd209..17d0ca9 100644 --- a/walkingpad-client/walkingpad_clientApp.swift +++ b/walkingpad-client/walkingpad_clientApp.swift @@ -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()