wxr
2022-01-14 3be0abe90546f2a3a45828f5e6c8adc4517ea9e0
HDL_ON/UI/UI2/FuntionControlView/Electrical/SocketPage_CumulativePowerConsumption.cs
New file
@@ -0,0 +1,404 @@
using System;
using System.Collections.Generic;
using HDL_ON.DAL.Server;
using HDL_ON.DriverLayer;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using Shared;
namespace HDL_ON.UI
{
    public class SocketPage_CumulativePowerConsumption : FrameLayout
    {
        static SocketPage_CumulativePowerConsumption bodyView;
        /// <summary>
        /// 顶部区域
        /// </summary>
        FrameLayout generalTableView;
        /// <summary>
        /// 实时功耗
        /// </summary>
        Button btnRealTimeData;
        /// <summary>
        /// 实时功耗单位
        /// </summary>
        Button btnTotalValueUint;
        /// <summary>
        /// 总功耗
        /// </summary>
        TextButton btnTotalValue;
        /// <summary>
        /// 历史数据区域
        /// </summary>
        FrameLayout diagramView;
        /// <summary>
        /// 曲线图数据string
        /// </summary>
        EchartsOption_BrokenLine brokenLine;
        /// <summary>
        /// 插查询的能源列表
        /// </summary>
        List<Function> queryList = new List<Function>();
        /// <summary>
        /// 查询条件
        /// hour、week、month
        /// </summary>
        string curQueryType = "hour";
        /// <summary>
        /// 能源列表区域
        /// </summary>
        FrameLayout energyListView;
        /// <summary>
        /// 历史数据图表
        /// </summary>
        MyEchartsViewOn myEchartsView_Line;
        Function function;
        /// <summary>
        /// 主页
        /// </summary>
        public SocketPage_CumulativePowerConsumption(Function function)
        {
            this.function = function;
            bodyView = this;
        }
        /// <summary>
        ///
        /// </summary>
        public void LoadPage()
        {
            new TopViewDiv(bodyView, Language.StringByID(StringId.CumulativePowerConsumption)).LoadTopView();
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
            var contentView = new FrameLayout()
            {
                Y = Application.GetRealHeight(64),
                Height = Application.GetRealHeight(603),
            };
            bodyView.AddChidren(contentView);
            #region 顶部view
            generalTableView = new FrameLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealWidth(148),//419//148
                Radius = (uint)Application.GetRealWidth(5),
                BackgroundColor = CSS_Color.MainBackgroundColor,
            };
            contentView.AddChidren(generalTableView);
            btnTotalValue = new TextButton()
            {
                X = Application.GetRealWidth(18),
                Y = Application.GetRealWidth(24),
                Width = Application.GetRealWidth(18),
                Height = Application.GetRealWidth(52),
                TextColor = CSS_Color.FirstLevelTitleColor,
                IsBold = true,
                TextAlignment = TextAlignment.CenterLeft,
                TextSize = 40,
                Text = function.GetAttrState("total_electricity")
            };
            btnTotalValue.Width = btnTotalValue.GetTextWidth();
            generalTableView.AddChidren(btnTotalValue);
            btnTotalValueUint = new Button()
            {
                X = btnTotalValue.Right,
                Y = Application.GetRealWidth(24),
                Width = Application.GetRealWidth(60),
                Height = Application.GetRealWidth(28),
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                IsBold = true,
                Text = "kW‧h",
                TextAlignment = TextAlignment.CenterLeft,
            };
            generalTableView.AddChidren(btnTotalValueUint);
            btnRealTimeData = new Button()
            {
                X = Application.GetRealWidth(18),
                Y = btnTotalValue.Bottom,
                Width = Application.GetRealWidth(209),
                Height = Application.GetRealWidth(28),
                TextColor = CSS_Color.TextualColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextAlignment = TextAlignment.CenterLeft,
                TextID = StringId.RealTimePowerConsumption,
            };
            double realTimePower = 0;
            double.TryParse(function.GetAttrState("active_power"), out realTimePower);
            realTimePower /= 1000;
            btnRealTimeData.Text = Language.StringByID(StringId.RealTimePowerConsumption).Replace("{0}", realTimePower.ToString());
            generalTableView.AddChidren(btnRealTimeData);
            generalTableView.Height = Application.GetRealWidth(459);
            #endregion
            initDiagramView();
        }
        /// <summary>
        /// 加载曲线图
        /// </summary>
        /// <param name="contentView"></param>
        void initDiagramView()
        {
            brokenLine = new EchartsOption_BrokenLine();
            diagramView = new FrameLayout()
            {
                Y = Application.GetRealWidth(136),
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealWidth(281),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
                BorderColor = 0x00000000,
                BorderWidth = 0,
            };
            generalTableView.AddChidren(diagramView);
            #region 选择数据日期范围
            var showDataTypeView = new FrameLayout()
            {
                X = Application.GetRealWidth(136),
                Y = Application.GetRealHeight(12),
                Width = Application.GetRealWidth(144+47),
                Height = Application.GetRealHeight(24),
                BackgroundImagePath = "FunctionIcon/EnvironmentalScience/HistoryOpionBg1.png",
            };
            diagramView.AddChidren(showDataTypeView);
            var 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);
            var 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);
            var 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)
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Month);
            var btnShowHistroyData_Year = new Button()
            {
                X = Application.GetRealWidth(48 * 3),
                Width = Application.GetRealWidth(48),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.PromptingColor2,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                Text = Language.StringByID(StringId.Years),
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Year);
            btnShowHistroyData_Day.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/HistoryOpionBg1.png";
                btnShowHistroyData_Day.IsSelected = true;
                btnShowHistroyData_Month.IsSelected = false;
                btnShowHistroyData_Week.IsSelected = false;
                btnShowHistroyData_Year.IsSelected = false;
                curQueryType = "hour";
                brokenLine.YvalueText = "";
                LoadMothed_GetHistoryData();
            };
            btnShowHistroyData_Week.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/HistoryOpionBg2.png";
                btnShowHistroyData_Day.IsSelected = false;
                btnShowHistroyData_Month.IsSelected = false;
                btnShowHistroyData_Week.IsSelected = true;
                btnShowHistroyData_Year.IsSelected = false;
                curQueryType = "week";
                brokenLine.YvalueText = "";
                LoadMothed_GetHistoryData();
            };
            btnShowHistroyData_Month.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/HistoryOpionBg3.png";
                btnShowHistroyData_Day.IsSelected = false;
                btnShowHistroyData_Month.IsSelected = true;
                btnShowHistroyData_Week.IsSelected = false;
                btnShowHistroyData_Year.IsSelected = false;
                curQueryType = "month";
                brokenLine.YvalueText = "";
                LoadMothed_GetHistoryData();
            };
            btnShowHistroyData_Year.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/HistoryOpionBg4.png";
                btnShowHistroyData_Day.IsSelected = false;
                btnShowHistroyData_Month.IsSelected = false;
                btnShowHistroyData_Year.IsSelected = true;
                btnShowHistroyData_Week.IsSelected = false;
                curQueryType = "year_month";
                brokenLine.YvalueText = "";
                LoadMothed_GetHistoryData();
            };
            #endregion
            var historyDataView = new FrameLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealWidth(52),
                Height = Application.GetRealWidth(240),
            };
            diagramView.AddChidren(historyDataView);
            myEchartsView_Line = new MyEchartsViewOn()
            {
                Y = Application.GetRealWidth(10),
                Height = Application.GetRealWidth(220),
            };
            historyDataView.AddChidren(myEchartsView_Line);
            LoadMothed_GetHistoryData();
        }
        /// <summary>
        /// 读取历史数据
        /// </summary>
        void LoadMothed_GetHistoryData()
        {
            var loadPage = new Loading()
            {
                LodingBackgroundColor = 0x88888888,
            };
            diagramView.AddChidren(loadPage);
            //var d = new List<string> ();
            //var dd = new List<EnvironmentalSensorHistor>();
            //Random random = new Random() ;
            //for (int ii = 0; ii < 13; ii++)
            //{
            //    var i = random.Next(10000);
            //    d.Add(i.ToString());
            //    dd.Add(new EnvironmentalSensorHistor
            //    {
            //        fieldName = i.ToString(),
            //        fieldValue = i.ToString()
            //    });
            //}
            //brokenLine.InitXdataText(d);
            //brokenLine.InitYdataText(function.name, dd, "#80AEFF");
            //brokenLine.yTitle = "能耗(kW)";
            //brokenLine.xTitle = "小时";
            //var opString = brokenLine.InitOption();
            //myEchartsView_Line.ShowWithOption(opString);
            //return;
            new System.Threading.Thread(() =>
            {
                try
                {
                    Application.RunOnMainThread(() =>
                    {
                        loadPage.Start(Language.StringByID(StringId.PleaseWait));
                    });
                    var sensorType = function.spk.Split(".")[1];
                    var revertObj = new HttpServerRequest().GetSensorHistory(curQueryType, function.deviceId, "total_electricity");
                    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 (brokenLine.YvalueText == "")
                            {
                                foreach (var data in revertData)
                                {
                                    vs.Add(data.fieldName);
                                }
                                brokenLine.InitXdataText(vs);
                            }
                            if (function.GetAttribute("ydata") == null)
                            {
                                function.attributes.Add(new FunctionAttributes() { key = "ydata" });
                            }
                            function.SetAttrState("ydata", brokenLine.InitYdataText(function.name, revertData, "#80AEFF") + ",");
                        }
                    }
                    brokenLine.yTitle = Language.StringByID(StringId.EnergyConsumption) + "(kW)";
                    brokenLine.xTitle = Language.StringByID(StringId.timeMode);
                    if (curQueryType != "hour")
                    {
                        brokenLine.xTitle = Language.StringByID(StringId.Date);
                    }
                    var opString = brokenLine.InitOption();
                    Application.RunOnMainThread(() =>
                    {
                        myEchartsView_Line.ShowWithOption(opString);
                    });
                }
                catch (Exception ex)
                {
                    MainPage.Log($"sensor history error : {ex.Message}");
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loadPage.Hide();
                    });
                }
            })
            { IsBackground = true }.Start();
        }
    }
}