wxr
2020-09-10 af1cb3ecd0f4b0589e00b28f7f9edccf39e6e12b
HDL_ON/UI/UI2/FuntionControlView/EnvironmentalScience/EnvironmentalSensorPage.cs
@@ -6,35 +6,55 @@
namespace HDL_ON.UI
{
    public class EnvironmentalSensorPage : FrameLayout
    public partial class EnvironmentalSensorPage : FrameLayout
    {
        EnvironmentalSensorPage bodyView;
        FrameLayout showDataTypeView;
        Button btnShowHistroyData_Day;
        Button btnShowHistroyData_Week;
        Button btnShowHistroyData_Month;
        FrameLayout historyDataView;
        MyEchartsViewOn myEchartsView;
        Sensor sensor;
        List<Sensor> showSensors;
        /// <summary>
        /// 当前查询类型
        /// </summary>
        QueryType curQueryType = QueryType.NowDay;
        List<uint> tipColorList = new List<uint>()
        {
            0xFF80AEFF,0xFFFFD154,0xFFFF9D54,0xFFFE6A6A,0xFFB183C3,0xFFADE764,
        };
        List<string> tipColorStringList = new List<string>()
        {
            "#80AEFF","#FFD154","#FF9D54","#FE6A6A","#B183C3","#ADE764",
        };
        public EnvironmentalSensorPage(Sensor ss)
        {
            bodyView = this;
            sensor = ss;
            showSensors = new List<Sensor>();
            showSensors.Add(ss);
        }
        public void LoadPage()
        {
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
            VerticalScrolViewLayout contentView = new VerticalScrolViewLayout()
            {
                Y = Application.GetRealHeight(64),
                Height = Application.GetRealHeight(600),
                ScrollEnabled = false
            };
            bodyView.AddChidren(contentView);
@@ -53,7 +73,7 @@
            contentView.AddChidren(diagramView);
            #region 选择数据日期范围
            FrameLayout showDataTypeView = new FrameLayout()
            showDataTypeView = new FrameLayout()
            {
                X = Application.GetRealWidth(183),
                Y = Application.GetRealHeight(12),
@@ -63,7 +83,7 @@
            };
            diagramView.AddChidren(showDataTypeView);
            Button btnShowHistroyData_Day = new Button()
            btnShowHistroyData_Day = new Button()
            {
                Width = Application.GetRealWidth(48),
                TextAlignment = TextAlignment.Center,
@@ -75,7 +95,7 @@
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Day);
            Button btnShowHistroyData_Week = new Button()
            btnShowHistroyData_Week = new Button()
            {
                X = Application.GetRealWidth(48),
                Width = Application.GetRealWidth(48),
@@ -87,8 +107,7 @@
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Week);
            Button btnShowHistroyData_Month = new Button()
            btnShowHistroyData_Month = new Button()
            {
                X = Application.GetRealWidth(48 * 2),
                Width = Application.GetRealWidth(48),
@@ -96,13 +115,12 @@
                TextColor = CSS_Color.PromptingColor2,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                TextID = StringId.month,
                Text = DateTime.Now.Month.ToString() + Language.StringByID(StringId.month),
            };
            showDataTypeView.AddChidren(btnShowHistroyData_Month);
            #endregion
            FrameLayout historyDataView = new FrameLayout()
            historyDataView = new FrameLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(52),
@@ -111,45 +129,9 @@
            };
            diagramView.AddChidren(historyDataView);
            List<string> xStrings = new List<string>() ;
            List<string> yStrings = new List<string>();
            Random random = new Random();
            for (int i = 0; i < 24; i++)
            {
                xStrings.Add(i.ToString());
            myEchartsView = new MyEchartsViewOn();
                switch (sensor.functionType)
                {
                    case FunctionType.PM10:
                        yStrings.Add( Math.Round(random.NextDouble() * 130, 0).ToString());
                        break;
                    case FunctionType.PM25:
                        yStrings.Add( Math.Round(random.NextDouble() * 130, 0).ToString());
                        break;
                    //case FunctionType.Noise:
                    //    break;
                    case FunctionType.WindPower:
                        break;
                    case FunctionType.CO2:
                        yStrings.Add( Math.Round(random.NextDouble() * 6000, 0).ToString());
                        break;
                    case FunctionType.Temp:
                        yStrings.Add( Math.Round(random.NextDouble() * 40, 0).ToString());
                        break;
                    case FunctionType.TVOC:
                        yStrings.Add( Math.Round(random.NextDouble() * 10, 1).ToString());
                        break;
                    case FunctionType.Humidity:
                        yStrings.Add( Math.Round(random.NextDouble() * 100, 0).ToString());
                        break;
                }
            }
            MyEchartsView myEchartsView = new MyEchartsView();
            historyDataView.AddChidren(myEchartsView);
            myEchartsView.Show(sensor.name, xStrings.ToArray(), yStrings.ToArray());
            #region 底部等级提示
            HorizontalScrolViewLayout tipValuesView = new HorizontalScrolViewLayout()
@@ -180,12 +162,11 @@
            };
            diagramView.AddChidren(tipTextView);
            switch (sensor.functionType)
            {
                case FunctionType.PM10:
                case FunctionType.PM25:
                    tipValuesView.AddChidren(new Button() {
                    tipValuesView.AddChidren(new Button()
                    {
                        Width = Application.GetRealWidth(101),
                        Text = "35",
                        TextAlignment = TextAlignment.Center,
@@ -242,19 +223,15 @@
                    });
                    break;
                //case FunctionType.PM25:
                    //_intervalValue.Add("0 ~ 35");
                    //_intervalValue.Add("36 ~ 75");
                    //_intervalValue.Add("76 ~ 115");
                    //_intervalValue.Add("115 ~");
                    //_levelTextList.Add(StringId.Great);
                    //_levelTextList.Add(StringId.Good);
                    //_levelTextList.Add(StringId.MildPollution);
                    //_levelTextList.Add(StringId.HeavyPollution);
                    //break;
                //case FunctionType.Noise:
                //    break;
                case FunctionType.WindPower:
                    break;
                //_intervalValue.Add("0 ~ 35");
                //_intervalValue.Add("36 ~ 75");
                //_intervalValue.Add("76 ~ 115");
                //_intervalValue.Add("115 ~");
                //_levelTextList.Add(StringId.Great);
                //_levelTextList.Add(StringId.Good);
                //_levelTextList.Add(StringId.MildPollution);
                //_levelTextList.Add(StringId.HeavyPollution);
                //break;
                case FunctionType.CO2:
                    tipValuesView.AddChidren(new Button()
                    {
@@ -545,7 +522,7 @@
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealWidth(100),
                BackgroundColor= CSS_Color.MainBackgroundColor,
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
                BorderColor = 0x00000000,
                BorderWidth = 0,
@@ -579,8 +556,7 @@
            };
            selfInfoView.AddChidren(btnSeltSensorName);
           Button btnFromFoorAndRoom = new Button()
            Button btnFromFoorAndRoom = new Button()
            {
                X = Application.GetRealWidth(16),
                Y = btnSeltSensorName.Bottom,
@@ -616,7 +592,8 @@
            };
            selfInfoView.AddChidren(btnSeltSensorValue);
            var btnShowData = new Button()
            Button btnShowData;
            btnShowData = new Button()
            {
                X = Application.GetRealWidth(291),
                Y = Application.GetRealWidth(62),
@@ -624,9 +601,27 @@
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "FunctionIcon/EnvironmentalScience/DiagramIcon.png",
                SelectedImagePath = "FunctionIcon/EnvironmentalScience/DiagramIconOn.png",
                IsSelected = true,
            };
            selfInfoView.AddChidren(btnShowData);
            //btnShowData.MouseUpEventHandler = (sender, e) => {
            //    btnShowData.IsSelected = !btnShowData.IsSelected;
            //    if(btnShowData.IsSelected)
            //    {
            //        if (!showSensors.Contains(sensor))
            //        {
            //            showSensors.Add(sensor);
            //        }
            //    }
            //    else
            //    {
            //        if (showSensors.Contains(sensor))
            //        {
            //            showSensors.Remove(sensor);
            //        }
            //    }
            //    LoadMothed_GetSensorHistoryData();
            //};
            btnSetting.MouseUpEventHandler = (sender, e) =>
            {
@@ -646,17 +641,19 @@
            bool initTitleButton = true;
            foreach (var tempSensor in DB_ResidenceData.residenceData.functionList.sensorsEnvironmentalScience)
            foreach (var tempSensor in DB_ResidenceData.functionList.sensorsEnvironmentalScience)
            {
                if(tempSensor.sid == sensor.sid)
                if (tempSensor.sid == sensor.sid)
                {
                    continue;
                }
                if(tempSensor.functionType != sensor.functionType)
                if (tempSensor.functionType != sensor.functionType)
                {
                    continue;
                }
                if(initTitleButton)
                contentView.ScrollEnabled = true;
                if (initTitleButton)
                {
                    Button btnOthreTitle = new Button()
                    {
@@ -711,7 +708,6 @@
                    IsBold = true,
                };
                otherInfoView.AddChidren(btnSensorName);
                Button btnFromFoorAndRoom1 = new Button()
                {
@@ -771,20 +767,235 @@
                    SelectedImagePath = "FunctionIcon/EnvironmentalScience/DiagramIconOn.png",
                };
                otherInfoView.AddChidren(btnShowData1);
                btnShowData1.MouseUpEventHandler = (sender, e) => {
                    btnShowData1.IsSelected = !btnShowData1.IsSelected;
                    if (btnShowData1.IsSelected)
                    {
                        if (!showSensors.Contains(sensor))
                        {
                            showSensors.Add(sensor);
                        }
                    }
                    else
                    {
                        if (showSensors.Contains(sensor))
                        {
                            showSensors.Remove(sensor);
                        }
                    }
                    LoadMothed_GetSensorHistoryData();
                };
                #endregion
            }
            #endregion
            new TopViewDiv(bodyView, Language.StringByID(sensor.functionTypeNameId)).LoadTopView();
            LoadMothed_GetSensorHistoryData();
            LoadEvent_ChangeSensorHistoryShowType();
        }
    }
    //------------
    public partial class EnvironmentalSensorPage
    {
       public  enum QueryType
        {
            NowDay = 0,//当天
            NowMonth = 1,//指定月
            NowYear = 2,//今年
            LatelySevenDay = 3//最近七天
        }
        /// <summary>
        /// 读取传感器历史数据
        /// </summary>
        void LoadMothed_GetSensorHistoryData()
        {
            string echartRootJsonString;
            EchartsOption echartRootJson = new EchartsOption();
            var seriesList = new List<EchartSeriesItem>();
            echartRootJson.xAxis.data = new List<string>();
            var loadPage = new Loading() {
                LodingBackgroundColor = 0x88888888,
            };
            historyDataView.AddChidren(loadPage);
            new System.Threading.Thread(() =>
            {
                try
                {
                    Application.RunOnMainThread(() => {
                        loadPage.Start("Loading...");
                    });
                    var curMonth = 0;
                    if(curQueryType == QueryType.NowMonth)
                    {
                        curMonth = DateTime.Now.Month;
                    }
                    int i = 0;
                    foreach (var sr in DB_ResidenceData.functionList.sensorsEnvironmentalScience)
                    {
                        if (sr.functionType != sensor.functionType)
                        {
                            continue;
                        }
                        if (showSensors.Find((obj) => obj.sid == sr.sid) == null)
                        {
                            i++;
                            continue;
                        }
                        var revertObj = new DAL.Server.HttpServerRequest().GetSensorHistory(sr.bus_Data.SubnetID, sr.bus_Data.DeviceID,
                               5, (int)(sr.functionType) % 256, (int)curQueryType, sr.bus_Data.LoopID, curMonth,
                               DB_ResidenceData.residenceData.residenceGatewayMAC);
                        if (revertObj != null)
                        {
                            if (revertObj.StateCode == "SUCCESS")
                            {
                                var revertData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SensorPushHistoryRes>>(revertObj.ResponseData.ToString());
                                var sItem = new EchartSeriesItem
                                {
                                    name = sr.GetRoomListName(),
                                    data = new List<int>(),
                                    lineStyle = new EchartLineStyle()
                                    {
                                        color = tipColorStringList[i],
#if __IOS__
                                        width = 5,
#elif __ANDROID__
                                        width = 2,
#endif
                                    },
                                };
                                int ass = 0;
                                for (int j = 0; j < revertData.Count; j++)
                                {
                                    if (curQueryType == QueryType.NowMonth && revertData[j].CreatedOnUtc.Day > DateTime.Now.Day )
                                    {
                                        continue;
                                    }
                                    if (revertData[j].TargetValue.ToString() != "NaN")
                                    {
                                        ass = Convert.ToInt32(revertData[j].TargetValue);
                                    }
                                    else
                                    {
                                        ass = 0;
                                    }
                                    sItem.data.Add(ass);
                                    switch (curQueryType)
                                    {
                                        case QueryType.NowDay:
                                            echartRootJson.xAxis.data.Add(revertData[j].CreatedOnUtc.Hour.ToString());
                                            break;
                                        case QueryType.LatelySevenDay:
                                            echartRootJson.xAxis.data.Add(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(revertData[j].CreatedOnUtc.DayOfWeek));
                                            break;
                                        case QueryType.NowMonth:
                                            echartRootJson.xAxis.data.Add(revertData[j].CreatedOnUtc.Day.ToString());
                                            break;
                                    }
                                }
                                seriesList.Add(sItem);
                            }
                            else
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    new PublicAssmebly().TipMsgAutoClose(Language.StringByID(StringId.GetSensorHistoryFailed), true);
                                });
                            }
                        }
                        i++;
                    }
                    //echartRootJson.title.text = "温度(℃)";lineStyle
                    //echartRootJson.color = new List<string> { "#80AEFF" };//, "FFD154", "FF9D54", "FE6A6A", "B183C3", "ADE764"
                    echartRootJson.series = seriesList;
#if __IOS__
                    echartRootJson.xAxis.axisLabel.fontSize = 22;
                    echartRootJson.yAxis.axisLabel.fontSize = 22;
#else
                    echartRootJson.xAxis.axisLabel.fontSize = 8;
                    echartRootJson.yAxis.axisLabel.fontSize = 8;
#endif
                    echartRootJsonString = Newtonsoft.Json.JsonConvert.SerializeObject(echartRootJson);
                    Application.RunOnMainThread(() =>
                    {
                        myEchartsView.ShowWithOptionJsonString(echartRootJsonString);
                    });
                }
                catch (Exception ex)
                {
                    MainPage.Log($"sensor history error : {ex.Message}");
                }
                finally
                {
                    Application.RunOnMainThread(() => {
                        loadPage.Hide();
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        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 = QueryType.NowDay;
                LoadMothed_GetSensorHistoryData();
            };
            btnShowHistroyData_Month.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/SensorHistoryBg3.png";
                btnShowHistroyData_Day.IsSelected = false;
                btnShowHistroyData_Month.IsSelected = true;
                btnShowHistroyData_Week.IsSelected = false;
                curQueryType = QueryType.NowMonth;
                LoadMothed_GetSensorHistoryData();
            };
            btnShowHistroyData_Week.MouseUpEventHandler = (sender, e) =>
            {
                showDataTypeView.BackgroundImagePath = "FunctionIcon/EnvironmentalScience/SensorHistoryBg2.png";
                btnShowHistroyData_Day.IsSelected = false;
                btnShowHistroyData_Month.IsSelected = false;
                btnShowHistroyData_Week.IsSelected = true;
                curQueryType = QueryType.LatelySevenDay;
                LoadMothed_GetSensorHistoryData();
            };
        }
        void LoadEvent_SwitchOtherRoomSensorData()
        {
        }
    }
}
    public class SensorPushHistoryRes
    {
        /// <summary>
        /// 实际目标值
        /// </summary>
        public float TargetValue { get; set; }
        /// <summary>
        /// 查询的时间集群
        /// </summary>
        public DateTime CreatedOnUtc { get; set; }
    }
}