using log4net;
using NEIntelligentControl2.Models;
using NEIntelligentControl2.Models.AGC;
using NEIntelligentControl2.Models.Datas;
using NEIntelligentControl2.Models.Messages;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NEIntelligentControl2.Service.AGC
{
///
/// AGC信息管理
///
public class AGCManager
{
private static readonly ILog log = LogManager.GetLogger("File");
///
/// AGC 信息
///
public List AGCInfos;
///
/// 标签点
///
private List _TagInfos;
///
/// 需要读取历史的标签点
///
private List _HistoryTagInfos;
///
/// 所有标签点
///
private string _Tags;
///
/// 数据路径
///
private string _Url;
///
/// 历史数据url
///
private string _HistoyUrl;
///
/// 数据路径
///
private string _DataUrl;
///
/// 信息路径
///
private string _UrlInfo;
private WEBHelper _WEBHelper;
public AGCManager(WEBHelper web)
{
_WEBHelper = web;
InitUrl();
Init();
}
private void InitUrl()
{
try
{
#if (DEBUG)
_UrlInfo = ConfigurationManager.AppSettings["ServicePathDebug"];
_Url = ConfigurationManager.AppSettings["DataServicePathDebug"];
#else
_UrlInfo = ConfigurationManager.AppSettings["ServicePath"];
_Url = ConfigurationManager.AppSettings["DataServicePath"];
#endif
}
catch (Exception ex)
{
Console.WriteLine("读取配置文件[ServicePath]或[DataServicePath]出错!", ex);
}
}
///
/// 初始化
///
private void Init()
{
try
{
_HistoryTagInfos = new List();
_TagInfos = new List();
AGCInfos = _WEBHelper.HttpGetJSON>($"{_UrlInfo}/api/station/agc_net");
InitTags();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
///
/// 初始化标签点
///
private void InitTags()
{
StringBuilder sb = new StringBuilder();
foreach (var v in AGCInfos)
{
FindTags(sb, v.AiPoints);
FindTags(sb, v.DiPoints);
FindHistoryTags(v);
_TagInfos.AddRange(v.AiPoints);
_TagInfos.AddRange(v.DiPoints);
_TagInfos.Add(v.Status);
sb.Append(v.Status.Tag).Append(',');
}
_Tags = sb.ToString();
}
///
/// 枚举需要读取历史的标签点
///
private void FindHistoryTags(AGCInfo ai)
{
_HistoryTagInfos.Add(ai.Status);
var vs = ai.AiPoints.Where(p => (p.Type == TagType.PowerSet || p.Type == TagType.ActualPower) && !string.IsNullOrWhiteSpace(p.Tag));
foreach (var v in vs)
{
_HistoryTagInfos.Add(v);
}
}
///
/// 枚举标签
///
///
///
private void FindTags(StringBuilder sb, List points)
{
if (points == null) return;
foreach (var v in points)
{
if (v == null) continue;
foreach (var vv in v.Tags)
{
if (string.IsNullOrWhiteSpace(vv)) continue;
sb.Append(vv).Append(',');
}
}
}
///
/// 数据刷新
///
public void RefreshData(System.Windows.FrameworkElement fe)
{
try
{
if (_DataUrl == null)
{
_DataUrl = $"{_Url}/ts/latest?keys={_Tags}";
}
var vs = _WEBHelper.HttpGetJSON>(_DataUrl);
fe.Dispatcher.Invoke(() =>
{
try
{
foreach (var v in _TagInfos)
{
v.UpdateData(vs);
}
}
catch (Exception ex)
{
log.Error("曲线上面里" + ex.Message);
}
});
}
catch (Exception ex)
{
log.Error("曲线上面" + ex.Message+ _DataUrl);
}
}
///
/// 历史数据刷新
///
public void RefreshLineData(System.Windows.FrameworkElement fe)
{
try
{
if (_HistoyUrl == null)
{
_HistoyUrl = $"{_Url}/ts/history/snap?interval=60";
}
var dte = DateTime.Now.GetTimeSpan();
var dts = DateTime.Now.AddHours(-8).GetTimeSpan();
foreach (var v in _HistoryTagInfos)
{
if (v.Tag == null) continue;
var vs = _WEBHelper.HttpGetJSON>($"{_HistoyUrl}&tagName={v.Tag}&startTs={dts}&endTs={dte}");
fe.Dispatcher.Invoke(() => {
try {
v.UpdateHistoryData(vs);
}
catch(Exception ex)
{
log.Error("曲线里" + ex.Message);
}
});
}
}
catch (Exception ex)
{
log.Error("曲线" + ex.Message);
}
}
}
}