Skip to content

Commit

Permalink
Don't wake the pump unnecessarily - check if it's already responsive …
Browse files Browse the repository at this point in the history
…up-front
  • Loading branch information
oskarpearson committed Dec 13, 2016
1 parent 34192c9 commit bcf598a
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions mmeowlink/handlers/stick.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,11 @@ def __call__ (self, command, repetitions=None, ack_wait_seconds=None):
# We don't want to miss the reply, so take off a bit:
time.sleep((repetitions * 0.016) - 2.2)

# Sometimes the first packet received will be mangled by the simultaneous
# transmission of a CGMS and the pump. We thus retry on invalid packets
# being received. Note how ever that we do *not* retry on timeouts, since
# our wait period is typically very long here, which would lead to long
# waits with no activity. It's better to fail and retry externally
while (time.time() <= start + ack_wait_seconds):
try:
self.wait_for_ack()
return True
except CommsException, InvalidPacketReceived:
log.error("Response not received - retrying at %s" % time.time)
try:
self.wait_for_ack()
return True
except CommsException, InvalidPacketReceived:
log.error("%s - Response not received - retrying" % time.time())

return False

Expand All @@ -203,27 +197,46 @@ def __init__ (self, link, serial):
self.link = link
self.serial = serial

# Sends a few attempts at getting the pump model. If the pump responds to these,
# then it's awake
def check_pump_awake (self):
try:
self.model = self.read_model()
except (CommsException, InvalidPacketReceived):
pass

if len(self.model.getData( )) == 3:
return True
else:
return False

def power_control (self, minutes=None):
""" Control Pumping """
log.info('BEGIN POWER CONTROL %s' % self.serial)
self.command = commands.PowerControl(**dict(minutes=minutes, serial=self.serial))
repeater = Repeater(self.link)

status = repeater(self.command, repetitions=500, ack_wait_seconds=20)

return True
if status:
# See if the pump is on and responding to messages. If it isn't, then
# we need to send a wakeup to the pump
if self.check_pump_awake():
log.info('Pump %s is already responding. Not sneding wakeup messages' % self.serial)
return True
else:
raise CommsException("No acknowledgement from pump on wakeup. Is it out of range or is the battery too low?")
log.info('BEGIN POWER CONTROL %s' % self.serial)
self.command = commands.PowerControl(**dict(minutes=minutes, serial=self.serial))
repeater = Repeater(self.link)

status = repeater(self.command, repetitions=500, ack_wait_seconds=20)

if status:
return True
else:
raise CommsException("No acknowledgement from pump on wakeup. Is it out of range or is the battery too low?")

def execute (self, command):
command.serial = self.serial

for retry_count in range(self.STANDARD_RETRY_COUNT):
try:
sender = Sender(self.link)
return sender(command)
sender = Sender(self.link)
return sender(command)
except (CommsException, AssertionError) as e:
log.error("Timed out or other comms exception - %s - retrying: %s of %s" % (e, retry_count, self.STANDARD_RETRY_COUNT))
time.sleep(self.RETRY_BACKOFF * retry_count)
log.error("Timed out or other comms exception - %s - retrying: %s of %s" % (e, retry_count, self.STANDARD_RETRY_COUNT))
time.sleep(self.RETRY_BACKOFF * retry_count)

0 comments on commit bcf598a

Please sign in to comment.