From b47fb150e68631ad1efccbcf07b20a2718eb54fa Mon Sep 17 00:00:00 2001 From: gluap Date: Tue, 9 Jan 2024 20:49:38 +0100 Subject: [PATCH] periodic update requests --- README.rst | 4 ++++ pyduofern/__init__.py | 2 +- pyduofern/duofern.py | 5 +++-- pyduofern/duofern_stick.py | 24 ++++++++++++++++++------ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 56f0e77..98ef654 100644 --- a/README.rst +++ b/README.rst @@ -172,6 +172,10 @@ Changelog ========= **0.36** +- add periodic requests for status updates. + +**0.36** + - add rudimentary tracking of successfully sent messages and resending of unacknowledged ones. **0.35.1** diff --git a/pyduofern/__init__.py b/pyduofern/__init__.py index c7ff4c5..1ad6ff6 100644 --- a/pyduofern/__init__.py +++ b/pyduofern/__init__.py @@ -21,7 +21,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -__version__ = "0.36.1" +__version__ = "0.36.2" __all__ = ['DuofernException', 'DuofernStick', 'DuofernStickAsync', 'duoACK'] diff --git a/pyduofern/duofern.py b/pyduofern/duofern.py index c4600ee..42d29e0 100644 --- a/pyduofern/duofern.py +++ b/pyduofern/duofern.py @@ -35,7 +35,7 @@ # ^([^\n]+=) \(([^?\n]+)\?([^:\n]+):([^\)\n]+)\)?#? # $1 $3 if $2 else $4 -logger = logging.getLogger(__file__) +logger = logging.getLogger(__name__) duoStatusRequest = "0DFFnn400000000000000000000000000000yyyyyy01" duoCommand = "0Dkknnnnnnnnnnnnnnnnnnnn000000zzzzzzyyyyyy00" @@ -556,7 +556,8 @@ def parse(self, msg): id = msg[4:4 + 4] if id not in sensorMsg: - logger.info("unknown message {}".format(msg)) + logger.warning("unknown message {}".format(msg)) + return chan = msg[sensorMsg[id]['chan'] * 2 + 2:sensorMsg[id]['chan'] * 2 + 4] if code[0:2] in ("61", "70", "71"): diff --git a/pyduofern/duofern_stick.py b/pyduofern/duofern_stick.py index f35a86f..7c3249d 100644 --- a/pyduofern/duofern_stick.py +++ b/pyduofern/duofern_stick.py @@ -48,7 +48,7 @@ def hex(stuff): return codecs.getencoder('hex')(stuff)[0].decode("utf-8") -logger = logging.getLogger(__file__) +logger = logging.getLogger(__name__) duoInit1 = "01000000000000000000000000000000000000000000" duoInit2 = "0E000000000000000000000000000000000000000000" @@ -65,7 +65,7 @@ def hex(stuff): duoRemotePair = "0D0006010000000000000000000000000000yyyyyy01" -MIN_MESSAGE_INTERVAL_MILLIS = 25 +MIN_MESSAGE_INTERVAL_MILLIS = 50 RESEND_SECONDS = (2,4) def refresh_serial_connection(function): @@ -104,6 +104,8 @@ def __init__(self, system_code=None, config_file_json=None, duofern_parser=None, self.pairing = False self.unpairing = False + self.updating_interval = 30 + self.system_code = None if system_code is not None: if not ephemeral and 'system_code' in self.config: @@ -270,6 +272,10 @@ def pair(self, timeout=10): threading.Timer(timeout, self.stop_pair).start() self.pairing = True + def status_request(self): + self.send(duoStatusRequest.lower()) + + def unpair(self, timeout=10): self.send(duoStartUnpair) threading.Timer(10, self.stop_unpair).start() @@ -585,7 +591,7 @@ def _simple_write(self, string_to_write): # SimpleWrite def handle_write_queue(self): try: tosend = self.write_queue.get(block=False, timeout=None) - logger.info("sending {} from write queue, {} msgs left in queue".format(tosend, self.write_queue.qsize())) + logger.debug("sending {} from write queue, {} msgs left in queue".format(tosend, self.write_queue.qsize())) self._simple_write(tosend) self.unacknowledged[tosend[-14:-2]] = WaitingMessage(tosend, datetime.datetime.now()+datetime.timedelta(seconds=random.uniform(*RESEND_SECONDS))) except Empty: @@ -603,6 +609,7 @@ def command(self, *args, **kwargs): if self.recording: with open(self.record_filename, "a") as recorder: recorder.write("sending_command {} {}\n".format(args,kwargs)) + self.duofern_parser.set(*args, **kwargs) def add_serial_and_send(self, msg): @@ -614,6 +621,7 @@ def run(self): self.running = True self._initialize() last_resend_check = datetime.datetime.now() + last_periodic_update = datetime.datetime.now() toggle = False while self.running: toggle = not toggle @@ -635,10 +643,14 @@ def run(self): self.serial_connection.timeout = 1 if not self.write_queue.empty() or not self.rewrite_queue.empty() and ( (datetime.datetime.now() - self.last_send) >= datetime.timedelta(milliseconds=MIN_MESSAGE_INTERVAL_MILLIS)): - if toggle or self.rewrite_queue: - self.handle_write_queue() - else: + if toggle and self.rewrite_queue: self.handle_rewrite_queue() + else: + self.handle_write_queue() + + if self.updating_interval and datetime.datetime.now() - last_periodic_update > datetime.timedelta(seconds=self.updating_interval): + last_periodic_update = datetime.datetime.now() + self.status_request() if datetime.datetime.now() - last_resend_check > datetime.timedelta(seconds=0.1): self.handle_resends()