Created
December 11, 2019 14:37
-
-
Save cwschroeder/bfad1b560878f0c5e70c065c019a7762 to your computer and use it in GitHub Desktop.
Selenium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
namespace PissUi.Com | |
{ | |
public class Allnet | |
{ | |
private static Dictionary<string, IWebDriver> webDrivers = new Dictionary<string, IWebDriver> | |
{ | |
{ "http://192.168.40.231/", null }, | |
{ "http://192.168.40.232/", null }, | |
{ "http://192.168.40.233/", null }, | |
{ "http://192.168.40.234/", null } | |
}; | |
public static List<Station> Stations = new List<Station>(); | |
public static Station WarnStation => Stations.First(s => s.Type == StationType.Warn); | |
static Allnet() | |
{ | |
var urls = webDrivers.Keys.ToArray(); | |
Stations.Add(new Station() | |
{ | |
Index = 0, | |
No = 1, | |
Url = urls[0], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 1, | |
No = 2, | |
Url = urls[0], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 2, | |
No = 3, | |
Url = urls[0], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 0, | |
No = 4, | |
Url = urls[1], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 1, | |
No = 5, | |
Url = urls[1], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 2, | |
No = 6, | |
Url = urls[1], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 0, | |
No = 7, | |
Url = urls[2], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 1, | |
No = 8, | |
Url = urls[2], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 2, | |
No = 9, | |
Url = urls[2], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 0, | |
No = 10, | |
Url = urls[3], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 3, | |
No = 11, | |
Url = urls[0], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 4, | |
No = 12, | |
Url = urls[0], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 5, | |
No = 13, | |
Url = urls[0], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 3, | |
No = 14, | |
Url = urls[1], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 4, | |
No = 15, | |
Url = urls[1], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 5, | |
No = 16, | |
Url = urls[1], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 3, | |
No = 17, | |
Url = urls[2], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 4, | |
No = 18, | |
Url = urls[2], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 5, | |
No = 19, | |
Url = urls[2], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 1, | |
No = 20, | |
Url = urls[3], | |
Type = StationType.Plug | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 3, | |
No = 0, | |
Url = urls[3], | |
Type = StationType.Tariff | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 4, | |
No = 0, | |
Url = urls[3], | |
Type = StationType.Aux | |
}); | |
Stations.Add(new Station() | |
{ | |
Index = 5, | |
No = 0, | |
Url = urls[3], | |
Type = StationType.Warn | |
}); | |
} | |
public static async Task SwitchPattern(string pattern, bool switchOn, CancellationToken token) | |
{ | |
if (switchOn && !WarnStation.IsOn) | |
{ | |
// activate warn light | |
MainWindow.Log("Turning on warning light..."); | |
await SwitchPlugs(new[] { WarnStation }, true, token); | |
} | |
var commands = pattern.Split(new[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries); | |
var stationsToSwitch = new List<Station>(); | |
foreach (var command in commands) | |
{ | |
var ranges = command.Trim().Split('-'); | |
if (ranges.Length != 2 && ranges.Length != 1) | |
continue; | |
if (!int.TryParse(ranges[0], out var start)) | |
continue; | |
var end = start; | |
if (ranges.Length == 2) | |
{ | |
if (!int.TryParse(ranges[1], out end)) | |
continue; | |
} | |
if (end < start || end > 20 || start < 1) | |
continue; | |
MainWindow.Log($"Switch pattern [{start}]-[{end}]"); | |
stationsToSwitch.AddRange(Stations.Where(s => s.No >= start && s.No <= end)); | |
} | |
await SwitchPlugs(stationsToSwitch, switchOn, token); | |
if (!switchOn) | |
{ | |
// when turn off turn off also always turn off AUX and TARIFF too | |
await SwitchAux(false, token); | |
await SwitchTariff(false, token); | |
} | |
if (!Stations.Where(s => s.Type != StationType.Warn).Any(s => s.IsOn)) | |
{ | |
// deactivate warn light | |
MainWindow.Log("Turning off warning light..."); | |
await SwitchPlugs(new[] { WarnStation }, false, token); | |
} | |
} | |
public static async Task SwitchPlugs(IEnumerable<Station> stationsToSwitch, bool switchOn, CancellationToken token) | |
{ | |
var urlsToSwitch = stationsToSwitch.ToLookup(s => s.Url, s => s); | |
var tasks = urlsToSwitch.Select(async grouping => | |
{ | |
var url = grouping.Key; | |
var driver = await InitWebDriver(url); | |
var stations = grouping.ToList(); | |
try | |
{ | |
while (!token.IsCancellationRequested) | |
{ | |
var switchButtons = driver.FindElements(By.ClassName("power_button_border")); | |
if (switchButtons.Count < 6) | |
{ | |
await Task.Delay(10); | |
continue; | |
} | |
if (switchButtons.Any(b => b.Displayed == false)) | |
{ | |
await Task.Delay(10); | |
continue; | |
} | |
foreach (var station in stations) | |
{ | |
MainWindow.Log($"Switching station {url} {station.Index} [{station.Type}] to state {switchOn}..."); | |
var pb_one_div = driver.FindElement(By.Id($"power_color_{station.Index + 1}")); | |
var style = pb_one_div.GetAttribute("style"); | |
if (style == "background-color: rgb(64, 64, 64);") | |
{ | |
// Switch is OFF | |
if (switchOn) | |
{ | |
switchButtons[station.Index].Click(); | |
station.IsOn = true; | |
} | |
} | |
else | |
{ | |
// Switch is ON | |
if (!switchOn) | |
{ | |
switchButtons[station.Index].Click(); | |
station.IsOn = false; | |
} | |
} | |
// read back | |
//style = pb_one_div.GetAttribute("style"); | |
//station.IsOn = style == "background-color: rgb(64, 64, 64);"; | |
MainWindow.Log($"Station {url} {station.Index} [{station.Type}] new state: {station.IsOn}"); | |
} | |
break; | |
} | |
} | |
catch (Exception ex) | |
{ | |
MainWindow.Log(ex.Message); | |
} | |
}); | |
try | |
{ | |
await Task.WhenAll(tasks); | |
} | |
catch (Exception ex) | |
{ | |
MainWindow.Log(ex.Message); | |
} | |
} | |
private static async Task ReadPlugStates(string url, CancellationToken token) | |
{ | |
IWebDriver driver = await InitWebDriver(url); | |
var urlStations = Stations.Where(s => s.Url == url).ToList(); | |
while (!token.IsCancellationRequested) | |
{ | |
var switchButtons = driver.FindElements(By.ClassName("power_button_border")); | |
if (switchButtons.Count < 6) | |
{ | |
await Task.Delay(10); | |
continue; | |
} | |
if (switchButtons.Any(b => b.Displayed == false)) | |
{ | |
await Task.Delay(10); | |
continue; | |
} | |
for (int i = 1; i <= 6; i++) | |
{ | |
var pb_one_div = driver.FindElement(By.Id($"power_color_{i}")); | |
var style = pb_one_div.GetAttribute("style"); | |
if (style == "background-color: rgb(64, 64, 64);") | |
{ | |
// Switch is OFF | |
var station = urlStations.First(s => s.Index == i - 1); | |
station.IsOn = false; | |
} | |
else | |
{ | |
// Switch is ON | |
var station = urlStations.First(s => s.Index == i - 1); | |
station.IsOn = true; | |
} | |
} | |
//driver.Quit(); | |
break; | |
} | |
} | |
public static async Task ReadAllPlugStates(CancellationToken token) | |
{ | |
var urlsToSwitch = Stations.ToLookup(s => s.Url, s => s); | |
var tasks = urlsToSwitch.Select(async grouping => | |
{ | |
var url = grouping.Key; | |
var driver = await InitWebDriver(url); | |
var stations = grouping.ToList(); | |
try | |
{ | |
var urlStations = Stations.Where(s => s.Url == url).ToList(); | |
while (!token.IsCancellationRequested) | |
{ | |
var switchButtons = driver.FindElements(By.ClassName("power_button_border")); | |
if (switchButtons.Count < 6) | |
{ | |
await Task.Delay(10); | |
continue; | |
} | |
if (switchButtons.Any(b => b.Displayed == false)) | |
{ | |
await Task.Delay(10); | |
continue; | |
} | |
foreach (var station in stations) | |
{ | |
var pb_one_div = driver.FindElement(By.Id($"power_color_{station.Index + 1}")); | |
var style = pb_one_div.GetAttribute("style"); | |
if (style == "background-color: rgb(64, 64, 64);") | |
{ | |
// Switch is OFF | |
station.IsOn = false; | |
} | |
else | |
{ | |
// Switch is ON | |
station.IsOn = true; | |
} | |
MainWindow.Log($"Station {url} {station.Index} [{station.Type}] current state {station.IsOn}"); | |
} | |
//driver.Quit(); | |
break; | |
} | |
} | |
catch (Exception ex) | |
{ | |
MainWindow.Log(ex.Message); | |
} | |
}); | |
await Task.WhenAll(tasks); | |
} | |
private static async Task<IWebDriver> InitWebDriver(string url) | |
{ | |
var driver = webDrivers[url]; | |
if (driver != null) | |
{ | |
MainWindow.Log("Reusing existing driver..."); | |
return driver; | |
} | |
ChromeOptions options = new ChromeOptions(); | |
options.AddArgument("headless"); | |
var service = ChromeDriverService.CreateDefaultService(); | |
service.HideCommandPromptWindow = true; | |
//var appPath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); | |
MainWindow.Log($"Starting a new webdriver for target {url} ..."); | |
await Task.Run(() => | |
{ | |
try | |
{ | |
driver = new ChromeDriver(service, options); | |
driver.Navigate().GoToUrl(url); | |
var menueElement = driver.FindElement(By.Id("topMain")); | |
menueElement.Click(); | |
var sensorpanelElement = driver.FindElement(By.Id("sensorpanel")); | |
sensorpanelElement.Click(); | |
} | |
catch (Exception ex) | |
{ | |
driver = null; | |
MainWindow.Log(ex.Message); | |
} | |
}); | |
webDrivers[url] = driver; | |
return driver; | |
} | |
public static async Task<bool> SwitchTariff(bool isOn, CancellationToken token) | |
{ | |
var stationTariffs = | |
Stations.Where(s => s.Type == StationType.Tariff).ToList(); | |
try | |
{ | |
MainWindow.Log($"Switching tariffs..."); | |
if (isOn && !WarnStation.IsOn) | |
{ | |
// activate warn light | |
MainWindow.Log("Turning on warning light..."); | |
await SwitchPlugs(new[] { WarnStation }, true, token); | |
} | |
await SwitchPlugs(stationTariffs, isOn, token); | |
if (!Stations.Where(s => s.Type != StationType.Warn).Any(s => s.IsOn)) | |
{ | |
// deactivate warn light | |
MainWindow.Log("Turning off warning light..."); | |
await SwitchPlugs(new[] { WarnStation }, false, token); | |
} | |
return true; | |
} | |
catch (Exception e) | |
{ | |
MainWindow.Log(e.Message); | |
return false; | |
} | |
} | |
public static async Task<bool> SwitchAux(bool isOn, CancellationToken token) | |
{ | |
var stationTariffs = | |
Stations.Where(s => s.Type == StationType.Aux).ToList(); | |
try | |
{ | |
MainWindow.Log($"Switching aux..."); | |
if (isOn && !WarnStation.IsOn) | |
{ | |
// activate warn light | |
MainWindow.Log("Turning on warning light..."); | |
await SwitchPlugs(new[] { WarnStation }, true, token); | |
} | |
await SwitchPlugs(stationTariffs, isOn, token); | |
if (!Stations.Where(s => s.Type != StationType.Warn).Any(s => s.IsOn)) | |
{ | |
// deactivate warn light | |
MainWindow.Log("Turning off warning light..."); | |
await SwitchPlugs(new[] { WarnStation }, false, token); | |
} | |
return true; | |
} | |
catch (Exception e) | |
{ | |
MainWindow.Log(e.Message); | |
return false; | |
} | |
} | |
public static void KillChromeProcesses() | |
{ | |
var processes = Process.GetProcesses(); | |
foreach (var p in processes) | |
{ | |
if (p.ProcessName.Contains("chrome")) | |
{ | |
try | |
{ | |
p.Kill(); | |
} | |
catch (Exception ex) | |
{ | |
MainWindow.Log(ex.Message); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment