Skip to content

Commit

Permalink
Make calibration file attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
linucks committed Aug 23, 2024
1 parent ef90012 commit f262f37
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions mqtt_io/modules/sensor/dfr0300.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def __init__(self) -> None:
self.kvalue_low: float = INITIAL_KVALUE
self.kvalue_mid: float = INITIAL_KVALUE
self.kvalue_high: float = INITIAL_KVALUE
if os.path.exists(CALIBRATION_FILE):
self.calibration_file = CALIBRATION_FILE
if os.path.exists(self.calibration_file):
self.kvalue_low, self.kvalue_mid, self.kvalue_high = self.read_calibration()

def calibrate(self, voltage: float, temperature: float) -> None:
Expand Down Expand Up @@ -103,8 +104,7 @@ def calc_kvalue(
# Should think about looping to stabalise the reading before writing
self.write_calibration()

@staticmethod
def read_calibration() -> Tuple[float, float, float]:
def read_calibration(self) -> Tuple[float, float, float]:
"""Read calibrated values from json file.
{
"kvalue_low": 1.0,
Expand All @@ -113,22 +113,22 @@ def read_calibration() -> Tuple[float, float, float]:
}
"""
if os.path.exists(CALIBRATION_FILE):
if os.path.exists(self.calibration_file):
with open(
CALIBRATION_FILE, "r", encoding=CALIBRATION_FILE_ENCODING
self.calibration_file, "r", encoding=CALIBRATION_FILE_ENCODING
) as file_handle:
data = json.load(file_handle)
kvalue_low = float(data["kvalue_low"])
kvalue_mid = float(data["kvalue_mid"])
kvalue_high = float(data["kvalue_high"])
return (kvalue_low, kvalue_mid, kvalue_high)
raise FileNotFoundError(f"Calibration file ${CALIBRATION_FILE} not found")
raise FileNotFoundError(f"Calibration file ${self.calibration_file} not found")

def write_calibration(self) -> None:
"""Write calibrated values to json file."""
try:
with open(
CALIBRATION_FILE, "w", encoding=CALIBRATION_FILE_ENCODING
self.calibration_file, "w", encoding=CALIBRATION_FILE_ENCODING
) as file_handle:
data = {
"kvalue_low": self.kvalue_low,
Expand Down

0 comments on commit f262f37

Please sign in to comment.