From 0eb830c23a0a5675a25a20a01e7a6c2cb23bb93e Mon Sep 17 00:00:00 2001 From: shinedday <shinedday@gmail.com> Date: Mon, 9 May 2022 16:19:54 +0200 Subject: [PATCH] Add comment to agent class --- agent.py | 62 +++++++++++++++++++++++++++++++++++++-------- tool/mqtt_client.py | 5 +--- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/agent.py b/agent.py index 75c5a11..dc77b1b 100644 --- a/agent.py +++ b/agent.py @@ -1,3 +1,6 @@ +""" +Agent class file +""" import sys from ast import literal_eval from typing import Dict @@ -6,6 +9,9 @@ from tool.schedulable import Schedulable class Agent(Schedulable): + """ + base class for agent + """ def __init__(self, identifier: int) -> None: self.id = identifier @@ -23,15 +29,27 @@ class Agent(Schedulable): self.publish("cycle_done", "") print("init done") - def on_initialization(self): + def on_initialization(self) -> None: + """ + This method will be executed at the end of __init__() + """ pass - def add_neighbor(self, client, userdata, message): + def add_neighbor(self, client, userdata, message) -> None: + """ + Called when the agent, receive metrics + put the metric in a list that will be rad during on_perceive + param message: metric (dict) of the neighbor + """ print(message.payload.decode("utf-8")) result = literal_eval(message.payload.decode("utf-8")) self.next_neighbors.append(result) def log(self, message: str) -> None: + """ + Convenient method to log things (will be printed in the amas's stdout + :param message: + """ self.client.publish( "agent/" + str(self.id) + "/log", "[AGENT] " + str(self.id) + " : " + message @@ -39,27 +57,53 @@ class Agent(Schedulable): return def publish(self, topic: str, message) -> None: + """ + publish a message on the topic + :param topic: str + :param message: content of the message + """ self.client.publish("agent/" + str(self.id) + "/" + topic, message) - def on_cycle_begin(self): + def on_cycle_begin(self) -> None: + """ + This method will be executed at the start of each cycle + """ self.log("on_cycle_begin") - def on_perceive(self): + def on_perceive(self) -> None: + """ + Method that should be used to open the neighbor metrics and use them + """ self.log("on_perceive") - def on_decide(self): + def on_decide(self) -> None: + """ + Should be override + """ self.log("on_decide") - def on_act(self): + def on_act(self) -> None: + """ + Should be override + """ self.log("on_act") - def on_cycle_end(self): + def on_cycle_end(self) -> None: + """ + This method will be executed at the end of each cycle + """ self.log("on_cycle_end") def send_metric(self) -> Dict: + """ + Should be override if the neighbor need to be aware of any other info, should be a dict + """ return {"id": self.id} - def run(self): + def run(self) -> None: + """ + Main method of the agent + """ while not self.exit_bool: self.wait() @@ -81,7 +125,5 @@ class Agent(Schedulable): if __name__ == '__main__': - print("id agent: ", int(sys.argv[1])) - a = Agent(int(sys.argv[1])) a.run() diff --git a/tool/mqtt_client.py b/tool/mqtt_client.py index 2c946a2..0384587 100644 --- a/tool/mqtt_client.py +++ b/tool/mqtt_client.py @@ -13,10 +13,7 @@ class MqttClient: def __init__(self, client_id: str = None): self.client = Client(client_id=client_id) self.client.username_pw_set(username="goyon", password="mosquitto") - self.client.connect( - read_broker(str(pathlib.Path(__file__).parent.resolve())+"/config.json"), - 1883, 60 - ) + self.client.connect(read_broker(str(pathlib.Path(__file__).parent.resolve()) + "/config.json")) self.client.loop_start() def subscribe(self, topic, fun): -- GitLab