Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add script for provisioning Flex pipettes in emulation #12897

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Script to write pipette serial numbers to eeprom files.

This script is configured to run against the emulation container.
High level overview of the script:

- Read pipette definition from environment variables
- Generate pipette serial code
- Write pipette serial code to eeprom file
- Profit
"""

import os
import json
from dataclasses import dataclass
from typing import Optional
from opentrons_hardware.firmware_bindings.constants import PipetteName
from opentrons_hardware.instruments.pipettes.serials import serial_val_from_parts

LEFT_PIPETTE_ENV_VAR_NAME = "LEFT_OT3_PIPETTE_DEFINITION"
RIGHT_PIPETTE_ENV_VAR_NAME = "RIGHT_OT3_PIPETTE_DEFINITION"


@dataclass
class OT3PipetteEnvVar:
"""OT3 Pipette Environment Variable."""

pipette_name: PipetteName
pipette_model: int
pipette_serial_code: bytes
eeprom_file_path: str

@classmethod
def from_json_string(cls, env_var_string: str) -> "OT3PipetteEnvVar":
"""Create OT3PipetteEnvVar from json string."""
env_var = json.loads(env_var_string)
return cls(
pipette_name=PipetteName[env_var["pipette_name"]],
pipette_model=env_var["pipette_model"],
pipette_serial_code=env_var["pipette_serial_code"].encode("utf-8"),
eeprom_file_path=env_var["eeprom_file_path"],
)

def _generate_serial_code(self) -> bytes:
"""Generate serial code."""
return serial_val_from_parts(
self.pipette_name, self.pipette_model, self.pipette_serial_code
)

def generate_eeprom_file(self) -> None:
"""Generate eeprom file for pipette."""
with open(self.eeprom_file_path, "wb") as f:
f.write(self._generate_serial_code())


def _get_env_var(env_var_name: str) -> str:
"""Check that environment variable is set."""
env_var_value: Optional[str] = os.environ.get(env_var_name)
if env_var_value is None:
raise ValueError(
f"Environment variable {env_var_name} is not set. "
"Please set this environment variable and try again."
)
return env_var_value


def main() -> None:
"""Main function."""
left_pipette_json_string = _get_env_var(LEFT_PIPETTE_ENV_VAR_NAME)
right_pipette_json_string = _get_env_var(RIGHT_PIPETTE_ENV_VAR_NAME)

OT3PipetteEnvVar.from_json_string(left_pipette_json_string).generate_eeprom_file()
OT3PipetteEnvVar.from_json_string(right_pipette_json_string).generate_eeprom_file()


if __name__ == "__main__":
main()
87 changes: 87 additions & 0 deletions hardware/tests/test_scripts/test_emulation_pipette_provision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Tests for the emulation_pipette_provision script."""
import json
import os
from typing import Any, Generator, Tuple
import pytest
from opentrons_hardware.firmware_bindings.constants import PipetteName
from opentrons_hardware.scripts import emulation_pipette_provision
from opentrons_hardware.instruments.pipettes.serials import serial_val_from_parts


@pytest.fixture
def tmp_eeprom_file_paths(tmpdir: Any) -> Tuple[str, str]:
"""Create temporary eeprom file paths."""
volumes = tmpdir.mkdir("volumes")
left_dir = volumes.mkdir("left-pipette-eeprom")
right_dir = volumes.mkdir("right-pipette-eeprom")
left_file_path = left_dir.join("eeprom.bin")
right_file_path = right_dir.join("eeprom.bin")
return (str(left_file_path), str(right_file_path))


@pytest.fixture
def set_env_vars(
tmp_eeprom_file_paths: Generator[Tuple[str, str], None, None], monkeypatch: Any
) -> Generator[None, None, None]:
"""Set environment variables."""
left, right = tmp_eeprom_file_paths
monkeypatch.setenv(
"LEFT_OT3_PIPETTE_DEFINITION",
json.dumps(
{
"pipette_name": "p1000_multi",
"pipette_model": 34,
"pipette_serial_code": "20230609",
"eeprom_file_path": left,
}
),
)

monkeypatch.setenv(
"RIGHT_OT3_PIPETTE_DEFINITION",
json.dumps(
{
"pipette_name": "p50_multi",
"pipette_model": 34,
"pipette_serial_code": "20230609",
"eeprom_file_path": right,
}
),
)
yield
monkeypatch.delenv("LEFT_OT3_PIPETTE_DEFINITION")
monkeypatch.delenv("RIGHT_OT3_PIPETTE_DEFINITION")


@pytest.fixture
def expected_values() -> Tuple[bytes, bytes]:
"""Expected values."""
return (
serial_val_from_parts(
PipetteName["p1000_multi"], 34, "20230609".encode("utf-8")
),
serial_val_from_parts(PipetteName["p50_multi"], 34, "20230609".encode("utf-8")),
)


def test_main(
set_env_vars: Generator[None, None, None],
tmp_eeprom_file_paths: Tuple[str, str],
expected_values: Tuple[bytes, bytes],
) -> None:
"""Test main."""
left_eeprom_path, right_eeprom_path = tmp_eeprom_file_paths
expected_left, expected_right = expected_values
assert not os.path.exists(left_eeprom_path)
assert not os.path.exists(right_eeprom_path)
emulation_pipette_provision.main()
assert os.path.exists(left_eeprom_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth it to verify that the contents of these files is correct by calling serial_val_from_parts and reading the files. Unfortunate that we can't deserialize the encoded serial values though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, I am calling that function under the hood to generate the serial number.
Is it redundant to call the same function to compare that value to?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what makes it worth testing is that you want to verify that the script, viewed as a black box, is generating the right contents in the right files. I agree that it isn't the most thorough way to test it, but it at least makes sure you're 1) getting all of the info from the environment variable 2) putting the left/right info in the correct respective files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well good thing you suggested this.
I, being an idiot, was only ever evaluating the left pipette and storing it to both left and right.
Thanks @fsinapi. You saved me from myself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert os.path.exists(right_eeprom_path)

with open(left_eeprom_path, "rb") as f:
left_eeprom = f.read()
with open(right_eeprom_path, "rb") as f:
right_eeprom = f.read()

assert left_eeprom == expected_left
assert right_eeprom == expected_right