import os
import shutil
from selenium import webdriver
import time
import threading
import random

frameIndex = 0
iswork = 0
threadNum = 0

# def road_situation():
#     try:
#         # 每隔90秒截屏一次
#         t = threading.Timer(1, road_situation)
#         t.start()

#         # 初始化一个谷歌浏览器实例
#         driver = webdriver.Chrome()
#         driver.maximize_window()

#         picture_url = "http://localdata/test.html"
#         driver.get(picture_url)
#         # driver.refresh()

#         driver.get_screenshot_as_file(r"pic\%s.png" %time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())))

#         print(os.getcwd())
#         driver.quit()
#     except BaseException as msg:
#         print("[err] %s" % msg)

def save_screenshot():
    global frameIndex, iswork, threadNum

    try:
        if iswork == 0:
            return
        threadNum += 1 

        t = threading.Timer(0.02, save_screenshot)
        t.start()

        # rowNum = int(driver.find_element_by_id("rowNum").get_attribute('value'))
        # rsCount = int(driver.find_element_by_id("rsCount").get_attribute('value'))
        endFlag = int(driver.find_element_by_id("endFlag").get_attribute('value'))
        print("[%s] %d" % (time.strftime('%H:%M:%S', time.localtime(time.time())), frameIndex))
        # print("[%s][%d] %d / %d" % (time.strftime('%H:%M:%S', time.localtime(time.time())), frameIndex, rowNum, rsCount))

        # if rowNum <= rsCount:
        if endFlag == 0:
            # t = threading.Timer(0.04, save_screenshot)
            # t.start()

            # if rowNum > 0:
                frameIndex += 1
                # file = r"pic\%s.png" % time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
                # file = r"pic\%s.png" % rowNum
                file = r"pic\%s.png" % (frameIndex)
                # file = r"pic\%s_%d.png" % (frameIndex, rowNum)
                driver.get_screenshot_as_file(file)
                # png_data = driver.get_screenshot_as_png()  # 获取二进制数据流
                # print(os.getcwd())
                print(file)

            # if rowNum == rsCount:
            #     print("save_screenshot finished !")
            #     driver.quit()
            
                threadNum -= 1
        else:
            print("save_screenshot finished !!")
            threadNum -= 1
            iswork = 0
            if iswork == 0 and threadNum <= 0:
                driver.close()
                driver.quit()
    except BaseException as msg:
        print("[err] %s" % msg)
        threadNum -= 1
        iswork = 0
        if iswork == 0 and threadNum <= 0:
            driver.close()
            driver.quit()

try:
    driver = webdriver.Chrome(r"chromedriver.exe")
    picture_url = "http://localdata/test.html"

    driver.maximize_window()
    driver.get(picture_url)
    # driver.refresh()

    # print(dir(driver))

    # time.sleep(1)

    iswork = 1
    save_screenshot()

    # driver.get_screenshot_as_file('1.png')
    # print("%s: Screenshot save success!!" % picture_url)
    # driver.close()
    # driver.quit()
except BaseException as msg:
    print("[err] %s" % msg)
    driver.close()
    driver.quit()
