Skip to content

Commit 62be055

Browse files
committed
Implement a RebootJob
Closes #21
1 parent a84922b commit 62be055

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

mqterm/jobs.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import logging
23
from binascii import hexlify
34
from hashlib import sha256
@@ -247,6 +248,38 @@ def output(self):
247248
return BytesIO(str(self.bytes_written).encode("utf-8"))
248249

249250

251+
class RebootJob(Job):
252+
"""A job to perform a hard or soft reboot of the device."""
253+
254+
argc = 1
255+
256+
def __init__(self, *args, **kwargs):
257+
super().__init__(*args, **kwargs)
258+
self.mode = self.args[0]
259+
260+
def output(self):
261+
"""Reboot the device after a given delay."""
262+
import machine
263+
264+
# Schedule reboot in one second
265+
async def reboot_callback():
266+
await asyncio.sleep(1)
267+
try:
268+
machine.reset() if self.mode == "hard" else machine.soft_reset()
269+
except AttributeError:
270+
logging.error("Operation not supported on this platform")
271+
272+
asyncio.create_task(reboot_callback())
273+
274+
# Log the reboot action and return as output
275+
msg = f"Performing {self.mode} reboot"
276+
if self.mode == "hard":
277+
logging.critical(msg)
278+
else:
279+
logging.warning(msg)
280+
return BytesIO(msg.encode("utf-8"))
281+
282+
250283
# Map commands to associated job names
251284
COMMANDS = {
252285
"whoami": WhoAmIJob,
@@ -255,4 +288,5 @@ def output(self):
255288
"ls": ListDirJob,
256289
"cp": PutFileJob,
257290
"ota": FirmwareUpdateJob,
291+
"reboot": RebootJob,
258292
}

tests/test_jobs.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Job,
1313
PlatformInfoJob,
1414
PutFileJob,
15+
RebootJob,
1516
WhoAmIJob,
1617
)
1718

@@ -169,7 +170,7 @@ def test_wait_partial_block(self):
169170
def test_last_block_fill(self):
170171
"""Should fill space in last block with empty data on final update"""
171172
initial_data = b"\xde\xad\xbe\xef"
172-
checksum = hexlify(sha256(initial_data).digest()).decode('utf-8')
173+
checksum = hexlify(sha256(initial_data).digest()).decode("utf-8")
173174
job = FirmwareUpdateJob("ota", [checksum])
174175

175176
# Send and complete the update
@@ -195,7 +196,7 @@ def test_last_block_fill(self):
195196
def test_output(self):
196197
"""Should return the total bytes written as output"""
197198
initial_data = b"\xde\xad\xbe\xef"
198-
checksum = hexlify(sha256(initial_data).digest()).decode('utf-8')
199+
checksum = hexlify(sha256(initial_data).digest()).decode("utf-8")
199200
job = FirmwareUpdateJob("ota", [checksum])
200201

201202
# Send and complete the update
@@ -205,3 +206,17 @@ def test_output(self):
205206
# Output should be the total bytes written
206207
output = job.output().read().decode("utf-8").strip()
207208
self.assertEqual(output, "4")
209+
210+
211+
class TestRebootJob(TestCase):
212+
def test_run_hard(self):
213+
"""Reboot job should signal and perform a hard reboot"""
214+
job = RebootJob("reboot", ["hard"])
215+
output = job.output().read().decode("utf-8").strip()
216+
self.assertEqual(output, "Performing hard reboot")
217+
218+
def test_run_soft(self):
219+
"""Reboot job should signal a soft reboot"""
220+
job = RebootJob("reboot", ["soft"])
221+
output = job.output().read().decode("utf-8").strip()
222+
self.assertEqual(output, "Performing soft reboot")

0 commit comments

Comments
 (0)