From 0fa0dd0830199d77c0014c5e4bb306c3d049de7e Mon Sep 17 00:00:00 2001 From: shinedday <shinedday@gmail.com> Date: Thu, 5 May 2022 14:47:16 +0200 Subject: [PATCH] Add Schedulable class to simplify the rest --- agent.py | 24 +++--------------------- amas.py | 39 +++++---------------------------------- environment.py | 25 +++---------------------- ihm.py | 9 +++++++++ main.py | 19 ------------------- schedulable.py | 32 ++++++++++++++++++++++++++++++++ scheduler.py | 42 ++++++++++++++---------------------------- 7 files changed, 66 insertions(+), 124 deletions(-) delete mode 100644 main.py create mode 100644 schedulable.py diff --git a/agent.py b/agent.py index 4189823..1695997 100644 --- a/agent.py +++ b/agent.py @@ -1,31 +1,23 @@ import sys -import time from ast import literal_eval from typing import Dict -from mqtt_client import MqttClient +from schedulable import Schedulable -class Agent(MqttClient): +class Agent(Schedulable): def __init__(self, identifier: int) -> None: self.id = identifier - MqttClient.__init__(self, client_id="Agent"+str(self.id)) - - self.wait_delay = 0.01 + Schedulable.__init__(self, client_id="Agent"+str(self.id)) self.subscribe("scheduler/agent/wakeup", self.scheduler_wake_up) - self.wake_up_token = False - self.neighbors = [] self.next_neighbors = [] self.subscribe("amas/agent/"+str(self.id)+"/neighbor", self.add_neighbor) - self.exit_bool = False - self.subscribe("ihm/exit", self.exit_procedure) - self.on_initialization() self.publish("cycle_done", "") @@ -34,9 +26,6 @@ class Agent(MqttClient): def on_initialization(self): pass - def exit_procedure(self, client, userdata, message): - self.exit_bool = True - def add_neighbor(self, client, userdata, message): print(message.payload.decode("utf-8")) result = literal_eval(message.payload.decode("utf-8")) @@ -56,13 +45,6 @@ class Agent(MqttClient): self.wake_up_token = True print("waked up") - def wait(self): - print("waiting") - while not self.wake_up_token: - time.sleep(self.wait_delay) - self.wake_up_token = False - print("end wait") - def on_cycle_begin(self): self.log("on_cycle_begin") diff --git a/amas.py b/amas.py index 4d3ccbb..1f2d533 100644 --- a/amas.py +++ b/amas.py @@ -4,62 +4,35 @@ Amas class from ast import literal_eval from pexpect import pxssh -from mqtt_client import MqttClient -from time import sleep +from schedulable import Schedulable -from agent import Agent - -def create_agent(identifier): - a = Agent(identifier) - a.run() - return - -class Amas(MqttClient): +class Amas(Schedulable): """ Amas class """ def __init__(self): - MqttClient.__init__(self, client_id="Amas") - self.nbr_cycle: int = 0 + Schedulable.__init__(self, client_id="Amas") self.subscribe("scheduler/schedulable/wakeup", self.scheduler_wake_up) - self.wait_delay = 0.01 - self.wake_up_token = False - self.next_id = 0 - - self.exit_bool = False - self.subscribe("ihm/exit", self.exit_procedure) - self.agents_metric = [None for _ in range(self.next_id)] self.on_initialization() self.on_initial_agents_creation() - self.client.publish("amas/action_done", "") def on_initial_agents_creation(self): pass - def exit_procedure(self, client, userdata, message): - self.exit_bool = True - def scheduler_wake_up(self, client, userdata, message): self.wake_up_token = True print("waked up") - def wait(self): - print("waiting") - while not self.wake_up_token: - sleep(self.wait_delay) - self.wake_up_token = False - print("end wait") - def add_agent(self): try: s = pxssh.pxssh() @@ -68,7 +41,7 @@ class Amas(MqttClient): password = "raspberry" s.login(hostname, username, password) - s.sendline('nohup python \"Desktop/mqtt_goyon/iotamak-core/agent.py\" ' + str(self.next_id)+" &") + s.sendline('nohup python \"Desktop/mqtt_goyon/iotamak-core/agent.py\" ' + str(self.next_id) + " &") s.prompt() print(s.before.decode('utf-8')) @@ -92,7 +65,6 @@ class Amas(MqttClient): agent_id = result.get("id") self.agents_metric[agent_id] = result - def on_initialization(self) -> None: """ This method will be executed at the end of __init__() @@ -111,7 +83,6 @@ class Amas(MqttClient): """ pass - def run(self) -> None: while not self.exit_bool: @@ -123,7 +94,7 @@ class Amas(MqttClient): self.on_cycle_begin() self.client.publish("amas/action_done", "") - #agent cycle + # agent cycle self.wait() self.on_cycle_end() diff --git a/environment.py b/environment.py index da5e886..407fef8 100644 --- a/environment.py +++ b/environment.py @@ -1,28 +1,20 @@ """ Environment class """ -from time import sleep -from mqtt_client import MqttClient +from schedulable import Schedulable -class Environment(MqttClient): +class Environment(Schedulable): """ Environment class """ def __init__(self): - MqttClient.__init__(self, client_id="Env") - self.nbr_cycle: int = 0 + Schedulable.__init__(self, client_id="Env") self.subscribe("scheduler/schedulable/wakeup", self.scheduler_wake_up) - self.wait_delay = 0.01 - self.wake_up_token = False - - self.exit_bool = False - self.subscribe("ihm/exit", self.exit_procedure) - self.on_initialization() self.client.publish("env/action_done", "") @@ -31,17 +23,6 @@ class Environment(MqttClient): self.wake_up_token = True print("waked up") - def wait(self): - print("waiting") - while not self.wake_up_token: - sleep(self.wait_delay) - self.wake_up_token = False - print("end wait") - - - def exit_procedure(self, client, userdata, message): - self.exit_bool = True - def on_initialization(self) -> None: """ This method will be executed at the end of __init__() diff --git a/ihm.py b/ihm.py index 4118743..f7300fc 100644 --- a/ihm.py +++ b/ihm.py @@ -1,3 +1,5 @@ +from time import sleep + from mqtt_client import MqttClient @@ -17,3 +19,10 @@ class Ihm(MqttClient): if cmd.lower() == "exit": self.client.publish("ihm/exit") exit_bool = True + + sleep(2) + + +if __name__ == '__main__': + a = Ihm() + a.run() \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index a88bb1c..0000000 --- a/main.py +++ /dev/null @@ -1,19 +0,0 @@ -import sys - -from amas import Amas - -class SimpleAmas(Amas): - - def __init__(self, nbr_agent): - self.agent_to_create = nbr_agent - super().__init__() - - def on_initial_agents_creation(self): - for _ in range(self.agent_to_create): - self.add_agent() - -if __name__ == '__main__': - print("nbr agent: ", int(sys.argv[1])) - - a = SimpleAmas(int(sys.argv[1])) - a.run() diff --git a/schedulable.py b/schedulable.py new file mode 100644 index 0000000..43e33c5 --- /dev/null +++ b/schedulable.py @@ -0,0 +1,32 @@ +""" +Tool class that implement basic interaction that help to finish processes +""" +from time import sleep + +from mqtt_client import MqttClient + + +class Schedulable(MqttClient): + """ + Base class for Agent/Amas/Env/scheduler + """ + + def __init__(self, client_id: str): + MqttClient.__init__(self, client_id) + + self.exit_bool: bool = False + self.subscribe("ihm/exit", self.exit_procedure) + + self.nbr_cycle: int = 0 + self.wait_delay: float = 0.01 + self.wake_up_token: int = 0 + + def wait(self): + # print("Waiting") + while not self.wake_up_token > 1: + sleep(self.wait_delay) + self.wake_up_token -= 1 + # print("End wait") + + def exit_procedure(self, client, userdata, message): + self.exit_bool = True diff --git a/scheduler.py b/scheduler.py index f0078e7..d5290db 100644 --- a/scheduler.py +++ b/scheduler.py @@ -1,16 +1,13 @@ from time import sleep -from mqtt_client import MqttClient +from schedulable import Schedulable -class Scheduler(MqttClient): +class Scheduler(Schedulable): def __init__(self): - MqttClient.__init__(self, client_id="Scheduler") - self.nbr_cycle: int = 0 - - self.wait_delay = 0.01 + Schedulable.__init__(self, client_id="Scheduler") self.sleep_between_cycle = 5 self.nbr_agent = 0 @@ -21,16 +18,12 @@ class Scheduler(MqttClient): self.agent_waiting = 0 self.schedulable_waiting = 0 - self.exit_bool = False - self.subscribe("ihm/exit", self.exit_procedure) - print("Init done") - def exit_procedure(self, client, userdata, message): - self.exit_bool = True - def update_schedulable(self, client, userdata, message): self.schedulable_waiting += 1 + if self.schedulable_waiting == 2: + self.wake_up_token += 1 print("__Schedulable is waiting") def update_nbr_agent(self, client, userdata, message): @@ -40,18 +33,10 @@ class Scheduler(MqttClient): def agent_done(self, client, userdata, message): self.agent_waiting += 1 + if self.agent_waiting >= self.nbr_agent: + self.wake_up_token += 1 print("__Agent done") - def wait_agent(self): - while self.agent_waiting < self.nbr_agent: - sleep(self.wait_delay) - self.agent_waiting = 0 - - def wait_schedulable(self): - while self.schedulable_waiting < 2: - sleep(self.wait_delay) - self.schedulable_waiting = 0 - def first_part(self) -> None: """ first part of a cycle @@ -59,7 +44,7 @@ class Scheduler(MqttClient): self.client.publish("scheduler/schedulable/wakeup", "") # Amas on cycle begin # Environment on cycle begin - self.wait_schedulable() + self.wait() def main_part(self) -> None: """ @@ -67,10 +52,10 @@ class Scheduler(MqttClient): """ self.client.publish("scheduler/agent/wakeup", "") # Agent doing phase 1 - self.wait_agent() + self.wait() self.client.publish("scheduler/agent/wakeup", "") # agent doing phase 2 - self.wait_agent() + self.wait() def last_part(self) -> None: """ @@ -79,7 +64,7 @@ class Scheduler(MqttClient): self.client.publish("scheduler/schedulable/wakeup", "") # Amas on cycle end # Environment on cycle end - self.wait_schedulable() + self.wait() def run(self) -> None: """ @@ -88,10 +73,10 @@ class Scheduler(MqttClient): # wait that all schedulable have init print("Waiting schedulable") - self.wait_schedulable() + self.wait() # Wait that all agent have init print("Waiting agents :", self.nbr_agent) - self.wait_agent() + self.wait() while not self.exit_bool: print("Cycle : ", self.nbr_cycle) @@ -109,6 +94,7 @@ class Scheduler(MqttClient): self.client.publish("scheduler/schedulable/wakeup", "") self.client.publish("scheduler/agent/wakeup", "") + sleep(2) if __name__ == '__main__': -- GitLab