using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.Devices.Enumeration; using Windows.Devices.I2c; using System.Net; using System.Threading.Tasks; using Windows.System; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace OpenHabBrowser { /// /// An empty page that can be used on its own or navigated to within a Frame. /// public sealed partial class MainPage : Page { //public static string openPanel_url = "http://phoenix:8080/habpanel/index.html#/view/default"; public static string openPanel_url = "http://192.168.2.10:8080/habpanel/index.html#/view/default"; public static Boolean test_mode = false; public MainPage() { this.InitializeComponent(); Logger.add("Started"); try { webView.Settings.IsJavaScriptEnabled = true; webView.ScriptNotify += WebView_ScriptNotify; webView.Navigate(new Uri(openPanel_url)); Logger.add("Navigate to: " + openPanel_url); init_Brighness_timer(); } catch (Exception ex) { Logger.add("Navigate to FAILED: " + ex.Message); } } private void WebView_ScriptNotify(object sender, NotifyEventArgs e) { Logger.add("ScriptNotify: " + e.Value); } private DispatcherTimer timer_Update; private void init_Brighness_timer() { try { Settings_Sub.Brightness.init(); } catch (Exception ex) { Logger.add("Init Timer FAILED: " + ex.Message); } try { timer_Update = new DispatcherTimer(); timer_Update.Interval = TimeSpan.FromMilliseconds(60000); timer_Update.Tick += Timer_Update_Tick; timer_Update.Start(); } catch (Exception ex) { Logger.add("Init Timer FAILED: " + ex.Message); } } private Boolean timerTaskRunning = false; private void Timer_Update_Tick(object sender, object e) { if (!timerTaskRunning) { timerTaskRunning = true; try { getData(); } catch (Exception ex) { Logger.add("Timer Tick FAILED: " + ex.Message); } timerTaskRunning = false; } } private void getData() { try { Uri uri = new Uri("http://phoenix/GetData.aspx?NightScreen=GET"); TimeSpan timeout = new TimeSpan(0, 0, 1); webRequest(uri).Wait(timeout); } catch (Exception ex) { Logger.add("ERROR getData: " + ex.Message); } } private static string responseContent = string.Empty; private static int tryCounter = 0; private async Task webRequest(Uri uri) { responseContent = "-99"; try { HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest; WebResponse response = await request.GetResponseAsync(); using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); if (!test_mode)Settings_Sub.Brightness.set(byte.Parse(responseContent)); else { Random rnd = new Random(); Byte[] b = new Byte[10]; rnd.NextBytes(b); Settings_Sub.Brightness.set(b[0]); tryCounter = 0; } } } catch (Exception ex) { Logger.add("ERROR: Get Data from Server: " + ex.Message + " -> " + uri.OriginalString); tryCounter++; if (tryCounter >= 10) { SystemTasks.Reboot(); } } } } }