Skip to content
Snippets Groups Projects
schedulable.py 1.16 KiB
"""
Tool class that implement basic interaction that help to finish processes
"""
from time import sleep

import sys
import pathlib
import threading

sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))

from iotAmak.tool.mqtt_client import MqttClient


class Schedulable(MqttClient):
    """
    Base class for Agent/Amas/Env/scheduler
    """

    def __init__(self, broker_ip: str, client_id: str):
        MqttClient.__init__(self, broker_ip, client_id)

        self.exit_bool: bool = False
        self.subscribe("ihm/exit", self.exit_procedure)

        self.nbr_cycle: int = 0

        self.semaphore = threading.Semaphore(0)

    def wake_up(self, client, userdata, message) -> None:
        """
        Called by the scheduler to wake up the schedulable
        """
        self.semaphore.release()
        # print("Waked up")

    def wait(self) -> None:
        """
        Basic wait method
        """
        # print("Waiting")
        self.semaphore.release()
        # print("End wait")

    def exit_procedure(self, client, userdata, message) -> None:
        """
        Called by the Ihm to exit as soon as possible
        """
        self.exit_bool = True