
#Copyright © 2024 – WildGate Consultancy Solutions Limited – all Rights Reserved
#Proof of concept to constantly capture image from mapserver
#
#9 Oct 24 - based on StreamCapturedMap9Feb24.py
#
#  to open node red diashbiard via Garrision
#  capture and decode QR Code
#  save to .json file
#
#
#
#
#
#  if needed to view run in strealit with
#
#   streamlit run /var/www/wildgate.org/public_html/CaptureQRviaGarr-Oct24.py 
#



from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

from selenium.webdriver.chrome.service import Service as ChromeService  # added 25/1/24
from webdriver_manager.chrome import ChromeDriverManager # added 25/1/24

from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder

import streamlit as st # added 30 Jan 24

import time
from time import sleep
import cv2

#url = "http://134.209.236.202:1880/ui"
#url1 = "http://134.209.236.202:1880/Secondmap"
#url = "https://live.garrison-ultra.com/login/"
url = "https://002158.garrison-ultra.com/"
target = "http:nodered.securemapview.com/ui"



options = Options()
#options.headless = True  # remed out 25/1/24

options.add_argument("--headless")  # added 25/1/24
options.add_argument("window-size=1920,1080") # remed out 25/1/24
options.add_argument('--no-sandbox')  # added 25/1/24
options.add_argument('--disable-dev-shm-usage')  # added 25/1/24
options.add_experimental_option("useAutomationExtension", False) #added 6 Feb 24 - removes "Chrome is being controlled ..."
options.add_experimental_option("excludeSwitches",["enable-automation"])  #added 6 Feb 24 - removes "Chrome is being controlled ..."

#driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)  # added 25/1/24
driver = webdriver.Chrome(options=options) # chnaged from above  9 )ct 24
#open the webpage first , do this only once to keep open, and delay 12 secs to alow full page
#to form 
print('got to here')
driver.get(url)
print('and got to here 1')
#driver1.get(url1)
sleep(4)

#From main window select logon windon , entery key and submit
print('got to here 2')
curwin = driver.current_window_handle
print (curwin)
list = driver.window_handles
print (list)

#driver.switch_to.default_content()

driver.switch_to.window(list[0])
#driver.switch_to.window(curwin)
driver.maximize_window()  # remed 8/2/24
#driver.set_window_size(1920, 1080) # added 8 Feb 24

#logon = driver.find_element(By.CLASS_NAME, "field._1c4e2w1")

#print(logon)
print('got to here 3')

# for testing only
#attrs=[]
#for attr in logon.get_property('attributes'):
    #attrs.append([attr['name'], attr['value']]) # original
#    attrs.append([attr['name']])
#print(attrs)
###       *****************************************************           ###

try:
    cont = driver.find_element(By.XPATH, '//html/body/div/div/form/div[2]/button[normalize-space()="Continue with an access key"]').click()    
    print(cont)
    print('clicked to use access key')
        
except:
    print('continue button field not found')

#add logon detail
sleep(2) # wait for button   
#logon = driver.find_element(By.XPATH, '//html/body/div/div/form/div[1]/div[2]/input').click() # this works
logon = driver.find_element(By.XPATH, '//html/body/div/div/form/div[1]/div[2]/input').send_keys("kzVgqNQkQvuHYaup303POAvW6nxcK0s_ho78QG91PMzBVEymROxF75H0EPekvC_8")
sleep(1)
signin = driver.find_element(By.XPATH, '//html/body/div/div/form//div[2]/button[1]').click()
# kzVgqNQkQvuHYaup303POAvW6nxcK0s_ho78QG91PMzBVEymROxF75H0EPekvC_8 # updated Jul 24

#switch back to main frame

sleep(3)

list = driver.window_handles
print (list)

#driver.switch_to.default_content()
driver.switch_to.window(list[0])  # this selects top window OK
driver.maximize_window()
print('waiting for google screen')
sleep(3)

###       *****************************************************           ###

driver.save_screenshot('test1.png')

 #driver.switch_to.default_content() ###     move back up             ###

###  main loop ##
#then continually capture image
#st.set_page_config(layout="wide")
#placeholder = st.empty()  # added 30 Jan 24

while(True):
    driver.save_screenshot('QR.png')
    #frame = cv2.imread('map1.png')
    print('saving.......')
    # stream if needed
    #with placeholder.container():
        #st.image(frame, caption='Test Highside Map server')
        #sleep(0.5)
    sleep(2)




        
