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
|
from base import mqtt_base
|
||||||
import devices
|
import devices
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||||
@ -28,6 +29,7 @@ class base(mqtt_base):
|
|||||||
self.__display_dict__ = {}
|
self.__display_dict__ = {}
|
||||||
self.__control_dict__ = {}
|
self.__control_dict__ = {}
|
||||||
self.__capabilities__ = None
|
self.__capabilities__ = None
|
||||||
|
self.__active_tx__ = {}
|
||||||
|
|
||||||
def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
|
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)
|
self.__tx__(self.__display_dict__[(id(ext_device), ext_key)], data)
|
||||||
|
|
||||||
def __tx__(self, 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, ):
|
if type(data) not in (str, ):
|
||||||
data = json.dumps(data)
|
data = json.dumps(data)
|
||||||
self.mqtt_client.send(self.topic + '/' + key, 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):
|
def __rx_videv_data__(self, client, userdata, message):
|
||||||
my_key = message.topic.split('/')[-1]
|
my_key = message.topic.split('/')[-1]
|
||||||
ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
|
try:
|
||||||
if my_key in self.keys():
|
data = json.loads(message.payload)
|
||||||
try:
|
except json.decoder.JSONDecodeError:
|
||||||
data = json.loads(message.payload)
|
data = message.payload
|
||||||
except json.decoder.JSONDecodeError:
|
if my_key in self.__active_tx__:
|
||||||
data = message.payload
|
tm, tx_data = self.__active_tx__.pop(my_key)
|
||||||
if data != self[my_key] or not on_change_only:
|
do_ex = data != tx_data and time.time() - tm < 2
|
||||||
ext_device.set(ext_key, data)
|
|
||||||
self.set(my_key, data)
|
|
||||||
else:
|
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):
|
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