From 12d638cdff5b8c809e11383bbb95fa280e38c0c9 Mon Sep 17 00:00:00 2001
From: shinedday <shinedday@gmail.com>
Date: Mon, 9 May 2022 10:57:31 +0200
Subject: [PATCH] Add config.json support as well as better network update

---
 ihm.py                | 15 ++++++++++++---
 tool/confi_reader.py  | 10 +++++++---
 tool/config.json      | 19 ++-----------------
 tool/mqtt_client.py   | 13 +++++++++++--
 tool/remote_client.py |  6 ++++++
 tool/schedulable.py   |  5 +++++
 tool/ssh_client.py    | 21 +++++++++------------
 tool/update.py        | 24 ++++++++++++++----------
 8 files changed, 66 insertions(+), 47 deletions(-)
 create mode 100644 tool/remote_client.py

diff --git a/ihm.py b/ihm.py
index cc88f5b..ef306fc 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 cef7a36..da0b904 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 274c074..3d54d65 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 ed865d4..2c946a2 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 0000000..2627909
--- /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 293f35f..5ff162b 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 4fcc760..29f45cf 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 8d923f7..e78bdf9 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):
-- 
GitLab