Skip to content

Commit

Permalink
.machine.watchdog: Remove usage of RTC.memory, bugfix if timeout was …
Browse files Browse the repository at this point in the history
…changed by init(timeout)
  • Loading branch information
kevinkk525 committed Apr 3, 2020
1 parent b904f3f commit fdb5ab0
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions pysmartnode/components/machine/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
constructor_args: {
id: 0 #optional, number of the timer to be used
timeout: 120 #optional, defaults to 120s, resets machine after this
# use_rtc_memory: true #optional, use rtc memory as backup if filesystem not available, only esp8266 supported
}
}
"""

__updated__ = "2019-10-19"
__version__ = "1.2"
__updated__ = "2020-04-03"
__version__ = "1.3"

import gc
import uasyncio as asyncio
Expand All @@ -27,54 +26,41 @@


class WDT:
def __init__(self, id=0, timeout=120, use_rtc_memory=True):
def __init__(self, id=0, timeout=120):
self._timeout = timeout / 10
self._counter = 0
self._timer = machine.Timer(id)
self._use_rtc_memory = use_rtc_memory
self._has_filesystem = False
self.init()
asyncio.create_task(self._resetCounter())
""" Done in pysmartnode.main
if use_rtc_memory and platform == "esp8266":
rtc = machine.RTC()
if rtc.memory() == b"WDT reset":
logging.getLogger("WDT").critical("Reset reason: Watchdog")
rtc.memory(b"")
elif sys_vars.hasFilesystem():
self._has_filesystem = True
try:
with open("reset_reason.txt", "r") as f:
if f.read() == "True":
logging.getLogger("WDT").warn("Reset reason: Watchdog")
except Exception as e:
print(e) # file probably just does not exist
try:
os.remove("reset_reason.txt")
except Exception as e:
logging.getLogger("WDT").error("Error saving to file: {!s}".format(e))
try:
with open("reset_reason.txt", "r") as f:
if f.read() == "True":
logging.getLogger("WDT").warn("Reset reason: Watchdog")
except Exception as e:
print(e) # file probably just does not exist
try:
os.remove("reset_reason.txt")
except Exception as e:
logging.getLogger("WDT").error("Error saving to file: {!s}".format(e))
"""

def _wdt(self, t):
self._counter += self._timeout
if self._counter >= self._timeout * 10:
if self._use_rtc_memory and platform == "esp8266":
rtc = machine.RTC()
rtc.memory(b"WDT reset")
elif self._has_filesystem:
try:
with open("reset_reason.txt", "w") as f:
f.write("WDT reset")
except Exception as e:
print("Error saving to file: {!s}".format(e))
try:
with open("reset_reason.txt", "w") as f:
f.write("WDT reset")
except Exception as e:
print("Error saving to file: {!s}".format(e))
machine.reset()

def feed(self):
self._counter = 0

def init(self, timeout=None):
timeout = timeout or self._timeout
self._timeout = timeout
self._timeout = timeout / 10
self._timer.init(period=int(self._timeout * 1000), mode=machine.Timer.PERIODIC,
callback=self._wdt)

Expand Down

0 comments on commit fdb5ab0

Please sign in to comment.