wxr
2022-10-27 bbd6b592a6b883e013ff1e0a574976ceba5009ea
HDL_ON/UI/UI2/FuntionControlView/Acst/AcstSubHistoryPage.cs
New file
@@ -0,0 +1,417 @@
using System;
using System.Collections.Generic;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using Shared;
namespace HDL_ON.UI
{
    public class AcstSubHistoryPage :FrameLayout
    {
        AcstSubHistoryPage bodyView;
        FrameLayout showDataTypeView;
        Button btnShowHistroyData_Day;
        Button btnShowHistroyData_Week;
        Button btnShowHistroyData_Month;
        /// <summary>
        /// echart容器
        /// </summary>
        MyEchartsViewOn tempEchartsView;
        /// <summary>
        /// echart绘制数据
        /// </summary>
        EchartsOption_BrokenLine tempEchartOption;
        Button btnTempTipText;
        MyEchartsViewOn humiEchartsView;
        EchartsOption_BrokenLine humiEchartsOption;
        Button btnHumiTipText;
        string curQueryType = "hour";
        string queryTime;
        Function function;
        public AcstSubHistoryPage(Function temp)
        {
            function = temp;
            bodyView = this;
        }
        /// <summary>
        ///
        /// </summary>
        public void LoadPage()
        {
            new TopViewDiv(bodyView, function.GetRoomListName()).LoadTopView();
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
            //var contentView = new FrameLayout()
            //{
            //    Y = Application.GetRealHeight(64),
            //    Height = Application.GetRealHeight(603),
            //};
            //bodyView.AddChidren(contentView);
            #region 选择数据日期范围
            showDataTypeView = new FrameLayout()
            {
                X = Application.GetRealWidth(199),
                Y = Application.GetRealHeight(83),
                Width = Application.GetRealWidth(144),
                Height = Application.GetRealHeight(24),
                BackgroundImagePath = "FunctionIcon/EnvironmentalScience/SensorHistoryBg1.png",
            };
            bodyView.AddChidren(showDataTypeView);
            btnShowHistroyData_Day = new Button()
            {
                Width = Application.GetRealWidth(48),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.PromptingColor2,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                IsSelected = true,
                TextID = StringId.day,
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Day);
            btnShowHistroyData_Week = new Button()
            {
                X = Application.GetRealWidth(48),
                Width = Application.GetRealWidth(48),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.PromptingColor2,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                TextID = StringId.week,
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Week);
            btnShowHistroyData_Month = new Button()
            {
                X = Application.GetRealWidth(48 * 2),
                Width = Application.GetRealWidth(48),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.PromptingColor2,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                Text = Language.StringByID(StringId.month),//DateTime.Now.Month.ToString() +
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Month);
            #endregion
            #region 温度历史数据
            var tempHistoryDataView = new FrameLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(123),
                Height = Application.GetRealHeight(256),
                Width = Application.GetRealWidth(343),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(6),
            };
            bodyView.AddChidren(tempHistoryDataView);
            tempEchartsView = new MyEchartsViewOn()
            {
                Y = Application.GetRealWidth(10),
                Height = Application.GetRealHeight(207),
            };
            tempHistoryDataView.AddChidren(tempEchartsView);
            tempEchartOption = new EchartsOption_BrokenLine();
            btnTempTipText = new Button()
            {
                Y = Application.GetRealHeight(222),
                Height = Application.GetRealHeight(34),
                Text = "最近24小时的平均室内温度",
                TextSize = 10,
                TextColor = 0xFF798394,
                TextAlignment = TextAlignment.Center,
            };
            tempHistoryDataView.AddChidren(btnTempTipText);
            #endregion
            #region 湿度历史数据
            var humiHistoryDataView = new FrameLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = tempHistoryDataView.Bottom + Application.GetRealWidth(10),
                Height = Application.GetRealHeight(256),
                Width = Application.GetRealWidth(343),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(6),
            };
            bodyView.AddChidren(humiHistoryDataView);
            humiEchartsView = new MyEchartsViewOn()
            {
                Y = Application.GetRealWidth(10),
                Height = Application.GetRealHeight(207),
            };
            humiHistoryDataView.AddChidren(humiEchartsView);
            humiEchartsOption = new EchartsOption_BrokenLine();
            btnHumiTipText = new Button()
            {
                Y = Application.GetRealHeight(222),
                Height = Application.GetRealHeight(34),
                Text = "最近24小时的平均室内湿度",
                TextSize = 10,
                TextColor = 0xFF798394,
                TextAlignment = TextAlignment.Center,
            };
            humiHistoryDataView.AddChidren(btnHumiTipText);
            #endregion
#if DEBUG
            //List<string> vs = new List<string>() {
            //            "1","2","3","4","5","6","7","8","9","10","11","12","13","14"
            //        };
            //tempEchartOption.InitXdataText(vs);
            //tempEchartOption.InitYdataText("yData", new List<EnvironmentalSensorHistor>() {
            //    new EnvironmentalSensorHistor{ fieldName = "1", fieldValue= "23"  },
            //    new EnvironmentalSensorHistor{ fieldName = "2", fieldValue= "22"  },
            //    new EnvironmentalSensorHistor{ fieldName = "3", fieldValue= "-11"  },
            //    new EnvironmentalSensorHistor{ fieldName = "4", fieldValue= "21"  },
            //    new EnvironmentalSensorHistor{ fieldName = "5", fieldValue= "-41"  },
            //    new EnvironmentalSensorHistor{ fieldName = "6", fieldValue= "23"  },
            //    new EnvironmentalSensorHistor{ fieldName = "7", fieldValue= "-31"  },
            //    new EnvironmentalSensorHistor{ fieldName = "8", fieldValue= "-23"  },
            //    new EnvironmentalSensorHistor{ fieldName = "9", fieldValue= "-11"  },
            //    new EnvironmentalSensorHistor{ fieldName = "10", fieldValue= "-21"  },
            //    new EnvironmentalSensorHistor{ fieldName = "11", fieldValue= "24"  },
            //    new EnvironmentalSensorHistor{ fieldName = "12", fieldValue= "41"  },
            //    new EnvironmentalSensorHistor{ fieldName = "13", fieldValue= "32"  },
            //    new EnvironmentalSensorHistor{ fieldName = "14", fieldValue= "23"  }
            //}, "#FF9D54");
            //tempEchartOption.yTitle = "温度 (°)";
            //tempEchartOption.xTitle = "";
            //var opString = tempEchartOption.InitOption();
            //tempEchartsView.ShowWithOption(opString);
            //List<string> vs2 = new List<string>() {
            //            "1","2","3","4","5","6","7","8","9","10","11","12","13","14"
            //        };
            //humiEchartsOption.InitXdataText(vs2);
            //humiEchartsOption.InitYdataText("yData", new List<EnvironmentalSensorHistor>() {
            //    new EnvironmentalSensorHistor{ fieldName = "1", fieldValue= "23"  },
            //    new EnvironmentalSensorHistor{ fieldName = "2", fieldValue= "22"  },
            //    new EnvironmentalSensorHistor{ fieldName = "3", fieldValue= "-11"  },
            //    new EnvironmentalSensorHistor{ fieldName = "4", fieldValue= "21"  },
            //    new EnvironmentalSensorHistor{ fieldName = "5", fieldValue= "-41"  },
            //    new EnvironmentalSensorHistor{ fieldName = "6", fieldValue= "23"  },
            //    new EnvironmentalSensorHistor{ fieldName = "7", fieldValue= "-31"  },
            //    new EnvironmentalSensorHistor{ fieldName = "8", fieldValue= "-23"  },
            //    new EnvironmentalSensorHistor{ fieldName = "9", fieldValue= "-11"  },
            //    new EnvironmentalSensorHistor{ fieldName = "10", fieldValue= "-21"  },
            //    new EnvironmentalSensorHistor{ fieldName = "11", fieldValue= "24"  },
            //    new EnvironmentalSensorHistor{ fieldName = "12", fieldValue= "41"  },
            //    new EnvironmentalSensorHistor{ fieldName = "13", fieldValue= "32"  },
            //    new EnvironmentalSensorHistor{ fieldName = "14", fieldValue= "23"  }
            //}, "#FF9D54");
            //humiEchartsOption.yTitle = "湿度 (%)";
            //humiEchartsOption.xTitle = "";
            //var opString2 = humiEchartsOption.InitOption();
            //humiEchartsView.ShowWithOption(opString2);
#endif
            LoadEvent_ChangeSensorHistoryShowType();
            LoadMothed_GetHistoryData();
        }
        void LoadEvent_ChangeSensorHistoryShowType()
        {
            btnShowHistroyData_Day.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/SensorHistoryBg1.png";
                btnShowHistroyData_Day.IsSelected = true;
                btnShowHistroyData_Month.IsSelected = false;
                btnShowHistroyData_Week.IsSelected = false;
                curQueryType = "hour";
                tempEchartOption.YvalueText = "";
                humiEchartsOption.YvalueText = "";
                LoadMothed_GetHistoryData();
            };
            btnShowHistroyData_Month.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/SensorHistoryBg3.png";
                btnShowHistroyData_Day.IsSelected = false;
                btnShowHistroyData_Month.IsSelected = true;
                btnShowHistroyData_Week.IsSelected = false;
                curQueryType = "month";
                tempEchartOption.YvalueText = "";
                humiEchartsOption.YvalueText = "";
                LoadMothed_GetHistoryData();
            };
            btnShowHistroyData_Week.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/SensorHistoryBg2.png";
                btnShowHistroyData_Day.IsSelected = false;
                btnShowHistroyData_Month.IsSelected = false;
                btnShowHistroyData_Week.IsSelected = true;
                curQueryType = "week";
                tempEchartOption.YvalueText = "";
                humiEchartsOption.YvalueText = "";
                LoadMothed_GetHistoryData();
            };
        }
        /// <summary>
        /// 正在读取温度历史数据
        /// </summary>
        bool inReadTempHistory = false;
        /// <summary>
        /// 正在读取湿度历史数据
        /// </summary>
        bool inReadHumiHistory = false;
        /// <summary>
        /// 读取历史数据
        /// </summary>
        void LoadMothed_GetHistoryData()
        {
            inReadTempHistory = true;
            inReadHumiHistory = true;
            var loadPage = new Loading()
            {
                LodingBackgroundColor = 0x88888888,
            };
            bodyView.AddChidren(loadPage);
            new System.Threading.Thread(() =>
            {
                try
                {
                    Application.RunOnMainThread(() =>
                    {
                        loadPage.Start(Language.StringByID(StringId.PleaseWait));
                    });
                    var revertObj = new HttpServerRequest().GetSensorHistory(curQueryType, function.deviceId , "room_temp");
                    if (revertObj != null)
                    {
                        if (revertObj.Code == StateCode.SUCCESS)
                        {
                            var revertData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EnvironmentalSensorHistor>>(revertObj.Data.ToString());
                            List<string> vs = new List<string>();
                            if (tempEchartOption.YvalueText == "")
                            {
                                foreach (var data in revertData)
                                {
                                    vs.Add(data.fieldName);
                                }
                                tempEchartOption.InitXdataText(vs);
                            }
                            tempEchartOption.InitYdataText("", revertData, "#FF80AEFF");
                            tempEchartOption.xTitle = Language.StringByID(StringId.timeMode);
                            var opString = tempEchartOption.InitOption();
                            Application.RunOnMainThread(() =>
                            {
                                tempEchartsView.ShowWithOption(opString);
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    MainPage.Log($"history error : {ex.Message}");
                }
                finally
                {
                    inReadTempHistory = false;
                    if (!inReadHumiHistory)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            loadPage.Hide();
                        });
                    }
                }
            })
            { IsBackground = true }.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    Application.RunOnMainThread(() =>
                    {
                        loadPage.Start(Language.StringByID(StringId.PleaseWait));
                    });
                    var revertObj = new HttpServerRequest().GetSensorHistory(curQueryType,function.deviceId, "room_humidity");
                    if (revertObj != null)
                    {
                        if (revertObj.Code == StateCode.SUCCESS)
                        {
                            var revertData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EnvironmentalSensorHistor>>(revertObj.Data.ToString());
                            List<string> vs = new List<string>();
                            if (humiEchartsOption.YvalueText == "")
                            {
                                foreach (var data in revertData)
                                {
                                    vs.Add(data.fieldName);
                                }
                                humiEchartsOption.InitXdataText(vs);
                            }
                            humiEchartsOption.InitYdataText("", revertData, "#FF80AEFF");
                        }
                    }
                    humiEchartsOption.xTitle = Language.StringByID(StringId.timeMode);
                    var opString = humiEchartsOption.InitOption();
                    Application.RunOnMainThread(() =>
                    {
                        humiEchartsView.ShowWithOption(opString);
                    });
                }
                catch (Exception ex)
                {
                    MainPage.Log($"history error : {ex.Message}");
                }
                finally
                {
                    inReadHumiHistory = false;
                    if (!inReadTempHistory)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            loadPage.Hide();
                        });
                    }
                }
            })
            { IsBackground = true }.Start();
        }
    }
}