#!/usr/bin/python3
#
# requirment phantomjs,ntpdate,requests

from toolInitializer import _thread
from fwutech_enum import ToolInitializer
from tools.executor import FwutechEnum
from tools.utils import Executor
from tools.webspeed import Utils
from tools.checkConfig import CheckConfigAndToolsutils

utilsObject = Utils()
conf = utilsObject.readConf()
checkConfigAndTools = CheckConfigAndTools(conf)

# Execute scenario periodically
def runScenario():
    # Mount /home to readonly
    utilsObject.runCmd(FwutechEnum.MOUNT_READONLY_CMD.value)
    MID = utilsObject.getMID()
    print(MID)
    
    primaryTasks = conf["scenarioModelModels"][0]["taskModels"]
    tasks = utilsObject.sortTasks(
        utilsObject.integrateTasks(
            primaryTasks, utilsObject.integratePingTask(utilsObject.get_pingTask(primaryTasks))
        )
    )
    
    toolInitializer = ToolInitializer()
    
    while True:
        try:
            checkConfigAndTools.syncScenarioFromServer()
        except:
            pass
        
        try:
            checkConfigAndTools.checkUpgradeTools()
        except:
            pass

        # Sync date
        try:
            date_command = FwutechEnum.DATE_CMD.value % (
                os.path.dirname(os.path.abspath(__file__)).replace("\'", "")
            )
            shortName = "None"
            date_url = FwutechEnum.DATE_URL.value % (
                conf["serverIp"],
                str(conf["serverInfoPort"]),
                str(MID),
                utilsObject.getMidParent(),
                utilsObject.getInfo()["model"],
                utilsObject.getInfo()["type"]
            )
            print(date_url)
            print(utilsObject.runCmd(date_command + " " + date_url))
        except:
            print(FwutechEnum.DATE_FAILD_MSG.value)

        results = []
        for task in tasks:
            try:
                buildedObject = toolInitializer.build(
                    "tools." + task["toolName"], task["toolName"].capitalize(), task
                )
                taskResult = buildedObject.convetColonSytx(
                    buildedObject.executeTask()
                )
                print(taskResult)
                temp_rs = json.loads(taskResult)
                
                if isinstance(temp_rs, list):
                    results.extend(temp_rs)
                else:
                    results.append(temp_rs)
            except:
                pass

        scenario_result = {
            "mid": MID,
            "scenarioResults": [
                {
                    "scenarioName": conf["scenarioModelModels"][0]["scenarioName"],
                    "resultOfScenarioTaskModels": results
                }
            ]
        }
        
        url = FwutechEnum.SEND_RESULT_URL.value % (
            conf["serverIp"], str(conf["serverInfoPort"])
        )
        
        utilsObject.send(url, utilsObject.prepareResult(scenario_result))
        time.sleep(60 * conf["periodicalRunSenario"])

runScenario()
