using Shared.Common;
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.MainPage
{
///
/// 温湿度,PM2.5,Co2的历史记录界面
///
public class DeviceTemperatureHistoryForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 左右滑动的控件
///
private HorizontalScrolViewLayout horizontalScrolView = null;
///
/// 设备的某一个回路
///
private CommonDevice device = null;
///
/// 1:温度 2:湿度 3:PM2.5 4:CO2
///
private int nowSelectObjType = -1;
///
/// 1:1天 2:1周 3:一个月
///
private string nowSelectTimeType = "1";
///
/// 历史记录数据(缓存使用)
///
private Dictionary> dicHistoryData = new Dictionary>();
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
///
public void ShowForm(CommonDevice i_device)
{
this.device = i_device;
this.ScrollEnabled = false;
//设置头部信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uHistoryLog));
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//清空bodyFrame
this.ClearBodyFrame();
//因为曲线图很宽,所以使用左右滑动的控件
this.horizontalScrolView = new HorizontalScrolViewLayout();
#if Android
horizontalScrolView.Y = Application.GetRealHeight(243);
horizontalScrolView.Height = bodyFrameLayout.Height - Application.GetRealHeight(243);
#endif
#if iOS
horizontalScrolView.Y = Application.GetRealHeight(300);
horizontalScrolView.Height = bodyFrameLayout.Height - Application.GetRealHeight(300);
#endif
bodyFrameLayout.AddChidren(horizontalScrolView);
//初始化类别控件
this.InitTypeSelectControl();
//初始化日期菜单控件
this.InitDateMenuControl();
}
#endregion
#region ■ 初始化类别控件_____________________
///
/// 初始化类别控件
///
private void InitTypeSelectControl()
{
//先获取这个设备的具体类型
var myType = LocalDevice.Current.GetMyDeviceEnumInfo(new List { this.device });
var XX = ControlCommonResourse.XXLeft;
//类别之间的间距
var defultSpace = Application.GetRealWidth(80);
NormalViewControl nowBtnSelect = null;
//温度
FrameLayoutStatuControl frameTemperatrue = null;
//湿度
FrameLayoutStatuControl frameHumidity = null;
//PM2.5
FrameLayoutStatuControl framePm = null;
//CO2
//FrameLayoutStatuControl frameCo2 = null;
//检测是否拥有温度功能
if (this.CheckDeviceHadTemperatrue(myType) == true)
{
//温度
frameTemperatrue = this.CreatTypeSelectControl(Language.StringByID(R.MyInternationalizationString.uTemperature));
frameTemperatrue.X = XX;
XX = frameTemperatrue.Right + defultSpace;
frameTemperatrue.ButtonClickEvent += (sender, e) =>
{
//同一个东西,不处理
if (nowSelectObjType == 1) { return; }
nowSelectObjType = 1;
//设置选择控件的状态(原来的变更为取消)
this.SetSelectControlStatu(nowBtnSelect, false, 0);
nowBtnSelect = frameTemperatrue.GetChildren(0) as NormalViewControl;
//设置选择控件的状态(当前变更为选择)
this.SetSelectControlStatu(nowBtnSelect, true, 1);
//刷新Echart控件
this.RefreshEchartControl();
};
}
//检测是否拥有湿度功能
if (this.CheckDeviceHadHumidity(myType) == true)
{
//湿度
frameHumidity = this.CreatTypeSelectControl(Language.StringByID(R.MyInternationalizationString.uHumidity));
frameHumidity.X = XX;
XX = frameHumidity.Right + defultSpace;
frameHumidity.ButtonClickEvent += (sender, e) =>
{
//同一个东西,不处理
if (nowSelectObjType == 2) { return; }
nowSelectObjType = 2;
//设置选择控件的状态(原来的变更为取消)
this.SetSelectControlStatu(nowBtnSelect, false, 0);
nowBtnSelect = frameHumidity.GetChildren(0) as NormalViewControl;
//设置选择控件的状态(当前变更为选择)
this.SetSelectControlStatu(nowBtnSelect, true, 2);
//刷新Echart控件
this.RefreshEchartControl();
};
}
//检测该设备是否拥有PM2.5的功能
if (this.CheckDeviceHadPM25(myType) == true)
{
//PM2.5
framePm = this.CreatTypeSelectControl("PM2.5");
framePm.X = XX;
XX = framePm.Right + defultSpace;
framePm.ButtonClickEvent += (sender, e) =>
{
//同一个东西,不处理
if (nowSelectObjType == 3) { return; }
nowSelectObjType = 3;
//设置选择控件的状态(原来的变更为取消)
this.SetSelectControlStatu(nowBtnSelect, false, 0);
nowBtnSelect = framePm.GetChildren(0) as NormalViewControl;
//设置选择控件的状态(当前变更为选择)
this.SetSelectControlStatu(nowBtnSelect, true, 3);
//刷新Echart控件
this.RefreshEchartControl();
};
}
//检测该设备是否拥有CO²的功能
//if (this.CheckDeviceHadCO2(myType) == true)
//{
// //CO²
// frameCo2 = this.CreatTypeSelectControl("CO²");
// frameCo2.X = framePm.Right + defultSpace;
// frameCo2.ButtonClickEvent += (sender, e) =>
// {
// //同一个东西,不处理
// if (nowSelectObjType == 4) { return; }
// nowSelectObjType = 4;
// //设置选择控件的状态(原来的变更为取消)
// this.SetSelectControlStatu(nowBtnSelect, false, 0);
// nowBtnSelect = frameCo2.GetChildren(0) as NormalViewControl;
// //设置选择控件的状态(当前变更为选择)
// this.SetSelectControlStatu(nowBtnSelect, true, 4);
// //刷新Echart控件
// this.RefreshEchartControl();
// };
//}
//默认初始选择
if (frameTemperatrue != null)
{
this.nowSelectObjType = 1;
nowBtnSelect = frameTemperatrue.GetChildren(0) as NormalViewControl;
this.SetSelectControlStatu(nowBtnSelect, true, 1);
}
else if (frameHumidity != null)
{
this.nowSelectObjType = 2;
nowBtnSelect = frameHumidity.GetChildren(0) as NormalViewControl;
this.SetSelectControlStatu(nowBtnSelect, true, 2);
}
else if (framePm != null)
{
this.nowSelectObjType = 3;
nowBtnSelect = framePm.GetChildren(0) as NormalViewControl;
this.SetSelectControlStatu(nowBtnSelect, true, 3);
}
//else if (frameCo2 != null)
//{
// this.nowSelectObjType = 4;
// nowBtnSelect = frameCo2.GetChildren(0) as NormalViewControl;
// this.SetSelectControlStatu(nowBtnSelect, true, 4);
//}
}
///
/// 创建类别控件原型
///
///
///
private FrameLayoutStatuControl CreatTypeSelectControl(string i_text)
{
//桌布控件
var frameControl = new FrameLayoutStatuControl();
frameControl.Y = Application.GetRealHeight(35);
frameControl.Height = Application.GetRealHeight(60);
frameControl.Width = Application.GetRealWidth(100);
frameControl.UseClickStatu = false;
bodyFrameLayout.AddChidren(frameControl);
//选择控件
var btnSelect = new NormalViewControl(Application.GetRealWidth(40), Application.GetRealWidth(40), false);
btnSelect.BackgroundColor = UserCenterColor.Current.White;
btnSelect.Radius = (uint)Application.GetRealWidth(12);
btnSelect.BorderWidth = 1;
btnSelect.BorderColor = UserCenterColor.Current.TextGrayColor2;
btnSelect.Gravity = Gravity.CenterVertical;
frameControl.AddChidren(btnSelect);
//显示文本
var btnText = new NormalViewControl(Application.GetRealWidth(90), frameControl.Height, false);
btnText.Text = i_text;
btnText.TextColor = UserCenterColor.Current.TextGrayColor3;
btnText.X = btnSelect.Right + Application.GetRealWidth(35);
btnText.Width = btnText.GetRealWidthByText();
frameControl.AddChidren(btnText);
//计算真实宽度
frameControl.Width = btnText.Right;
return frameControl;
}
///
/// 设置选择控件的状态
///
/// 选择控件
/// 是否是选择状态
/// 因为颜色不一样 1:温度 2:湿度 3:PM2.5 4:CO2
private void SetSelectControlStatu(NormalViewControl btnSelect, bool isSelect, int div)
{
if (btnSelect == null) { return; }
if (isSelect == true)
{
btnSelect.BorderWidth = 0;
btnSelect.BorderColor = UserCenterColor.Current.Transparent;
if (div == 1)
{
btnSelect.BackgroundColor = 0xfffc744b;
}
else if (div == 2)
{
btnSelect.BackgroundColor = 0xff65749f;
}
else if (div == 3)
{
btnSelect.BackgroundColor = 0xff35b87f;
}
else
{
btnSelect.BackgroundColor = 0xfffbc909;
}
}
else
{
btnSelect.BorderWidth = 1;
btnSelect.BorderColor = UserCenterColor.Current.TextGrayColor2;
btnSelect.BackgroundColor = UserCenterColor.Current.White;
}
}
#endregion
#region ■ 初始化日期菜单控件_________________
///
/// 初始化日期菜单控件
///
private void InitDateMenuControl()
{
var dicText = new Dictionary();
//1天
dicText["1"] = "1" + Language.StringByID(R.MyInternationalizationString.uDate1);
//1周
dicText["2"] = "1" + Language.StringByID(R.MyInternationalizationString.uWeek1);
//1个月
dicText["3"] = "1" + Language.StringByID(R.MyInternationalizationString.uOneMonth1);
var menuContr = new TextGroupMenuControl(dicText);
menuContr.Y = Application.GetRealHeight(98);
bodyFrameLayout.AddChidren(menuContr);
menuContr.SelectEvent += (selectKey, selectText) =>
{
this.nowSelectTimeType = selectKey;
//刷新Echart控件
this.RefreshEchartControl();
};
//执行初始化控件
menuContr.InitControl();
}
#endregion
#region ■ 初始化日期曲线控件_________________
///
/// 初始化日期曲线控件
///
private void InitDateChartControl(MyEchartsViewOn myEchartsView)
{
string strKey = this.nowSelectObjType + "_" + this.nowSelectTimeType;
if (this.dicHistoryData.ContainsKey(strKey) == false)
{
this.ShowProgressBar();
//获取云端数据
var listOneDayData = this.ReadDataFromClound();
if (listOneDayData == null)
{
this.CloseProgressBar();
return;
}
this.dicHistoryData[strKey] = listOneDayData;
}
//根据【一天制】合并历史记录 key:'16:00'格式(本地时间,分钟强制为00)
var dicHistory = this.MargeHistoryDataInDayType(this.dicHistoryData[strKey]);
//获取坐标轴单位对象(24个)
var nowTime = DateTime.Now;
int nowHour = nowTime.Hour;
var listDay = new List();
var listHour = new List();
for (int i = 1; i <= 24; i++)
{
//因为云端需要经过1个小时之后,才会覆盖数据库,所以不能算当前的时间点
nowHour--;
if (nowHour < 0)
{
nowHour = 23;
//向前推一天
nowTime = nowTime.AddDays(-1);
}
//当前的时间放在前面
listHour.Add("'" + nowHour + ":00'");
listDay.Add(nowTime.ToString("yyyy-MM-dd"));
}
//X轴文本
string XAxisText = "[" + string.Join(",", listHour) + "]";
//mark的样板 {0}:日期(2020-12-12) {1}:时间(16:00) {2}:值 {3}:优良评价(仅对PM2.5有效)
var markTextSample = this.GetEchartTipTextSample();
//组装Value
string valueText = string.Empty;
for (int i = 0; i < listHour.Count; i++)
{
//有值则设置值
string hour = listHour[i];
if (dicHistory.ContainsKey(hour) == true)
{
decimal value = dicHistory[hour].deValue;
//把'去掉
hour = hour.Replace("'", string.Empty);
//按以下格式显示
//2019-12-16 18:56
//湿度:68%
var mark = markTextSample.Replace("{0}", listDay[i]).Replace("{1}", hour).Replace("{2}", value.ToString());
//特殊处理PM2.5的显示
this.AdjustPM25TipText(ref mark, value);
valueText += "{value:" + value + ",mark:" + mark + "},\r\n";
}
//没有数据则设置为null
else
{
valueText += "{value:null,mark:''},\r\n";
}
}
valueText = "[" + valueText + "]";
//获取曲线控件共通Option
//{0}:光标移动时,那条竖线的颜色
//{1}:X轴的组员项
//{2}:Y轴的单位格式
//{3}:X轴组员对应的值
//{4}:曲线的颜色
string commonJson = this.GetChartControlCommonOption();
commonJson = commonJson.Replace("{0}", this.GetEchartLineColor());
commonJson = commonJson.Replace("{1}", XAxisText);
commonJson = commonJson.Replace("{2}", this.GetEchartUint());
commonJson = commonJson.Replace("{3}", valueText);
commonJson = commonJson.Replace("{4}", this.GetEchartLineColor());
HdlThreadLogic.Current.RunMain(() =>
{
//加载刷新图表
myEchartsView.ShowWithOption(commonJson);
this.CloseProgressBar();
});
}
///
/// 根据【一天制】合并历史记录 key:16:00格式(本地时间,分钟强制为00)
///
///
///
private Dictionary MargeHistoryDataInDayType(List i_listData)
{
//只能要今天和昨天的
string nowDay = DateTime.Now.ToString("yyyy-MM-dd");
string yesterDay = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
var dicData = new Dictionary>();
foreach (var data in i_listData)
{
//即使没有数据,云端也会返回空的数据
if (data.Value == null) { continue; }
//将UTC时间转为本地时间
var utcTime = UserCenterLogic.ConvertUtcTimeToLocalTime(data.EnterUtcTime);
data.Day = utcTime.ToString("yyyy-MM-dd");
if (data.Day != nowDay && data.Day != yesterDay)
{
//只能要今天和昨天的
continue;
}
//转换结果值
this.ConvertHistoryDataValue(data);
data.Hour = utcTime.ToString("HH:00");
if (dicData.ContainsKey(data.Hour) == false)
{
dicData[data.Hour] = new List();
}
dicData[data.Hour].Add(data);
}
var dicReturn = new Dictionary();
foreach (var list in dicData.Values)
{
//取平均值
var data = new HistoryData();
data.Day = list[0].Day;
data.Hour = list[0].Hour;
decimal valueCount = 0;
for (int i = 0; i < list.Count; i++)
{
valueCount += list[i].deValue;
}
//计算平均值
data.deValue = this.CalculateAvgResult(valueCount, list.Count);
dicReturn["'" + data.Hour + "'"] = data;
}
return dicReturn;
}
#endregion
#region ■ 初始化周期曲线控件_________________
///
/// 初始化周期曲线控件
///
private void InitWeekChartControl(MyEchartsViewOn myEchartsView)
{
string strKey = this.nowSelectObjType + "_" + this.nowSelectTimeType;
if (this.dicHistoryData.ContainsKey(strKey) == false)
{
this.ShowProgressBar();
//获取云端数据
var listOneWeekData = this.ReadDataFromClound();
if (listOneWeekData == null)
{
this.CloseProgressBar();
return;
}
this.dicHistoryData[strKey] = listOneWeekData;
}
//根据【一周制】合并历史记录 key:2020-12-12-16:00格式(本地时间,分钟强制为00)
var dicHistory = this.MargeHistoryDataInWeekType(this.dicHistoryData[strKey]);
//获取坐标轴单位对象(14个)
var nowDateTime = DateTime.Now;
var listDay = new List();
//只能要过去7天(不含今天)
for (int i = 1; i <= 7; i++)
{
nowDateTime = nowDateTime.AddDays(-1);
var strDay = nowDateTime.ToString("yyyy-MM-dd");
listDay.Add(strDay + "-0:00");
listDay.Add(strDay + "-12:00");
}
//X轴文本
string XAxisText = "['1','','2','','3','','4','','5','','6','','7']";
//mark的样板 {0}:日期(2020-12-12) {1}:时间(16:00) {2}:值 {3}:优良评价(仅对PM2.5有效)
var markTextSample = this.GetEchartTipTextSample();
//组装Value
string valueText = string.Empty;
for (int i = 0; i < listDay.Count; i++)
{
//有值则设置值
if (dicHistory.ContainsKey(listDay[i]) == true)
{
var info = dicHistory[listDay[i]];
//按以下格式显示
//2019-12-16 0:00~11:59
//湿度:68%
string mark = string.Empty;
if (info.Hour == "0:00")
{
mark = markTextSample.Replace("{0}", info.Day).Replace("{1}", "0:00~11:59").Replace("{2}", info.deValue.ToString());
}
else
{
mark = markTextSample.Replace("{0}", info.Day).Replace("{1}", "12:00~23:59").Replace("{2}", info.deValue.ToString());
}
//特殊处理PM2.5的显示
this.AdjustPM25TipText(ref mark, info.deValue);
valueText += "{value:" + info.deValue + ",mark:" + mark + "},\r\n";
}
//没有数据则设置为null
else
{
valueText += "{value:null,mark:''},\r\n";
}
}
valueText = "[" + valueText + "]";
//获取曲线控件共通Option
//{0}:光标移动时,那条竖线的颜色
//{1}:X轴的组员项
//{2}:Y轴的单位格式
//{3}:X轴组员对应的值
//{4}:曲线的颜色
string commonJson = this.GetChartControlCommonOption();
commonJson = commonJson.Replace("{0}", this.GetEchartLineColor());
commonJson = commonJson.Replace("{1}", XAxisText);
commonJson = commonJson.Replace("{2}", this.GetEchartUint());
commonJson = commonJson.Replace("{3}", valueText);
commonJson = commonJson.Replace("{4}", this.GetEchartLineColor());
HdlThreadLogic.Current.RunMain(() =>
{
//加载刷新图表
myEchartsView.ShowWithOption(commonJson);
this.CloseProgressBar();
});
}
///
/// 根据【一周制】合并历史记录 key:2020-12-12-16:00格式(本地时间,分钟强制为00)
///
///
///
private Dictionary MargeHistoryDataInWeekType(List i_listData)
{
//只能要过去7天(不含今天)
var nowDateTime = DateTime.Now;
var listDay = new HashSet();
for (int i = 1; i <= 7; i++)
{
nowDateTime = nowDateTime.AddDays(-1);
listDay.Add(nowDateTime.ToString("yyyy-MM-dd"));
}
var dicData = new Dictionary>();
foreach (var data in i_listData)
{
//即使没有数据,云端也会返回空的数据
if (data.Value == null) { continue; }
//将UTC时间转为本地时间
var utcTime = UserCenterLogic.ConvertUtcTimeToLocalTime(data.EnterUtcTime);
data.Day = utcTime.ToString("yyyy-MM-dd");
if (listDay.Contains(data.Day) == false)
{
//只能要过去7天(不含今天)
continue;
}
//转换结果值
this.ConvertHistoryDataValue(data);
//0:00~11:59归为 0:00 12:00~23:00归为12:00
int year = Convert.ToInt32(utcTime.ToString("HH"));
if (0 <= year && year <= 11)
{
data.Hour = "0:00";
}
else
{
data.Hour = "12:00";
}
string strKey = data.Day + "-" + data.Hour;
if (dicData.ContainsKey(strKey) == false)
{
dicData[strKey] = new List();
}
dicData[strKey].Add(data);
}
var dicReturn = new Dictionary();
foreach (var list in dicData.Values)
{
//取平均值
var data = new HistoryData();
data.Day = list[0].Day;
data.Hour = list[0].Hour;
decimal valueCount = 0;
for (int i = 0; i < list.Count; i++)
{
valueCount += list[i].deValue;
}
//计算平均值
data.deValue = this.CalculateAvgResult(valueCount, list.Count);
dicReturn[data.Day + "-" + data.Hour] = data;
}
return dicReturn;
}
#endregion
#region ■ 初始化月期曲线控件_________________
///
/// 初始化月期曲线控件
///
private void InitMonthChartControl(MyEchartsViewOn myEchartsView)
{
string strKey = this.nowSelectObjType + "_" + this.nowSelectTimeType;
if (this.dicHistoryData.ContainsKey(strKey) == false)
{
this.ShowProgressBar();
//获取云端数据
var listOneMonthData = this.ReadDataFromClound();
if (listOneMonthData == null)
{
this.CloseProgressBar();
return;
}
this.dicHistoryData[strKey] = listOneMonthData;
}
//根据【一月制】合并历史记录 key:2020-12-12格式(本地时间)
var dicHistory = this.MargeHistoryDataInMonthType(this.dicHistoryData[strKey]);
//获取坐标轴单位对象(30个)
var nowDateTime = DateTime.Now;
var listDay = new List();
var listXAxis = new List();
//只能要过去30天(不含今天)
for (int i = 1; i <= 30; i++)
{
nowDateTime = nowDateTime.AddDays(-1);
listDay.Add(nowDateTime.ToString("yyyy-MM-dd"));
listXAxis.Add("'" + i.ToString() + "'");
}
//X轴文本
string XAxisText = "[" + string.Join(",", listXAxis) + "]";
//mark的样板 {0}:日期(2020-12-12) {1}:时间(16:00) {2}:值 {3}:优良评价(仅对PM2.5有效)
var markTextSample = this.GetEchartTipTextSample();
//组装Value
string valueText = string.Empty;
for (int i = 0; i < listDay.Count; i++)
{
//有值则设置值
if (dicHistory.ContainsKey(listDay[i]) == true)
{
var info = dicHistory[listDay[i]];
//按以下格式显示
//2019-12-16
//湿度:68%
var mark = markTextSample.Replace("{0}", info.Day).Replace("{1}", string.Empty).Replace("{2}", info.deValue.ToString());
//特殊处理PM2.5的显示
this.AdjustPM25TipText(ref mark, info.deValue);
valueText += "{value:" + info.deValue + ",mark:" + mark + "},\r\n";
}
//没有数据则设置为null
else
{
valueText += "{value:null,mark:''},\r\n";
}
}
valueText = "[" + valueText + "]";
//获取曲线控件共通Option
//{0}:光标移动时,那条竖线的颜色
//{1}:X轴的组员项
//{2}:Y轴的单位格式
//{3}:X轴组员对应的值
//{4}:曲线的颜色
string commonJson = this.GetChartControlCommonOption();
commonJson = commonJson.Replace("{0}", this.GetEchartLineColor());
commonJson = commonJson.Replace("{1}", XAxisText);
commonJson = commonJson.Replace("{2}", this.GetEchartUint());
commonJson = commonJson.Replace("{3}", valueText);
commonJson = commonJson.Replace("{4}", this.GetEchartLineColor());
HdlThreadLogic.Current.RunMain(() =>
{
//加载刷新图表
myEchartsView.ShowWithOption(commonJson);
this.CloseProgressBar();
});
}
///
/// 根据【一月制】合并历史记录 key:2020-12-12格式(本地时间)
///
///
///
private Dictionary MargeHistoryDataInMonthType(List i_listData)
{
//只能要过去30天(不含今天)
var nowDateTime = DateTime.Now;
var listDay = new HashSet();
for (int i = 1; i <= 30; i++)
{
nowDateTime = nowDateTime.AddDays(-1);
listDay.Add(nowDateTime.ToString("yyyy-MM-dd"));
}
var dicData = new Dictionary>();
foreach (var data in i_listData)
{
//即使没有数据,云端也会返回空的数据
if (data.Value == null) { continue; }
//将UTC时间转为本地时间
var utcTime = UserCenterLogic.ConvertUtcTimeToLocalTime(data.EnterUtcTime);
data.Day = utcTime.ToString("yyyy-MM-dd");
if (listDay.Contains(data.Day) == false)
{
//只能要过去30天(不含今天)
continue;
}
//转换结果值
this.ConvertHistoryDataValue(data);
string strKey = data.Day + "-" + data.Hour;
if (dicData.ContainsKey(data.Day) == false)
{
dicData[data.Day] = new List();
}
dicData[data.Day].Add(data);
}
var dicReturn = new Dictionary();
foreach (var list in dicData.Values)
{
//取平均值
var data = new HistoryData();
data.Day = list[0].Day;
decimal valueCount = 0;
for (int i = 0; i < list.Count; i++)
{
valueCount += list[i].deValue;
}
//计算平均值
data.deValue = this.CalculateAvgResult(valueCount, list.Count);
dicReturn[data.Day] = data;
}
return dicReturn;
}
#endregion
#region ■ 刷新Echart控件_____________________
///
/// 刷新Echart控件
///
private void RefreshEchartControl()
{
this.horizontalScrolView.RemoveAll();
//重新初始化Echart控件
var myEchartsView = new MyEchartsViewOn();
myEchartsView.Height = this.horizontalScrolView.Height;
myEchartsView.Width = this.horizontalScrolView.Width;
this.horizontalScrolView.AddChidren(myEchartsView);
if (this.nowSelectObjType == -1)
{
//虽然有点不可能,但是还是判断一下
return;
}
HdlThreadLogic.Current.RunThread(() =>
{
//1天
if (this.nowSelectTimeType == "1")
{
//初始化日期曲线控件
this.InitDateChartControl(myEchartsView);
}
//1周
else if (this.nowSelectTimeType == "2")
{
//初始化周期曲线控件
this.InitWeekChartControl(myEchartsView);
}
//1月
else if (this.nowSelectTimeType == "3")
{
//初始化月期曲线控件
this.InitMonthChartControl(myEchartsView);
}
});
}
#endregion
#region ■ 曲线控件共通Option_________________
///
/// 获取曲线控件共通Option
///
///
private string GetChartControlCommonOption()
{
//{0}:光标移动时,那条竖线的颜色
//{1}:X轴的组员项
//{2}:Y轴的单位格式
//{3}:X轴组员对应的值
//{4}:曲线的颜色
#if Android
return @"{
color: ['#73A0FA', '#73DEB3', '#FFB761'],
tooltip: {
trigger: 'axis',
backgroundColor:'#777779',
axisPointer: {
type: 'line',
lineStyle: {
type: 'dashed',
color:{0}
}
},
formatter:function(pra){
return pra[0].data.mark;
}
},
dataZoom:
{
type: 'inside',
show: true,
start: 0,
end: 50
},
grid: {
left: '10',
right: '0',
bottom: '25',
top: '45',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap:true,
axisLabel:{
color: '#666666',
textStyle: {
fontSize: 12
},
},
data: {1},
splitLine: {
show: false
},
axisTick: {
show: false
},
axisLine: {
show: false
},
},
yAxis: {
type: 'value',
axisLabel: {
color: '#666666',
formatter:'{value}{2}',
textStyle: {
fontSize: 12
},
},
splitLine: {
show: true,
lineStyle: {
color: '#CCCCCC',
width:1,
type:'dashed'
}
},
axisTick: {
show: false
},
axisLine: {
show: false
},
},
series: [{
type: 'line',
smooth: true,
symbol:'emptyCircle',
symbolSize:6,
connectNulls:true,
color:{4},
data: {3}
}
]
}";
#endif
#if iOS
return @"{
color: ['#73A0FA', '#73DEB3', '#FFB761'],
backgroundColor:'#f5f6fa',
tooltip: {
trigger: 'axis',
backgroundColor:'#777779',
axisPointer: {
type: 'line',
lineStyle: {
type: 'dashed',
color:{0}
}
},
textStyle: {
fontSize: 36
},
formatter:function(pra){
return pra[0].data.mark;
}
},
dataZoom:
{
type: 'inside',
show: true,
start: 0,
end: 50
},
grid: {
left: '10',
right: '0',
bottom: '25',
top: '45',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap:true,
axisLabel:{
color: '#666666',
textStyle: {
fontSize: 30
},
},
data: {1},
splitLine: {
show: false
},
axisTick: {
show: false
},
axisLine: {
show: false
},
},
yAxis: {
type: 'value',
axisLabel: {
color: '#666666',
formatter:'{value}{2}',
textStyle: {
fontSize: 30
},
},
splitLine: {
show: true,
lineStyle: {
color: '#CCCCCC',
width:1,
type:'dashed'
}
},
axisTick: {
show: false
},
axisLine: {
show: false
},
},
series: [{
type: 'line',
smooth: true,
symbol:'emptyCircle',
symbolSize:15,
connectNulls:true,
color:{4},
data: {3}
}
]
}";
#endif
}
#endregion
#region ■ 获取云端数据_______________________
///
/// 获取云端数据
///
private List ReadDataFromClound()
{
string gwMacmark = string.Empty;
if (ZbGateway.GateWayBaseInfomations.ContainsKey(this.device.CurrentGateWayId) == false)
{
//找不到网关的MacMark
return null;
}
gwMacmark = ZbGateway.GateWayBaseInfomations[this.device.CurrentGateWayId].MacMark;
//1026:温度; 1037:Co2; 1066:PM2.5; 1029:湿度
int dataType = 0;
if (this.nowSelectObjType == 1) { dataType = 1026; }
else if (this.nowSelectObjType == 2) { dataType = 1029; }
else if (this.nowSelectObjType == 3) { dataType = 1066; }
else if (this.nowSelectObjType == 4) { dataType = 1037; }
//0:今天;1:最近七天;2:最近30天
int queryType = -1;
if (this.nowSelectTimeType == "1") { queryType = 0; }
else if (this.nowSelectTimeType == "2") { queryType = 1; }
else if (this.nowSelectTimeType == "3") { queryType = 2; }
var pra = new
{
RequestVersion = CommonPage.RequestVersion,
RequestSource = 0,
LoginAccessToken = Config.Instance.Token,
HomeId = Config.Instance.Home.Id,
GatewayId = gwMacmark,
DeviceAttr = this.device.DeviceAddr,
Epoint = this.device.DeviceEpoint,
DataType = dataType,
QueryType = queryType,
TimeZone = TimeZoneInfo.Local.BaseUtcOffset.TotalHours
};
var result = UserCenterLogic.GetResponseDataByRequestHttps("AirQuality/GetZigbeeAirQualiryHistory", false, pra, new List { "NotCheck" }, false);
if (result == null)
{
return null;
}
return Newtonsoft.Json.JsonConvert.DeserializeObject>(result);
}
///
/// 获取云端数据
///
private List ReadDataFromClound2()
{
var listData = new List();
var data1 = new HistoryData { deValue = 30, EnterUtcTime = "2021-01-14 3:35:00" };
listData.Add(data1);
var data2 = new HistoryData { deValue = 31.6m, EnterUtcTime = "2021-01-14 3:25:00" };
listData.Add(data2);
var data3 = new HistoryData { deValue = 32, EnterUtcTime = "2021-01-14 3:15:00" };
listData.Add(data3);
var data4 = new HistoryData { deValue = 27.2m, EnterUtcTime = "2021-01-14 2:25:00" };
listData.Add(data4);
var data5 = new HistoryData { deValue = 28, EnterUtcTime = "2021-01-14 1:35:00" };
listData.Add(data5);
var data6 = new HistoryData { deValue = 33, EnterUtcTime = "2021-01-13 23:55:00" };
listData.Add(data6);
var data7 = new HistoryData { deValue = 32, EnterUtcTime = "2021-01-13 15:01:10" };
listData.Add(data7);
var data8 = new HistoryData { deValue = 30, EnterUtcTime = "2021-01-13 12:11:12" };
listData.Add(data8);
var data9 = new HistoryData { deValue = 30, EnterUtcTime = "2021-01-13 0:11:12" };
listData.Add(data9);
return listData;
}
///
/// 历史数据
///
private class HistoryData
{
public string GatewayId = string.Empty;
public string DeviceAddr = string.Empty;
public int Epoint = 0;
public string DataType = string.Empty;
///
/// 云端沙雕,把这个整为字符串
///
public string Value = string.Empty;
public decimal deValue = 0;
///
/// 2021-01-12 16:00:00格式(云端返回)
///
public string EnterUtcTime = string.Empty;
///
/// 2021-01-12格式(手动赋值,本地时间)
///
public string Day = string.Empty;
///
/// 16:00格式(手动赋值,本地时间,分钟强制为00)
///
public string Hour = string.Empty;
}
#endregion
#region ■ 一般方法___________________________
///
/// 获取图标控件线的颜色
///
///
private string GetEchartLineColor()
{
//温度
if (this.nowSelectObjType == 1)
{
return "'#FC744B'";
}
//湿度
else if (this.nowSelectObjType == 2)
{
return "'#65749F'";
}
//PM2.5
else if (this.nowSelectObjType == 3)
{
return "'#35B87F'";
}
return "'#FBC909'";
}
///
/// 获取图标控件单位
///
///
private string GetEchartUint()
{
//温度
if (this.nowSelectObjType == 1)
{
return "℃";
}
//湿度
else if (this.nowSelectObjType == 2)
{
return "%";
}
//PM2.5
else if (this.nowSelectObjType == 3)
{
return "ug/m³";
}
return "ppm";
}
///
/// 获取曲线控件的显示文本样式
///
///
private string GetEchartTipTextSample()
{
//温度
if (this.nowSelectObjType == 1)
{
var text = Language.StringByID(R.MyInternationalizationString.uTemperature);
return "'{0} {1}
" + text + ":{2}" + this.GetEchartUint() + "'";
}
//湿度
else if (this.nowSelectObjType == 2)
{
var text = Language.StringByID(R.MyInternationalizationString.uHumidity);
return "'{0} {1}
" + text + ":{2}" + this.GetEchartUint() + "'";
}
//PM2.5
else if (this.nowSelectObjType == 3)
{
var text = Language.StringByID(R.MyInternationalizationString.uHumidity);
return "'{0} {1}
" + text + ":{2}" + this.GetEchartUint() + "
{3}'";
}
//CO²
else if (this.nowSelectObjType == 4)
{
var text = "CO²";
return "'{0} {1}
" + text + ":{2}" + this.GetEchartUint() + "'";
}
return "''";
}
///
/// 特殊处理PM2.5的Tip显示
///
/// mark字符串
/// 值
private void AdjustPM25TipText(ref string strMark, decimal i_value)
{
if (this.nowSelectObjType != 3)
{
//选择的不是PM2.5分支
return;
}
if (0 <= i_value && i_value <= 35)
{
//优
strMark = strMark.Replace("{3}", Language.StringByID(R.MyInternationalizationString.ExcellentAirQuality));
}
else if (35 < i_value && i_value <= 75)
{
//良
strMark = strMark.Replace("{3}", Language.StringByID(R.MyInternationalizationString.GoodAirQuality));
}
else if (75 < i_value && i_value <= 115)
{
//轻度污染
strMark = strMark.Replace("{3}", Language.StringByID(R.MyInternationalizationString.LightPollution));
}
else
{
//重度污染
strMark = strMark.Replace("{3}", Language.StringByID(R.MyInternationalizationString.HeavyPollution));
}
}
///
/// 检测该设备是否拥有温度功能
///
///
///
private bool CheckDeviceHadTemperatrue(DeviceEnumInfo enumInfo)
{
//PM2.5和空气质量传感器都有温度功能
if (enumInfo.ConcreteType == DeviceConcreteType.Sensor_PMTwoPointFive
|| enumInfo.ConcreteType == DeviceConcreteType.AirQualitySensor)
{
return true;
}
var myDevice = this.device as TemperatureSensor;
if (myDevice != null && myDevice.SensorDiv == 1)
{
//温度传感器拥有温度功能
return true;
}
return false;
}
///
/// 检测该设备是否拥有湿度功能
///
///
///
private bool CheckDeviceHadHumidity(DeviceEnumInfo enumInfo)
{
//PM2.5和空气质量传感器都有湿度功能
if (enumInfo.ConcreteType == DeviceConcreteType.Sensor_PMTwoPointFive
|| enumInfo.ConcreteType == DeviceConcreteType.AirQualitySensor)
{
return true;
}
var myDevice = this.device as TemperatureSensor;
if (myDevice != null && myDevice.SensorDiv == 2)
{
//湿度传感器拥有湿度功能
return true;
}
return false;
}
///
/// 检测该设备是否拥有PM2.5的功能
///
///
///
private bool CheckDeviceHadPM25(DeviceEnumInfo enumInfo)
{
//PM2.5和空气质量传感器都有PM2.5的功能
if (enumInfo.ConcreteType == DeviceConcreteType.Sensor_PMTwoPointFive
|| enumInfo.ConcreteType == DeviceConcreteType.AirQualitySensor)
{
return true;
}
return false;
}
///
/// 检测该设备是否拥有CO²的功能
///
///
///
private bool CheckDeviceHadCO2(DeviceEnumInfo enumInfo)
{
//PM2.5和空气质量传感器都有CO²的功能
if (enumInfo.ConcreteType == DeviceConcreteType.Sensor_PMTwoPointFive
|| enumInfo.ConcreteType == DeviceConcreteType.AirQualitySensor)
{
return true;
}
return false;
}
///
/// 转换结果值
///
///
private void ConvertHistoryDataValue(HistoryData data)
{
//云端返回的数据可能带小数点,所以需要把小数点给去掉(强转为int会报错)
int intValue = (int)Convert.ToDecimal(data.Value);
//温度,需要除以100(会出现负数)
if (this.nowSelectObjType == 1)
{
if (intValue > 32767)
{
//负数(特殊处理)
string strValue = (intValue - 65536).ToString();
//小数点需要一位(先把最后一位去掉)
strValue = strValue.Substring(0, strValue.Length - 1);
//然后再插入一个小数点
data.deValue = strValue == string.Empty ? 0m : Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
else
{
//小数点需要一位(先把最后一位去掉)
string strValue = intValue.ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
//然后再插入一个小数点
data.deValue = strValue == string.Empty ? 0m : Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
}
//湿度,需要除以100(不会出现负数)
else if (this.nowSelectObjType == 2)
{
//小数点需要一位(先把最后一位去掉)
string strValue = intValue.ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
//然后再插入一个小数点
data.deValue = strValue == string.Empty ? 0m : Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
else
{
//其他都不用变
data.deValue = Convert.ToDecimal(intValue);
}
}
///
/// 计算平均值
///
///
private decimal CalculateAvgResult(decimal i_valueCount, int i_count)
{
//温度和湿度需要一位小数
if (this.nowSelectObjType == 1 || this.nowSelectObjType == 2)
{
return Math.Round(i_valueCount / i_count, 1);
}
else
{
//不需要小数
return Math.Round(i_valueCount / i_count, 0);
}
}
#endregion
}
}