videv loopback prevention ignore rx after tx
This commit is contained in:
parent
8aabd272f9
commit
f70ba22ce2
@ -13,6 +13,7 @@ Targets:
|
||||
from base import mqtt_base
|
||||
import devices
|
||||
import json
|
||||
import time
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
@ -28,6 +29,7 @@ class base(mqtt_base):
|
||||
self.__display_dict__ = {}
|
||||
self.__control_dict__ = {}
|
||||
self.__capabilities__ = None
|
||||
self.__active_tx__ = {}
|
||||
|
||||
def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
|
||||
"""
|
||||
@ -49,6 +51,8 @@ class base(mqtt_base):
|
||||
self.__tx__(self.__display_dict__[(id(ext_device), ext_key)], data)
|
||||
|
||||
def __tx__(self, key, data):
|
||||
if key in self.__control_dict__:
|
||||
self.__active_tx__[key] = (time.time(), data)
|
||||
if type(data) not in (str, ):
|
||||
data = json.dumps(data)
|
||||
self.mqtt_client.send(self.topic + '/' + key, data)
|
||||
@ -69,17 +73,23 @@ class base(mqtt_base):
|
||||
|
||||
def __rx_videv_data__(self, client, userdata, message):
|
||||
my_key = message.topic.split('/')[-1]
|
||||
ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
|
||||
if my_key in self.keys():
|
||||
try:
|
||||
data = json.loads(message.payload)
|
||||
except json.decoder.JSONDecodeError:
|
||||
data = message.payload
|
||||
if data != self[my_key] or not on_change_only:
|
||||
ext_device.set(ext_key, data)
|
||||
self.set(my_key, data)
|
||||
try:
|
||||
data = json.loads(message.payload)
|
||||
except json.decoder.JSONDecodeError:
|
||||
data = message.payload
|
||||
if my_key in self.__active_tx__:
|
||||
tm, tx_data = self.__active_tx__.pop(my_key)
|
||||
do_ex = data != tx_data and time.time() - tm < 2
|
||||
else:
|
||||
self.logger.info("Ignoring rx message with topic %s", message.topic)
|
||||
do_ex = True
|
||||
if do_ex:
|
||||
ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
|
||||
if my_key in self.keys():
|
||||
if data != self[my_key] or not on_change_only:
|
||||
ext_device.set(ext_key, data)
|
||||
self.set(my_key, data)
|
||||
else:
|
||||
self.logger.info("Ignoring rx message with topic %s", message.topic)
|
||||
|
||||
def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user