Last active
April 19, 2023 13:26
-
-
Save ejohnso49/02ecbd3b629315ac63bb8b00f207465a to your computer and use it in GitHub Desktop.
Continuously export Memfault chunks from serial port
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import subprocess | |
import click | |
import serial | |
def run_cli_post_chunks(output, project_key, device_serial): | |
args = [ | |
"memfault", | |
"--project-key", | |
project_key, | |
"post-chunk", | |
"--device-serial", | |
device_serial, | |
"--encoding", | |
"sdk_data_export", | |
str(output), | |
] | |
subprocess.run(args=args) | |
@click.command() | |
@click.argument("device") | |
@click.argument("output", type=click.Path(exists=False, path_type=pathlib.Path)) | |
@click.option("--project-key", help="Project key to export data to") | |
@click.option("--device-serial", help="Device serial") | |
@click.option("--baudrate", default=115200, help="Baudrate of serial port") | |
@click.option("--timeout", default=60, help="Read timeout before sending batch of chunks") | |
def read_chunks(device, output, project_key, device_serial, baudrate, timeout): | |
if output.exists(): | |
click.echo(f"Overwriting file {output}") | |
port = serial.Serial(device, baudrate=baudrate, timeout=timeout) | |
while True: | |
with open(output, "wb") as output_file: | |
lines = port.readlines() | |
output_file.writelines([line + b"\n" for line in lines]) | |
output_file.flush() | |
run_cli_post_chunks(output, project_key, device_serial) | |
if __name__ == "__main__": | |
read_chunks(auto_envvar_prefix="MEMFAULT") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment