"""
升级 & 备份控制器示例
"""

import platform
from datetime import datetime
# 根据操作系统导入相应的模块
if platform.system() == 'Windows':
    from Release.windows import xCoreSDK_python
    from Release.windows import xCoreSDK_Upgrade_py
elif platform.system() == 'Linux':
    from Release.linux import xCoreSDK_python
    from Release.linux import xCoreSDK_Upgrade_py
else:
    raise ImportError("Unsupported operating system")


def export_example(upgrader):

    encrtpted = True
    ec = {}
    print("Start exporting controller backup")
    # 导出到当前目录下, 导出控制器日志
    filename = f"export_{datetime.now():%Y-%m-%d_%H-%M-%S}.{ 'rpa' if encrtpted else 'zip' }"
    upgrader.exportBackup(filename, {xCoreSDK_Upgrade_py.BackupItem.controllerLog}, ec, encrtpted)
    if ec['ec'] == 0:
        print("Backup exported successfully")
    else:
        print(f"Backup export failed {ec['message']}")


def upgrade_example(upgrader):
    ec = {}
    print("Start upgrade controller")
    # 升级控制器版本/备份
    upgrader.upgrade(R'C:\Users\rokae\v3.1.2.rpa', ec)
    if ec['ec'] == 0:
        print("Upgrade completed successfully")
    else:
        print(f"Upgrade failed {ec['message']}")


if __name__ == "__main__":
    try:
        remote_ip = "192.168.0.160"
        # 创建升级对象
        upgrader = xCoreSDK_Upgrade_py.BaseUpgrade(remote_ip)

        # 导出控制器备份
        export_example(upgrader)

        # 升级控制器
        # upgrade_example(upgrader)

    except Exception as e:
        print(f"Connection error: {e}")
