Skip to content
Snippets Groups Projects
Commit 8d679e3c authored by shinedday's avatar shinedday
Browse files

Add lot of IHM QOl

parent 226ac344
Branches
Tags
No related merge requests found
# IoTAMAK-core # IoTAMAK-core
- [IoTAMAK-core](#iotamak-core)
- [Reseau](#reseau)
- [Random note](#random-note)
- [Agent Cycle](#agent-cycle)
- [Shared work](#shared-work)
- [Wait function](#wait-function)
- [Experience format](#experience-format)
- [Topics](#topics)
- [Agent](#agent)
- [Scheduler](#scheduler)
- [Amas](#amas)
- [Env](#env)
- [Mosquitto (Windows)](#mosquitto-windows)
- [Mosquitto (Linux) Didn't work](#mosquitto-linux-didnt-work)
requirement : requirement :
* paho-mqtt * paho-mqtt
...@@ -22,7 +38,7 @@ SSH activé avec hostname et mdp ...@@ -22,7 +38,7 @@ SSH activé avec hostname et mdp
WSL (pour pouvoir démarer l'amas) WSL (pour pouvoir démarer l'amas)
# ? # Random note
Start procédure : Start procédure :
1. start broker 1. start broker
...@@ -142,6 +158,27 @@ while not condition: ...@@ -142,6 +158,27 @@ while not condition:
Solution a trouvé ! Solution a trouvé !
# Experience format
```
/experienceNameFolder/
/agent.py
/amas.py
/env.py
/...
```
Les noms amas, agent et env sont obligatoire et ne peuvent pas etre modifier
Ils doivent contenir un code de la sorte :
```py
if __name__ == '__main__':
a = MyAgent(int(sys.argv[1]), ...) #MyAmas() MyEnv()
a.run()
```
Pour ajouter des attributs a l'initialisation aux agents il suffit de rajouter des `type(sys.argv[x])`.
# Topics # Topics
## Agent ## Agent
......
...@@ -5,15 +5,17 @@ from ast import literal_eval ...@@ -5,15 +5,17 @@ from ast import literal_eval
from pexpect import pxssh from pexpect import pxssh
from schedulable import Schedulable from schedulable import Schedulable
from ssh_client import SSHClient, Cmd
class Amas(Schedulable): class Amas(Schedulable, SSHClient):
""" """
Amas class Amas class
""" """
def __init__(self): def __init__(self):
Schedulable.__init__(self, client_id="Amas") Schedulable.__init__(self, client_id="Amas")
SSHClient.__init__(self)
self.subscribe("scheduler/schedulable/wakeup", self.wake_up) self.subscribe("scheduler/schedulable/wakeup", self.wake_up)
...@@ -29,28 +31,20 @@ class Amas(Schedulable): ...@@ -29,28 +31,20 @@ class Amas(Schedulable):
def on_initial_agents_creation(self): def on_initial_agents_creation(self):
pass pass
def add_agent(self): def add_agent(self, agent_to_create):
try:
s = pxssh.pxssh()
hostname = "192.168.123.18"
username = "pi"
password = "raspberry"
s.login(hostname, username, password)
s.sendline('nohup python \"Desktop/mqtt_goyon/iotamak-core/agent.py\" ' + str(self.next_id) + " &")
s.prompt()
print(s.before.decode('utf-8'))
agents = []
for _ in range(agent_to_create):
agents.append(Cmd(
cmd='nohup python \"Desktop/mqtt_goyon/iotamak-core/agent.py\" ' + str(self.next_id) + " &"
))
self.subscribe("agent/" + str(self.next_id) + "/metric", self.agent_metric) self.subscribe("agent/" + str(self.next_id) + "/metric", self.agent_metric)
self.subscribe("agent/" + str(self.next_id) + "/log", self.agent_log) self.subscribe("agent/" + str(self.next_id) + "/log", self.agent_log)
self.client.publish("amas/agent/new", self.next_id) self.client.publish("amas/agent/new", self.next_id)
self.next_id += 1 self.next_id += 1
s.logout() self.run_cmd(0, agents)
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)
def agent_log(self, client, userdata, message): def agent_log(self, client, userdata, message):
print("[Log] " + str(message.payload.decode("utf-8")) + " on topic " + message.topic) print("[Log] " + str(message.payload.decode("utf-8")) + " on topic " + message.topic)
......
import subprocess
import sys
from os import path
from time import sleep from time import sleep
from mqtt_client import MqttClient from mqtt_client import MqttClient
from ssh_client import SSHClient, Cmd, RemoteClient
from update import VersionManager
class Ihm(MqttClient): class Ihm(MqttClient, SSHClient):
def __init__(self): def __init__(self):
MqttClient.__init__(self, "Ihm") MqttClient.__init__(self, "Ihm")
SSHClient.__init__(self)
def run(self): def run(self):
exit_bool = False exit_bool = False
experiment_folder = ""
while not exit_bool: while not exit_bool:
cmd = input() print("\n")
cmd = input(">")
if cmd.lower() == "exit": if cmd.lower() == "exit":
self.client.publish("ihm/exit") self.client.publish("ihm/exit")
exit_bool = True exit_bool = True
if cmd.lower() == "kill":
commands = [
Cmd(
cmd="ps -ef | grep 'python '",
prefix="[BEFORE]"
),
Cmd(
cmd="for pid in $(ps -ef | grep 'python ' | awk '{print $2}'); do kill $pid; done",
do_print=False
),
Cmd(
cmd="ps -ef | grep 'python '",
prefix="[AFTER]"
)
]
for i_client in range(len(self.clients)):
print("Hostname :", self.clients[i_client].hostname, " User :", self.clients[i_client].user)
self.run_cmd(i_client, commands)
if cmd.lower() == "agent":
commands = [
Cmd(
cmd="ps -ef | grep 'python '"
)]
for i_client in range(len(self.clients)):
print("Hostname :", self.clients[i_client].hostname, " User :", self.clients[i_client].user)
self.run_cmd(i_client, commands)
if cmd.lower() == "update":
r = RemoteClient("192.168.105.18", "pi", "raspberry")
updater = VersionManager(r)
updater.update()
if cmd.lower() == "load":
print("Give Path to experiment folder(relavive)")
cmd = input(">")
# check if exist
print("Check experiment:")
print(" | -> folder : ", path.exists(cmd))
print(" | -> agent.py : ", path.exists(cmd + "/agent.py"))
print(" | -> amas.py : ", path.exists(cmd + "/amas.py"))
print(" | -> env.py : ", path.exists(cmd + "/env.py"))
if path.exists(cmd) and path.exists(cmd + "/agent.py") and path.exists(
cmd + "/amas.py") and path.exists(cmd + "/env.py"):
experiment_folder = cmd
print("Experiment loaded")
if cmd.lower() == "start":
if experiment_folder != "":
# choose exec
# start subprocess scheduler
subprocess.Popen([sys.executable, 'scheduler.py'])
# wait(1)
# start subprocess amas
subprocess.Popen([sys.executable, experiment_folder+'/amas.py'])
# start subprocess env
subprocess.Popen([sys.executable, experiment_folder+'/env.py'])
if cmd.lower() == "pause":
pass
if cmd.lower() == "unpause":
pass
sleep(2) sleep(2)
if __name__ == '__main__': if __name__ == '__main__':
a = Ihm() a = Ihm()
a.run() a.run()
\ No newline at end of file
...@@ -6,7 +6,7 @@ class MqttClient: ...@@ -6,7 +6,7 @@ class MqttClient:
def __init__(self, client_id: str = None): def __init__(self, client_id: str = None):
self.client = Client(client_id=client_id) self.client = Client(client_id=client_id)
self.client.username_pw_set(username="goyon", password="mosquitto") self.client.username_pw_set(username="goyon", password="mosquitto")
self.client.connect("192.168.199.209", 1883, 60) self.client.connect("192.168.105.209", 1883, 60)
self.client.loop_start() self.client.loop_start()
def subscribe(self, topic, fun): def subscribe(self, topic, fun):
......
...@@ -63,7 +63,7 @@ class PhiAgent(Agent): ...@@ -63,7 +63,7 @@ class PhiAgent(Agent):
self.fork_wait = True self.fork_wait = True
def on_decide(self): def on_decide(self):
# super().on_decide() super().on_decide()
if self.right_fork.tanken_by == -1: if self.right_fork.tanken_by == -1:
self.ask_spoon(False) self.ask_spoon(False)
...@@ -78,7 +78,7 @@ class PhiAgent(Agent): ...@@ -78,7 +78,7 @@ class PhiAgent(Agent):
self.ask_spoon(True) self.ask_spoon(True)
def on_act(self): def on_act(self):
# super().on_act() super().on_act()
if self.state == 0: if self.state == 0:
self.log("Is Thinking") self.log("Is Thinking")
......
from pexpect import pxssh
import sys import sys
import pathlib import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from amas import Amas from amas import Amas
from ssh_client import Cmd
class PhiAmas(Amas): class PhiAmas(Amas):
...@@ -15,31 +14,23 @@ class PhiAmas(Amas): ...@@ -15,31 +14,23 @@ class PhiAmas(Amas):
def on_initial_agents_creation(self): def on_initial_agents_creation(self):
for _ in range(self.agent_to_create): self.add_agent(self.agent_to_create)
self.add_agent()
def add_agent(self):
try:
s = pxssh.pxssh()
hostname = "192.168.199.18"
username = "pi"
password = "raspberry"
s.login(hostname, username, password)
s.sendline('nohup python \"Desktop/mqtt_goyon/iotamak-core/philosophers/phi_agent.py\" ' + str(self.next_id) def add_agent(self, agent_to_create):
+ " " + str(self.agent_to_create) + " &")
s.prompt()
print(s.before.decode('utf-8'))
agents = []
for _ in range(agent_to_create):
agents.append(Cmd(
cmd='nohup python \"Desktop/mqtt_goyon/iotamak-core/philosophers/agent.py\" ' + str(self.next_id)
+ " " + str(self.agent_to_create) + " &"
))
self.subscribe("agent/" + str(self.next_id) + "/log", self.agent_log) self.subscribe("agent/" + str(self.next_id) + "/log", self.agent_log)
self.client.publish("amas/agent/new", self.next_id) self.client.publish("amas/agent/new", self.next_id)
self.next_id += 1 self.next_id += 1
s.logout() self.run_cmd(0, agents)
except pxssh.ExceptionPxssh as e: # self.run_cmd(1, agents[2:])
print("pxssh failed on login.")
print(e)
if __name__ == '__main__': if __name__ == '__main__':
s = PhiAmas(3) s = PhiAmas(3)
......
File moved
...@@ -23,7 +23,7 @@ class Schedulable(MqttClient): ...@@ -23,7 +23,7 @@ class Schedulable(MqttClient):
def wake_up(self, client, userdata, message): def wake_up(self, client, userdata, message):
self.wake_up_token += 1 self.wake_up_token += 1
print("Waked up") # print("Waked up")
def wait(self): def wait(self):
# print("Waiting") # print("Waiting")
......
...@@ -95,7 +95,7 @@ class Scheduler(Schedulable): ...@@ -95,7 +95,7 @@ class Scheduler(Schedulable):
self.last_part() self.last_part()
# sleep(self.sleep_between_cycle) # sleep(self.sleep_between_cycle)
input() # input()
self.nbr_cycle += 1 self.nbr_cycle += 1
self.client.publish("scheduler/schedulable/wakeup", "") self.client.publish("scheduler/schedulable/wakeup", "")
......
from pexpect import pxssh
class Cmd:
def __init__(self, cmd: str, do_print: bool = True, prefix: str = ""):
self.cmd = cmd
self.do_print = do_print
self.prefix = prefix
class RemoteClient:
def __init__(self, hostname, user, password):
self.hostname = hostname
self.user = user
self.password = password
class SSHClient:
def __init__(self):
self.clients = [
RemoteClient("192.168.105.18", "pi", "raspberry")# ,
# RemoteClient("192.168.199.75", "pi", "raspberry")
]
def run_cmd(self, client: int, cmd: list):
try:
s = pxssh.pxssh()
dest = self.clients[client]
s.login(dest.hostname, dest.user, dest.password)
for command in cmd:
s.sendline(command.cmd)
s.prompt()
if command.do_print:
print(command.prefix, s.before.decode('utf-8'))
s.logout()
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)
import paramiko
import os
from ssh_client import RemoteClient
class VersionManager:
def __init__(self, remote_client: RemoteClient):
self.client = remote_client
def update(self):
# TODO: rm previous files
transport = paramiko.Transport((self.client.hostname, 22))
transport.connect(username=self.client.user, password=self.client.password)
sftp = MySFTPClient.from_transport(transport)
sftp.mkdir("./Desktop/mqtt_goyon/iotamak-core", ignore_existing=True)
sftp.put_dir("/mnt/d/work/stage m1/iotamak-core", "./Desktop/mqtt_goyon/iotamak-core")
sftp.close()
class MySFTPClient(paramiko.SFTPClient):
def put_dir(self, source, target):
''' Uploads the contents of the source directory to the target path. The
target directory needs to exists. All subdirectories in source are
created under target.
'''
for item in os.listdir(source):
if os.path.isfile(os.path.join(source, item)):
self.put(os.path.join(source, item), '%s/%s' % (target, item))
print(os.path.join(source, item))
else:
if any([i in item for i in [".git", "__pycache__", ".idea", ".vscode"]]):
pass
else:
self.mkdir('%s/%s' % (target, item), ignore_existing=True)
self.put_dir(os.path.join(source, item), '%s/%s' % (target, item))
print(os.path.join(source, item))
def mkdir(self, path, mode=511, ignore_existing=False):
''' Augments mkdir by adding an option to not fail if the folder exists '''
try:
super(MySFTPClient, self).mkdir(path, mode)
except IOError:
if ignore_existing:
pass
else:
raise
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment