Skip to content
Snippets Groups Projects
Commit 12d638cd authored by shinedday's avatar shinedday
Browse files

Add config.json support as well as better network update

parent a6a135c3
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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")
{
"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"
}
......
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)
class RemoteClient:
def __init__(self, hostname, user, password):
self.hostname = hostname
self.user = user
self.password = password
......@@ -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
......
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:
......
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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment