bendikwa / igrill

Hacking the iGrill (mini, V2, V3 & Pulse 2000)
MIT License
77 stars 38 forks source link

reconnect when disconnected #11

Closed brianjmurrell closed 3 years ago

brianjmurrell commented 5 years ago

Hi. Great to see a fork of this great work surviving. Although, if I'm honest, I wish the basic igrill functionality and your mqtt implementation on top of it were separated into separate repos, but it is what it is. I just honestly don't need the mqtt functionality. But I digress.

Do you not find that you can get disconnected from your iGrill periodically? I ended up doing this to deal with it:

class myIgrill(IGrillV2Peripheral):
    def __init__(self, *args, **kwargs):
        self.address = args[0]
        super(myIgrill, self).__init__(*args, **kwargs)

    def read_temperature(self, *args, **kwargs):
        try:
            return super(myIgrill, self).read_temperature(*args, **kwargs)
        except bluepy.btle.BTLEDisconnectError:
            while True:
                try:
                    print("Disconnected from iGrill, trying to reconnect...")
                    device = self.__init__(self.address)
                    return super(myIgrill, self).read_temperature(*args, **kwargs)
                except bluepy.btle.BTLEDisconnectError:
                    print("Failed to reconnect -- trying again...")
                    continue

As you can see, I've modified read_temperature to reconnect if trying to read the temps fails with a BTLEDisconnectError. Do you think this is is something the iGrill() class itself could benefit from?

bendikwa commented 4 years ago

Yes, I have had an issue with getting disconnected, and ending up in a bad state. Thank you for the tip, I’ll look into this the next time I have some time to do tesing.

-Bendik

bendikwa commented 4 years ago

As far as I can see, this is already done in igrill.py def run(self)? All calls to device.read_temperature() ( and device.read_battery() ) is wrapped a try/except Exception, and on the next cycle the device is reconnected with device = self.device_types[self.type](self.address, self.name)

Am I missing something?

PaulAntonDeen commented 4 years ago

Mine is reconnecting after loosing the connection.

bendikwa commented 3 years ago

I'm closing this due to inactivity