lixiaojun
2023-03-21 c6c912c9a0b1235805dea4fbb471e26a7f13a47d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using IStation.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.DataDockingApi
{
    /// <summary>
    /// 
    /// </summary>
    public class HandleHelper : IDataDockingApi.IHandleHelper 
    {
        /// <summary>
        /// 
        /// </summary>
        public void HandleData(List<DataDockingConfigure.Mapper> mappers, Action<List<Model.MonitorDataDockingReceiveRecord>> 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<Model.MonitorDataDockingSrcRecord>();
            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<Model.MonitorDataDockingReceiveRecord>();
                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);
            }
 
        }
    }
 
}