using System; using System.Collections.Generic; using System.Threading; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.System.Threading; using Windows.UI; using Windows.UI.Core; 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 WinRTXamlToolkit.Controls.DataVisualization.Charting; // Add using statements to the GrovePi libraries using GrovePi; using GrovePi.Sensors; using GrovePi.I2CDevices; using GrovePi.Common; using System.Threading.Tasks; using System.Net; using Windows.System; //“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍 namespace TanGrove { /// /// 可用于自身或导航至 Frame 内部的空白页。 /// public class DataPoint { public int Time { get; set; } public double Value { get; set; } } public sealed partial class MainPage : Page { //List LightList; List TemperatureList; List HumidityList; List TemperatureListRoom; List HumidityListRoom; int RelayOnOff = 0; int LastRelayOnOff = 1000; int LightValue; double VacVoltage; double VacReading; double LastTemp; double LastHumi; double LastTempRoom; double LastHumiRoom; static int DataPointCnt = 10; IRelay GroveRelay; IDHTTemperatureAndHumiditySensor GroveTempHumi; IDHTTemperatureAndHumiditySensor GroveTempHumiRoom; ILightSensor GroveLight; IRotaryAngleSensor VacSensor; //treat the vac sensor like a rotary knob IButtonSensor GroveButton; IRgbLcdDisplay GroveLCD; public MainPage() { this.InitializeComponent(); this.Loaded += MainPage_Loaded; } void MainPage_Loaded(object sender, RoutedEventArgs e) { InitGrovePi(); InitDataList(); InitWorker(); UpdataUISlow(); UpdateUIFast(); } private void InitGrovePi() { System.Diagnostics.Debug.WriteLine(DeviceFactory.Build.GrovePi().GetFirmwareVersion()); GroveLight = DeviceFactory.Build.LightSensor(Pin.AnalogPin2); VacSensor = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin1); GroveRelay = DeviceFactory.Build.Relay(Pin.DigitalPin2); GroveTempHumi = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin3, DHTModel.Dht11); GroveTempHumiRoom = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin4, DHTModel.Dht22); GroveButton = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin7); GroveLCD = DeviceFactory.Build.RgbLcdDisplay(); //GroveLCD.SetBacklightRgb(255, 50, 255); GroveLCD.SetBacklightRgb(255, 0, 0); //GroveLCD.SetBacklightRgb(0, 255, 255); DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin2, PinMode.Output); Delay.Milliseconds(10); DeviceFactory.Build.GrovePi().Flush(); DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin6, PinMode.Output); Delay.Milliseconds(10); DeviceFactory.Build.GrovePi().Flush(); } private void InitDataList() { //LightList = new List(); //for (int i = 0; i < DataPointCnt; i++) //{ // LightList.Add(new DataPoint() { Time = i + 1, Value = 0 }); //} TemperatureList = new List(); for (int i = 0; i < DataPointCnt; i++) { TemperatureList.Add(new DataPoint() { Time = i + 1, Value = 0 }); } HumidityList = new List(); for (int i = 0; i < DataPointCnt; i++) { HumidityList.Add(new DataPoint() { Time = i + 1, Value = 0 }); } TemperatureListRoom = new List(); for (int i = 0; i < DataPointCnt; i++) { TemperatureListRoom.Add(new DataPoint() { Time = i + 1, Value = 0 }); } HumidityListRoom = new List(); for (int i = 0; i < DataPointCnt; i++) { HumidityListRoom.Add(new DataPoint() { Time = i + 1, Value = 0 }); } } private void InitWorker() { IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync( async (workItem) => { int work_index = 0; int light = 0; SensorStatus button = SensorStatus.Off; while (true) { if (work_index < 5) { button = GroveButton.CurrentState; if (button == SensorStatus.On) { RelayOnOff = 1; GroveRelay.ChangeState(SensorStatus.On); Delay.Milliseconds(100); } else { RelayOnOff = 0; GroveRelay.ChangeState(SensorStatus.Off); Delay.Milliseconds(500); } work_index++; } else { // Read temp & humidity GroveTempHumi.Measure(); GroveTempHumiRoom.Measure(); LastTemp = GroveTempHumi.TemperatureInFahrenheit; LastHumi = GroveTempHumi.Humidity; LastTempRoom = GroveTempHumiRoom.TemperatureInFahrenheit; LastHumiRoom = GroveTempHumiRoom.Humidity; light = GroveLight.SensorValue(); LightValue = light; VacReading = VacSensor.SensorValue(); //if i could get the voltage, i can calc the vacuum VacVoltage = VacSensor.Voltage(); //ShiftLeft(LightList, light); ShiftLeft(TemperatureList, LastTemp); ShiftLeft(HumidityList, LastHumi); ShiftLeft(TemperatureListRoom, LastTempRoom); ShiftLeft(HumidityListRoom, LastHumiRoom); work_index = 0; } await Dispatcher.RunAsync( CoreDispatcherPriority.High, () => { if (work_index == 0) { UpdataUISlow(); } else { UpdateUIFast(); } }); //Delay.Milliseconds(250); } } ); } private static void ShiftLeft(List list, double newValue) { for (int i = 0; i < DataPointCnt - 1; i++) { list[i].Value = list[i + 1].Value; } list[DataPointCnt - 1].Value = newValue; } private void UpdataUISlow() { List lst = new List(TemperatureList); (LineChart0.Series[0] as LineSeries).ItemsSource = lst; lst = new List(HumidityList); (LineChart0.Series[1] as LineSeries).ItemsSource = lst; lst = new List(TemperatureListRoom); (LineChart1.Series[0] as LineSeries).ItemsSource = lst; lst = new List(HumidityListRoom); (LineChart1.Series[1] as LineSeries).ItemsSource = lst; //lst = new List(LightList); //(LineChart1.Series[2] as LineSeries).ItemsSource = lst; GroveLCD.SetText("T1:" + LastTemp.ToString("F2") + " H1:" + LastHumi.ToString("F1") + "\nT2:" + LastTempRoom.ToString("F2") + " H2:" + LastHumiRoom.ToString("F1")); } private void UpdateUIFast() { PieSlice.Radius = 50; PieSlice.StartAngle = -90; //ex. if lux is 500, then 500/1000 is 0.5, that times 180, give 90...90 degress from -90 would be straightup, so 0 double angle = -90 + (LightValue * 180 / 1000); if (angle > 179) angle = 90; PieSlice.EndAngle = angle; Textblock2.Text = Convert.ToString(LightValue); //display the vac data... Textblock3.Text = Convert.ToString(VacVoltage); Textblock4.Text = Convert.ToString(VacReading); if (LastRelayOnOff != RelayOnOff) { if (RelayOnOff > 0) { Rect0.Fill = new SolidColorBrush(Colors.Green); Textblock0.Text = "On"; } else { Rect0.Fill = new SolidColorBrush(Colors.Red); Textblock0.Text = "Off"; } LastRelayOnOff = RelayOnOff; } } private void button_Click(object sender, RoutedEventArgs e) { //add code to shutdown the app and device //App.Current.Exit(); ShutdownComputer(); //ShutdownManager.BeginShutdown(ShutdownKind.Shutdown, TimeSpan.Zero); } private async void ShutdownComputer() { String URL = "http://localhost:8080/api/control/shutdown"; System.Diagnostics.Debug.WriteLine(URL); StreamReader SR = await PostJsonStreamData(URL); } private async Task PostJsonStreamData(String URL) { System.Net.HttpWebRequest wrGETURL = null; Stream objStream = null; StreamReader objReader = null; try { wrGETURL = (HttpWebRequest)WebRequest.Create(URL); wrGETURL.Method = "POST"; wrGETURL.Credentials = new NetworkCredential("Administrator", "p@ssw0rd"); HttpWebResponse Response = (HttpWebResponse)(await wrGETURL.GetResponseAsync()); if (Response.StatusCode == HttpStatusCode.OK) { objStream = Response.GetResponseStream(); objReader = new StreamReader(objStream); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("GetData " + e.Message); } return objReader; } } }