using IStation.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.DataDockingApi
{
///
///
///
public class HandleHelper : IDataDockingApi.IHandleHelper
{
///
///
///
public void HandleData(List mappers, Action> receive)
{
if (mappers == null || mappers.Count < 1)
return;
var appParas = AppParasHelper.Get();
if (appParas == null)
{
LogHelper.Error("苏州金庭仪表 Api 数据对接中,缺少对接配置文件");
}
string token = null;
token = TokenHelper.Get();
if (string.IsNullOrEmpty(token))
{
LogHelper.Error("苏州金庭仪表 Api 数据对接中,获取Token失败");
return;
}
var url_history = appParas.url.history;
url_history = $"{url_history}?pageNumber=1&startTime={DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}&endTime={DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")}";
var result_history = HttpRequestHelper.History(url_history, token);
if (result_history == null)
{
LogHelper.Info("苏州金庭仪表 Api 数据对接中,获取实时数据失败");
return;
}
if (result_history.status != "SUCCESS")
{
LogHelper.Info("苏州金庭仪表 Api 数据对接中,获取实时数据失败(status!= SUCCESS)");
return;
}
if (result_history.content.result == null || result_history.content.result.Count() < 1)
{
LogHelper.Info("苏州金庭仪表 Api 数据对接中,获取实时数据失败(数据为空)");
return;
}
var items = result_history.content.result.OrderByDescending(x => x.reportTime).ToList();
var code_list = items.Select(x => x.meterCode).Distinct().ToList();
var src_list = new List();
foreach (var metercode in code_list)
{
var record = items.Find(t => t.meterCode == metercode);
if (record != null)
{
if (record.instantFlow != null)
{
var src_item_q = new Model.MonitorDataDockingSrcRecord();
src_item_q.SignId = $"{metercode}_q";
src_item_q.RecordType = eMonitorType.General;
src_item_q.SrcTime = record.reportTime;
src_item_q.SrcValue = record.instantFlow.Value.ToString();
src_list.Add(src_item_q);
}
if (record.cumulativeFlow != null)
{
var src_item_ql = new Model.MonitorDataDockingSrcRecord();
src_item_ql.SignId = $"{metercode}_ql";
src_item_ql.RecordType = eMonitorType.General;
src_item_ql.SrcTime = record.reportTime;
src_item_ql.SrcValue = record.cumulativeFlow.Value.ToString();
src_list.Add(src_item_ql);
}
if (record.pressure != null)
{
var src_item_pr = new Model.MonitorDataDockingSrcRecord();
src_item_pr.SignId = $"{metercode}_pr";
src_item_pr.RecordType = eMonitorType.General;
src_item_pr.SrcTime = record.reportTime;
src_item_pr.SrcValue = record.pressure.Value.ToString();
src_list.Add(src_item_pr);
}
}
}
if (src_list != null && src_list.Count > 0)
{
var record_receive_list = new List();
foreach (var mapper in mappers)
{
var src = src_list.Find(x=>x.SignId==mapper.SignId);
if (src != null)
{
var record = new Model.MonitorDataDockingReceiveRecord() {
SysId=mapper.SysId,
RecordType=src.RecordType,
SrcTime=src.SrcTime,
SrcValue=src.SrcValue
};
record_receive_list.Add(record);
}
}
receive(record_receive_list);
}
if (appParas.debug)
{
DebugHelper.Debug(src_list);
}
}
}
}