diff --git a/ihm.py b/ihm.py index cc88f5bced09c5bca8227eb47eacc035674a38a1..ef306fc0ddcc308adf602aa874e676f0a3e8da14 100644 --- a/ihm.py +++ b/ihm.py @@ -4,7 +4,8 @@ from os import path from time import sleep from tool.mqtt_client import MqttClient -from tool.ssh_client import SSHClient, Cmd, RemoteClient +from tool.ssh_client import SSHClient, Cmd +from tool.remote_client import RemoteClient from tool.update import VersionManager class Ihm(MqttClient, SSHClient): @@ -63,8 +64,16 @@ class Ihm(MqttClient, SSHClient): # automatise la mise a jour de l'exerience sur les raspberry if cmd.lower() == "update": - r = RemoteClient("192.168.24.18", "pi", "raspberry") - updater = VersionManager(r) + commands = [ + Cmd( + cmd="rm -r Desktop/mqtt_goyon/iotamak-core" + ) + ] + 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) + + updater = VersionManager() updater.update() # charge une experience et verifie le format diff --git a/tool/confi_reader.py b/tool/confi_reader.py index cef7a3633606c6595d573a194fd28848bd1e91c9..da0b9046f354f6536799c3827bb5d15072bd9a8c 100644 --- a/tool/confi_reader.py +++ b/tool/confi_reader.py @@ -1,10 +1,13 @@ import json -from tool.ssh_client import RemoteClient +import sys +import pathlib +sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) +from tool.remote_client import RemoteClient -def read_ssh(path): +def read_ssh(path): with open(path, 'r', encoding='utf-8') as f: dict = json.load(f) @@ -17,8 +20,9 @@ def read_ssh(path): )) return res + def read_broker(path): with open(path, 'r', encoding='utf-8') as f: dict = json.load(f) - return dict.get("broker") \ No newline at end of file + return dict.get("broker") diff --git a/tool/config.json b/tool/config.json index 274c0747ced40ac9efe87b76055f8fc1a2c1e372..3d54d6516a4a3241a0e5a74671033b54a1be5c0a 100644 --- a/tool/config.json +++ b/tool/config.json @@ -1,23 +1,8 @@ { - "broker" : "192.168.1.1", + "broker" : "192.168.24.209", "clients_ssh" : [ { - "hostname" : "192.168.1.1", - "user" : "pi", - "password" : "raspberry" - }, - { - "hostname" : "192.168.1.1", - "user" : "pi", - "password" : "raspberry" - }, - { - "hostname" : "192.168.1.1", - "user" : "pi", - "password" : "raspberry" - }, - { - "hostname" : "192.168.1.1", + "hostname" : "192.168.24.18", "user" : "pi", "password" : "raspberry" } diff --git a/tool/mqtt_client.py b/tool/mqtt_client.py index ed865d4c78d2686231b541e9852007e00e5dd57f..2c946a2fb07b992c6f26105bb48b97948804885d 100644 --- a/tool/mqtt_client.py +++ b/tool/mqtt_client.py @@ -1,15 +1,24 @@ +import sys +import pathlib + +sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) + from paho.mqtt.client import Client +from tool.confi_reader import read_broker + 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("192.168.24.209", 1883, 60) + self.client.connect( + read_broker(str(pathlib.Path(__file__).parent.resolve())+"/config.json"), + 1883, 60 + ) self.client.loop_start() def subscribe(self, topic, fun): - self.client.subscribe(topic) self.client.message_callback_add(topic, fun) diff --git a/tool/remote_client.py b/tool/remote_client.py new file mode 100644 index 0000000000000000000000000000000000000000..2627909c77c321cb4a8a48e7ea2d665e19c2d387 --- /dev/null +++ b/tool/remote_client.py @@ -0,0 +1,6 @@ +class RemoteClient: + + def __init__(self, hostname, user, password): + self.hostname = hostname + self.user = user + self.password = password diff --git a/tool/schedulable.py b/tool/schedulable.py index 293f35f5287bebacf597e976f2d09e9b885e5e41..5ff162b1a1111f3dbe4b6a51d64408e344fd3b4e 100644 --- a/tool/schedulable.py +++ b/tool/schedulable.py @@ -3,6 +3,11 @@ Tool class that implement basic interaction that help to finish processes """ from time import sleep +import sys +import pathlib + +sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) + from tool.mqtt_client import MqttClient diff --git a/tool/ssh_client.py b/tool/ssh_client.py index 4fcc760d0c688fb1801aa13eca908ec197a3d16c..29f45cfe365add3c77928c088e33bdd8fb0d4cf7 100644 --- a/tool/ssh_client.py +++ b/tool/ssh_client.py @@ -1,5 +1,13 @@ +import os + from pexpect import pxssh +import sys +import pathlib + +sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) +from tool.confi_reader import read_ssh + class Cmd: @@ -9,21 +17,10 @@ class Cmd: 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.24.18", "pi", "raspberry")# , - # RemoteClient("192.168.199.75", "pi", "raspberry") - ] + self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve())+"/config.json") def run_cmd(self, client: int, cmd: list): try: diff --git a/tool/update.py b/tool/update.py index 8d923f717c17202f0b1cbfb2b359fae41ad622b8..e78bdf9a37f7b91c75cfada6905326b8acc0262d 100644 --- a/tool/update.py +++ b/tool/update.py @@ -1,23 +1,27 @@ import paramiko import os -from tool.ssh_client import RemoteClient +import sys +import pathlib + +sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) +from tool.confi_reader import read_ssh class VersionManager: - def __init__(self, remote_client: RemoteClient): - self.client = remote_client + def __init__(self): + self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve())+"/config.json") 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() + for client in self.clients: + transport = paramiko.Transport((client.hostname, 22)) + transport.connect(username=client.user, password=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):