Skip to content

Commit

Permalink
periodic update requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gluap committed Jan 9, 2024
1 parent 62bdaf1 commit b47fb15
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
2 changes: 1 addition & 1 deletion pyduofern/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down
5 changes: 3 additions & 2 deletions pyduofern/duofern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"):
Expand Down
24 changes: 18 additions & 6 deletions pyduofern/duofern_stick.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit b47fb15

Please sign in to comment.