diff --git a/dist/iotAmak-0.0.1-py3-none-any.whl b/dist/iotAmak-0.0.1-py3-none-any.whl index 6593a49ae36d5395a80151bb29aef8b266b2bb5f..4cab34533845b9eed0c0fc38bd61dc3fcf727f46 100644 Binary files a/dist/iotAmak-0.0.1-py3-none-any.whl and b/dist/iotAmak-0.0.1-py3-none-any.whl differ diff --git a/dist/iotAmak-0.0.2-py3-none-any.whl b/dist/iotAmak-0.0.2-py3-none-any.whl new file mode 100644 index 0000000000000000000000000000000000000000..dd5c9043d601ea0921fdeb91c6f1d05defb0ff05 Binary files /dev/null and b/dist/iotAmak-0.0.2-py3-none-any.whl differ diff --git a/iotAmak/agent.py b/iotAmak/agent.py index 52a1bf3d74af6df508105ee4740f4ba6e483a033..f749a10d66939e8e24eab2e05e12599e0ede0e2f 100644 --- a/iotAmak/agent.py +++ b/iotAmak/agent.py @@ -2,7 +2,7 @@ Agent class file """ from ast import literal_eval -from typing import Dict +from typing import Dict, List import sys import pathlib @@ -18,14 +18,14 @@ class Agent(Schedulable): """ def __init__(self, identifier: int, broker_ip: str) -> None: - self.id = identifier + self.id: int = identifier Schedulable.__init__(self, broker_ip, "Agent" + str(self.id)) self.subscribe("scheduler/agent/wakeup", self.wake_up) - self.neighbors = [] - self.next_neighbors = [] + self.neighbors: List[Dict] = [] + self.next_neighbors: List[Dict] = [] self.subscribe("amas/agent/" + str(self.id) + "/neighbor", self.add_neighbor) self.on_initialization() @@ -58,7 +58,6 @@ class Agent(Schedulable): "agent/" + str(self.id) + "/log", "[AGENT] " + str(self.id) + " : " + message ) - return def publish(self, topic: str, message) -> None: """ @@ -126,3 +125,4 @@ class Agent(Schedulable): self.publish("metric", str(self.send_metric())) self.publish("cycle_done", "") + self.nbr_cycle += 1 diff --git a/iotAmak/amas.py b/iotAmak/amas.py index 304d48756a121b0a8815f4a56b1fede6be6a0790..89a83123be56087cb6683b397f6f2229600b0be4 100644 --- a/iotAmak/amas.py +++ b/iotAmak/amas.py @@ -2,7 +2,7 @@ Amas class """ from ast import literal_eval -from typing import List +from typing import List, Dict import sys import pathlib @@ -26,20 +26,20 @@ class Amas(Schedulable, SSHClient): SSHClient.__init__(self, true_client) - self.broker_ip = broker_ip + self.broker_ip: str = broker_ip self.subscribe("scheduler/schedulable/wakeup", self.wake_up) - self.next_id = 0 + self.next_id: int = 0 - self.agents_cmd = [] + self.agents_cmd: List[Cmd] = [] self.on_initialization() self.on_initial_agents_creation() - self.agents_metric = [None for _ in range(self.next_id)] + self.agents_metric: List[Dict] = [{} for _ in range(self.next_id)] self.client.publish("amas/action_done", "") - def on_initial_agents_creation(self): + def on_initial_agents_creation(self) -> None: """ Convenient method to initially create the agents, is called at the end of initialization """ @@ -128,6 +128,7 @@ class Amas(Schedulable, SSHClient): if self.exit_bool: return + self.publish("amas/all_metric", str(self.agents_metric)) self.on_cycle_begin() self.client.publish("amas/action_done", "") diff --git a/iotAmak/scheduler.py b/iotAmak/scheduler.py index 9c97156ff4e5c2551adb5a264a28dec8a24dd0c8..ef7f5b5d1484912dd5bd45b5badb9b6e89c072a3 100644 --- a/iotAmak/scheduler.py +++ b/iotAmak/scheduler.py @@ -160,13 +160,9 @@ class Scheduler(Schedulable): sleep(self.sleep_between_cycle) self.nbr_cycle += 1 + self.publish("scheduler/cycledone", "") # exit self.client.publish("scheduler/schedulable/wakeup", "") self.client.publish("scheduler/agent/wakeup", "") sleep(2) - - -if __name__ == '__main__': - s = Scheduler(str(sys.argv[1])) - s.run() diff --git a/iotAmak/tool/mqtt_client.py b/iotAmak/tool/mqtt_client.py index 1311df14878d836ff16aa82391165064276e3982..3c5289ac766e68c2acd355966455da4c258caf3d 100644 --- a/iotAmak/tool/mqtt_client.py +++ b/iotAmak/tool/mqtt_client.py @@ -1,17 +1,31 @@ +""" +MQTT client class file +""" +from typing import Callable + from paho.mqtt.client import Client class MqttClient: + """ + Base class to any instance that need to interact with the broker + """ - def __init__(self, broker_ip, client_id): - self.client = Client(client_id=client_id) + def __init__(self, broker_ip: str, client_id: str): + self.client: Client = Client(client_id=client_id) self.client.username_pw_set(username="goyon", password="mosquitto") self.client.connect(host=broker_ip) self.client.loop_start() - def subscribe(self, topic, fun): + def subscribe(self, topic: str, fun: Callable) -> None: + """ + subscribe to the topic, and use fun whenever you receive a message + """ self.client.subscribe(topic) self.client.message_callback_add(topic, fun) - def publish(self, topic, message): + def publish(self, topic: str, message) -> None: + """ + publish the message in a specified topic + """ self.client.publish(topic, message) diff --git a/iotAmak/tool/remote_client.py b/iotAmak/tool/remote_client.py index f257a535787eeb4bba96599a66a00b5de9402fca..21a84c9b4e3fc0e6eaf24d62f5bb3b76b984ea77 100644 --- a/iotAmak/tool/remote_client.py +++ b/iotAmak/tool/remote_client.py @@ -1,9 +1,20 @@ +""" +remote client class file +""" + + class RemoteClient: + """ + Class used to store information about the raspberry + """ - def __init__(self, hostname, user, password): - self.hostname = hostname - self.user = user - self.password = password + def __init__(self, hostname: str, user: str, password: str): + self.hostname: str = hostname + self.user: str = user + self.password: str = password def to_send(self) -> dict: + """ + convert the current instance in a dict, use to send it through command line + """ return {"hostname": self.hostname, "user": self.user, "password": self.password} diff --git a/iotAmak/tool/schedulable.py b/iotAmak/tool/schedulable.py index 3ba5e34f0d095ad743216696a78957ab82f39580..0abff6e5ff7e0451d908cdab0e6cecc61cacb589 100644 --- a/iotAmak/tool/schedulable.py +++ b/iotAmak/tool/schedulable.py @@ -26,16 +26,25 @@ class Schedulable(MqttClient): self.wait_delay: float = 0.01 self.wake_up_token: int = 0 - def wake_up(self, client, userdata, message): + def wake_up(self, client, userdata, message) -> None: + """ + Called by the scheduler to wake up the schedulable + """ self.wake_up_token += 1 # print("Waked up") - def wait(self): + def wait(self) -> None: + """ + Basic wait method + """ # print("Waiting") while self.wake_up_token == 0: sleep(self.wait_delay) self.wake_up_token -= 1 # print("End wait") - def exit_procedure(self, client, userdata, message): + def exit_procedure(self, client, userdata, message) -> None: + """ + Called by the Ihm to exit as soon as possible + """ self.exit_bool = True diff --git a/iotAmak/tool/ssh_client.py b/iotAmak/tool/ssh_client.py index 5d4155f9379ff320fdb0270d7e3a4e22b06e255c..83f1f76df0451905b74dd0fa72cc458c5130ebf8 100644 --- a/iotAmak/tool/ssh_client.py +++ b/iotAmak/tool/ssh_client.py @@ -26,7 +26,7 @@ class SSHClient: def __init__(self, clients: List[RemoteClient]): self.clients = clients - def run_cmd(self, client: int, cmd: list) -> list[str]: + def run_cmd(self, client: int, cmd: list, repeat: bool = False) -> list[str]: ret = [] try: s = pxssh.pxssh() @@ -44,6 +44,8 @@ class SSHClient: except pxssh.ExceptionPxssh as e: print("pxssh failed on login.") print(e) + if not repeat: + self.run_cmd(client, cmd, True) return ret def update(self, experiment_name, path_to_experiment): diff --git a/setup.py b/setup.py index c1654f3852c9c7ad3dd054fe260975e19b8a5e33..0388a00fec50a68a58b7edf63e1ebec919198901 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup( name='iotAmak', packages=find_packages(), - version='0.0.1', + version='0.0.2', description='AmakFramework in python', author='SMAC - GOYON Sebastien', install_requires=[],