node-zwave-js icon indicating copy to clipboard operation
node-zwave-js copied to clipboard

Keep track of rate of incoming messages per second

Open AlCalzone opened this issue 6 months ago • 0 comments

This could be done using an exponential moving average, similar to what we already do for the background RSSI. Quick mockup in Python - thanks Copilot :D

import time

class MessageRateTracker:
    def __init__(self, averaging_period=60):
        self.averaging_period = averaging_period  # Zeitraum in Sekunden
        self.rate = 0  # Aktuelle Nachrichtenrate
        self.last_update_time = time.time()  # Letzter Aktualisierungszeitpunkt

    def update(self, message_count=1):
        current_time = time.time()
        elapsed_time = current_time - self.last_update_time

        if elapsed_time > 0:
            # Berechne den Gewichtungsfaktor für den gleitenden Durchschnitt
            alpha = 1 - (1 / self.averaging_period)
            decay = alpha ** elapsed_time

            # Aktualisiere die Rate mit dem exponentiellen gleitenden Durchschnitt
            self.rate = self.rate * decay + message_count * (1 - decay)

            # Aktualisiere den letzten Aktualisierungszeitpunkt
            self.last_update_time = current_time

    def get_rate(self):
        return self.rate

AlCalzone avatar May 19 '25 12:05 AlCalzone