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 EnergyMainPage : FrameLayout
{
static EnergyMainPage bodyView;
///
/// 顶部区域
///
FrameLayout generalTableView;
///
/// 实时功耗
///
Button btnRealTimeData;
///
/// 实时功耗单位
///
Button btnTotalValueUint;
///
/// 总功耗
///
TextButton btnTotalValue;
///
/// 历史数据区域
///
FrameLayout diagramView;
///
/// 曲线图
///
MyEchartsViewOn myLineEchartsView;
///
/// 曲线图数据string
///
EchartsOption_BrokenLine brokenLine;
///
/// 曲线图下拉按钮
///
Button btnDrodDown;
///
/// 插查询的能源列表
///
List queryList = new List();
///
/// 查询条件
/// hour、week、month
///
string curQueryType = "hour";
///
/// 能源列表区域
///
FrameLayout energyListView;
///
/// 历史数据图表
///
MyEchartsViewOn myEchartsView;
///
/// 曲线图是否展开
///
bool isUnfoldOrStow = false;
List energyList = new List();
List colorList = new List(){0xFF80AEFF, 0xFFFFD154, 0xFFFF9D54, 0xFFFE6A6A, 0xFFB183C3, 0xFFADE764,
0xFFD7504B, 0xFFC6E579, 0xFFF4E001, 0xFFF0805A, 0xFF26C0C0};
List colorList2 = new List() {
"#80AEFF", "#FFD154", "#FF9D54", "#FE6A6A", "#B183C3", "#ADE764",
"#D7504B", "#C6E579", "#F4E001", "#F0805A", "#26C0C0"};
///
/// 主页
///
public EnergyMainPage()
{
bodyView = this;
energyList = FunctionList.List.GetEnergyList();
}
///
///
///
public void LoadPage()
{
new TopViewDiv(bodyView, Language.StringByID(StringId.EnergyMonitoring)).LoadTopView();
bodyView.BackgroundColor = CSS_Color.BackgroundColor;
var contentView = new VerticalScrolViewLayout()
{
Y = Application.GetRealHeight(64),
Height = Application.GetRealHeight(603),
};
bodyView.AddChidren(contentView);
#region 顶部view
generalTableView = new FrameLayout()
{
Gravity = Gravity.CenterHorizontal,
Y = Application.GetRealHeight(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 = "000"
};
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,
};
generalTableView.AddChidren(btnRealTimeData);
UpdataValue();
var echartsView = new FrameLayout()
{
Width = Application.GetRealWidth(118),
Height = Application.GetRealWidth(118),
X = Application.GetRealWidth(227),
//Y = Application.GetRealWidth(16),
};
generalTableView.AddChidren(echartsView);
myEchartsView = new MyEchartsViewOn()
{
Width = Application.GetRealWidth(118),
Height = Application.GetRealWidth(118),
};
echartsView.AddChidren(myEchartsView);
Dictionary list = new Dictionary();
foreach(var ene in energyList)
{
double tt = 0;
double.TryParse(ene.GetAttrState(FunctionAttributeKey.TotalElectricity), out tt);
tt *= 100;//乘以100,小于1无法加载echart
list.Add(ene.name, tt.ToString());
}
var echartsPie = new EchartsOption_Pie();
var echartRootJson = echartsPie.InitDateJson(list);
//var echartRootJsonString = Newtonsoft.Json.JsonConvert.SerializeObject(echartRootJson);
myEchartsView.ShowWithOption(echartRootJson);
btnDrodDown = new Button()
{
Y = Application.GetRealWidth(118),
Width = Application.GetRealWidth(343),
Height = Application.GetRealWidth(30),
UnSelectedImagePath = "FunctionIcon/Energy/EnergyDrodDownIcon.png",
SelectedImagePath = "FunctionIcon/Energy/EnergyDrodUpIcon.png",
};
initDiagramView();
generalTableView.AddChidren(btnDrodDown);
btnDrodDown.MouseUpEventHandler = (sender, e) => {
isUnfoldOrStow = btnDrodDown.IsSelected = !btnDrodDown.IsSelected;
showDiagramView();
};
#endregion
energyListView = new FrameLayout()
{
Y = generalTableView.Bottom + Application.GetRealWidth(8),
Height = Application.GetRealWidth(116 * energyList.Count + 50),
};
contentView.AddChidren(energyListView);
int index = 0;
foreach (var energy in energyList)
{
EnergyRow(energy, energyListView, index);
index++;
Control.Ins.SendReadCommand(energy);
}
}
///
///
///
void showDiagramView()
{
if (isUnfoldOrStow)//展开
{
generalTableView.Height = Application.GetRealHeight(419);
btnDrodDown.Y = Application.GetRealWidth(389);
btnDrodDown.IsSelected = true;
}
else
{
generalTableView.Height = Application.GetRealHeight(148);
btnDrodDown.Y = Application.GetRealWidth(118);
btnDrodDown.IsSelected = false;
}
energyListView.Y = generalTableView.Bottom + Application.GetRealWidth(8);
}
///
/// 加载曲线图
///
///
void initDiagramView()
{
brokenLine = new EchartsOption_BrokenLine();
diagramView = new FrameLayout()
{
Y = Application.GetRealHeight(136),
Gravity = Gravity.CenterHorizontal,
Width = Application.GetRealWidth(343),
Height = Application.GetRealWidth(241),
BackgroundColor = CSS_Color.MainBackgroundColor,
Radius = (uint)Application.GetRealWidth(12),
BorderColor = 0x00000000,
BorderWidth = 0,
};
generalTableView.AddChidren(diagramView);
#region 选择数据日期范围
var showDataTypeView = new FrameLayout()
{
X = Application.GetRealWidth(183),
Y = Application.GetRealHeight(12),
Width = Application.GetRealWidth(144),
Height = Application.GetRealHeight(24),
BackgroundImagePath = "FunctionIcon/EnvironmentalScience/SensorHistoryBg1.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 = DateTime.Now.Month.ToString() + Language.StringByID(StringId.month),
};
showDataTypeView.AddChidren(btnShowHistroyData_Month);
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";
brokenLine.YvalueText = "";
foreach (var enery in queryList)
{
LoadMothed_GetHistoryData(enery,false);
}
};
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";
brokenLine.YvalueText = "";
foreach (var enery in queryList)
{
LoadMothed_GetHistoryData(enery, false);
}
};
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";
brokenLine.YvalueText = "";
foreach (var enery in queryList)
{
LoadMothed_GetHistoryData(enery, false);
}
};
#endregion
var historyDataView = new FrameLayout()
{
Gravity = Gravity.CenterHorizontal,
Y = Application.GetRealHeight(52),
//Width = Application.GetRealWidth(343 - 32),
Height = Application.GetRealHeight(178),
};
diagramView.AddChidren(historyDataView);
myEchartsView = new MyEchartsViewOn() {
Y = Application.GetRealHeight(10),
Height = Application.GetRealHeight(150),
};
historyDataView.AddChidren(myEchartsView);
}
///
/// 读取历史数据
///
void LoadMothed_GetHistoryData(Function sr,bool isDel)
{
if(isDel)//删除数据现实
{
brokenLine.YvalueText = "";
foreach (var ene in queryList)
{
brokenLine.YvalueText += ene.GetAttrState("ydata") + ",";
}
Application.RunOnMainThread(() =>
{
myEchartsView.ShowWithOption(brokenLine.InitOption());
});
return;
}
var loadPage = new Loading()
{
LodingBackgroundColor = 0x88888888,
};
diagramView.AddChidren(loadPage);
new System.Threading.Thread(() =>
{
try
{
Application.RunOnMainThread(() =>
{
loadPage.Start(Language.StringByID(StringId.PleaseWait));
});
var sensorType = sr.spk.Split(".")[1];
var revertObj = new HttpServerRequest().GetSensorHistory(curQueryType, sr.deviceId, "total_electricity");
if (revertObj != null)
{
if (revertObj.Code == StateCode.SUCCESS)
{
var revertData = Newtonsoft.Json.JsonConvert.DeserializeObject>(revertObj.Data.ToString());
List vs = new List();
if (brokenLine.YvalueText == "")
{
foreach (var data in revertData)
{
vs.Add(data.fieldName);
}
brokenLine.InitXdataText(vs);
if (sr.GetAttribute("ydata") == null)
{
sr.attributes.Add(new FunctionAttributes() { key = "ydata" });
}
sr.SetAttrState("ydata", brokenLine.InitYdataText(sr.name, revertData, sr.GetAttrState("color")) + ",");
}
else
{
if (sr.GetAttribute("ydata") == null)
{
sr.attributes.Add(new FunctionAttributes() { key = "ydata" });
}
sr.SetAttrState("ydata", brokenLine.InitYdataText(sr.name, revertData, sr.GetAttrState("color"))+",");
}
}
}
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.ShowWithOption(opString);
});
}
catch (Exception ex)
{
MainPage.Log($"sensor history error : {ex.Message}");
}
finally
{
Application.RunOnMainThread(() =>
{
loadPage.Hide();
});
}
})
{ IsBackground = true }.Start();
}
///
///
///
public void EnergyRow(Function energy, FrameLayout view, int index)
{
var btnLine = new Button()
{
Y = Application.GetRealWidth(116 * index),
Height = Application.GetRealWidth(16),
};
view.AddChidren(btnLine);
//定义代表色
energy.attributes.Add(new FunctionAttributes() { key = "color", state = colorList2[index] });
var row = new EnergyRow(energy, colorList[index]);
row.SetHistory_Action = (isAdd) =>
{
if (isAdd)
{
if(!isUnfoldOrStow)//如果曲线图没有加载出来,则自动加载出来
{
isUnfoldOrStow = true;
showDiagramView();
}
queryList.Add(row.function);
LoadMothed_GetHistoryData(row.function, false);
}
else
{
queryList.Remove(row.function);
LoadMothed_GetHistoryData(row.function, true);
if (queryList.Count == 0)//如果查询列表为0,自动收起曲线图
{
isUnfoldOrStow = false;
showDiagramView();
}
}
};
row.Y = btnLine.Bottom;
view.AddChidren(row);
listButton_value.Add(row.btnValue);
Control.Ins.SendReadCommand(energy);
}
public List