diff --git a/inky_run.py b/inky_run.py index e2fff6a..2fb9718 100644 --- a/inky_run.py +++ b/inky_run.py @@ -1,7 +1,12 @@ +"""Basic Inkycal run script. + +Assumes that the settings.json file is in the /boot directory. +set render=True to render the display, set render=False to only run the modules. +""" import asyncio from inkycal import Inkycal inky = Inkycal(render=True) # Initialise Inkycal # If your settings.json file is not in /boot, use the full path: inky = Inkycal('path/to/settings.json', render=True) -inky.test() # test if Inkycal can be run correctly, running this will show a bit of info for each module +inky.run(run_once=True) # test if Inkycal can be run correctly, running this will show a bit of info for each module asyncio.run(inky.run()) # If there were no issues, you can run Inkycal nonstop diff --git a/inkycal/main.py b/inkycal/main.py index ac3dd88..c1dd0f1 100644 --- a/inkycal/main.py +++ b/inkycal/main.py @@ -13,7 +13,7 @@ from inkycal import loggers # noqa from inkycal.custom import * from inkycal.display import Display from inkycal.modules.inky_image import Inkyimage as Images -from inkycal.utils.json_cache import JSONCache +from inkycal.utils import JSONCache logger = logging.getLogger(__name__) diff --git a/inkycal/modules/inkycal_slideshow.py b/inkycal/modules/inkycal_slideshow.py index 9f098df..253397d 100755 --- a/inkycal/modules/inkycal_slideshow.py +++ b/inkycal/modules/inkycal_slideshow.py @@ -8,7 +8,7 @@ from inkycal.custom import * # PIL has a class named Image, use alias for Inkyimage -> Images from inkycal.modules.inky_image import Inkyimage as Images, image_to_palette from inkycal.modules.template import inkycal_module -from inkycal.utils.json_cache import JSONCache +from inkycal.utils import JSONCache logger = logging.getLogger(__name__) diff --git a/inkycal/utils/__init__.py b/inkycal/utils/__init__.py new file mode 100644 index 0000000..e0a85ab --- /dev/null +++ b/inkycal/utils/__init__.py @@ -0,0 +1,2 @@ +from .pisugar import PiSugar +from .json_cache import JSONCache \ No newline at end of file diff --git a/inkycal/utils/pisugar.py b/inkycal/utils/pisugar.py new file mode 100644 index 0000000..7f00cee --- /dev/null +++ b/inkycal/utils/pisugar.py @@ -0,0 +1,78 @@ +"""PiSugar helper class for Inkycal.""" + +import logging +import subprocess + +from inkycal.settings import Settings + +settings = Settings() + +logger = logging.getLogger(__name__) + + +class PiSugar: + + def __init__(self): + # replace "command" with actual command + self.command_template = 'echo "command" | nc -q 0 127.0.0.1 8423' + self.allowed_commands = ["get battery", "get model", "get rtc_time", "get rtc_alarm_enabled", + "get rtc_alarm_time", "get alarm_repeat", "rtc_pi2rtc"] + + def _get_output(self, command): + if command not in self.allowed_commands: + logger.error(f"Command {command} not allowed") + return None + cmd = self.command_template.replace("command", command) + try: + result = subprocess.run(cmd, text=True, capture_output=True) + if result.returncode != 0: + print(f"Command failed with {result.stderr}") + return None + return result + except Exception as e: + logger.error(f"Error executing command: {e}") + return None + + def get_battery(self) -> int or None: + """Get the battery level in percentage. + + Returns: + int or None: The battery level in percentage or None if the command fails. + """ + battery_output = self._get_output("get battery") + if battery_output: + for line in battery_output.splitlines(): + if 'battery:' in line: + return int(line.split(':')[1].strip()) + return None + + def get_model(self): + """Get the PiSugar model.""" + model_output = self._get_output("get model") + if model_output: + for line in model_output.splitlines(): + if 'model:' in line: + return line.split(':')[1].strip() + return None + + def get_rtc_time(self): + """Get the RTC time.""" + return self._get_output("get rtc_time") + + def get_rtc_alarm_enabled(self): + """Get the RTC alarm enabled status.""" + return self._get_output("get rtc_alarm_enabled") + + def get_rtc_alarm_time(self): + """Get the RTC alarm time.""" + return self._get_output("get rtc_alarm_time") + + def get_alarm_repeat(self): + """Get the alarm repeat status.""" + return self._get_output("get alarm_repeat") + + def rtc_pi2rtc(self): + """Sync the Pi time to RTC.""" + return self._get_output("rtc_pi2rtc") + +