package net.northking.cctp.se.device.bean;

import io.appium.java_client.AppiumDriver;
import net.northking.cctp.element.core.DeviceDriver;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MobileDriverKeepThread extends Thread{

    private DeviceDriver driver;

    private Logger logger = LoggerFactory.getLogger(MobileDriverKeepThread.class);

    private int waitTime;

    private String deviceId;

    public MobileDriverKeepThread(DeviceDriver driver, int waitTime,String deviceId) {
        this.driver = driver;
        this.waitTime = waitTime;
        this.deviceId = deviceId;
    }

    @Override
    public void run() {
        while (!isInterrupted()) {
            if (driver instanceof MobileDeviceDriver) {
                MobileDeviceDriver deviceDriver = (MobileDeviceDriver) driver;
                deviceDriver.send("hello");
            } else {
                AppiumDriver<WebElement> deviceDriver = (AppiumDriver<WebElement>) driver;
                deviceDriver.getOrientation();
            }
            logger.info("设备【{}】的driver存活。。。。。。。。。。。。",deviceId);
            try {
                Thread.sleep(waitTime * 1000);
            } catch (InterruptedException e) {
                break;
            }
        }
        logger.info("设备【{}】driver守护线程已终止.......................................",this.deviceId);
    }

}