def save(self) -> bool: logger.debug(f"saving to {self.filepath}") if not self.filepath.exists(): return False with open(self.filepath, "w") as f: json.dump(self.light_data, f) return True
def setup(self) -> None: logger.debug("setting up light data") parent = self.filepath.parent parent.mkdir(parents=True, exist_ok=True) if not self.filepath.exists(): fh = open(self.filepath, "w") fh.write("[]") fh.close()
def read(self) -> list: logger.debug(f"reading from file {self.filepath}") with open(self.filepath) as f: json_content = f.read() if not json_content: json_content = "[]" self.light_data = json.loads(json_content) return self.light_data
def soil_moisture_runner(plant: BasePlant) -> None: water_pump = WaterPump() logger.debug("starting soil moisture runner") while True: soil_moisture = get_soil_moisture_value() logger.debug(f"soil moisture: {soil_moisture}") if soil_moisture < plant.soil_moisture_min: water_pump.run() time.sleep(3)
def temperature_runner(plant: BasePlant) -> None: while True: temperature = get_temperature_data() logger.debug(f"temperature: {temperature}") if temperature < plant.temperature_min: toggle_heater() time.sleep(3)
def fetch_data(self) -> dict: logger.debug("fetching light data") response = requests.get(API_URL) response.raise_for_status() data = response.json() sunrise = parse_datetime(data["results"]["sunrise"]).isoformat() sunset = parse_datetime(data["results"]["sunset"]).isoformat() object = {"date": self.today, "sunrise": sunrise, "sunset": sunset} self.append_data(object) return object
def humidity_runner(plant: BasePlant) -> None: while True: humidity = get_humidity_data() logger.debug( f"humidity: {humidity} / humidifier.is_running: {humidifier.is_running}" ) if not humidifier.is_running and humidity < plant.humidity_min: humidifier.toggle() elif humidifier.is_running and humidity > plant.optimal_humidity: humidifier.toggle() time.sleep(3)
def main(): """ Get the sunrise and sunset data for the day Usage: python pyplanter/scripts/light.py """ while True: logger.debug("Updating light data") light = Light() light.get_latest_data() light.save() time.sleep(60)
def get_latest_data(self) -> dict: logger.debug("getting latest light data") data = self.light_data if len(data) and data[-1]["date"] == self.today: return data[-1] return self.fetch_data()
def remove_job(self, id: str): logger.debug(f"Removing job from queue: {id}") if not self.scheduler.get_job(id): logger.warning(f"Unable to remove job {id}") return self.scheduler.remove_job(id)
def add_job(self, id: str, func: Callable, **kwargs): logger.debug(f"Adding job to queue: {id}") self.jobs[id] = self.scheduler.add_job(func, **kwargs)