Innovenergy_trunk/firmware/Venus_Release/update_Venus.py

119 lines
4.1 KiB
Python
Raw Normal View History

2024-07-01 09:49:57 +00:00
import subprocess
import sys
import asyncio
import os
async def run_remote_command(remote_host, command):
ssh_command = ['ssh', f'root@{remote_host}', command]
process = await asyncio.create_subprocess_exec(
*ssh_command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
stdout_decoded = stdout.decode()
stderr_decoded = stderr.decode()
if process.returncode == 0:
return stdout_decoded + stderr_decoded
else:
return f"Failed to run the command: {command}, error: {stderr_decoded}"
async def check_GX_type(remote_host):
command = "cat /etc/venus/machine"
return await run_remote_command(remote_host, command)
async def run_rclocal(remote_host):
command = "chmod +x /data/rc.local && /data/rc.local"
return await run_remote_command(remote_host, command)
2024-07-02 11:27:06 +00:00
async def stop_battery_service(remote_host):
command = "/opt/victronenergy/serial-starter/stop-tty.sh ttyUSB0"
# command = "/opt/victronenergy/serial-starter/stop-tty.sh ttyUSB1"
return await run_remote_command(remote_host, command)
async def start_battery_service(remote_host):
command = "/opt/victronenergy/serial-starter/start-tty.sh ttyUSB0"
# command = "/opt/victronenergy/serial-starter/start-tty.sh ttyUSB1"
return await run_remote_command(remote_host, command)
2024-07-01 09:49:57 +00:00
async def upload_files(remote_host):
file_location_mappings = {
"rc.local": "/data/",
"dbus-fzsonick-48tl": "/data/",
"pika-0.13.1": "/data/innovenergy/"
}
venus_release_files_folder = os.path.join(os.getcwd(), "VenusReleaseFiles")
if not os.path.exists(venus_release_files_folder):
return "VenusReleaseFiles folder does not exist."
try:
tasks = []
for file_name, remote_location in file_location_mappings.items():
file_path = os.path.join(venus_release_files_folder, file_name)
if not os.path.exists(file_path):
raise FileNotFoundError(f"File {file_name} not found in {venus_release_files_folder}.")
command = [
"rsync",
"-r",
file_path,
f"root@{remote_host}:{remote_location}"
]
tasks.append(command)
# Execute rsync commands asynchronously
for task in tasks:
subprocess.run(task, check=True)
return "All files uploaded successfully."
except FileNotFoundError as e:
return str(e)
except subprocess.CalledProcessError as e:
return f"Error occurred while uploading files: {e}"
except Exception as e:
return f"An error occurred while uploading files: {str(e)}"
async def import_pika(remote_host):
change_dir_command = "cd /data/innovenergy/pika-0.13.1/"
install_command = "python2 setup.py install --user"
command = f"{change_dir_command} && {install_command}"
return await run_remote_command(remote_host, command)
async def main(remote_host):
##### 1. check whether it's Venus ######
gx_type = await check_GX_type(remote_host)
if gx_type == "beaglebone\n":
##### 2. upload VPN and battery files ######
print("Upload pika and battery files!")
if(await upload_files(remote_host)!="All files uploaded successfully."):
sys.exit("Failed to upload files!")
else:
print(await upload_files(remote_host))
#### 3. import pika ####
print("Import pika!")
print(await import_pika(remote_host))
2024-07-02 11:27:06 +00:00
##### 4. stop battery service ######
print("Stop battery service!")
print(await stop_battery_service(remote_host))
##### 5. run rc.local ######
2024-07-01 09:49:57 +00:00
print("Run rc.local to set password, timezone and cp battery folder!")
print(await run_rclocal(remote_host))
2024-07-02 11:27:06 +00:00
##### 6. start battery service ######
print("Start battery service!")
print(await start_battery_service(remote_host))
2024-07-01 09:49:57 +00:00
else:
sys.exit("It's not Venus GX!")
if __name__ == "__main__":
remote_host = sys.argv[1]
asyncio.run(main(remote_host))